Пример #1
0
def train():
    model = load_model()
    model.summary()
    plot_model(model, to_file='model.png')
    json_string = model.to_json()
    open('model_architecture.json', 'w').write(json_string)

    x_train = np.load('final_AAI_Dataset.npz')['trainSet']
    y_train = np.load('final_AAI_Dataset.npz')['trainLabel']
    # y_train = np.reshape(y_train, (y_train.shape[0], y_train.shape[1]))
    x_test = np.load('final_AAI_Dataset.npz')['testSet']
    y_test = np.load('final_AAI_Dataset.npz')['testLabel']
    # y_test = np.reshape(y_test, (y_test.shape[0], y_test.shape[1]))

    # x_train = (x_train - np.mean(x_train)) / np.std(x_train)
    # y_train = (y_train - np.mean(y_train)) / np.std(y_train)
    # x_test = (x_test - np.mean(x_test)) / np.std(x_test)
    # y_test = (y_test - np.mean(y_test)) / np.std(y_test)

    print('Training....................')
    model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])

    history = LossHistory()
    model.fit(x_train, y_train,
              batch_size=64, epochs=200,
              validation_data=(x_test, y_test),
              callbacks=[history])
    model.save_weights('model_weights.h5')

    history.loss_plot('epoch')
Пример #2
0
    def train(self, path):
        from loss_history import LossHistory
        self.path = path
        dataframe = pd.read_csv(self.path, header=None, names=['sex', 'age', 'dweight', 'cweight', 'd_0', 'd_1', 'd_2', 'd_3', 'd_4', 'd_5', 'd_6', 'd_7', 'd_8', 'd_9', 'd_10', 'd_11', 'd_12',
                                                               'd_13', 'd_14', 'd_15', 'd_16', 'c_0', 'c_1', 'c_2', 'c_3', 'c_4', 'c_5', 'c_6', 'c_7', 'c_8', 'c_9', 'c_10', 'c_11', 'c_12', 'c_13', 'c_14', 'c_15', 'c_16', 'mm', 'anti'])

        X_tranin, Y_tranin, X_val, Y_val = self.split_train_test(dataframe)

        model = self.build(X_tranin.shape[1])
        # Fit the model
        optimizer = optimizers.Adam(lr=LR, beta_1=0.9,
                                    beta_2=0.999, epsilon=1e-08, decay=DECAY)

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

        history = LossHistory()

        model.fit(X_tranin, Y_tranin, batch_size=BS,
                  validation_data=(X_val, Y_val),
                  epochs=EPOCHS, verbose=VERBOSE, callbacks=[history])

        history.save_loss(PLOT, 'result' + os.path.sep +
                          self.id + os.path.sep)
        self.model = model
        self.history = history

        mm_acc = history.mm_val_acc[-1]
        anti_acc = history.anti_val_acc[-1]
        self.save_model(model, mm_acc, anti_acc)

        logger = get_logger()
        logger.info(self.id + ': training over. mm acc: {' + str(mm_acc) + '}\tanti acc: {' + str(anti_acc) +
                    '}\thyperparameters are ' + str(LR) + '\t' +
                    str(DECAY) + '\t' + str(EPOCHS) + '\t' + str(BS))
Пример #3
0
 def _get_calbacks():
     loss_history = LossHistory()
     early_stop = keras.callbacks.EarlyStopping(monitor="val_loss",
                                                patience=10)
     reduce_learn_rate = keras.callbacks.ReduceLROnPlateau(
         monitor="val_loss", factor=0.1, patience=20)
     nan_loss = keras.callbacks.TerminateOnNaN()
     return [loss_history, early_stop, reduce_learn_rate, nan_loss]
