Exemplo n.º 1
0
# summarize results
#print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))

# create model
model = build_model(x_train.shape[1:], y_train.shape[-1], activation[0],
                    learn_rate[0], loss[0], optimizer[0], drop_rate[0])

print('built model..')

# Data Augmentation
datagen = ImageDataGenerator(featurewise_center=True,
                             featurewise_std_normalization=True,
                             rotation_range=0.0,
                             fill_mode='nearest',
                             horizontal_flip=True,
                             vertical_flip=True,
                             rescale=1. / 255,
                             preprocessing_function=None,
                             validation_split=0.25)

datagen.fit(x_train)

model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size[0]),
                    epochs=epochs[0])

# Save model with weights
model.save(data_dir + '/models/yelp_model.h5')

score = model.evaluate(x_test, y_test)
print('Model loss:', score)
#print('Model accuracy:', score)
Exemplo n.º 2
0
ltk_keras = ltk
model = KerasClassifier(build_fn= ltk_keras.Build_Model, verbose=1)
hyperparameters = ltk_keras.create_hyperparameters()
search = RandomizedSearchCV(estimator=model,
                            param_distributions=hyperparameters,
                            n_iter=10, n_jobs=-1, cv=3, verbose=1)
Max_acc = 0

data_generator = ImageDataGenerator(
    rotation_range=20,
    width_shift_range=0.02,
    height_shift_range=0.02,
    horizontal_flip=True
)
for i in range(10):
    search.fit(X_list_train[i], Y_list_train[i])
    print(search.best_params_)
    model = ltk_keras.Build_Model(search.best_params_["keep_prob"], search.best_params_['optimizer'], search.best_params_['kernel_regularizer_num'])
    history = model.fit_generator(
        data_generator.flow(X_list_train[i], Y_list_train[i], batch_size=search.best_params_["batch_size"]),
        steps_per_epoch= IMG_ROWS * IMG_COLS * IMG_CHANNELS,
        epochs = 10,
        validation_data = (X_test, Y_test),
        verbose=1
    )
    loss, acc = model.evaluate(X_test, Y_test)
    print("정답률: ", acc)
    if Max_acc < acc:
        Max_acc = acc
print("최종정답률: ", acc)
Exemplo n.º 3
0
	grid_result = grid.fit(base_model, outputs)

	# summarize results
	print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
	for params, mean_score, scores in grid_result.grid_scores_:
	    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))

    


    early_stopping = EarlyStopping(patience=20)
    checkpointer = ModelCheckpoint('inception_resnet_bottleneck_drug_best.h5', verbose=1, save_best_only=True)

    ImageFile.LOAD_TRUNCATED_IMAGES = True

    model.fit_generator(batches, steps_per_epoch=num_train_steps, epochs=1000, callbacks=[early_stopping, checkpointer], validation_data=val_batches, validation_steps=num_valid_steps)
    model.save_weights('inception_resnet_bottleneck_drug_weights.h5')
    model.save('inception_resnet_bottleneck_drug.h5')

#     for layer in model.layers[-31:]:
#         layer.trainable=True
#     for layer in model.layers[:-31]:
#         layer.trainable=False

#     model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), metrics=['accuracy'])

#     checkpointer = ModelCheckpoint('./resnet50_best_safety.h5', verbose=1, save_best_only=True)

