def VGG_16(image_shape):
    bn_mode = 0
    bn_axis = 1
    decay = 1e-5
    activation = 'linear'
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=image_shape))
    model.add(
        Convolution2D(64, 3, 3, activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(64, 3, 3, activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(128,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(128,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(256,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(256,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(256,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation=activation,
                      W_regularizer=l2(decay)))
    model.add(BatchNormalization(mode=bn_mode, axis=bn_axis))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
    model.add(Flatten())
    model.add(Dense(4096, activation=activation, W_regularizer=l2(decay)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation=activation, W_regularizer=l2(decay)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1, activation='sigmoid'))
    return model
model.add(Activation(quantized_tanh))
model.add(
    QuantizedConv2D(64,
                    kernel_size=kernel_size,
                    data_format='channels_last',
                    H=H,
                    nb=nb,
                    kernel_lr_multiplier=kernel_lr_multiplier,
                    kernel_regularizer=keras.regularizers.l2(weight_decay),
                    kernel_initializer=he_normal(),
                    padding='same',
                    use_bias=use_bias,
                    name='block1_conv2'))
model.add(BatchNormalization())
model.add(Activation(quantized_tanh))
model.add(MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool'))

# Block 2
model.add(
    QuantizedConv2D(128,
                    kernel_size=kernel_size,
                    data_format='channels_last',
                    H=H,
                    nb=nb,
                    kernel_lr_multiplier=kernel_lr_multiplier,
                    kernel_regularizer=keras.regularizers.l2(weight_decay),
                    kernel_initializer=he_normal(),
                    padding='same',
                    use_bias=use_bias,
                    name='block2_conv1'))
model.add(BatchNormalization())
Exemplo n.º 3
0
def nn_base(input_tensor=None, trainable=False):
    # Determine proper input shape
    if K.image_dim_ordering() == 'th':
        input_shape = (3, None, None)
    else:
        input_shape = (None, None, 3)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    # Block 1
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv1')(img_input)
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

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

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

    # Block 4
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv1')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv2')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

    # Block 5
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv1')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv2')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv3')(x)
    # x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)

    return x
Exemplo n.º 4
0
def conv1(nn):
    nn.add(Conv2D(32, (3, 3)))
    nn.add(Activation("relu"))
    nn.add(MaxPooling2D(pool_size=(2, 2)))
    return nn
Exemplo n.º 5
0
### TODO: Define your architecture.
activation = 'relu'

for epochs in [10]:
    for dense1 in [16]:
        name = '_'.join(str(x) for x in list([dense1, epochs, '3']))
        log_dir = os.path.join('./logs/', name)
        model = Sequential()
        with K.name_scope('helloworld'):
            model.add(
                Conv2D(filters=16,
                       kernel_size=2,
                       padding='valid',
                       activation=activation,
                       input_shape=(224, 224, 3)))
            model.add(MaxPooling2D(pool_size=2))
            model.add(
                Conv2D(filters=dense1,
                       kernel_size=2,
                       padding='valid',
                       activation=activation))
            model.add(MaxPooling2D(pool_size=2))
            model.add(
                Conv2D(filters=32,
                       kernel_size=2,
                       padding='valid',
                       activation=activation))
            model.add(MaxPooling2D(pool_size=4))
            model.add(Flatten())

            # model.add(Dense(500, activation=activation))
Exemplo n.º 6
0
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.callbacks import EarlyStopping
from keras import backend as K
import skimage
from sklearn.model_selection import train_test_split
import pydicom

os.environ["CUDA_VISIBLE_DEVICES"] = "0"

classifier = Sequential()

classifier.add(Conv2D(64, (5, 5), input_shape=(256, 256, 1),
                      activation='relu'))
classifier.add(Dropout(0.2))
classifier.add(MaxPooling2D(pool_size=(4, 4)))
classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(Dropout(0.2))
classifier.add(MaxPooling2D(pool_size=(4, 4)))
classifier.add(Flatten())
classifier.add(Dense(units=128, activation='relu'))
classifier.add(Dropout(0.3))
classifier.add(Dense(units=32, activation='relu'))
classifier.add(Dense(units=1, activation='sigmoid'))

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

X = []
y = []
Exemplo n.º 7
0
def test_layer_design():
    x_train, x_test, y_train, y_test = create_data_set(list(range(10)), 20)
    RI = de.reletive_information(x_train)
    dis_set = []
    effectiveness_set = []
    loss_set = []
    ac_set = []
    loop = 50
    bs = 128
    for i in range(loop):
        x_train, x_test, y_train, y_test = create_data_set(list(range(10)), 1000)
        model = Sequential()
        model.add(Conv2D(5, kernel_size=(3, 3),
                         activation='relu',
                         input_shape=input_shape))
        # model.add(Conv2D(10 + i * 5, (3, 3), activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))
        # # #extra layer

        # extra layer
        model.add(Flatten())
        # model.add(Dense(128, activation='relu'))
        # model.add(Dense(128, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(num_classes, activation='softmax'))

        model.compile(loss=keras.losses.categorical_crossentropy,
                      optimizer=keras.optimizers.Adadelta(),
                      metrics=['accuracy'])

        # model.save_weights('Final_experiment/test_information_boundary/init.h5')
        # model.load_weights('Final_experiment/test_information_boundary_standardinit.h5')
        model.load_weights('Final_experiment/test_information_boundary_sliminit.h5')
        model.save_weights('Final_experiment/test_information_boundary/init.h5')
        model.fit(x_train, y_train,
                  batch_size=bs,
                  epochs=epochs,
                  verbose=1,
                  validation_data=(x_test, y_test))
        score = model.evaluate(x_test, y_test, verbose=0)
        print('Test loss:', score[0])
        print('Test accuracy:', score[1])
        bs = bs-2
        model.save_weights('Final_experiment/test_information_boundary/end.h5')

        dis,effectiveness = ef.effectiveness('Final_experiment/test_information_boundary/init.h5', 'Final_experiment/test_information_boundary/end.h5',
                         model, kl.KL_div, RI)
        loss_set.append(score[0])
        ac_set.append(score[1])
        dis_set.append(dis)
        effectiveness_set.append(effectiveness)


    dr.save_data(loss_set,'Final_experiment/test_information_boundary/loss.csv')
    dr.save_data(ac_set, 'Final_experiment/test_information_boundary/ac.csv')
    dr.save_data(dis_set, 'Final_experiment/test_information_boundary/dis.csv')
    dr.save_data(effectiveness_set, 'Final_experiment/test_information_boundary/ef.csv')

    plt.plot(range(loop),loss_set,label = 'loss')
    plt.plot(range(loop), ac_set,label = 'accuracy')
    plt.legend()
    plt.savefig('Final_experiment/test_information_boundary/performance_evaluation.png')
    plt.close()
    plt.plot(range(loop), dis_set,label = 'dis')
    plt.legend()
    plt.savefig('Final_experiment/test_information_boundary/dis.png')
    plt.show()
Exemplo n.º 8
0
    model . fit ( x_train , y_train , batch_size = 100 , epochs = 20 )

    result_train = model . evaluate ( x_train , y_train )
    print ( '\nTrain Acc:\n' , result_train [ 1 ])

    result_test = model . evaluate ( x_test , y_test )
    print ( '\nTest Acc:\n' , result_test [ 1 ])

    # do CNN
    x_train = x_train . reshape ( x_train . shape [ 0 ], 1 , 28 , 28 )
    x_test = x_test . reshape ( x_test . shape [ 0 ], 1 , 28 , 28 )

    model2 = Sequential ()
    model2 . add ( Conv2D ( 25 , ( 3 , 3 ), input_shape =(
        1 , 28 , 28 ), data_format = 'channels_first' ))
    model2 . add ( MaxPooling2D (( 2 , 2 )))
    model2 . add ( Conv2D ( 50 , ( 3 , 3 )))
    model2 . add ( MaxPooling2D (( 2 , 2 )))
    model2 . add ( Flatten ())
    model2 . add ( Dense ( units = 100 , activation = 'relu' ))
    model2 . add ( Dense ( units = 10 , activation = 'softmax' ))
    model2 . summary ()

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

    model2 . fit ( x_train , y_train , batch_size = 100 , epochs = 20 )

    result_train = model2 . evaluate ( x_train , y_train )
    print ( '\nTrain CNN Acc:\n' , result_train [ 1 ])
    result_test = model2 . evaluate ( x_test , y_test )
Exemplo n.º 9
0
def MusicTaggerCNN():
    # number of convolutional filters to use
    nb_filters = 32
    # size of pooling area for max pooling
    nb_pool = 2
    # convolution kernel size
    nb_conv = 3

    batch_size = 128
    #nb_classes = 10
    nb_epoch = 50
    feq_num = 12

    model = Sequential()

    # Conv layer 1 output shape (32, 300, 12)
    model.add(
        Convolution2D(
            batch_input_shape=(None, 1, seg_num, feq_num),
            filters=32,
            kernel_size=3,
            strides=1,
            padding='same',  # Padding method
            data_format='channels_first',
        ))
    model.add(Activation('relu'))

    # Pooling layer 1 (max pooling) output shape (32, 150, 6)
    model.add(
        MaxPooling2D(
            pool_size=2,
            strides=2,
            padding='same',  # Padding method
            data_format='channels_first',
        ))

    # Conv layer 2 output shape (64, 150, 6)
    model.add(
        Convolution2D(64,
                      3,
                      strides=1,
                      padding='same',
                      data_format='channels_first'))
    model.add(Activation('relu'))

    # Pooling layer 2 (max pooling) output shape (64, 75, 3)
    model.add(MaxPooling2D(2, 2, 'same', data_format='channels_first'))

    # Fully connected layer 1 input shape (64 * 75 * 3) = (14400), output shape (1024)
    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation('relu'))

    # Fully connected layer 2 to shape (40) for 40 classes
    model.add(Dense(40))
    model.add(Activation('softmax'))

    # Another way to define your optimizer
    adam = Adam(lr=1e-4)
    model.compile(optimizer=adam,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    model.fit(
        X_train,
        Y_train,
        epochs=nb_epoch,
        batch_size=batch_size,
    )

    return model
Exemplo n.º 10
0
def mnist_cnnv_datagen():
    """
    使用keras图片增强
    :return:
    """
    batch_size = 128
    nb_classes = 10  # 分类数
    nb_epoch = 12  # 训练轮数
    # 输入图片的维度
    img_rows, img_cols = 28, 28
    # 卷积滤镜的个数
    nb_filters = 32
    # 最大池化,池化核大小
    pool_size = (2, 2)
    # 卷积核大小
    kernel_size = (3, 3)

    (X_train, y_train), (X_test, y_test) = mnist.load_data(os.path.join(root_path, "data", "mnist", "mnist.npz"))

    if K.image_dim_ordering() == 'th':
        # 使用 Theano 的顺序:(conv_dim1, channels, conv_dim2, conv_dim3)
        X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
        X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        # 使用 TensorFlow 的顺序:(conv_dim1, conv_dim2, conv_dim3, channels)
        X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
        X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)

    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train /= 255
    X_test /= 255

    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)

    model = keras.models.Sequential()

    model.add(Convolution2D(nb_filters, kernel_size, padding='same', input_shape=input_shape,
                            data_format="channels_last", activation="relu"))
    model.add(Convolution2D(nb_filters, kernel_size, padding='same', activation="relu"))
    model.add(Convolution2D(nb_filters, kernel_size, padding='same', activation="relu"))
    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))

    model.add(Convolution2D(nb_filters * 2, kernel_size, padding='same', activation="relu"))
    model.add(Convolution2D(nb_filters * 2, kernel_size, padding='same', activation="relu"))
    model.add(Convolution2D(nb_filters * 2, kernel_size, padding='same', activation="relu"))
    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(128, activation="relu"))
    model.add(Dropout(0.5))
    model.add(Dense(nb_classes, activation="softmax"))

    model.summary()

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

    # 图像增强
    train_datagen = image.ImageDataGenerator(rotation_range=10, width_shift_range=0.2, height_shift_range=0.2,
                                             horizontal_flip=True, vertical_flip=False)
    validation_datagen = image.ImageDataGenerator(rotation_range=10, width_shift_range=0.2, height_shift_range=0.2)

    train_datagen.fit(X_train)
    validation_datagen.fit(X_test)

    train_generate = train_datagen.flow(X_train, Y_train, batch_size=batch_size)
    validation_generate = train_datagen.flow(X_test, Y_test, batch_size=batch_size)

    model.fit_generator(train_generate, steps_per_epoch=X_train.shape[0] // batch_size, epochs=nb_epoch, verbose=1,
                        validation_data=validation_generate, validation_steps=X_test.shape[0] // batch_size, workers=1,
                        use_multiprocessing=False)

    test_loss, test_acc = model.evaluate_generator(validation_generate, steps=X_test.shape[0] // batch_size, workers=1,
                                                   use_multiprocessing=False)

    logger.info('Test accuracy: {0} {1}'.format(test_loss, test_acc))

    predictions = model.predict(X_test)
    predictions = np.argmax(predictions, 1)
    labels = np.argmax(Y_test, 1)
    for i in range(10):
        logger.info("predict:{0} , label:{1}".format(predictions[i], labels[i]))