Пример #4
0
def train_top_model(y_train,
                    y_val,
                    train_data=None,
                    validation_data=None,
                    exp=None,
                    previousWeights=None):
    if exp == None:
        exp = expNo

    print('Training top model...')
    train_labels = y_train
    if train_data == None:
        train_data = np.load(open(w_path + train_feat_name))

    validation_labels = y_val
    if validation_data == None:
        validation_data = np.load(open(w_path + val_feat_name))

    print('Bottleneck features are loaded.')

    model = get_umpm_topmodel.build_umpm_topmodel(train_data.shape[1:],
                                                  fcLayers=topModelFCconfig,
                                                  reg_w=REG_W_FC,
                                                  reg_b=REG_B_FC,
                                                  bn_eps=BN_EPS,
                                                  dropout=DROPOUT_FC)

    if previousWeights != None:
        model.load_weights(previousWeights)

    model.compile(optimizer='rmsprop',
                  loss='mse',
                  metrics=['mean_squared_error'])

    # callback to save history
    chkpt_path = '/home/edogan/data/weights/umpm_topmodel%s_checkpoints/' % exp
    os.mkdir(chkpt_path)
    filepath = os.path.join(
        chkpt_path,
        'weights-improvement-{epoch:03d}-{val_mean_squared_error:.4f}.hdf5')
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_mean_squared_error',
                                 verbose=0,
                                 save_best_only=True,
                                 mode='min')
    losshist = LossHistory('topmodel')

    model.fit(train_data,
              train_labels,
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              validation_data=(validation_data, validation_labels),
              callbacks=[losshist, checkpoint])

    print('Topmodel training is completed at %s' %
          dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
Пример #5
0
 def train_model(self):
     self.output_loss = mse(K.flatten(self.inputs), K.flatten(self.outputs))
     self.output_loss *= 96 * 96 * 3
     self.kl_loss = 1 + self.z_log_var - K.square(self.z_mean) - K.exp(
         self.z_log_var)
     self.kl_loss = K.sum(self.kl_loss, axis=-1)
     self.kl_loss *= -0.5
     self.total_loss = K.mean(self.output_loss + self.kl_loss)
     self.vae_model.add_loss(self.total_loss)
     self.adam = Adam(lr=self.learning_rate,
                      beta_1=0.9,
                      beta_2=0.999,
                      epsilon=1e-08)
     self.vae_model.compile(optimizer=self.adam)
     print(self.vae_model.summary())
     self.history = LossHistory()
     self.vae_model.fit(self.train_data,
                        epochs=self.epochs,
                        batch_size=self.batch_size,
                        validation_data=(self.test_data, None),
                        callbacks=[self.history])
Пример #6
0
def main():
    gpus = digit_counter(os.environ["CUDA_VISIBLE_DEVICES"])[0]

    params = [[0.001, 80, 10, 2, 0.6], [0.001, 320, 40, 8, 0.6],
              [0.001, 160, 20, 4, 0.6], [0.001, 640, 80, 16, 0.6],
              [0.001, 960, 120, 24, 0.6], [0.001, 1280, 160, 32, 0.6]]
    # [0.001, 1600, 200, 40, 0.6]]

    for id, param in enumerate(params):
        name = str(param) + str(datetime.now()).replace(" ", "_")

        # tensorboard_cb = keras.callbacks.TensorBoard(log_dir='./graph', histogram_freq=0,
        #           write_graph=True, write_images=True)
        stop = EarlyStopping(monitor='val_loss',
                             min_delta=0.001,
                             patience=5,
                             verbose=1,
                             mode='auto')
        # checkpointer = ModelCheckpoint(filepath='../weights/checkpoint.hdf5', verbose=1,
        #                                save_best_only=True)
        history = LossHistory()

        callbacks = [history, stop]

        if USE_MULTI is True:
            parallel_model, model = network.create_model(model_params=param,
                                                         multi_gpu=USE_MULTI,
                                                         gpus=gpus)
            train_with_all(DATASET_PATH,
                           VALSET_PATH,
                           target_model_name=name,
                           model=parallel_model,
                           save_model=model,
                           nb_epochs=20,
                           callbacks=callbacks)

        else:
            model = network.create_model(model_params=param,
                                         multi_gpu=USE_MULTI,
                                         gpus=gpus)
            train_with_all(DATASET_PATH,
                           VALSET_PATH,
                           target_model_name=name,
                           model=model,
                           save_model=model,
                           nb_epochs=20,
                           callbacks=callbacks)

            printc("Param set: {} done".format(id))
            printc("Params: {}".format(param))

            printc("All done", 'okgreen')
Пример #7
0
# add the model on top of the convolutional base
model.add(top_model)

# set the first FREEZE_UPTO layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:FREEZE_UPTO]:
    layer.trainable = False

# Let's train the model (only last conv layer + top_model)
model.compile(loss='mse',
              optimizer=SGD(lr=LEARNING_RATE, momentum=MOMENTUM),
              metrics=['mean_squared_error'])  # or just skip the metrics param

# Callbacks
losshist = LossHistory('finetune_umpm' + expNo)
chkpt_path = '/home/edogan/checkpoints/%s' % experiment_name
os.mkdir(chkpt_path)
filepath = os.path.join(
    chkpt_path,
    'weights-improvement-{epoch:03d}-{val_mean_squared_error:.2f}.hdf5')
checkpoint = ModelCheckpoint(filepath,
                             monitor='val_mean_squared_error',
                             verbose=0,
                             save_best_only=True,
                             mode='min')

print('Starting training...')

model.fit(X_train,
          Y_train,
Пример #8
0
#sys.exit()

# create dir for checkpoints
chkpt_path = '/home/edogan/checkpoints/%s' % experiment_name
os.mkdir(chkpt_path)



# CALLBACKS:  ==================
# checkpoint
filepath = os.path.join(chkpt_path, 'weights-improvement-{epoch:02d}-{val_mean_squared_error:.2f}.hdf5')
checkpoint = ModelCheckpoint(filepath, monitor='val_mean_squared_error', verbose=0, save_best_only=True, mode='min')
# early stopping
# earlyStopping = EarlyStopping(monitor='val_mean_squared_error', min_delta=0.1, patience=50, verbose=0, mode='min')
# history of loss/mse
losshist = LossHistory()

# callbacks_list = [checkpoint, earlyStopping]
callbacks_list = [losshist, checkpoint]


# ACTUAL TRAINING: ==================
if not data_augmentation:
  print('Not using data augmentation.')
else:
  print('Using manually augmented data.')

model.fit(X_train, Y_train,
              batch_size=batch_size,
              nb_epoch=nb_epoch,
              validation_data=(X_test, Y_test),
Пример #9
0
                               patience=EARLY_STOPPING_PATIENCE,
                               verbose=0,
                               mode='auto')

# reduce lr
reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                              factor=0.1,
                              patience=REDUCE_LR_PATIENCE,
                              verbose=0,
                              mode='auto',
                              epsilon=0.0001,
                              cooldown=0,
                              min_lr=0)

# 创建一个 LossHistory 实例
history = LossHistory()

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

# fit
model.fit_generator(train_generator,
                    steps_per_epoch=train_generator.samples // BATCH_SIZE,
                    epochs=EPOCHS,
                    validation_data=valid_generator,
                    validation_steps=valid_generator.samples // BATCH_SIZE,
                    callbacks=[check_point, early_stopping, history])

# 绘制 loss 曲线和 batch 曲线
Пример #10
0
x_train, y_train = x_data[0:train_size], y_data[0:train_size]
output_dim = len(y_train[0])
print('x_train_size:', len(x_train))
print('y_train_size:', len(y_train))
x_test, y_test = x_data[train_size - 1:data_size - 1], y_data[train_size -
                                                              1:data_size - 1]
print('x_test_size:', len(x_test))
print('y_test_size:', len(y_test))
print('start lstm')

train_generator = batch_generator([x_train, y_train], 100)
test_generator = batch_generator([x_test, y_test], 100)

model = get_model(timesteps, data_dim, output_dim)
model.summary()
earlystop = EarlyStopping()
losshistory = LossHistory()
modelcheck = ModelCheckpoint(path_base + 'bestmodel.m',
                             monitor='val_loss',
                             verbose=1,
                             save_best_only=True)
model.fit_generator(train_generator,
                    steps_per_epoch=100,
                    epochs=10,
                    callbacks=[losshistory, modelcheck],
                    validation_data=test_generator,
                    validation_steps=5)
losshistory.save('./history.json')
losshistory.loss_plot('epoch')
losshistory.loss_plot('batch')
    def __init__(self,
                 n_feat=None,
                 n_epoch=None,
                 batch_size=None,
                 encoder_layers=None,
                 decoder_layers=None,
                 n_hidden_units=None,
                 encoding_dim=None,
                 denoising=None):
        args, _, _, values = inspect.getargvalues(inspect.currentframe())
        values.pop("self")

        for arg, val in values.items():
            setattr(self, arg, val)

        loss_history = LossHistory()

        early_stop = keras.callbacks.EarlyStopping(monitor="val_loss",
                                                   patience=10)

        reduce_learn_rate = keras.callbacks.ReduceLROnPlateau(
            monitor="val_loss", factor=0.1, patience=20)

        self.callbacks_list = [loss_history, early_stop, reduce_learn_rate]

        for i in range(self.encoder_layers):
            if i == 0:
                self.input_data = Input(shape=(self.n_feat, ))
                self.encoded = BatchNormalization()(self.input_data)
                self.encoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.encoded)
                self.encoded = Dropout(rate=0.5)(self.encoded)
            elif i > 0 and i < self.encoder_layers - 1:
                self.encoded = BatchNormalization()(self.encoded)
                self.encoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.encoded)
                self.encoded = Dropout(rate=0.5)(self.encoded)
            elif i == self.encoder_layers - 1:
                self.encoded = BatchNormalization()(self.encoded)
                self.encoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.encoded)

        self.mu = Dense(units=self.encoding_dim,
                        activation="linear")(self.encoded)
        self.log_sigma = Dense(units=self.encoding_dim,
                               activation="linear")(self.encoded)
        z = Lambda(self.sample_z, output_shape=(self.encoding_dim, ))(
            [self.mu, self.log_sigma])

        self.decoded_layers_dict = {}

        decoder_counter = 0

        for i in range(self.decoder_layers):
            if i == 0:
                self.decoded_layers_dict[decoder_counter] = BatchNormalization(
                )
                decoder_counter += 1
                self.decoded_layers_dict[decoder_counter] = Dense(
                    units=self.n_hidden_units, activation="elu")
                decoder_counter += 1
                self.decoded_layers_dict[decoder_counter] = Dropout(rate=0.5)

                self.decoded = self.decoded_layers_dict[decoder_counter - 2](z)
                self.decoded = self.decoded_layers_dict[decoder_counter - 1](
                    self.decoded)
                self.decoded = self.decoded_layers_dict[decoder_counter](
                    self.decoded)

                decoder_counter += 1
            elif i > 0 and i < self.decoder_layers - 1:
                self.decoded_layers_dict[decoder_counter] = BatchNormalization(
                )
                decoder_counter += 1
                self.decoded_layers_dict[decoder_counter] = Dense(
                    units=self.n_hidden_units, activation="elu")
                decoder_counter += 1
                self.decoded_layers_dict[decoder_counter] = Dropout(rate=0.5)

                self.decoded = self.decoded_layers_dict[decoder_counter - 2](
                    self.decoded)
                self.decoded = self.decoded_layers_dict[decoder_counter - 1](
                    self.decoded)
                self.decoded = self.decoded_layers_dict[decoder_counter](
                    self.decoded)

                decoder_counter += 1
            elif i == self.decoder_layers - 1:
                self.decoded_layers_dict[decoder_counter] = BatchNormalization(
                )
                decoder_counter += 1
                self.decoded_layers_dict[decoder_counter] = Dense(
                    units=self.n_hidden_units, activation="elu")

                self.decoded = self.decoded_layers_dict[decoder_counter - 1](
                    self.decoded)
                self.decoded = self.decoded_layers_dict[decoder_counter](
                    self.decoded)
                decoder_counter += 1

        # Output would have shape: (batch_size, n_feat).
        self.decoded_layers_dict[decoder_counter] = Dense(units=self.n_feat,
                                                          activation="sigmoid")
        self.decoded = self.decoded_layers_dict[decoder_counter](self.decoded)

        self.autoencoder = Model(self.input_data, self.decoded)
        self.autoencoder.compile(optimizer=keras.optimizers.Adam(),
                                 loss=self.vae_loss)
