'why model.predict() does not return the same prediction when I pass into it an ImageDataGenerator or the same data unit by unit

In the first time, I passed an ImageDataGenerator to my model.predict() :

def import_dataset(path,img_size):
    dataset = ImageDataGenerator(rescale=1./255).flow_from_directory(
                    directory=path,
                   target_size=img_size,
                   shuffle=True,
                   batch_size=32
                   )
    return dataset
path_test= "C:/Users/am.reguig/Desktop/bd_images_IA/bd_images_test/bd_pour_modele/acoustique/test"
size=(450,300)
test_batches= import_dataset(path_test,size)
model = load_model('C:/Users/am.reguig/Desktop/codes/incepV3_imgsize=450_300_epochs=6_lr=0.0001_trainable_layers=5_units=256___512.h5')
prediction_inception=model.predict(
        x=test_batches,                         # les données de test\n",
        #steps=len(test_batches),                # nombre de pas pas époche\n",
        verbose=1)         
print(np.argmax(prediction_inception, axis=-1)) 

so I get this result :

Found 89 images belonging to 3 classes.
3/3 [==============================] - 5s 1s/step
[1 1 1 1 1 1 1 1 0 1 1 1 1 1 2 1 1 0 1 2 1 1 1 1 1 2 1 1 2 1 2 1 1 1 1 1 1
1 1 2 1 1 1 1 1 1 1 0 2 1 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 0 1 2 1 1 1 1 1 1
1 2 2 1 2 1 0 1 1 1 1 1 0 1 2]

In the second time, I passed my dataset unit by unit :

model = load_model('C:/Users/am.reguig/Desktop/codes/incepV3_imgsize=450_300_epochs=6_lr=0.0001_trainable_layers=5_units=256___512.h5')
l=[]    
for indx in range(0,3):
    all_classes = ['Chantier_images/', 'Ferroviaire_images/', 'Routier_images/']
    classe = all_classes[indx]
    path_dir = 'C:/Users/am.reguig/Desktop/bd_images_IA/bd_images_test/bd_pour_modele/acoustique/test/'
    parent_dir_class = path_dir + classe
    ii = 0 
    for filename in glob.glob(os.path.join(parent_dir_class, '*.jpg')):
        image = load_img(filename, target_size=(450, 300, 3))
        image = img_to_array(image)
        image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
        image = preprocess_input(image)
        y_prob = model.predict(image)

        l.append(np.argmax(y_prob, axis=-1)[0])
        if (y_prob[0][indx]>0.80):
            ii+=1
    print(l)

I get this :

[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

I want to indrestand why I did not get the same results



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source