Keras: Keras - Python 3.6 support

Created on 11 Jul 2017  Â·  26Comments  Â·  Source: keras-team/keras

Will #Keras work with Python 3.6? I'm really tired of downgrading my different versions of Python at this point.

Most helpful comment

Sorry, I found my problem.
My model was contained compiled lambda which is incompatible between 3.5 and 3.6.
Once I regenerate my model under python 3.6. Everything becomes working.

P.S. my testing environment was ubuntu 16.04 + conda python 3.6.2 + keras 1.2.0

All 26 comments

Yes. Currently using 3.6.0 with Keras 2.0.6, no problem with it.

Let me welcome you to the flexible and also beautiful world of Anaconda environments, where you don't need to downgrade anything, you just install exactly that version that you need in parallel to your existing python distribution on whatever platform you are on.

In any case if you find anything in the codebase that is incompatible with
Py3.6, that should be fixed, so either report it, or send a PR to fix it
(preferred).

On 11 July 2017 at 01:30, hutauf notifications@github.com wrote:

Let me welcome you to the flexible and also beautiful world of Anaconda
environments, where you don't need to downgrade anything, you just install
exactly that version that you need in parallel to your existing python
distribution on whatever platform you are on.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/7297#issuecomment-314372561,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AArWb5CwRjCfaACU8n4zAOJNRo4wm3HPks5sMzKJgaJpZM4OTl4N
.

I found two Python 3 issues below.

For the first one, the fix would be something along the lines of
https://docs.python.org/3/library/2to3.html?highlight=reload#2to3fixer-reload

The second is an issue that a linter like flake8 would catch but should not be a problem for users, I put a fix in anyway in #7300.

I tried to add flake8 to the testing but I failed:
https://github.com/fchollet/keras/pull/7315/files

$ flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics
./docs/autogen.py:71:5: F821 undefined name 'reload'
    reload(sys)
    ^
./keras/utils/io_utils.py:145:21: F821 undefined name 'raw_input'
        get_input = raw_input
                    ^

With Windows 10, Keras 2.0.6, Python 3.6.1 I'm getting a SystemError: Unknown opcode when I run the line

keras.models.load_model('yolo.h5')

The model is as described here. I have no problems when I switch to Python 3.5.3.

A "SystemError" cannot be a problem with Keras. Semantically, it's an
internal problem encountered by your Python interpreter.

On 23 July 2017 at 15:48, Ryan Fox notifications@github.com wrote:

With Windows 10, Keras 2.0.6, Python 3.6.1 I'm getting a SystemError:
Unknown opcode when I run the line

keras.models.load_model('yolo.h5')

The model is as described here
https://github.com/allanzelener/YAD2K/blob/a42c760ef868bc115e596b56863dc25624d2e756/README.md#quick-start.
I have no problems when I switch to Python 3.5.3.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/7297#issuecomment-317287790,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AArWb6hVLpvLy6TgDM3NwtOl27AVlJDXks5sQ83MgaJpZM4OTl4N
.

Got it. Possibly a tensorflow issue?

In any case, 3.5 is working for me, so I have a workaround. Thank you.

Runs just fine with mine now, I think I only had issues before bc I had trouble with TF installation on PC (was 1000x easier on a Mac).

Had exactly the same problem, dropping to 3.5 also fixed it for me

Why this issue is closed ?
I am also facing with this problem, and does not know any workaround/solutions.
I tried python 3.6.0, 3.6.2, Keras 1.2.0, 2.0.6 2.0.8 Ubuntu 16.04/ Windows 10 x64
Always the same result SystemError: unknown opcode
(But it was my model, worked in 3.5.3/Keras 1.2.0)
P.S. I am using Theano as a backend.

Sorry, I found my problem.
My model was contained compiled lambda which is incompatible between 3.5 and 3.6.
Once I regenerate my model under python 3.6. Everything becomes working.

P.S. my testing environment was ubuntu 16.04 + conda python 3.6.2 + keras 1.2.0

@dmitriyse how did you convert compiled lambda function? I am facing same issue.

@adityapatadia, hopefully I found python sources for my model. I don't know any solution to convert python 3.X binary to python 3.Y binary.

@dmitriyse I ended up training my model under python 3.6 and use it.

Hi, I was facing a similar issue. I tried the following

  1. Rather from command prompt use Anaconda Prompt
  2. Once you have Opened Anaconda prompt install Tensor flow and Keras by the following
    conda install tensorflow
    conda install keras
  3. Post this open your spyder application and run
    import tensorflow works
    import keras

PS : The same worked for me

You can use conda install -c hesi_m keras to install the latest version of Keras (2.1.6) and Tensorflow(1.8) for you.

Sorry, I found my problem.
My model was contained compiled lambda which is incompatible between 3.5 and 3.6.
Once I regenerate my model under python 3.6. Everything becomes working.

P.S. my testing environment was ubuntu 16.04 + conda python 3.6.2 + keras 1.2.0

@dmitriyse , How you have regenerated the model? I have downloaded the weight from someone else google drive and using the "YOLOv2" model from .
Could you please suggest on How you have regenerated the model on python3.6?

My system conda environment configuration:

  • Python 3.6.7 :: Anaconda, Inc.
  • keras 2.2.4
  • Tensorflow 1.13.1 backend

