How to let predict_generator make ordered prediction in deep learning?












1















I am designing a deep learning model to classify images and I am using the following code to check the predication performance. It compares the images' labels with the predicted classes and then returns the error predication.



I was using sequential model, and this code was working fine. But, now it does not work fine.. predict_generator changes the order of the images. (predict_classes and validation_generator are not in the same order)



I want to make the prediction in the same order of the images in validation_generator, Do you know how can I do that??



validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(image_size, image_size),
batch_size=val_batchsize,
class_mode='categorical',
shuffle=False)

# Get the filenames from the generator
fnames = validation_generator.filenames

# Get the ground truth from generator
ground_truth = validation_generator.classes

# Get the label to class mapping from the generator
label2index = validation_generator.class_indices

# Getting the mapping from class index to class label
idx2label = dict((v,k) for k,v in label2index.items())

# Get the predictions from the model using the generator
predictions = model.predict_generator(validation_generator, steps=validation_generator.samples/validation_generator.batch_size,verbose=1)
predicted_classes = np.argmax(predictions,axis=1)

errors = np.where(predicted_classes != ground_truth)[0]
print("No of errors = {}/{}".format(len(errors),validation_generator.samples))

# Show the errors
for i in range(len(errors)):
pred_class = np.argmax(predictions[errors[i]])
pred_label = idx2label[pred_class]

title = 'Original label:{}, Prediction :{}, confidence : {:.3f}'.format(
fnames[errors[i]].split('/')[0],
pred_label,
predictions[errors[i]][pred_class])

original = load_img('{}/{}'.format(validation_dir,fnames[errors[i]]))
plt.figure(figsize=[7,7])
plt.axis('off')
plt.title(title)
plt.imshow(original)
plt.show()


The model code



from keras.engine import  Model
vgg16_model = keras.applications.vgg16.VGG16()

input_layer2 = vgg16_model.input
vgg16_model.get_layer(index = 0).name = 'input2'

last_layer2 = vgg16_model.get_layer('block4_pool').output

x = Conv2D(512, (3, 3), activation='relu', name='block5_conv1', padding='same')(last_layer2)
x = BatchNormalization(name='bn1')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv2', padding='same')(x)
x = BatchNormalization(name='bn2')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv3', padding='same')(x)
x = BatchNormalization(name='bn3')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name = 'block5_pool')(x)
x = Flatten(name = 'flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
out2 = Dropout(0.3, name='dropout1')(x)

#Classification layer
output_layer2 = Dense(class_no, activation='softmax', name='prediction')(out2)
vgg16_face_model = Model(input_layer2, output_layer2)

vgg16_face_model.trainable = True
set_trainable = False
for layer in vgg16_face_model.layers:
if layer.name == 'block5_conv1':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False


Please Help me..










share|improve this question

























  • Where is the model?

    – Milo Lu
    Nov 22 '18 at 1:28











  • I have added the model .. please check it ..

    – Noran
    Nov 22 '18 at 6:52
















1















I am designing a deep learning model to classify images and I am using the following code to check the predication performance. It compares the images' labels with the predicted classes and then returns the error predication.



I was using sequential model, and this code was working fine. But, now it does not work fine.. predict_generator changes the order of the images. (predict_classes and validation_generator are not in the same order)



I want to make the prediction in the same order of the images in validation_generator, Do you know how can I do that??



validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(image_size, image_size),
batch_size=val_batchsize,
class_mode='categorical',
shuffle=False)

# Get the filenames from the generator
fnames = validation_generator.filenames

# Get the ground truth from generator
ground_truth = validation_generator.classes

# Get the label to class mapping from the generator
label2index = validation_generator.class_indices

# Getting the mapping from class index to class label
idx2label = dict((v,k) for k,v in label2index.items())

# Get the predictions from the model using the generator
predictions = model.predict_generator(validation_generator, steps=validation_generator.samples/validation_generator.batch_size,verbose=1)
predicted_classes = np.argmax(predictions,axis=1)

errors = np.where(predicted_classes != ground_truth)[0]
print("No of errors = {}/{}".format(len(errors),validation_generator.samples))

