What I try to do
I'm trying to train a convolutional neural network (CNN) for image-detection using Keras with Tensorflow-GPU as backend in Python 2.7, since I need to use it with ROS kinetic, which only supports Python 2.7 (and not 3.5). My model is a Sequential (code see down below).
What I am using
Pycharm-Community 2018.1.4
Keras 2.2.0
Tensorflow-GPU 1.8.0
60000 input images, 100x100 pixels (3 channels), 3 classes ("train_set")
20000 evaluation images, same dimensions ("evaluation_set")
What works
When training the model on my train_set using Python 3.5 and evaluate it using Python 3.5 it works perfectly fine (train_accuracy: 0.99874, evaluation_accuracy: 0.9993).
What does not work
When training the model on my train_set using Python 2.7 and evaluate it using Python 2.7 my accuracy drops drastically (train_accuracy: 0.695, evaluation_accuracy: 0.543), which is not much more than guessing on 3 classes (which would be 0.3333).
I also tried training the model in Python 3.5 and load it in Python 2.7 for evaluation and prediction, but the results are as worse as before.
In all cases I am using the exact same code:
def build_model(training_input):
model = Sequential()
model.add(Conv2D(32, (3, 3)) # Add some layers
model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['accuracy'])
def train():
input_training = np.array(input_training_list) # input_training_list is a list containing the imagedata
labels_training = np.array(label_training_list) # label_training_list is a list containing the labels corresponding to the imagedata
model = create_model(input_training)
history = model.fit(input_training, labels_training, epochs=10, shuffle=True, batch_size=20)
model.save(model_directory + "my_model.h5")
def evaluation():
input_evaluation = np.array(input_evaluation_list)
labels_evaluation = np.array(label_evaluation_list)
model = load_model(model_directory + "my_model.h5")
loss, acc = model.evaluate(input_evaluation, labels_evaluation, batch_size=1)
I heard that many people have issues loading the same model in different Sessions(), using different computers or different versions of Python. But here the same architecture gives completely different results in both Python versions.
model.save_weights()
and then rebuild the model in Python 2 and justmodel.load_weights()
?model.save_weights("my_weights.h5")
in my working Python 3.5 script, build the same model/architecture in Python 2.7 and loaded the weights into that modelmodel.load_weights("my_weights.h5")
. But the results don't change. What confuses me is that even training the model in Python 2.7 gives such bad results.