Exemplo n.º 11
0
first_image = image_batch[0]

data_augmentation = Sequential([
    keras.layers.experimental.preprocessing.RandomFlip("horizontal",
                                                       input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, 3)),
    keras.layers.experimental.preprocessing.RandomRotation(0.1),
    keras.layers.experimental.preprocessing.RandomZoom(0.1)
])

num_classes = 120

model = Sequential(name="Dog_Breed_Classifier", layers=[
    data_augmentation,
    keras.layers.experimental.preprocessing.Rescaling(1. / 255),
    Conv2D(16, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Dropout(0.2),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(num_classes)])

model.compile(optimizer=Adam(),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

epochs = 5
Exemplo n.º 12
0
def mnist_conv():
    """
    使用卷积神经网络训练图片分类
    :return:
    """
    batch_size = 128
    nb_classes = 10  # 分类数
    nb_epoch = 20  # 训练轮数
    # 输入图片的维度
    img_rows, img_cols = 28, 28
    # 卷积滤镜的个数
    nb_filters = 32
    # 最大池化,池化核大小
    pool_size = (2, 2)
    # 卷积核大小
    kernel_size = (3, 3)

    (X_train, y_train), (X_test, y_test) = mnist.load_data(os.path.join(root_path, "data", "mnist", "mnist.npz"))

    if K.image_dim_ordering() == 'th':
        # 使用 Theano 的顺序:(conv_dim1, channels, conv_dim2, conv_dim3)
        X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
        X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        # 使用 TensorFlow 的顺序:(conv_dim1, conv_dim2, conv_dim3, channels)
        X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
        X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)

    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train /= 255
    X_test /= 255

    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)

    model = keras.models.Sequential()
    # 可分离卷积
    # model.add(SeparableConv2D(nb_filters, kernel_size, padding='valid', activation="relu",
    #                           input_shape=input_shape, data_format="channels_last"))
    model.add(Convolution2D(nb_filters, kernel_size, padding='valid', input_shape=input_shape,
                            data_format="channels_last", activation="relu"))
    model.add(Convolution2D(nb_filters, kernel_size, padding="valid", activation="relu",
                            data_format="channels_last"))
    model.add(MaxPooling2D(pool_size=pool_size))

    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation="relu"))
    model.add(Dropout(0.25))
    model.add(Dense(nb_classes, activation="softmax"))

    model.summary()
    # keras.utils.vis_utils.plot_model(model, to_file="keras_mnist_cnn.png")

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

    os.makedirs(os.path.join(root_path, "tmp", "mnist", "logs"), exist_ok=True)
    tensorBoard_callback = keras.callbacks.TensorBoard(os.path.join(root_path, "tmp", "mnist", "logs"),
                                                       batch_size=batch_size, write_images=True, write_graph=True)
    os.makedirs(os.path.join(root_path, "tmp", "mnist", "models"), exist_ok=True)
    model_checkpoint = keras.callbacks.ModelCheckpoint(
        os.path.join(root_path, "tmp", "mnist", "models", "mnist_model_{epoch:02d}-{val_acc:.4f}.h5"),
        save_best_only=False, save_weights_only=False, monitor='val_acc')
    model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch,
              verbose=1, validation_data=(X_test, Y_test), callbacks=[tensorBoard_callback, model_checkpoint])

    test_loss, test_acc = model.evaluate(X_test, Y_test, verbose=1)

    logger.info('Test accuracy: {0} {1}'.format(test_loss, test_acc))

    predictions = model.predict(X_test)
    predictions = np.argmax(predictions, 1)
    labels = np.argmax(Y_test, 1)
    for i in range(10):
        logger.info("predict:{0} , label:{1}".format(predictions[i], labels[i]))
