0

I cut the code because it is quite durity, Here is code for learning rate scheduler and the model use that optimizer.

initial_learning_rate = 0.001
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate,
    decay_steps=100000,
    decay_rate=0.96,
    staircase=False) 

opt=tf.keras.optimizers.SGD(learning_rate=lr_schedule)

and this is model save code. i saved it after model.fit

midmodel_2.save('../HY/HY_history/5fold/{}'.format(model_name2))
with open('../HY/HY_history/5fold/{}'.format(model_history_name2), 'wb') as f:
    pickle.dump(history.history, f)
midmodel_2.save_weights('../HY/HY_history/5fold/{}'.format(model_weight2))

load model code is here

import keras
from keras.models import load_model

loaded_model=load_model('./mobilenetv2_epoch50_fold1_210402_Adagrad_AugNo_25_freeze_schYes.h5')
loaded_model.load_weights('./mobilenetv2_epoch50_fold1_210402_Adagrad_AugNo_25_freeze_schYesw.h5')

This is the error what i have, In this screenshot i didn't use GPU but even with gpu in console also raise same error.

Exception has occurred: ValueError
Attempt to convert a value ({'class_name': 'ExponentialDecay', 'config': {'initial_learning_rate': 0.001, 'decay_steps': 100000, 'decay_rate': 0.96, 'staircase': False, 'name': None}}) with an unsupported type (<class 'dict'>) to a Tensor.
  File "D:\lab2\2021_cell\code\load_test\test.py", line 8, in <module>
    `loaded_model=load_model('./mobilenetv2_epoch50_fold1_210402_Adagrad_AugNo_25_freeze_schYes.h5')`

I did convert model.h5 file to model.json by using ' tensorflowjs_converter ' but it can't load model also. Is there any way to load this without training and saving it to json format again?

  • 1
    You seem to be mixing imports between tf.keras and keras, that is a bad idea, it is not supported, and will produce all kinds of strange errors like you see here. – Dr. Snoopy Apr 05 '21 at 10:47
  • Ooh.. noo... okey l'll be becareful next time thanks for yor help! – Ha yeong Yoon Apr 05 '21 at 12:13

1 Answers1

0

I find some method to fix this problem, but don't know why that is working.

If you got difficult with load model which has learning scheduler, It is helpful save model to json file and save weights to h5 file. and just load together

here is some example

filename = path + '/' + model_name
weightname = path + '/' + model_name[:-5] +'_weight.h5'

        
json_file = open(filename, 'r') 
model = json_file.read()
json_file.close()
model =model_from_json(model)
model.load_weights(weightname)
        
model.compile(optimizer=SGD, loss='categorical_crossentropy', metrics=['acc','Precision', 'Recall']) 
        

In this case, you have to compile model first before predict or somethings.