前言:
利用while True:的方法後,已經能夠以少次數的 epochs 來重複儲存模型,但至於總共訓練了幾次,我將在之後新增方法
程式碼:
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)
while True :
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)
else:
print(WEIGHTS_FINAL + '模型不存在,將新建訓練模型')
# 訓練模型
while True :
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("儲存訓練模型")
net_final.save(WEIGHTS_FINAL)
else:
print(WEIGHTS_FINAL + '路徑不存在,請確認路徑')