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

error while using the saved model #9

Open
un-lock-me opened this issue Feb 18, 2019 · 7 comments
Open

error while using the saved model #9

un-lock-me opened this issue Feb 18, 2019 · 7 comments

Comments

@un-lock-me
Copy link

un-lock-me commented Feb 18, 2019

When I want to use the saved mode to get prediction based on that, it raises

  File "/home/sgnbx/Downloads/projects/CRNN-with-STN-master/prediction.py", line 20, in <module>
    model = load_model('weightswithoutstnlrchanged.best.hdf5', custom_objects={"bknd": backend})
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/keras/engine/saving.py", line 419, in load_model
    model = _deserialize_model(f, custom_objects, compile)
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/keras/engine/saving.py", line 312, in _deserialize_model
    sample_weight_mode=sample_weight_mode)
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/keras/engine/training.py", line 129, in compile
    loss_functions.append(losses.get(loss.get(name)))
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/keras/losses.py", line 133, in get
    return deserialize(identifier)
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/keras/losses.py", line 114, in deserialize
    printable_module_name='loss function')
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 165, in deserialize_keras_object
    ':' + function_name)
ValueError: Unknown loss function:<lambda>

Do you have any idea of this?

this is my piece of code:

model = load_model('weightswithoutstnlrchanged.best.hdf5', custom_objects={"bknd": backend})
sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
adam = optimizers.Adam()

model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)

I gave custom_object backend because firstly it did not recognize backend.
now it raising error for loss, and I tried to assign loss in the custom object but it did not work.or maybe I have to try something else.

can you please have a look on this.
Thank you

@un-lock-me
Copy link
Author

Regarding this, I want to convert
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd) this part loss={'ctc': lambda y_true, y_pred: y_pred} to a function.

but I am not getting what are you doing here
can you please explain it so I can have the correspond function.

Thank you!

@sbillburg
Copy link
Owner

Regarding this, I want to convert
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd) this part loss={'ctc': lambda y_true, y_pred: y_pred} to a function.

but I am not getting what are you doing here
can you please explain it so I can have the correspond function.

Thank you!

the trained model is a compiled and complete network with weights, you can just use
model = load_model('weightswithoutstnlrchanged.best.hdf5')
print(model.predict(INPUT_IMAGE))
to show the result

@un-lock-me
Copy link
Author

un-lock-me commented Feb 20, 2019

Regarding this, I want to convert
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd) this part loss={'ctc': lambda y_true, y_pred: y_pred} to a function.
but I am not getting what are you doing here
can you please explain it so I can have the correspond function.
Thank you!

the trained model is a compiled and complete network with weights, you can just use
model = load_model('weightswithoutstnlrchanged.best.hdf5')
print(model.predict(INPUT_IMAGE))
to show the result

Thank you so much for replying back.
But it does not work, raises error loss not found, bknd not found.
After searching I find this workaround giving custom_object to it.
It works well for bknd so not getting error for that but loss still raises error:
So with this code:
mlmodel = load_model('weightswithoutstnlrchanged.best.hdf5', custom_objects={"bknd": backend,'ctc':loss})
It raises this error:

    mlmodel = load_model('weightswithoutstnlrchanged.best.hdf5', custom_objects={"bknd": backend,'ctc':loss})
NameError: name 'loss' is not defined

@sbillburg
Copy link
Owner

See line 53, line 142 in CRNN_with_STN.py
Did you define 'ctc' before you use it?

@un-lock-me
Copy link
Author

Actually yes, As per my search Keras has problem loading the model while there are custom layers.
I resolved it another way, however interested to know how can I do that while a custom loss there is in the code.

@un-lock-me
Copy link
Author

@sbillburg Can you please let me know your idea toward this error?
when I use model.predict('boston.jpg') it throws this error. I believe we have to convert it to the correct format before feeding to the model.