Exemplo n.º 13
0
# that is itself callable. It is not clear whether it is a function or an object - but we can call it as if it is a function.
# This allows us to build computational graphs that are more complex than the Sequential Model allows

# Create an input layer
inputs = Input(shape=(img_size_flat, ))

# Variable used for building the Nerual Network
net = inputs

# The input is an image as a flattened array with 784 elements
# But the convolutional layers expected images with shape (28, 28, 1
net = Reshape(img_shape_full)(net)

# First convolutional layer with relu-activation and max-pooling
net = Conv2D(filters=16, kernel_size=5, strides=1, padding='same', name='layer_conv1')(net)
net = MaxPooling2D(pool_size=2, strides=2)(net)

# Second convolutional layer with relu-activation and max-pooling
net = Conv2D(filters=36, kernel_size=5, strides=1, padding='same', name='layer_conv2')(net)
net = MaxPooling2D(pool_size=2, strides=2)(net)

# Flatten the output of the conv-layer from 4-dim to 2-dim
net = Flatten()(net)

# First fully-connected / dense layer with relu-activation
net = Dense(128, activation='relu')(net)

# Last fully-connected / dense layer with softmax-activation
net = Dense(units=num_classes, activation='softmax')(net)

outputs = net
Exemplo n.º 14
0
def MyModel(input_shape):
    model = Sequential([
        ############# ENCODER ###############
        # 224x224x1
        Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv0', input_shape=input_shape),
        # BatchNormalization(axis=3, name='bn0'),

        # 224x224x64
        Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv1'),
        # BatchNormalization(axis=3, name='bn1'),
        # 224x224x64
        MaxPooling2D((2, 2), strides=(2, 2), name='max_pool1'),

        # 112x112x64
        Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv2'),
        # BatchNormalization(axis=3, name='bn2'),

        # 112x112x128
        Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv3'),
        # BatchNormalization(axis=3, name='bn3'),
        MaxPooling2D((2, 2), strides=(2, 2), name='max_pool3'),

        # 56x56x128
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv4'),
        # BatchNormalization(axis=3, name='bn4'),

        # 56x56x256
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv5'),
        # BatchNormalization(axis=3, name='bn5'),

        # 56x56x256
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv6'),
        # BatchNormalization(axis=3, name='bn6'),
        # 56x56x256
        MaxPooling2D((2, 2), strides=(2, 2), name='max_pool6'),

        # 28x28x256
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv7'),
        # BatchNormalization(axis=3, name='bn7'),

        # 28x28x512
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv8'),
        # BatchNormalization(axis=3, name='bn8'),

        # 28x28x512
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv9'),
        # BatchNormalization(axis=3, name='bn9'),
        # 28x28x512
        MaxPooling2D((2, 2), strides=(2, 2), name='max_pool9'),

        # 14x14x512
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv10'),
        # BatchNormalization(axis=3, name='bn10'),

        # 14x14x512
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv11'),
        # BatchNormalization(axis=3, name='bn11'),

        # 14x14x512
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='conv12'),
        # BatchNormalization(axis=3, name='bn12'),
        # 14x14x512
        MaxPooling2D((2, 2), strides=(2, 2), name='max_pool12'),
        # 7x7x512

        ############# DECODER ###############
        # 7x7x512
        Conv2D(512, (1, 1), strides=(1, 1), activation='relu', name='conv13'),
        # BatchNormalization(axis=3, name='bn13'),
        Dropout(0.3),

        # 7x7x512
        UpSampling2D(size=(2, 2), name='upsampling14'),
        Conv2D(512, (5, 5), strides=(1, 1), padding='same', activation='relu', name='conv14'),
        # BatchNormalization(axis=3, name='bn14'),
        Dropout(0.3),

        # 14x14x512
        UpSampling2D(size=(2, 2), name='upsampling15'),
        Conv2D(256, (5, 5), strides=(1, 1), padding='same', activation='relu', name='conv15'),
        # BatchNormalization(axis=3, name='bn15'),
        Dropout(0.3),

        # 28x28x256
        UpSampling2D(size=(2, 2), name='upsampling16'),
        Conv2D(128, (5, 5), strides=(1, 1), padding='same', activation='relu', name='conv16'),
        # BatchNormalization(axis=3, name='bn16'),
        Dropout(0.3),

        # 56x56x128
        UpSampling2D(size=(2, 2), name='upsampling17'),
        Conv2D(64, (5, 5), strides=(1, 1), padding='same', activation='relu', name='conv17'),
        # BatchNormalization(axis=3, name='bn17'),
        Dropout(0.3),

        # 112x112x64
        UpSampling2D(size=(2, 2), name='upsampling18'),
        Conv2D(32, (5, 5), strides=(1, 1), padding='same', activation='relu', name='conv18'),
        # BatchNormalization(axis=3, name='bn18'),
        Dropout(0.3),

        # 224x224x32
        Conv2D(2, (5, 5), strides=(1, 1), padding='same', activation='sigmoid', name='conv19'),
        # BatchNormalization(axis=3, name='bn19'),
    ],

        name='MyModel')

    # 224x224x2

    return model
x_test = img_data[ int(split_ratio*np.shape(img_data)[0]):] 
print(X_train.shape)

#..............................................................................

#Define input shape :
input_shape = X_train[0].shape

print(input_shape)

#..............................................................................