Пример #12
0
            yield X_train, y_train


if __name__ == '__main__':
    # Load generator function
    batch_size = 256
    train_generator = generator(train_samples, batch_size=batch_size)
    validation_generator = generator(validation_samples, batch_size=batch_size)

    # Load model
    retrain = False
    if not retrain:
        model = behavioral_cloning_model()
    else:
        model = load_model('model.h5')
    history = LossHistory()

    # Start training
    model.fit_generator(train_generator,
                        steps_per_epoch=len(train_samples) / batch_size,
                        validation_data=validation_generator,
                        validation_steps=len(validation_samples) / batch_size,
                        epochs=15,
                        verbose=2,
                        callbacks=[history])
    print('training finished')

    # Plot training history
    history.loss_plot('epoch')

    # Save Keras model
Пример #13
0
test_datagen = ImageDataGenerator(rescale=1. / 255)
single_validation_generator = test_datagen.flow_from_directory(
    valid_dir,
    target_size=(INPUT1_DIMS[1], INPUT1_DIMS[1]),
    batch_size=BS,
    class_mode='categorical')

CLASSES = single_train_generator.num_classes
params.num_labels = CLASSES

# initialize the model
print("[INFO] creating model...")
overwriting = os.path.exists(history_filename) and restore_from is None
assert not overwriting, "Weights found in model_dir, aborting to avoid overwrite"
loss_history = LossHistory(history_filename)
EPOCHS += loss_history.get_initial_epoch()

