Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keras - Python 3.6 support #7297

Closed
erinjerri opened this issue Jul 11, 2017 · 27 comments
Closed

Keras - Python 3.6 support #7297

erinjerri opened this issue Jul 11, 2017 · 27 comments

Comments

@erinjerri
Copy link

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

@yuyang-huang
Copy link
Contributor

yuyang-huang commented Jul 11, 2017

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

@hutauf
Copy link

hutauf commented Jul 11, 2017

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.

@fchollet
Copy link
Member

fchollet commented Jul 11, 2017 via email

@cclauss
Copy link
Contributor

cclauss commented Jul 12, 2017

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
                    ^

@ryanfox
Copy link

ryanfox commented Jul 23, 2017

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.

@fchollet
Copy link
Member

fchollet commented Jul 23, 2017 via email

@ryanfox
Copy link

ryanfox commented Jul 23, 2017

Got it. Possibly a tensorflow issue?

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

@erinjerri
Copy link
Author

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).

@dgorissen
Copy link
Contributor

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

@dmitriyse
Copy link

dmitriyse commented Sep 18, 2017

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.

@dmitriyse
Copy link

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

@adityapatadia
Copy link

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

cclauss pushed a commit to cclauss/keras that referenced this issue Oct 16, 2017
To match [setup.py](https://github.com/fchollet/keras/blob/master/setup.py#L34).  There are a number of questions raised in keras-team#7297 about Python 3.6 support so let's test that version.
@dmitriyse
Copy link

@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.

fchollet pushed a commit that referenced this issue Oct 16, 2017
To match [setup.py](https://github.com/fchollet/keras/blob/master/setup.py#L34).  There are a number of questions raised in #7297 about Python 3.6 support so let's test that version.
@adityapatadia
Copy link

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

@karanbgsr
Copy link

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

@MyVanitar
Copy link

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.

@anubhav0fnu
Copy link

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?

@dmitriyse
Copy link

@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.

@Sparsh-Bansal
Copy link

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

@cclauss
Copy link
Contributor

cclauss commented Jul 25, 2019

Please provide a full stack trace of the error.

@Sparsh-Bansal
Copy link

@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

@cclauss
Copy link
Contributor

cclauss commented Jul 25, 2019

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

@Sparsh-Bansal
Copy link

@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')

@cclauss
Copy link
Contributor

cclauss commented Jul 25, 2019

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.

@Sparsh-Bansal
Copy link

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

@fqx
Copy link

fqx commented Jul 31, 2019

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

@annong
Copy link

annong commented Dec 29, 2020

@Sparsh-Bansal I used Lambda layer and have same error, how to resolve it?
I used class SamplingLayer(Layer): to define myself layer to replace the lambda layer. The problem was solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests