lim-anggun / FgSegNet

FgSegNet: Foreground Segmentation Network, Foreground Segmentation Using Convolutional Neural Networks for Multiscale Feature Encoding
https://doi.org/10.1016/j.patrec.2018.08.002
Other
240 stars 76 forks source link

Save models #4

Closed InstantWindy closed 6 years ago

InstantWindy commented 6 years ago

I have a question, after training the model ,how to save the model's weight? For every scene,I need to save corresponding weight,?if so ,it needs to save 52 weight files, could you help me?

lim-anggun commented 6 years ago

Hi @InstantWindy, it a scene-specific method as described in the paper. To save only weights, set save_weights_only=True To save only models, set save_weights_only=False

chk = keras.callbacks.ModelCheckpoint(mdl_path, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True, mode='auto', period=1)

InstantWindy commented 6 years ago

but if set save_weights_only=True,where the weight file is? For every scene ,after training the model is deleted, when testing ,how to use the trained model? I try to use the code "model.save_weights('weights.h5') " for every scene saving the weights. I don't know, could you explain in detail?Thank you

lim-anggun commented 6 years ago

@InstantWindy, the following Keras function saves the model for you automatically. Each model is saved at mdl_path, where mdl_path is the path of your model: eg:mdl_path = '/models/my_highway_model.h5'

chk = keras.callbacks.ModelCheckpoint(mdl_path, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=1)

after training, load the model from mdl_path:

from keras.models import load_model
model = load_model(mdl_path)
model.predict(...)

There are some differences between keras model and weight, you can read Keras docs for more details. Here

LavenderMP commented 5 years ago

After i trained the whole CDnet2014 i got the .h5 model but i can't load it it gives erros: Traceback (most recent call last): File "test.py", line 6, in <module> model = load_model(PATH) File "C:\Users\hvhnk\Anaconda3\envs\tf-gpu\lib\site-packages\keras\models.py", line 264, in load_model sample_weight_mode=sample_weight_mode) File "C:\Users\hvhnk\Anaconda3\envs\tf-gpu\lib\site-packages\keras\engine\training.py", line 681, in compile loss_function = losses.get(loss) File "C:\Users\hvhnk\Anaconda3\envs\tf-gpu\lib\site-packages\keras\losses.py", line 102, in get return deserialize(identifier) File "C:\Users\hvhnk\Anaconda3\envs\tf-gpu\lib\site-packages\keras\losses.py", line 94, in deserialize printable_module_name='loss function') File "C:\Users\hvhnk\Anaconda3\envs\tf-gpu\lib\site-packages\keras\utils\generic_utils.py", line 159, in deserialize_keras_object ':' + function_name) ValueError: Unknown loss function:loss

lim-anggun commented 5 years ago

Hi @LavenderMP, You need to add custom objects when loading the model:

from my_upsampling_2d import MyUpSampling2D
from FgSegNet_M_S_module import loss, acc, loss2, acc2

model = load_model(model_path, custom_objects={'MyUpSampling2D': MyUpSampling2D,  'loss':loss, 'acc':acc, 'loss2':loss2, 'acc2':acc2})