예제 #1
0
파일: dsrcnn.py 프로젝트: maustinstar/IRIS
def model():
    inputLayer = Input(shape=(256, 256, 3))
    DSRCNN = UpSampling2D(size=2)(inputLayer)
    inputImage = DSRCNN
    DSRCNN = Conv2D(9, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(16, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(32, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(64, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(64, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(64, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(32, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(16, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(9, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("relu")(DSRCNN)
    DSRCNN = Conv2D(3, (3, 3),
                    strides=(1, 1),
                    padding="same",
                    init='glorot_uniform')(DSRCNN)
    DSRCNN = Activation("linear")(DSRCNN)
    residualImage = DSRCNN
    outputImage = add([inputImage, residualImage])

    DSRCNN = Model(inputLayer, outputImage)
    print DSRCNN.summary()
    return DSRCNN
예제 #2
0
def residual_TCN_LSTM(n_nodes,
                      conv_len,
                      n_classes,
                      n_feat,
                      max_len,
                      loss='categorical_crossentropy',
                      online=False,
                      optimizer="rmsprop",
                      depth=3,
                      return_param_str=False):
    n_layers = len(n_nodes)

    inputs = Input(shape=(max_len, n_feat))
    model = inputs
    prev = Conv1D(n_nodes[0], conv_len, padding='same')(model)

    # encoder
    for i in range(n_layers):

        for j in range(depth):
            # convolution over the temporal dimension
            current = encoder_identify_block(prev, n_nodes[i], conv_len)
            # residual connection within residual block
            if j != 0:
                model = add([prev, current])
                model = Activation('relu')(model)
            prev = current

        if i < (n_layers - 1):
            model = MaxPooling1D(2)(model)

    # decoder
    # for i in range(n_layers):
    #
    #     for j in range(depth):
    #
    #         current = decoder_identify_block(model, n_nodes[-i - 1])
    #         model = add([prev, current])
    #         prev = current
    #     model = UpSampling1D(2)(model)

    # Output FC layer
    model = TimeDistributed(Dense(n_classes, activation="softmax"))(model)

    model = Model(inputs=inputs, outputs=model)
    model.compile(loss=loss,
                  optimizer=optimizer,
                  sample_weight_mode="temporal",
                  metrics=['accuracy'])
    model.summary()

    if return_param_str:
        param_str = "ED-TCN_C{}_L{}".format(conv_len, n_layers)
        if online:
            param_str += "_online"

        return model, param_str
    else:
        return model
예제 #3
0
def model_train(img_size,
                batch_size,
                epochs,
                optimizer,
                learning_rate,
                train_list,
                validation_list,
                style=2):

    print('Style {}.'.format(style))

    if style == 1:
        input_img = Input(shape=img_size)

        #model = Sequential()

        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal',
                       input_shape=img_size)(input_img)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)

        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)

        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)

        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(1, (3, 3),
                       padding='same',
                       kernel_initializer='he_normal')(model)
        res_img = model

        output_img = merge.Add()([res_img, input_img])

        model = Model(input_img, output_img)

        #model.load_weights('vdsr_model_edges.h5')

        adam = Adam(lr=0.000005)
        #sgd = SGD(lr=1e-3, momentum=0.9, decay=1e-4, nesterov=False)
        sgd = SGD(lr=0.01, momentum=0.9, decay=0.001, nesterov=False)
        #model.compile(sgd, loss='mse', metrics=[PSNR, "accuracy"])
        model.compile(adam,
                      loss='mse',
                      metrics=[ssim, ssim_metric, PSNR, "accuracy"])

        model.summary()

    else:

        input_img = Input(shape=img_size)

        model = Conv2D(64, (3, 3),
                       padding='valid',
                       kernel_initializer='he_normal')(input_img)
        model_0 = Activation('relu')(model)

        total_conv = 22  # should be even number
        total_conv -= 2  # subtract first and last
        residual_block_num = 5  # should be even number

        for _ in range(residual_block_num):  # residual block
            model = Conv2D(64, (3, 3),
                           padding='same',
                           kernel_initializer='he_normal')(model_0)
            model = Activation('relu')(model)
            for _ in range(int(total_conv / residual_block_num) - 1):
                model = Conv2D(64, (3, 3),
                               padding='same',
                               kernel_initializer='he_normal')(model)
                model = Activation('relu')(model)
                model_0 = add([model, model_0])

        model = Conv2D(1, (3, 3),
                       padding='valid',
                       kernel_initializer='he_normal')(model)
        res_img = model

        input_img1 = crop(1, 2, -2)(input_img)
        input_img1 = crop(2, 2, -2)(input_img1)

        print(input_img.shape)
        print(input_img1.shape)
        output_img = merge.Add()([res_img, input_img1])
        # output_img = res_img
        model = Model(input_img, output_img)

        # model.load_weights('./vdsr_model_edges.h5')
        # adam = Adam(lr=learning_rate)
        adam = Adadelta()
        # sgd = SGD(lr=1e-7, momentum=0.9, decay=1e-2, nesterov=False)
        sgd = SGD(lr=learning_rate,
                  momentum=0.9,
                  decay=1e-4,
                  nesterov=False,
                  clipnorm=1)
        if optimizer == 0:
            model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR])
        else:
            model.compile(sgd, loss='mse', metrics=[ssim, ssim_metric, PSNR])

        model.summary()

    mycallback = MyCallback(model)
    timestamp = time.strftime("%m%d-%H%M", time.localtime(time.time()))
    csv_logger = callbacks.CSVLogger(
        'data/callbacks/training_{}.log'.format(timestamp))
    filepath = "./checkpoints/weights-improvement-{epoch:03d}-{PSNR:.2f}.hdf5"
    checkpoint = ModelCheckpoint(filepath, monitor=PSNR, verbose=1, mode='max')
    callbacks_list = [mycallback, checkpoint, csv_logger]

    # print('Loading training data.')
    # x = load_images(DATA_PATH)
    # print('Loading data label.')
    # y = load_images(LABEL_PATH)
    # print('Loading validation data.')
    # val = load_images(VAL_PATH)
    # print('Loading validation label.')
    # val_label = load_images(VAL_LABEL_PATH)

    # print(x.shape)
    # print(y.shape)
    # print(val.shape)
    # print(val_label.shape)

    with open('./model/vdsr_architecture.json', 'w') as f:
        f.write(model.to_json())

    # datagen = ImageDataGenerator(rotation_range=45,
    #                              zoom_range=0.15,
    #                              horizontal_flip=True,
    #                              vertical_flip=True)

    # history = model.fit_generator(datagen.flow(x, y, batch_size=batch_size),
    #                     steps_per_epoch=len(x) // batch_size,
    #                     validation_data=(val, val_label),
    #                     validation_steps=len(val) // batch_size,
    #                     epochs=epochs,
    #                     callbacks=callbacks_list,
    #                     verbose=1,
    #                     shuffle=True,
    #                     workers=256,
    #                     use_multiprocessing=True)

    history = model.fit_generator(
        image_gen(train_list, batch_size=batch_size),
        steps_per_epoch=384400 * (len(train_list)) // batch_size,
        # steps_per_epoch=4612800//batch_size,
        validation_data=image_gen(validation_list, batch_size=batch_size),
        validation_steps=384400 * (len(validation_list)) // batch_size,
        epochs=epochs,
        workers=1024,
        callbacks=callbacks_list,
        verbose=1)

    print("Done training!!!")

    print("Saving the final model ...")

    model.save('vdsr_model.h5')  # creates a HDF5 file
    del model  # deletes the existing model

    # plt.plot(history.history['accuracy'])
    # plt.plot(history.history['val_accuracy'])
    # plt.title('Model accuracy')
    # plt.ylabel('Accuracy')
    # plt.xlabel('Epoch')
    # plt.legend(['Train', 'validation'], loc='upper left')
    # # plt.show()
    # plt.savefig('accuracy.png')

    # Plot training & validation loss values
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'validation'], loc='upper left')
    # plt.show()
    plt.savefig('loss.png')

    plt.plot(history.history['PSNR'])
    plt.plot(history.history['val_PSNR'])
    plt.title('Model PSNR')
    plt.ylabel('PSNR')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'validation'], loc='upper left')
    # plt.show()
    plt.savefig('PSNR.png')
