Ejemplo n.º 1
0
features = features.reshape(features.shape[0], 224, 224, 3)
features.shape

features /= 255  #normalize in [0, 1]

train_x, test_x, train_y, test_y = train_test_split(
    features, target_classes,
    test_size=0.30)  #, random_state=42), stratify=target_classes)

#VGG-Face model
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
Ejemplo n.º 2
0
inp = Input(X_train.shape[1:])

num_ens = 2
outs = []
for i in range(0, num_ens):
    conv1 = Conv2D(32, (3, 3),
                   kernel_initializer='he_uniform',
                   activation='relu')(inp)
    conv1 = BatchNormalization()(conv1)
    conv1 = Dropout(0.3)(conv1)

    conv3 = Conv2D(32, (3, 3),
                   kernel_initializer='he_uniform',
                   activation='relu')(conv1)
    conv3 = BatchNormalization()(conv3)
    conv3 = MaxPooling2D()(conv3)
    conv3 = Dropout(0.3)(conv3)

    conv4 = Conv2D(64, (3, 3),
                   kernel_initializer='he_uniform',
                   activation='relu')(conv3)
    conv4 = BatchNormalization()(conv4)
    conv4 = MaxPooling2D()(conv4)
    conv4 = Dropout(0.3)(conv4)

    conv5 = Conv2D(64, (3, 3),
                   kernel_initializer='he_uniform',
                   activation='relu')(conv4)
    conv5 = BatchNormalization()(conv5)
    conv5 = MaxPooling2D()(conv5)
    conv5 = Dropout(0.3)(conv5)
Ejemplo n.º 3
0
def RESNET50(include_top=True, weights='vggface',
             input_tensor=None, input_shape=None,
             pooling=None,
             classes=8631):
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=32,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top,
                                      weights=weights)

    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_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    x = Conv2D(
        64, (7, 7), use_bias=False, strides=(2, 2), padding='same',
        name='conv1/7x7_s2')(img_input)
    x = BatchNormalization(axis=bn_axis, name='conv1/7x7_s2/bn')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

    x = resnet_conv_block(x, 3, [64, 64, 256], stage=2, block=1, strides=(1, 1))
    x = resnet_identity_block(x, 3, [64, 64, 256], stage=2, block=2)
    x = resnet_identity_block(x, 3, [64, 64, 256], stage=2, block=3)

    x = resnet_conv_block(x, 3, [128, 128, 512], stage=3, block=1)
    x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=2)
    x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=3)
    x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=4)

    x = resnet_conv_block(x, 3, [256, 256, 1024], stage=4, block=1)
    x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=2)
    x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=3)
    x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=4)
    x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=5)
    x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=6)

    x = resnet_conv_block(x, 3, [512, 512, 2048], stage=5, block=1)
    x = resnet_identity_block(x, 3, [512, 512, 2048], stage=5, block=2)
    x = resnet_identity_block(x, 3, [512, 512, 2048], stage=5, block=3)

    x = AveragePooling2D((7, 7), name='avg_pool')(x)

    if include_top:
        x = Flatten()(x)
        x = Dense(classes, activation='softmax', name='classifier')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='vggface_resnet50')

    # load weights
    if weights == 'vggface':
        if include_top:
            weights_path = get_file('rcmalli_vggface_tf_resnet50.h5',
                                    utils.RESNET50_WEIGHTS_PATH,
                                    cache_subdir=utils.VGGFACE_DIR)
        else:
            weights_path = get_file('rcmalli_vggface_tf_notop_resnet50.h5',
                                    utils.RESNET50_WEIGHTS_PATH_NO_TOP,
                                    cache_subdir=utils.VGGFACE_DIR)
        model.load_weights(weights_path)
        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)
            if include_top:
                maxpool = model.get_layer(name='avg_pool')
                shape = maxpool.output_shape[1:]
                dense = model.get_layer(name='classifier')
                layer_utils.convert_dense_weights_data_format(dense, shape,
                                                              'channels_first')

        if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow':
            warnings.warn('You are using the TensorFlow backend, yet you '
                          'are using the Theano '
                          'image data format convention '
                          '(`image_data_format="channels_first"`). '
                          'For best performance, set '
                          '`image_data_format="channels_last"` in '
                          'your Keras config '
                          'at ~/.keras/keras.json.')
    elif weights is not None:
        model.load_weights(weights)

    return model
Ejemplo n.º 4
0
from keras.layers import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Flatten, Dense
from keras.datasets import mnist
from keras.utils import to_categorical

model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))

