def inception_block_1a(X): """ Implementation of an inception block """ X_3x3 = Conv2D(96, (1, 1), data_format='channels_first', name='inception_3a_3x3_conv1')(X) X_3x3 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_3x3_bn1')(X_3x3) X_3x3 = Activation('relu')(X_3x3) X_3x3 = ZeroPadding2D(padding=(1, 1), data_format='channels_first')(X_3x3) X_3x3 = Conv2D(128, (3, 3), data_format='channels_first', name='inception_3a_3x3_conv2')(X_3x3) X_3x3 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_3x3_bn2')(X_3x3) X_3x3 = Activation('relu')(X_3x3) X_5x5 = Conv2D(16, (1, 1), data_format='channels_first', name='inception_3a_5x5_conv1')(X) X_5x5 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_5x5_bn1')(X_5x5) X_5x5 = Activation('relu')(X_5x5) X_5x5 = ZeroPadding2D(padding=(2, 2), data_format='channels_first')(X_5x5) X_5x5 = Conv2D(32, (5, 5), data_format='channels_first', name='inception_3a_5x5_conv2')(X_5x5) X_5x5 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_5x5_bn2')(X_5x5) X_5x5 = Activation('relu')(X_5x5) X_pool = MaxPooling2D(pool_size=3, strides=2, data_format='channels_first')(X) X_pool = Conv2D(32, (1, 1), data_format='channels_first', name='inception_3a_pool_conv')(X_pool) X_pool = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_pool_bn')(X_pool) X_pool = Activation('relu')(X_pool) X_pool = ZeroPadding2D(padding=((3, 4), (3, 4)), data_format='channels_first')(X_pool) X_1x1 = Conv2D(64, (1, 1), data_format='channels_first', name='inception_3a_1x1_conv')(X) X_1x1 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_1x1_bn')(X_1x1) X_1x1 = Activation('relu')(X_1x1) # CONCAT inception = concatenate([X_3x3, X_5x5, X_pool, X_1x1], axis=1) return inception
def inverted_block(x, strides=(1, 1), init_weights=None, **metaparameters): """ Construct an Inverted Residual Block x : input to the block strides : strides n_filters : number of filters alpha : width multiplier expansion : multiplier for expanding number of filters reg : kernel regularizer """ n_filters = metaparameters['n_filters'] alpha = metaparameters['alpha'] if 'alpha' in metaparameters: alpha = metaparameters['alpha'] else: alpha = MobileNetV2.alpha if 'expansion' in metaparameters: expansion = metaparameters['expansion'] else: expansion = MobileNetV2.expansion if 'reg' in metaparameters: reg = metaparameters['reg'] else: reg = MobileNetV2.reg if init_weights is None: init_weights = MobileNetV2.init_weights # Remember input shortcut = x # Apply the width filter to the number of feature maps for the pointwise convolution filters = int(n_filters * alpha) n_channels = int(x.shape[3]) # Dimensionality Expansion (non-first block) if expansion > 1: # 1x1 linear convolution x = Conv2D(expansion * n_channels, (1, 1), padding='same', use_bias=False, kernel_initializer=init_weights, kernel_regularizer=reg)(x) x = BatchNormalization()(x) x = ReLU(6.)(x) # Strided convolution to match number of filters if strides == (2, 2): x = ZeroPadding2D(padding=((0, 1), (0, 1)))(x) padding = 'valid' else: padding = 'same' # Depthwise Convolution x = DepthwiseConv2D((3, 3), strides, padding=padding, use_bias=False, kernel_initializer=init_weights, kernel_regularizer=reg)(x) x = BatchNormalization()(x) x = ReLU(6.)(x) # Linear Pointwise Convolution x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', use_bias=False, kernel_initializer=init_weights, kernel_regularizer=reg)(x) x = BatchNormalization()(x) # Number of input filters matches the number of output filters if n_channels == filters and strides == (1, 1): x = Add()([shortcut, x]) return x
def __init__(self, pad_size): self.pad_size = pad_size self.pad = ZeroPadding2D(padding=((0, 0), (pad_size, 0)))
def faceRecoModel(input_shape): """ Implementation of the Inception model used for FaceNet Arguments: input_shape -- shape of the images of the dataset Returns: model -- a Model() instance in Keras """ # Define the input as a tensor with shape input_shape X_input = Input(input_shape) # Zero-Padding X = ZeroPadding2D((3, 3))(X_input) # First Block X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1')(X) X = BatchNormalization(axis = 1, name = 'bn1')(X) X = Activation('relu')(X) # Zero-Padding + MAXPOOL X = ZeroPadding2D((1, 1))(X) X = MaxPool2D((3, 3), strides = 2)(X) # Second Block X = Conv2D(64, (1, 1), strides = (1, 1), name = 'conv2')(X) X = BatchNormalization(axis = 1, epsilon=0.00001, name = 'bn2')(X) X = Activation('relu')(X) # Zero-Padding + MAXPOOL X = ZeroPadding2D((1, 1))(X) # Second Block X = Conv2D(192, (3, 3), strides = (1, 1), name = 'conv3')(X) X = BatchNormalization(axis = 1, epsilon=0.00001, name = 'bn3')(X) X = Activation('relu')(X) # Zero-Padding + MAXPOOL X = ZeroPadding2D((1, 1))(X) X = MaxPool2D(pool_size = 3, strides = 2)(X) # Inception 1: a/b/c X = inception_block_1a(X) X = inception_block_1b(X) X = inception_block_1c(X) # Inception 2: a/b X = inception_block_2a(X) X = inception_block_2b(X) # Inception 3: a/b X = inception_block_3a(X) X = inception_block_3b(X) # Top layer X = AveragePooling2D(pool_size=(3, 3), strides=(1, 1), data_format='channels_first')(X) X = Flatten()(X) X = Dense(128, name='dense_layer')(X) # L2 normalization X = Lambda(lambda x: K.l2_normalize(x,axis=1))(X) # Create model instance model = Model(inputs = X_input, outputs = X, name='FaceRecoModel') return model
def VGG16_face(input_shape: dict): rows = input_shape['video_height'] columns = input_shape['video_width'] channels = input_shape['video_channels'] model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(rows, columns, channels))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPool2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(128, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(128, (3, 3), activation='relu')) model.add(MaxPool2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(256, (3, 3), activation='relu')) model.add(MaxPool2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(512, (3, 3), activation='relu')) model.add(MaxPool2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Conv2D(512, (3, 3), activation='relu')) model.add(MaxPool2D((2, 2), strides=(2, 2))) model.add(Conv2D(4096, (7, 7), activation='relu')) model.add(Dropout(0.5)) model.add(Conv2D(4096, (1, 1), activation='relu')) model.add(Dropout(0.5)) model.add(Conv2D(2622, (1, 1))) model.load_weights(os.path.join(MODEL_PATH, 'vgg_face_base', 'checkpoint')).expect_partial() return model
def loadModel(): #Define VGG_FACE_MODEL architecture 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))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(Convolution2D(4096, (7, 7), activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(4096, (1, 1), activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(2622, (1, 1))) model.add(Flatten()) model.add(Activation('softmax')) # Load VGG Face model weights model.load_weights('vgg_face_weights.h5') # Remove Last Softmax layer and get model upto last flatten layer with outputs 2622 units vgg_face=Model(inputs=model.layers[0].input,outputs=model.layers[-2].output) return vgg_face
def buildResNet152Model(img_height, img_width, img_channl, num_classes, num_GPU): inputs = Input(shape=(img_height, img_width, img_channl)) x = ZeroPadding2D((3, 3))(inputs) x = Conv2d_BN(x, nb_filter=64, kernel_size=(7, 7), strides=(2, 2), padding='valid') x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x) x = Conv_Block_3(x, nb_filter=[64, 64, 256], kernel_size=(3, 3), strides=(1, 1), with_conv_shortcut=True) x = Conv_Block_3(x, nb_filter=[64, 64, 256], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[64, 64, 256], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[512, 512, 2048], kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True) x = Conv_Block_3(x, nb_filter=[512, 512, 2048], kernel_size=(3, 3)) x = Conv_Block_3(x, nb_filter=[512, 512, 2048], kernel_size=(3, 3)) x = AveragePooling2D(pool_size=(7, 7))(x) x = Flatten()(x) outputs = Dense(num_classes, activation='softmax')(x) model = Model(inputs=inputs, outputs=outputs) model.summary() if (num_GPU > 1): model = multi_gpu_model(model, gpus=num_GPU) model.compile(loss=categorical_crossentropy, optimizer=Adam(lr=0.001), metrics=['accuracy']) return model
def _crnn_layers(self, input_shape, output_shape): channel_axis = 3 melgram_input = Input(shape=input_shape, dtype="float32") # Input block padding = self.network_input_width - input_shape[1] left_pad = int(padding / 2) if padding % 2: right_pad = left_pad + 1 else: right_pad = left_pad input_padding = ((0, 0), (left_pad, right_pad)) hidden = ZeroPadding2D(padding=input_padding)(melgram_input) # Conv block 1 hidden = Conv2D(64, (3, 3), padding=self.padding, name='conv1')(hidden) hidden = BatchNormalization(axis=channel_axis, name='bn1')(hidden) hidden = ELU()(hidden) hidden = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(hidden) hidden = Dropout(0.1, name='dropout1')(hidden) # Conv block 2 hidden = Conv2D(128, (3, 3), padding=self.padding, name='conv2')(hidden) hidden = BatchNormalization(axis=channel_axis, name='bn2')(hidden) hidden = ELU()(hidden) hidden = MaxPooling2D(pool_size=(3, 3), strides=(3, 3), name='pool2')(hidden) hidden = Dropout(0.1, name='dropout2')(hidden) # Conv block 3 hidden = Conv2D(128, (3, 3), padding=self.padding, name='conv3')(hidden) hidden = BatchNormalization(axis=channel_axis, name='bn3')(hidden) hidden = ELU()(hidden) hidden = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool3')(hidden) hidden = Dropout(0.1, name='dropout3')(hidden) # Conv block 4 hidden = Conv2D(128, (3, 3), padding=self.padding, name='conv4')(hidden) hidden = BatchNormalization(axis=channel_axis, name='bn4')(hidden) hidden = ELU()(hidden) hidden = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool4')(hidden) hidden = Dropout(0.1, name='dropout4')(hidden) # reshaping hidden = Reshape((15, 128))(hidden) # GRU block 1, 2, output embed_size = 32 hidden = GRU(embed_size, return_sequences=True, name='gru1')(hidden) hidden = GRU(embed_size, return_sequences=self.attention, name='gru2')(hidden) if self.attention: attention = Dense(1)(hidden) attention = Flatten()(attention) attention_act = Activation("softmax")(attention) attention = RepeatVector(embed_size)(attention_act) attention = Permute((2, 1))(attention) merged = Multiply()([hidden, attention]) hidden = Lambda(lambda xin: K.sum(xin, axis=1))(merged) if self.output_dropout: hidden = Dropout(self.output_dropout)(hidden) output = Dense(output_shape, activation='sigmoid', name='crnn_output')(hidden) return melgram_input, output
import numpy as np from tensorflow.keras.models import Model, Sequential from tensorflow.keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, Activation from tensorflow.keras.preprocessing.image import load_img, save_img, img_to_array from tensorflow.keras.applications.imagenet_utils import preprocess_input from tensorflow.keras.preprocessing import image if True: model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3))) model.add(Convolution2D(64, (3, 3), name='conv1_1')) model.add(Activation('relu', name='relu1_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, (3, 3), name='conv1_2')) model.add(Activation('relu', name='relu1_2')) model.add(MaxPooling2D((2, 2), strides=(2, 2), name='pool1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), name='conv2_1')) model.add(Activation('relu', name='relu2_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), name='conv2_2')) model.add(Activation('relu', name='relu2_2')) model.add(MaxPooling2D((2, 2), strides=(2, 2), name='pool2')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), name='conv3_1')) model.add(Activation('relu', name='relu3_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), name='conv3_2')) model.add(Activation('relu', name='relu3_2'))
def build_encoder_decoder(): # Encoder input_tensor = Input(shape=(320, 320, 4)) x = ZeroPadding2D((1, 1))(input_tensor) x = Conv2D(64, (3, 3), activation='relu', name='conv1_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(64, (3, 3), activation='relu', name='conv1_2')(x) orig_1 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_2')(x) orig_2 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_3')(x) orig_3 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_3')(x) orig_4 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_3')(x) orig_5 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) # Decoder # x = Conv2D(4096, (7, 7), activation='relu', padding='valid', name='conv6')(x) # x = BatchNormalization()(x) # x = UpSampling2D(size=(7, 7))(x) x = Conv2D(512, (1, 1), activation='relu', padding='same', name='deconv6', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_5) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_5) # print('origReshaped.shape: ' + str(K.int_shape(origReshaped))) xReshaped = Reshape(shape)(x) # print('xReshaped.shape: ' + str(K.int_shape(xReshaped))) together = Concatenate(axis=1)([origReshaped, xReshaped]) # print('together.shape: ' + str(K.int_shape(together))) x = Unpooling()(together) x = Conv2D(512, (5, 5), activation='relu', padding='same', name='deconv5', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_4) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_4) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (5, 5), activation='relu', padding='same', name='deconv4', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_3) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_3) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(128, (5, 5), activation='relu', padding='same', name='deconv3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_2) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_2) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_1) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_1) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=input_tensor, outputs=x) return model
from tensorflow.keras.models import Model from tensorflow.keras.optimizers import SGD from tensorflow.keras.applications.vgg16 import preprocess_input import tensorflow_datasets as tfds import cv2 as cv import numpy as np from tensorflow.keras.utils import to_categorical import os # L2/HardNet Architecture (with color)? la01 = ZeroPadding2D(1, input_shape=(32,32, 3)) la02 = Conv2D(32, kernel_size=(3, 3)) la03 = BatchNormalization(epsilon=0.0001, scale=False, center=False) la04 = ReLU() la05 = ZeroPadding2D(1) la06 = Conv2D(32, kernel_size=(3, 3)) la07 = BatchNormalization(epsilon=0.0001, scale=False, center=False) la08 = ReLU() la09 = ZeroPadding2D(1) la10 = Conv2D(64, kernel_size=(3, 3), strides=2) la11 = BatchNormalization(epsilon=0.0001, scale=False, center=False) la12 = ReLU() la13 = ZeroPadding2D(1)
def make_resnet_model(): model = Sequential() model.add(Input(shape=(300, 300, 3), name='input_layer'), ) model.add(ZeroPadding2D(padding=(3, 3))) model.add(Conv2D(32, (10, 10), strides=2, kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(ZeroPadding2D(padding=(1, 1))) model.add(MaxPooling2D((2, 2), strides=1, padding='same')) model.add( Conv2D(32, (1, 1), strides=1, padding='valid', kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Conv2D(32, (3, 3), strides=1, padding='same', kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) # model.add(MaxPooling2D((2, 2), strides=1, padding='same')) model.add( Conv2D(32, (1, 1), strides=2, padding='valid', kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Conv2D(32, (3, 3), strides=1, padding='same', kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Conv2D(32, (3, 3), strides=1, padding='valid', kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) # model.add(MaxPooling2D((2, 2), strides=1, padding='same')) # model.add(Conv2D(8, (1, 1), strides=1, padding='same', activation='relu', kernel_initializer='he_normal')) # model.add(Flatten()) # model.add(Dense(8, activation='relu')) # model.add(Dropout(0.5)) model.add(GlobalAveragePooling2D()) model.add(Dense(3, activation='softmax', name='output_layer')) model.summary() return model
LeNet1.add(AveragePooling2D(pool_size=2, strides=2)) LeNet1.add( Conv2D(filters=12, kernel_size=5, padding='valid', strides=1, activation='tanh')) LeNet1.add(AveragePooling2D(pool_size=2, strides=2)) LeNet1.add(Flatten()) LeNet1.add(Dense(units=10, activation='softmax')) LeNet1.build(input_shape=(None, 28, 28, 1)) LeNet1.summary() LeNet4 = Sequential() LeNet4.add(ZeroPadding2D(padding=2)) LeNet4.add( Conv2D(filters=4, kernel_size=5, padding='valid', strides=1, activation='tanh')) LeNet4.add(AveragePooling2D(pool_size=2, strides=2)) LeNet4.add( Conv2D(filters=16, kernel_size=5, padding='valid', strides=1, actvation='tanh')) LeNet4.add(AveragePooling2D(pool_size=2, strides=2)) LeNet4.add(Flatten())
def PCBL(x, num_filters): x = ZeroPadding2D(((1, 0), (1, 0)))(x) x = CBL(x, num_filters, (3, 3), strides=(2, 2)) return x
def get_transcoder(input_shape, action_count, conv_info): cross_timer_input, cross_timer_next, is_crossed = create_timer( cross_timer_count, name='cross_timer_input') carpisma_timer_input, carpisma_timer_next, is_carpisma = create_timer( carpisma_timer_count, name='carpisma_timer_input') input = Input(shape=input_shape, name='encoded_input') input_action = Input(shape=(action_count, ), name='action') input_action_crossed = Lambda(merge_action_crossed_fn)( [input_action, is_crossed, carpisma_timer_input]) merged_input = Lambda(action_merge)([input, input_action_crossed]) #input_action next_state = merged_input idx = 0 for info in conv_info: if info[0] == Conv2D: conv = info[0]( filters=info[1], kernel_size=info[2], strides=info[3], padding="same", activation=info[4], use_bias=False, kernel_initializer=tf.keras.initializers.RandomUniform( minval=0.005, maxval=0.01, seed=None), name=f'conv_transcoder_{idx}' ) #, kernel_initializer=RandomNormal(mean=0.0, stddev=0.001, seed=None), kernel_regularizer=l1(0.1), bias_regularizer=l1(0.1) elif info[0] == Reshape: conv = info[0]((info[1], info[2], info[3])) elif info[0] == Conv2DTranspose: conv = info[0](filters=info[1], kernel_size=info[2], strides=info[3], padding="valid", activation=info[4], use_bias=False, kernel_initializer='glorot_normal', name=f'conv_transcoder_{idx}') idx += 1 next_state = conv(next_state) tavuk = Lambda(channel_slice_function(10, 13))(next_state) cars = Lambda(channel_slice_function(0, 10))(next_state) x0 = 46 y0 = 190 top_crop = 18 #20 @ersin - en ustte kaliyordu bottom_crop = 17 tavuk_top = Lambda(image_slice_function(((0, top_crop), (0, 160))))(tavuk) tavuk_top_sum = Lambda(sum_tavuk)(tavuk_top) reward = Lambda(reward_sum)(tavuk_top_sum) cross_timer_next = Lambda( add_to_timer(cross_timer_count))([cross_timer_next, tavuk_top_sum]) tavuk_bottom = Lambda( image_slice_function(((210 - bottom_crop, 210), (0, 160))))(tavuk) tavuk_bottom_sum = Lambda(sum_tavuk)(tavuk_bottom) tavuk_middle = Lambda( image_slice_function(((top_crop, 210 - bottom_crop), (0, 160))))(tavuk) tavuk_add_bottom = ZeroPadding2D( ((y0, 210 - y0 - 1), (x0, 160 - x0 - 1)))(tavuk_top_sum) tavuk_add_bottom2 = ZeroPadding2D( ((y0, 210 - y0 - 1), (x0, 160 - x0 - 1)))(tavuk_bottom_sum) tavuk_middle_padded = ZeroPadding2D( ((top_crop, bottom_crop), (0, 0)))(tavuk_middle) tavuk2 = Lambda(channel_slice_function(0, 1))(tavuk_middle_padded) tavuk2 = Add()([tavuk_add_bottom, tavuk_add_bottom2, tavuk2]) tavuk_rest = Lambda(channel_slice_function(1, 3))(tavuk_middle_padded) next_state = Concatenate()([cars, tavuk2, tavuk_rest]) next_obs = model_ae_decoder_channels(next_state) carpisma = Lambda(check_carpisma)(next_obs) #tavuk_alpha = Lambda(get_tavuk_alpha)(next_obs) #car_alpha = Lambda(get_car_alpha)(next_obs) carpisma_timer_next = Lambda( add_to_timer(carpisma_timer_count))([carpisma_timer_next, carpisma]) model_transcoder = Model( inputs=[input, input_action, cross_timer_input, carpisma_timer_input], outputs=[next_state, cross_timer_next, carpisma_timer_next, reward], name="transcoder") #,car_alpha,tavuk_alpha return model_transcoder
def build_model(start_neurons): backbone = Xception(input_shape=(img_h, img_w, 3), weights=None, include_top=False) input = backbone.input conv4 = backbone.layers[121].output conv4 = LeakyReLU(alpha=0.1)(conv4) pool4 = MaxPooling2D((2, 2))(conv4) pool4 = Dropout(0.1)(pool4) # Middle convm = Conv2D(start_neurons*32, (3, 3), activation=None, padding="same")(pool4) convm = residual_block(convm, start_neurons*32) convm = residual_block(convm, start_neurons*32) convm = LeakyReLU(alpha=0.1)(convm) # 8 -> 16 deconv4 = Conv2DTranspose(start_neurons*16, (3, 3), strides=(2, 2), padding="same")(convm) uconv4 = concatenate([deconv4, conv4]) uconv4 = Dropout(0.1)(uconv4) uconv4 = Conv2D(start_neurons*16, (3, 3), activation=None, padding="same")(uconv4) uconv4 = residual_block(uconv4, start_neurons * 16) uconv4 = residual_block(uconv4, start_neurons*16) uconv4 = LeakyReLU(alpha=0.1)(uconv4) # 16 -> 32 deconv3 = Conv2DTranspose(start_neurons*8, (3, 3), strides=(2, 2), padding="same")(uconv4) conv3 = backbone.layers[31].output uconv3 = concatenate([deconv3, conv3]) uconv3 = Dropout(0.1)(uconv3) uconv3 = Conv2D(start_neurons*8, (3, 3), activation=None, padding="same")(uconv3) uconv3 = residual_block(uconv3, start_neurons*8) uconv3 = residual_block(uconv3, start_neurons*8) uconv3 = LeakyReLU(alpha=0.1)(uconv3) # 32 -> 64 deconv2 = Conv2DTranspose(start_neurons*4, (3, 3), strides=(2, 2), padding="same")(uconv3) conv2 = backbone.layers[21].output conv2 = ZeroPadding2D(((1,0),(1,0)))(conv2) uconv2 = concatenate([deconv2, conv2]) uconv2 = Dropout(0.1)(uconv2) uconv2 = Conv2D(start_neurons*4, (3, 3), activation=None, padding="same")(uconv2) uconv2 = residual_block(uconv2, start_neurons*4) uconv2 = residual_block(uconv2, start_neurons*4) uconv2 = LeakyReLU(alpha=0.1)(uconv2) # 64 -> 128 deconv1 = Conv2DTranspose(start_neurons*2, (3, 3), strides=(2, 2), padding="same")(uconv2) conv1 = backbone.layers[11].output conv1 = ZeroPadding2D(((3,0),(3,0)))(conv1) uconv1 = concatenate([deconv1, conv1]) uconv1 = Dropout(0.1)(uconv1) uconv1 = Conv2D(start_neurons*2, (3, 3), activation=None, padding="same")(uconv1) uconv1 = residual_block(uconv1, start_neurons*2) uconv1 = residual_block(uconv1, start_neurons*2) uconv1 = LeakyReLU(alpha=0.1)(uconv1) # 128 -> 256 uconv0 = Conv2DTranspose(start_neurons*1, (3, 3), strides=(2, 2), padding="same")(uconv1) uconv0 = Dropout(0.1)(uconv0) uconv0 = Conv2D(start_neurons*1, (3, 3), activation=None, padding="same")(uconv0) uconv0 = residual_block(uconv0, start_neurons*1) uconv0 = residual_block(uconv0, start_neurons*1) uconv0 = LeakyReLU(alpha=0.1)(uconv0) uconv0 = Dropout(0.1/2)(uconv0) output_layer_noActi = Conv2D(3, (1,1), padding="same", activation=None)(uconv0) model = Model(inputs=input, outputs=output_layer_noActi) return model
def create(input_shape, num_class=1, activation=tf.nn.relu): opts = locals().copy() # model = Depth_BBBP(num_class, activation) # return model, opts concat_axis = 3 inputs = tf.keras.layers.Input(shape=input_shape) Conv2D_ = functools.partial(tfp.layers.Convolution2DReparameterization, activation=activation, padding='same') conv1 = Conv2D_(32, (3, 3))(inputs) conv1 = Conv2D_(32, (3, 3))(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D_(64, (3, 3))(pool1) conv2 = Conv2D_(64, (3, 3))(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D_(128, (3, 3))(pool2) conv3 = Conv2D_(128, (3, 3))(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D_(256, (3, 3))(pool3) conv4 = Conv2D_(256, (3, 3))(conv4) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) conv5 = Conv2D_(512, (3, 3))(pool4) conv5 = Conv2D_(512, (3, 3))(conv5) up_conv5 = UpSampling2D(size=(2, 2))(conv5) ch, cw = get_crop_shape(conv4, up_conv5) crop_conv4 = Cropping2D(cropping=(ch, cw))(conv4) up6 = concatenate([up_conv5, crop_conv4], axis=concat_axis) conv6 = Conv2D_(256, (3, 3))(up6) conv6 = Conv2D_(256, (3, 3))(conv6) up_conv6 = UpSampling2D(size=(2, 2))(conv6) ch, cw = get_crop_shape(conv3, up_conv6) crop_conv3 = Cropping2D(cropping=(ch, cw))(conv3) up7 = concatenate([up_conv6, crop_conv3], axis=concat_axis) conv7 = Conv2D_(128, (3, 3))(up7) conv7 = Conv2D_(128, (3, 3))(conv7) up_conv7 = UpSampling2D(size=(2, 2))(conv7) ch, cw = get_crop_shape(conv2, up_conv7) crop_conv2 = Cropping2D(cropping=(ch, cw))(conv2) up8 = concatenate([up_conv7, crop_conv2], axis=concat_axis) conv8 = Conv2D_(64, (3, 3))(up8) conv8 = Conv2D_(64, (3, 3))(conv8) up_conv8 = UpSampling2D(size=(2, 2))(conv8) ch, cw = get_crop_shape(conv1, up_conv8) crop_conv1 = Cropping2D(cropping=(ch, cw))(conv1) up9 = concatenate([up_conv8, crop_conv1], axis=concat_axis) conv9 = Conv2D_(32, (3, 3))(up9) conv9 = Conv2D_(32, (3, 3))(conv9) ch, cw = get_crop_shape(inputs, conv9) conv9 = ZeroPadding2D(padding=((ch[0], ch[1]), (cw[0], cw[1])))(conv9) conv10 = Conv2D(num_class, (1, 1))(conv9) conv10 = 1e-6 * conv10 model = tf.keras.models.Model(inputs=inputs, outputs=conv10) return model, opts
def test_delete_channels_zeropadding2d(channel_index, data_format): layer = ZeroPadding2D([2, 3], data_format=data_format) layer_test_helper_flatten_2d(layer, channel_index, data_format)
def yolo4lite_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create YOLO_v4 Lite MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) f1 = mobilenet.get_layer('conv_pw_13_relu').output # f1 :13 x 13 x (1024*alpha) for 416 input #feature map 1 head (13 x 13 x (512*alpha) for 416 input) x1 = make_yolo_spp_depthwise_separable_head(f1, int(512*alpha), block_id_str='14') #upsample fpn merge for feature map 1 & 2 x1_upsample = compose( DarknetConv2D_BN_Leaky(int(256*alpha), (1,1)), UpSampling2D(2))(x1) f2 = mobilenet.get_layer('conv_pw_11_relu').output # f2: 26 x 26 x (512*alpha) for 416 input x2 = DarknetConv2D_BN_Leaky(int(256*alpha), (1,1))(f2) x2 = Concatenate()([x2, x1_upsample]) #feature map 2 head (26 x 26 x (256*alpha) for 416 input) x2 = make_yolo_depthwise_separable_head(x2, int(256*alpha), block_id_str='15') #upsample fpn merge for feature map 2 & 3 x2_upsample = compose( DarknetConv2D_BN_Leaky(int(128*alpha), (1,1)), UpSampling2D(2))(x2) f3 = mobilenet.get_layer('conv_pw_5_relu').output # f3 : 52 x 52 x (256*alpha) for 416 input x3 = DarknetConv2D_BN_Leaky(int(128*alpha), (1,1))(f3) x3 = Concatenate()([x3, x2_upsample]) #feature map 3 head & output (52 x 52 x (256*alpha) for 416 input) #x3, y3 = make_depthwise_separable_last_layers(x3, int(128*alpha), num_anchors*(num_classes+5), block_id_str='16') x3 = make_yolo_depthwise_separable_head(x3, int(128*alpha), block_id_str='16') y3 = compose( Depthwise_Separable_Conv2D_BN_Leaky(int(256*alpha), (3,3), block_id_str='16_3'), DarknetConv2D(num_anchors*(num_classes+5), (1,1)))(x3) #downsample fpn merge for feature map 3 & 2 x3_downsample = compose( ZeroPadding2D(((1,0),(1,0))), Darknet_Depthwise_Separable_Conv2D_BN_Leaky(int(256*alpha), (3,3), strides=(2,2), block_id_str='16_4'))(x3) x2 = Concatenate()([x3_downsample, x2]) #feature map 2 output (26 x 26 x (512*alpha) for 416 input) #x2, y2 = make_depthwise_separable_last_layers(x2, int(256*alpha), num_anchors*(num_classes+5), block_id_str='17') x2 = make_yolo_depthwise_separable_head(x2, int(256*alpha), block_id_str='17') y2 = compose( Depthwise_Separable_Conv2D_BN_Leaky(int(512*alpha), (3,3), block_id_str='17_3'), DarknetConv2D(num_anchors*(num_classes+5), (1,1)))(x2) #downsample fpn merge for feature map 2 & 1 x2_downsample = compose( ZeroPadding2D(((1,0),(1,0))), Darknet_Depthwise_Separable_Conv2D_BN_Leaky(int(512*alpha), (3,3), strides=(2,2), block_id_str='17_4'))(x2) x1 = Concatenate()([x2_downsample, x1]) #feature map 1 output (13 x 13 x (1024*alpha) for 416 input) #x1, y1 = make_depthwise_separable_last_layers(x1, int(512*alpha), num_anchors*(num_classes+5), block_id_str='18') x1 = make_yolo_depthwise_separable_head(x1, int(512*alpha), block_id_str='18') y1 = compose( Depthwise_Separable_Conv2D_BN_Leaky(int(1024*alpha), (3,3), block_id_str='18_3'), DarknetConv2D(num_anchors*(num_classes+5), (1,1)))(x1) return Model(inputs, [y1, y2, y3])
def ResNet50(input_shape=None, classes=1000): """Instantiates the ResNet50 architecture. # Arguments 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, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. classes: optional number of classes to classify images into # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ img_input = tf.keras.layers.Input(shape=input_shape) bn_axis = 3 # Channel is at dim 3 x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid', kernel_initializer='he_normal', name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = ZeroPadding2D(padding=(1, 1), name='pool1_pad')(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 = GlobalAveragePooling2D()(x) outputs = Dense(classes, activation='softmax', name='fc')(x) inputs = img_input # Create model. model = Model(inputs, x, name='pokemonresnet') model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) return model
def ResNet50(include_top=True, OS=8, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. 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, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. 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 block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, 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. """ """ Modified ResNet50 feature extractor body with specified output stride and skip level feature """ if OS == 8: origin_os16_stride = (1, 1) origin_os16_block_rate = 2 origin_os32_stride = (1, 1) origin_os32_block_rate = 4 elif OS == 16: origin_os16_stride = (2, 2) origin_os16_block_rate = 1 origin_os32_stride = (1, 1) origin_os32_block_rate = 2 elif OS == 32: origin_os16_stride = (2, 2) origin_os16_block_rate = 1 origin_os32_stride = (2, 2) origin_os32_block_rate = 1 else: raise ValueError('invalid output stride', OS) if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') 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=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 backend.is_keras_tensor(input_tensor): #img_input = Input(tensor=input_tensor, shape=input_shape) #else: #img_input = input_tensor img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid', kernel_initializer='he_normal', name='conv1')(x) x = CustomBatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = ReLU()(x) x = ZeroPadding2D(padding=(1, 1), name='pool1_pad')(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') # skip level feature, with output stride = 4 skip = x 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') # original output stride changes to 16 from here, so we start to control block stride and dilation rate x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', strides=origin_os16_stride) # origin: stride=(2, 2) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', rate=origin_os16_block_rate) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', rate=origin_os16_block_rate) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', rate=origin_os16_block_rate) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', rate=origin_os16_block_rate) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', rate=origin_os16_block_rate) # original output stride changes to 32 from here x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', strides=origin_os32_stride, rate=origin_os16_block_rate) # origin: stride=(2, 2) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', rate=origin_os32_block_rate) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', rate=origin_os32_block_rate) if include_top: x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) else: warnings.warn('The output shape of `ResNet50(include_top=False)` ' 'has been changed since Keras 2.2.0.') # 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': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = 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) elif weights is not None: model.load_weights(weights) backbone_len = len(model.layers) # need to return feature map and skip connection, # not the whole "no top" model return x, skip, backbone_len
def miniResNet(width, height, depth, classes, stages, filters, reg=0.0001, bnEps=2e-5, bnMom=0.9, dataset="cifar"): # initialize the input shape to be "channels last" and the channels dimension itself inputShape = (height, width, depth) chanDim = -1 # if we are using "channels first", update the input shape and channels dimension if K.image_data_format() == "channels_first": inputShape = (depth, height, width) chanDim = 1 # set the input and apply BN inputs = Input(shape=inputShape, name="conv2d_1_input") x = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum=bnMom)(inputs) # check if we are utilizing the CIFAR dataset if dataset == "cifar": # apply a single CONV layer x = Conv2D(filters[0], (3, 3), use_bias=False, padding="same", kernel_regularizer=l2(reg))(x) # check to see if we are using the Tiny ImageNet dataset elif dataset == "tiny_imagenet": # apply CONV => BN => ACT => POOL to reduce spatial size x = Conv2D(filters[0], (5, 5), use_bias=False, padding="same", kernel_regularizer=l2(reg))(x) x = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum=bnMom)(x) x = Activation("relu")(x) x = ZeroPadding2D((1, 1))(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) # loop over the number of stages for i in range(0, len(stages)): # initialize the stride, then apply a residual module used to reduce the spatial size of the input volume stride = (1, 1) if i == 0 else (2, 2) x = ConvNetFactory.residual_module(x, filters[i + 1], stride, chanDim, red=True, bnEps=bnEps, bnMom=bnMom) # loop over the number of layers in the stage for j in range(0, stages[i] - 1): # apply a ResNet module x = ConvNetFactory.residual_module(x, filters[i + 1], (1, 1), chanDim, bnEps=bnEps, bnMom=bnMom) # apply BN => ACT => POOL x = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum=bnMom)(x) x = Activation("relu")(x) x = AveragePooling2D((8, 8))(x) # softmax classifier x = Flatten()(x) x = Dense(classes, kernel_regularizer=l2(reg))(x) x = Activation("softmax")(x) # create the model model = Model(inputs, x, name="resnet") # return the constructed network architecture return model
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') major, minor, revision = np.ndarray(shape=(3, ), dtype='int32', buffer=weights_file.read(12)) if (major * 10 + minor) >= 2 and major < 1000 and minor < 1000: seen = np.ndarray(shape=(1, ), dtype='int64', buffer=weights_file.read(8)) else: seen = np.ndarray(shape=(1, ), dtype='int32', buffer=weights_file.read(4)) print('Weights Header: ', major, minor, revision, seen) print('Parsing Darknet config.') unique_config_file = unique_config_sections(config_path) cfg_parser = configparser.ConfigParser() cfg_parser.read_file(unique_config_file) print('Creating Keras model.') input_layer = Input(shape=(None, None, 3)) prev_layer = input_layer all_layers = [] weight_decay = float(cfg_parser['net_0']['decay'] ) if 'net_0' in cfg_parser.sections() else 5e-4 count = 0 out_index = [] for section in cfg_parser.sections(): print('Parsing section {}'.format(section)) if section.startswith('convolutional'): filters = int(cfg_parser[section]['filters']) size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) pad = int(cfg_parser[section]['pad']) activation = cfg_parser[section]['activation'] batch_normalize = 'batch_normalize' in cfg_parser[section] padding = 'same' if pad == 1 and stride == 1 else 'valid' # Setting weights. # Darknet serializes convolutional weights as: # [bias/beta, [gamma, mean, variance], conv_weights] prev_layer_shape = K.int_shape(prev_layer) weights_shape = (size, size, prev_layer_shape[-1], filters) darknet_w_shape = (filters, weights_shape[2], size, size) weights_size = np.product(weights_shape) print('conv2d', 'bn' if batch_normalize else ' ', activation, weights_shape) conv_bias = np.ndarray(shape=(filters, ), dtype='float32', buffer=weights_file.read(filters * 4)) count += filters if batch_normalize: bn_weights = np.ndarray(shape=(3, filters), dtype='float32', buffer=weights_file.read(filters * 12)) count += 3 * filters bn_weight_list = [ bn_weights[0], # scale gamma conv_bias, # shift beta bn_weights[1], # running mean bn_weights[2] # running var ] conv_weights = np.ndarray(shape=darknet_w_shape, dtype='float32', buffer=weights_file.read(weights_size * 4)) count += weights_size # DarkNet conv_weights are serialized Caffe-style: # (out_dim, in_dim, height, width) # We would like to set these to Tensorflow order: # (height, width, in_dim, out_dim) conv_weights = np.transpose(conv_weights, [2, 3, 1, 0]) conv_weights = [conv_weights] if batch_normalize else [ conv_weights, conv_bias ] # Handle activation. act_fn = None if activation == 'leaky': pass # Add advanced activation later. elif activation != 'linear': raise ValueError( 'Unknown activation function `{}` in section {}'.format( activation, section)) # Create Conv2D layer if stride > 1: # Darknet uses left and top padding instead of 'same' mode prev_layer = ZeroPadding2D(((1, 0), (1, 0)))(prev_layer) conv_layer = (Conv2D(filters, (size, size), strides=(stride, stride), kernel_regularizer=l2(weight_decay), use_bias=not batch_normalize, weights=conv_weights, activation=act_fn, padding=padding))(prev_layer) if batch_normalize: conv_layer = (BatchNormalization( weights=bn_weight_list))(conv_layer) prev_layer = conv_layer if activation == 'linear': all_layers.append(prev_layer) elif activation == 'leaky': act_layer = LeakyReLU(alpha=0.1)(prev_layer) prev_layer = act_layer all_layers.append(act_layer) elif section.startswith('route'): ids = [int(i) for i in cfg_parser[section]['layers'].split(',')] layers = [all_layers[i] for i in ids] if len(layers) > 1: print('Concatenating route layers:', layers) concatenate_layer = Concatenate()(layers) all_layers.append(concatenate_layer) prev_layer = concatenate_layer else: skip_layer = layers[0] # only one layer to route all_layers.append(skip_layer) prev_layer = skip_layer elif section.startswith('maxpool'): size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) all_layers.append( MaxPooling2D(pool_size=(size, size), strides=(stride, stride), padding='same')(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('shortcut'): index = int(cfg_parser[section]['from']) activation = cfg_parser[section]['activation'] assert activation == 'linear', 'Only linear activation supported.' all_layers.append(Add()([all_layers[index], prev_layer])) prev_layer = all_layers[-1] elif section.startswith('upsample'): stride = int(cfg_parser[section]['stride']) assert stride == 2, 'Only stride=2 supported.' all_layers.append(UpSampling2D(stride)(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('yolo'): out_index.append(len(all_layers) - 1) all_layers.append(None) prev_layer = all_layers[-1] elif section.startswith('net'): pass else: raise ValueError( 'Unsupported section header type: {}'.format(section)) # Create and save model. if len(out_index) == 0: out_index.append(len(all_layers) - 1) model = Model(inputs=input_layer, outputs=[all_layers[i] for i in out_index]) print(model.summary()) if args.weights_only: model.save_weights('{}'.format(output_path)) print('Saved Keras weights to {}'.format(output_path)) else: model.save('{}'.format(output_path)) print('Saved Keras model to {}'.format(output_path)) # Check to see if all weights have been read. remaining_weights = len(weights_file.read()) / 4 weights_file.close() print('Read {} of {} from Darknet weights.'.format( count, count + remaining_weights)) if remaining_weights > 0: print('Warning: {} unused weights'.format(remaining_weights)) if args.plot_model: plot(model, to_file='{}.png'.format(output_root), show_shapes=True) print('Saved model plot to {}.png'.format(output_root))
def build_stage2_generator(): # 1. CA 확대 신경망 input_layer = Input(shape=(1024, )) input_lr_images = Input(shape=(64, 64, 3)) ca = Dense(256)(input_layer) mean_logsigma = LeakyReLU(0.2)(ca) c = Lambda(generate_c)(mean_logsigma) # 2. 이미지 인코더 x = ZeroPadding2D(padding=(1, 1))(input_lr_images) x = Conv2D(128, kernel_size=3, strides=1, use_bias=False)(x) x = ReLU()(x) x = ZeroPadding2D(padding=(1, 1))(x) x = Conv2D(256, kernel_size=4, strides=2, use_bias=False)(x) x = BatchNormalization()(x) x = ReLU()(x) x = ZeroPadding2D(padding=(1, 1))(x) x = Conv2D(512, kernel_size=4, strides=2, use_bias=False)(x) x = BatchNormalization()(x) x = ReLU()(x) # 접합 블록 c_code = Lambda(joint_block)([c, x]) # 3. 잔차 블록 x = ZeroPadding2D(padding=(1, 1))(c_code) x = Conv2D(512, kernel_size=3, strides=1, use_bias=False)(x) x = BatchNormalization()(x) x = ReLU()(x) x = residual_block(x) x = residual_block(x) x = residual_block(x) x = residual_block(x) # 4. 상향 표본추출 x = UpSampling2D(size=(2, 2))(x) x = Conv2D(512, kernel_size=3, padding='same', strides=1, use_bias=False)(x) x = BatchNormalization()(x) x = ReLU()(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(256, kernel_size=3, padding='same', strides=1, use_bias=False)(x) x = BatchNormalization()(x) x = ReLU()(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(128, kernel_size=3, padding='same', strides=1, use_bias=False)(x) x = BatchNormalization()(x) x = ReLU()(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(64, kernel_size=3, padding='same', strides=1, use_bias=False)(x) x = BatchNormalization()(x) x = ReLU()(x) x = Conv2D(3, kernel_size=3, padding='same', strides=1, use_bias=False)(x) x = Activation('tanh')(x) model = Model(inputs=[input_layer, input_lr_images], outputs=[x, mean_logsigma]) return model
def nn_base(input_tensor=None, trainable=False): # Determine proper input shape if K.image_data_format() == 'channels_first': 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_data_format() == 'channels_last': 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
def yolo4_body(inputs, num_anchors, num_classes): """Create YOLO_V4 model CNN body in Keras.""" darknet = Model(inputs, darknet_body(inputs)) #19x19 head y19 = DarknetConv2D_BN_Leaky(512, (1, 1))(darknet.output) y19 = DarknetConv2D_BN_Leaky(1024, (3, 3))(y19) y19 = DarknetConv2D_BN_Leaky(512, (1, 1))(y19) maxpool1 = MaxPooling2D(pool_size=(13, 13), strides=(1, 1), padding='same')(y19) maxpool2 = MaxPooling2D(pool_size=(9, 9), strides=(1, 1), padding='same')(y19) maxpool3 = MaxPooling2D(pool_size=(5, 5), strides=(1, 1), padding='same')(y19) y19 = Concatenate()([maxpool1, maxpool2, maxpool3, y19]) y19 = DarknetConv2D_BN_Leaky(512, (1, 1))(y19) y19 = DarknetConv2D_BN_Leaky(1024, (3, 3))(y19) y19 = DarknetConv2D_BN_Leaky(512, (1, 1))(y19) y19_upsample = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(y19) #38x38 head y38 = DarknetConv2D_BN_Leaky(256, (1, 1))(darknet.layers[204].output) y38 = Concatenate()([y38, y19_upsample]) y38 = DarknetConv2D_BN_Leaky(256, (1, 1))(y38) y38 = DarknetConv2D_BN_Leaky(512, (3, 3))(y38) y38 = DarknetConv2D_BN_Leaky(256, (1, 1))(y38) y38 = DarknetConv2D_BN_Leaky(512, (3, 3))(y38) y38 = DarknetConv2D_BN_Leaky(256, (1, 1))(y38) y38_upsample = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(y38) #76x76 head y76 = DarknetConv2D_BN_Leaky(128, (1, 1))(darknet.layers[131].output) y76 = Concatenate()([y76, y38_upsample]) y76 = DarknetConv2D_BN_Leaky(128, (1, 1))(y76) y76 = DarknetConv2D_BN_Leaky(256, (3, 3))(y76) y76 = DarknetConv2D_BN_Leaky(128, (1, 1))(y76) y76 = DarknetConv2D_BN_Leaky(256, (3, 3))(y76) y76 = DarknetConv2D_BN_Leaky(128, (1, 1))(y76) #76x76 output y76_output = DarknetConv2D_BN_Leaky(256, (3, 3))(y76) y76_output = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(y76_output) #38x38 output y76_downsample = ZeroPadding2D(((1, 0), (1, 0)))(y76) y76_downsample = DarknetConv2D_BN_Leaky(256, (3, 3), strides=(2, 2))(y76_downsample) y38 = Concatenate()([y76_downsample, y38]) y38 = DarknetConv2D_BN_Leaky(256, (1, 1))(y38) y38 = DarknetConv2D_BN_Leaky(512, (3, 3))(y38) y38 = DarknetConv2D_BN_Leaky(256, (1, 1))(y38) y38 = DarknetConv2D_BN_Leaky(512, (3, 3))(y38) y38 = DarknetConv2D_BN_Leaky(256, (1, 1))(y38) y38_output = DarknetConv2D_BN_Leaky(512, (3, 3))(y38) y38_output = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(y38_output) #19x19 output y38_downsample = ZeroPadding2D(((1, 0), (1, 0)))(y38) y38_downsample = DarknetConv2D_BN_Leaky(512, (3, 3), strides=(2, 2))(y38_downsample) y19 = Concatenate()([y38_downsample, y19]) y19 = DarknetConv2D_BN_Leaky(512, (1, 1))(y19) y19 = DarknetConv2D_BN_Leaky(1024, (3, 3))(y19) y19 = DarknetConv2D_BN_Leaky(512, (1, 1))(y19) y19 = DarknetConv2D_BN_Leaky(1024, (3, 3))(y19) y19 = DarknetConv2D_BN_Leaky(512, (1, 1))(y19) y19_output = DarknetConv2D_BN_Leaky(1024, (3, 3))(y19) y19_output = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(y19_output) yolo4_model = Model(inputs, [y19_output, y38_output, y76_output]) return yolo4_model
def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha, depth_multiplier=1, strides=(1, 1), block_id=1): """Adds a depthwise convolution block. A depthwise convolution block consists of a depthwise conv, batch normalization, relu6, pointwise convolution, batch normalization and relu6 activation. # Arguments inputs: Input tensor of shape `(rows, cols, channels)` (with `channels_last` data format) or (channels, rows, cols) (with `channels_first` data format). pointwise_conv_filters: Integer, the dimensionality of the output space (i.e. the number of output filters in the pointwise convolution). alpha: controls the width of the network. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. depth_multiplier: The number of depthwise convolution output channels for each input channel. The total number of depthwise convolution output channels will be equal to `filters_in * depth_multiplier`. strides: An integer or tuple/list of 2 integers, specifying the strides of the convolution along the width and height. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value != 1 is incompatible with specifying any `dilation_rate` value != 1. block_id: Integer, a unique identification designating the block number. # Input shape 4D tensor with shape: `(batch, channels, rows, cols)` if data_format='channels_first' or 4D tensor with shape: `(batch, rows, cols, channels)` if data_format='channels_last'. # Output shape 4D tensor with shape: `(batch, filters, new_rows, new_cols)` if data_format='channels_first' or 4D tensor with shape: `(batch, new_rows, new_cols, filters)` if data_format='channels_last'. `rows` and `cols` values might have changed due to stride. # Returns Output tensor of block. """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 pointwise_conv_filters = int(pointwise_conv_filters * alpha) if strides == (1, 1): x = inputs else: x = ZeroPadding2D(((0, 1), (0, 1)), name='conv_pad_%d' % block_id)(inputs) x = YoloDepthwiseConv2D((3, 3), padding='same' if strides == (1, 1) else 'valid', depth_multiplier=depth_multiplier, strides=strides, use_bias=False, name='conv_dw_%d' % block_id)(x) x = CustomBatchNormalization(axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x) x = ReLU(6., name='conv_dw_%d_relu' % block_id)(x) x = YoloConv2D(pointwise_conv_filters, (1, 1), padding='same', use_bias=False, strides=(1, 1), name='conv_pw_%d' % block_id)(x) x = CustomBatchNormalization(axis=channel_axis, name='conv_pw_%d_bn' % block_id)(x) return ReLU(6., name='conv_pw_%d_relu' % block_id)(x)
def MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000, **kwargs): """Instantiates the MobileNetV2 architecture. # Arguments input_shape: optional shape tuple, to be specified if you would like to use a model with an input img resolution that is not (224, 224, 3). It should have exactly 3 inputs channels (224, 224, 3). You can also omit this option if you would like to infer input_shape from an input_tensor. If you choose to include both input_tensor and input_shape then input_shape will be used if they match, if the shapes do not match then we will throw an error. E.g. `(160, 160, 3)` would be one valid value. alpha: controls the width of the network. This is known as the width multiplier in the MobileNetV2 paper, but the name is kept for consistency with MobileNetV1 in Keras. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. 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 block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, 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 or invalid alpha, rows when weights='imagenet' """ #global backend, layers, models, keras_utils #backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs) if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') 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') 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) # Determine proper input shape and default size. # If both input_shape and input_tensor are used, they should match #if input_shape is not None and input_tensor is not None: #try: #is_input_t_tensor = K.is_keras_tensor(input_tensor) #except ValueError: #try: #is_input_t_tensor = K.is_keras_tensor( #get_source_inputs(input_tensor)) #except ValueError: #raise ValueError('input_tensor: ', input_tensor, #'is not type input_tensor') #if is_input_t_tensor: #if K.image_data_format == 'channels_first': #if K.int_shape(input_tensor)[1] != input_shape[1]: #raise ValueError('input_shape: ', input_shape, #'and input_tensor: ', input_tensor, #'do not meet the same shape requirements') #else: #if K.int_shape(input_tensor)[2] != input_shape[1]: #raise ValueError('input_shape: ', input_shape, #'and input_tensor: ', input_tensor, #'do not meet the same shape requirements') #else: #raise ValueError('input_tensor specified: ', input_tensor, #'is not a keras tensor') # If input_shape is None, infer shape from input_tensor #if input_shape is None and input_tensor is not None: #try: #K.is_keras_tensor(input_tensor) #except ValueError: #raise ValueError('input_tensor: ', input_tensor, #'is type: ', type(input_tensor), #'which is not a valid type') #if input_shape is None and not K.is_keras_tensor(input_tensor): #default_size = 224 #elif input_shape is None and K.is_keras_tensor(input_tensor): #if K.image_data_format() == 'channels_first': #rows = K.int_shape(input_tensor)[2] #cols = K.int_shape(input_tensor)[3] #else: #rows = K.int_shape(input_tensor)[1] #cols = K.int_shape(input_tensor)[2] #if rows == cols and rows in [96, 128, 160, 192, 224]: #default_size = rows #else: #default_size = 224 # If input_shape is None and no input_tensor #elif input_shape is None: #default_size = 224 # If input_shape is not None, assume default size #else: #if K.image_data_format() == 'channels_first': #rows = input_shape[1] #cols = input_shape[2] #else: #rows = input_shape[0] #cols = input_shape[1] #if rows == cols and rows in [96, 128, 160, 192, 224]: #default_size = rows #else: #default_size = 224 # If input_shape is None and input_tensor is None using standard shape if input_shape is None and input_tensor is None: input_shape = (None, None, 3) if K.image_data_format() == 'channels_last': row_axis, col_axis = (0, 1) else: row_axis, col_axis = (1, 2) rows = input_shape[row_axis] cols = input_shape[col_axis] if weights == 'imagenet': if alpha not in [0.35, 0.50, 0.75, 1.0, 1.3, 1.4]: raise ValueError('If imagenet weights are being loaded, ' 'alpha can be one of `0.35`, `0.50`, `0.75`, ' '`1.0`, `1.3` or `1.4` only.') if rows != cols or rows not in [96, 128, 160, 192, 224]: rows = 224 warnings.warn('`input_shape` is undefined or non-square, ' 'or `rows` is not in [96, 128, 160, 192, 224].' ' Weights for input shape (224, 224) will be' ' loaded as the default.') 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 img_input = input_tensor channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 first_block_filters = _make_divisible(32 * alpha, 8) x = ZeroPadding2D(padding=correct_pad(K, img_input, 3), name='Conv1_pad')(img_input) x = YoloConv2D(first_block_filters, kernel_size=3, strides=(2, 2), padding='valid', use_bias=False, name='Conv1')(x) x = CustomBatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='bn_Conv1')(x) x = ReLU(6., name='Conv1_relu')(x) x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1, expansion=1, block_id=0) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, expansion=6, block_id=1) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, expansion=6, block_id=2) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, expansion=6, block_id=3) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=4) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=5) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=2, expansion=6, block_id=6) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=7) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=8) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=9) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=10) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=11) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=12) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=2, expansion=6, block_id=13) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=14) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=15) x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, expansion=6, block_id=16) # no alpha applied to last conv as stated in the paper: # if the width multiplier is greater than 1 we # increase the number of output channels if alpha > 1.0: last_block_filters = _make_divisible(1280 * alpha, 8) else: last_block_filters = 1280 x = YoloConv2D(last_block_filters, kernel_size=1, use_bias=False, name='Conv_1')(x) x = CustomBatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='Conv_1_bn')(x) x = ReLU(6., name='out_relu')(x) if include_top: x = GlobalAveragePooling2D()(x) x = Dense(classes, activation='softmax', use_bias=True, name='Logits')(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='mobilenetv2_%0.2f_%s' % (alpha, rows)) # Load weights. if weights == 'imagenet': if include_top: model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(alpha) + '_' + str(rows) + '.h5') weight_path = BASE_WEIGHT_PATH + model_name weights_path = get_file(model_name, weight_path, cache_subdir='models') else: model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(alpha) + '_' + str(rows) + '_no_top' + '.h5') weight_path = BASE_WEIGHT_PATH + model_name weights_path = get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def YOLOv3Net(cfgfile, model_size, num_classes): blocks = parse_cfg(cfgfile) outputs = {} output_filters = [] filters = [] out_pred = [] scale = 0 inputs = input_image = Input(shape=model_size) inputs = inputs / 255.0 for i, block in enumerate(blocks[1:]): # If it is a convolutional layer if (block["type"] == "convolutional"): activation = block["activation"] filters = int(block["filters"]) kernel_size = int(block["size"]) strides = int(block["stride"]) if strides > 1: inputs = ZeroPadding2D(((1, 0), (1, 0)))(inputs) inputs = Conv2D(filters, kernel_size, strides=strides, padding='valid' if strides > 1 else 'same', name='conv_' + str(i), use_bias=False if ("batch_normalize" in block) else True)(inputs) if "batch_normalize" in block: inputs = BatchNormalization(name='bnorm_' + str(i))(inputs) if activation == "leaky": inputs = LeakyReLU(alpha=0.1, name='leaky_' + str(i))(inputs) elif (block["type"] == "upsample"): stride = int(block["stride"]) inputs = UpSampling2D(stride)(inputs) # If it is a route layer elif (block["type"] == "route"): block["layers"] = block["layers"].split(',') start = int(block["layers"][0]) if len(block["layers"]) > 1: end = int(block["layers"][1]) - i filters = output_filters[i + start] + output_filters[ end] # Index negatif :end - index inputs = tf.concat([outputs[i + start], outputs[i + end]], axis=-1) else: filters = output_filters[i + start] inputs = outputs[i + start] elif block["type"] == "shortcut": from_ = int(block["from"]) inputs = outputs[i - 1] + outputs[i + from_] # Yolo detection layer elif block["type"] == "yolo": mask = block["mask"].split(",") mask = [int(x) for x in mask] anchors = block["anchors"].split(",") anchors = [int(a) for a in anchors] anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)] anchors = [anchors[i] for i in mask] n_anchors = len(anchors) out_shape = inputs.get_shape().as_list() inputs = tf.reshape(inputs, [-1, n_anchors * out_shape[1] * out_shape[2], \ 5 + num_classes]) box_centers = inputs[:, :, 0:2] box_shapes = inputs[:, :, 2:4] confidence = inputs[:, :, 4:5] classes = inputs[:, :, 5:num_classes + 5] box_centers = tf.sigmoid(box_centers) confidence = tf.sigmoid(confidence) classes = tf.sigmoid(classes) anchors = tf.tile(anchors, [out_shape[1] * out_shape[2], 1]) box_shapes = tf.exp(box_shapes) * tf.cast(anchors, dtype=tf.float32) x = tf.range(out_shape[1], dtype=tf.float32) y = tf.range(out_shape[2], dtype=tf.float32) cx, cy = tf.meshgrid(x, y) cx = tf.reshape(cx, (-1, 1)) cy = tf.reshape(cy, (-1, 1)) cxy = tf.concat([cx, cy], axis=-1) cxy = tf.tile(cxy, [1, n_anchors]) cxy = tf.reshape(cxy, [1, -1, 2]) strides = (input_image.shape[1] // out_shape[1], \ input_image.shape[2] // out_shape[2]) box_centers = (box_centers + cxy) * strides prediction = tf.concat( [box_centers, box_shapes, confidence, classes], axis=-1) if scale: out_pred = tf.concat([out_pred, prediction], axis=1) else: out_pred = prediction scale = 1 outputs[i] = inputs output_filters.append(filters) model = Model(input_image, out_pred) model.summary() return model
def baseModel(): 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))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Convolution2D(4096, (7, 7), activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(4096, (1, 1), activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(2622, (1, 1))) model.add(Flatten()) model.add(Activation('softmax')) return model