예제 #4
0
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(1, (3, 3), padding='same', kernel_initializer='he_normal')(model)
res_img = model

output_img = add([res_img, input_img])

model = Model(input_img, output_img)

# model.load_weights('./checkpoints/weights-improvement-20-26.93.hdf5')

adam = Adam(lr=0.001, decay=1e-5, clipvalue=0.1, epsilon=1e-8)
sgd = SGD(lr=1e-2, momentum=0.9, decay=1e-4, nesterov=False)
model.compile(adam, loss='mse', metrics=[PSNR, "accuracy"])

model.summary()
# 每个epoch保存一次模型
filepath = "./checkpoints2/vdsr-{epoch:02d}-{PSNR:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor=PSNR, verbose=1, mode='max')
callbacks_list = [checkpoint]
# 记录每次训练的loss,PSNR值
train_log = CSVLogger(filename="train.log")
model.fit(x=train_input, y=train_label, batch_size=BATCH_SIZE, epochs=EPOCHS, callbacks=callbacks_list, shuffle=True)

print("Done training!!!")

예제 #5
0
def ED_TCN(n_nodes,
           conv_len,
           n_classes,
           n_feat,
           max_len,
           loss='categorical_crossentropy',
           online=False,
           optimizer="rmsprop",
           activation='norm_relu',
           attention=True,
           return_param_str=False):
    n_layers = len(n_nodes)

    # inputs = Input(shape=(max_len, n_feat))
    inputs = Input(shape=(None, n_feat))

    # attention layer, apply weightings to input
    if attention:
        model = attention_block(inputs, 100)
    else:
        model = inputs

    # ---- Encoder ----
    for i in range(n_layers):
        # Pad beginning of sequence to prevent usage of future data
        if online: model = ZeroPadding1D((conv_len // 2, 0))(model)
        # convolution over the temporal dimension
        model = Conv1D(n_nodes[i], conv_len, padding='same')(model)
        if online: model = Cropping1D((0, conv_len // 2))(model)

        model = SpatialDropout1D(0.3)(model)

        if activation == 'norm_relu':
            model = Activation('relu')(model)
            model = Lambda(channel_normalization,
                           name="encoder_norm_{}".format(i))(model)
        elif activation == 'wavenet':
            model = WaveNet_activation(model)
        else:
            model = Activation(activation)(model)

        # hidden features layer when in the last interation
        model = MaxPooling1D(2)(model)

    # ---- Decoder ----
    for i in range(n_layers):
        model = UpSampling1D(2)(model)
        if online: model = ZeroPadding1D((conv_len // 2, 0))(model)
        model = Conv1D(n_nodes[-i - 1], conv_len, padding='same')(model)
        if online: model = Cropping1D((0, conv_len // 2))(model)

        model = SpatialDropout1D(0.3)(model)

        if activation == 'norm_relu':
            model = Activation('relu')(model)
            model = Lambda(channel_normalization,
                           name="decoder_norm_{}".format(i))(model)
        elif activation == 'wavenet':
            model = WaveNet_activation(model)
        else:
            model = Activation(activation)(model)

    # Output FC layer
    model = TimeDistributed(Dense(n_classes, activation="softmax"))(model)

    model = Model(inputs=inputs, outputs=model)
    model.compile(loss=loss,
                  optimizer=optimizer,
                  sample_weight_mode="temporal",
                  metrics=['accuracy'])
    model.summary()

    if return_param_str:
        param_str = "ED-TCN_C{}_L{}".format(conv_len, n_layers)
        if online:
            param_str += "_online"

        return model, param_str
    else:
        return model
예제 #6
0
def model_train(img_size, batch_size, epochs, optimizer, learning_rate, train_list, validation_list, style=2):

    print('Style {}.'.format(style))

    if style == 1:
        input_img = Input(shape=img_size)

        #model = Sequential()

        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', input_shape=img_size)(input_img)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)

        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)

        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)

        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        model = Activation('relu')(model)
        model = Conv2D(1, (3, 3), padding='same', kernel_initializer='he_normal')(model)
        res_img = model

        output_img = merge.Add()([res_img, input_img])

        model = Model(input_img, output_img)

        #model.load_weights('vdsr_model_edges.h5')

        adam = Adam(lr=0.000005)
        sgd = SGD(lr=0.01, momentum=0.9, decay=0.001, nesterov=False)
        model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR, "accuracy"])

        model.summary()

    else:

        input_img = Input(shape=img_size)

        model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', use_bias=False)(input_img)
        model = BatchNormalization()(model)
        model_0 = Activation('relu')(model)

        total_conv = 22  # should be even number
        total_conv -= 2  # subtract first and last
        residual_block_num = 5  # should be even number

        for _ in range(residual_block_num):  # residual block
            model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', use_bias=False)(model_0)
            model = BatchNormalization()(model)
            model = Activation('relu')(model)
            print(_)
            for _ in range(int(total_conv/residual_block_num)-1):
                model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', use_bias=False)(model)
                model = BatchNormalization()(model)
                model = Activation('relu')(model)
                model_0 = add([model, model_0])
                print(_)

        model = Conv2DTranspose(64, (5, 5), padding='valid', kernel_initializer='he_normal', use_bias=False)(model)
        model = BatchNormalization()(model)
        model = LeakyReLU()(model)
        model = Conv2D(1, (5, 5), padding='valid', kernel_initializer='he_normal')(model)
        
        res_img = model

        #input_img1 = crop(1,22,-22)(input_img)
        #input_img1 = crop(2,22,-22)(input_img1)

        print(input_img.shape)
        output_img = merge.Add()([res_img, input_img])
        # output_img = res_img
        model = Model(input_img, output_img)

        # model.load_weights('./vdsr_model_edges.h5')
        # adam = Adam(lr=learning_rate)
        adam = Adadelta()
        sgd = SGD(lr=learning_rate, momentum=0.9, decay=1e-4, nesterov=False, clipnorm=1)
        if optimizer == 0:
            model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR])
        else:
            model.compile(sgd, loss='mse', metrics=[ssim, ssim_metric, PSNR])


        model.summary()

    mycallback = MyCallback(model)
    timestamp = time.strftime("%m%d-%H%M", time.localtime(time.time()))
    csv_logger = callbacks.CSVLogger('data/callbacks/deconv/training_{}.log'.format(timestamp))
    filepath="./checkpoints/deconv/weights-improvement-{epoch:03d}-{PSNR:.2f}-{ssim:.2f}.hdf5"
    checkpoint = ModelCheckpoint(filepath, monitor=PSNR, verbose=1, mode='max')
    callbacks_list = [mycallback, checkpoint, csv_logger]

    with open('./model/deconv/vdsr_architecture.json', 'w') as f:
        f.write(model.to_json())

    history = model.fit_generator(image_gen(train_list, batch_size=batch_size), 
                        steps_per_epoch=(409600//8)*len(train_list) // batch_size,
                        validation_data=image_gen(validation_list,batch_size=batch_size),
                        validation_steps=(409600//8)*len(validation_list) // batch_size,
                        epochs=epochs,
                        workers=1024,
                        callbacks=callbacks_list,
                        verbose=1)

    print("Done training!!!")

    print("Saving the final model ...")

    model.save('vdsr_model.h5')  # creates a HDF5 file 
    del model  # deletes the existing model


    # Plot training & validation loss values
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'validation'], loc='upper left')
    # plt.show()
    plt.savefig('loss.png')

    plt.plot(history.history['PSNR'])
    plt.plot(history.history['val_PSNR'])
    plt.title('Model PSNR')
    plt.ylabel('PSNR')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'validation'], loc='upper left')
    # plt.show()
    plt.savefig('PSNR.png')