# Show the errors
for i in range(len(errors)):
pred_class = np.argmax(predictions[errors[i]])
pred_label = idx2label[pred_class]

title = 'Original label:{}, Prediction :{}, confidence : {:.3f}'.format(
fnames[errors[i]].split('/')[0],
pred_label,
predictions[errors[i]][pred_class])

original = load_img('{}/{}'.format(validation_dir,fnames[errors[i]]))
plt.figure(figsize=[7,7])
plt.axis('off')
plt.title(title)
plt.imshow(original)
plt.show()


The model code



from keras.engine import  Model
vgg16_model = keras.applications.vgg16.VGG16()

input_layer2 = vgg16_model.input
vgg16_model.get_layer(index = 0).name = 'input2'

last_layer2 = vgg16_model.get_layer('block4_pool').output

x = Conv2D(512, (3, 3), activation='relu', name='block5_conv1', padding='same')(last_layer2)
x = BatchNormalization(name='bn1')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv2', padding='same')(x)
x = BatchNormalization(name='bn2')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv3', padding='same')(x)
x = BatchNormalization(name='bn3')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name = 'block5_pool')(x)
x = Flatten(name = 'flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
out2 = Dropout(0.3, name='dropout1')(x)

#Classification layer
output_layer2 = Dense(class_no, activation='softmax', name='prediction')(out2)
vgg16_face_model = Model(input_layer2, output_layer2)

vgg16_face_model.trainable = True
set_trainable = False
for layer in vgg16_face_model.layers:
if layer.name == 'block5_conv1':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False


Please Help me..










share|improve this question

























  • Where is the model?

    – Milo Lu
    Nov 22 '18 at 1:28











  • I have added the model .. please check it ..

    – Noran
    Nov 22 '18 at 6:52














1












1








1








I am designing a deep learning model to classify images and I am using the following code to check the predication performance. It compares the images' labels with the predicted classes and then returns the error predication.



I was using sequential model, and this code was working fine. But, now it does not work fine.. predict_generator changes the order of the images. (predict_classes and validation_generator are not in the same order)



I want to make the prediction in the same order of the images in validation_generator, Do you know how can I do that??



validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(image_size, image_size),
batch_size=val_batchsize,
class_mode='categorical',
shuffle=False)

# Get the filenames from the generator
fnames = validation_generator.filenames

# Get the ground truth from generator
ground_truth = validation_generator.classes

# Get the label to class mapping from the generator
label2index = validation_generator.class_indices

# Getting the mapping from class index to class label
idx2label = dict((v,k) for k,v in label2index.items())

# Get the predictions from the model using the generator
predictions = model.predict_generator(validation_generator, steps=validation_generator.samples/validation_generator.batch_size,verbose=1)
predicted_classes = np.argmax(predictions,axis=1)

errors = np.where(predicted_classes != ground_truth)[0]
print("No of errors = {}/{}".format(len(errors),validation_generator.samples))

# Show the errors
for i in range(len(errors)):
pred_class = np.argmax(predictions[errors[i]])
pred_label = idx2label[pred_class]

title = 'Original label:{}, Prediction :{}, confidence : {:.3f}'.format(
fnames[errors[i]].split('/')[0],
pred_label,
predictions[errors[i]][pred_class])

original = load_img('{}/{}'.format(validation_dir,fnames[errors[i]]))
plt.figure(figsize=[7,7])
plt.axis('off')
plt.title(title)
plt.imshow(original)
plt.show()


The model code



from keras.engine import  Model
vgg16_model = keras.applications.vgg16.VGG16()

input_layer2 = vgg16_model.input
vgg16_model.get_layer(index = 0).name = 'input2'

last_layer2 = vgg16_model.get_layer('block4_pool').output

