Why is the Keras model always predicting the same class / How can I improve the accuracy of this model?
$begingroup$
First post here. I'm working on a project about multi-class image classification and created a python script using Keras to train a model with transfer learning. To my dismay the model has always predicted the same class, I've simplified the model down to 3 image classes (I'm using a kaggle food image stock with 800 training samples and 800 validation samples per class plus image reformatting) and tried different optimizers, yet it still comes down to the same class while the model also apparently only has an accuracy of ~0.2563 at 25 epochs of training. I've posted the code below, how can I improve the accuracy of this script and solve the same predicted class problem?
import pandas as pd
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense, GlobalAveragePooling2D
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras import optimizers
from keras import applications
from keras.applications.vgg16 import preprocess_input
img_classes = 3
base_model = applications.VGG16(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(img_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)
for i, layer in enumerate(model.layers):
print(i, layer.name)
for layer in model.layers[:25]:
layer.trainable = False
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input)
train_generator = train_datagen.flow_from_directory('./food-101/bigtrain',
target_size=(128, 128),
color_mode='rgb',
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
val_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input,)
val_generator = val_datagen.flow_from_directory(
'./food-101/bigval',
target_size=(128, 128),
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
# model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.compile(optimizer=optimizers.SGD(lr=0.00001,
momentum=0.9,
decay=0.0001,
nesterov=True), loss='categorical_crossentropy', metrics=['accuracy'])
batch_size = 1
validation_steps = 64 // batch_size
step_size_train = train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
steps_per_epoch=step_size_train,
epochs=25,
validation_data=val_generator,
validation_steps=validation_steps)
model.save('./test_try_vgg_9.h5')
Example prediction results:
classes: apple_pie, churros, miso_soup
miso soup
[0.3202575 0.48074356 0.19899891] rmsprop
[0.45246536 0.4505403 0.09699439] sgd
churros
[0.37473327 0.35784692 0.2674198 ] rmsprop
[0.4145825 0.465228 0.12018944] sgd
This is the prediction script:
from keras.models import load_model
from keras import optimizers
from keras.preprocessing import image
import numpy as np
from keras.applications.vgg16 import preprocess_input
# dimensions of our images
img_width, img_height = 512, 512
# load model
model = load_model('./test_try_vgg_9.h5')
# predicting images
img = image.load_img('./food-101/training/apple_pie/551535.jpg')
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
print("Probability: ")
print(pred[0])
keras multiclass-classification
New contributor
$endgroup$
add a comment |
$begingroup$
First post here. I'm working on a project about multi-class image classification and created a python script using Keras to train a model with transfer learning. To my dismay the model has always predicted the same class, I've simplified the model down to 3 image classes (I'm using a kaggle food image stock with 800 training samples and 800 validation samples per class plus image reformatting) and tried different optimizers, yet it still comes down to the same class while the model also apparently only has an accuracy of ~0.2563 at 25 epochs of training. I've posted the code below, how can I improve the accuracy of this script and solve the same predicted class problem?
import pandas as pd
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense, GlobalAveragePooling2D
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras import optimizers
from keras import applications
from keras.applications.vgg16 import preprocess_input
img_classes = 3
base_model = applications.VGG16(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(img_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)
for i, layer in enumerate(model.layers):
print(i, layer.name)
for layer in model.layers[:25]:
layer.trainable = False
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input)
train_generator = train_datagen.flow_from_directory('./food-101/bigtrain',
target_size=(128, 128),
color_mode='rgb',
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
val_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input,)
val_generator = val_datagen.flow_from_directory(
'./food-101/bigval',
target_size=(128, 128),
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
# model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.compile(optimizer=optimizers.SGD(lr=0.00001,
momentum=0.9,
decay=0.0001,
nesterov=True), loss='categorical_crossentropy', metrics=['accuracy'])
batch_size = 1
validation_steps = 64 // batch_size
step_size_train = train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
steps_per_epoch=step_size_train,
epochs=25,
validation_data=val_generator,
validation_steps=validation_steps)
model.save('./test_try_vgg_9.h5')
Example prediction results:
classes: apple_pie, churros, miso_soup
miso soup
[0.3202575 0.48074356 0.19899891] rmsprop
[0.45246536 0.4505403 0.09699439] sgd
churros
[0.37473327 0.35784692 0.2674198 ] rmsprop
[0.4145825 0.465228 0.12018944] sgd
This is the prediction script:
from keras.models import load_model
from keras import optimizers
from keras.preprocessing import image
import numpy as np
from keras.applications.vgg16 import preprocess_input
# dimensions of our images
img_width, img_height = 512, 512
# load model
model = load_model('./test_try_vgg_9.h5')
# predicting images
img = image.load_img('./food-101/training/apple_pie/551535.jpg')
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
print("Probability: ")
print(pred[0])
keras multiclass-classification
New contributor
$endgroup$
add a comment |
$begingroup$
First post here. I'm working on a project about multi-class image classification and created a python script using Keras to train a model with transfer learning. To my dismay the model has always predicted the same class, I've simplified the model down to 3 image classes (I'm using a kaggle food image stock with 800 training samples and 800 validation samples per class plus image reformatting) and tried different optimizers, yet it still comes down to the same class while the model also apparently only has an accuracy of ~0.2563 at 25 epochs of training. I've posted the code below, how can I improve the accuracy of this script and solve the same predicted class problem?
import pandas as pd
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense, GlobalAveragePooling2D
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras import optimizers
from keras import applications
from keras.applications.vgg16 import preprocess_input
img_classes = 3
base_model = applications.VGG16(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(img_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)
for i, layer in enumerate(model.layers):
print(i, layer.name)
for layer in model.layers[:25]:
layer.trainable = False
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input)
train_generator = train_datagen.flow_from_directory('./food-101/bigtrain',
target_size=(128, 128),
color_mode='rgb',
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
val_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input,)
val_generator = val_datagen.flow_from_directory(
'./food-101/bigval',
target_size=(128, 128),
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
# model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.compile(optimizer=optimizers.SGD(lr=0.00001,
momentum=0.9,
decay=0.0001,
nesterov=True), loss='categorical_crossentropy', metrics=['accuracy'])
batch_size = 1
validation_steps = 64 // batch_size
step_size_train = train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
steps_per_epoch=step_size_train,
epochs=25,
validation_data=val_generator,
validation_steps=validation_steps)
model.save('./test_try_vgg_9.h5')
Example prediction results:
classes: apple_pie, churros, miso_soup
miso soup
[0.3202575 0.48074356 0.19899891] rmsprop
[0.45246536 0.4505403 0.09699439] sgd
churros
[0.37473327 0.35784692 0.2674198 ] rmsprop
[0.4145825 0.465228 0.12018944] sgd
This is the prediction script:
from keras.models import load_model
from keras import optimizers
from keras.preprocessing import image
import numpy as np
from keras.applications.vgg16 import preprocess_input
# dimensions of our images
img_width, img_height = 512, 512
# load model
model = load_model('./test_try_vgg_9.h5')
# predicting images
img = image.load_img('./food-101/training/apple_pie/551535.jpg')
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
print("Probability: ")
print(pred[0])
keras multiclass-classification
New contributor
$endgroup$
First post here. I'm working on a project about multi-class image classification and created a python script using Keras to train a model with transfer learning. To my dismay the model has always predicted the same class, I've simplified the model down to 3 image classes (I'm using a kaggle food image stock with 800 training samples and 800 validation samples per class plus image reformatting) and tried different optimizers, yet it still comes down to the same class while the model also apparently only has an accuracy of ~0.2563 at 25 epochs of training. I've posted the code below, how can I improve the accuracy of this script and solve the same predicted class problem?
import pandas as pd
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense, GlobalAveragePooling2D
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras import optimizers
from keras import applications
from keras.applications.vgg16 import preprocess_input
img_classes = 3
base_model = applications.VGG16(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(img_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)
for i, layer in enumerate(model.layers):
print(i, layer.name)
for layer in model.layers[:25]:
layer.trainable = False
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input)
train_generator = train_datagen.flow_from_directory('./food-101/bigtrain',
target_size=(128, 128),
color_mode='rgb',
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
val_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input,)
val_generator = val_datagen.flow_from_directory(
'./food-101/bigval',
target_size=(128, 128),
classes=['apple_pie', 'churros', 'miso_soup'],
batch_size=1,
class_mode='categorical',
shuffle=True)
# model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.compile(optimizer=optimizers.SGD(lr=0.00001,
momentum=0.9,
decay=0.0001,
nesterov=True), loss='categorical_crossentropy', metrics=['accuracy'])
batch_size = 1
validation_steps = 64 // batch_size
step_size_train = train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
steps_per_epoch=step_size_train,
epochs=25,
validation_data=val_generator,
validation_steps=validation_steps)
model.save('./test_try_vgg_9.h5')
Example prediction results:
classes: apple_pie, churros, miso_soup
miso soup
[0.3202575 0.48074356 0.19899891] rmsprop
[0.45246536 0.4505403 0.09699439] sgd
churros
[0.37473327 0.35784692 0.2674198 ] rmsprop
[0.4145825 0.465228 0.12018944] sgd
This is the prediction script:
from keras.models import load_model
from keras import optimizers
from keras.preprocessing import image
import numpy as np
from keras.applications.vgg16 import preprocess_input
# dimensions of our images
img_width, img_height = 512, 512
# load model
model = load_model('./test_try_vgg_9.h5')
# predicting images
img = image.load_img('./food-101/training/apple_pie/551535.jpg')
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
print("Probability: ")
print(pred[0])
keras multiclass-classification
keras multiclass-classification
New contributor
New contributor
New contributor
asked 14 mins ago
vanillinxvanillinx
1
1
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
vanillinx is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f44595%2fwhy-is-the-keras-model-always-predicting-the-same-class-how-can-i-improve-the%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
vanillinx is a new contributor. Be nice, and check out our Code of Conduct.
vanillinx is a new contributor. Be nice, and check out our Code of Conduct.
vanillinx is a new contributor. Be nice, and check out our Code of Conduct.
vanillinx is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f44595%2fwhy-is-the-keras-model-always-predicting-the-same-class-how-can-i-improve-the%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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