예제 #7
0
파일: vdsr.py 프로젝트: ehumss/VDSR-Keras
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(1, (3, 3), padding='same', kernel_initializer='he_normal')(model)
res_img = model

output_img = add([res_img, input_img])

model = Model(input_img, output_img)

# model.load_weights('./checkpoints/weights-improvement-20-26.93.hdf5')

adam = Adam(lr=0.00001)
sgd = SGD(lr=1e-5, momentum=0.9, decay=1e-4, nesterov=False)
model.compile(adam, loss='mse', metrics=[PSNR, "accuracy"])

model.summary()

filepath="./checkpoints/weights-improvement-{epoch:02d}-{PSNR:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor=PSNR, verbose=1, mode='max')
callbacks_list = [checkpoint]

model.fit_generator(image_gen(train_list), steps_per_epoch=len(train_list) // BATCH_SIZE,  \
					validation_data=image_gen(test_list), validation_steps=len(train_list) // BATCH_SIZE, \
					epochs=EPOCHS, workers=8, callbacks=callbacks_list)

print("Done training!!!")

print("Saving the final model ...")

model.save('vdsr_model.h5')  # creates a HDF5 file 
del model  # deletes the existing model
예제 #8
0
def ResCat_aux(n_classes, input_shape, dropout=0.5, aux_ind=0):
    filter_numbers = [32, 64, 128]
    init_filters = 32
    merge_layers = []
    outputs = []
    input = Input(shape=input_shape)
    conv1 = Conv2D(filters=init_filters,
                   kernel_size=(7, 7),
                   strides=(2, 2),
                   padding='valid',
                   kernel_initializer='he_normal',
                   kernel_regularizer=l2(1.e-4))(input)
    norm = BatchNormalization(axis=CHANNEL_AXIS)(conv1)
    relu = Activation("relu")(norm)
    dr = Dropout(dropout)(relu)
    model = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding="same")(dr)

    for fn in filter_numbers:
        res1 = bn_relu_conv(model, fn, (3, 3), dropout=dropout)
        res2 = bn_relu_conv(res1, fn, (3, 3), strides=2, dropout=dropout)

        input_shape = K.int_shape(model)
        residual_shape = K.int_shape(res2)
        stride_width = int(
            round(input_shape[ROW_AXIS] / residual_shape[ROW_AXIS]))
        stride_height = int(
            round(input_shape[COL_AXIS] / residual_shape[COL_AXIS]))
        equal_channels = input_shape[CHANNEL_AXIS] == residual_shape[
            CHANNEL_AXIS]

        # 1 X 1 conv if shape is different. Else identity.
        if stride_width > 1 or stride_height > 1 or not equal_channels:
            model = Conv2D(filters=residual_shape[CHANNEL_AXIS],
                           kernel_size=(1, 1),
                           strides=(stride_width, stride_height),
                           padding="valid",
                           kernel_initializer="he_normal",
                           kernel_regularizer=l2(0.0001))(model)
        model = add([res2, model])
        merge_layers.append(model)
        # Last activation
    norm = BatchNormalization(axis=CHANNEL_AXIS)(model)
    model = Activation("relu")(norm)
    # Classifier block
    block_shape = K.int_shape(model)
    model = AveragePooling2D(pool_size=(block_shape[ROW_AXIS],
                                        block_shape[COL_AXIS]),
                             strides=(1, 1))(model)
    flatten = Flatten()(model)
    dense = Dense(units=n_classes,
                  kernel_initializer="he_normal",
                  activation="sigmoid",
                  name='main')(flatten)

    outputs.append(dense)
    ## AUX, does obj exists?
    aux = merge_layers[aux_ind]
    pool = AveragePooling2D(pool_size=(3, 3), strides=(2, 2))(aux)
    flatten = Flatten()(pool)
    fc = Dense(units=filter_numbers[-1],
               kernel_initializer="he_normal",
               activation="relu")(flatten)
    dense = Dense(units=2,
                  kernel_initializer="he_normal",
                  activation="softmax",
                  name='aux')(fc)
    outputs.append(dense)

    model = Model(inputs=input, outputs=outputs)
    model.summary()
    return model