前言:
經過一個晚上的找資料,模組終於能繼續訓練,但目前還沒有強制停止儲存模型的方法,所以之後會再改善一下功能性
程式碼:
if os.path.exists(DATASET_PATH):
if os.path.exists(DATASET_PATH + WEIGHTS_FINAL):
print(WEIGHTS_FINAL + "模型存在,將繼續訓練模型")
# net_final.save(WEIGHTS_FINAL)
new_net_final = load_model(WEIGHTS_FINAL)
new_net_final.fit(train_batches,
steps_per_epoch = train_batches.samples // BATCH_SIZE,
validation_data = valid_batches,
validation_steps = valid_batches.samples // BATCH_SIZE,
epochs = NUM_EPOCHS)
# 儲存訓練好的模型
print("儲存訓練模型")
new_net_final.save(WEIGHTS_FINAL)
# 從 HDF5 檔案中載入模型
# net_final = tf.contrib.keras.models.load_model(WEIGHTS_FINAL)
# 將參數儲存至 HDF5 檔案(不含模型)
# net_final.save_weights('my_model_weights.h5')
# 從 HDF5 檔案載入參數(不含模型)
# net_final.load_weights('my_model_weights.h5')
# net_final.load_weights(tf.train.latest_checkpoint(checkpoint_dir))
else:
print(WEIGHTS_FINAL + '模型不存在,將新建訓練模型')
# 訓練模型
net_final.fit(train_batches,
steps_per_epoch = train_batches.samples // BATCH_SIZE,
validation_data = valid_batches,
validation_steps = valid_batches.samples // BATCH_SIZE,
epochs = NUM_EPOCHS)
# callbacks = [cp_callback])
# 儲存訓練好的模型
print("儲存訓練模型")
net_final.save(WEIGHTS_FINAL)
else:
print(WEIGHTS_FINAL + '路徑不存在,請確認路徑')