model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
Ejemplo n.º 5
0
    # load model
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    model = Sequential()
    weight_decay = 0.0005

    model.add(Conv2D(64, (3, 3), padding='same',
                     input_shape=x_train.shape[1:],kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    model.add(Dropout(0.3))

    model.add(Conv2D(64, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())

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

    model.add(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    model.add(Dropout(0.4))

    model.add(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())

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

    model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
Ejemplo n.º 6
0
def get_model(height, nclass):
    input = Input(shape=(height, None, 1), name='the_input')
    m = Conv2D(64,
               kernel_size=(3, 3),
               activation='relu',
               padding='same',
               name='conv1')(input)
    m = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(m)
    m = Conv2D(128,
               kernel_size=(3, 3),
               activation='relu',
               padding='same',
               name='conv2')(m)
    m = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool2')(m)
    m = Conv2D(256,
               kernel_size=(3, 3),
               activation='relu',
               padding='same',
               name='conv3')(m)
    m = Conv2D(256,
               kernel_size=(3, 3),
               activation='relu',
               padding='same',
               name='conv4')(m)

    m = ZeroPadding2D(padding=(0, 1))(m)
    m = MaxPooling2D(pool_size=(2, 2),
                     strides=(2, 1),
                     padding='valid',
                     name='pool3')(m)

    m = Conv2D(512,
               kernel_size=(3, 3),
               activation='relu',
               padding='same',
               name='conv5')(m)
    m = BatchNormalization(axis=1)(m)
    m = Conv2D(512,
               kernel_size=(3, 3),
               activation='relu',
               padding='same',
               name='conv6')(m)
    m = BatchNormalization(axis=1)(m)
    m = ZeroPadding2D(padding=(0, 1))(m)
    m = MaxPooling2D(pool_size=(2, 2),
                     strides=(2, 1),
                     padding='valid',
                     name='pool4')(m)
    m = Conv2D(512,
               kernel_size=(2, 2),
               activation='relu',
               padding='valid',
               name='conv7')(m)

    m = Permute((2, 1, 3), name='permute')(m)
    m = TimeDistributed(Flatten(), name='timedistrib')(m)

    m = Bidirectional(GRU(rnnunit, return_sequences=True), name='blstm1')(m)
    m = Dense(rnnunit, name='blstm1_out', activation='linear')(m)
    m = Bidirectional(GRU(rnnunit, return_sequences=True), name='blstm2')(m)
    y_pred = Dense(nclass, name='blstm2_out', activation='softmax')(m)

    basemodel = Model(inputs=input, outputs=y_pred)

    labels = Input(name='the_labels', shape=[
        None,
    ], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')
    loss_out = Lambda(ctc_lambda_func, output_shape=(1, ),
                      name='ctc')([y_pred, labels, input_length, label_length])
    model = Model(inputs=[input, labels, input_length, label_length],
                  outputs=[loss_out])
    sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
    #model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer='adadelta')
    model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)
    model.summary()
    return model, basemodel
Ejemplo n.º 7
0
def training(model_num, HY, num_epochs):
	#keras.backend.clear_session()
	#print(HY)
	#time.sleep(2)

	# reshape training data into 2d
	X_train_c = X_train.reshape(len(X_train), 28, 28, 1)
	X_test_c = X_test.reshape(len(X_test), 28, 28, 1)

	model = Sequential()

	no_conv_layers = True
	no_dense_layers = True
	# ADD CONVOLUTION LAYERS
	for i,c_params in enumerate(HY["conv_layers"]):
		no_conv_layers = False

		num_filters, kernel_size, pooling_size = c_params

		# if it's the first layer, need to specify input shape
		if i==0: 
			model.add(Conv2D(num_filters, kernel_size, kernel_regularizer=keras.regularizers.l2(HY["k_reg"]), input_shape=(28,28,1)))
		else:
			model.add(Conv2D(num_filters, kernel_size, kernel_regularizer=keras.regularizers.l2(HY["k_reg"])))

		# add activation, pooling, dropout 
		model.add(Activation(HY["activation"]))
		if pooling_size:           
			model.add(MaxPooling2D(pool_size=pooling_size))
		if HY["dropout"]:
			model.add(Dropout(HY["dropout"]))

	if not no_conv_layers:
		model.add(Flatten())

	for i,dense_nodes in enumerate(HY["dense_layers"]):
		no_dense_layers = False

		if no_conv_layers and i==0:
			model.add(Dense(dense_nodes, kernel_regularizer=keras.regularizers.l2(HY["k_reg"]), input_dim=784))
		else:
			model.add(Dense(dense_nodes, kernel_regularizer=keras.regularizers.l2(HY["k_reg"])))

		# add activation and dropout 
		model.add(Activation(HY["activation"]))
		if HY["dropout"]:
			model.add(Dropout(HY["dropout"]))

	# once all the hidden nodes are added, add the output layer with a softmax activation
	if no_conv_layers and no_dense_layers:
		model.add(Dense(10, input_dim=784))
	else:
		model.add(Dense(10))
	model.add(Activation('softmax'))

	model.compile(optimizer=keras.optimizers.SGD(lr=HY["learning_rate"], clipnorm=HY["grad_clip_norm"]),  loss='categorical_crossentropy', metrics=['accuracy'])

	def report_ep_loss(ep,logs):
		leader.update(ep, model_num, float(logs['val_loss']))

	if no_conv_layers:
		model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=num_epochs, verbose=1, callbacks=[keras.callbacks.LambdaCallback(on_epoch_end=report_ep_loss)])
	else:
		model.fit(X_train_c, y_train, validation_data=(X_test_c, y_test), epochs=num_epochs, verbose=1, callbacks=[keras.callbacks.LambdaCallback(on_epoch_end=report_ep_loss)])
	leader.model_finished(model_num)
	keras.backend.clear_session()
Ejemplo n.º 8
0
def build_model_GSLRE(classSize, x=4):
    model = keras.Sequential()
    model.add(
        Conv2D(filters=96,
               kernel_size=(3, 3),
               padding='same',
               name='conv1',
               input_shape=(96, 96, 1)))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    D = channelD(96, 128, 48, 48, 3, x)
    model.add(
        Conv2D(filters=D, kernel_size=(3, 1), padding='same',
               name='conv2_de1'))
    model.add(
        Conv2D(filters=128,
               kernel_size=(1, 3),
               padding='same',
               name='conv2_de2'))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    D = channelD(128, 160, 24, 24, 3, x)
    model.add(
        Conv2D(filters=D, kernel_size=(3, 1), padding='same',
               name='conv3_de1'))
    model.add(
        Conv2D(filters=160,
               kernel_size=(1, 3),
               padding='same',
               name='conv3_de2'))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    D = channelD(160, 256, 12, 12, 3, x)
    model.add(
        Conv2D(filters=D,
               kernel_size=(3, 1),
               padding='same',
               name='conv4_1_de1'))
    model.add(
        Conv2D(filters=256,
               kernel_size=(1, 3),
               padding='same',
               name='conv4_1_de2'))
    model.add(BatchNormalization())
    model.add(PReLU())

    D = channelD(256, 256, 12, 12, 3, x)
    model.add(
        Conv2D(filters=D,
               kernel_size=(3, 1),
               padding='same',
               name='conv4_2_de1'))
    model.add(
        Conv2D(filters=256,
               kernel_size=(1, 3),
               padding='same',
               name='conv4_2_de2'))
    model.add(BatchNormalization())
    model.add(PReLU())

    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    D = channelD(256, 384, 6, 6, 3, x)
    model.add(
        Conv2D(filters=D,
               kernel_size=(3, 1),
               padding='same',
               name='conv5_1_de1'))
    model.add(
        Conv2D(filters=384,
               kernel_size=(1, 3),
               padding='same',
               name='conv5_1_de2'))
    model.add(BatchNormalization())
    model.add(PReLU())

    D = channelD(384, 384, 6, 6, 3, x)
    model.add(
        Conv2D(filters=D,
               kernel_size=(3, 1),
               padding='same',
               name='conv5_2_de1'))
    model.add(
        Conv2D(filters=384,
               kernel_size=(1, 3),
               padding='same',
               name='conv5_2_de2'))
    model.add(BatchNormalization())
    model.add(PReLU())

    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    model.add(Flatten())
    model.add(Dense(1024, name='fc1'))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(Dropout(0.5))

    model.add(Dense(classSize, activation='softmax', name='fc2'))

    return model
Ejemplo n.º 9
0
        *LSTM - Recurrent Layer
        *GRU - Recurrent Integration version Layer
        *MaxPooling2D - Max pooling to a convolutional neural network in code
        *Flatten - used to reshape the tensor to such a shape which is equal to the number of elements present in the tensor

    """

    # Training model from one input (CT scan picture)
    model_CT_lungs = Sequential()
    model_CT_lungs.add(
        Conv2D(32,
               kernel_size=(3, 3),
               activation='relu',
               input_shape=(224, 224, 3)))
    model_CT_lungs.add(Conv2D(128, (3, 3), activation='relu'))
    model_CT_lungs.add(MaxPooling2D(pool_size=(2, 2)))
    model_CT_lungs.add(Dropout(0.25))

    model_CT_lungs.add(Conv2D(64, (3, 3), activation='relu'))
    model_CT_lungs.add(MaxPooling2D(pool_size=(2, 2)))
    model_CT_lungs.add(Dropout(0.25))

    model_CT_lungs.add(Conv2D(128, (3, 3), activation='relu'))
    model_CT_lungs.add(MaxPooling2D(pool_size=(2, 2)))
    model_CT_lungs.add(Dropout(0.25))

    model_CT_lungs.add(Flatten())
    model_CT_lungs.add(Dense(64, activation='relu'))
    model_CT_lungs.add(Dropout(0.5))
    model_CT_lungs.add(Dense(1, activation='sigmoid'))
    """
                strides=(1, 1),
                padding='valid',
                data_format='channels_first',
                kernel_regularizer=keras.regularizers.l2(reg3))(x3)
    x3 = BatchNormalization(axis=1)(x3)
    x3 = Activation('relu')(x3)
    x3 = Conv2D(filters=128,
                kernel_size=3,
                strides=(1, 1),
                padding='valid',
                data_format='channels_first',
                kernel_regularizer=keras.regularizers.l2(reg3))(x3)
    x3 = BatchNormalization(axis=1)(x3)
    x3 = Activation('relu')(x3)
    x3 = MaxPooling2D(pool_size=(2, 2),
                      strides=None,
                      padding='valid',
                      data_format='channels_first')(x3)
    x3 = Flatten()(x3)
    x3 = Dropout(rate=.5, seed=seed)(x3)

    reg5 = reg3
    x5 = Conv2D(filters=6,
                kernel_size=5,
                strides=(1, 1),
                padding='valid',
                data_format='channels_first',
                kernel_regularizer=keras.regularizers.l2(reg5))(main_in)
    x5 = BatchNormalization(axis=1)(x5)
    x5 = Activation('relu')(x5)
    x5 = Conv2D(filters=128,
                kernel_size=5,
Ejemplo n.º 11
0
def build_model(classSize, x=4):
    model = keras.Sequential()
    model.add(
        Conv2D(filters=96,
               kernel_size=(3, 3),
               padding='same',
               name='conv1',
               input_shape=(96, 96, 1)))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    model.add(
        Conv2D(filters=128, kernel_size=(3, 3), padding='same', name='conv2'))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    model.add(
        Conv2D(filters=160, kernel_size=(3, 3), padding='same', name='conv3'))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    model.add(
        Conv2D(filters=256, kernel_size=(3, 3), padding='same',
               name='conv4_1'))
    model.add(BatchNormalization())
    model.add(PReLU())

    model.add(
        Conv2D(filters=256, kernel_size=(3, 3), padding='same',
               name='conv4_2'))
    model.add(BatchNormalization())
    model.add(PReLU())

    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    model.add(
        Conv2D(filters=384, kernel_size=(3, 3), padding='same',
               name='conv5_1'))
    model.add(BatchNormalization())
    model.add(PReLU())

    model.add(
        Conv2D(filters=384, kernel_size=(3, 3), padding='same',
               name='conv5_2'))
    model.add(BatchNormalization())
    model.add(PReLU())

    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

    model.add(Flatten())
    model.add(Dense(1024, name='fc1'))
    model.add(BatchNormalization())
    model.add(PReLU())
    model.add(Dropout(0.5))

    model.add(Dense(classSize, activation='softmax', name='fc2'))

    return model
Ejemplo n.º 12
0
train_data, test_data = train_data/255, test_data/255


train_label = keras.utils.to_categorical(train_label, 2)
test_label = keras.utils.to_categorical(test_label, 2)

# AlexNet
model = Sequential()
#第一段
model.add(Conv2D(filters=96, kernel_size=(11,11),
                 strides=(4,4), padding='valid',
                 input_shape=(resize,resize,3),
                 activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3,3),
                       strides=(2,2),
                       padding='valid'))
#第二段
model.add(Conv2D(filters=256, kernel_size=(5,5),
                 strides=(1,1), padding='same',
                 activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3,3),
                       strides=(2,2),
                       padding='valid'))
#第三段
model.add(Conv2D(filters=384, kernel_size=(3,3),
                 strides=(1,1), padding='same',
                 activation='relu'))
model.add(Conv2D(filters=384, kernel_size=(3,3),
                 strides=(1,1), padding='same',
def build_model(SHAPE, nb_classes, bn_axis, seed=None):
    # We can't use ResNet50 directly, as it might cause a negative dimension
    # error.
    if seed:
        np.random.seed(seed)

    input_layer = Input(shape=SHAPE)

    # block 1
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv1')(input_layer)
    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)

    x = Flatten(name='flatten')(x)
    x = Dense(4096, activation='relu', name='fc1')(x)
    x = Dense(4096, activation='relu', name='fc2')(x)
    x = Dense(nb_classes, activation='softmax', name='predictions')(x)

    model = Model(input_layer, x)

    return model
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)
	
datagen.fit(x_train)
validgen.fit(x_test)

#Define the NN architecture
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten
#Two hidden layers
nn = Sequential()
nn.add(Conv2D(32, 3, 3, activation='relu', input_shape=input_shape))
nn.add(Conv2D(32, 3, 3, activation='relu'))
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Conv2D(64, 3, 3, activation='relu'))
nn.add(Conv2D(64, 3, 3, activation='relu'))
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Conv2D(128, 3, 3, activation='relu'))
nn.add(Flatten())
nn.add(Dense(256, activation='relu'))
nn.add(Dense(10, activation='softmax'))