# Encode the image :
input_img = Input(shape=input_shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

encoded_shape = encoded.shape
print(encoded_shape)

#..............................................................................

#Decode the encoded image :

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
Exemplo n.º 16
0
FLAGS = flags.FLAGS

flags.DEFINE_integer('nb_epochs', 6, 'Number of epochs to train model')
flags.DEFINE_integer('batch_size', 128, 'Size of training batches')
flags.DEFINE_float('learning_rate', 0.1, 'Learning rate for training')

sess = tf.Session()
keras.backend.set_session(sess)

input_img = Input(shape=(1, 28, 28))
y = tf.placeholder(tf.float32, shape=(None, 10))

encoder = Sequential()
x1 = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x2 = MaxPooling2D((2, 2), padding='same')(x1)
x3 = Conv2D(8, (3, 3), activation='relu', padding='same')(x2)
x4 = MaxPooling2D((2, 2), padding='same')(x3)
x5 = Conv2D(8, (3, 3), activation='relu', padding='same')(x4)
encoded = MaxPooling2D((2, 2), padding='same')(x5)
ae = Model(input_img, encoded)
encoder.add(ae)
encoder.add(Flatten())
encoder.add(Dense(10))
encoder.summary()

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x6 = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x7 = UpSampling2D((2, 2))(x6)
x8 = Conv2D(8, (3, 3), activation='relu', padding='same')(x7)
Exemplo n.º 17
0
from keras.preprocessing.image import ImageDataGenerator
  

#define cnn model using transfer learning
#def define_model():
  #load the model
  #model= VGG16(include_top= False, input_shape= (128,128,3))
  
  #freeze model layers as not trainable
  #for layer in model.layers:
    #layer.trainable= False

model= Sequential()

model.add(Conv2D(32,(3,3), padding= 'same', activation= 'relu', input_shape=(128,128,3)))
model.add(MaxPooling2D(2,2))
model.add(SpatialDropout2D(0.1))  #reduces overfitting

model.add(Conv2D(32,(3,3), padding= 'same', activation= 'relu'))
model.add(MaxPooling2D(2,2))
model.add(BatchNormalization())
model.add(SpatialDropout2D(0.2))

model.add(Conv2D(32,(3,3), padding= 'same', activation= 'relu'))
model.add(MaxPooling2D(2,2))
model.add(BatchNormalization())
model.add(SpatialDropout2D(0.2))

model.add(Conv2D(32,(3,3), padding= 'same', activation= 'relu'))
model.add(MaxPooling2D(2,2))
model.add(BatchNormalization())
Exemplo n.º 18
0
def build_model(identity_len=57, orientation_len=2,
        emotion_len=Emotion.length(), initial_shape=(5,4), deconv_layers=5,
        num_kernels=None, optimizer='adam'):
    """
    Builds a deconvolution FaceGen model.

    Args (optional):
        identity_len (int): Length of the identity input vector.
        orientation_len (int): Length of the orientation input vector.
        emotion_len (int): Length of the emotion input vector.
        initial_shape (tuple<int>): The starting shape of the deconv. network.
        deconv_layers (int): How many deconv. layers to use. More layers
            gives better resolution, although requires more GPU memory.
        num_kernels (list<int>): Number of convolution kernels for each layer.
        optimizer (str): The optimizer to use. Will only use default values.
    Returns:
        keras.Model, the constructed model.
    """

    if num_kernels is None:
        num_kernels = [128, 128, 96, 96, 32, 32, 16]

    # TODO: Parameter validation

    identity_input    = Input(shape=(identity_len,),    name='identity')
    orientation_input = Input(shape=(orientation_len,), name='orientation')
    emotion_input     = Input(shape=(emotion_len,),     name='emotion')

    # Hidden representation for input parameters

    id_fc = LeakyReLU()( Dense(512)(identity_input) )
    or_fc = LeakyReLU()( Dense(512)(orientation_input) )
    em_fc = LeakyReLU()( Dense(512)(emotion_input) )

    params = merge([id_fc, or_fc, em_fc], mode='concat')
    params = LeakyReLU()( Dense(1024)(params) )

    # Apply deconvolution layers

    height, width = initial_shape

    x = LeakyReLU()( Dense(height*width*num_kernels[0])(params) )
    if K.image_dim_ordering() == 'th':
        x = Reshape((num_kernels[0], height, width))(x)
    else:
        x = Reshape((height, width, num_kernels[0]))(x)

    for i in range(0, deconv_layers):
        # Pool and upsample
        x = MaxPooling2D((1,1))(x)
        x = UpSampling2D((2,2))(x)

        # Apply 5x5 and 3x3 convolutions

        # If we didn't specify the number of kernels to use for this many
        # layers, just repeat the last one in the list.
        idx = i if i < len(num_kernels) else -1
        x = LeakyReLU()( Convolution2D(num_kernels[idx], 5, 5, border_mode='same')(x) )
        x = LeakyReLU()( Convolution2D(num_kernels[idx], 3, 3, border_mode='same')(x) )
        x = BatchNormalization()(x)

    # Last deconvolution layer: Create 3-channel image.
    x = MaxPooling2D((1,1))(x)
    x = UpSampling2D((2,2))(x)
    x = LeakyReLU()( Convolution2D(8, 5, 5, border_mode='same')(x) )
    x = LeakyReLU()( Convolution2D(8, 3, 3, border_mode='same')(x) )
    x = Convolution2D(3, 3, 3, border_mode='same', activation='sigmoid')(x)

    # Compile the model

    model = Model(input=[identity_input, orientation_input,
            emotion_input], output=x)

    # TODO: Optimizer options
    model.compile(optimizer=optimizer, loss='msle')

    return model
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, BatchNormalization, Dropout, GlobalAveragePooling2D
from keras.optimizers import Adam
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint

model = Sequential()
model.add(
    Conv2D(filters=32,
           kernel_size=3,
           activation='relu',
           padding='same',
           input_shape=(32, 32, 3)))
model.add(Conv2D(filters=10, kernel_size=3, activation='relu', padding='same'))
model.add(Conv2D(filters=32, kernel_size=3, activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D())
model.add(Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'))
model.add(Conv2D(filters=10, kernel_size=3, activation='relu', padding='same'))
model.add(Conv2D(filters=10, kernel_size=3, activation='relu', padding='same'))
model.add(Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D())
model.add(Conv2D(filters=128, kernel_size=3, activation='relu',
                 padding='same'))
model.add(Conv2D(filters=10, kernel_size=3, activation='relu', padding='same'))
model.add(Conv2D(filters=10, kernel_size=3, activation='relu', padding='same'))
model.add(Conv2D(filters=128, kernel_size=3, activation='relu',
                 padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D())
model.add(Conv2D(filters=256, kernel_size=3, activation='relu',
Exemplo n.º 20
0
def make_cifar10_model(**args):
    from keras.models import Sequential, Model
    from keras.layers import Dense, Activation, Dropout, Flatten, Input, Permute
    from keras.layers import Convolution2D, MaxPooling2D, Conv2D
    import keras.backend as K
    if args: logging.debug("receiving arguments {}".format(args))
    nb_classes = 10
    img_rows, img_cols = 32, 32

    # use 1 kernel size for all convolutional layers
    ks = args.get('kernel_size', 3)

    # tune the number of filters for each convolution layer
    nb_filters1 = args.get('nb_filters1', 48)
    nb_filters2 = args.get('nb_filters2', 96)
    nb_filters3 = args.get('nb_filters3', 192)

    # tune the pool size once
    ps = args.get('pool_size', 2)
    pool_size = (ps, ps)

    # tune the dropout rates independently
    do4 = args.get('dropout1', 0.25)
    do5 = args.get('dropout2', 0.5)

    # tune the dense layers independently
    dense1 = args.get('dense1', 512)
    dense2 = args.get('dense2', 256)

    if K.image_dim_ordering() == 'th':
        input_shape = (3, img_rows, img_cols)
    else:
        input_shape = (img_rows, img_cols, 3)

    #act = 'sigmoid'
    act = 'relu'

    i = Input(input_shape)
    l = Conv2D(nb_filters1, (ks, ks), padding='same', activation=act)(i)
    l = MaxPooling2D(pool_size=pool_size)(l)
    #l = Dropout(do1)(l)

    l = Conv2D(nb_filters2, (ks, ks), padding='same', activation=act)(l)
    #l = Conv2D(nb_filters2, (ks, ks))(l)
    l = MaxPooling2D(pool_size=pool_size)(l)
    #l = Dropout(do2)(l)

    l = Conv2D(nb_filters3, (ks, ks), padding='same', activation=act)(l)
    #l = Conv2D(nb_filters3, (ks, ks))(l)
    l = MaxPooling2D(pool_size=pool_size)(l)
    #l = Dropout(do3)(l)

    l = Flatten()(l)
    l = Dense(dense1, activation=act)(l)
    l = Dropout(do4)(l)
    l = Dense(dense2, activation=act)(l)
    l = Dropout(do5)(l)

    o = Dense(nb_classes, activation='softmax')(l)

    model = Model(inputs=i, outputs=o)
    #model.summary()

    return model
Exemplo n.º 21
0
from keras.layers import Flatten
from keras.layers import Dense

#Initialising the CNN

classifier = Sequential()

# Step 1 -- Convolution

classifier.add(
    Convolution2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu')
)  # as we are using the TEnserFlow packet and hence the arguements are accepted in the reverse order

# Step 2 --> MAx Pooling

classifier.add(MaxPooling2D(pool_size=(2, 2)))

# Adding a second Convolution layer

classifier.add(Convolution2D(
    32, 3, 3, activation='relu'))  #since keras knows the images now

classifier.add(MaxPooling2D(pool_size=(2, 2)))
# Step 3 --> Flattening

classifier.add(Flatten())

#Step 4--> Full COnnections

classifier.add(Dense(output_dim=128, activation='relu'))  #HIDDEN LAYER
classifier.add(Dense(output_dim=1, activation='sigmoid'))  #OUTPUT LAYER
Exemplo n.º 22
0
    def buildNetwork(self):
        """ Assemble shared layers
        """
        inp = Input((self.env_dim))
        x = Reshape(self.env_dim)(inp)

        # Break input into s images for each history window
        inputs = []
        for s in range(self.env_dim[-1]):
            inputs.append(Reshape((self.env_dim[0], self.env_dim[1], 1)) \
                            (Lambda(lambda x: x[:,:,:,s])(inp)))

        ### Create feature extraction network for each history image
        outs = []
        for i in inputs:
            x = Conv2D(kernel_size=(8, 8),
                       strides=(4, 4),
                       filters=8,
                       activation='relu',
                       padding='same',
                       kernel_regularizer=keras.regularizers.l2(0.01))(i)
            x = Conv2D(kernel_size=(4, 4),
                       strides=(2, 2),
                       filters=16,
                       activation='relu',
                       padding='same',
                       kernel_regularizer=keras.regularizers.l2(0.01))(x)
            x = Conv2D(kernel_size=(3, 3),
                       strides=(1, 1),
                       filters=16,
                       activation='relu',
                       padding='same',
                       kernel_regularizer=keras.regularizers.l2(0.01))(x)
            x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)

            outs.append(
                Reshape((self.env_dim[0] // 2 // 2 // 2,
                         self.env_dim[1] // 2 // 2 // 2, -1))(x))

        ### Concatenate history network outputs
        x = Concatenate(axis=-1)(outs)
        x = Conv2D(kernel_size=(3, 3),
                   strides=(1, 1),
                   filters=32,
                   activation='relu',
                   padding='same',
                   kernel_regularizer=keras.regularizers.l2(0.01))(x)
        x = Conv2D(kernel_size=(3, 3),
                   strides=(1, 1),
                   filters=64,
                   activation='relu',
                   padding='same',
                   kernel_regularizer=keras.regularizers.l2(0.01))(x)
        x = Conv2D(kernel_size=(3, 3),
                   strides=(1, 1),
                   filters=64,
                   activation='relu',
                   padding='same',
                   kernel_regularizer=keras.regularizers.l2(0.01))(x)
        x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)

        x = Flatten()(x)
        return Model(inp, x)
Exemplo n.º 23
0
nb_filters = 72

pool_size = (2, 2)

kernel_size = (3, 3)

input_shape = (img_rows, img_cols, 1)



model = Sequential()

model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
                        border_mode='valid',
                        input_shape=input_shape, name="conv_1", init='glorot_normal'))
model.add(MaxPooling2D(pool_size=pool_size, name="maxpool_1"))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1], name="conv_2", init='glorot_normal'))
model.add(MaxPooling2D(pool_size=pool_size, name="maxpool_2"))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1], name="conv_3", init='glorot_normal'))
model.add(MaxPooling2D(pool_size=pool_size, name="maxpool_3"))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1], name="conv_4", init='glorot_normal'))
model.add(MaxPooling2D(pool_size=pool_size, name="maxpool_4"))
model.add(BatchNormalization(epsilon=0.001, mode=0, axis=-1, momentum=0.99, weights=None, beta_init='zero', gamma_init='one', gamma_regularizer=None, beta_regularizer=None))
model.add(Flatten())
model.add(Dense(nb_classes, init='glorot_normal'))
model.add(Activation('linear'))

