Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras model (tensorflow backend) trains perfectly well in python 3.5 but very bad in python 2.7

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.

like image 243
benjamin Avatar asked Jun 15 '18 09:06

benjamin


1 Answers

I found the solution for my problem (thanks to user1735003 for the tip regarding my data).
The reason for my bad results was a wrong data-implementation due to the differences regarding Python 2.x and Python 3.x. When implementing my image-data I use

for i in range(len(evaluation_files)):
    input_evaluation = np.divide(ndimage.imread(evaluation_files[i]), 255)

But here is the Problem: In Python 3.x this works perfectly fine, since a division of two integers results in a float, but in Python 2.x the result is an integer as well, so my input_evalution list only consists of zeros. I need to divide by 255.0 (make the result a float).

input_evaluation = np.divide(ndimage.imread(evaluation_files[i]), 255.0)

Alternatively import division from __future__ to get floating point results from integer divisions already in python 2.

from __future__ import division

There are some mayor differences when using Python 2.x or Python 3.x which you can see very nicely for example on http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html .

I also managed training my model on Python 3.5, save it using model.save('my_model') and load it in Python 2.7 using keras.models.load_model('my_model'), which works perfectly fine.

One would also easily just save the weights using model.save_weights('my_weights'), create a new model of the same architecture(!) in Python 2.7 and load the weights into that model using model.load_weights('my_weights'), but since just loading the model itself works perfectly fine that way is much easier.

like image 197
benjamin Avatar answered Oct 16 '22 08:10

benjamin