x = Conv2D(512, (3, 3), activation='relu', name='block5_conv1', padding='same')(last_layer2)
x = BatchNormalization(name='bn1')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv2', padding='same')(x)
x = BatchNormalization(name='bn2')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv3', padding='same')(x)
x = BatchNormalization(name='bn3')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name = 'block5_pool')(x)
x = Flatten(name = 'flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
out2 = Dropout(0.3, name='dropout1')(x)

#Classification layer
output_layer2 = Dense(class_no, activation='softmax', name='prediction')(out2)
vgg16_face_model = Model(input_layer2, output_layer2)

vgg16_face_model.trainable = True
set_trainable = False
for layer in vgg16_face_model.layers:
if layer.name == 'block5_conv1':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False


Please Help me..










share|improve this question
















I am designing a deep learning model to classify images and I am using the following code to check the predication performance. It compares the images' labels with the predicted classes and then returns the error predication.



I was using sequential model, and this code was working fine. But, now it does not work fine.. predict_generator changes the order of the images. (predict_classes and validation_generator are not in the same order)



I want to make the prediction in the same order of the images in validation_generator, Do you know how can I do that??



validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(image_size, image_size),
batch_size=val_batchsize,
class_mode='categorical',
shuffle=False)

# Get the filenames from the generator
fnames = validation_generator.filenames

# Get the ground truth from generator
ground_truth = validation_generator.classes

# Get the label to class mapping from the generator
label2index = validation_generator.class_indices

# Getting the mapping from class index to class label
idx2label = dict((v,k) for k,v in label2index.items())

# Get the predictions from the model using the generator
predictions = model.predict_generator(validation_generator, steps=validation_generator.samples/validation_generator.batch_size,verbose=1)
predicted_classes = np.argmax(predictions,axis=1)

errors = np.where(predicted_classes != ground_truth)[0]
print("No of errors = {}/{}".format(len(errors),validation_generator.samples))

# Show the errors
for i in range(len(errors)):
pred_class = np.argmax(predictions[errors[i]])
pred_label = idx2label[pred_class]

title = 'Original label:{}, Prediction :{}, confidence : {:.3f}'.format(
fnames[errors[i]].split('/')[0],
pred_label,
predictions[errors[i]][pred_class])

original = load_img('{}/{}'.format(validation_dir,fnames[errors[i]]))
plt.figure(figsize=[7,7])
plt.axis('off')
plt.title(title)
plt.imshow(original)
plt.show()


The model code



from keras.engine import  Model
vgg16_model = keras.applications.vgg16.VGG16()

input_layer2 = vgg16_model.input
vgg16_model.get_layer(index = 0).name = 'input2'

last_layer2 = vgg16_model.get_layer('block4_pool').output

x = Conv2D(512, (3, 3), activation='relu', name='block5_conv1', padding='same')(last_layer2)
x = BatchNormalization(name='bn1')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv2', padding='same')(x)
x = BatchNormalization(name='bn2')(x)
x = Conv2D(512, (3, 3), activation='relu', name='block5_conv3', padding='same')(x)
x = BatchNormalization(name='bn3')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name = 'block5_pool')(x)
x = Flatten(name = 'flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
out2 = Dropout(0.3, name='dropout1')(x)

#Classification layer
output_layer2 = Dense(class_no, activation='softmax', name='prediction')(out2)
vgg16_face_model = Model(input_layer2, output_layer2)

vgg16_face_model.trainable = True
set_trainable = False
for layer in vgg16_face_model.layers:
if layer.name == 'block5_conv1':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False


Please Help me..







python tensorflow machine-learning keras neural-network






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:20







Noran

















asked Nov 21 '18 at 21:59









NoranNoran

1999




1999













  • Where is the model?

    – Milo Lu
    Nov 22 '18 at 1:28











  • I have added the model .. please check it ..

    – Noran
    Nov 22 '18 at 6:52



















  • Where is the model?

    – Milo Lu
    Nov 22 '18 at 1:28











  • I have added the model .. please check it ..

    – Noran
    Nov 22 '18 at 6:52

















Where is the model?

– Milo Lu
Nov 22 '18 at 1:28





Where is the model?

– Milo Lu
Nov 22 '18 at 1:28













I have added the model .. please check it ..

– Noran
Nov 22 '18 at 6:52





I have added the model .. please check it ..

– Noran
Nov 22 '18 at 6:52












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421050%2fhow-to-let-predict-generator-make-ordered-prediction-in-deep-learning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421050%2fhow-to-let-predict-generator-make-ordered-prediction-in-deep-learning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith