Esempio n. 1
0
model.fit(x_train,
          y_train,
          epochs=epochs,
          batch_size=batch_size,
          validation_data=(x_test, y_test),
          callbacks=[checkpoint, lrschedual],
          verbose=1)
end_time = datetime.datetime.now()
best_model = load_model(model_save_path)
loss_acc = best_model.evaluate(x_test, y_test)
print('loss: {:.3f}, acc: {:.3f}'.format(loss_acc[0], loss_acc[1]))
print('training time: {}s'.format((end_time - start_time).seconds))

# plot the history of training
Funcs.save_history(model.history, model_history_path)
Funcs.plot_history(model.history, history_path)

# use highest accuracy model for prediction
pred = best_model.predict(x_test)
pred_idx = Funcs.get_pred_index(pred, num_pred=num_pred, y=y_test)
x = Funcs.undo_input(x_test[pred_idx])
pred = Funcs.decode_prediction(pred[pred_idx], json_path)
y = Funcs.decode_prediction(y_test[pred_idx], json_path)
Funcs.plot_prediction(x, pred, fname=pred_path, y=y)

# amend model filename to easily recognize
new_model_save_path = model_save_path[:-3] + '_' + str(int(
    loss_acc[1] * 10000)) + model_save_path[-3:]
os.rename(model_save_path, new_model_save_path)
print('saved model to {}'.format(new_model_save_path))
model.fit(x_train,
          y_train,
          epochs=epochs,
          batch_size=batch_size,
          validation_data=(x_test, y_test),
          callbacks=[checkpoint, lrschedual],
          verbose=2)
end_time = datetime.datetime.now()
best_model = load_model(model_save_path)
loss_acc = best_model.evaluate(x_test, y_test)
print('loss: {:.3f}, acc: {:.3f}'.format(loss_acc[0], loss_acc[1]))
print('training time: {}s'.format((end_time - start_time).seconds))

# plot the history of training
Funcs.save_history(model.history, model_history_path)
Funcs.plot_history(model.history, history_path, num_xticks=6)

# use highest accuracy model for prediction
pred = best_model.predict(x_test)
pred_idx = Funcs.get_pred_index(pred, num_pred=num_pred, y=y_test)
x = Funcs.undo_input(x_test[pred_idx])
pred = Funcs.decode_prediction(pred[pred_idx], json_path)
y = Funcs.decode_prediction(y_test[pred_idx], json_path)
Funcs.plot_prediction(x, pred, fname=pred_path, y=y)

# amend model filename to easily recognize
new_model_save_path = model_save_path[:-3] + '_' + str(int(
    loss_acc[1] * 10000)) + model_save_path[-3:]
os.rename(model_save_path, new_model_save_path)
print('saved model to {}'.format(new_model_save_path))