Also, could you please explain how a model could be system specific? any suggestions?

@anubhav0fnu, no any magic. I have some another DB with the model and performed transform from this source to Keras model under python 3.6. I haven't found any way to convert from 3.5 to 3.6.

I m getting the same error ''SystemError: unknown opcode'' with Python 3.5,3.6,3.7
Please help

Please provide a full stack trace of the error.

@cclauss
Traceback (most recent call last):
File "D:/ML/One_shot_detection/main.py", line 19, in
loaded_model = model_from_json(loaded_model_json)
File "C:\Users\spars\Anaconda3\envs\One_shot_detection\lib\site-packages\tensorflow\python\keras\saving\model_config.py", line 96, in model_from_json
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\spars\Anaconda3\envs\One_shot_detection\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 89, in deserialize
printable_module_name='layer')
File "C:\Users\spars\Anaconda3\envs\One_shot_detection\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 192, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\spars\Anaconda3\envs\One_shot_detection\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1131, in from_config
process_node(layer, node_data)
File "C:\Users\spars\Anaconda3\envs\One_shot_detection\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1089, in process_node
layer(input_tensors, *kwargs)
File "C:\Users\spars\Anaconda3\envs\One_shot_detection\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 634, in __call__
outputs = call_fn(inputs, *args, *
kwargs)
File "C:\Users\spars\Anaconda3\envs\One_shot_detection\lib\site-packages\tensorflow\python\keras\layers\core.py", line 785, in call
return self.function(inputs, **arguments)
File "D:/ML/One_shot_detection/model3.py", line 57, in euclidean_distance
x, y = vects
SystemError: unknown opcode

The first and last calls in this stack trace are __D:/ML/One_shot_detection__. What package is that?

@cclauss
I'm doing face recognition by Siamese Network , Here is my code :
import tensorflow as tf
from tensorflow.python.keras.layers import Conv2D , MaxPool2D , Lambda , Dense , Input , Flatten
from tensorflow.python.keras.layers import BatchNormalization ,Dropout
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.optimizers import rmsprop ,adam
from tensorflow.python.keras.models import Model , load_model ,model_from_json
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.activations import relu , sigmoid ,tanh
import numpy as np
import os
import time
import cv2

def build_model_network():
model = Sequential()

model.add(Conv2D(input_shape=(128,128,3) , filters=64 ,kernel_size=(10,10), activation=relu))

model.add(BatchNormalization())

model.add(Dropout(0.1))

model.add(MaxPool2D(pool_size=(2,2)))

model.add(Conv2D(filters=128 , kernel_size=(7,7) , activation=relu))

model.add(BatchNormalization())

model.add(MaxPool2D(pool_size=(2,2)))

model.add(Conv2D(filters=128 , kernel_size=(4,4) , activation=relu))

model.add(BatchNormalization())

model.add(MaxPool2D(pool_size=(2,2)))

model.add(Conv2D(filters=256 , kernel_size=(4,4) , activation=relu))

model.add(BatchNormalization())

model.add(Dropout(rate=0.1))

model.add(Flatten())

model.add(Dense(units=4096 , activation=sigmoid))

return model
model = build_model_network()
input_x1 = Input(shape=(128,128,3))
input_x2 = Input(shape=(128,128,3))

output_x1 = model(input_x1)
output_x2 = model(input_x2)

def euclidean_distance(vects):
x, y = vects
return K.sqrt(K.sum(K.square(x - y), axis=1, keepdims=True))

def contrastive_loss(y_true, y_pred):
margin = 1
return K.mean(y_true * K.square(y_pred) + (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))

distance = Lambda(euclidean_distance)([output_x1 , output_x2])
output_ = Dense(1,activation=sigmoid)(distance)

rms = rmsprop()

model = Model([input_x1 , input_x2] , output_)
model.compile(loss=contrastive_loss , optimizer = 'rmsprop')

X1 = np.load('numpy_files/X1.npy',allow_pickle=True)
X2 = np.load('numpy_files/X2.npy' , allow_pickle=True)
Y = np.load('numpy_files/Y.npy' , allow_pickle=True)
print(X1.shape)
print(X1[0])

data_dimension = 128
X11 = X1.reshape( ( X1.shape[0] , 128,128,3 ) ).astype( np.float32 )
X22 = X2.reshape( ( X2.shape[0] , 128,128,3 ) ).astype( np.float32 )

model.fit([X11,X22] , Y , batch_size=5 , epochs=1 , validation_split=None )

I tried to save and load my model by two ways but getting the same error in both

First : model.save('siamese.h5')
model = load_model('siamese.h5')
Second: model_json = model.to_json()
with open("model_num.json", "w") as json_file:
json_file.write(model_json)
model.save_weights("model_num.h5")
json_file = open('model_num.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model_num.h5")
loaded_model.save('model_num.hdf5')
loaded_model=load_model('model_num.hdf5')

Pasting code in this way loses all indentation. Please edit the code above to add three backticks ``` on a _separate_ line before the code and another three backticks on a _separate_ line after the code.

Lambda layer was causing the problem , now its resolved.
Thanks @cclauss for coordinating .

@Sparsh-Bansal I used Lambda layer and have same error, how to resolve it?

Was this page helpful?
0 / 5 - 0 ratings