Traceback (most recent call last):
  File "/home/sgnbx/Downloads/projects/CRNN-with-STN-master/CRNN_with_STN.py", line 170, in <module>
    print model.predict('boston.jpg')
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python2.7/site-packages/keras/engine/training.py", line 1149, in predict
    x, _, _ = self._standardize_user_data(x)
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python2.7/site-packages/keras/engine/training.py", line 751, in _standardize_user_data
    exception_prefix='input')
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python2.7/site-packages/keras/engine/training_utils.py", line 92, in standardize_input_data
    data = [standardize_single_array(x) for x in data]
  File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python2.7/site-packages/keras/engine/training_utils.py", line 27, in standardize_single_array
    elif x.ndim == 1:
AttributeError: 'str' object has no attribute 'ndim'

@yosraammar
Copy link

when I load a ctc model I have this problem: ValueError: Unknown loss function:
any help please.

pad each output label to maximum text length

train_padded_txt = pad_sequences(training_txt, maxlen=max_label_len, padding='post', value = len(char_list))
valid_padded_txt = pad_sequences(valid_txt, maxlen=max_label_len, padding='post', value = len(char_list))

input with shape of height=32 and width=128

input with shape of height=32 and width=128

inputs = Input(shape=(32,128,1))

convolution layer with kernel size (3,3)

conv_1 = Conv2D(64, (3,3), activation = 'relu', padding='same')(inputs)

poolig layer with kernel size (2,2)

pool_1 = MaxPool2D(pool_size=(2, 2), strides=2)(conv_1)

conv_2 = Conv2D(128, (3,3), activation = 'relu', padding='same')(pool_1)
pool_2 = MaxPool2D(pool_size=(2, 2), strides=2)(conv_2)

conv_3 = Conv2D(256, (3,3), activation = 'relu', padding='same')(pool_2)

conv_4 = Conv2D(256, (3,3), activation = 'relu', padding='same')(conv_3)

poolig layer with kernel size (2,1)

pool_4 = MaxPool2D(pool_size=(2, 1))(conv_4)

conv_5 = Conv2D(512, (3,3), activation = 'relu', padding='same')(pool_4)

Batch normalization layer

batch_norm_5 = BatchNormalization()(conv_5)

conv_6 = Conv2D(512, (3,3), activation = 'relu', padding='same')(batch_norm_5)
batch_norm_6 = BatchNormalization()(conv_6)
pool_6 = MaxPool2D(pool_size=(2, 1))(batch_norm_6)

conv_7 = Conv2D(512, (2,2), activation = 'relu')(pool_6)

squeezed = Lambda(lambda x: K.squeeze(x, 1))(conv_7)

bidirectional LSTM layers with units=128

blstm_1 = Bidirectional(LSTM(128, return_sequences=True, dropout = 0.2))(squeezed)
blstm_2 = Bidirectional(LSTM(128, return_sequences=True, dropout = 0.2))(blstm_1)

outputs = Dense(len(char_list)+1, activation = 'softmax')(blstm_2)

model to be used at test time

act_model = Model(inputs, outputs)

act_model.summary()

#the CTC loss fnction is to predict the output text, it is very helpfull for the
#text recognition topic.
labels = Input(name='the_labels', shape=[max_label_len], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')

def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args

return K.ctc_batch_cost(labels, y_pred, input_length, label_length)

loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([outputs, labels, input_length, label_length])

#model to be used at training time
model = Model(inputs=[inputs, labels, input_length, label_length], outputs=loss_out)

#train the model

model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer = 'adam')
filepath= "/home/yosra/Downloads/best_model.hdf5"
checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')

callbacks_list = [checkpoint]
training_img = np.array(training_img)
train_input_length = np.array(train_input_length)
train_label_length = np.array(train_label_length)

valid_img = np.array(valid_img)
valid_input_length = np.array(valid_input_length)
valid_label_length = np.array(valid_label_length)

model.fit(x=[training_img, train_padded_txt, train_input_length, train_label_length],
y=np.zeros(len(training_img)), batch_size=batch_size,
epochs = epochs,
validation_data = ([valid_img, valid_padded_txt, valid_input_length, valid_label_length],[np.zeros(len(valid_img))]),
verbose = 1, callbacks = callbacks_list)

model.save(filepath)

#test the model
from keras.models import load_model

load the saved best model weights

new_model = load_model(filepath)

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

3 participants