if LOSS_FN == 'center':
    loss_fn = get_center_loss(CENTER_LOSS_ALPHA, CLASSES)
elif LOSS_FN == 'softmax':
    loss_fn = get_softmax_loss()
else:
    loss_fn = get_total_loss(LAMBDA, CENTER_LOSS_ALPHA, CLASSES)

if restore_from is None:
    if model_name == 'densenet':
        model = DenseNetBaseModel(CLASSES, use_imagenet_weights).model
    elif model_name == "inception":
        model = Inceptionv3Model(CLASSES, use_imagenet_weights).model
    elif model_name == 'vgg':
    def __init__(self,
                 input_shape=None,
                 n_epoch=None,
                 batch_size=None,
                 encoder_layers=None,
                 decoder_layers=None,
                 n_hidden_units=None,
                 encoding_dim=None,
                 stateful=None,
                 denoising=None):
        args, _, _, values = inspect.getargvalues(inspect.currentframe())
        values.pop("self")

        for arg, val in values.items():
            setattr(self, arg, val)

        loss_history = LossHistory()

        early_stop = keras.callbacks.EarlyStopping(monitor="val_loss",
                                                   patience=10)

        reduce_learn_rate = keras.callbacks.ReduceLROnPlateau(
            monitor="val_loss", factor=0.1, patience=20)

        self.callbacks_list = [loss_history, early_stop, reduce_learn_rate]

        # 2D-lattice with time on the x-axis (across rows) and with space on the y-axis (across columns).
        if self.stateful is True:
            self.input_data = Input(batch_shape=self.input_shape)
            self.n_rows = self.input_shape[1]
            self.n_cols = self.input_shape[2]
        else:
            self.input_data = Input(shape=self.input_shape)
            self.n_rows = self.input_shape[0]
            self.n_cols = self.input_shape[1]

        for i in range(self.encoder_layers):
            if i == 0:
                # Returns a sequence of n_rows vectors of dimension n_hidden_units.
                self.encoded = CuDNNLSTM(units=self.n_hidden_units,
                                         return_sequences=True,
                                         stateful=self.stateful)(
                                             self.input_data)
            else:
                self.encoded = CuDNNLSTM(units=self.n_hidden_units,
                                         return_sequences=True,
                                         stateful=self.stateful)(self.encoded)

        # Returns 1 vector of dimension encoding_dim.
        self.encoded = CuDNNLSTM(units=self.encoding_dim,
                                 return_sequences=False,
                                 stateful=self.stateful)(self.encoded)

        # Returns a sequence containing n_rows vectors where each vector is of dimension encoding_dim.
        # output_shape: (None, n_rows, encoding_dim).
        self.decoded = RepeatVector(self.n_rows)(self.encoded)

        for i in range(self.decoder_layers):
            self.decoded = CuDNNLSTM(units=self.n_hidden_units,
                                     return_sequences=True,
                                     stateful=self.stateful)(self.decoded)

        # If return_sequences is True: 3D tensor with shape (batch_size, timesteps, units).
        # Else: 2D tensor with shape (batch_size, units).
        # Note that n_rows here is timesteps and n_cols here is units.
        # If return_state is True: a list of tensors.
        # The first tensor is the output. The remaining tensors are the last states, each with shape (batch_size, units).
        # If stateful is True: the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch.
        # For LSTM (not CuDNNLSTM) If unroll is True: the network will be unrolled, else a symbolic loop will be used. Unrolling can speed-up a RNN, although it tends to be more memory-intensive. Unrolling is only suitable for short sequences.
        self.decoded = CuDNNLSTM(units=self.n_cols,
                                 return_sequences=True,
                                 stateful=self.stateful)(self.decoded)

        self.autoencoder = Model(self.input_data, self.decoded)
        self.autoencoder.compile(optimizer=keras.optimizers.Adam(),
                                 loss="mean_squared_error")
