Exemplo n.º 1
0
    print(np.shape(x_train_resized))
    print(np.shape(x_test_resized))
    model = MobileNet(include_top=True,
                      weights=None,
                      classes=2,
                      pooling='max',
                      input_shape=(200, 200, 3))

    model.load_weights(weight_path)

    checkpoint = ModelCheckpoint(filepath=os.path.join(
        save_dir,
        'MobileNetV2_weight.{epoch:02d}-{loss:.2f}-{categorical_accuracy:.2f}.hdf5'
    ),
                                 verbose=1,
                                 monitor='categorical_accuracy',
                                 save_best_only=True)

    opt = Adam(lr=5e-6)
    model.compile(optimizer=opt,
                  loss=losses.categorical_crossentropy,
                  metrics=[metrics.categorical_accuracy])
    # model.fit(x_train_resized,y_train,epochs=20,batch_size=6,callbacks=[checkpoint])
    #
    # model.save(model_path)
    # model.save_weights(weight_path)

    score1 = model.evaluate(x_train_resized, y_train, batch_size=6)
    score2 = model.evaluate(x_test_resized, y_test, batch_size=6)
    print(score1)
    print(score2)
Exemplo n.º 2
0
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False)  # randomly flip images

    # Compute quantities required for feature-wise normalization
    # (std, mean, and principal components if ZCA whitening is applied).
    datagen.fit(x_train)

    # Fit the model on the batches generated by datagen.flow().
    model.fit_generator(datagen.flow(x_train, y_train,
                                     batch_size=batch_size),
                        epochs=epochs,
                        validation_data=(x_test, y_test),
                        workers=4)

# Save model and weights
if not os.path.isdir(save_dir):
    os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)

# Score trained model.
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])