简体   繁体   中英

Is there a way to train a CNN model, save the weights of this CNN, and then use this weights to retrain this CNN for other train data?

Someone told me about this type of experiment. The first step is to train a CNN and keep the weights and the second step is to use these weights to retrain this CNN, but this time to add more data to your train set (fine-tune).

I guess it's something like transfer learning but with a CNN that you train. Is there a way to choose the weights before training a CNN and those choosen weights to be you file?

So what i have done so far is train my CNN model and save the weights to a h5 file, with the code below

model.compile(loss='categorical_crossentropy', optimizer=opt,metrics=['accuracy'])
validation_data=(x_testcnn, y_test))
checkpoint_path= 'scratchmodel.best.h5'
save_dir = os.path.join(os.getcwd(), 'weights')
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                             save_weights_only=True,
                                             verbose=1)
 cnnhistory=model.fit(x_traincnn, 
      y_train,
      batch_size=16,           
      epochs=400,
      validation_data=(x_testcnn,y_test),
      callbacks=[cp_callback])

Now I want to retrain the same CNN, with the same weights,but this time with added data to the train set. Is there a way to do this? Thanks for helping.

Yes, you just need to load the weights to the newly created model and then train with your new data.

from tensorflow.python.keras.models import load_model #Tensorflow 2.0

new_model.compile(loss='categorical_crossentropy', optimizer=opt,metrics=['accuracy'])
new_model = load_model(filepath, compile=False) #compile=False allows you to load saved optimizer state

new_model.fit(...) # Fit on new data, leveraging training on old data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM