0
$\begingroup$
image_datagen.flow_from_directory(
    directory=src_path_train,
    target_size=(100, 100),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    subset='training',
    shuffle=True,
    seed=42
)

What does shuffle in the code snippet mean? Does this indicate that the flow_from_directory function shuffles the images before loading them? if so, how does it help the training procedure?

Again, I'm reading an article where the shuffle setting is True for training and validation but False for testing. Why is this different for testing?

train_generator = image_datagen.flow_from_directory(
    directory=src_path_train,
    target_size=(100, 100),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    subset='training',
    shuffle=True,
    seed=42
)
valid_generator = image_datagen.flow_from_directory(
    directory=src_path_train,
    target_size=(100, 100),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    subset='validation',
    shuffle=True,
    seed=42
)
test_generator = test_datagen.flow_from_directory(
    directory=src_path_test,
    target_size=(100, 100),
    color_mode="rgb",
    batch_size=1,
    class_mode=None,
    shuffle=False,
    seed=42
)

the above code snippet is taken from the article where the shuffle setting is True for training and validation but False for testing.

$\endgroup$

2 Answers 2

2
$\begingroup$

When shuffle = True your dataset will be randomly shuffled to avoid any overfitting in training. Passing samples in different orders makes the model more robust to overfitting. That's why during training it is advisable to turn on shuffling while during inference (validation/test), you only need to get the output, no training. Hence, no shuffling.

Even though everything is random here, you can still reproduce your result using the seed parameter. It will reproduce the same result every time. If you don't use seed, then at every run, your model will be different and you cannot reproduce the results.

$\endgroup$
1
$\begingroup$

Shuffle leads to more representative learning. In any batch, there are more chances of different class examples than sampling done without shuffle . Like in deck of cards, if you shuffle chances of same card number ocuuring together reduces . So training is robust but I don’t think it has to relate to overfitting .

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.