#Model visualization
#We can plot the model by using the ```plot_model``` function. We need to install *pydot, graphviz and pydot-ng*.
#from keras.util import plot_model
#plot_model(nn, to_file='nn.png', show_shapes=true)

#Compile the NN
nn.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])
Ejemplo n.º 15
0
def ResNet50(include_top=True, weights='imagenet',
             input_tensor=None, input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the ResNet50 architecture.
    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format="channels_last"` in your Keras config
    at ~/.keras/keras.json.
    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.
    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 244)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    # Returns
        A Keras model instance.
    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=197,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top)

    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_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    x = ZeroPadding2D((3, 3))(img_input)
    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x = AveragePooling2D((7, 7), name='avg_pool')(x)

    if include_top:
        x = Flatten()(x)
        x = Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='resnet50')

    # load weights
    if weights == 'imagenet':
        PATH = os.getcwd()
        if include_top:
            weights_path = os.path.join(PATH, 'resnet50_weights_tf_dim_ordering_tf_kernels.h5')
            ''' get_file('resnet50_weights_tf_dim_ordering_tf_kernels.h5',
                                    WEIGHTS_PATH,
                                    cache_subdir='models',
                                    md5_hash='a7b3fe01876f51b976af0dea6bc144eb') '''
        else:
            weights_path = os.path.join(PATH, 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
            ''' get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                                    WEIGHTS_PATH_NO_TOP,
                                    cache_subdir='models',
                                    md5_hash='a268eb855778b3df3c7506639542a6af') '''
        model.load_weights(weights_path)
        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)

        if K.image_data_format() == 'channels_first':
            if include_top:
                maxpool = model.get_layer(name='avg_pool')
                shape = maxpool.output_shape[1:]
                dense = model.get_layer(name='fc1000')
                layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')

            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
    return model
Ejemplo n.º 16
0
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print('X_train shape:', X_crop_train.shape)
print(X_crop_train.shape[0], 'train samples')

nb_filters = 32
kernel_size = (3, 3)
pool_size = (2, 2)

input_img = Input(shape=(img_rows, img_cols, 1))
encoded = Convolution2D(nb_filters,
                        kernel_size[0],
                        kernel_size[1],
                        border_mode="same",
                        activation='relu')(input_img)
encoded = MaxPooling2D(pool_size=(2, 2))(encoded)
encoded = Flatten()(encoded)
encoded = Dense(1024, activation='sigmoid')(encoded)

autoencoder = Model(input_img, encoded)
autoencoder.compile(optimizer='adadelta', loss='poisson')
# at this point the representation is (32*32, 1)

# autoencoder.load_weights("cnn_autoencoder_weights.h5")
if __name__ == "__main__":
    autoencoder.fit(X_train,
                    X_crop_train,
                    batch_size=batch_size,
                    nb_epoch=nb_epoch,
                    verbose=1,
                    callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
def model(train_gen, test_gen):
    '''
    Model providing function:

    Create Keras model with double curly brackets dropped-in as needed.
    Return value has to be a valid python dictionary with two customary keys:
        - loss: Specify a numeric evaluation metric to be minimized
        - status: Just use STATUS_OK and see hyperopt documentation if not feasible
    The last one is optional, though recommended, namely:
        - model: specify the model just created so that we can later use it again.
    '''

    #numpy.random.seed(8994)
    nb_classes = 11
    sequence_length = 5
    img_rows, img_cols, img_depth = 64, 64, 3
    input_shape = (img_rows, img_cols, img_depth)

    nb_epoch = 12
    nb_filters = {{choice([32, 64, 128])}}
    pool_size = (2, 2)
    kernel_size = (3, 3)
    cls_weights = [1., 1., 1., 1., 1., 1., 2*{{uniform(0, 1)}}]
    #optimizer = {{choice(['rmsprop', 'adam', 'sgd', 'adadelta'])}} # TODO: leads to inf loss
    optimizer = 'adadelta'

    main_input = Input(shape=input_shape, dtype='float32', name='main_input')
    shared = Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
                           border_mode='valid', input_shape=input_shape)(main_input)
    shared = Activation('relu')(shared)
    shared = Convolution2D(nb_filters, kernel_size[0], kernel_size[1])(shared)
    shared = Activation('relu')(shared)
    shared = MaxPooling2D(pool_size=pool_size)(shared)

    # Conditional extra convolutional layer
    if conditional({{choice(['two', 'three'])}}) == 'three':
        shared = Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
                               border_mode='valid', input_shape=input_shape)(main_input)
        shared = Activation('relu')(shared)
        shared = MaxPooling2D(pool_size=pool_size)(shared)

    dropout_coef = {{uniform(0, 1)}}
    shared = Dropout(dropout_coef)(shared)
    shared = Flatten()(shared)
    shared = Dense(128)(shared)
    shared = Activation('relu')(shared)
    shared = Dropout(dropout_coef)(shared)

    length_cls = Dense((sequence_length + 1))(shared)  # to account for sequence length + 1
    length_cls = Activation('softmax', name="length_cls")(length_cls)

    # 2. First digit classifier
    first_cls = Dense(nb_classes)(shared)
    first_cls = Activation('softmax', name="first_cls")(first_cls)

    # 3. Second digit classifier
    second_cls = Dense(nb_classes)(shared)
    second_cls = Activation('softmax', name="second_cls")(second_cls)

    # 4. Third digit classifier
    third_cls = Dense(nb_classes)(shared)
    third_cls = Activation('softmax', name="third_cls")(third_cls)

    # 5. Forth digit classifier
    forth_cls = Dense(nb_classes)(shared)
    forth_cls = Activation('softmax', name="forth_cls")(forth_cls)

    # 6. Fifth digit classifier
    fifth_cls = Dense(nb_classes)(shared)
    fifth_cls = Activation('softmax', name="fifth_cls")(fifth_cls)

    #7. Digit boxes coordinates regresssion
    coord_regr = Dense(20, name="coord_regr")(shared)

    # model compilation and training
    model = Model(input=[main_input], output=[length_cls, first_cls, second_cls, third_cls, forth_cls, fifth_cls, coord_regr])

    model.compile(loss={'length_cls': 'categorical_crossentropy', 'first_cls': 'categorical_crossentropy',
                        'second_cls': 'categorical_crossentropy', 'third_cls': 'categorical_crossentropy',
                        'forth_cls': 'categorical_crossentropy', 'fifth_cls': 'categorical_crossentropy',
                        'coord_regr': 'mean_squared_error'},
                  optimizer=optimizer,
                  metrics={'length_cls': 'accuracy', 'first_cls': 'accuracy',
                           'second_cls': 'accuracy', 'third_cls': 'accuracy',
                           'forth_cls': 'accuracy', 'fifth_cls': 'accuracy',
                           'coord_regr': iou_metric_func}, loss_weights=cls_weights)

    directory = "output/" + str(datetime.datetime.now())
    if not os.path.exists(directory):
        os.makedirs(directory)

    checkpointer = ModelCheckpoint(filepath=directory + "/weights.hdf5", verbose=1, save_best_only=True)
    tensorboard = TensorBoard(log_dir=directory, histogram_freq=0, write_graph=True, write_images=False)
    # Todo: P2 add EarlyStopping callback
    # Todo: P3 add LearningRateSchedule callback
    # TODO: P2 batch size are not randomized
    model.fit_generator(train_gen, samples_per_epoch=33401, nb_epoch=nb_epoch, verbose=1, validation_data=test_gen,
                        nb_val_samples=13068, callbacks=[checkpointer, tensorboard])


    score = model.evaluate_generator(test_gen, val_samples=13068)

    print('Test accuracy:', score)
    return {'loss': score[0], 'status': STATUS_OK, 'model': model}
Ejemplo n.º 18
0
from keras.layers import Conv2D, Input, MaxPooling2D, Flatten

x = Input(shape=(256, 256, 3))
y = Conv2D(3, (3, 3), padding='same')(x)
import keras
z = keras.layers.add([x, y])

# 视觉问答model
main_input = Input(
    None,
    None,
)

model = Conv2D(64, (3, 3), activation='relu', padding='same')(main_input)

model = Conv2D(64, (3, 3), activation='relu')(model)

model = MaxPooling2D((2, 2))(model)

model = Conv2D(256, (3, 3), activation='relu', padding='same')(model)

model = Conv2D(256, (3, 3), activation='relu')(model)
model = MaxPooling2D((2, 2))(model)
model = Flatten(model)
input_img = None
encode_img = model(input_img)

ques_input = None
input_ques = None
encode_ques = model(input_ques)
Ejemplo n.º 19
0
y_test = keras.utils.to_categorical(y_test, num_classes)

#Below is where we define model architecture!
model = Sequential(
)  # The layers are stacked sequentially.  Ask me about the functional API if you want!
model.add(
    Conv2D(
        32,
        kernel_size=(
            3, 3),  # We'll have 32 3x3 filters with RANDOM weights to start
        activation=
        'relu',  # This is our non-linear activation function so we can take advantage of multiple layers
        input_shape=input_shape)
)  # Keras handles this automatically for other layers
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(
    2, 2)))  # A non-linear filter that takes the max of each 2x2 block.
model.add(
    Dropout(0.25)
)  # Helps prevent overfitting by randomly excluding 25% of neurons during an epoch
model.add(Flatten())  # a 3x3 matrix will beoome a 1x9 vector.
model.add(
    Dense(128, activation='relu')
)  # Now we look for all possible combinations of features and how they fit together
model.add(Dropout(0.5))
model.add(
    Dense(num_classes, activation='softmax')
)  # We have one last dense connected layer with each of our outputs! Softmax acts as a probability.

#We compile the model now so we can train it.
model.compile(
    loss=keras.losses.
Ejemplo n.º 20
0
show_image(train_images[1])

import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
model = Sequential()


model = Sequential([
    Conv2D(filters = 8, kernel_size=(3, 3), activation='relu',padding='same',input_shape=(28, 28, 1)),
    Dropout(0.5),
    Conv2D(filters = 8, kernel_size=(3, 3), activation='relu'),
    Dropout(0.5),
    
    MaxPooling2D(pool_size=(2, 2),strides=4),
    
    Flatten(),
    
    Dense(256, activation='relu'),
    Dropout(0.05),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dropout(0.05),
    Dense(32, activation='relu'),
    Dense(10, activation='softmax')
  
  
])

#literally compiles the model, u have to run after any changes in the model
def fcn_8(input_shape=(para.img_cols, para.img_rows, para.channels),
          classes=para.num_classes,
          input_tensor=None):
    #img_input = Input(shape=input_shape)

    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=56,
                                      data_format=K.image_data_format(),
                                      include_top=False)

    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
    #print(img_input.shape)
    x = img_input
    # Encoder
    x = Conv2D(64, (3, 3), padding="same", name="block1_conv0")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(64, (3, 3), padding="same", name="block1_conv1")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    y1 = x

    x = Conv2D(128, (3, 3), padding="same", name="block2_conv0")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(128, (3, 3), padding="same", name="block2_conv1")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    y2 = x

    x = Conv2D(256, (3, 3), padding="same", name="block3_conv0")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(256, (3, 3), padding="same", name="block3_conv1")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(256, (3, 3), padding="same", name="block3_conv2")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    y3 = x

    x = Conv2D(512, (3, 3), padding="same", name="block4_conv0")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(512, (3, 3), padding="same", name="block4_conv1")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(512, (3, 3), padding="same", name="block4_conv2")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    y4 = x

    x = Conv2D(512, (3, 3), padding="same", name="block5_conv0")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(512, (3, 3), padding="same", name="block5_conv1")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Conv2D(512, (3, 3), padding="same", name="block5_conv2")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    y5 = x

    #   =============
    #   The Decoder
    #   =============
    #   Block 6
    o = y5
    o = (Conv2D(classes, (1, 1), padding='same', name='block6_conv1'))(o)

    #   Block 7
    o2 = y4
    o2 = (Conv2D(classes, (1, 1), padding='same', name='block7_conv1'))(o2)
    o, o2 = crop(o, o2, img_input)
    o = Add()([o, o2])
    o = UpSampling2D(size=(2, 2))(o)

    #   Block 8
    o2 = y3
    o2 = (Conv2D(classes, (1, 1), padding='same', name='block8_conv1'))(o2)
    o2, o = crop(o2, o, img_input)
    o = Add()([o2, o])
    o = UpSampling2D(size=(2, 2))(o)

    #   Block 9
    o2 = y2
    o2 = (Conv2D(classes, (1, 1), padding='same', name='block9_conv1'))(o2)
    o2, o = crop(o2, o, img_input)
    o = Add()([o2, o])
    o = UpSampling2D(size=(2, 2))(o)

    #   Block 10
    o2 = y1
    o2 = (Conv2D(classes, (1, 1), padding='same', name='block10_conv1'))(o2)
    o2, o = crop(o2, o, img_input)
    o = Add()([o2, o])
    o = UpSampling2D(size=(2, 2))(o)

    model = Model(img_input, o)
    cols = model.output_shape[1]
    rows = model.output_shape[2]

    o = Conv2D(classes, (1, 1), padding="valid")(o)
    o = Reshape((cols * rows, classes))(o)  #  *****************
    o = Activation("softmax")(o)

    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    model = Model(inputs, o)
    return model, rows, cols
Ejemplo n.º 22
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
Ejemplo n.º 23
0
if __name__ == "__main__":
    # Convolutional Neural Network

    # Part 1 - Data Pre-processing
    # for our dogs/cats dataset, this work has already manually been done.

    # Part 2 - Building the CNN

    # Initialize our CNN
    classifier = Sequential()

    # Step 1 - Convolution
    classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu'))

    # Step 2 - Max Pooling
    classifier.add(MaxPooling2D(pool_size = (2, 2)))

    # Add second convolutional layer to improve accuracy
    classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
    classifier.add(MaxPooling2D(pool_size = (2, 2)))

    # Step 3 - Flattening
    classifier.add(Flatten())

    # Step 4 - Full connection
    classifier.add(Dense(units = 128, activation = 'relu'))
    classifier.add(Dense(units = 1, activation = 'sigmoid'))

    # Step 5 - Compiling the CNN
    classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
np.set_printoptions(threshold=np.inf) #Print complete arrays
batch_size = 64
epochs = 50
learning_rate = 0.0001
first_layer_filters = 32
second_layer_filters = 64
ks = 2
mp = 2
dense_layer_size = 128

#model
model = Sequential()
model.add(Conv2D(first_layer_filters, kernel_size=(ks, ks),
                 activation='relu',
                 input_shape= (7, 7, 1)))
model.add(MaxPooling2D(pool_size=(mp, mp)))
model.add(Conv2D(second_layer_filters, (ks, ks), activation='relu'))
model.add(MaxPooling2D(pool_size=(mp, mp)))
model.add(Flatten())
model.add(Dense(dense_layer_size, activation='relu'))
model.add(Dense(2, activation='softmax'))

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

#Train network
history = model.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          validation_data=(X_test, y_test),
Ejemplo n.º 25
0
def Conv_Block(inpt,nb_filter,kernel_size,strides=(1,1), with_conv_shortcut=False):  
    x = Conv2d_BN(inpt,nb_filter=nb_filter,kernel_size=kernel_size,strides=strides,padding='same')  
    x = Conv2d_BN(x, nb_filter=nb_filter, kernel_size=kernel_size,padding='same')  
    if with_conv_shortcut:  
        shortcut = Conv2d_BN(inpt,nb_filter=nb_filter,strides=strides,kernel_size=kernel_size)  
        x = add([x,shortcut])  
        return x  
    else:  
        x = add([x,inpt])  
        return x  
  
inpt = Input(shape=(height,width,num_channels))  
x = ZeroPadding2D((0,0))(inpt)  
x = Conv2d_BN(x,nb_filter=32,kernel_size=(7,7),strides=(1,1),padding='same')  
x = BatchNormalization()(x)
x = MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')(x)  
# #(56,56,64)
x = Conv_Block(x,nb_filter=32,kernel_size=(5,5),strides=(1,1),with_conv_shortcut=False)    
# x = Conv_Block(x,nb_filter=64,kernel_size=(5,5),strides=(2,2),with_conv_shortcut=True)  
# x = Conv_Block(x,nb_filter=64,kernel_size=(5,5),strides=(2,2),with_conv_shortcut=True)
x = MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')(x)  
# x = Conv2d_BN(x,nb_filter=64,kernel_size=(5,5),strides=(1,1),padding='same')  
x = Conv_Block(x,nb_filter=64,kernel_size=(3,3),strides=(1,1),with_conv_shortcut=True)  
x = MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')(x)  

# x = Conv_Block(x,nb_filter=64,kernel_size=(3,3),strides=(1,1),with_conv_shortcut=False)  
# x = MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')(x)  
x = Conv_Block(x,nb_filter=64,kernel_size=(3,3),strides=(1,1),with_conv_shortcut=False)  
x = MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')(x)  
x = Conv_Block(x,nb_filter=128,kernel_size=(3,3),strides=(1,1),with_conv_shortcut=True)  
x = MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')(x)  
Ejemplo n.º 26
0
def _main(args):
    config_path = os.path.expanduser(args.config_path)
    weights_path = os.path.expanduser(args.weights_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')
    weights_header = np.ndarray(
        shape=(5, ), dtype='int32', buffer=weights_file.read(20))
    print('Weights Header: ', weights_header)
    # TODO: Check transpose flag when implementing fully connected layers.
    # transpose = (weight_header[0] > 1000) or (weight_header[1] > 1000)

    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.')
    if args.fully_convolutional:
        image_height, image_width = None, None
    else:
        image_height = int(cfg_parser['net_0']['height'])
        image_width = int(cfg_parser['net_0']['width'])

    prev_layer = Input(shape=(image_height, image_width, 3))
    all_layers = [prev_layer]
    outputs = []

    weight_decay = float(cfg_parser['net_0']['decay']
                         ) if 'net_0' in cfg_parser.sections() else 5e-4
    count = 0

    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]

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

            # TODO: This assumes channel last dim_ordering.
            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

                # TODO: Keras BatchNormalization mistakenly refers to var
                # as std.
                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)
            # TODO: Add check for Theano dim ordering.
            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))

            padding = 'same' if pad == 1 and stride == 1 else 'valid'
            # Adjust padding model for darknet.
            if stride == 2:
                prev_layer = ZeroPadding2D(((1, 0), (1, 0)))(prev_layer)

            # Create Conv2D 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(prev_layer)

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

        elif section.startswith('avgpool'):
            if cfg_parser.items(section) != []:
                raise ValueError('{} with params unsupported.'.format(section))
            all_layers.append(GlobalAveragePooling2D()(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('route'):
            ids = [int(i) for i in cfg_parser[section]['layers'].split(',')]
            if len(ids) == 2:
                for i, item in enumerate(ids):
                    if item != -1:
                        ids[i] = item + 1

            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('shortcut'):
            ids = [int(i) for i in cfg_parser[section]['from'].split(',')][0]
            activation = cfg_parser[section]['activation']
            shortcut = add([all_layers[ids], prev_layer])
            if activation == 'linear':
                shortcut = Activation('linear')(shortcut)
            all_layers.append(shortcut)
            prev_layer = all_layers[-1]

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

        elif section.startswith('yolo'):
            classes = int(cfg_parser[section]['classes'])
            # num = int(cfg_parser[section]['num'])
            # mask = int(cfg_parser[section]['mask'])
            n1, n2 = int(prev_layer.shape[1]), int(prev_layer.shape[2])
            n3 = 3
            n4 = (4 + 1 + classes)
            yolo = Reshape((n1, n2, n3, n4))(prev_layer)
            all_layers.append(yolo)
            prev_layer = all_layers[-1]
            outputs.append(len(all_layers) - 1)

        elif (section.startswith('net')):
            pass  # Configs not currently handled during model definition.
        else:
            raise ValueError(
                'Unsupported section header type: {}'.format(section))

    # Create and save model.
    model = Model(inputs=all_layers[0],
                  outputs=[all_layers[i] for i in outputs])
    print(model.summary())
    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))

    plot(model, to_file='{}.png'.format(output_root), show_shapes=True)
    print('Saved model plot to {}.png'.format(output_root))
Ejemplo n.º 27
0
def _reduction_A(ip, p, filters, weight_decay=5e-5, id=None):
    '''Adds a Reduction cell for NASNet-A (Fig. 4 in the paper)

    # Arguments:
        ip: input tensor `x`
        p: input tensor `p`
        filters: number of output filters
        weight_decay: l2 regularization weight
        id: string id

    # Returns:
        a Keras tensor
    '''
    """"""
    channel_dim = 1 if K.image_data_format() == 'channels_first' else -1

    with K.name_scope('reduction_A_block_%s' % id):
        p = _adjust_block(p, ip, filters, weight_decay, id)

        h = Activation('relu')(ip)
        h = Conv2D(filters, (1, 1),
                   strides=(1, 1),
                   padding='same',
                   name='reduction_conv_1_%s' % id,
                   use_bias=False,
                   kernel_initializer='he_normal',
                   kernel_regularizer=l2(weight_decay))(h)
        h = BatchNormalization(axis=channel_dim,
                               momentum=_BN_DECAY,
                               epsilon=_BN_EPSILON,
                               name='reduction_bn_1_%s' % id)(h)

        with K.name_scope('block_1'):
            x1_1 = _separable_conv_block(h,
                                         filters, (5, 5),
                                         strides=(2, 2),
                                         weight_decay=weight_decay,
                                         id='reduction_left1_%s' % id)
            x1_2 = _separable_conv_block(p,
                                         filters, (7, 7),
                                         strides=(2, 2),
                                         weight_decay=weight_decay,
                                         id='reduction_1_%s' % id)
            x1 = add([x1_1, x1_2], name='reduction_add_1_%s' % id)

        with K.name_scope('block_2'):
            x2_1 = MaxPooling2D((3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='reduction_left2_%s' % id)(h)
            x2_2 = _separable_conv_block(p,
                                         filters, (7, 7),
                                         strides=(2, 2),
                                         weight_decay=weight_decay,
                                         id='reduction_right2_%s' % id)
            x2 = add([x2_1, x2_2], name='reduction_add_2_%s' % id)

        with K.name_scope('block_3'):
            x3_1 = AveragePooling2D((3, 3),
                                    strides=(2, 2),
                                    padding='same',
                                    name='reduction_left3_%s' % id)(h)
            x3_2 = _separable_conv_block(p,
                                         filters, (5, 5),
                                         strides=(2, 2),
                                         weight_decay=weight_decay,
                                         id='reduction_right3_%s' % id)
            x3 = add([x3_1, x3_2], name='reduction_add3_%s' % id)

        with K.name_scope('block_4'):
            x4 = AveragePooling2D((3, 3),
                                  strides=(1, 1),
                                  padding='same',
                                  name='reduction_left4_%s' % id)(x1)
            x4 = add([x2, x4])

        with K.name_scope('block_5'):
            x5_1 = _separable_conv_block(x1,
                                         filters, (3, 3),
                                         weight_decay=weight_decay,
                                         id='reduction_left4_%s' % id)
            x5_2 = MaxPooling2D((3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='reduction_right5_%s' % id)(h)
            x5 = add([x5_1, x5_2], name='reduction_add4_%s' % id)

        x = concatenate([x2, x3, x4, x5],
                        axis=channel_dim,
                        name='reduction_concat_%s' % id)
        return x, ip
    plt.show()
(X_train, _), (X_test, _) = mnist.load_data()
X_train = X_train.astype('float32') / 255.
X_test = X_test.astype('float32') / 255.
X_train = X_train[..., np.newaxis]
X_test = X_test[..., np.newaxis]

input_size = 784
epochs = 5
batch_size = 256

input_img = Input(shape=(28, 28, 1)) 
x = Conv2D(8, (3, 3), padding='same')(input_img)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (7, 7, 16)
x = Conv2DTranspose(16, (3, 3), padding='same')(encoded)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2DTranspose(8, (3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2DTranspose(1, (3, 3), activation='sigmoid', padding='same')(x)
Ejemplo n.º 29
0
def VGG16(include_top=True, weights='vggface',
          input_tensor=None, input_shape=None,
          pooling=None,
          classes=2622):
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=48,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top)

    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

    # Block 1
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')(
        img_input)
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)

    # Block 2
    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')(
        x)
    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')(
        x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(x)

    # Block 3
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')(
        x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')(
        x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')(
        x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(x)

    # Block 4
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(
        x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(
        x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(
        x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x)

    # Block 5
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')(
        x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')(
        x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')(
        x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool5')(x)

    if include_top:
        # Classification block
        x = Flatten(name='flatten')(x)
        x = Dense(4096, name='fc6')(x)
        x = Activation('relu', name='fc6/relu')(x)
        x = Dense(4096, name='fc7')(x)
        x = Activation('relu', name='fc7/relu')(x)
        x = Dense(classes, name='fc8')(x)
        x = Activation('softmax', name='fc8/softmax')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

            # Ensure that the model takes into account
            # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
        # Create model.
    model = Model(inputs, x, name='vggface_vgg16')  # load weights
    if weights == 'vggface':
        if include_top:
            weights_path = get_file('rcmalli_vggface_tf_vgg16.h5',
                                    utils.
                                    VGG16_WEIGHTS_PATH,
                                    cache_subdir=utils.VGGFACE_DIR)
        else:
            weights_path = get_file('rcmalli_vggface_tf_notop_vgg16.h5',
                                    utils.VGG16_WEIGHTS_PATH_NO_TOP,
                                    cache_subdir=utils.VGGFACE_DIR)
        model.load_weights(weights_path, by_name=True)
        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)

        if K.image_data_format() == 'channels_first':
            if include_top:
                maxpool = model.get_layer(name='pool5')
                shape = maxpool.output_shape[1:]
                dense = model.get_layer(name='fc6')
                layer_utils.convert_dense_weights_data_format(dense, shape,
                                                              'channels_first')

            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
    return model
# 借助全连接层实现CNN分类

model = Sequential()

# model.add(BatchNormalization(axis=-1))
model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=(28, 28, 1))) # 输入28*28,1代表灰度
# 需训练的参数:3*3*1*32 + 32 = 320                        # 1代表输入深度
model.add(Activation('relu'))                           # relu激活
model.add(BatchNormalization(axis=-1))                        # 批量标准化

model.add(Conv2D(filters=32, kernel_size=(3, 3)))       # 因采用3*3卷积核,正常卷积,输出dim(26-2)*(26-2)*32
# 需训练的参数:3*3*32* 32 + 32 = 9248                     # 32代表偏置

model.add(Activation('relu'))                           # relu激活

model.add(MaxPooling2D(pool_size=(2, 2)))               # max poling池化,di = 12*12*32
model.add(BatchNormalization(axis=-1))                         # 归一化

model.add(Conv2D(filters=64, kernel_size=(3, 3)))
# 需训练的参数:3*3*32 * 64 + 64 = 18496

model.add(Activation('relu'))
BatchNormalization(axis=1)

model.add(Conv2D(filters=64, kernel_size=(3, 3)))
# 需训练的参数:3*3*64 * 64 + 64 = 36928
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
# Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小。