Пример #15
0
def create_model():

    train_gen = ImageDataGenerator()
    valid_gen = ImageDataGenerator()
    train_generator = train_gen.flow_from_directory(TRAIN_DATA_PATH,
                                                    IMAGE_SIZE,
                                                    shuffle=True,
                                                    batch_size=BATCH_SIZE,
                                                    color_mode='grayscale')
    valid_generator = valid_gen.flow_from_directory(VALID_DATA_PATH,
                                                    IMAGE_SIZE,
                                                    batch_size=BATCH_SIZE,
                                                    color_mode='grayscale')

    inputs = Input((*IMAGE_SIZE, 1))
    x_input = Lambda(my_preprocess)(inputs)

    # block1
    x = Conv2D(64, (3, 3),
               input_shape=(*IMAGE_SIZE, 1),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block1_conv1')(x_input)
    x = Conv2D(64, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block1_conv2')(x)
    x = MaxPooling2D((2, 2),
                     strides=(2, 2),
                     padding='same',
                     name='block1_pool')(x)

    # block2
    x = Conv2D(128, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block2_conv1')(x)
    x = Conv2D(128, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block2_conv2')(x)
    x = MaxPooling2D((2, 2),
                     strides=(2, 2),
                     padding='same',
                     name='block2_pool')(x)

    # block3
    x = Conv2D(256, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block3_conv1')(x)
    x = Conv2D(256, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block3_conv2')(x)
    x = Conv2D(256, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block3_conv3')(x)
    x = MaxPooling2D((2, 2),
                     strides=(2, 2),
                     padding='same',
                     name='block3_pool')(x)

    # side1
    x_side1 = SeparableConv2D(512, (3, 3),
                              padding='same',
                              use_bias=False,
                              name='side1_sepconv1')(x)
    x_side1 = BatchNormalization(name='side1_bn1')(x_side1)
    x_side1 = Activation('relu', name='side1_act1')(x_side1)
    x_side1 = SeparableConv2D(512, (3, 3),
                              padding='same',
                              use_bias=False,
                              name='side1_sepconv2')(x_side1)
    x_side1 = BatchNormalization(name='side1_bn2')(x_side1)
    x_side1 = Activation('relu', name='side1_act2')(x_side1)
    x_side1 = MaxPooling2D((2, 2),
                           strides=(2, 2),
                           padding='same',
                           name='side1_pool')(x_side1)
    x_side1 = SeparableConv2D(728, (3, 3),
                              padding='same',
                              use_bias=False,
                              name='side1_sepconv3')(x_side1)
    x_side1 = BatchNormalization(name='side1_bn3')(x_side1)
    x_side1 = Activation('relu', name='side1_act3')(x_side1)
    x_side1 = SeparableConv2D(728, (3, 3),
                              padding='same',
                              activation='relu',
                              name='side1_sepconv4')(x_side1)
    x_side1 = GlobalAveragePooling2D(name='side1_gap')(x_side1)

    # side2
    x_side2_1_1 = Conv2D(256, (1, 1),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side2_1_conv1')(x)
    x_side2_1_2 = Conv2D(256, (1, 1),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side2_2_conv1')(x)
    x_side2_1_2 = Conv2D(256, (3, 3),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side2_2_conv2')(x_side2_1_2)
    x_side2_1_3 = Conv2D(256, (3, 3),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side2_3_conv1')(x)
    x_side2_1_3 = Conv2D(256, (1, 1),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side2_3_conv2')(x_side2_1_3)
    x_side2_1 = keras.layers.concatenate(
        [x_side2_1_1, x_side2_1_2, x_side2_1_3])

    x_side2_2_1 = Conv2D(256, (1, 1),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side3_1_conv1')(x_side2_1)
    x_side2_2_2 = Conv2D(256, (1, 1),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side3_2_conv1')(x_side2_1)
    x_side2_2_2 = Conv2D(256, (3, 3),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side3_2_conv2')(x_side2_2_2)
    x_side2_2_3 = Conv2D(256, (3, 3),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side3_3_conv1')(x_side2_1)
    x_side2_2_3 = Conv2D(256, (1, 1),
                         strides=(1, 1),
                         padding='same',
                         activation='relu',
                         name='side3_3_conv2')(x_side2_2_3)

    x_side2_2 = keras.layers.concatenate(
        [x_side2_2_1, x_side2_2_2, x_side2_2_3])
    x_side2 = GlobalAveragePooling2D(name='side2_gap')(x_side2_2)

    # block4
    x = Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block4_conv1')(x)
    x = Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block4_conv2')(x)
    x = Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               name='block4_conv3')(x)

    x = GlobalAveragePooling2D(name='gap')(x)

    x = keras.layers.concatenate([x, x_side1, x_side2])

    x = Dropout(DROP_RATE, name='dropout1')(x)
    predictions = Dense(CLASS_NUM, activation='softmax', name='dense1')(x)
    model = Model(inputs=inputs, outputs=predictions)
    model.summary()
    plot_model(model,
               to_file=os.path.join(RESULT_PATH, 'my_model.png'),
               show_shapes=True)

    check_point = ModelCheckpoint(monitor='val_loss',
                                  filepath=os.path.join(
                                      MODEL_PATH, MODEL_NAME),
                                  verbose=1,
                                  save_best_only=True,
                                  save_weights_only=False,
                                  mode='auto')

    # early stopping
    early_stopping = EarlyStopping(monitor='val_loss',
                                   patience=EARLY_STOPPING_PATIENCE,
                                   verbose=0,
                                   mode='auto')

    # reduce lr
    reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.1,
                                  patience=REDUCE_LR_PATIENCE,
                                  verbose=0,
                                  mode='auto',
                                  epsilon=0.0001,
                                  cooldown=0,
                                  min_lr=0)

    # 创建一个 LossHistory 实例
    history = LossHistory()

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

    # fit
    model.fit_generator(train_generator,
                        steps_per_epoch=train_generator.samples // BATCH_SIZE,
                        epochs=EPOCHS,
                        validation_data=valid_generator,
                        validation_steps=valid_generator.samples // BATCH_SIZE,
                        callbacks=[check_point, early_stopping, history])

    # 绘制 loss 曲线和 batch 曲线
    history.loss_plot('batch', os.path.join(RESULT_PATH, 'my_loss_batch.png'))
    history.acc_plot('batch', os.path.join(RESULT_PATH, 'my_batch.png'))
    history.loss_plot('epoch', os.path.join(RESULT_PATH, 'my_loss_epoch.png'))
    history.acc_plot('epoch', os.path.join(RESULT_PATH, 'my_acc_epoch.png'))
    K.clear_session()
Пример #16
0
def mergeFinetuneModel():

    X_train = []
    X_valid = []

    filenames = [
        os.path.join(OUTPUT_PATH, 'inceptionv3-finetune-output.hdf5'),
        os.path.join(OUTPUT_PATH, 'resnet50-finetune-output.hdf5'),
        os.path.join(OUTPUT_PATH, 'xception-finetune-output.hdf5'),
        os.path.join(OUTPUT_PATH, 'vgg16-finetune-output.hdf5')
    ]

    for filename in filenames:
        with h5py.File(filename, 'r') as h:
            X_train.append(np.array(h['X_train']))
            X_valid.append(np.array(h['X_val']))
            y_train = np.array(h['y_train'])
            y_valid = np.array(h['y_val'])

    for x in X_train:
        print(x.shape)

    for x in X_valid:
        print(x.shape)

    X_train = np.concatenate(X_train, axis=1)
    X_valid = np.concatenate(X_valid, axis=1)

    # check
    print('X_train shape:', X_train.shape)
    print('X_valid shape:', X_valid.shape)
    print('y_train shape:', y_train.shape)
    print('y_valid shape:', y_valid.shape)

    X_train, y_train = shuffle(X_train, y_train)
    y_train = to_categorical(y_train)
    X_valid, y_valid = shuffle(X_valid, y_valid)
    y_valid = to_categorical(y_valid)

    print('X_train shape:', X_train.shape)
    print('X_valid shape:', X_valid.shape)
    print('y_train shape:', y_train.shape)
    print('y_valid shape:', y_valid.shape)

    inputs = Input(X_train.shape[1:])
    x = Dense(2048, activation='relu')(inputs)
    x = Dropout(DROP_RATE)(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(CLASS_NUM, activation='softmax')(inputs)
    model = Model(inputs, predictions)
    check_point = ModelCheckpoint(filepath=os.path.join(
        MODEL_PATH, 'merge-model-01.hdf5'),
                                  verbose=1,
                                  save_best_only=True)
    early_stopping = EarlyStopping(monitor='val_loss',
                                   min_delta=0.0001,
                                   patience=EARLY_STOPPING_PATIENCE,
                                   verbose=1,
                                   mode='auto')
    # 创建一个 LossHistory 实例
    history = LossHistory()

    model.compile(loss='binary_crossentropy',
                  optimizer=Adam(lr=LEARNING_RATE),
                  metrics=['accuracy'])
    model.fit(X_train,
              y_train,
              epochs=EPOCHS,
              batch_size=BATCH_SIZE,
              validation_data=(X_valid, y_valid),
              callbacks=[early_stopping, check_point, history])

    # 绘制 loss 曲线和 batch 曲线
    history.loss_plot('batch',
                      os.path.join(RESULT_PATH, 'merge_all_loss_batch.png'))
    history.acc_plot('batch',
                     os.path.join(RESULT_PATH, 'merge_all_acc_batch.png'))
    history.loss_plot('epoch',
                      os.path.join(RESULT_PATH, 'merge_all_loss_epoch.png'))
    history.acc_plot('epoch',
                     os.path.join(RESULT_PATH, 'merge_all_acc_epoch.png'))
Пример #17
0
class Vae:
    def __init__(self, pic_train_nb=5000, pic_test_nb=100):
        self.train_data = load_data_anime_face(pic_train_nb) / 255
        self.test_data = load_data_anime_face(pic_test_nb) / 255
        self.input_shape = (96, 96, 3)
        self.batch_size = 512
        self.latent_dim = 50
        self.epochs = 200
        self.learning_rate = 0.01

    def build_model(self):
        # Encoder
        self.inputs = Input(shape=self.input_shape, name="Encoder_input")
        self.x = Conv2D(filters=16,
                        kernel_size=(3, 3),
                        activation='relu',
                        strides=2,
                        padding='same',
                        data_format="channels_last")(self.inputs)
        self.x = Conv2D(filters=32,
                        kernel_size=(3, 3),
                        activation='relu',
                        strides=2,
                        padding='same',
                        data_format="channels_last")(self.x)
        self.shape = K.int_shape(self.x)
        self.x = Flatten()(self.x)
        self.x = Dense(16, activation='relu')(self.x)

        self.z_mean = Dense(self.latent_dim, name='z_mean')(self.x)
        self.z_log_var = Dense(self.latent_dim, name='z_log_var')(self.x)
        self.z = Lambda(sample, output_shape=(self.latent_dim, ),
                        name='z')([self.z_mean, self.z_log_var])
        self.encoder = Model(self.inputs,
                             [self.z_mean, self.z_log_var, self.z],
                             name='encoder')
        print(self.encoder.summary())

        # Decoder
        self.latent_inputs = Input(shape=(self.latent_dim, ),
                                   name='z_sampling')
        self.x = Dense(self.shape[1] * self.shape[2] * self.shape[3],
                       activation='relu')(self.latent_inputs)
        self.x = Reshape((self.shape[1], self.shape[2], self.shape[3]))(self.x)
        self.x = Conv2DTranspose(filters=32,
                                 kernel_size=(3, 3),
                                 activation='relu',
                                 strides=2,
                                 padding='same',
                                 data_format="channels_last")(self.x)
        self.x = Conv2DTranspose(filters=16,
                                 kernel_size=(3, 3),
                                 activation='relu',
                                 strides=2,
                                 padding='same',
                                 data_format="channels_last")(self.x)
        self.outputs = Conv2DTranspose(filters=3,
                                       kernel_size=(3, 3),
                                       activation='sigmoid',
                                       padding='same',
                                       name='decoder_output')(self.x)
        self.decoder = Model(self.latent_inputs, self.outputs, name='decoder')
        print(self.decoder.summary())

        # Instantiate VAE model
        self.outputs = self.decoder(self.encoder(self.inputs)[2])
        self.vae_model = Model(self.inputs, self.outputs, name='vae')

    def train_model(self):
        self.output_loss = mse(K.flatten(self.inputs), K.flatten(self.outputs))
        self.output_loss *= 96 * 96 * 3
        self.kl_loss = 1 + self.z_log_var - K.square(self.z_mean) - K.exp(
            self.z_log_var)
        self.kl_loss = K.sum(self.kl_loss, axis=-1)
        self.kl_loss *= -0.5
        self.total_loss = K.mean(self.output_loss + self.kl_loss)
        self.vae_model.add_loss(self.total_loss)
        self.adam = Adam(lr=self.learning_rate,
                         beta_1=0.9,
                         beta_2=0.999,
                         epsilon=1e-08)
        self.vae_model.compile(optimizer=self.adam)
        print(self.vae_model.summary())
        self.history = LossHistory()
        self.vae_model.fit(self.train_data,
                           epochs=self.epochs,
                           batch_size=self.batch_size,
                           validation_data=(self.test_data, None),
                           callbacks=[self.history])

    def plot_given_z(self, z):
        pict = self.decoder.predict(z)
        plot(pict)

    def plot_random_result(self):
        z = np.array([np.random.normal(0, 1, self.latent_dim)])
        self.plot_given_z(z)

    def plot_random_train(self):
        train_size = np.shape(self.train_data)[0]
        train_pic_id = np.random.randint(0, train_size)
        train_pic = np.array([self.train_data[train_pic_id]])
        plot(train_pic)
        predict_pic = self.vae_model.predict(train_pic)
        plot(predict_pic)

    def plot_random_test(self):
        test_size = np.shape(self.test_data)[0]
        test_pic_id = np.random.randint(0, test_size)
        test_pic = np.array([self.test_data[test_pic_id]])
        plot(test_pic)
        predict_pic = self.vae_model.predict(test_pic)
        plot(predict_pic)

    def plot_loss(self):
        self.history.loss_plot('epoch')

    def save_model(self):
        return 0

    def load_model(self):
        return 0
    def __init__(self,
                 n_feat=None,
                 n_epoch=None,
                 batch_size=None,
                 encoder_layers=None,
                 decoder_layers=None,
                 n_hidden_units=None,
                 encoding_dim=None,
                 denoising=None):
        args, _, _, values = inspect.getargvalues(inspect.currentframe())
        values.pop("self")

        for arg, val in values.items():
            setattr(self, arg, val)

        loss_history = LossHistory()

        early_stop = keras.callbacks.EarlyStopping(monitor="val_loss",
                                                   patience=10)

        reduce_learn_rate = keras.callbacks.ReduceLROnPlateau(
            monitor="val_loss", factor=0.1, patience=20)

        self.callbacks_list = [loss_history, early_stop, reduce_learn_rate]

        for i in range(self.encoder_layers):
            if i == 0:
                self.input_data = Input(shape=(self.n_feat, ))
                self.encoded = BatchNormalization()(self.input_data)
                self.encoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.encoded)
                self.encoded = Dropout(rate=0.5)(self.encoded)
            elif 0 < i < self.encoder_layers - 1:
                self.encoded = BatchNormalization()(self.encoded)
                self.encoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.encoded)
                self.encoded = Dropout(rate=0.5)(self.encoded)
            elif i == self.encoder_layers - 1:
                self.encoded = BatchNormalization()(self.encoded)
                self.encoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.encoded)

        self.encoded = BatchNormalization()(self.encoded)
        self.encoded = Dense(units=self.encoding_dim,
                             activation="sigmoid")(self.encoded)

        for i in range(self.decoder_layers):
            if i == 0:
                self.decoded = BatchNormalization()(self.encoded)
                self.decoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.decoded)
                self.decoded = Dropout(rate=0.5)(self.decoded)
            elif 0 < i < self.decoder_layers - 1:
                self.decoded = BatchNormalization()(self.decoded)
                self.decoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.decoded)
                self.decoded = Dropout(rate=0.5)(self.decoded)
            elif i == self.decoder_layers - 1:
                self.decoded = BatchNormalization()(self.decoded)
                self.decoded = Dense(units=self.n_hidden_units,
                                     activation="elu")(self.decoded)

        # Output would have shape: (batch_size, n_feat).
        self.decoded = BatchNormalization()(self.decoded)
        self.decoded = Dense(units=self.n_feat,
                             activation="sigmoid")(self.decoded)

        self.autoencoder = Model(self.input_data, self.decoded)
        self.autoencoder.compile(optimizer=keras.optimizers.Adam(),
                                 loss="mean_squared_error")
    def __init__(self,
                 input_shape=None,
                 n_epoch=None,
                 batch_size=None,
                 encoder_layers=None,
                 decoder_layers=None,
                 filters=None,
                 kernel_size=None,
                 strides=None,
                 pool_size=None,
                 denoising=None):
        args, _, _, values = inspect.getargvalues(inspect.currentframe())
        values.pop("self")

        for arg, val in values.items():
            setattr(self, arg, val)

        loss_history = LossHistory()

        early_stop = keras.callbacks.EarlyStopping(monitor="val_loss",
                                                   patience=10)

        reduce_learn_rate = keras.callbacks.ReduceLROnPlateau(
            monitor="val_loss", factor=0.1, patience=20)

        self.callbacks_list = [loss_history, early_stop, reduce_learn_rate]

        for i in range(self.encoder_layers):
            if i == 0:
                self.input_data = Input(shape=self.input_shape)
                self.encoded = BatchNormalization()(self.input_data)
                self.encoded = convolutional.Conv2D(
                    filters=self.filters,
                    kernel_size=self.kernel_size,
                    strides=self.strides,
                    activation="elu",
                    padding="same")(self.encoded)
                self.encoded = Dropout(rate=0.5)(self.encoded)
            elif 0 < i < self.encoder_layers - 1:
                self.encoded = BatchNormalization()(self.encoded)
                self.encoded = convolutional.Conv2D(
                    filters=self.filters,
                    kernel_size=self.kernel_size,
                    strides=self.strides,
                    activation="elu",
                    padding="same")(self.encoded)
                self.encoded = Dropout(rate=0.5)(self.encoded)
            elif i == self.encoder_layers - 1:
                self.encoded = BatchNormalization()(self.encoded)
                self.encoded = convolutional.Conv2D(
                    filters=self.filters,
                    kernel_size=self.kernel_size,
                    strides=self.strides,
                    activation="elu",
                    padding="same")(self.encoded)

        self.encoded = pooling.MaxPooling2D(strides=self.pool_size,
                                            padding="same")(self.encoded)
        self.decoded = BatchNormalization()(self.encoded)
        self.decoded = convolutional.Conv2D(filters=self.filters,
                                            kernel_size=self.kernel_size,
                                            strides=self.strides,
                                            activation="elu",
                                            padding="same")(self.decoded)
        self.decoded = convolutional.UpSampling2D(size=self.pool_size)(
            self.decoded)

        for i in range(self.decoder_layers):
            if i < self.decoder_layers - 1:
                self.decoded = BatchNormalization()(self.decoded)
                self.decoded = convolutional.Conv2D(
                    filters=self.filters,
                    kernel_size=self.kernel_size,
                    strides=self.strides,
                    activation="elu",
                    padding="same")(self.decoded)
                self.decoded = Dropout(rate=0.5)(self.decoded)
            else:
                self.decoded = BatchNormalization()(self.decoded)
                self.decoded = convolutional.Conv2D(
                    filters=self.filters,
                    kernel_size=self.kernel_size,
                    strides=self.strides,
                    activation="elu",
                    padding="same")(self.decoded)

        # 4D tensor with shape: (samples, new_rows, new_cols, filters).
        # Remember think of this as a 2D-Lattice across potentially multiple channels per observation.
        # Rows represent time and columns represent some quantities of interest that evolve over time.
        # Channels might represent different sources of information.
        self.decoded = BatchNormalization()(self.decoded)
        self.decoded = convolutional.Conv2D(filters=self.input_shape[2],
                                            kernel_size=self.kernel_size,
                                            strides=self.strides,
                                            activation="sigmoid",
                                            padding="same")(self.decoded)

        self.autoencoder = Model(self.input_data, self.decoded)
        self.autoencoder.compile(optimizer=keras.optimizers.Adam(),
                                 loss="mean_squared_error")
Пример #20
0
# add the model on top of the convolutional base
model.add(top_model)

# set the first FREEZE_UPTO layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:FREEZE_UPTO]:
    layer.trainable = False

# Let's train the model (only last conv layer + top_model)
model.compile(loss='mse',
              optimizer=SGD(lr=LEARNING_RATE, momentum=MOMENTUM),
              metrics=['mean_squared_error'])  # or just skip the metrics param

# Callbacks
losshist = LossHistory('finetune_he' + expNo)
chkpt_path = '/home/edogan/checkpoints/%s' % experiment_name
os.mkdir(chkpt_path)
filepath = os.path.join(
    chkpt_path,
    'weights-improvement-{epoch:03d}-{val_mean_squared_error:.2f}.hdf5')
checkpoint = ModelCheckpoint(filepath,
                             monitor='val_mean_squared_error',
                             verbose=0,
                             save_best_only=True,
                             mode='min')

print('Starting training...')

model.fit(X_train,
          Y_train,
Пример #21
0
def finetuneModel():
    train_gen = ImageDataGenerator()
    valid_gen = ImageDataGenerator()
    train_generator = train_gen.flow_from_directory(TRAIN_DATA_PATH,
                                                    IMAGE_SIZE,
                                                    shuffle=True,
                                                    batch_size=BATCH_SIZE,
                                                    color_mode='grayscale')
    valid_generator = valid_gen.flow_from_directory(VALID_DATA_PATH,
                                                    IMAGE_SIZE,
                                                    batch_size=BATCH_SIZE,
                                                    color_mode='grayscale')
    inputs = Input((*IMAGE_SIZE, 1))
    x = Lambda(my_preprocess)(inputs)
    base_model = InceptionV3(input_tensor=x, weights=None, include_top=False)
    x = GlobalAveragePooling2D(name='my_global_average_pooling_layer_1')(
        base_model.output)
    x = Dropout(DROP_RATE, name='my_dropout_layer_1')(x)
    predictions = Dense(CLASS_NUM,
                        activation='softmax',
                        name='my_dense_layer_1')(x)
    model = Model(base_model.input, predictions)
    plot_model(model,
               to_file=os.path.join(RESULT_PATH, 'inceptionv3.png'),
               show_shapes=True)

    # set trainable layer
    for layer in model.layers[:INCEPTIONV3_NO_TRAINABLE_LAYERS]:
        layer.trainable = False
    for layer in model.layers[INCEPTIONV3_NO_TRAINABLE_LAYERS:]:
        layer.trainable = True
    model.summary()

    # check
    for i, layer in enumerate(model.layers):
        print('{}: {}, {}'.format(i, layer.name, layer.trainable))
    print('=' * 100)
    layers = zip(range(len(model.layers)), [x.name for x in model.layers])
    for layer_num, layer_name in layers:
        print('{}: {}'.format(layer_num + 1, layer_name))

    # check point
    check_point = ModelCheckpoint(monitor='val_loss',
                                  filepath=os.path.join(
                                      MODEL_PATH, MODEL_NAME),
                                  verbose=1,
                                  save_best_only=True,
                                  save_weights_only=False,
                                  mode='auto')

    # early stoppiing
    early_stopping = EarlyStopping(monitor='val_loss',
                                   patience=EARLY_STOPPING_PATIENCE,
                                   verbose=0,
                                   mode='auto')

    # 创建一个 LossHistory 实例
    history = LossHistory()

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

    # fit
    model.fit_generator(train_generator,
                        steps_per_epoch=train_generator.samples // BATCH_SIZE,
                        epochs=EPOCHS,
                        validation_data=valid_generator,
                        validation_steps=valid_generator.samples // BATCH_SIZE,
                        callbacks=[check_point, early_stopping, history])

    # 绘制 loss 曲线和 batch 曲线
    history.loss_plot('batch',
                      os.path.join(RESULT_PATH, 'inceptionv3_loss_batch.png'))
    history.acc_plot('batch',
                     os.path.join(RESULT_PATH, 'inceptionv3_acc_batch.png'))
    history.loss_plot('epoch',
                      os.path.join(RESULT_PATH, 'inceptionv3_loss_epoch.png'))
    history.acc_plot('epoch',
                     os.path.join(RESULT_PATH, 'inceptionv3_acc_epoch.png'))