Ejemplo n.º 1
0
    q = Dense(1)(input3)
    f = add([q, f])

    model = Model(inputs=[input1, input2, input3], outputs=f)
    model.summary()

    return model


input_size = (1, 15, 64, 128)
Nlags = 15

fname_param = '/media/smalldave/Storage/GBM/LSTM/3d_mean_spread_best_parameters_{}.hdf5'.format(
    '1_9')
early_stopping = EarlyStopping(monitor='val_loss',
                               patience=10,
                               mode='auto',
                               min_delta=100000)
model_checkpoint = ModelCheckpoint(fname_param,
                                   verbose=1,
                                   save_best_only=True,
                                   mode='min',
                                   save_weights_only=True)

inx = Dataset(
    '/media/smalldave/Storage/GBM/LSTM/total_forecast_precipitation_mean_spread_input.nc'
)

Px_mean = inx.variables['precipitation'][:, :, :, :, 0]
Px_mean = Px_mean[:, np.newaxis, :, :, :]
Px_mean = np.moveaxis(Px_mean, 4, 2)
Px_spread = inx.variables['precipitation'][:, :, :, :, 1]
    VGG16_model = Model(VGG16_notop.input, output)
    VGG16_model.summary()

    optimizer = SGD(lr=learning_rate, momentum=0.9, decay=0.001, nesterov=True)
    VGG16_model.compile(loss='categorical_crossentropy', optimizer=optimizer,
                        metrics=['accuracy', "top_k_categorical_accuracy"])
    # 创建一个实例LossHistory
    history = LossHistory()

    # autosave best Model
    # best_model_file = model_dir + "VGG16_UCM_weights.h5"
    # best_model_file = model_dir + "RVGG16_2015_4_classes_weights.h5"
    best_model_file = model_dir + "RVGG16_10_cls_128_nopre_weights.h5"
    best_model = ModelCheckpoint(best_model_file, monitor='val_acc', verbose=1, save_best_only=True)
    early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='auto')

    # this is the augmentation configuration we will use for training
    train_datagen = ImageDataGenerator(
        samplewise_center=True,  # 输入数据集去中心化,按feature执行
        rescale=1. / 255,  # 重缩放因子
        shear_range=0.1,  # 剪切强度(逆时针方向的剪切变换角度)
        zoom_range=0.1,  # 随机缩放的幅度
        rotation_range=10.,  # 图片随机转动的角度
        width_shift_range=0.1,  # 图片水平偏移的幅度
        height_shift_range=0.1,  # 图片竖直偏移的幅度
        horizontal_flip=True,  # 进行随机水平翻转
        vertical_flip=True,  # 进行随机竖直翻转
    )

    # this is the augmentation configuration we will use for validation:
Ejemplo n.º 3
0
regressor.add(LSTM(units=64, return_sequences=True, activation="relu"))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units=32, activation="relu"))
regressor.add(Dropout(0.2))

regressor.add(Dense(units=20, activation='relu'))
regressor.summary()

regressor.compile(optimizer='adam', loss='mean_squared_error')

mcp = ModelCheckpoint('weights{epoch:08d}.h5',
                      save_weights_only=True,
                      period=5)
tb = TensorBoard('logs')
es = EarlyStopping(monitor='val_loss', min_delta=1e-10, patience=10, verbose=1)
rlr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, verbose=1)

regressor.fit(input_data,
              output_data,
              epochs=300,
              shuffle=True,
              callbacks=[es, rlr, mcp, tb],
              validation_split=0.2,
              verbose=1,
              batch_size=64)

model_json = regressor.to_json()
with open('last_model.json', 'w') as json_file:
    json_file.write(model_json)
regressor.save_weights('last_model.h5')
Ejemplo n.º 4
0
X_train, X_test, y_train, y_test = train_test_split(input_data,
                                                    label_data,
                                                    test_size=0.2)

# A shallow logistics regression models
model = Sequential()
model.add(Dense(13, input_shape=(30, ), activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer=Adam(lr=0.001),
              metrics=['accuracy'])

# early stopping and early stopper
earlystopper = EarlyStopping(monitor='val_loss',
                             min_delta=0,
                             patience=15,
                             verbose=1,
                             mode='auto')

# fit model with early stopper with 2000
history = model.fit(X_train,
                    y_train,
                    epochs=2000,
                    validation_split=0.15,
                    verbose=0,
                    callbacks=[earlystopper])
history_dict = history.history

# plot the loss
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
Ejemplo n.º 5
0
Archivo: test.py Proyecto: coco1578/IDA
    result_accuracy_test = list()
    result_f1_train = list()
    result_f1_test = list()
    result_precision_train = list()
    result_precision_test = list()
    result_recall_train = list()
    result_recall_test = list()
    # result_balanced_error_train = list()
    # result_balanced_error_test = list()

    X, y, number_of_classes = preprocess_data(dataset_path,
                                              resize_shape=(1, 3184))

    kf = StratifiedKFold(n_splits=CV, shuffle=True)
    early_stopping = EarlyStopping(monitor='val_loss',
                                   mode='auto',
                                   patience=10)
    mcp = ModelCheckpoint('/Users/coco/Desktop/best_model.h5',
                          save_best_only=True,
                          monitor='val_loss',
                          mode='min')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss',
                                       factor=0.1,
                                       patience=10,
                                       verbose=1,
                                       epsilon=1e-4,
                                       mode='min')

    for i, (idx_train, idx_test) in enumerate(kf.split(X, y)):

        X_train = X[idx_train]
