import numpy as np import matplotlib.pyplot as plt import pandas as pd import keras as k from keras.models import Sequential from keras.layers import Convolution2D, MaxPooling2D, Flatten, Dense Mnist = k.datasets.mnist (X_train, Y_train), (X_test, Y_test) = Mnist.load_data(path='Mnist.npz') X_train, X_test = X_train / 255.0, X_test / 255.0 img_rows, img_cols = 28, 28 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) #num_category=10 #Y_train = k.utils.to_categorical(Y_train, num_category) #Y_test = k.utils.to_categorical(Y_test, num_category) model = Sequential() model.add(Convolution2D(32, 3, 3, input_shape=(input_shape), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(output_dim=100, activation='relu')) model.add(Dense(output_dim=10, activation='softmax')) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) #model.summary() model.fit(X_train, Y_train, nb_epoch=10, validation_data=(X_test, Y_test))
def ExtractFeature(): # Determine proper input shape input_shape = None input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), require_flatten=True) img_input = Input(shape=input_shape) # Block 1 dcnn = Convolution2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')(img_input) dcnn = Convolution2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(dcnn) dcnn = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(dcnn) # Block 2 dcnn = Convolution2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')(dcnn) dcnn = Convolution2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')(dcnn) dcnn = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(dcnn) # Block 3 dcnn = Convolution2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')(dcnn) dcnn = Convolution2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')(dcnn) dcnn = Convolution2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')(dcnn) dcnn = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(dcnn) # Block 4 dcnn = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(dcnn) dcnn = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(dcnn) dcnn = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(dcnn) dcnn = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(dcnn) # Block 5 dcnn = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')(dcnn) dcnn = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')(dcnn) dcnn = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')(dcnn) dcnn = MaxPooling2D((2, 2), strides=(2, 2), name='pool5')(dcnn) # Classification block dcnn = Flatten(name='flatten')(dcnn) dcnn = Dense(4096, name='fc6')(dcnn) dcnn = Activation('relu', name='fc6/relu')(dcnn) dcnn = Dense(4096, name='fc7')(dcnn) dcnn = Activation('relu', name='fc7/relu')(dcnn) dcnn = Dense(2622, name='fc8')(dcnn) dcnn = Activation('softmax', name='fc8/softmax')(dcnn) inputs = img_input # Create model model = Model(inputs, dcnn) # load weights weights_path = get_file('vgg16_weights.h5', '~/.keras', cache_subdir='models') model.load_weights(weights_path, by_name=True) return model
Y_training_added_augmented = np_utils.to_categorical( Y_training_added_augmented, nclasses) Y_testing = np_utils.to_categorical(Y_testing, nclasses) #Data Augmentation Step datagen = ImageDataGenerator(rotation_range=90) # fit parameters from data datagen.fit(X_training_added_augmented) # 7. Define model architecture model = Sequential() model.add( Convolution2D(32, 2, 2, activation='relu', input_shape=(1, patch_size, patch_size), dim_ordering='th')) model.add(Convolution2D(32, 2, 2, activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(nclasses, activation='softmax')) # 8. Compile model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
def nn_base(input_tensor=None, trainable=False): # Determine proper input shape if K.image_dim_ordering() == 'th': input_shape = (1, None, None) else: input_shape = (None, None, 1) 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 x = ZeroPadding2D((3, 3))(img_input) x = Convolution2D(64, (7, 7), strides=(2, 2), name='conv1', trainable=trainable)(x) x = FixedBatchNormalization(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), trainable=trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', trainable=trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', trainable=trainable) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', trainable=trainable) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', trainable=trainable) return x
import numpy import math import sys from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Convolution2D, MaxPooling2D from keras.optimizers import SGD from keras.utils import np_utils # set dataset path dataset_path = '../cifar_10/' exec(open("read_dataset2img.py").read()) '''CNN model''' model = Sequential() model.add( Convolution2D(32, 3, 3, border_mode='same', input_shape=X_train[0].shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.2)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(classes)) model.add(Activation('softmax')) '''setting optimizer''' learning_rate = 0.01 learning_decay = 0.01 / 32 sgd = SGD(lr=learning_rate, decay=learning_decay, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy',
import numpy as np from keras.models import Sequential from keras.layers import Convolution2D, Activation, MaxPooling2D, Dense from keras_module.hwh_layers import SpatialPyramidPooling batch_size = 64 num_channels = 3 num_classes = 10 model = Sequential() # uses theano ordering. Note that we leave the image size as None to allow multiple image sizes model.add( Convolution2D(32, 3, 3, border_mode='same', input_shape=(None, None, 3))) model.add(Activation('relu')) model.add(Convolution2D(32, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(SpatialPyramidPooling([1, 2, 4])) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd') # train on 64x64x3 images # model.fit(np.random.rand(batch_size, 64, 64,num_channels), np.zeros((batch_size, num_classes)))
else: # Spilt validation set split = np.random.permutation(5000) xla = xla_total[split[0:5000 - nb_val]] yla = yla_total[split[0:5000 - nb_val]] xva = xla_total[split[5000 - nb_val:5000]] yva = yla_total[split[5000 - nb_val:5000]] xla = xla.astype('float32') / 255. xva = xva.astype('float32') / 255. model = Sequential() model.add( Convolution2D(startfilter, 3, 3, border_mode='same', dim_ordering='th', input_shape=(3, 32, 32))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Convolution2D(startfilter, 3, 3, dim_ordering='th')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D((2, 2), dim_ordering='th')) model.add(Dropout(0.25)) model.add( Convolution2D(startfilter * 2, 3, 3, border_mode='same',
def SqueezeNet(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, transfer_learning=False): """Instantiates the SqueezeNet architecture. transfer_learning (bool): if true, then the returned model will have a trainable top exactly like the main squeeznet model's top. But the bottom (!) of the model will not be trainable. """ 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 classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') input_shape = _obtain_input_shape(input_shape, default_size=227, 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 x = Convolution2D(64, (3, 3), strides=(2, 2), padding='valid', name='conv1')(img_input) x = Activation('relu', name='relu_conv1')(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x) x = fire_module(x, fire_id=2, squeeze=16, expand=64) x = fire_module(x, fire_id=3, squeeze=16, expand=64) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x) x = fire_module(x, fire_id=4, squeeze=32, expand=128) x = fire_module(x, fire_id=5, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x) x = fire_module(x, fire_id=6, squeeze=48, expand=192) x = fire_module(x, fire_id=7, squeeze=48, expand=192) x = fire_module(x, fire_id=8, squeeze=64, expand=256) x = fire_module(x, fire_id=9, squeeze=64, expand=256) if include_top: # It's not obvious where to cut the network... # Could do the 8th or 9th layer... some work recommends cutting earlier layers. x = Dropout(0.5, name='drop9')(x) x = Convolution2D(classes, (1, 1), padding='valid', name='conv10')(x) x = Activation('relu', name='relu_conv10')(x) x = GlobalAveragePooling2D()(x) x = Activation('softmax', name='loss')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) elif pooling == None: pass else: raise ValueError("Unknown argument for 'pooling'=" + pooling) # 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 model = Model(inputs, x, name='squeezenet') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'squeezenet_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'squeezenet_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') 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 K.backend() == 'tensorflow': print('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.') if transfer_learning: s_model = Sequential() s_model.add(model) s_model.add(Dropout(0.5)) s_model.add(Convolution2D(classes, (1, 1), padding='valid')) s_model.add(Activation('relu')) s_model.add(GlobalAveragePooling2D()) s_model.add(Activation('softmax')) s_model.layers[0].trainable = False return s_model else: return model
def train_MyNet(train_samples, validation_samples, train_generator, validation_generator): """ This function includes the architecture of the network as well as the training method :param train_samples: list of samples, each sample includes image paths and steering values, for training :param validation_samples: list of samples, each sample includes image paths and steering values, for validation :param train_generator: generator object to give training batches :param validation_generator: generator object to give validation batches :return: saves trained model and plots training and validation loss over number of epochs """ drop_rate = 0.05 model = Sequential() model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160, 320, 3))) model.add(Cropping2D(cropping=((50, 20), (0, 0)))) model.add(Convolution2D(12, 5, 5, activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D()) model.add(Dropout(rate=drop_rate)) model.add(Convolution2D(12, 5, 5, activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D()) model.add(Dropout(rate=drop_rate)) model.add(Convolution2D(12, 3, 3, activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D()) model.add(Dropout(rate=drop_rate)) model.add(Convolution2D(12, 3, 3, activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D()) model.add(Dropout(rate=drop_rate)) model.add(Flatten()) model.add(Dense(120)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(rate=drop_rate)) model.add(Dense(120)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(rate=drop_rate)) model.add(Dense(50)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(rate=drop_rate)) model.add(Dense(25)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(rate=drop_rate)) model.add(Dense(1)) # NO ACTIVATION FUNCTION BECAUSE REGRESSION NETWORK model.compile(loss='mse', optimizer='adam') #history_object = model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=5, verbose=1) history_object = model.fit_generator(train_generator, steps_per_epoch=m.ceil(len(train_samples)/batch_size), validation_data=validation_generator, validation_steps=m.ceil(len(validation_samples)/batch_size), epochs=12, verbose=1) model.save('model.h5') ### print the keys contained in the history object print(history_object.history.keys()) ### plot the training and validation loss for each epoch plt.plot(history_object.history['loss']) plt.title('model mean squared error loss') plt.ylabel('mean squared error loss') plt.xlabel('epoch') plt.legend(['training set'], loc='upper right') plt.show() plt.plot(history_object.history['val_loss']) plt.title('model mean squared error loss') plt.ylabel('mean squared error loss') plt.xlabel('epoch') plt.legend(['validation set'], loc='upper right') plt.show()
def VGG16(include_top=True, weights='imagenet', input_tensor=None): ''' # Arguments include_top: 是否包含最后的三层全连接层 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. ''' if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') # Determine proper input shape if K.image_dim_ordering() == 'th': input_shape = (3, 224, 224) else: input_shape = (224, 224, 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) else: img_input = input_tensor # Block 1 x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1')(img_input) x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv1')(x) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv1')(x) x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv2')(x) x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv1')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv2')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv1')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv2')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(1000, activation='softmax', name='predictions')(x) # Create model model = Model(img_input, x) # load weights if weights == 'imagenet': print('K.image_dim_ordering:', K.image_dim_ordering()) if K.image_dim_ordering() == 'th': if include_top: weights_path = get_file( 'vgg16_weights_th_dim_ordering_th_kernels.h5', TH_WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16_weights_th_dim_ordering_th_kernels_notop.h5', TH_WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) else: if include_top: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels.h5', TF_WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) return model
stride_cols = 20 resized_shape = (60, 60) input_shape = (3, 60, 60) file = open('mean_var.pkl', 'rb') mean_image, var_image = cPickle.load(file) file.close() file = open('mean_var_full.pkl', 'rb') mean_image_full, var_image_full = cPickle.load(file) file.close() model = Sequential() model.add(Convolution2D(32, 3, 3, input_shape=input_shape)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Convolution2D(32, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D((2, 2))) model.add(Convolution2D(64, 3, 3, input_shape=input_shape)) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D((2, 2))) model.add(Flatten()) model.add(Dense(4096))
# -1为sample的个数, 1为通道(黑白), 28*28像素 x_train = x_train.reshape(-1, 1, 28, 28) x_test = x_test.reshape(-1, 1, 28, 28) # One-hot encoding y_train = np_utils.to_categorical(y_train, num_classes=10) y_test = np_utils.to_categorical(y_test, num_classes=10) # Another way to build your cnn model = Sequential() # Conv layer 1 output shape(32, 28, 28) model.add( Convolution2D( filters=32, # 过滤器个数 kernel_size=(5, 5), # filter的大小 padding='same', # padding method input_shape=( 1, # channels 28, 28))) model.add(Activation('relu')) # Pooling layer 1 (max pooling) output shape(32, 14, 14) model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same')) # Conv layer 2 output shape(64, 14, 14) model.add(Convolution2D(filters=64, kernel_size=(5, 5), padding='same')) model.add(Activation('relu')) # Pooling layer2 (max pooling) output shape(64, 7, 7) model.add(MaxPool2D(pool_size=(2, 2), padding='same'))
#Part 1 - Building the CNN #Importing the Keras libraries and packages import keras from keras.models import Sequential from keras.layers import Convolution2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense #Initializing the CNN classifier = Sequential() #Step 1 - Convolution classifier.add( Convolution2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu')) #Step 2 - Pooling classifier.add(MaxPooling2D(pool_size=(2, 2))) #Step 3 - Flattening classifier.add(Flatten()) #Step 4 - Full connection classifier.add(Dense(output_dim=128, activation='relu')) classifier.add(Dense(output_dim=1, activation='sigmoid')) #Compiling the CNN classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.01) model.compile(optimizer=adam, loss="mse", metrics=['accuracy']) model.load_weights(fileWeights) print("Loaded model from disk:") model.summary() # re-create our model and restart training. else: pool_size = (2, 3) model = Sequential() model.add(MaxPooling2D(pool_size=pool_size, input_shape=input_shape)) model.add(Lambda(lambda x: x / 127.5 - 1.)) model.add(Convolution2D(5, 5, 24, subsample=(4, 4), border_mode="same")) model.add(ELU()) model.add(Convolution2D(5, 5, 36, subsample=(2, 2), border_mode="same")) model.add(ELU()) model.add(Convolution2D(5, 5, 48, subsample=(2, 2), border_mode="same")) model.add(ELU()) model.add(Convolution2D(3, 3, 64, subsample=(2, 2), border_mode="same")) model.add(ELU()) model.add(Convolution2D(3, 3, 64, subsample=(2, 2), border_mode="same")) model.add(Flatten()) model.add(Dropout(.2)) model.add(ELU()) model.add(Dense(1164)) model.add(Dropout(.2)) model.add(ELU()) model.add(Dense(100))
def DenseNet(input_shape=None, dense_blocks=3, dense_layers=-1, growth_rate=12, nb_classes=None, dropout_rate=None, bottleneck=False, compression=1.0, weight_decay=1e-4, depth=40): """ Creating a DenseNet Arguments: input_shape : shape of the input images. E.g. (28,28,1) for MNIST dense_blocks : amount of dense blocks that will be created (default: 3) dense_layers : number of layers in each dense block. You can also use a list for numbers of layers [2,4,3] or define only 2 to add 2 layers at all dense blocks. -1 means that dense_layers will be calculated by the given depth (default: -1) growth_rate : number of filters to add per dense block (default: 12) nb_classes : number of classes dropout_rate : defines the dropout rate that is accomplished after each conv layer (except the first one). In the paper the authors recommend a dropout of 0.2 (default: None) bottleneck : (True / False) if true it will be added in convolution block (default: False) compression : reduce the number of feature-maps at transition layer. In the paper the authors recomment a compression of 0.5 (default: 1.0 - will have no compression effect) weight_decay : weight decay of L2 regularization on weights (default: 1e-4) depth : number or layers (default: 40) Returns: Model : A Keras model instance """ if nb_classes == None: raise Exception( 'Please define number of classes (e.g. num_classes=10). This is required for final softmax.' ) if compression <= 0.0 or compression > 1.0: raise Exception( 'Compression have to be a value between 0.0 and 1.0. If you set compression to 1.0 it will be turn off.' ) if type(dense_layers) is list: if len(dense_layers) != dense_blocks: raise AssertionError( 'Number of dense blocks have to be same length to specified layers' ) elif dense_layers == -1: dense_layers = int((depth - 4) / 3) if bottleneck: dense_layers = dense_layers / 2 dense_layers = [dense_layers for _ in range(dense_blocks)] else: dense_layers = [dense_layers for _ in range(dense_blocks)] img_input = Input(shape=input_shape) nb_channels = growth_rate print('Creating DenseNet %s' % __version__) print('#############################################') print('Dense blocks: %s' % dense_blocks) print('Layers per dense block: %s' % dense_layers) print('#############################################') # Initial convolution layer x = Convolution2D(2 * growth_rate, (3, 3), padding='same', strides=(1, 1), use_bias=False, kernel_regularizer=l2(weight_decay))(img_input) # Building dense blocks for block in range(dense_blocks - 1): # Add dense block x, nb_channels = dense_block(x, dense_layers[block], nb_channels, growth_rate, dropout_rate, bottleneck, weight_decay) # Add transition_block x = transition_layer(x, nb_channels, dropout_rate, compression, weight_decay) nb_channels = int(nb_channels * compression) # Add last dense block without transition but for that with global average pooling x, nb_channels = dense_block(x, dense_layers[-1], nb_channels, growth_rate, dropout_rate, weight_decay) x = BatchNormalization()(x) x = Activation('relu')(x) x = GlobalAveragePooling2D()(x) x = Dense(nb_classes, activation='softmax')(x) return Model(img_input, x, name='densenet')
y_test = to_categorical(y_test) #Adding layers from keras.layers import Convolution2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense from keras.models import Sequential from keras.optimizers import adam model = Sequential() i = 1 n = 6 for i in range(i): model.add( Convolution2D(filters=n, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1))) n = n * 2 model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(units=40, activation='relu')) model.add(Dense(units=10, activation='softmax')) model.summary() model.compile(optimizer=adam(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy']) trained_model = model.fit(X_train, y_train_cat, epochs=1, validation_data=(X_test, y_test)) acc = trained_model.history['accuracy'][-1:][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=(4, ), dtype='int32', buffer=weights_file.read(16)) 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] 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] # border_mode='same' is equivalent to Darknet pad=1 border_mode = 'same' if pad == 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) # 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)) # Create Conv2D layer conv_layer = (Convolution2D( filters, size, size, border_mode=border_mode, subsample=(stride, stride), bias=not batch_normalize, weights=conv_weights, activation=act_fn, W_regularizer=l2(weight_decay)))(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('maxpool'): size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) all_layers.append( MaxPooling2D(pool_size=(size, size), strides=(stride, stride), border_mode='same')(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(',')] layers = [all_layers[i] for i in ids] if len(layers) > 1: print('Merging layers:', layers) merge_layer = merge(layers, mode='concat') all_layers.append(merge_layer) prev_layer = merge_layer else: skip_layer = layers[0] # only one layer to route all_layers.append(skip_layer) prev_layer = skip_layer elif section.startswith('reorg'): block_size = int(cfg_parser[section]['stride']) assert block_size == 2, 'Only reorg with stride 2 supported.' all_layers.append( Lambda(space_to_depth_x2, output_shape=space_to_depth_x2_output_shape, name='space_to_depth_x2')(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('region'): with open('{}_anchors.txt'.format(output_root), 'w') as f: print(cfg_parser[section]['anchors'], file=f) elif (section.startswith('net') or section.startswith('cost') or section.startswith('softmax')): pass # Configs not currently handled during model definition. else: raise ValueError( 'Unsupported section header type: {}'.format(section)) # Create and save model. model = Model(input=all_layers[0], output=all_layers[-1]) 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(len(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))
print(X_test.shape[0], 'test samples') # 转换为one_hot类型 Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) # 构建模型 model = Sequential() """ model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1], border_mode='same', input_shape=input_shape)) """ model.add( Convolution2D(nb_filters, (kernel_size[0], kernel_size[1]), padding='same', input_shape=input_shape)) # 卷积层1 model.add(Activation('relu')) # 激活层 model.add(Convolution2D(nb_filters, (kernel_size[0], kernel_size[1]))) # 卷积层2 model.add(Activation('relu')) # 激活层 model.add(MaxPooling2D(pool_size=pool_size)) # 池化层 model.add(Dropout(0.25)) # 神经元随机失活 model.add(Flatten()) # 拉成一维数据 model.add(Dense(128)) # 全连接层1 model.add(Activation('relu')) # 激活层 model.add(Dropout(0.5)) # 随机失活 model.add(Dense(nb_classes)) # 全连接层2 model.add(Activation('softmax')) # Softmax评分 # 编译模型 model.compile(loss='categorical_crossentropy',
from keras.layers import Dense # In[5]: from keras.models import Sequential # In[6]: model = Sequential() # In[7]: model.add( Convolution2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(64, 64, 3))) # In[8]: model.summary() # In[9]: model.add(MaxPooling2D(pool_size=(2, 2))) # In[10]: model.add(Convolution2D( filters=32, kernel_size=(3, 3),
def learn(target_name="George_W_Bush", target_count=530): all_data = [] input_shape = (120, 120, 1) target_size = (120, 120) # grab all pictures of the target and add them with label 1 pics = glob.glob('lfw/' + target_name + '/*.jpg') for pic in pics: all_data.append((img_to_array( load_img(pic, color_mode="grayscale", target_size=target_size)), 1)) f = open('lfw-names.txt') lines = f.readlines() np.random.shuffle(lines) # randomize which non-target photos are included for line in lines: name = line.split()[0] if name == target_name: # let's not add the target twice continue # add all photos for the non-target person with the 0 label pics = glob.glob('lfw/' + name + '/*.jpg') for pic in pics: all_data.append((img_to_array( load_img(pic, color_mode="grayscale", target_size=target_size)), 0)) # make sure we have an even split between target and non-target # having too many of either will skew training data # we are aiming for half target and half other if len(all_data) > target_count * 2: break # shuffle the data np.random.shuffle(all_data) trn_data = [] trn_lbls = [] test_data = [] test_lbls = [] train_test_split = int(.75 * len(all_data)) # add some of data to the training data sets for pic_arr, lbl in all_data[:train_test_split]: trn_data.append(pic_arr) trn_lbls.append(lbl) # add rotated versions of the picture trn_data.append(random_rotation(pic_arr, 15)) trn_lbls.append(lbl) trn_data.append(random_rotation(pic_arr, 30)) trn_lbls.append(lbl) # add the remaining data to the test data sets for pic_arr, lbl in all_data[train_test_split:]: test_data.append(pic_arr) test_lbls.append(lbl) trn_data = np.array(trn_data) trn_lbls = np.array(trn_lbls) test_data = np.array(test_data) test_lbls = np.array(test_lbls) # construct the model opt = keras.optimizers.Adam(learning_rate=5e-4) model = keras.Sequential() model.add(Convolution2D(30, kernel_size=(3, 3), input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(keras.layers.BatchNormalization()) model.add(Convolution2D(60, kernel_size=(3, 3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(90, kernel_size=(3, 3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(120, kernel_size=(3, 3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dropout(.5)) model.add(Dense( 1, activation='sigmoid')) # Output Layer. 1==target, 0==not target model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy']) # train the model print("{}'s in the train dataset: {}/{}".format(target_name, np.sum(trn_lbls), len(trn_lbls))) model.fit(trn_data, trn_lbls, verbose=1, epochs=5) return model, test_data, test_lbls
X_train = X_train.astype('float32') X_test = X_test.astype('float32') #cross-connections between two conv layers, Y is the middle layer, while U and V are side layers. inputYUV = Input(shape=(3, 32, 32)) inputNorm = BatchNormalization(axis=1)(inputYUV) # To simplify the data augmentation, I delay slicing until this point. # Not sure if there is a better way to handle it. ---Petar inputY = Lambda(lambda x: x[:, 0:1, :, :], output_shape=(1, 32, 32))(inputNorm) inputU = Lambda(lambda x: x[:, 1:2, :, :], output_shape=(1, 32, 32))(inputNorm) inputV = Lambda(lambda x: x[:, 2:3, :, :], output_shape=(1, 32, 32))(inputNorm) convY = Convolution2D(32, 3, 3, border_mode='same', activation='relu')(inputY) convU = Convolution2D(16, 3, 3, border_mode='same', activation='relu')(inputU) convV = Convolution2D(16, 3, 3, border_mode='same', activation='relu')(inputV) convY = Convolution2D(32, 3, 3, border_mode='same', activation='relu')(convY) convU = Convolution2D(16, 3, 3, border_mode='same', activation='relu')(convU) convV = Convolution2D(16, 3, 3, border_mode='same', activation='relu')(convV) poolY = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(convY) poolU = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(convU) poolV = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(convV) poolY = Dropout(0.25)(poolY) poolU = Dropout(0.25)(poolU) poolV = Dropout(0.25)(poolV)
X = (X - np.min(X)) / np.max(X) print("X shape is %s" % str(X.shape)) print("y shape is %s" % str(y.shape)) y = np_utils.to_categorical(y, 2) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) N = 10 # Number of feature maps w, h = 3, 3 # Conv. window size model = Sequential() model.add( Convolution2D(nb_filter=N, nb_col=w, nb_row=h, activation='relu', input_shape=(64, 64, 3))) model.add(MaxPooling2D((2, 2))) model.add(Convolution2D(nb_filter=N, nb_col=w, nb_row=h, activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Flatten()) model.add(Dense(2, activation='sigmoid')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.fit(X_train, y_train, batch_size=32, nb_epoch=20, validation_data=(X_test, y_test))
def convolutions(): dropout = 0.15 bn_momentum = 0.4 l2 = 0.0001 # Model architecture definition model = Sequential() model.add( Convolution2D(32, (7, 7), activation='relu', input_shape=(224, 224, 3), kernel_regularizer=regularizers.l2(l2))) model.add(BatchNormalization(momentum=bn_momentum)) model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Dropout(0.2)) model.add( Convolution2D(32, (7, 7), activation='relu', kernel_regularizer=regularizers.l2(l2))) model.add(BatchNormalization(momentum=bn_momentum)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(dropout)) model.add( Convolution2D(64, (5, 5), activation='relu', kernel_regularizer=regularizers.l2(l2))) model.add(BatchNormalization(momentum=bn_momentum)) model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Dropout(0.2)) model.add( Convolution2D(64, (5, 5), activation='relu', kernel_regularizer=regularizers.l2(l2))) model.add(BatchNormalization(momentum=bn_momentum)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(dropout)) model.add( Convolution2D(128, (3, 3), activation='relu', kernel_regularizer=regularizers.l2(l2))) model.add(BatchNormalization(momentum=bn_momentum)) model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Dropout(dropout)) model.add( Convolution2D(128, (3, 3), activation='relu', kernel_regularizer=regularizers.l2(l2))) model.add(BatchNormalization(momentum=bn_momentum)) #model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(dropout)) # this converts our 3D feature maps to 1D feature vectors model.add(Flatten()) model.add(Dense(1024, activation='relu')) model.add(Dense(512, activation='relu')) model.add(Dense(256, activation='relu')) # Output Layer model.add(Dense(2, activation='softmax')) # antes softmax return model
# np.random.shuffle(li) # X=np.array(li[:,0]) # Y=np.array(li[:,1]) # In[ ]: # In[6]: from keras.models import Sequential from keras.layers import Convolution2D, MaxPool2D, Dropout, Dense, Flatten # In[7]: model = Sequential() model.add( Convolution2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3))) model.add(MaxPool2D(2, 2)) model.add(Dropout(0.25)) model.add(Convolution2D(64, (3, 3), activation='relu')) model.add(MaxPool2D(2, 2)) model.add(Convolution2D(32, (3, 3), activation='relu')) model.add(MaxPool2D(2, 2)) model.add(Flatten()) model.add(Dense(16, activation='relu')) model.add(Dense(1, activation='sigmoid')) # In[8]: model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
from keras.models import Sequential from keras.layers import Convolution2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense classifier = Sequential() #convolution classifier.add( Convolution2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu')) #max pooling classifier.add(MaxPooling2D(pool_size=(2, 2))) classifier.add(Convolution2D(32, 3, 3, activation='relu')) classifier.add(MaxPooling2D(pool_size=(2, 2))) #Flattening classifier.add(Flatten()) #Hidden and output layers classifier.add(Dense(units=128, activation='relu')) classifier.add(Dense(units=1, activation='sigmoid')) classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255)
frames = 41 window_size = 512 * (frames - 1) log_specgrams = [] labels = [] for l, sub_dir in enumerate(sub_dirs): for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)): sound_clip,s = librosa.load(fn) label = fn.split('\\')[2].split('-')[3].split('.')[0] ''' model = Sequential() # input: 100x100 images with 3 channels -> (3, 100, 100) tensors. # this applies 32 convolution filters of size 3x3 each. model.add( Convolution2D(64, 3, 3, border_mode='valid', input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3]))) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add(Convolution2D(64, 3, 3, border_mode='valid')) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5))
def BuildRCNN(nbChannels, shape1, shape2, nbClasses, nbRCL, nbFilters, filtersize): def RCL_block(l_settings, l, pool=True, increase_dim=False): input_num_filters = l_settings.output_shape[1] if increase_dim: out_num_filters = input_num_filters * 2 else: out_num_filters = input_num_filters conv1 = Convolution2D(out_num_filters, 1, 1, border_mode='same') stack1 = conv1(l) stack2 = BatchNormalization()(stack1) stack3 = PReLU()(stack2) conv2 = Convolution2D(out_num_filters, filtersize, filtersize, border_mode='same', init='he_normal') stack4 = conv2(stack3) stack5 = merge([stack1, stack4], mode='sum') stack6 = BatchNormalization()(stack5) stack7 = PReLU()(stack6) conv3 = Convolution2D(out_num_filters, filtersize, filtersize, border_mode='same', weights=conv2.get_weights()) stack8 = conv3(stack7) stack9 = merge([stack1, stack8], mode='sum') stack10 = BatchNormalization()(stack9) stack11 = PReLU()(stack10) conv4 = Convolution2D(out_num_filters, filtersize, filtersize, border_mode='same', weights=conv2.get_weights()) stack12 = conv4(stack11) stack13 = merge([stack1, stack12], mode='sum') stack14 = BatchNormalization()(stack13) stack15 = PReLU()(stack14) if pool: stack16 = MaxPooling2D((2, 2), border_mode='same')(stack15) stack17 = Dropout(0.1)(stack16) else: stack17 = Dropout(0.1)(stack15) return stack17 #Build Network input_img = Input(shape=(shape1, shape2, nbChannels)) conv_l = Convolution2D(nbFilters, filtersize, filtersize, border_mode='same', activation='relu') l = conv_l(input_img) for n in range(nbRCL): if n % 2 == 0: l = RCL_block(conv_l, l, pool=False) else: l = RCL_block(conv_l, l, pool=True) out = Flatten()(l) l_out = Dense(nbClasses, activation='softmax')(out) model = Model(input=input_img, output=l_out) return model
random_state=2017) #%% print(x_train[4, :, :, 0]) #%% from keras.callbacks import ModelCheckpoint, EarlyStopping model_path = "/media/gabor/ALL/MachineLearning/kaggle/TensorFlow_Speech_Recognition/models" patience = 15 early_stopping = EarlyStopping(patience=patience, verbose=1) checkpointer = ModelCheckpoint(os.path.join(model_path, 'cnn_mfcc2.model'), save_best_only=True, verbose=1) input_shape = (120, 101, 1) nclass = 31 inp = Input(shape=input_shape) img_1 = BatchNormalization()(inp) img_1 = Convolution2D(22, kernel_size=2, activation=activations.relu)(img_1) img_1 = Convolution2D(22, kernel_size=2, activation=activations.relu)(img_1) img_1 = MaxPooling2D(pool_size=(2, 2))(img_1) img_1 = Dropout(0.25)(img_1) img_1 = Convolution2D(36, kernel_size=3, activation=activations.relu)(img_1) img_1 = Convolution2D(36, kernel_size=3, activation=activations.relu)(img_1) img_1 = MaxPooling2D(pool_size=(2, 2))(img_1) img_1 = Dropout(0.25)(img_1) img_1 = Convolution2D(75, kernel_size=3, activation=activations.relu)(img_1) img_1 = Convolution2D(75, kernel_size=3, activation=activations.relu)(img_1) img_1 = MaxPooling2D(pool_size=(2, 2))(img_1) img_1 = Dropout(0.25)(img_1) img_1 = Convolution2D(140, kernel_size=3, activation=activations.relu)(img_1) img_1 = Convolution2D(140, kernel_size=3, activation=activations.relu)(img_1) img_1 = MaxPooling2D(pool_size=(2, 2))(img_1) img_1 = Dropout(0.25)(img_1)
X_test = X_val.astype('float32') print(X_train.shape[0], 'train samples') print(X_val.shape[0], 'test samples') #---Model-Definition: input_shape = X_train.shape[1:] model = Sequential() #Start with 4 Convolutiional Layers to recognize the image model.add( Convolution2D(60, 16, 16, subsample=(2, 2), border_mode='same', input_shape=input_shape, activation='relu', dim_ordering='tf')) model.add( Convolution2D(100, 2, 2, border_mode='same', input_shape=input_shape, activation='relu', dim_ordering='tf')) model.add(MaxPooling2D(pool_size=(2, 2), border_mode='same', dim_ordering='tf')) model.add( Convolution2D(140,
def ssd_300(image_size, n_classes, input_tensor = None, mode='training', min_scale=None, max_scale=None, scales=None, aspect_ratios_global=None, aspect_ratios_per_layer=[[1.0, 2.0, 0.5], [1.0, 2.0, 0.5, 3.0, 1.0/3.0], [1.0, 2.0, 0.5, 3.0, 1.0/3.0], [1.0, 2.0, 0.5, 3.0, 1.0/3.0], [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]], two_boxes_for_ar1=True, steps=[8, 16, 32, 64, 100, 300], offsets=None, clip_boxes=False, variances=[0.1, 0.1, 0.2, 0.2], coords='centroids', normalize_coords=True, subtract_mean=[123, 117, 104], divide_by_stddev=None, swap_channels=[2, 1, 0], confidence_thresh=0.01, iou_threshold=0.45, top_k=200, nms_max_output_size=400, return_predictor_sizes=False): ''' Build a Keras model with SSD300 architecture, see references. The base network is a reduced atrous VGG-16, extended by the SSD architecture, as described in the paper. Most of the arguments that this function takes are only needed for the anchor box layers. 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. 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)`. input_tensor: Tensor with shape (batch, height, width, channels) n_classes (int): The number of positive classes, e.g. 20 for Pascal VOC, 80 for MS COCO. mode (str, optional): One of 'training', 'inference' and 'inference_fast'. In 'training' mode, the model outputs the raw prediction tensor, while in 'inference' and 'inference_fast' modes, the raw predictions are decoded into absolute coordinates and filtered via confidence thresholding, non-maximum suppression, and top-k filtering. The difference between latter two modes is that 'inference' follows the exact procedure of the original Caffe implementation, while 'inference_fast' uses a faster prediction decoding procedure. 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. 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`. 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. 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 prediction layers. aspect_ratios_per_layer (list, optional): A list containing one aspect ratio list for each prediction layer. This allows you to set the aspect ratios for each predictor layer individually, which is the case for the original SSD300 implementation. If a list is passed, it overrides `aspect_ratios_global`. 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. steps (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be either ints/floats or tuples of two ints/floats. These numbers represent for each predictor layer how many pixels apart the anchor box center points should be vertically and horizontally along the spatial grid over the image. If the list contains ints/floats, then that value will be used for both spatial dimensions. If the list contains tuples of two ints/floats, then they represent `(step_height, step_width)`. If no steps are provided, then they will be computed such that the anchor box center points will form an equidistant grid within the image dimensions. offsets (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be either floats or tuples of two floats. These numbers represent for each predictor layer how many pixels from the top and left boarders of the image the top-most and left-most anchor box center points should be as a fraction of `steps`. The last bit is important: The offsets are not absolute pixel values, but fractions of the step size specified in the `steps` argument. If the list contains floats, then that value will be used for both spatial dimensions. If the list contains tuples of two floats, then they represent `(vertical_offset, horizontal_offset)`. If no offsets are provided, then they will default to 0.5 of the step size. clip_boxes (bool, optional): If `True`, clips the anchor box coordinates to stay within image boundaries. variances (list, optional): A list of 4 floats >0. The anchor box offset for each coordinate will be divided by its respective variance value. coords (str, optional): The box coordinate format to be used internally by the model (i.e. this is not the input format of the ground truth labels). Can be either 'centroids' for the format `(cx, cy, w, h)` (box center coordinates, width, and height), 'minmax' for the format `(xmin, xmax, ymin, ymax)`, or 'corners' for the format `(xmin, ymin, xmax, ymax)`. 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. subtract_mean (array-like, optional): `None` or an array-like object of integers or floating point values of any shape that is broadcast-compatible with the image shape. The elements of this array will be subtracted from the image pixel intensity values. For example, pass a list of three integers to perform per-channel mean normalization for color images. divide_by_stddev (array-like, optional): `None` or an array-like object of non-zero integers or floating point values of any shape that is broadcast-compatible with the image shape. The image pixel intensity values will be divided by the elements of this array. For example, pass a list of three integers to perform per-channel standard deviation normalization for color images. swap_channels (list, optional): Either `False` or a list of integers representing the desired order in which the input image channels should be swapped. confidence_thresh (float, optional): A float in [0,1), the minimum classification confidence in a specific positive class in order to be considered for the non-maximum suppression stage for the respective class. A lower value will result in a larger part of the selection process being done by the non-maximum suppression stage, while a larger value will result in a larger part of the selection process happening in the confidence thresholding stage. iou_threshold (float, optional): A float in [0,1]. All boxes that have a Jaccard similarity of greater than `iou_threshold` with a locally maximal box will be removed from the set of predictions for a given class, where 'maximal' refers to the box's confidence score. top_k (int, optional): The number of highest scoring predictions to be kept for each batch item after the non-maximum suppression stage. nms_max_output_size (int, optional): The maximal number of predictions that will be left over after the NMS stage. return_predictor_sizes (bool, optional): If `True`, this function not only returns the model, but also a list containing the spatial dimensions of the predictor layers. This isn't strictly necessary since you can always get their sizes easily via the Keras API, but it's convenient and less error-prone to get them this way. They are only relevant for training anyway (SSDBoxEncoder needs to know the spatial dimensions of the predictor layers), for inference you don't need them. Returns: model: The Keras SSD300 model. predictor_sizes (optional): 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 = 6 # The number of predictor conv layers in the network is 6 for the original SSD300. n_classes += 1 # Account for the background class. img_height, img_width, img_channels = image_size[0], image_size[1], image_size[2] ############################################################################ # Get a few exceptions out of the way. ############################################################################ 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: 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)) if (not (steps is None)) and (len(steps) != n_predictor_layers): raise ValueError("You must provide at least one step value per predictor layer.") if (not (offsets is None)) and (len(offsets) != n_predictor_layers): raise ValueError("You must provide at least one offset value per predictor layer.") ############################################################################ # Compute the anchor box parameters. ############################################################################ # Set the aspect ratios for each predictor layer. These are only needed for the anchor box layers. if aspect_ratios_per_layer: aspect_ratios = aspect_ratios_per_layer else: aspect_ratios = [aspect_ratios_global] * n_predictor_layers # 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 ar in aspect_ratios_per_layer: if (1 in ar) & two_boxes_for_ar1: n_boxes.append(len(ar) + 1) # +1 for the second box for aspect ratio 1 else: n_boxes.append(len(ar)) 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 = [n_boxes] * n_predictor_layers if steps is None: steps = [None] * n_predictor_layers if offsets is None: offsets = [None] * n_predictor_layers ############################################################################ # Define functions for the Lambda layers below. ############################################################################ def identity_layer(tensor): return tensor def input_mean_normalization(tensor): return tensor - np.array(subtract_mean) def input_stddev_normalization(tensor): return tensor / np.array(divide_by_stddev) def input_channel_swap(tensor): if len(swap_channels) == 3: return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]]], axis=-1) elif len(swap_channels) == 4: return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]], tensor[...,swap_channels[3]]], axis=-1) ############################################################################# # Functions for Shufflenetv1 architeture ############################################################################# def _conv_blockSSD(inputs, filters,block_id=11): channel_axis = -1 x = ZeroPadding2D(padding=(1, 1), name='conv_pad_%d_1' % block_id)(inputs) x = Conv2D(filters, (1,1),padding='valid',use_bias=False,strides=(1, 1),name='conv__%d_1'%block_id)(x) x = BatchNormalization(axis=channel_axis, name='conv_%d_bn_1'% block_id)(x) x = Activation('relu', name='conv_%d_relu_1'% block_id)(x) Conv = Conv2D(filters*2, (3,3), padding='valid', use_bias=False, strides=(2, 2), name='conv__%d_2' % block_id)(x) x = BatchNormalization(axis=channel_axis, name='conv_%d_bn_2' % block_id)(Conv) x = Activation('relu', name='conv_%d_relu_2' % block_id)(x) return x,Conv ############################################################################ # Build the network. ############################################################################ if input_tensor != None: x = Input(tensor=input_tensor, shape=(img_height, img_width, img_channels)) else: x = Input(shape=(img_height, img_width, img_channels)) # The following identity layer is only needed so that the subsequent lambda layers can be optional. x1 = Lambda(identity_layer, output_shape=(img_height, img_width, img_channels), name='identity_layer')(x) if not (divide_by_stddev is None): x1 = Lambda(input_stddev_normalization, output_shape=(img_height, img_width, img_channels), name='input_stddev_normalization')(x1) if not (subtract_mean is None): x1 = Lambda(input_mean_normalization, output_shape=(img_height, img_width, img_channels), name='input_mean_normalization')(x1) if swap_channels: x1 = Lambda(input_channel_swap, output_shape=(img_height, img_width, img_channels), name='input_channel_swap')(x1) # Get squeezenet architecture squeezenet_v1 = squeezenet.SqueezeNet(1000, inputs=(img_height, img_width, img_channels), include_top=False) FeatureExtractor = Model(inputs=squeezenet_v1.input, outputs=squeezenet_v1.get_layer('concatenate_8').output) merge9 = FeatureExtractor(x1) maxpool9 = MaxPooling2D( pool_size=(3, 3), strides=(2, 2), name='maxpool9', padding='same', data_format="channels_last")(merge9) fire10_squeeze = Convolution2D( 64, (1, 1), activation='relu', kernel_initializer='glorot_uniform', padding='same', name='fire10_squeeze', data_format="channels_last")(maxpool9) fire10_expand1 = Convolution2D( 256, (1, 1), activation='relu', kernel_initializer='glorot_uniform', padding='same', name='fire10_expand1', data_format="channels_last")(fire10_squeeze) fire10_expand2 = Convolution2D( 256, (3, 3), activation='relu', kernel_initializer='glorot_uniform', padding='same', name='fire10_expand2', data_format="channels_last")(fire10_squeeze) merge10 = Concatenate(axis=-1)([fire10_expand1, fire10_expand2]) layer, conv11_2 = _conv_blockSSD(merge10, 256, block_id=11) layer, conv12_2 = _conv_blockSSD(layer, 128, block_id=12) layer, conv13_2 = _conv_blockSSD(layer, 128, block_id=13) layer, conv14_2 = _conv_blockSSD(layer, 64, block_id=14) ### Build the convolutional predictor layers on top of the base network # We precidt `n_classes` confidence values for each box, hence the confidence predictors have depth `n_boxes * n_classes` # Output shape of the confidence layers: `(batch, height, width, n_boxes * n_classes)` conv9_mbox_conf = Conv2D(n_boxes[0] * n_classes, (3, 3), padding='same', name='conv9_mbox_conf')(merge9) conv10_mbox_conf = Conv2D(n_boxes[1] * n_classes, (3, 3), padding='same', name='conv10_mbox_conf')(merge10) conv11_2_mbox_conf = Conv2D(n_boxes[2] * n_classes, (3, 3), padding='same', name='conv11_2_mbox_conf')(conv11_2) conv12_2_mbox_conf = Conv2D(n_boxes[3] * n_classes, (3, 3), padding='same', name='conv12_2_mbox_conf')(conv12_2) conv13_2_mbox_conf = Conv2D(n_boxes[4] * n_classes, (3, 3), padding='same', name='conv13_2_mbox_conf')(conv13_2) conv14_2_mbox_conf = Conv2D(n_boxes[5] * n_classes, (3, 3), padding='same', name='conv14_2_mbox_conf')(conv14_2) # We predict 4 box coordinates for each box, hence the localization predictors have depth `n_boxes * 4` # Output shape of the localization layers: `(batch, height, width, n_boxes * 4)` conv9_mbox_loc = Conv2D(n_boxes[0] * 4, (3, 3), padding='same', name='conv9_mbox_loc')(merge9) conv10_mbox_loc = Conv2D(n_boxes[1] * 4, (3, 3), padding='same', name='conv10_mbox_loc')(merge10) conv11_2_mbox_loc = Conv2D(n_boxes[2] * 4, (3, 3), padding='same', name='conv11_2_mbox_loc')(conv11_2) conv12_2_mbox_loc = Conv2D(n_boxes[3] * 4, (3, 3), padding='same', name='conv12_2_mbox_loc')(conv12_2) conv13_2_mbox_loc = Conv2D(n_boxes[4] * 4, (3, 3), padding='same', name='conv13_2_mbox_loc')(conv13_2) conv14_2_mbox_loc = Conv2D(n_boxes[5] * 4, (3, 3), padding='same', name='conv14_2_mbox_loc')(conv14_2) ### Generate the anchor boxes (called "priors" in the original Caffe/C++ implementation, so I'll keep their layer names) # Output shape of anchors: `(batch, height, width, n_boxes, 8)` conv9_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[0], next_scale=scales[1], aspect_ratios=aspect_ratios[0], two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[0], this_offsets=offsets[0], clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv9_mbox_priorbox')(conv9_mbox_loc) conv10_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[1], next_scale=scales[2], aspect_ratios=aspect_ratios[1], two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[1], this_offsets=offsets[1], clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv10_mbox_priorbox')(conv10_mbox_loc) conv11_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[2], next_scale=scales[3], aspect_ratios=aspect_ratios[2], two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[2], this_offsets=offsets[2], clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv11_2_mbox_priorbox')(conv11_2_mbox_loc) conv12_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[3], next_scale=scales[4], aspect_ratios=aspect_ratios[3], two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[3], this_offsets=offsets[3], clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv12_2_mbox_priorbox')(conv12_2_mbox_loc) conv13_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[4], next_scale=scales[5], aspect_ratios=aspect_ratios[4], two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[4], this_offsets=offsets[4], clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv13_2_mbox_priorbox')(conv13_2_mbox_loc) conv14_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[5], next_scale=scales[6], aspect_ratios=aspect_ratios[5], two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[5], this_offsets=offsets[5], clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv14_2_mbox_priorbox')(conv14_2_mbox_loc) ### Reshape # 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 conv9_mbox_conf_reshape = Reshape((-1, n_classes), name='conv9_mbox_conf_reshape')(conv9_mbox_conf) conv10_mbox_conf_reshape = Reshape((-1, n_classes), name='conv10_mbox_conf_reshape')(conv10_mbox_conf) conv11_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv11_2_mbox_conf_reshape')(conv11_2_mbox_conf) conv12_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv12_2_mbox_conf_reshape')(conv12_2_mbox_conf) conv13_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv13_2_mbox_conf_reshape')(conv13_2_mbox_conf) conv14_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv14_2_mbox_conf_reshape')(conv14_2_mbox_conf) # Reshape the box 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 conv9_mbox_loc_reshape = Reshape((-1, 4), name='conv9_mbox_loc_reshape')(conv9_mbox_loc) conv10_mbox_loc_reshape = Reshape((-1, 4), name='conv10_mbox_loc_reshape')(conv10_mbox_loc) conv11_2_mbox_loc_reshape = Reshape((-1, 4), name='conv11_2_mbox_loc_reshape')(conv11_2_mbox_loc) conv12_2_mbox_loc_reshape = Reshape((-1, 4), name='conv12_2_mbox_loc_reshape')(conv12_2_mbox_loc) conv13_2_mbox_loc_reshape = Reshape((-1, 4), name='conv13_2_mbox_loc_reshape')(conv13_2_mbox_loc) conv14_2_mbox_loc_reshape = Reshape((-1, 4), name='conv14_2_mbox_loc_reshape')(conv14_2_mbox_loc) # Reshape the anchor box tensors, yielding 3D tensors of shape `(batch, height * width * n_boxes, 8)` conv9_mbox_priorbox_reshape = Reshape((-1, 8), name='conv9_mbox_priorbox_reshape')(conv9_mbox_priorbox) conv10_mbox_priorbox_reshape = Reshape((-1, 8), name='conv10_mbox_priorbox_reshape')(conv10_mbox_priorbox) conv11_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv11_2_mbox_priorbox_reshape')(conv11_2_mbox_priorbox) conv12_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv12_2_mbox_priorbox_reshape')(conv12_2_mbox_priorbox) conv13_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv13_2_mbox_priorbox_reshape')(conv13_2_mbox_priorbox) conv14_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv14_2_mbox_priorbox_reshape')(conv14_2_mbox_priorbox) ### Concatenate the predictions from the different layers # 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, the number of boxes per layer # Output shape of `mbox_conf`: (batch, n_boxes_total, n_classes) mbox_conf = Concatenate(axis=1, name='mbox_conf')([conv9_mbox_conf_reshape, conv10_mbox_conf_reshape, conv11_2_mbox_conf_reshape, conv12_2_mbox_conf_reshape, conv13_2_mbox_conf_reshape, conv14_2_mbox_conf_reshape]) # Output shape of `mbox_loc`: (batch, n_boxes_total, 4) mbox_loc = Concatenate(axis=1, name='mbox_loc')([conv9_mbox_loc_reshape, conv10_mbox_loc_reshape, conv11_2_mbox_loc_reshape, conv12_2_mbox_loc_reshape, conv13_2_mbox_loc_reshape, conv14_2_mbox_loc_reshape]) # Output shape of `mbox_priorbox`: (batch, n_boxes_total, 8) mbox_priorbox = Concatenate(axis=1, name='mbox_priorbox')([conv9_mbox_priorbox_reshape, conv10_mbox_priorbox_reshape, conv11_2_mbox_priorbox_reshape, conv12_2_mbox_priorbox_reshape, conv13_2_mbox_priorbox_reshape, conv14_2_mbox_priorbox_reshape]) # 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 mbox_conf_softmax = Activation('softmax', name='mbox_conf_softmax')(mbox_conf) # Concatenate the class and box predictions and the anchors to one large predictions vector # Output shape of `predictions`: (batch, n_boxes_total, n_classes + 4 + 8) predictions = Concatenate(axis=2, name='predictions')([mbox_conf_softmax, mbox_loc, mbox_priorbox]) if mode == 'training': model = Model(inputs=x, outputs=predictions) elif mode == 'inference': decoded_predictions = DecodeDetections(confidence_thresh=confidence_thresh, iou_threshold=iou_threshold, top_k=top_k, nms_max_output_size=nms_max_output_size, coords=coords, #normalize_coords=normalize_coords, #change this parameter for inference normalize_coords=False, img_height=img_height, img_width=img_width, name='decoded_predictions')(predictions) model = Model(inputs=x, outputs=decoded_predictions) elif mode == 'inference_fast': decoded_predictions = DecodeDetectionsFast(confidence_thresh=confidence_thresh, iou_threshold=iou_threshold, top_k=top_k, nms_max_output_size=nms_max_output_size, coords=coords, normalize_coords=normalize_coords, img_height=img_height, img_width=img_width, name='decoded_predictions')(predictions) model = Model(inputs=x, outputs=decoded_predictions) else: raise ValueError("`mode` must be one of 'training', 'inference' or 'inference_fast', but received '{}'.".format(mode)) return model