Beispiel #1
0
# Check point
cp_callback = "./models/weights.{epoch:02d}.h5"
checkpoint = ModelCheckpoint(cp_callback,
                             monitor='val_loss',
                             verbose=1,
                             save_best_only=True,
                             mode='min')
callbacks_list = [checkpoint]

# Tiến hành huấn luyện mô hình
history = model.fit_generator(train_generator,
                              steps_per_epoch=709595 // BATCH_SIZE,
                              epochs=N_EPOCHS,
                              validation_data=validation_generator,
                              validation_steps=72000 // BATCH_SIZE,
                              callbacks=callbacks_list)

# lưu mô hình
with open("./models/trongso.json", "w") as json_file:
    json_file.write(model.to_json())

# Biểu đồ training & validation accuracy values
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
Beispiel #2
0
if earlyStopFlag:
    callbacksList.append(earlyStop)
if reduceLRFlag:
    callbacksList.append(reduce_lr)
if modelCheckpointFlag:
    callbacksList.append(modelCheckpoint)

history = model.fit_generator(
    trainGenerator,
    steps_per_epoch=trainSamplesNumber // batchSize * foldAugment,
    epochs=epochs,
    verbose=1,
    callbacks=callbacksList,
    validation_data=validationGenerator,
    class_weight=getClassWeights(trainGenerator.classes),
    shuffle=True,
    validation_steps=validateSamplesNumber // batchSize)

score = model.evaluate_generator(testGenerator, testSamplesNumber)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

# serialize model to JSON
model_json = model.to_json()
with open(modelFile, "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights(weightsFile)
print("Saved model to disk")