Same prediction for all inputs with inception model
See original GitHub issueHi everyone! I trained an inception model with my custom images (dataset of 8.000 images), then saved it. And when I try to make predictions with it, whatever is the input images to predict, it’s the same output! I checked everything, and all was seeming good to me…
# Here is the code to train the model `import os import numpy as np import pandas as pd from scipy.misc import imread from scipy.misc import imresize from inception_v3 import InceptionV3 from keras.preprocessing import image from keras.models import Model from keras.layers import Dense, GlobalAveragePooling2D from keras import backend as K import keras from keras.callbacks import ModelCheckpoint root_dir = os.path.abspath(‘.’) data_dir = os.path.join(root_dir, ‘data’) os.path.exists(root_dir) os.path.exists(data_dir) nb_epoch = int(raw_input("Nb epoch ? : ")) print (‘…Debut parsing et resize…’) train = pd.read_csv(os.path.join(data_dir, ‘images’, ‘test.csv’)) train.head() temp = [] for img_name in train.filename: image_path = os.path.join(data_dir, ‘images’, img_name) img_tmp = imread(image_path, mode=‘RGB’) img = imresize(img_tmp, (299, 299)) img = img.astype(‘float32’) temp.append(img) print (‘…Fin parsing et resize…’) train_x = np.stack(temp) train_x = train_x#, train_x[split_size:] train_y = keras.utils.np_utils.to_categorical(train.label.values) base_model = InceptionV3(weights=None, include_top=False)
x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation=‘relu’)(x) predictions = Dense(196, activation=‘softmax’)(x)
model = Model(input=base_model.input, output=predictions)
print (‘…Debut compilation du model…’) model.compile(optimizer=‘rmsprop’, loss=‘categorical_crossentropy’) print (‘…Fin compilation du model…’) filepath=“weights-improvement-{epoch:02d}.hdf5” checkpoint = ModelCheckpoint(filepath, verbose=1, save_best_only=True, mode=‘max’) callbacks_list = [checkpoint] print (‘Debut entrainement’) trained_model = model.fit(train_x, train_y, nb_epoch=nb_epoch, callbacks=callbacks_list, batch_size=8) model.save(‘car_model.h5’) print (‘Fin de l'entrainement’) `
## And here is the code to make predictions
`import os import numpy as np import pandas as pd from scipy.misc import imread from scipy.misc import imresize from inception_v3 import InceptionV3 from inception_v3 import preprocess_input from keras.preprocessing import image from keras.models import Model from keras.layers import Dense, GlobalAveragePooling2D from keras import backend as K import keras from keras.callbacks import ModelCheckpoint from imagenet_utils import decode_predictions root_dir = os.path.abspath(‘.’) data_dir = os.path.join(root_dir, ‘data’) os.path.exists(root_dir) os.path.exists(data_dir)
base_model = InceptionV3(weights=None, include_top=False)
x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation=‘relu’)(x) predictions = Dense(197, activation=‘softmax’)(x)
model = Model(input=base_model.input, output=predictions)
print (‘…Debut compilation du model…’) model.load_weights(‘car_model.h5’) model.compile(optimizer=‘rmsprop’, loss=‘categorical_crossentropy’) print (‘…Fin compilation du model…’)
#while True: #img_path = raw_input('Img path : ') img_path = ‘/home/images/truck1.jpg’ # Pas propre. A changer print (img_path) img_tmp = imread(img_path, mode=‘RGB’) img = imresize(img_tmp, (299, 299)) img = img.astype(‘float32’) x_ = np.expand_dims(img, axis=0) preds = model.predict(x_) decode_predictions(preds) print (‘Fin prediction’) `
I hope you’ll find the problem, because I don’t see at all…
(I trained the model over 50 epochs, so the problem don’t come for here)
Issue Analytics
- State:
- Created 7 years ago
- Comments:17
Top GitHub Comments
Did you forget to preprocess your input for the prediction ?
helllllllllo, @leminhlong-pixta i have the same problem as you described above. can you tell me a bit more specific about “preprocessing” ? did you solve the problem by subtracting mean or something ? thanks!