model.compile(loss='hinge',
              optimizer='adadelta',
              metrics=['accuracy'])
Exemplo n.º 24
0
                        batch_size=BS,
                        target_size=TS)
valid_batch = generator('data/valid',
                        shuffle=True,
                        batch_size=BS,
                        target_size=TS)
SPE = len(train_batch.classes) // BS
VS = len(valid_batch.classes) // BS
print(SPE, VS)

# img,labels= next(train_batch)
# print(img.shape)

model = Sequential([
    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(24, 24, 1)),
    MaxPooling2D(pool_size=(1, 1)),
    Conv2D(32, (3, 3), activation='relu'),
    MaxPooling2D(pool_size=(1, 1)),
    #32 convolution filters used each of size 3x3
    #again
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D(pool_size=(1, 1)),

    #64 convolution filters used each of size 3x3
    #choose the best features via pooling

    #randomly turn neurons on and off to improve convergence
    Dropout(0.25),
    #flatten since too many dimensions, we only want a classification output
    Flatten(),
    #fully connected to get all relevant data
Exemplo n.º 25
0
def _main(args):
    print(args.config_path)
    config_path = os.path.expanduser(args.config_path)
    weights_path = os.path.expanduser(args.weights_path)
    print(config_path)
    assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format(
        config_path)
    assert weights_path.endswith(
        '.weights'), '{} is not a .weights file'.format(weights_path)

    output_path = os.path.expanduser(args.output_path)
    assert output_path.endswith(
        '.h5'), 'output path {} is not a .h5 file'.format(output_path)
    output_root = os.path.splitext(output_path)[0]

    # Load weights and config.
    print('Loading weights.')
    weights_file = open(weights_path, 'rb')
    major, minor, revision = np.ndarray(
        shape=(3, ), dtype='int32', buffer=weights_file.read(12))
    if (major*10+minor)>=2 and major<1000 and minor<1000:
        seen = np.ndarray(shape=(1,), dtype='int64', buffer=weights_file.read(8))
    else:
        seen = np.ndarray(shape=(1,), dtype='int32', buffer=weights_file.read(4))
    print('Weights Header: ', major, minor, revision, seen)

    print('Parsing Darknet config.')
    unique_config_file = unique_config_sections(config_path)
    cfg_parser = configparser.ConfigParser()
    cfg_parser.read_file(unique_config_file)

    print('Creating Keras model.')
    input_layer = Input(shape=(None, None, 3))
    prev_layer = input_layer
    all_layers = []

    weight_decay = float(cfg_parser['net_0']['decay']
                         ) if 'net_0' in cfg_parser.sections() else 5e-4
    count = 0
    out_index = []
    for section in cfg_parser.sections():
        print('Parsing section {}'.format(section))
        if section.startswith('convolutional'):
            filters = int(cfg_parser[section]['filters'])
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            pad = int(cfg_parser[section]['pad'])
            activation = cfg_parser[section]['activation']
            batch_normalize = 'batch_normalize' in cfg_parser[section]

            padding = 'same' if pad == 1 and stride == 1 else 'valid'

            # Setting weights.
            # Darknet serializes convolutional weights as:
            # [bias/beta, [gamma, mean, variance], conv_weights]
            prev_layer_shape = K.int_shape(prev_layer)

            weights_shape = (size, size, prev_layer_shape[-1], filters)
            darknet_w_shape = (filters, weights_shape[2], size, size)
            weights_size = np.product(weights_shape)

            print('conv2d', 'bn'
                  if batch_normalize else '  ', activation, weights_shape)

            conv_bias = np.ndarray(
                shape=(filters, ),
                dtype='float32',
                buffer=weights_file.read(filters * 4))
            count += filters

            if batch_normalize:
                bn_weights = np.ndarray(
                    shape=(3, filters),
                    dtype='float32',
                    buffer=weights_file.read(filters * 12))
                count += 3 * filters

                bn_weight_list = [
                    bn_weights[0],  # scale gamma
                    conv_bias,  # shift beta
                    bn_weights[1],  # running mean
                    bn_weights[2]  # running var
                ]

            conv_weights = np.ndarray(
                shape=darknet_w_shape,
                dtype='float32',
                buffer=weights_file.read(weights_size * 4))
            count += weights_size

            # DarkNet conv_weights are serialized Caffe-style:
            # (out_dim, in_dim, height, width)
            # We would like to set these to Tensorflow order:
            # (height, width, in_dim, out_dim)
            conv_weights = np.transpose(conv_weights, [2, 3, 1, 0])
            conv_weights = [conv_weights] if batch_normalize else [
                conv_weights, conv_bias
            ]

            # Handle activation.
            act_fn = None
            if activation == 'leaky':
                pass  # Add advanced activation later.
            elif activation != 'linear':
                raise ValueError(
                    'Unknown activation function `{}` in section {}'.format(
                        activation, section))

            # Create Conv2D layer
            if stride>1:
                # Darknet uses left and top padding instead of 'same' mode
                prev_layer = ZeroPadding2D(((1,0),(1,0)))(prev_layer)
            conv_layer = (Conv2D(
                filters, (size, size),
                strides=(stride, stride),
                kernel_regularizer=l2(weight_decay),
                use_bias=not batch_normalize,
                weights=conv_weights,
                activation=act_fn,
                padding=padding))(prev_layer)

            if batch_normalize:
                conv_layer = (BatchNormalization(
                    weights=bn_weight_list))(conv_layer)
            prev_layer = conv_layer

            if activation == 'linear':
                all_layers.append(prev_layer)
            elif activation == 'leaky':
                act_layer = LeakyReLU(alpha=0.1)(prev_layer)
                prev_layer = act_layer
                all_layers.append(act_layer)

        elif section.startswith('route'):
            ids = [int(i) for i in cfg_parser[section]['layers'].split(',')]
            layers = [all_layers[i] for i in ids]
            if len(layers) > 1:
                print('Concatenating route layers:', layers)
                concatenate_layer = Concatenate()(layers)
                all_layers.append(concatenate_layer)
                prev_layer = concatenate_layer
            else:
                skip_layer = layers[0]  # only one layer to route
                all_layers.append(skip_layer)
                prev_layer = skip_layer

        elif section.startswith('maxpool'):
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            all_layers.append(
                MaxPooling2D(
                    pool_size=(size, size),
                    strides=(stride, stride),
                    padding='same')(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('shortcut'):
            index = int(cfg_parser[section]['from'])
            activation = cfg_parser[section]['activation']
            assert activation == 'linear', 'Only linear activation supported.'
            all_layers.append(Add()([all_layers[index], prev_layer]))
            prev_layer = all_layers[-1]

        elif section.startswith('upsample'):
            stride = int(cfg_parser[section]['stride'])
            assert stride == 2, 'Only stride=2 supported.'
            all_layers.append(UpSampling2D(stride)(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('yolo'):
            out_index.append(len(all_layers)-1)
            all_layers.append(None)
            prev_layer = all_layers[-1]

        elif section.startswith('net'):
            pass

        else:
            raise ValueError(
                'Unsupported section header type: {}'.format(section))

    # Create and save model.
    if len(out_index)==0: out_index.append(len(all_layers)-1)
    model = Model(inputs=input_layer, outputs=[all_layers[i] for i in out_index])
    print(model.summary())
    if args.weights_only:
        model.save_weights('{}'.format(output_path))
        print('Saved Keras weights to {}'.format(output_path))
    else:
        model.save('{}'.format(output_path))
        print('Saved Keras model to {}'.format(output_path))

    # Check to see if all weights have been read.
    remaining_weights = len(weights_file.read()) / 4
    weights_file.close()
    print('Read {} of {} from Darknet weights.'.format(count, count +
                                                       remaining_weights))
    if remaining_weights > 0:
        print('Warning: {} unused weights'.format(remaining_weights))

    if args.plot_model:
        plot(model, to_file='{}.png'.format(output_root), show_shapes=True)
        print('Saved model plot to {}.png'.format(output_root))
Exemplo n.º 26
0
def build_cnn(nr_convs):
    model = Sequential()

    model.add(Conv2D(32, (5, 5), name="conv1", activation='relu', input_shape=(224,224,3)))
    model.add(BatchNormalization(axis=3, name="batch1"))
    model.add(MaxPooling2D(pool_size=(2,2), name="pool1"))
    
    if(nr_convs > 1):
        model.add(Conv2D(16, (5, 5), name="conv2", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch2"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool2"))

    if(nr_convs > 2):
        model.add(Conv2D(64, (5, 5), name="conv3", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch3"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool3"))

    if(nr_convs > 3):
        model.add(Conv2D(32, (3, 3), name="conv4", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch4"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool4"))
        
    if(nr_convs > 4):
        model.add(Conv2D(32, (3, 3), name="conv5", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch5"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool5"))

    if(nr_convs > 5):
        model.add(Conv2D(32, (3, 3), name="conv6", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch6"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool6"))

    if(nr_convs > 6):
        model.add(Conv2D(16, (3, 3), name="conv7", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch7"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool7"))

    if(nr_convs > 7):
        model.add(Conv2D(8, (3, 3), name="conv8", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch8"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool8"))

    if(nr_convs > 8):
        model.add(Conv2D(8, (3, 3), name="conv9", activation='relu'))
        model.add(BatchNormalization(axis=3, name="batch9"))
        model.add(MaxPooling2D(pool_size=(2,2), name="pool9"))

    #model.add(Dropout(0.25))
    #
    model.add(Flatten())

    model.add(Dense(64, activation='relu', name='dense1'))
    model.add(Dropout(0.25))

    model.add(Dense(32, activation='relu', name='dense2'))

    model.add(Dense(16, activation='relu', name='dense3'))

    model.add(Dense(2, activation='softmax', name='dense4'))
    #
    sgd = SGD(lr=1e-6, decay=4e-5, momentum=0.9, nesterov=False)
    model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

    return model
    ],
    class_mode='other',
    batch_size=16)

#create model
model = Sequential()

#add model layers
model.add(
    Conv2D(64,
           data_format="channels_last",
           kernel_size=3,
           input_shape=(108, 192, 3),
           activation='relu'))
model.add(Conv2D(64, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, kernel_size=3, activation='relu'))
model.add(Conv2D(256, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dense(32, activation='relu'))

#compile model using accuracy to measure model performance
model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['accuracy'])
Exemplo n.º 28
0
def build_model(image_size,
                n_classes,
                min_scale=0.1,
                max_scale=0.9,
                scales=None,
                aspect_ratios_global=[0.5, 1.0, 2.0],
                aspect_ratios_per_layer=None,
                two_boxes_for_ar1=True,
                limit_boxes=True,
                variances=[1.0, 1.0, 1.0, 1.0],
                coords='centroids',
                normalize_coords=False):
    '''
    Build a Keras model with SSD architecture, see references.

    The model consists of convolutional feature layers and a number of convolutional
    predictor layers that take their input from different feature layers.
    The model is fully convolutional.

    The implementation found here is a smaller version of the original architecture
    used in the paper (where the base network consists of a modified VGG-16 extended
    by a few convolutional feature layers), but of course it could easily be changed to
    an arbitrarily large SSD architecture by following the general design pattern used here.
    This implementation has 7 convolutional layers and 4 convolutional predictor
    layers that take their input from layers 4, 5, 6, and 7, respectively.

    In case you're wondering why this function has so many arguments: All arguments except
    the first two (`image_size` and `n_classes`) are only needed so that the anchor box
    layers can produce the correct anchor boxes. In case you're training the network, the
    parameters passed here must be the same as the ones used to set up `SSDBoxEncoder`.
    In case you're loading trained weights, the parameters passed here must be the same
    as the ones used to produce the trained weights.

    Some of these arguments are explained in more detail in the documentation of the
    `SSDBoxEncoder` class.

    Note: Requires Keras v2.0 or later. Training currently works only with the
    TensorFlow backend (v1.0 or later).

    Arguments:
        image_size (tuple): The input image size in the format `(height, width, channels)`.
        n_classes (int): The number of categories for classification including
            the background class (i.e. the number of positive classes +1 for
            the background calss).
        min_scale (float, optional): The smallest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images. Defaults to 0.1.
        max_scale (float, optional): The largest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images. All scaling factors between the smallest and the
            largest will be linearly interpolated. Note that the second to last of the linearly interpolated
            scaling factors will actually be the scaling factor for the last predictor layer, while the last
            scaling factor is used for the second box for aspect ratio 1 in the last predictor layer
            if `two_boxes_for_ar1` is `True`. Defaults to 0.9.
        scales (list, optional): A list of floats containing scaling factors per convolutional predictor layer.
            This list must be one element longer than the number of predictor layers. The first `k` elements are the
            scaling factors for the `k` predictor layers, while the last element is used for the second box
            for aspect ratio 1 in the last predictor layer if `two_boxes_for_ar1` is `True`. This additional
            last scaling factor must be passed either way, even if it is not being used.
            Defaults to `None`. If a list is passed, this argument overrides `min_scale` and
            `max_scale`. All scaling factors must be greater than zero.
        aspect_ratios_global (list, optional): The list of aspect ratios for which anchor boxes are to be
            generated. This list is valid for all predictor layers. The original implementation uses more aspect ratios
            for some predictor layers and fewer for others. If you want to do that, too, then use the next argument instead.
            Defaults to `[0.5, 1.0, 2.0]`.
        aspect_ratios_per_layer (list, optional): A list containing one aspect ratio list for each predictor layer.
            This allows you to set the aspect ratios for each predictor layer individually. If a list is passed,
            it overrides `aspect_ratios_global`. Defaults to `None`.
        two_boxes_for_ar1 (bool, optional): Only relevant for aspect ratio lists that contain 1. Will be ignored otherwise.
            If `True`, two anchor boxes will be generated for aspect ratio 1. The first will be generated
            using the scaling factor for the respective layer, the second one will be generated using
            geometric mean of said scaling factor and next bigger scaling factor. Defaults to `True`, following the original
            implementation.
        limit_boxes (bool, optional): If `True`, limits box coordinates to stay within image boundaries.
            This would normally be set to `True`, but here it defaults to `False`, following the original
            implementation.
        variances (list, optional): A list of 4 floats >0 with scaling factors (actually it's not factors but divisors
            to be precise) for the encoded predicted box coordinates. A variance value of 1.0 would apply
            no scaling at all to the predictions, while values in (0,1) upscale the encoded predictions and values greater
            than 1.0 downscale the encoded predictions. If you want to reproduce the configuration of the original SSD,
            set this to `[0.1, 0.1, 0.2, 0.2]`, provided the coordinate format is 'centroids'. Defaults to `[1.0, 1.0, 1.0, 1.0]`.
        coords (str, optional): The box coordinate format to be used. Can be either 'centroids' for the format
            `(cx, cy, w, h)` (box center coordinates, width, and height) or 'minmax' for the format
            `(xmin, xmax, ymin, ymax)`. Defaults to 'centroids'.
        normalize_coords (bool, optional): Set to `True` if the model is supposed to use relative instead of absolute coordinates,
            i.e. if the model predicts box coordinates within [0,1] instead of absolute coordinates. Defaults to `False`.

    Returns:
        model: The Keras SSD model.
        predictor_sizes: A Numpy array containing the `(height, width)` portion
            of the output tensor shape for each convolutional predictor layer. During
            training, the generator function needs this in order to transform
            the ground truth labels into tensors of identical structure as the
            output tensors of the model, which is in turn needed for the cost
            function.

    References:
        https://arxiv.org/abs/1512.02325v5
    '''

    n_predictor_layers = 4 # The number of predictor conv layers in the network

    # Get a few exceptions out of the way first
    if aspect_ratios_global is None and aspect_ratios_per_layer is None:
        raise ValueError("`aspect_ratios_global` and `aspect_ratios_per_layer` cannot both be None. At least one needs to be specified.")
    if aspect_ratios_per_layer:
        if len(aspect_ratios_per_layer) != n_predictor_layers:
            raise ValueError("It must be either aspect_ratios_per_layer is None or len(aspect_ratios_per_layer) == {}, but len(aspect_ratios_per_layer) == {}.".format(n_predictor_layers, len(aspect_ratios_per_layer)))

    if (min_scale is None or max_scale is None) and scales is None:
        raise ValueError("Either `min_scale` and `max_scale` or `scales` need to be specified.")
    if scales:
        if len(scales) != n_predictor_layers+1:
            raise ValueError("It must be either scales is None or len(scales) == {}, but len(scales) == {}.".format(n_predictor_layers+1, len(scales)))
    else: # If no explicit list of scaling factors was passed, compute the list of scaling factors from `min_scale` and `max_scale`
        scales = np.linspace(min_scale, max_scale, n_predictor_layers+1)

    if len(variances) != 4: # We need one variance value for each of the four box coordinates
        raise ValueError("4 variance values must be pased, but {} values were received.".format(len(variances)))
    variances = np.array(variances)
    if np.any(variances <= 0):
        raise ValueError("All variances must be >0, but the variances given are {}".format(variances))

    # Set the aspect ratios for each predictor layer. These are only needed for the anchor box layers.
    if aspect_ratios_per_layer:
        aspect_ratios_conv4 = aspect_ratios_per_layer[0]
        aspect_ratios_conv5 = aspect_ratios_per_layer[1]
        aspect_ratios_conv6 = aspect_ratios_per_layer[2]
        aspect_ratios_conv7 = aspect_ratios_per_layer[3]
    else:
        aspect_ratios_conv4 = aspect_ratios_global
        aspect_ratios_conv5 = aspect_ratios_global
        aspect_ratios_conv6 = aspect_ratios_global
        aspect_ratios_conv7 = aspect_ratios_global

    # Compute the number of boxes to be predicted per cell for each predictor layer.
    # We need this so that we know how many channels the predictor layers need to have.
    if aspect_ratios_per_layer:
        n_boxes = []
        for aspect_ratios in aspect_ratios_per_layer:
            if (1 in aspect_ratios) & two_boxes_for_ar1:
                n_boxes.append(len(aspect_ratios) + 1) # +1 for the second box for aspect ratio 1
            else:
                n_boxes.append(len(aspect_ratios))
        n_boxes_conv4 = n_boxes[0]
        n_boxes_conv5 = n_boxes[1]
        n_boxes_conv6 = n_boxes[2]
        n_boxes_conv7 = n_boxes[3]
    else: # If only a global aspect ratio list was passed, then the number of boxes is the same for each predictor layer
        if (1 in aspect_ratios_global) & two_boxes_for_ar1:
            n_boxes = len(aspect_ratios_global) + 1
        else:
            n_boxes = len(aspect_ratios_global)
        n_boxes_conv4 = n_boxes
        n_boxes_conv5 = n_boxes
        n_boxes_conv6 = n_boxes
        n_boxes_conv7 = n_boxes

    # Input image format
    img_height, img_width, img_channels = image_size[0], image_size[1], image_size[2]

    # Design the actual network
    x = Input(shape=(img_height, img_width, img_channels))
    normed = Lambda(lambda z: z/127.5 - 1., # Convert input feature range to [-1,1]
                    output_shape=(img_height, img_width, img_channels),
                    name='lambda1')(x)

    conv1 = Conv2D(32, (5, 5), name='conv1', strides=(1, 1), padding="same")(normed)
    conv1 = BatchNormalization(axis=3, momentum=0.99, name='bn1')(conv1) # Tensorflow uses filter format [filter_height, filter_width, in_channels, out_channels], hence axis = 3
    conv1 = ELU(name='elu1')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2), name='pool1')(conv1)

    conv2 = Conv2D(48, (3, 3), name='conv2', strides=(1, 1), padding="same")(pool1)
    conv2 = BatchNormalization(axis=3, momentum=0.99, name='bn2')(conv2)
    conv2 = ELU(name='elu2')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2), name='pool2')(conv2)

    conv3 = Conv2D(64, (3, 3), name='conv3', strides=(1, 1), padding="same")(pool2)
    conv3 = BatchNormalization(axis=3, momentum=0.99, name='bn3')(conv3)
    conv3 = ELU(name='elu3')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2), name='pool3')(conv3)

    conv4 = Conv2D(64, (3, 3), name='conv4', strides=(1, 1), padding="same")(pool3)
    conv4 = BatchNormalization(axis=3, momentum=0.99, name='bn4')(conv4)
    conv4 = ELU(name='elu4')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2), name='pool4')(conv4)

    conv5 = Conv2D(48, (3, 3), name='conv5', strides=(1, 1), padding="same")(pool4)
    conv5 = BatchNormalization(axis=3, momentum=0.99, name='bn5')(conv5)
    conv5 = ELU(name='elu5')(conv5)
    pool5 = MaxPooling2D(pool_size=(2, 2), name='pool5')(conv5)

    conv6 = Conv2D(48, (3, 3), name='conv6', strides=(1, 1), padding="same")(pool5)
    conv6 = BatchNormalization(axis=3, momentum=0.99, name='bn6')(conv6)
    conv6 = ELU(name='elu6')(conv6)
    pool6 = MaxPooling2D(pool_size=(2, 2), name='pool6')(conv6)

    conv7 = Conv2D(32, (3, 3), name='conv7', strides=(1, 1), padding="same")(pool6)
    conv7 = BatchNormalization(axis=3, momentum=0.99, name='bn7')(conv7)
    conv7 = ELU(name='elu7')(conv7)

    # The next part is to add the convolutional predictor layers on top of the base network
    # that we defined above. Note that I use the term "base network" differently than the paper does.
    # To me, the base network is everything that is not convolutional predictor layers or anchor
    # box layers. In this case we'll have four predictor layers, but of course you could
    # easily rewrite this into an arbitrarily deep base network and add an arbitrary number of
    # predictor layers on top of the base network by simply following the pattern shown here.

    # Build the convolutional predictor layers on top of conv layers 4, 5, 6, and 7
    # We build two predictor layers on top of each of these layers: One for classes (classification), one for box coordinates (localization)
    # We precidt `n_classes` confidence values for each box, hence the `classes` predictors have depth `n_boxes * n_classes`
    # We predict 4 box coordinates for each box, hence the `boxes` predictors have depth `n_boxes * 4`
    # Output shape of `classes`: `(batch, height, width, n_boxes * n_classes)`
    classes4 = Conv2D(n_boxes_conv4 * n_classes, (3, 3), strides=(1, 1), padding="valid", name='classes4')(conv4)
    classes5 = Conv2D(n_boxes_conv5 * n_classes, (3, 3), strides=(1, 1), padding="valid", name='classes5')(conv5)
    classes6 = Conv2D(n_boxes_conv6 * n_classes, (3, 3), strides=(1, 1), padding="valid", name='classes6')(conv6)
    classes7 = Conv2D(n_boxes_conv7 * n_classes, (3, 3), strides=(1, 1), padding="valid", name='classes7')(conv7)
    # Output shape of `boxes`: `(batch, height, width, n_boxes * 4)`
    boxes4 = Conv2D(n_boxes_conv4 * 4, (3, 3), strides=(1, 1), padding="valid", name='boxes4')(conv4)
    boxes5 = Conv2D(n_boxes_conv5 * 4, (3, 3), strides=(1, 1), padding="valid", name='boxes5')(conv5)
    boxes6 = Conv2D(n_boxes_conv6 * 4, (3, 3), strides=(1, 1), padding="valid", name='boxes6')(conv6)
    boxes7 = Conv2D(n_boxes_conv7 * 4, (3, 3), strides=(1, 1), padding="valid", name='boxes7')(conv7)

    # Generate the anchor boxes
    # Output shape of `anchors`: `(batch, height, width, n_boxes, 8)`
    anchors4 = AnchorBoxes(img_height, img_width, this_scale=scales[0], next_scale=scales[1], aspect_ratios=aspect_ratios_conv4,
                           two_boxes_for_ar1=two_boxes_for_ar1, limit_boxes=limit_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors4')(boxes4)
    anchors5 = AnchorBoxes(img_height, img_width, this_scale=scales[1], next_scale=scales[2], aspect_ratios=aspect_ratios_conv5,
                           two_boxes_for_ar1=two_boxes_for_ar1, limit_boxes=limit_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors5')(boxes5)
    anchors6 = AnchorBoxes(img_height, img_width, this_scale=scales[2], next_scale=scales[3], aspect_ratios=aspect_ratios_conv6,
                           two_boxes_for_ar1=two_boxes_for_ar1, limit_boxes=limit_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors6')(boxes6)
    anchors7 = AnchorBoxes(img_height, img_width, this_scale=scales[3], next_scale=scales[4], aspect_ratios=aspect_ratios_conv7,
                           two_boxes_for_ar1=two_boxes_for_ar1, limit_boxes=limit_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors7')(boxes7)

    # Reshape the class predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, n_classes)`
    # We want the classes isolated in the last axis to perform softmax on them
    classes4_reshaped = Reshape((-1, n_classes), name='classes4_reshape')(classes4)
    classes5_reshaped = Reshape((-1, n_classes), name='classes5_reshape')(classes5)
    classes6_reshaped = Reshape((-1, n_classes), name='classes6_reshape')(classes6)
    classes7_reshaped = Reshape((-1, n_classes), name='classes7_reshape')(classes7)
    # Reshape the box coordinate predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, 4)`
    # We want the four box coordinates isolated in the last axis to compute the smooth L1 loss
    boxes4_reshaped = Reshape((-1, 4), name='boxes4_reshape')(boxes4)
    boxes5_reshaped = Reshape((-1, 4), name='boxes5_reshape')(boxes5)
    boxes6_reshaped = Reshape((-1, 4), name='boxes6_reshape')(boxes6)
    boxes7_reshaped = Reshape((-1, 4), name='boxes7_reshape')(boxes7)
    # Reshape the anchor box tensors, yielding 3D tensors of shape `(batch, height * width * n_boxes, 8)`
    anchors4_reshaped = Reshape((-1, 8), name='anchors4_reshape')(anchors4)
    anchors5_reshaped = Reshape((-1, 8), name='anchors5_reshape')(anchors5)
    anchors6_reshaped = Reshape((-1, 8), name='anchors6_reshape')(anchors6)
    anchors7_reshaped = Reshape((-1, 8), name='anchors7_reshape')(anchors7)

    # Concatenate the predictions from the different layers and the assosciated anchor box tensors
    # Axis 0 (batch) and axis 2 (n_classes or 4, respectively) are identical for all layer predictions,
    # so we want to concatenate along axis 1
    # Output shape of `classes_merged`: (batch, n_boxes_total, n_classes)
    classes_concat = Concatenate(axis=1, name='classes_concat')([classes4_reshaped,
                                                                 classes5_reshaped,
                                                                 classes6_reshaped,
                                                                 classes7_reshaped])

    # Output shape of `boxes_final`: (batch, n_boxes_total, 4)
    boxes_concat = Concatenate(axis=1, name='boxes_concat')([boxes4_reshaped,
                                                             boxes5_reshaped,
                                                             boxes6_reshaped,
                                                             boxes7_reshaped])

    # Output shape of `anchors_final`: (batch, n_boxes_total, 8)
    anchors_concat = Concatenate(axis=1, name='anchors_concat')([anchors4_reshaped,
                                                                 anchors5_reshaped,
                                                                 anchors6_reshaped,
                                                                 anchors7_reshaped])

    # The box coordinate predictions will go into the loss function just the way they are,
    # but for the class predictions, we'll apply a softmax activation layer first
    classes_softmax = Activation('softmax', name='classes_softmax')(classes_concat)

    # Concatenate the class and box coordinate predictions and the anchors to one large predictions tensor
    # Output shape of `predictions`: (batch, n_boxes_total, n_classes + 4 + 8)
    predictions = Concatenate(axis=2, name='predictions')([classes_softmax, boxes_concat, anchors_concat])

    model = Model(inputs=x, outputs=predictions)

    # Get the spatial dimensions (height, width) of the convolutional predictor layers, we need them to generate the default boxes
    # The spatial dimensions are the same for the `classes` and `boxes` predictors
    predictor_sizes = np.array([classes4._keras_shape[1:3],
                                classes5._keras_shape[1:3],
                                classes6._keras_shape[1:3],
                                classes7._keras_shape[1:3]])

    return model, predictor_sizes

##########################
# source model
##########################

input = Input(shape=(IMG_ROWS,IMG_COLS,IMG_CHANNELS))

# build model
out = Conv2D(64, (3, 3), padding='same', kernel_regularizer=keras.regularizers.l2(weight_decay), kernel_initializer=he_normal(), name='block1_conv1')(input)
out = BatchNormalization(name='batch_normalization_1')(out)
out = Activation('relu')(out)
out = Conv2D(64, (3, 3),padding='same', kernel_regularizer=keras.regularizers.l2(weight_decay), kernel_initializer=he_normal(), name='block1_conv2')(out)
out = BatchNormalization(name='batch_normalization_2')(out)
out = Activation('relu')(out)
out = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(out)

# Block 2
out = Conv2D(128, (3, 3), padding='same', kernel_regularizer=keras.regularizers.l2(weight_decay), kernel_initializer=he_normal(), name='block2_conv1')(out)
out = BatchNormalization(name='batch_normalization_3')(out)
out = Activation('relu')(out)
out = Conv2D(128, (3, 3), padding='same', kernel_regularizer=keras.regularizers.l2(weight_decay), kernel_initializer=he_normal(), name='block2_conv2')(out)
out = BatchNormalization(name='batch_normalization_4')(out)
out = Activation('relu')(out)
out = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(out)

# Block 3
out = Conv2D(256, (3, 3),  padding='same', kernel_regularizer=keras.regularizers.l2(weight_decay), kernel_initializer=he_normal(), name='block3_conv1')(out)
out = BatchNormalization(name='batch_normalization_5')(out)
out = Activation('relu')(out)
out = Conv2D(256, (3, 3),  padding='same', kernel_regularizer=keras.regularizers.l2(weight_decay), kernel_initializer=he_normal(), name='block3_conv2')(out)
Exemplo n.º 30
0
input_shape = tuple([X_train.shape[x] for x in range(1, len(X_train.shape))])
print(input_shape)
inputs = Input(shape=input_shape)
zen_layer = Lambda(lambda x: x / 255.0 - 0.5)(
    inputs)  # Normalizes and centers images

# First convolutional layer runs filters of conv_shapes sizes on the preprocessed inputs
# The outputs are then concatenated into a single layer
for shp in conv_shapes:
    stacks.append(
        Conv2D(depths[0], (shp, shp), padding='same',
               activation='elu')(zen_layer))

layer = concatenate(stacks, axis=3)
maxPool = MaxPooling2D((2, 2), strides=(2, 2), padding='valid')(layer)

# The following convolutional layers each share the stacking-parallel shape
for i in range(1, len(depths)):
    stacks = []
    for shp in conv_shapes:
        stacks.append(
            Conv2D(depths[i], (shp, shp), padding='same',
                   activation='elu')(maxPool))

    layer = concatenate(stacks, axis=3)
    maxPool = MaxPooling2D((2, 2), strides=(2, 2), padding='valid')(layer)

flat_layer = Flatten()(maxPool)
flat_layer = Dropout(.5)(flat_layer)  # Dropout reduces overfitting