Ejemplo n.º 6
0
TRAINING
==================================================================================
Change filepath accordingly
"""

from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
filepath = 'weight' + filename_extension + '.h5'

checkpoint = ModelCheckpoint(filepath,
                             monitor='val_accuracy',
                             verbose=1,
                             save_best_only=True,
                             mode='max')

early = EarlyStopping(monitor='val_loss',
                      mode='min',
                      patience=40,
                      restore_best_weights=True)

callbacks_list = [checkpoint, early]
"""
Decalre input size and number of classes
"""
input_shape = (nrows, ncolumns, 1)
nclass = 24
"""
SELECT YOUR MODEL
"""
model = from_paper(input_shape, nclass)

history = model.fit_generator(train_gen,
                              steps_per_epoch=nclass * 1000 / batch_size,
def gridSearchLstmKer(X_train, X_test, y_train, y_test, y, dummy_y):
    lstm_units = [32, 64]
    kernels = [3, 5, 7]
    """constant parameters"""
    num_classes = dummy_y.shape[1]
    class_weight = compute_class_weight('balanced', np.unique(y), y)
    num_classes = dummy_y.shape[1]
    input_shape = X_train.shape

    grid_search_lst = list(itertools.product(*[lstm_units, kernels]))

    batch_size = 100
    epochs = 100

    for lst, ker in grid_search_lst:

        model = create_model(num_classes=num_classes,
                             num_lstm_units=lst,
                             X_train_shape=input_shape,
                             kernel_size=ker)

        E_Stop = EarlyStopping(monitor='val_loss',
                               min_delta=0,
                               patience=10,
                               verbose=0,
                               mode='auto')

        checkpointer = ModelCheckpoint(
            filepath='results/{0}_{1}.weights.best.hdf5'.format(lst, ker),
            verbose=1,
            save_best_only=True)

        model.fit(X_train,
                  y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  validation_data=(X_test, y_test),
                  class_weight=class_weight,
                  callbacks=[checkpointer, E_Stop],
                  shuffle=True)

    # now lets test things and output the results into the text file.

    class_weight_norm = class_weight / np.linalg.norm(class_weight)

    f = open('out_inverse_and_mean.txt', 'w')
    for lst, ker in grid_search_lst:
        f.write(
            'For the model with lstm units = {0} and kernels = {1} \n'.format(
                lst, ker))

        model = create_model(num_classes=num_classes,
                             num_lstm_units=lst,
                             X_train_shape=input_shape,
                             kernel_size=ker)
        model.load_weights('results/{0}_{1}.weights.best.hdf5'.format(
            lst, ker))
        score = model.evaluate(X_test, y_test, verbose=0)
        f.write(
            'The total loss is {0} and the average accuracy is {1} \n'.format(
                score[0], score[1]))

        y_pred = np_utils.to_categorical(model.predict_classes(X_test))
        precision, recall, fscore, _ = precision_recall_fscore_support(
            y_test, y_pred)
        f.write('The precision is {0} and average precision is {1}\n'.format(
            precision, precision.mean()))
        f.write('The recall is {0} and the average recall is {1}\n'.format(
            recall, recall.mean()))
        f.write(
            'The fscore is {0} , the average fscore is {1}, the inverse weighted fscore is {2}'
            .format(fscore, fscore.mean(), np.inner(class_weight_norm,
                                                    fscore)))

        f.write('\n \n \n')

    f.close()
    def on_train_begin(self, logs={}):
        self.losses = []
        self.val_losses = []
        self.acc = []
        self.val_acc = []

    def on_epoch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
        self.val_losses.append(logs.get('val_loss'))
        self.acc.append(logs.get('acc'))
        self.val_acc.append(logs.get('val_acc'))


# Early stopping if the validation loss doesn't decrease anymore
early_stopping = EarlyStopping(monitor='val_loss',
                               patience=EARLY_SOPPING_PATIENCE,
                               verbose=1,
                               mode='min')
# We just save the best model, not the last one used
filepath = SAVE_MODEL_DIR + SAVE_MODEL_WEIGHT_NAME
model_checkpoint = ModelCheckpoint(filepath=filepath,
                                   monitor='val_loss',
                                   save_best_only=True,
                                   verbose=1,
                                   mode='min',
                                   save_weights_only=True)

# Generate a ROC Curve


def generate_results(y_test, y_score):
    fpr, tpr, _ = roc_curve(y_test, y_score)
Ejemplo n.º 9
0
#print(y_train)
#for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=16): # chunks of 32 samples
#print(datagen.flow(X_train, y_train, batch_size=16))
#loss = model.train_on_batch(X_batch, y_batch)
# k=k+1
#print(k)
# model.fit(X_train,y_train,verbose=2, validation_split=0.25, shuffle= True)

#print(loss)

import keras.callbacks
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import ModelCheckpoint

callbacks = [
    EarlyStopping(monitor='val_loss', patience=10, verbose=1),
    ReduceLROnPlateau(monitor='val_loss',
                      factor=0.1,
                      patience=3,
                      min_lr=0.00000001,
                      verbose=1),
    ModelCheckpoint('/content/drive/My Drive/model_yolo.h5',
                    monitor='val_loss',
                    verbose=1,
                    save_best_only=True,
                    save_weights_only=False)
]

model.fit_generator(DATA_GEN(X_train_samples, y_train_samples, batch_size=12),
                    steps_per_epoch=875,
                    epochs=100,
Ejemplo n.º 10
0
                                                   target_size=size1,
                                                   batch_size=batch_size,
                                                   class_mode='categorical')


from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, EarlyStopping

checkpoint = ModelCheckpoint('weights.{epoch:02d}.hdf5',
                            save_best_only=False,
                            verbose=1)


early_stop = EarlyStopping(monitor='val_loss',
                          min_delta=0,
                          patience=3,
                          verbose=1,
                          restore_best_weights=True)

callbacks = [checkpoint]

model.compile(loss='categorical_crossentropy',
               optimizer=Adam(lr=1e-3), metrics=['accuracy'])

hostory = model.fit_generator(train_generator,
                             steps_per_epoch=n_train//batch_size,
                              epochs=epochs,
                             callbacks = callbacks,
                             validation_data = validation_generator,
                             validation_steps = n_validation//batch_size)
Ejemplo n.º 11
0
trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(32, input_shape=(look_back, 1)))
# model.add(LSTM(16, return_sequences=True))
# model.add(LSTM(16))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['acc'])
model.fit(trainX,
          trainY,
          epochs=200,
          batch_size=1,
          verbose=2,
          callbacks=[EarlyStopping(monitor='loss', patience=2), tensorboard])
# make predictions

trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:, 0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:, 0]))
print('Test Score: %.2f RMSE' % (testScore))
Ejemplo n.º 12
0
    df_train.inc_angle = df_train.inc_angle.replace('na', 0)
    idx_tr = np.where(df_train.inc_angle > 0)

    Ytrain = Ytrain[idx_tr[0]]
    Xtrain = Xtrain[idx_tr[0], ...]

    Xtr_more = get_more_images(Xtrain)
    Ytr_more = np.concatenate((Ytrain, Ytrain, Ytrain))

    model = getModel()
    model.summary()

    batch_size = 32
    earlyStopping = EarlyStopping(monitor='val_loss',
                                  patience=10,
                                  verbose=0,
                                  mode='min')
    mcp_save = ModelCheckpoint('.mdl_wts.hdf5',
                               save_best_only=True,
                               monitor='val_loss',
                               mode='min')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss',
                                       factor=0.1,
                                       patience=7,
                                       verbose=1,
                                       epsilon=1e-4,
                                       mode='min')

    model.fit(Xtr_more,
              Ytr_more,
              batch_size=batch_size,
Ejemplo n.º 13
0
from keras.layers import Dense, GlobalAveragePooling2D
from keras.callbacks import ModelCheckpoint, TensorBoard, EarlyStopping, CSVLogger
from data import DataSet
import time
import os.path

data = DataSet()

# Helper: Save the model.
checkpointer = ModelCheckpoint(filepath=os.path.join(
    'data', 'checkpoints', 'inception.{epoch:03d}-{val_loss:.2f}.hdf5'),
                               verbose=1,
                               save_best_only=True)

# Helper: Stop when we stop learning.
early_stopper = EarlyStopping(patience=10)

# Helper: TensorBoard
tensorboard = TensorBoard(log_dir=os.path.join('data', 'logs'))


def get_generators():
    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       shear_range=0.2,
                                       horizontal_flip=True,
                                       rotation_range=10.,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2)

    test_datagen = ImageDataGenerator(rescale=1. / 255)
Ejemplo n.º 14
0
all_error_data = np.concatenate(
    (error_question_sequence, error_answer_sequence, error_label), axis=1)
all_data = np.concatenate((all_correct_data, all_error_data), axis=0)
np.random.shuffle(all_data)
# all_data = all_data[0:400000,:]
Q, R, L, Q_val, R_val, L_val = split_data(all_data[:, 0:15],
                                          all_data[:, 15:30], all_data[:, 30],
                                          all_data.shape[0], 0.1)

embedding_matrix = create_embedding_matrix(word_dict, dim)
dual_encoder_model = dual_encoder(15, embedding_matrix,
                                  len(word_dict.wv.vocab) + 1)
# earlystopping = EarlyStopping(monitor='val_acc', patience=8, verbose=2, mode='max')
# checkpoint = ModelCheckpoint("./earlystop_model.h5", monitor='val_acc', save_best_only=True, verbose=0, mode='max')
earlystopping = EarlyStopping(monitor='val_loss',
                              patience=5,
                              verbose=2,
                              mode='min')
checkpoint = ModelCheckpoint("./earlystop_model.h5",
                             monitor='val_loss',
                             save_best_only=True,
                             verbose=0,
                             mode='min')
dual_encoder_model.fit([Q, R],
                       L,
                       validation_data=([Q_val, R_val], L_val),
                       batch_size=1000,
                       epochs=100,
                       verbose=2,
                       callbacks=[checkpoint, earlystopping])
model = load_model("./earlystop_model.h5")
Ejemplo n.º 15
0
trials = Trials()
best2 = fmin(objective, space2, algo=tpe.suggest, trials=trials, max_evals=10)

#Building Stateful Model
lstm_hidden = hyperopt.space_eval(space2, best2)
print lstm_hidden
tsteps2 = std_inv / std_inv2

#Building model
lstm_model = build_lstm_v1.lstm_multi_104(lstm_hidden, train_data2.shape[2],
                                          H_t2.shape[2], tsteps2)
save_model = lstm_model

##callbacks for Early Stopping
callbacks = [EarlyStopping(monitor='val_loss', patience=5)]

#parameters for simulation
attempt_max = 5
epoch_max = 100
min_epoch = 10

#Criterion for early stopping
tau = 3
e_mat = numpy.zeros((epoch_max, attempt_max))
e_temp = numpy.zeros((tau, ))

tol = 0
count = 0
val_loss_v = []
epsilon = 1  #initialzing error
Ejemplo n.º 16
0
def RNN():
    inputs = Input(name='inputs', shape=[max_len])
    layer = Embedding(max_words, 50, input_length=max_len)(inputs)
    layer = GRU(64)(layer)
    layer = Dense(256, name='FC1')(layer)
    layer = Activation('relu')(layer)
    layer = Dropout(0.5)(layer)
    layer = Dense(1, name='out_layer')(layer)
    layer = Activation('sigmoid')(layer)
    model = Model(inputs=inputs, outputs=layer)
    return model


model = RNN()
model.summary()
model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

model.fit(sequences_matrix,
          Y_train,
          batch_size=512,
          epochs=10,
          validation_split=0.05,
          callbacks=[EarlyStopping(monitor='val_loss', min_delta=10 ^ -5)])

model.save('lstm.h5')
test_sequences = tok.texts_to_sequences(X_test)
test_sequences_matrix = sequence.pad_sequences(test_sequences, maxlen=max_len)
accr = model.evaluate(test_sequences_matrix, Y_test)
Ejemplo n.º 17
0
    # Flatten
    model.add(Flatten())
    # FC layers
    model.add(Dense(100, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(10, activation='relu'))
    model.add(Dense(1))

    return model

model = nvidia_model()
# Use Adam optimizer to minimize the mean squared error
model.compile(optimizer='adam', loss='mse')

# use early stopping to avoid overfitting
early_stopping = EarlyStopping(monitor='val_loss', patience=PATIENCE)
# trian the model
history_object = model.fit_generator(train_generator,
                                     steps_per_epoch=train_samples.shape[0] // BATCH_SIZE,
                                     validation_data=valid_generator,
                                     validation_steps=valid_samples.shape[0] // BATCH_SIZE,
                                     epochs=EPOCHS,
                                     verbose=1,
                                     callbacks=[early_stopping])

model.save('model.h5')

# # print the keys contained in the history object
# print(history_object.history.keys())

# # plot the training and validation loss for each epoch
Ejemplo n.º 18
0
def make_predictions(X, Y, val_X, val_Y, test_X, test_Y, s, test_ids):

    cl_w = compute_class_weight('balanced', np.unique(Y), Y)
    earlystop = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=patience, \
                          verbose=1, mode='auto')

    print('Build model CNN model')

    in_txt = Input(name='in_norm',
                   batch_shape=tuple([None, maxlen]),
                   dtype='int32')

    # init with pre-trained embeddings
    emb_char = Embedding(len(word2index),
                         embedding_dims,
                         embeddings_initializer=Constant(embedding_matrix),
                         trainable=True,
                         input_length=maxlen,
                         name='emb_char')

    emb_seq = emb_char(in_txt)

    z = Dropout(dropout_prob[0])(emb_seq)

    # convolutional block
    conv_blocks = []
    for sz in filter_sizes:
        conv = Convolution1D(filters=num_filters,
                             kernel_size=sz,
                             padding="valid",
                             activation="relu",
                             strides=1,
                             kernel_regularizer=regularizers.l2(0.01),
                             kernel_initializer=initializer_func)(z)
        conv = MaxPooling1D(pool_size=2)(conv)
        conv = Flatten()(conv)
        conv_blocks.append(conv)
    z = Concatenate()(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]

    z = Dropout(dropout_prob[1])(z)
    z = Dense(hidden_dims,
              activation="relu",
              kernel_regularizer=regularizers.l2(0.01),
              kernel_initializer=initializer_func)(z)

    out_soft = Dense(1,
                     activation='sigmoid',
                     name='out_soft',
                     kernel_initializer=initializer_func,
                     kernel_regularizer=regularizers.l2(0.01))(z)

    model = Model(inputs=in_txt, outputs=out_soft)

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

    model.fit(X,
              Y,
              batch_size=batch_size,
              epochs=epochs,
              validation_data=(val_X, val_Y),
              class_weight={
                  0: cl_w[0],
                  1: cl_w[1]
              },
              callbacks=[earlystop],
              verbose=0)

    y_hat = model.predict(val_X, batch_size=batch_size)
    y_hat = y_hat.flatten()
    res_list = {}
    thresholds = np.arange(0, 1, 0.1)

    f1_prod_list = []

    for p in thresholds:

        y_pred = []

        for y in y_hat:

            if y >= p:
                y_pred.append(1)
            else:
                y_pred.append(0)

        y_pred = np.array(y_pred)

        from sklearn.metrics import precision_recall_fscore_support

        precision, recall, f1, _ = precision_recall_fscore_support(
            val_Y, y_pred, average=None)
        f1_prod_list.append(np.prod(f1))
        res_list[p] = y_pred

    f1_prod_list = np.array(f1_prod_list)

    max_f1 = np.argmax(f1_prod_list)
    print('Positive class probability threshold %.4f' % thresholds[max_f1])
    p = thresholds[max_f1]
    y_hat = model.predict(test_X, batch_size=batch_size)
    y_hat = y_hat.flatten()
    y_pred = []

    for y in y_hat:

        if y >= p:
            y_pred.append(1)
        else:
            y_pred.append(0)

    y_pred = np.array(y_pred)
    test_y_hat = y_pred

    prec, recall, fm, support = precision_recall_fscore_support(
        test_Y, test_y_hat)

    print('F-measure')
    print(fm)
    print('Precision')
    print(prec)
    print('Recall')
    print(recall)
    print('Stat')
    print(support)

    accuracy_score = sklearn.metrics.accuracy_score(test_Y, test_y_hat)
    print('accuracy_score: {0}'.format(accuracy_score))

    roc_auc_score = sklearn.metrics.roc_auc_score(test_Y, test_y_hat)
    print('roc_auc_score: {0}'.format(roc_auc_score))

    false_positive_rate, true_positive_rate, thresholds_roc = sklearn.metrics.roc_curve(
        test_Y, test_y_hat)

    false_pos = []
    false_neg = []

    false_pos_probs = []
    false_neg_probs = []

    pos_class_probs = []

    pos_class_probs_low = []
    pos_class_probs_high = []

    y_hat_proba = y_hat

    pos_class_probs = np.array(y_hat_proba)
    threshold = thresholds[max_f1]

    for proba in pos_class_probs:
        if proba >= threshold:
            pos_class_probs_high.append(proba)
        else:
            pos_class_probs_low.append(proba)

    pos_class_probs_low = np.array(pos_class_probs_low)
    pos_class_probs_high = np.array(pos_class_probs_high)

    threshold_low = np.median(pos_class_probs_low)
    threshold_high = np.median(pos_class_probs_high)

    for n, proba in enumerate(pos_class_probs):
        if proba <= threshold_low and test_Y[n] == 1:
            false_neg.append(test_ids[n])
            false_neg_probs.append(proba)

        elif proba >= threshold_high and test_Y[n] == 0:
            false_pos.append(test_ids[n])
            false_pos_probs.append(proba)

    print('Positive class proba distribution')
    #print(pos_class_probs)
    pos_class_probs = np.array(pos_class_probs)
    print('stat')
    print(st.describe(pos_class_probs))
    print('median')
    print(np.median(pos_class_probs))
    print('\n')

    print('False negatives with p of positive class <= %.3f' % threshold_low)
    print('admission ids')
    print(false_neg)
    if len(false_neg_probs) > 0:
        print('p distribution')
        # print(false_neg_probs)
        false_neg_probs = np.array(false_neg_probs)
        print('stat')
        print(st.describe(false_neg_probs))
        print('median')
        print(np.median(false_neg_probs))
        print('\n')

    print('False positives with p of positive class >= %.3f' % threshold_high)
    print('admission ids')
    print(false_pos)
    if len(false_pos_probs) > 0:
        print('p distribution')
        # print(false_pos_probs)
        false_pos_probs = np.array(false_pos_probs)
        print('stat')
        print(st.describe(false_pos_probs))
        print('median')
        print(np.median(false_pos_probs))
        print('\n')

    return fm[1], prec[1], recall[1], roc_auc_score
Ejemplo n.º 19
0
def train(data_dir,
          output_dir,
          model_path='',
          batch_size=32,
          num_epochs=100,
          patience=5,
          verbose=False):
    """
    Trains the model.

    Args:
        data_dir (str): Directory where the data lives.
        output_dir (str): Directory to save model checkpoints.
    Args (optional):
        model_path (str): Path to model to load. If none specified, a new model
            will be used.
        batch_size (int): Batch size to use while training.
        num_epochs (int): Number of epochs to train for.
        patience (int): Number of epochs without loss decrease that can occur
            before training is stopped early.
    """

    # Validate output directory
    if not os.path.exists(output_dir):
        if verbose:
            print("No directory at '{}', creating new directory".format(
                output_dir))
        os.makedirs(output_dir)
    elif os.path.isfile(output_dir):
        raise RuntimeError(
            "Output directory '{}' is a file!".format(output_dir))

    # Create or load model
    if model_path:
        if verbose:
            print("Loading model from '{}'".format(model_path))
        model = load_model(model_path)
    else:
        if verbose:
            print("Building new model")
        model = build_model()

    info = parse_csv(data_dir)

    if verbose:
        print("Found {} paintings with {} artists and {} styles".format(
            len(info['entries']), len(info['artists']), len(info['styles'])))

    # Insert epoch format field to model name
    checkpoint_path = os.path.join(output_dir, 'Painter.model.e{epoch:02d}.h5')

    callbacks = [
        EarlyStopping(monitor='loss', patience=patience),
        ModelCheckpoint(checkpoint_path,
                        monitor='loss',
                        verbose=0,
                        save_best_only=True),
    ]

    # Compute the number of samples for each epoch, shaving off the samples
    # from the last batch
    samples_per_epoch = 2 * len(info['entries'])
    samples_per_epoch -= samples_per_epoch % batch_size

    model.fit_generator(
        training_data_generator(
            data_dir,
            info,
            batch_size=batch_size,
            input_size=model.input_shape[0][1:],
        ),
        samples_per_epoch=samples_per_epoch,
        nb_epoch=num_epochs,
        callbacks=callbacks,
        verbose=1,
    )
Ejemplo n.º 20
0
model.add(BatchNormalization())
model.add(Conv1D(filters=128, kernel_size=11, activation='relu'))
model.add(MaxPool1D(strides=2))

model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()

#model compile and train
model.compile(optimizer='SGD', loss = 'binary_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=512, epochs=50, class_weight = 'auto', validation_data=(X_val, y_val),
          callbacks=([EarlyStopping(monitor = 'val_loss',mode = 'min',verbose = 1, patience = 20),
                      ModelCheckpoint("CNN_model.hdf5", monitor="val_loss", save_best_only =True)]))

# plot loss
model = lm('CNN_model.hdf5')
plt.style.use('ggplot')
plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
plt.savefig("Model loss.png")
def gridSearchBatchSize(X_train, X_test, y_train, y_test, y, dummy_y, lstm,
                        ker):

    num_classes = dummy_y.shape[1]
    input_shape = X_train.shape
    class_weight = compute_class_weight('balanced', np.unique(y), y)
    class_weight_norm = class_weight / np.linalg.norm(class_weight)

    batch_size_lst = [64, 128, 256]

    for batch in batch_size_lst:

        if Path('results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(
                lstm, ker, batch)).is_file():
            continue

        model = create_model(num_classes=num_classes,
                             num_lstm_units=lstm,
                             X_train_shape=input_shape,
                             kernel_size=ker)

        E_Stop = EarlyStopping(monitor='val_loss',
                               min_delta=0,
                               patience=10,
                               verbose=0,
                               mode='auto')

        checkpointer = ModelCheckpoint(
            filepath='results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(
                lstm, ker, batch),
            verbose=1,
            save_best_only=True)

        model.fit(X_train,
                  y_train,
                  batch_size=batch,
                  epochs=epochs,
                  validation_data=(X_test, y_test),
                  class_weight=class_weight,
                  callbacks=[checkpointer, E_Stop],
                  shuffle=True)

    # Training is done, now let's test all of them in put the results in a file

    f = open('out_inverse_and_mean_batch.txt', 'w')

    for b in batch_size_lst:
        f.write(
            'For the model with lstm units = {0} and kernels = {1} and batch size {2}\n'
            .format(lstm, ker, b))

        model = create_model(num_classes=num_classes,
                             num_lstm_units=lstm,
                             X_train_shape=input_shape,
                             kernel_size=ker)
        model.load_weights(
            'results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(lstm, ker, b))
        score = model.evaluate(X_test, y_test, verbose=0)
        f.write(
            'The total loss is {0} and the average accuracy is {1} \n'.format(
                score[0], score[1]))

        y_pred = np_utils.to_categorical(model.predict_classes(X_test))
        precision, recall, fscore, _ = precision_recall_fscore_support(
            y_test, y_pred)

        f.write('The precision is {0} and average precision is {1}\n'.format(
            precision, precision.mean()))
        f.write('The recall is {0} and the average recall is {1}\n'.format(
            recall, recall.mean()))
        f.write(
            'The fscore is {0} , the average fscore is {1}, the inverse weighted fscore is {2}'
            .format(fscore, fscore.mean(), np.inner(class_weight_norm,
                                                    fscore)))

        f.write('\n \n \n')

    f.close()
Ejemplo n.º 22
0
x2 = Dense(100)(input2)
x2 = Dense(1000)(x2)
x2 = Dense(3000)(x2)
x2 = Dense(5000)(x2)

merge = concatenate([x1, x2])

output = Dense(1)(merge)

model = Model(inputs=[input1, input2], outputs=output)

# model.summary()

#3. 컴파일, 훈련
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
early_stop = EarlyStopping(monitor='loss', patience=3, mode='auto')
modelpath = './model/test0602/{epoch:02d}-{val_loss:.4f}.hdf5'
check_p = ModelCheckpoint(filepath=modelpath, monitor='val_loss', save_best_only=True, save_weights_only=False)
tb_hist = TensorBoard(log_dir='graph', histogram_freq=0, write_graph=True, write_images=True)
model.fit([x1_train, x2_train], y_train, epochs=100, batch_size=1, validation_split=0.2, callbacks=[early_stop, check_p])

#4. 평가, 예측
loss, mse = model.evaluate([x1_test, x2_test], y_test)

# print(x1_test[-1, :])       # [49700 49000 48500 47600 47200] 벡터
x1_pred = x1_test[-1, :].reshape(-1,5)
print(x1_pred)              # [[49700 49000 48500 47600 47200]] 행렬
# 벡터를 2차원으로 reshape ==> 행렬
x2_pred = x2_test[-1, :].reshape(-1,5)
print(x2_pred)              # [[19900 20150 19800 20100 254623]] 행렬
# 벡터를 2차원으로 reshape ==> 행렬
Ejemplo n.º 23
0
def _main():
    annotation_path = 'train.txt'
    log_dir = 'logs/000/'
    classes_path = 'model_data/voc_classes.txt'
    anchors_path = 'model_data/tiny_yolo_anchors.txt'
    class_names = get_classes(classes_path)
    num_classes = len(class_names)
    anchors = get_anchors(anchors_path)

    input_shape = (416, 416)  # multiple of 32, hw

    is_tiny_version = len(anchors) == 6  # default setting
    if is_tiny_version:
        model = create_tiny_model(
            input_shape,
            anchors,
            num_classes,
            freeze_body=2,
            weights_path='model_data/tiny_yolo_weights.h5')
    else:
        model = create_model(input_shape,
                             anchors,
                             num_classes,
                             freeze_body=2,
                             weights_path='model_data/yolo_weights.h5'
                             )  # make sure you know what you freeze

    logging = TensorBoard(log_dir=log_dir)
    checkpoint = ModelCheckpoint(
        log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5',
        monitor='val_loss',
        save_weights_only=True,
        save_best_only=True,
        period=3)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.1,
                                  patience=3,
                                  verbose=1)
    early_stopping = EarlyStopping(monitor='val_loss',
                                   min_delta=0,
                                   patience=10,
                                   verbose=1)

    val_split = 0.1
    with open(annotation_path) as f:
        lines = f.readlines()
    np.random.seed(10101)
    np.random.shuffle(lines)
    np.random.seed(None)
    num_val = int(len(lines) * val_split)
    num_train = len(lines) - num_val

    batch_size = 16
    #im = data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes)

    for num in data_generator_wrapper(lines[:num_train], batch_size,
                                      input_shape, anchors, num_classes):
        #print(num)
        im, b = num
        print("img")
        print(im[0][0][0][0])
        #print("label")
        #print(im[1])
        #print("bat")
        #print(b)

    #print(list(im))

# print(y_t)
#print(b)

# Train with frozen layers first, to get a stable loss.
# Adjust num epochs to your dataset. This step is enough to obtain a not bad model.
    if False:
        model.compile(
            optimizer=Adam(lr=1e-3),
            loss={
                # use custom yolo_loss Lambda layer.
                'yolo_loss': lambda y_true, y_pred: y_pred
            })

        batch_size = 16  #32
        print('Train on {} samples, val on {} samples, with batch size {}.'.
              format(num_train, num_val, batch_size))
        model.fit_generator(data_generator_wrapper(lines[:num_train],
                                                   batch_size, input_shape,
                                                   anchors, num_classes),
                            steps_per_epoch=max(1, num_train // batch_size),
                            validation_data=data_generator_wrapper(
                                lines[num_train:], batch_size, input_shape,
                                anchors, num_classes),
                            validation_steps=max(1, num_val // batch_size),
                            epochs=50,
                            initial_epoch=0,
                            callbacks=[logging, checkpoint])
        model.save_weights(log_dir + 'trained_weights_stage_1.h5')

    # Unfreeze and continue training, to fine-tune.
    # Train longer if the result is not good.
    if False:
        for i in range(len(model.layers)):
            model.layers[i].trainable = True
        model.compile(optimizer=Adam(lr=1e-4),
                      loss={
                          'yolo_loss': lambda y_true, y_pred: y_pred
                      })  # recompile to apply the change
        print('Unfreeze all of the layers.')

        batch_size = 4  #32 note that more GPU memory is required after unfreezing the body
        print('Train on {} samples, val on {} samples, with batch size {}.'.
              format(num_train, num_val, batch_size))
        model.fit_generator(
            data_generator_wrapper(lines[:num_train], batch_size, input_shape,
                                   anchors, num_classes),
            steps_per_epoch=max(1, num_train // batch_size),
            validation_data=data_generator_wrapper(lines[num_train:],
                                                   batch_size, input_shape,
                                                   anchors, num_classes),
            validation_steps=max(1, num_val // batch_size),
            epochs=100,
            initial_epoch=50,
            callbacks=[logging, checkpoint, reduce_lr, early_stopping])
        model.save_weights(log_dir + 'trained_weights_final.h5')
Ejemplo n.º 24
0
    model.compile(loss=nn_loss,
                  optimizer=model_params['optimizer'],
                  metrics=model_params['metrics'])

    model_checkpoint_file = model_params['model_folder'] + str(
        len(models)) + '_' + model_params['model_weights_file'] + '.h5'
    callbacks = [
        ModelCheckpoint(model_checkpoint_file,
                        monitor=model_params['monitor'],
                        verbose=1,
                        save_best_only=True,
                        mode=model_params['monitor_mode']),
        EarlyStopping(monitor=model_params['monitor'],
                      min_delta=0.00001,
                      verbose=1,
                      mode=model_params['monitor_mode'],
                      patience=model_params['es_patience']),
        TensorBoard(log_dir='{}{}'.format(
            TENSORBOARD_DIR, model_params['model_folder'].split('/')[-2]),
                    histogram_freq=0,
                    write_graph=True,
                    write_grads=1,
                    batch_size=model_params['batch_size'],
                    write_images=True),
        CSVLogger(model_params['model_folder'] + 'log.csv',
                  separator=',',
                  append=True),
        ReduceLROnPlateau(monitor=model_params['monitor'],
                          mode=model_params['monitor_mode'],
                          factor=0.5,
Ejemplo n.º 25
0
    def _model_constructor(self):
        ########################################
        ## sample train/validation data
        ########################################
        data_1, data_2, labels, _, _, _ = self._preprocess_data()

        #UPDownSampling
        pos_train = data_1[labels == 1]
        neg_train = data_1[labels == 0]
        data_1 = np.concatenate(
            (neg_train, pos_train[:int(0.8 * len(pos_train))], neg_train))
        del pos_train, neg_train
        pos_train = data_2[labels == 1]
        neg_train = data_2[labels == 0]
        data_2 = np.concatenate(
            (neg_train, pos_train[:int(0.8 * len(pos_train))], neg_train))
        del pos_train, neg_train
        pos_train = self.train_custom_features[labels == 1]
        neg_train = self.train_custom_features[labels == 0]
        self.train_custom_features = pd.concat(
            (neg_train, pos_train.iloc[:int(0.8 * len(pos_train))],
             neg_train)).as_matrix()

        labels = np.array([0] * neg_train.shape[0] + [1] *
                          pos_train[:int(0.8 * len(pos_train))].shape[0] +
                          [0] * neg_train.shape[0])
        del pos_train, neg_train
        print("Current duplicate content in the train set:", np.mean(labels))

        #self.train_custom_features = self.train_custom_features.as_matrix()

        perm = np.random.permutation(len(data_1))

        idx_train = perm[:int(len(data_1) * (1 - self.VALIDATION_SPLIT))]
        idx_val = perm[int(len(data_1) * (1 - self.VALIDATION_SPLIT)):]

        data_1_train = np.vstack((data_1[idx_train], data_2[idx_train]))
        data_2_train = np.vstack((data_2[idx_train], data_1[idx_train]))

        features_train = np.vstack((self.train_custom_features[idx_train],
                                    self.train_custom_features[idx_train]))

        labels_train = np.concatenate((labels[idx_train], labels[idx_train]))

        data_1_val = np.vstack((data_1[idx_val], data_2[idx_val]))
        data_2_val = np.vstack((data_2[idx_val], data_1[idx_val]))

        features_val = np.vstack((self.train_custom_features[idx_val],
                                  self.train_custom_features[idx_val]))

        labels_val = np.concatenate((labels[idx_val], labels[idx_val]))

        weight_val = np.ones(len(labels_val))
        if self.REWEIGH:
            weight_val *= 0.472001959
            weight_val[labels_val == 0] = 1.309028344

        ########################################
        ## define the model structure
        ########################################

        embedding_layer = self._create_embedding_layer()
        # lstm_layer = LSTM(self.NUM_LSTM,
        #                   dropout=self.RATE_DROP_LSTM,
        #                   recurrent_dropout=self.RATE_DROP_LSTM)
        convolution_layer = Conv1D(filters=NB_FILTER,
                                   kernel_size=FILTER_LENGTH,
                                   padding='valid',
                                   strides=1)
        timedist_layer = TimeDistributed(
            Dense(self.EMBEDDING_DIM, activation=self.RECTIFIER))
        lambda_layer = Lambda(lambda x: K.max(x, axis=1),
                              output_shape=(self.EMBEDDING_DIM, ))

        custom_dim = int(self.train_custom_features.shape[1])
        custom_input = Input(shape=(custom_dim, ), dtype='float32')
        custom_features = Dense(self.NUM_DENSE,
                                kernel_initializer='normal',
                                activation=self.RECTIFIER)(custom_input)
        custom_features = Dropout(self.RATE_DROP_DENSE)(custom_features)
        custom_features = BatchNormalization()(custom_features)

        sequence_1_input = Input(shape=(self.MAX_SEQUENCE_LENGTH, ),
                                 dtype='float32')
        embedded_sequences_1 = embedding_layer(sequence_1_input)
        Q1 = convolution_layer(embedded_sequences_1)
        Q1 = PReLU()(Q1)
        Q1 = GlobalMaxPooling1D()(Q1)
        Q1 = Dropout(self.RATE_DROP_DENSE)(Q1)

        # sequence_3_input = Input(shape=(self.MAX_SEQUENCE_LENGTH,),
        #                          dtype='float32')
        # embedded_sequences_3 = embedding_layer(sequence_3_input)
        # timedisted_sequences_3 = timedist_layer(embedded_sequences_3)
        # AQ1 = lambda_layer(timedisted_sequences_3)

        sequence_2_input = Input(shape=(self.MAX_SEQUENCE_LENGTH, ),
                                 dtype='float32')
        embedded_sequences_2 = embedding_layer(sequence_2_input)
        Q2 = convolution_layer(embedded_sequences_2)
        Q2 = PReLU()(Q2)
        Q2 = GlobalMaxPooling1D()(Q2)
        Q2 = Dropout(self.RATE_DROP_DENSE)(Q2)

        # sequence_4_input = Input(shape=(self.MAX_SEQUENCE_LENGTH,),
        #                          dtype='float32')
        # embedded_sequences_4 = embedding_layer(sequence_4_input)
        # timedisted_sequences_4 = timedist_layer(embedded_sequences_4)
        # AQ2 = lambda_layer(timedisted_sequences_4)

        merged = concatenate([Q1, Q2, custom_features])
        merged = BatchNormalization()(merged)

        merged = Dense(self.NUM_DENSE, kernel_initializer='normal')(merged)
        merged = PReLU()(merged)
        merged = Dropout(self.RATE_DROP_DENSE)(merged)
        merged = BatchNormalization()(merged)

        merged = Dense(round(0.8 * self.NUM_DENSE),
                       kernel_initializer='normal')(merged)
        merged = PReLU()(merged)
        merged = Dropout(self.RATE_DROP_DENSE)(merged)
        merged = BatchNormalization()(merged)

        merged = Dense(round(0.6 * self.NUM_DENSE),
                       kernel_initializer='normal')(merged)
        merged = PReLU()(merged)
        merged = Dropout(self.RATE_DROP_DENSE)(merged)
        merged = BatchNormalization()(merged)

        preds = Dense(1, activation='sigmoid')(merged)

        ########################################
        ## construct the model
        ########################################
        model = Model(inputs=[sequence_1_input,
                              sequence_2_input,
                              custom_input], \
                      outputs=preds)
        model.load_weights("sin_323_0.47.h5")
        adam = optimizers.Adam(clipnorm=1.)

        model.compile(loss='binary_crossentropy',
                      optimizer=adam,
                      metrics=['acc'])
        #model.summary()
        print("The model {} is built.".format(self.STAMP))

        ########################################
        ## add class weight
        ########################################
        if self.REWEIGH:
            class_weight = {0: 1.309028344, 1: 0.472001959}
        else:
            class_weight = None

        early_stopping = EarlyStopping(monitor='val_loss', patience=0)
        bst_model_path = self.STAMP + '.h5'
        model_checkpoint = ModelCheckpoint(bst_model_path,
                                           save_best_only=True,
                                           save_weights_only=True)

        hist = model.fit(
            [data_1_train, data_2_train, features_train],
            labels_train,
            validation_data=([data_1_val, data_2_val,
                              features_val], labels_val, weight_val),
            epochs=4,
            batch_size=512,
            shuffle=True,
            class_weight=class_weight,
            callbacks=[early_stopping, model_checkpoint])

        model.load_weights(bst_model_path)
        bst_val_score = min(hist.history['val_loss'])
        return (model, hist, bst_val_score)
Ejemplo n.º 26
0
test_generator = test_datagen.flow_from_directory(test_folder,
                                            target_size = (image_size, image_size),
                                            batch_size = test_batchsize,
                                            class_mode = 'categorical',
                                            shuffle = False)


# In[10]:


from keras.optimizers import Adam, SGD, RMSprop
from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping

save_path = "saved-weights-{epoch:02d}-{val_acc:.2f}.hdf5"
my_opt = Adam(lr=0.001, decay=1e-5)
early_stop = EarlyStopping(monitor='loss', patience=3, verbose=1)
periodic_saving = ModelCheckpoint(save_path, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
cnn.compile(loss='binary_crossentropy', 
            optimizer=my_opt,
            metrics=['accuracy'])


# In[11]:


cnn_model = cnn.fit_generator(train_generator,
                        epochs = 15,
                        steps_per_epoch=train_generator.samples/train_generator.batch_size,
                        validation_steps=validation_generator.samples/validation_generator.batch_size,
                        validation_data = validation_generator,
                        callbacks=[early_stop, periodic_saving],
Ejemplo n.º 27
0
vocab_size = len(tokenizer.word_index) + 1

model_instance = BidirectionalGRUConcPool(num_classes=NUM_CLASSES)

if TRAIN:
    model = model_instance.build(vocab_size,
                                 embedding_matrix,
                                 input_length=x_train.shape[1],
                                 embed_dim=embedding_dimension,
                                 summary=False)

    checkpoint = ModelCheckpoint(get_save_path(model_instance),
                                 save_best_only=True)

    early_stop = EarlyStopping(monitor='val_loss',
                               patience=5,
                               verbose=1,
                               min_delta=0.00001)

    reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.1,
                                  patience=2,
                                  verbose=1,
                                  epsilon=0.0001,
                                  mode='min',
                                  min_lr=0.0001)

    if FOLDS > 0:
        # Store initial weights
        init_weights = model.get_weights()

        print('Running {}-Fold Cross Validation..'.format(FOLDS))
Ejemplo n.º 28
0
import VQA
from keras.callbacks import ModelCheckpoint, EarlyStopping

batch_size = 128

vqa = VQA.VQA()
vqa.use_word_embedding = False
vqa_model = vqa.create_model()

# image_caption_model.load_weights('weights/weights-improvement-13-2.99.hdf5')
file_name = 'weights/weights-improvement-{epoch:02d}-{loss:.2f}.hdf5'
checkpoint = ModelCheckpoint(file_name, monitor='acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='acc', min_delta=0, patience=5, verbose=1, mode='auto')

train_generator = vqa.data_generator(batch_size=batch_size)
vqa_model.fit_generator(train_generator, steps_per_epoch=vqa.total_samples//batch_size, epochs=30, verbose=1, callbacks=[checkpoint, early])
    data = data.replace("unstable", 0)
    data = data.replace("stable", 1)
    data = data.drop("stab", axis=1)

    Y = data["stabf"]
    X = data.drop("stabf", axis=1)
    X = (X - X.mean()) / X.std()

    print(X.shape)

    train_data, test_data , train_label, test_label = train_test_split(X, Y, test_size=0.2,
                                                                     random_state=1337, stratify = Y)

    train_data, val_data, train_label, val_label = train_test_split(train_data, train_label, test_size=0.1,
                                                                      random_state=1337, stratify=train_label)

    val = [val_data, val_label]

    file_path = "egss.h5"

    checkpoint = ModelCheckpoint(file_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')

    early = EarlyStopping(monitor="val_acc", mode="max", patience=10)

    reduce_on_plateau = ReduceLROnPlateau(monitor="val_acc", mode="max", factor=0.1, patience=3)

    callbacks_list = [checkpoint, early, reduce_on_plateau]  # early

    model, repr_model = get_model(n_classes=1, lr=0.01)

    model.fit(x=train_data, y=train_label, validation_data=val, epochs=15, verbose=2, callbacks=callbacks_list)
Ejemplo n.º 30
0
def training(k, fileType, train_ground_truth_folder, train_noisy_folder,
             val_ground_truth_folder, val_noisy_folder, dest):
    """ 
        Training the data.
        
    Parameters: 
        k (int): number of bits to look at.
        fileType (str): type of the file. Could be one of the following: pdf, latex, jpeg, html.
        train_ground_truth_folder (str): the path to the training ground truth folder.
        train_noisy_folder (str): the path to the training noisy folder.
        val_ground_truth_folder (str): the path to the validation ground truth folder.
        val_noisy_folder (str): the path to the validation noisy folder.
        dest (str): the path to the destination folder. For training, the model presistence will be saved.
    """
    input_rows, input_cols = k, 1
    #pad_dim = 32

    # the data, shuffled and split between train and test sets
    (x_train, y_train, _) = load_data(train_ground_truth_folder,
                                      train_noisy_folder, fileType)
    (x_valid, y_valid, _) = load_data(val_ground_truth_folder,
                                      val_noisy_folder,
                                      fileType,
                                      kind="validation")
    print('Before reshape:')
    print('x_train shape:', x_train.shape)
    print('x_valid shape:', x_valid.shape)
    # add a zero column at the end
    x_train = np.append(x_train, np.zeros([len(x_train), 1]), 1)
    x_valid = np.append(x_valid, np.zeros([len(x_valid), 1]), 1)
    y_train = np.append(y_train, np.zeros([len(y_train), 1]), 1)
    y_valid = np.append(y_valid, np.zeros([len(y_valid), 1]), 1)

    # reshaping
    x_train = np.reshape(x_train, (len(x_train), input_rows, input_cols, 1))
    x_valid = np.reshape(x_valid, (len(x_valid), input_rows, input_cols, 1))
    y_train = np.reshape(y_train, (len(y_train), input_rows, input_cols, 1))
    y_valid = np.reshape(y_valid, (len(y_valid), input_rows, input_cols, 1))

    print('After reshape:')
    print('x_train shape:', x_train.shape)
    print('x_valid shape:', x_valid.shape)

    input_shape = (input_rows, input_cols, 1)

    # convert class vectors to binary class matrices
    print('Shuffling in unison')
    shuffle_in_unison(x_train, y_train)
    shuffle_in_unison(x_valid, y_valid)

    batch_size = 50
    epochs = 20

    if dest[-1] != '/':
        dest += '/'
    if dest.split('/')[-2].lower() != fileType:
        dest += fileType + '/'
    if not os.path.exists(dest):
        os.makedirs(dest)

    # below is an example for the html U-Net model
    model = Unet1DNoPooling(input_size=input_shape,
                            k=k,
                            loss=negGrowthRateLoss,
                            learning_rate=1e-5)
    filepath = dest + fileType + ".h5"
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='min')
    csv_logger = CSVLogger(dest + fileType + ".csv")
    # Helper: Early stopping.
    early_stopper = EarlyStopping(patience=5)
    model.fit(x_train,
              y_train,
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(x_valid, y_valid),
              callbacks=[checkpoint, csv_logger, early_stopper])
    model.save_weights(filepath)