i = 0 for batch in datagen.flow(x, batch_size=1): plt.figure(i) imgplot = plt.imshow(image.array_to_img(batch[0])) i += 1 if i % 4 == 0: break plt.show() # %% model = models.Sequential() model.add( layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3))) model.add(layers.MaxPool2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPool2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPool2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPool2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dropout(0.5)) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer=optimizers.RMSprop(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy']) # %%
plt.show() chanDim = -1 latentDim = 700 input = layers.Input(shape=(SHAPE[0], SHAPE[1], depth)) x = input # apply a CONV => RELU => BN operation x = layers.Conv2D(filters=175, kernel_size=2, strides=1, activation="relu", padding="same")(x) x = layers.MaxPool2D(2)(x) x = layers.BatchNormalization(axis=chanDim)(x) x = layers.Conv2D(filters=250, kernel_size=3, strides=1, activation="relu", padding="same")(x) x = layers.MaxPool2D(2)(x) x = layers.BatchNormalization(axis=chanDim)(x) # flatten the network and then construct our latent vector volumeSize = K.int_shape(x) x = layers.Flatten()(x) latent = layers.Dense(latentDim)(x) # build the encoder model
def unet_model(): # declaring the input layer # Input layer expects an RGB image, in the original paper the network consisted of only one channel. inputs = layers.Input(shape=(572, 572, 3)) # first part of the U - contracting part c0 = layers.Conv2D(64, activation='relu', kernel_size=3)(inputs) c1 = layers.Conv2D(64, activation='relu', kernel_size=3)( c0) # This layer for concatenating in the expansive part c2 = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(c1) c3 = layers.Conv2D(128, activation='relu', kernel_size=3)(c2) c4 = layers.Conv2D(128, activation='relu', kernel_size=3)( c3) # This layer for concatenating in the expansive part c5 = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(c4) c6 = layers.Conv2D(256, activation='relu', kernel_size=3)(c5) c7 = layers.Conv2D(256, activation='relu', kernel_size=3)( c6) # This layer for concatenating in the expansive part c8 = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(c7) c9 = layers.Conv2D(512, activation='relu', kernel_size=3)(c8) c10 = layers.Conv2D(512, activation='relu', kernel_size=3)( c9) # This layer for concatenating in the expansive part c11 = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(c10) c12 = layers.Conv2D(1024, activation='relu', kernel_size=3)(c11) c13 = layers.Conv2D(1024, activation='relu', kernel_size=3, padding='valid')(c12) # We will now start the second part of the U - expansive part t01 = layers.Conv2DTranspose(512, kernel_size=2, strides=(2, 2), activation='relu')(c13) crop01 = layers.Cropping2D(cropping=(4, 4))(c10) concat01 = layers.concatenate([t01, crop01], axis=-1) c14 = layers.Conv2D(512, activation='relu', kernel_size=3)(concat01) c15 = layers.Conv2D(512, activation='relu', kernel_size=3)(c14) t02 = layers.Conv2DTranspose(256, kernel_size=2, strides=(2, 2), activation='relu')(c15) crop02 = layers.Cropping2D(cropping=(16, 16))(c7) concat02 = layers.concatenate([t02, crop02], axis=-1) c16 = layers.Conv2D(256, activation='relu', kernel_size=3)(concat02) c17 = layers.Conv2D(256, activation='relu', kernel_size=3)(c16) t03 = layers.Conv2DTranspose(128, kernel_size=2, strides=(2, 2), activation='relu')(c17) crop03 = layers.Cropping2D(cropping=(40, 40))(c4) concat03 = layers.concatenate([t03, crop03], axis=-1) c18 = layers.Conv2D(128, activation='relu', kernel_size=3)(concat03) c19 = layers.Conv2D(128, activation='relu', kernel_size=3)(c18) t04 = layers.Conv2DTranspose(64, kernel_size=2, strides=(2, 2), activation='relu')(c19) crop04 = layers.Cropping2D(cropping=(88, 88))(c1) concat04 = layers.concatenate([t04, crop04], axis=-1) c20 = layers.Conv2D(64, activation='relu', kernel_size=3)(concat04) c21 = layers.Conv2D(64, activation='relu', kernel_size=3)(c20) # This is based on our dataset. The output channels are 3, think of it as each pixel will be classified # into three classes, but I have written 4 here, as I do padding with 0, so we end up have four classes. outputs = layers.Conv2D(4, kernel_size=1)(c21) model = tf.keras.Model(inputs=inputs, outputs=outputs, name="u-netmodel") return model
activation = 'relu' # convultion layer, batch norm, max pool filter_num = 2 previousLayer = layers.Conv2D(filters=filter_num, kernel_size=(3, 3), padding='same')(previousLayer) previousLayer = layers.MaxPooling2D((2, 2))(previousLayer) temp_layer = previousLayer previousLayer, temp_layer = shortcut_block(previousLayer, temp_layer, filter_num, activation) previousLayer, temp_layer = shortcut_block(previousLayer, temp_layer, filter_num, activation) filter_num *= 2 previousLayer = layers.MaxPool2D((2, 2))(previousLayer) temp_layer = previousLayer previousLayer, temp_layer = shortcut_block(previousLayer, temp_layer, filter_num, activation) # average pool, flatten, then dense to instatntiate output layer outputLayer = layers.AveragePooling2D((2, 2))(previousLayer) outputLayer = layers.Flatten()(outputLayer) outputLayer = layers.Dense(outputDimension, activation='sigmoid', kernel_regularizer=regularizers.l2(0.001), name='outputLayer')(outputLayer) #compile model ourResnet = Model(inputs=inputLayer, outputs=[outputLayer], name='fashion_mlp') ourResnet.compile(loss='categorical_crossentropy', metrics=['acc'],
# add the identity link to the output of the convolution block x = layers.add([x, shortcut]) x = layers.ReLU()(x) return x # The input tensor inputs = layers.Input(shape=(224, 224, 3)) # First Convolutional layer, where pooled feature maps will be reduced by 75% x = layers.ZeroPadding2D(padding=(3, 3))(inputs) x = layers.Conv2D(64, kernel_size=(7, 7), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='he_normal')(x) x = layers.BatchNormalization()(x) x = layers.ReLU()(x) x = layers.ZeroPadding2D(padding=(1, 1))(x) x = layers.MaxPool2D(pool_size=(3, 3), strides=(2, 2))(x) x = conv_block(64, x, strides=(1,1)) # First Residual Block Group of 64 filters for _ in range(2): x = bottleneck_block(64, x) # Double the size of filters and reduce feature maps by 75% (strides=2, 2) to fit the next Residual Group x = conv_block(128, x) # Second Residual Block Group of 128 filters for _ in range(3): x = bottleneck_block(128, x) # Double the size of filters and reduce feature maps by 75% (strides=2, 2) to fit the next Residual Group
def __call__(self, input_t, **kwarg): resnet_layer_1 = residual_net(channel_size=32, kernel_size = (3, 3)) resnet_layer_2 = residual_net(channel_size=64, kernel_size = (3, 3)) resnet_layer_3 = residual_net(channel_size=128, kernel_size = (3, 3)) resnet_layer_4 = residual_net(channel_size=256, kernel_size = (3, 3)) resnet_layer_5 = residual_net(channel_size=32, kernel_size = (3, 3)) resnet_layer_6 = residual_net(channel_size=64, kernel_size = (3, 3)) resnet_layer_7 = residual_net(channel_size=128, kernel_size = (3, 3)) resnet_layer_8 = residual_net(channel_size=256, kernel_size = (3, 3)) resnet_layer_9 = residual_net(channel_size=512, kernel_size = (3, 3)) mhab_layer_1 = Multihead_Attention_Block_1() mhab_layer_2 = Multihead_Attention_Block_1() mhab_layer_3 = Multihead_Attention_Block_1() mhab_layer_4 = Multihead_Attention_Block_2() mhab_layer_5 = Multihead_Attention_Block_2() mhab_layer_6 = Multihead_Attention_Block_2() mhab_layer_7 = Multihead_Attention_Block_2() cnn_1D_layer = cnn_block_1D_seq(kernel_size=5, strides_size=2) cnn_1D_layer_2 = cnn_block_1D(kernel_size=5, strides_size=2) x = input_t # x = cnn_1D_layer(x) # x = cnn_1D_layer_2(x) print(x.shape) x = layers.Conv2D(32, (3, 3), padding='same')(x) # x = resnet_layer_1(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x = tf.nn.relu(x) x = layers.BatchNormalization()(x) # x = layers.Dropout(0.2)(x) x = layers.Conv2D(64, (3, 3), padding='same')(x) # x = resnet_layer_2(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x = tf.nn.relu(x) # x = layers.Dropout(0.2)(x) x = layers.BatchNormalization()(x) x = layers.Dropout(0.2)(x) x = layers.Conv2D(128, (3, 3), padding='same')(x) # x = resnet_layer_3(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x = layers.MaxPool2D((4, 4), padding='same')(x) # x = tf.nn.relu(x) x = layers.BatchNormalization()(x) # x = layers.Dropout(0.2)(x) x = layers.Conv2D(256, (3, 3), padding='same')(x) # x = resnet_layer_4(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x = layers.MaxPool2D((4, 4), padding='same')(x) # x = tf.nn.relu(x) # x = layers.Dropout(0.2)(x) x = layers.BatchNormalization()(x) x = layers.Dropout(0.2)(x) # x = layers.LayerNormalization()(x) y = self.convert_shape(x) # y = mhab_layer_1(y) # y = mhab_layer_2(y) # y = mhab_layer_3(y) y = mhab_layer_4(y) y = mhab_layer_5(y) y = mhab_layer_6(y) # y = mhab_layer_7(y) # z = layers.Conv2D(256, (3, 3), padding='same')(x) # z = tf.nn.relu(z) # z = layers.BatchNormalization()(z) # z = layers.Dropout(0.2)(z) # z = layers.Conv2D(256, (3, 3), padding='same')(z) # z = tf.nn.relu(z) # z = layers.Dropout(0.2)(z) # z = layers.BatchNormalization()(z) # z = layers.Dropout(0.2)(z) z = resnet_layer_8(x) # z = layers.BatchNormalization()(z) # z = resnet_layer_4(z) # z = layers.LayerNormalization()(z) # z = layers.Dropout(0.2)(z) z = self.convert_shape(z) x = tf.math.add(y, z) # w = tf.math.add(y, z) # x = tf.math.add(v, w) x = tf.nn.relu(x) x = layers.BatchNormalization()(x) # x = layers.LayerNormalization()(x) # x = layers.LSTM(128, return_sequences=True)(x) x = layers.LSTM(256)(x) # x = layers.Bidirectional(layers.LSTM(128))(x) # x = layers.GlobalAveragePooling2D()(x) x = layers.Flatten()(x) # x = layers.Dropout(0.2)(x) # x = layers.Dense(256, activation='linear')(x) x = layers.Dropout(0.3)(x) # x = layers.Dense(128, activation='relu')(x) # x = layers.Dropout(0.2)(x) # x = layers.BatchNormalization()(x) return x
def DenoisingAutoencoderModel(input_shape): filters = [128, 256, 512] encoding_layers = 3 inputs = layers.Input(input_shape) x = inputs x_res = [inputs] # Encoding part for i in range(encoding_layers): x = DepthSeparableBlock(x, filters[i], name="conv" + str(i) + "a_") x = layers.MaxPool2D(pool_size=(2, 2), padding="same")( x) ### TO BE REPLACED WITH STRIDE IN THE CONV x = DepthSeparableBlock(x, filters[i], name="conv" + str(i) + "b_") x = layers.MaxPool2D(pool_size=(2, 2), padding="same")(x) x_res.append(x) # Encoded Representation x = DepthSeparableBlock(x, 1024, name="conv5_", activation=False) # Decoding Part for i in range(encoding_layers): j = encoding_layers - i x = DepthSeparableBlock(x, filters[j - 1], name="conv" + str(i + encoding_layers + 2) + "a_", activation=False) # Residual Addition crop = CalculateCrop( tf.keras.backend.int_shape(x)[1:3], tf.keras.backend.int_shape(x_res[j])[1:3]) x = layers.Cropping2D(crop)(x) x = layers.Add()([x, x_res[j]]) x = layers.Activation(relu)(x) x = layers.UpSampling2D(size=(2, 2))(x) x = DepthSeparableBlock(x, filters[j - 1], name="conv" + str(i + encoding_layers + 2) + "b_") x = layers.UpSampling2D(size=(2, 2))(x) print(tf.keras.backend.int_shape(x)) x = layers.Conv2D(input_shape[-1], kernel_size=(1, 1), name="PointWise_conv_LastResidual", kernel_regularizer=l2(weight_decay))(x) crop = CalculateCrop( tf.keras.backend.int_shape(x)[1:3], tf.keras.backend.int_shape(x_res[0])[1:3]) x = layers.Cropping2D(crop)(x) x = layers.Add()([x, x_res[0]]) x = layers.Activation(relu)(x) outputs = layers.Conv2D(1, kernel_size=(1, 1), name="Final_PoinWise_convolution", kernel_regularizer=l2(weight_decay))(x) return Model(inputs, outputs)
def GoogLeNet(height, width, channel, class_num, aux_logits=False): """ 建立DenseNet网络,只需要调节是否使用辅助分类器 :param height: 图像的高 :param width: 图像的宽 :param channel: 图像的通道数 :param class_num: 分类的数量 :param aux_logits: 是否使用辅助分类器 :return: """ input_image = layers.Input(shape=(height, width, channel), dtype="float32") # (None, 224, 224, 3) x = layers.Conv2D(64, kernel_size=7, strides=2, padding='SAME', activation='relu', name='conv2d_1')(input_image) # (None, 112, 112, 64) x = layers.MaxPool2D(pool_size=3, strides=2, padding='SAME', name='maxpool_1')(x) # (None, 56, 56, 64) x = layers.Conv2D(64, kernel_size=1, strides=1, activation='relu', name='conv2d_2')(x) # (None, 56, 56, 64) x = layers.Conv2D(192, kernel_size=3, strides=1, padding='SAME', activation='relu', name='conv2d_3')(x) # (None, 56, 56, 192) x = layers.MaxPool2D(pool_size=3, strides=2, padding='SAME', name='maxpool_2')(x) # (None, 28, 28, 192) x = Inception(64, 96, 128, 16, 32, 32, name='inception_3a')(x) # (None, 28, 28, 256=64+128+32+32) x = Inception(128, 128, 192, 32, 96, 64, name='inception_3b')(x) # (None, 28, 28, 480) x = layers.MaxPool2D(pool_size=3, strides=2, padding='SAME', name='maxpool_3')(x) # (None, 14, 14, 480) x = Inception(192, 96, 208, 16, 48, 64, name='inception_4a')(x) # 辅助分类器1 if aux_logits: aux1 = InceptionAux(class_num, name='aux_1')(x) # (None, 14, 14, 512) x = Inception(160, 112, 224, 24, 64, 64, name='inception_4b')(x) # (None, 14, 14, 512) x = Inception(128, 128, 256, 24, 64, 64, name='inception_4c')(x) # (None, 14, 14, 512) x = Inception(112, 144, 288, 32, 64, 64, name='inception_4d')(x) # 辅助分类器2 if aux_logits: aux2 = InceptionAux(class_num, name='aux_2')(x) # (None, 14, 14, 528) x = Inception(256, 160, 320, 32, 128, 128, name='inception_4e')(x) # (None, 14, 14, 832) x = layers.MaxPool2D(pool_size=3, strides=2, padding='SAME', name='maxpool_4')(x) # (None, 7, 7, 832) x = Inception(256, 160, 320, 32, 128, 128, name='inception_5a')(x) # (None, 7, 7, 832) x = Inception(384, 192, 384, 48, 128, 128, name='inception_5b')(x) # (None, 7, 7, 1024) x = layers.AvgPool2D(pool_size=7, strides=1, name='avgpool')(x) # (None, 1, 1, 1024) x = layers.Flatten(name='output_flatten')(x) x = layers.Dropout(rate=0.4, name='output_dropout')(x) # (None * 1024) x = layers.Dense(class_num, activation='relu')(x) # (None, class_num) output = layers.Softmax()(x) if aux_logits: model = models.Model(inputs=input_image, outputs=[aux1, aux2, output]) else: model = models.Model(inputs=input_image, outputs=output) model.summary() return model
# generate X and Y (inputs and labels). x_train = np.concatenate([songs_train_set, vox_train_set]) labels_train = np.asarray([1 for _ in range(SAMPLE_SIZE)] + [0 for _ in range(SAMPLE_SIZE)]) x_valid = np.concatenate([songs_valid_set, vox_valid_set]) labels_valid = np.asarray([1 for _ in range(valid_size)] + [0 for _ in range(valid_size)]) fc_layer_size = 128 img_size = IMG_SIZE conv_inputs = keras.Input(shape=(img_size[1], img_size[0], 4), name='ani_image') conv_layer = layers.Conv2D(24, kernel_size=3, activation='relu')(conv_inputs) conv_layer = layers.MaxPool2D(pool_size=(2, 2))(conv_layer) conv_x = layers.Flatten(name='flattened_features')( conv_layer) #turn image to vector. conv_x = layers.Dense(fc_layer_size, activation='relu', name='first_layer')(conv_x) conv_x = layers.Dense(fc_layer_size, activation='relu', name='second_layer')(conv_x) conv_outputs = layers.Dense(1, activation='sigmoid', name='class')(conv_x) conv_model = keras.Model(inputs=conv_inputs, outputs=conv_outputs) customAdam = keras.optimizers.Adam(lr=1e-6) conv_model.compile( optimizer=customAdam, # Optimizer # Loss function to minimize
def __init__(self, block, layers, classes=1000, data_format='channels_last', dilated=False, norm_layer=nn.BatchNormalization, norm_kwargs={}, last_gamma=False, deep_stem=False, stem_width=32, avg_down=False, final_drop=0.0, name_prefix='', **kwargs): self.inplanes = stem_width * 2 if deep_stem else 64 self.data_format = data_format super(ResNetV1b, self).__init__(name=name_prefix) self.norm_kwargs = norm_kwargs with tf.name_scope(self.name): if not deep_stem: self.conv1 = nn.Conv2D(filters=64, kernel_size=7, strides=2, padding='same', use_bias=False, data_format=data_format) else: self.conv1 = ['conv1'] self.conv1.append( nn.Conv2D(filters=stem_width, kernel_size=3, strides=2, padding='same', use_bias=False, data_format=data_format)) self.conv1.append(norm_layer(**norm_kwargs)) self.conv1.append(nn.Activation('relu')) self.conv1.append( nn.Conv2D(filters=stem_width, kernel_size=3, strides=1, padding='same', use_bias=False, data_format=data_format)) self.conv1.append(norm_layer(**norm_kwargs)) self.conv1.append(nn.Activation('relu')) self.conv1.append( nn.Conv2D(filters=stem_width * 2, kernel_size=3, strides=1, padding='same', use_bias=False, data_format=data_format)) self.bn1 = norm_layer(**norm_kwargs) self.relu = nn.Activation('relu') self.maxpool = nn.MaxPool2D(pool_size=3, strides=2, padding='same', data_format=data_format) self.layer1 = self._make_layer(1, block, 64, layers[0], avg_down=avg_down, norm_layer=norm_layer, last_gamma=last_gamma, data_format=data_format) self.layer2 = self._make_layer(2, block, 128, layers[1], strides=2, avg_down=avg_down, norm_layer=norm_layer, last_gamma=last_gamma, data_format=data_format) if dilated: self.layer3 = self._make_layer(3, block, 256, layers[2], strides=1, dilation=2, avg_down=avg_down, norm_layer=norm_layer, last_gamma=last_gamma, data_format=data_format) self.layer4 = self._make_layer(4, block, 512, layers[3], strides=1, dilation=4, avg_down=avg_down, norm_layer=norm_layer, last_gamma=last_gamma, data_format=data_format) else: self.layer3 = self._make_layer(3, block, 256, layers[2], strides=2, avg_down=avg_down, norm_layer=norm_layer, last_gamma=last_gamma, data_format=data_format) self.layer4 = self._make_layer(4, block, 512, layers[3], strides=2, avg_down=avg_down, norm_layer=norm_layer, last_gamma=last_gamma, data_format=data_format) self.avgpool = nn.GlobalAveragePooling2D(data_format=data_format) self.flat = nn.Flatten() self.drop = None if final_drop > 0.0: self.drop = nn.Dropout(final_drop) self.fc = nn.Dense(units=classes)