#     model.fit_generator(batches, steps_per_epoch=num_train_steps, epochs=1000, callbacks=[early_stopping, checkpointer], validation_data=val_batches, validation_steps=num_valid_steps)
#     model.save('resnet50_safety.h5')
Exemplo n.º 4
0
def train_model(param):

    train_out = np.load('data/train_out.npy')
    test_out = np.load('data/test_out.npy')
    train_norm_2d = np.load('data/train_norm_2d.npy')
    test_norm_2d = np.load('data/test_norm_2d.npy')
    train_norm_2d_new = np.load('data/train_norm_2d_new.npy')
    train_out_new = np.load('data/train_out_new.npy')
    out_threshold = 0.005

    ####################### GRID HYPERPARAMETERS ###########################

    KFolds = 3
    e = [5, 10, 15]
    learning_rate = [0.01, 0.005, 0.001]
    param_grid = dict(epochs=e, learning_rate=learning_rate)

    ####################### CV GRID SEARCH #################################
    augmentation = False

    t0 = time.time()
    l = []

    model = KerasClassifier(build_fn=create_model, batch_size=param)

    if augmentation == True:
        datagen = ImageDataGenerator(rotation_range=5,
                                     width_shift_range=0,
                                     height_shift_range=0,
                                     shear_range=0,
                                     zoom_range=0,
                                     horizontal_flip=True,
                                     fill_mode='nearest')

        #datagen.fit(train_norm_2d_new)
        print(train_norm_2d_new.shape)
        print(train_out_new.shape)
        print("Running augmented training now, with augmentation")
        history = model.fit_generator(datagen.flow(train_norm_2d_new,
                                                   train_out_new,
                                                   batch_size=param,
                                                   shuffle=True),
                                      steps_per_epoch=len(train_norm_2d_new) /
                                      param,
                                      epochs=10)
        indices_test = np.where(test_out > out_threshold)[0]
        test_out_pos = test_out[indices_test]
        test_out_new = np.tile(test_out_pos, 18)
        test_out_new = np.concatenate((test_out, test_out_new), axis=0)
        test_norm_2d_pos = test_norm_2d[indices_test, :, :, :]
        test_norm_2d_new = np.tile(test_norm_2d_pos, (18, 1, 1, 1))
        test_norm_2d_new = np.concatenate((test_norm_2d, test_norm_2d_new),
                                          axis=0)
        t1 = time.time()
        return calc_verification_scores(model, test_norm_2d_new,
                                        test_out_new), t1 - t0

    else:
        print("Running regular training, no augmentation")
        #ros=RandomOverSampler(random_state=0)
        #train_norm_2d_resampled, train_out_resampled=ros.fit_resample(train_norm_2d,train_out)

        start_time = time.time()
        grid = GridSearchCV(estimator=model,
                            param_grid=param_grid,
                            cv=KFolds,
                            n_jobs=-1)
        grid_result = grid.fit(train_norm_2d_new, train_out_new)

        means = grid_result.cv_results_['mean_test_score']
        stds = grid_result.cv_results_['std_test_score']
        params = grid_result.cv_results_['params']

        end_time = time.time()
        run_time = end_time - start_time
        #preds = model.predict(test_norm_2d)

        for mean, stdev, p in zip(means, stds, params):
            x = [("%f (%f) with: %r, batch: %r ,time: %r" %
                  (mean, stdev, p, param, run_time))]
            l.append(x)

        #calc_verification_scores(test_out,preds)
        return l
Exemplo n.º 5
0
combined_pred = test_crs.merge(pred_cols, left_index=True, right_index=True)

combined_pred.shape
combined_pred.to_csv('/home/joseh/data/cnn_cadr_student_testset_title.csv',
                     encoding='utf-8',
                     index=False)

## Try k-fold cross validation

for j, (train_idx, val_idx) in enumerate(folds):

    print('\nFold ', j)
    X_train_cv = X_train[train_idx]
    y_train_cv = y_train[train_idx]
    X_valid_cv = X_train[val_idx]
    y_valid_cv = y_train[val_idx]

    name_weights = "final_model_fold" + str(j) + "_weights.h5"
    callbacks = get_callbacks(name_weights=name_weights, patience_lr=10)
    generator = gen.flow(X_train_cv, y_train_cv, batch_size=batch_size)
    model = get_model()
    model.fit_generator(generator,
                        steps_per_epoch=len(X_train_cv) / batch_size,
                        epochs=15,
                        shuffle=True,
                        verbose=1,
                        validation_data=(X_valid_cv, y_valid_cv),
                        callbacks=callbacks)

    print(model.evaluate(X_valid_cv, y_valid_cv))