示例#1
0
def train(img_local_path, label_path, model_object_key):
    model = SqueezeNet(weights='imagenet')
    img = image.load_img(img_local_path, target_size=(227, 227))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    label_file = open(label_path)
    y = np.array([label_file.read()])
    label_file.close()

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(x, y)
    model.summary()
    model.save_weights(tmp_path + model_object_key)
    return history.history
def main():
    pl_train, pl_labels = get_dataset('./Pan_Licence/')
    pl_labels = to_categorical(pl_labels, num_classes=36)

    x_train, x_val, y_train, y_val = train_test_split(pl_train,
                                                      pl_labels,
                                                      test_size=0.2,
                                                      random_state=2064)

    tb = TensorBoard(log_dir='./logs/Squeezenet', write_graph=True)

    model = SqueezeNet()
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    print(model.summary())

    history = model.fit(x_train,
                        y_train,
                        batch_size=32,
                        epochs=5,
                        validation_split=0.1,
                        shuffle=True,
                        callbacks=[tb])

    ## Save Model
    json_model = model.to_json()
    with open('model_squeezenet.json', 'w') as f:
        f.write(json_model)
    model.save_weights('model_squeezenet.h5')
    print('Model Saved')

    print('Evaluating Model')
    predict = model.evaluate(x=x_val, y=y_val, batch_size=1)

    print('Score', predict[1] * 100.00)
    print('Loss', predict[0])
示例#3
0
adam = Adam(lr=0.040)
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="categorical_crossentropy",
              optimizer='adam',
              metrics=['accuracy'])
if os.path.isfile(weights_file):
    print('Loading weights: %s' % weights_file)
    model.load_weights(weights_file, by_name=True)

print('Fitting model')
# model.fit(images, classes, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_split=0.2, initial_epoch=0)
model.fit_generator(training_data,
                    samples_per_epoch=samples_per_epoch,
                    validation_data=validation_data,
                    nb_val_samples=nb_val_samples,
                    nb_epoch=nb_epoch,
                    verbose=1,
                    initial_epoch=initial_epoch)
print("Finished fitting model")

print('Saving weights')
model.save_weights(weights_file, overwrite=True)
print('Evaluating model')
# score = model.evaluate(images, classes, verbose=1)

# validation_data = ( (load_image(x), to_categorical([y], nb_classes=nb_classes)) for x, y in test )
# validation_data = gen(X_test, Y_test)
score = model.evaluate_generator(validation_data, val_samples=nb_val_samples)
# score = model.evaluate(X_test, Y_test, verbose=1)
print('result: %s' % score)