-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Loading model with custom loss function: ValueError: 'Unknown loss function' #5916
Comments
I have this exact same error and just noticed this morning. My metrics are very simple:
|
It looks like its trying to find the fn in generic_utils.py for a function_name which is binary_PTA which isnt found in custom_objects. @fchollet How do we add our metric to custom_objects? |
I did this as a work around:
|
@isaacgerg what Keras version are you running? I got the error described in the old title in Keras 2.0.0, now after updating to 2.0.2 I'm getting a new error (as described in the new title). But yeah, for the moment, saving and loading the weights separately is the way to go as a workaround. |
I use 2.0.1 |
Hi, the same problem here.
This is a know issue on keras 1 #3977. |
My work around is to load the json first and then load the weights. I keep my load function in a local lib with the rest of my keras workarounds ;) |
This PR should have fixed this issue. Can use:
|
Same here. |
I solved this problem by adding 'custom_bojects'
my loss function:
|
My workaround is like @dluvizon except I assigned
keras 2.0.4 |
Hi, I have a similar problem with: Does anyone can help me ? Many thanks! |
Hi @pigna90 , the easiest way is to define a python function in the form:
Then you pass it as your loss in |
Thanks @dluvizon, but I need three parameters as I wrote in my first post. How can I handle it? |
Checkout functools.partial This is really basic Python, so I think this is
not the appropriate forum to discuss it.
…On Tue, May 30, 2017, 8:55 AM Alessandro Romano ***@***.***> wrote:
Thanks @dluvizon <https://github.com/dluvizon>, but I need three
parameters as I wrote in my first post. How can I handle it?
The error is raised only when I try to reload the model that has been
saved.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#5916 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA3RW8F8kgL35_f7D3iXjj9kuVW6h0Opks5r_DwCgaJpZM4Mki1T>
.
|
@piccolbo actually I'm using also functools.partial, but I need to execute the following code in save/serialization time and in loading time: I hope that my issue it's relevant and clear. |
@Bisgates I compile my model like: I think that might because I give the worry name 'val_loss'? I tried 'loss' but it doesn't work too. Do you have any idea about that? |
@ZY0422 You want |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed. |
I have a similar problem |
@caocao1989 |
@SimulatedANeal |
@SimulatedANeal |
@SimulatedANeal I am using ssd_keras loss |
@Bisgates I'm loading my model using the weights as such:
I can't add custom objects, is there a way to add yolo_loss, when loading the model using load_weights. |
@dluvizon: Can you elaborate on how to add the custom_loss in keras2.0? |
@TanmayParekh @shuaiw24, this is how you do it. def sparse_weighted_loss_func(weights):
def sparse_weighted_loss(target, output):
return tf.multiply(tf.keras.backend.sparse_categorical_crossentropy(target, output), weights)
return sparse_weighted_loss
model.compile(loss=sparse_weighted_loss_func(weights), ...)
# during loading time, it will expect sparse_weighted_loss not sparse_weighted_loss_func
model = load_model('pathtomodel',
custom_objects: {'sparse_weighted_loss': sparse_weighted_loss_func(weights)}) |
The following code worked for me -- generator.compile(loss='mse', optimizer=opt, metrics=[perceptual_distance]) |
Giving the custom loss a name was the only thing that worked in tf 2.1 |
None of the above worked for me, but this did: Load the model with import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
IMG_WIDTH = 224
IMG_HEIGHT = IMG_WIDTH
CHANNELS = 3
LEARNING_RATE = 1e-5
NUM_LABELS = 128
def create_model():
feature_extractor_url = 'https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4'
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
input_shape=(IMG_HEIGHT, IMG_WIDTH, CHANNELS))
feature_extractor_layer.trainable = False
return Sequential([
feature_extractor_layer,
Dense(1024, activation='relu', name='hidden_layer'),
Dense(NUM_LABELS, activation='sigmoid', name='output')
])
def load_model(model_dir):
# got the compile=False idea from @Pepslee's comment:
# https://github.com/keras-team/keras/issues/5916#issuecomment-457624404
return tf.keras.models.load_model(model_dir,
compile=False,
custom_objects={'KerasLayer': hub.KerasLayer})
# this didn't work.
# neither did the custom_loss_function with __name__ thing.
# custom_objects={'KerasLayer': hub.KerasLayer,
# 'custom_loss': custom_loss,
# 'custom_metric': custom_metric})
def train(prev_model):
# the trick is to load the model with compile=False
if prev_model:
model = load_model(prev_model)
else:
model = create_model()
# and then compile manually,
# the same way it does with a new model.
model.compile(
optimizer=Adam(learning_rate=LEARNING_RATE),
loss=custom_loss,
metrics=[custom_metric]
)
# model.fit ...
# def custom_loss(y_true, y_pred):
# ...
# def custom_metric(y_true, y_pred, threshold=0.5):
# ... |
I have a problem when I load this model: ValueError: Unknown loss function: can you help me please. import numpy as np # linear algebra Input data files are available in the "../input/" directory.For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directoryimport matplotlib.pyplot as plt Importsimport os from keras.preprocessing.sequence import pad_sequences import tensorflow as tf #read xml file doc = ET.parse("/home/yosra/Downloads/IAMDataset/xml/a01-000u.xml")root = doc.getroot()dic = [] for i in root.iter('word'):dic.append(i.get('id'))label.append(i.get('text'))print(dic, ' ' ,label)Global Variableschar_list = string.ascii_letters + string.digits #function to decode the text into indice of char list #preprocess the data #Number of samples in xml filexml_samples = len(dic)#list of trining_set training_img = [] #lists for validation dataset max_label_len = 0 Training Variablesbatch_size = 256 k=1 for i, pic in enumerate(os.listdir('/home/yosra/Desktop/imagetest')):
print('kamlna') pad each output label to maximum text lengthtrain_padded_txt = pad_sequences(training_txt, maxlen=max_label_len, padding='post', value = len(char_list)) input with shape of height=32 and width=128input with shape of height=32 and width=128inputs = 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) 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 layerbatch_norm_5 = BatchNormalization()(conv_5) conv_6 = Conv2D(512, (3,3), activation = 'relu', padding='same')(batch_norm_5) 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=128blstm_1 = Bidirectional(LSTM(128, return_sequences=True, dropout = 0.2))(squeezed) outputs = Dense(len(char_list)+1, activation = 'softmax')(blstm_2) model to be used at test timeact_model = Model(inputs, outputs) act_model.summary() #the CTC loss fnction is to predict the output text, it is very helpfull for the def ctc_lambda_func(args):
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([outputs, labels, input_length, label_length]) #model to be used at training time #train the model model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer = 'adam') callbacks_list = [checkpoint] valid_img = np.array(valid_img) model.fit(x=[training_img, train_padded_txt, train_input_length, train_label_length], model.save(filepath) #test the model load the saved best model weightsnew_model = load_model(filepath) |
When I load my model, I have this error: ValueError: Unknown loss function:, any help please??? pad each output label to maximum text lengthtrain_padded_txt = pad_sequences(training_txt, maxlen=max_label_len, padding='post', value = len(char_list)) input with shape of height=32 and width=128input with shape of height=32 and width=128inputs = 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) 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 layerbatch_norm_5 = BatchNormalization()(conv_5) conv_6 = Conv2D(512, (3,3), activation = 'relu', padding='same')(batch_norm_5) 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=128blstm_1 = Bidirectional(LSTM(128, return_sequences=True, dropout = 0.2))(squeezed) outputs = Dense(len(char_list)+1, activation = 'softmax')(blstm_2) model to be used at test timeact_model = Model(inputs, outputs) act_model.summary() #the CTC loss fnction is to predict the output text, it is very helpfull for the def ctc_lambda_func(args):
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([outputs, labels, input_length, label_length]) #model to be used at training time #train the model model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer = 'adam') callbacks_list = [checkpoint] valid_img = np.array(valid_img) model.fit(x=[training_img, train_padded_txt, train_input_length, train_label_length], model.save(filepath) #test the model load the saved best model weightsnew_model = load_model(filepath) |
Stop posting into this thread!!! There's an answer above, and you're making it hard to find! WORKAROUNDmodel = tf.keras.models.load_model(path_here, compile=False) |
could anyone solve this problem please? def my_loss(y_true,y_pred,lambda_const,i,T,task_size=2): return lambda_const*categorical_crossentropy(y_trueSoft,y_predSoft) + (1-lambda_const)*categorical_crossentropy(y_trueHard,y_predHard) model_final.compile(loss=lambda y_true, y_pred: my_loss(y_true, y_pred, lambda_const,i,T), optimizer="adam", metrics=["acc"]) And when I want to load this model , I got this error,ValueError: Unknown loss function:. model_tmp=load_model(model_path_old,custom_objects={'angle bracket lambda angle bracket': lambda y_true, y_pred : my_loss(y_true, y_pred, lambda_const,i,T)})Can anyone help me ? Thanks a lot |
You first need to load your model with compile=False, then compile it. Like this: |
Thank you so much! But it didn't work. I still got this Unknown loss function: |
Custom objects should be called while compiling, so they should not be called in load_model. The above code piece is not the right answer, sorry. It should be: |
Thank you so much. It works! Hope you have a nice day! |
@weilinapple you reading comprehension is way below required minimum for an engineer, consider a different career path. |
@Demetrio92 your communication and social skills are below required minimum for an engineer, consider a different career path. |
model.compile(optimizer='adam', metrics=['accuracy'], this code works |
Specifically with custom objects such as the WeightedLogLoss function It is expected that to load the saved model correctly, one will need to use: >>> model = load_model('model.hdf5', custom_objects={'custom_loss': custom_loss}) Linked to #41 Refs: - keras-team/keras#5916 (comment) new file: astronet/t2/tests/func/test_model_save_load_predict.py
None of the above worked for me, because my sense is the my model is mmoe , multi-task learning. ps: |
many people try to find this issue. This problem is for the loss during training.
|
That really helped! Thank you!!! |
Dear expert! Traceback (most recent call last): base on your expertise, could you please advice to me some solutions? |
I trained and saved a model that uses a custom loss function (Keras version: 2.0.2):
model.compile(optimizer=adam, loss=SSD_Loss(neg_pos_ratio=neg_pos_ratio, alpha=alpha).compute_loss)
When I try to load the model, I get this error:
ValueError: ('Unknown loss function', ':compute_loss')
This is the stack trace:
Check that you are up-to-date with the master branch of Keras. You can update with:
pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.
The text was updated successfully, but these errors were encountered: