def U_net(input_size = (256,256,1)): N = input_size[0] inputs = Input(input_size) 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) outputs = layers.Conv2D(2, kernel_size=1)(c21) model = Model(inputs,outputs) model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy']) return model
def transform_net(img_width,img_height): input_tensor = layers.Input(shape=(img_width,img_height,3)) input_tensor1 = InputNormalize()(input_tensor) x = layers.Conv2D(32, kernel_size = (9,9), strides = (1,1), padding = 'same')(input_tensor1) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2D(64, kernel_size = (3,3), strides = (2,2), padding = 'same')(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2D(128, kernel_size = (3,3), strides = (2,2), padding = 'same')(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = residual_block(x) x = residual_block(x) x = residual_block(x) x = residual_block(x) x = residual_block(x) x = layers.Conv2DTranspose(64, kernel_size = (3,3), strides = (2,2), padding = 'same')(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2DTranspose(32, kernel_size = (3,3), strides = (2,2), padding = 'same')(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2DTranspose(3, kernel_size = (9,9), strides = (1,1), padding = 'same')(x) x = layers.BatchNormalization()(x) output_tensor = layers.Activation('tanh')(x) output_tensor2 = Denormalize()(output_tensor) model = Model(inputs = input_tensor,outputs = output_tensor2) return model
def build_generator_gan(img_shape, noise_shape = (100,)): ''' noise_shape : the dimension of the input vector for the generator_gan img_shape : the dimension of the output ''' input_noise = layers.Input(shape=noise_shape) d = layers.Dense(1024, activation="relu")(input_noise) d = layers.Dense(1024, activation="relu")(input_noise) d = layers.Dense(128*8*8, activation="relu")(d) d = layers.Reshape((8,8,128))(d) d = layers.BatchNormalization()(d) d = layers.Dropout(0.2)(d) d = layers.Conv2DTranspose(128, kernel_size=(2,2) , strides=(2,2) , use_bias=False)(d) d = layers.Conv2D( 64 , ( 1 , 1 ) , activation='relu' , padding='same', name="block_4")(d) ## 16,16 d = layers.Conv2DTranspose(32, kernel_size=(2,2) , strides=(2,2) , use_bias=False)(d) d = layers.Conv2D( 64 , ( 1 , 1 ) , activation='relu' , padding='same', name="block_5")(d) ## 32,32 if img_shape[0] == 64: d = layers.Conv2DTranspose(32, kernel_size=(2,2) , strides=(2,2) , use_bias=False)(d) d = layers.Conv2D( 64 , ( 1 , 1 ) , activation='relu' , padding='same', name="block_6")(d) ## 64,64 img = layers.Conv2D( 3 , ( 1 , 1 ) , activation='sigmoid' , padding='same', name="final_block")(d) ## 32, 32 model = models.Model(input_noise, img) model.summary() return(model)
def simpledeconv_b(**params): thought_vector_size = params['thought_vector_size'] img_width = params['img_width_decoder'] x_input = layers.Input(shape=(thought_vector_size, )) # Expand vector from 1x1 to 4x4 N = 4 depth = 1024 x = layers.Reshape((1, 1, -1))(x_input) x = layers.Conv2DTranspose(depth, (N, N), strides=(N, N), padding='same')(x) x = layers.BatchNormalization()(x) x = layers.LeakyReLU()(x) # Upsample to the desired width (powers of 2 only) while N < img_width: depth /= 2 N *= 2 x = layers.Conv2DTranspose(depth, (5, 5), strides=(2, 2), padding='same')(x) x = layers.BatchNormalization()(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(3, (3, 3), padding='same')(x) x = layers.Activation('sigmoid')(x) return models.Model(inputs=[x_input], outputs=x)
def __init__(self): super(Denoise, self).__init__() self.encoder = tf.keras.Sequential([ layers.Input(shape=(80, 80, 3)), layers.Conv2D(16, (3, 3), activation='relu', padding='same', strides=2), layers.Conv2D(8, (3, 3), activation='relu', padding='same', strides=2), layers.Conv2D(4, (3, 3), activation='relu', padding='same', strides=2), layers.Flatten(), layers.Dense(64) ]) self.decoder = tf.keras.Sequential([ layer.Input(self.encoder.output_shape) layers.Dense(400), layers.Reshape((10, 10)), layers.Conv2DTranspose(4, kernel_size=3, strides=2, activation='relu', padding='same'), layers.Conv2DTranspose(8, kernel_size=3, strides=2, activation='relu', padding='same'), layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation='relu', padding='same'), layers.Conv2D(3, kernel_size=(3, 3), activation='sigmoid', padding='same')])
def deconvolution_network(x): x = layers.Conv2DTranspose(num_bodyparts * 5, kernel_size=3, strides=2, padding='same', kernel_regularizer=l2(weight_regularization), bias_regularizer=l2(weight_regularization))(x) x = layers.Conv2DTranspose(num_bodyparts * 4, kernel_size=3, strides=2, padding='same', kernel_regularizer=l2(weight_regularization), bias_regularizer=l2(weight_regularization))(x) x = layers.Conv2DTranspose(num_bodyparts * 3, kernel_size=3, strides=2, padding='same', kernel_regularizer=l2(weight_regularization), bias_regularizer=l2(weight_regularization))(x) x = layers.Conv2DTranspose(num_bodyparts * 2, kernel_size=3, strides=2, padding='same', kernel_regularizer=l2(weight_regularization), bias_regularizer=l2(weight_regularization))(x) x = layers.Conv2DTranspose(num_bodyparts, kernel_size=3, strides=2, padding='same', kernel_regularizer=l2(weight_regularization), bias_regularizer=l2(weight_regularization), activation='sigmoid', name="deconvolution_network_output")(x) return x
def __call__(self, inputs): if self.mask: masked = layers.Lambda(capsule.mask)(inputs) masked = layers.Flatten()(masked) elif K.ndim(inputs) > 2: masked = layers.Flatten()(inputs) else: masked = inputs if self.conv: fc = layers.Dense(256, activation='relu')(masked) reshape = layers.Reshape((8, 8, 4))(fc) up1 = layers.Conv2DTranspose(256, 9, strides=2, activation='relu', padding='valid')(reshape) pad = layers.Lambda( lambda x: K.spatial_2d_padding(x, ((0, 1), (0, 1))))(up1) up2 = layers.Conv2DTranspose(256, 9, activation='relu', padding='valid')(pad) outputs = layers.Conv2D(self.image_shape[-1], 1, activation='sigmoid', name='reconstruction')(up2) else: up1 = layers.Dense(512, activation='relu')(masked) up2 = layers.Dense(1024, activation='relu')(up1) outputs = layers.Dense(np.prod(self.image_shape), activation='sigmoid')(up2) outputs = layers.Reshape(self.image_shape, name='reconstruction')(outputs) return outputs
def build_decoder(**params): thought_vector_size = params['thought_vector_size'] csr_size = params['csr_size'] csr_layers = params['csr_layers'] img_width = params['img_width'] x_input = layers.Input(shape=(thought_vector_size, )) # Expand vector from 1x1 to 4x4 N = 4 x = layers.Reshape((1, 1, -1))(x_input) x = layers.Conv2DTranspose(128, (N, N), strides=(N, N), padding='same')(x) x = layers.BatchNormalization()(x) x = layers.Activation(LeakyReLU())(x) # Upsample to the desired width (powers of 2 only) while N < img_width: x = layers.Conv2DTranspose(256, (5, 5), strides=(2, 2), padding='same')(x) x = layers.BatchNormalization()(x) x = layers.Activation(LeakyReLU())(x) if csr_layers > 0: x = QuadCSR(csr_size)(x) csr_layers -= 1 N *= 2 x = layers.Conv2D(3, (3, 3), padding='same')(x) x = layers.Activation('sigmoid')(x) return models.Model(inputs=[x_input], outputs=x)
def build_deep_autoencoder(img_shape, code_size): """ Deep autoencoding """ H,W,C = img_shape # encoder encoder = keras.models.Sequential() encoder.add(L.InputLayer(img_shape)) encoder.add(L.Conv2D(32, (3, 3), strides = (1, 1), padding="same", activation="elu")) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(64, (3, 3), strides = (1, 1), padding="same", activation="elu")) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(128, (3, 3), strides = (1, 1), padding="same", activation="elu")) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(256, (3, 3), strides = (1, 1), padding="same", activation="elu")) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Flatten()) # flatten image to vector encoder.add(L.Dense(code_size)) # actual encoder # decoder decoder = keras.models.Sequential() decoder.add(L.InputLayer((code_size,))) decoder.add(L.Dense(2*2*256)) #actual encoder decoder.add(L.Reshape((2,2,256))) #un-flatten decoder.add(L.Conv2DTranspose(filters=128, kernel_size=(3, 3), strides=2, activation="elu", padding="same")) decoder.add(L.Conv2DTranspose(filters=64, kernel_size=(3, 3), strides=2, activation="elu", padding="same")) decoder.add(L.Conv2DTranspose(filters=32, kernel_size=(3, 3), strides=2, activation="elu", padding="same")) decoder.add(L.Conv2DTranspose(filters=3, kernel_size=(3, 3), strides=2, activation=None, padding="same")) return encoder, decoder
def test(): c1 = valid_c(82) c2 = same_c(c1) c3 = valid_ct(c2) c4 = same_ct(c3) print(c1, c2, c3, c4) input = Input(shape=(82, 82, 3)) y = layers.Conv2D( 16, c_size, strides=s_long, activation='relu', )(input) y = layers.Conv2D(16, c_size, strides=s_long, activation='relu', padding='same')(y) y = layers.Conv2DTranspose(32, c_size, strides=s_long, activation='relu')(y) y = layers.Conv2DTranspose(32, c_size, strides=s_long, activation='relu', padding='same')(y) model = Model(input, y) model.summary()
def init_model(): model = models.Sequential() model.add( layers.Conv2DTranspose(18, strides=1, kernel_size=(3, 3), padding="same", input_shape=(28, 28, 3))) model.add( layers.Conv2DTranspose(9, strides=2, kernel_size=(3, 3), padding="same")) model.add( layers.Conv2DTranspose(6, strides=2, kernel_size=(3, 3), padding="same")) model.add( layers.Conv2DTranspose(3, strides=2, kernel_size=(3, 3), padding="same")) model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"]) return model
def get_generator(initial_filters=64): # inputs = layers.Input(shape=(256, 256, 3), name='generator_input') inputs = layers.Input(shape=(None, None, 3), name='generator_input') i64 = initial_filters i128 = i64 * 2 i256 = i64 * 4 x = ReflectionPadding2D((3, 3))(inputs) x = conv2d_unit(x, i64, 7, padding='valid', name='conv01_1') x = InstanceNormalization(name='in01_1')(x) x = layers.ReLU()(x) ''' pytorch的模型移植到keras。 kernel_size=3, stride=2, padding=1 --> 前面要加 ZeroPadding2D(1),且kernel_size=3, strides=2, padding='valid' kernel_size=3, stride=1, padding=1 --> 不用加 ZeroPadding2D(1), 且kernel_size=3, strides=1, padding='same' ''' x = layers.ZeroPadding2D(1)(x) x = conv2d_unit(x, i128, 3, strides=2, padding='valid', name='conv02_1') x = conv2d_unit(x, i128, 3, strides=1, padding='same', name='conv02_2') x = InstanceNormalization(name='in02_1')(x) x = layers.ReLU()(x) x = layers.ZeroPadding2D(1)(x) x = conv2d_unit(x, i256, 3, strides=2, padding='valid', name='conv03_1') x = conv2d_unit(x, i256, 3, strides=1, padding='same', name='conv03_2') x = InstanceNormalization(name='in03_1')(x) x = layers.ReLU()(x) # 残差部分 for i in range(8): shortcut = x x = ReflectionPadding2D((1, 1))(x) x = conv2d_unit(x, i256, 3, padding='valid', name='conv%.2d_1' % (4+i)) x = InstanceNormalization(name='in%.2d_1' % (4+i))(x) x = layers.ReLU()(x) x = ReflectionPadding2D((1, 1))(x) x = conv2d_unit(x, i256, 3, padding='valid', name='conv%.2d_2' % (4+i)) x = InstanceNormalization(name='in%.2d_2' % (4+i))(x) x = layers.add([x, shortcut]) # 上采样 x = layers.Conv2DTranspose(i128, 3, strides=2, padding='same', output_padding=1, name='deconv01_1')(x) x = conv2d_unit(x, i128, 3, strides=1, padding='same', name='deconv01_2') x = InstanceNormalization(name='in12_1')(x) x = layers.ReLU()(x) x = layers.Conv2DTranspose(i64, 3, strides=2, padding='same', output_padding=1, name='deconv02_1')(x) x = conv2d_unit(x, i64, 3, strides=1, padding='same', name='deconv02_2') x = InstanceNormalization(name='in13_1')(x) x = layers.ReLU()(x) x = ReflectionPadding2D((3, 3))(x) x = conv2d_unit(x, 3, 7, padding='valid', name='deconv03_1') x = layers.Activation('tanh', name='generator_output')(x) model = keras.models.Model(inputs=inputs, outputs=x, name='generator') return model
def generator_network(x): def add_common_layers(y): y = layers.advanced_activations.LeakyReLU()(y) y = layers.Dropout(0.25)(y) return y x = layers.Dense(1024)(x) x = add_common_layers(x) # # input dimensions to the first de-conv layer in the generator # height_dim = 7 width_dim = 7 assert img_height % height_dim == 0 and img_width % width_dim == 0, \ 'Generator network must be able to transform `x` into a tensor of shape (img_height, img_width, img_channels).' x = layers.Dense(height_dim * width_dim * 128)(x) x = add_common_layers(x) x = layers.Reshape((height_dim, width_dim, -1))(x) x = layers.Conv2DTranspose(64, kernel_size, **conv_layer_keyword_args)(x) x = add_common_layers(x) # number of feature maps => number of image channels return layers.Conv2DTranspose(img_channels, 1, strides=2, padding='same', activation='tanh')(x)
def build_deep_autoencoder(img_shape, code_size): """PCA's deeper brother. See instructions above. Use `code_size` in layer definitions.""" H,W,C = img_shape # encoder encoder = keras.models.Sequential() encoder.add(L.InputLayer(img_shape)) ### YOUR CODE HERE: define encoder as per instructions above ### encoder.add(L.Conv2D(32, (3, 3), strides = (1, 1), padding="same", activation='elu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(64, (3, 3), strides = (1, 1), padding="same", activation='elu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(128, (3, 3), strides = (1, 1), padding="same", activation='elu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(256, (3, 3), strides = (1, 1), padding="same", activation='elu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Flatten()) #flatten image to vector encoder.add(L.Dense(code_size)) #actual encoder # decoder decoder = keras.models.Sequential() decoder.add(L.InputLayer((code_size,))) ### YOUR CODE HERE: define decoder as per instructions above ### decoder.add(L.Dense(2*2*256)) #actual encoder decoder.add(L.Reshape((2,2,256))) #un-flatten decoder.add(L.Conv2DTranspose(filters=128, kernel_size=(3, 3), strides=2, activation='elu', padding='same')) decoder.add(L.Conv2DTranspose(filters=64, kernel_size=(3, 3), strides=2, activation='elu', padding='same')) decoder.add(L.Conv2DTranspose(filters=32, kernel_size=(3, 3), strides=2, activation='elu', padding='same')) decoder.add(L.Conv2DTranspose(filters=3, kernel_size=(3, 3), strides=2, activation=None, padding='same')) return encoder, decoder
def fconv_e(inputs, contract, stride, filters, alpha=1): in_nfilters = backend.int_shape(inputs)[-1] out_nfilters = int(alpha * filters) channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1 # Expand x = layers.Conv2DTranspose(int(in_nfilters * contract), kernel_size=(5, 5), strides=5, padding='same', use_bias=False, activation=None)(inputs) x = layers.Activation('tanh')(x) # Contract x = layers.Conv2D(in_nfilters, kernel_size=(5, 5), strides=5, padding='same', use_bias=False, activation=None)(x) x = layers.Activation('tanh')(x) x = layers.Subtract()([inputs, x]) # Reduce/increase x = layers.Conv2DTranspose(out_nfilters, kernel_size=(5, 5), padding='same', strides=stride, use_bias=False, activation=None)(x) x = layers.Activation('linear')(x) # This layers does not have an activation function, it is linear return x
def build_upSample(resnext_out, stored_middle_layers): deconv1 = layers.Conv2DTranspose(1024, kernel_size=(1, 1), dilation_rate=(2, 2), strides=(2, 2))(resnext_out) concat1 = layers.Concatenate()([stored_middle_layers[3], deconv1]) add_common_layers(concat1) deconv2 = layers.Conv2DTranspose(512, kernel_size=(1, 1), dilation_rate=(2, 2), strides=(2, 2))(concat1) concat2 = layers.Concatenate()([stored_middle_layers[2], deconv2]) add_common_layers(concat2) deconv3 = layers.Conv2DTranspose(256, kernel_size=(1, 1), dilation_rate=(2, 2), strides=(2, 2))(concat2) concat3 = layers.Concatenate()([stored_middle_layers[1], deconv3]) add_common_layers(concat3) deconv4 = layers.Conv2DTranspose(128, kernel_size=(1, 1), dilation_rate=(2, 2), strides=(2, 2))(concat3) concat4 = layers.Concatenate()([stored_middle_layers[0], deconv4]) add_common_layers(concat4) final_upsample = layers.Conv2DTranspose(1, kernel_size=(1, 1), strides=(2, 2), activation='sigmoid')(concat4) return final_upsample
def create_generator(): img = L.Input(shape=(100, )) x = L.Dense(units=4 * 4 * 1024)(img) x = L.LeakyReLU()(x) x = L.Reshape(target_shape=(4, 4, 1024))(x) x = L.Conv2DTranspose(filters=512, kernel_size=(5, 5), padding='same', strides=(1, 1))(x) x = L.LeakyReLU()(x) x = L.Conv2DTranspose(filters=256, kernel_size=(5, 5), padding='same', strides=(2, 2))(x) x = L.LeakyReLU()(x) x = L.Conv2DTranspose(filters=128, kernel_size=(5, 5), padding='same', strides=(2, 2))(x) x = L.LeakyReLU()(x) x = L.Conv2DTranspose(filters=64, kernel_size=(5, 5), padding='same', strides=(2, 2))(x) x = L.LeakyReLU()(x) x = L.Conv2DTranspose(filters=3, kernel_size=(5, 5), padding='same', strides=(2, 2))(x) x = L.Activation('tanh')(x) gen = Model(inputs=img, outputs=x) return gen
def get_model(img_size, num_classes): inputs = keras.Input(shape=img_size + (3, )) #(img_size + (3,)) = (160, 160, 3) ### [First half of the network: downsampling inputs] ### # Entry block x = layers.Conv2D(32, 3, strides=2, padding="same")(inputs) x = layers.BatchNormalization()(x) x = layers.Activation("relu")(x) previous_block_activation = x # Set aside residual # Blocks 1, 2, 3 are identical apart from the feature depth. for filters in [64, 128, 256, 512]: x = layers.Activation("relu")(x) x = layers.SeparableConv2D(filters, 3, padding="same")(x) x = layers.BatchNormalization()(x) x = layers.Activation("relu")(x) x = layers.SeparableConv2D(filters, 3, padding="same")(x) x = layers.BatchNormalization()(x) x = layers.MaxPooling2D(3, strides=2, padding="same")(x) # Project residual residual = layers.Conv2D(filters, 1, strides=2, padding="same")(previous_block_activation) x = layers.add([x, residual]) # Add back residual previous_block_activation = x # Set aside next residual ### [Second half of the network: upsampling inputs] ### for filters in [512, 256, 128, 64, 32]: x = layers.Activation("relu")(x) x = layers.Conv2DTranspose(filters, 3, padding="same")(x) x = layers.BatchNormalization()(x) x = layers.Activation("relu")(x) x = layers.Conv2DTranspose(filters, 3, padding="same")(x) x = layers.BatchNormalization()(x) x = layers.UpSampling2D(2)(x) # Project residual residual = layers.UpSampling2D(2)(previous_block_activation) residual = layers.Conv2D(filters, 1, padding="same")(residual) x = layers.add([x, residual]) # Add back residual previous_block_activation = x # Set aside next residual # Add a per-pixel classification layer outputs = layers.Conv2D(num_classes, 3, activation="softmax", padding="same")(x) # Define the model model = keras.Model(inputs, outputs) return model
def generator(d=128, image_shape=[64, 64, 3]): conv_options = { 'kernel_initializer': initializers.normal(mean=0.0, stddev=0.02), } batchnor_options = { 'gamma_initializer': initializers.normal(mean=0.1, stddev=0.02), 'beta_initializer': initializers.constant(0), 'momentum': 0.9 } inputs = layers.Input([ 100, ]) s_h, s_w = image_shape[0], image_shape[1] s_h2, s_w2 = conv_out_size_same(s_h, 2), conv_out_size_same(s_w, 2) s_h4, s_w4 = conv_out_size_same(s_h2, 2), conv_out_size_same(s_w2, 2) s_h8, s_w8 = conv_out_size_same(s_h4, 2), conv_out_size_same(s_w4, 2) s_h16, s_w16 = conv_out_size_same(s_h8, 2), conv_out_size_same(s_w8, 2) x = layers.Dense(s_h16 * s_w16 * d * 8, **conv_options)(inputs) x = layers.Reshape([s_h16, s_w16, d * 8])(x) x = layers.BatchNormalization(**batchnor_options)(x) x = layers.Activation("relu")(x) x = layers.Conv2DTranspose(filters=d * 4, kernel_size=4, strides=2, padding="same", **conv_options)(x) x = layers.BatchNormalization(**batchnor_options)(x) x = layers.Activation("relu")(x) x = layers.Conv2DTranspose(filters=d * 2, kernel_size=4, strides=2, padding="same", **conv_options)(x) x = layers.BatchNormalization(**batchnor_options)(x) x = layers.Activation("relu")(x) x = layers.Conv2DTranspose(filters=d, kernel_size=4, strides=2, padding="same", **conv_options)(x) x = layers.BatchNormalization(**batchnor_options)(x) x = layers.Activation("relu")(x) x = layers.Conv2DTranspose(filters=3, kernel_size=4, strides=2, padding="same", **conv_options)(x) x = layers.Activation("tanh")(x) model = Model(inputs, x) return model
def create_model(self, num_class=3) : concat_axis = 3 inputs = layers.Input(shape = self.input_shape) s = layers.core.Lambda(lambda x: x / 255) (inputs) c1 = layers.Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (s) c1 = layers.Dropout(0.1) (c1) c1 = layers.Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c1) p1 = layers.MaxPooling2D((2, 2)) (c1) c2 = layers.Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p1) c2 = layers.Dropout(0.1) (c2) c2 = layers.Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c2) p2 = layers.MaxPooling2D((2, 2)) (c2) c3 = layers.Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p2) c3 = layers.Dropout(0.2) (c3) c3 = layers.Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c3) p3 = layers.MaxPooling2D((2, 2)) (c3) c4 = layers.Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p3) c4 = layers.Dropout(0.2) (c4) c4 = layers.Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c4) p4 = layers.MaxPooling2D(pool_size=(2, 2)) (c4) c5 = layers.Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p4) c5 = layers.Dropout(0.3) (c5) c5 = layers.Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c5) u6 = layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same') (c5) u6 = layers.concatenate([u6, c4]) c6 = layers.Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u6) c6 = layers.Dropout(0.2) (c6) c6 = layers.Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c6) u7 = layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same') (c6) u7 = layers.concatenate([u7, c3]) c7 = layers.Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u7) c7 = layers.Dropout(0.2) (c7) c7 = layers.Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c7) u8 = layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same') (c7) u8 = layers.concatenate([u8, c2]) c8 = layers.Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u8) c8 = layers.Dropout(0.1) (c8) c8 = layers.Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c8) u9 = layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (c8) u9 = layers.concatenate([u9, c1], axis=3) c9 = layers.Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u9) c9 = layers.Dropout(0.1) (c9) c9 = layers.Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c9) outputs = layers.Conv2D(num_class, (1, 1), activation='sigmoid') (c9) self.model = models.Model(inputs = inputs, output = outputs) self.model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[mean_iou])
def create_generator(): img = L.Input(shape=(32, 32, 3)) c1 = L.Conv2D(filters=64, kernel_size=(5, 5), padding='same', strides=(1, 1))(img) c1 = L.LeakyReLU()(c1) c2 = L.Conv2D(filters=128, kernel_size=(5, 5), padding='same', strides=(2, 2))(c1) c2 = L.LeakyReLU()(c2) c3 = L.Conv2D(filters=256, kernel_size=(5, 5), padding='same', strides=(2, 2))(c2) c3 = L.LeakyReLU()(c3) c4 = L.Conv2D(filters=512, kernel_size=(5, 5), padding='same', strides=(2, 2))(c3) c4 = L.LeakyReLU()(c4) f = L.Flatten()(c4) d = L.Dense(units=4 * 4 * 1024)(f) d = L.LeakyReLU()(d) r = L.Reshape(target_shape=(4, 4, 1024))(d) ct1 = L.Conv2DTranspose(filters=512, kernel_size=(5, 5), padding='same', strides=(1, 1))(r) ct1 = L.LeakyReLU()(ct1) ct1 = L.Concatenate()([ct1, c4]) ct2 = L.Conv2DTranspose(filters=256, kernel_size=(5, 5), padding='same', strides=(2, 2))(ct1) ct2 = L.LeakyReLU()(ct2) ct2 = L.Concatenate()([ct2, c3]) ct3 = L.Conv2DTranspose(filters=128, kernel_size=(5, 5), padding='same', strides=(2, 2))(ct2) ct3 = L.LeakyReLU()(ct3) ct3 = L.Concatenate()([ct3, c2]) ct4 = L.Conv2DTranspose(filters=64, kernel_size=(5, 5), padding='same', strides=(2, 2))(ct3) ct4 = L.LeakyReLU()(ct4) ct4 = L.Concatenate()([ct4, c1]) ct5 = L.Conv2DTranspose(filters=3, kernel_size=(5, 5), padding='same', strides=(2, 2))(ct4) x = L.Activation('tanh')(ct5) gen = Model(inputs=img, outputs=x) return gen
def createDecoder(latent_dim): latent_inputs = keras.Input(shape=(latent_dim,)) x = layers.Dense(7 * 7 * 64, activation="relu")(latent_inputs) x = layers.Reshape((7, 7, 64))(x) x = layers.Conv2DTranspose(64, 3, activation="relu", strides=2, padding="same")(x) x = layers.Conv2DTranspose(32, 3, activation="relu", strides=2, padding="same")(x) decoder_outputs = layers.Conv2DTranspose(1, 3, activation="sigmoid", padding="same")(x) decoder = keras.Model(latent_inputs, decoder_outputs, name="decoder") decoder.summary() return decoder
def binarize(self, fuse): p = layers.Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', use_bias=False)(fuse) p = layers.BatchNormalization()(p) p = layers.ReLU()(p) p = layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), kernel_initializer='he_normal', use_bias=False)(p) p = layers.BatchNormalization()(p) p = layers.ReLU()(p) p = layers.Conv2DTranspose(1, (2, 2), strides=(2, 2), kernel_initializer='he_normal', activation='sigmoid')(p) return p
def define_model(self): # INPUT LAYER input_img = layers.Input(shape=self.img_shape) # ENCODER ================================================================================================== x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same')(input_img) x = layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same', strides=2)(x) x = layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x) x = layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x) shape_before_flattening = K.int_shape(x) x = layers.Flatten()(x) x = layers.Dense(units=50, activation='relu')(x) latent_vector = layers.Dense(units=self.latent_space_dims, name='Latent_space',activity_regularizer=layers.regularizers.l1(10e-5) )(x) encoder = Model(input_img, latent_vector, name='Encoder') encoder.summary() # DECODER ========================================================================================= decoder_inputs = layers.Input(shape=K.int_shape(latent_vector)[1:]) d = layers.Dense(units=50, activation='relu')(decoder_inputs) d = layers.Dense(units=np.prod(shape_before_flattening[1:]), activation='relu')(d) d = layers.Reshape(target_shape=shape_before_flattening[1:])(d) d = layers.Conv2DTranspose(filters=64, kernel_size=(3, 3), padding='same', activation='relu')(d) d = layers.Conv2DTranspose(filters=64, kernel_size=(3, 3), padding='same', activation='relu')(d) d = layers.Conv2DTranspose(filters=64, kernel_size=(3, 3), padding='same', activation='relu', strides=2)(d) d = layers.Conv2DTranspose(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(d) decoded_img = layers.Conv2D(filters=1, kernel_size=(3, 3), padding='same', activation='sigmoid', name='decoded_img')(d) decoder = Model(decoder_inputs, decoded_img, name='decoder_model') decoder.summary() z_decoded = decoder(latent_vector) AE = Model(input_img, z_decoded) AE.compile(optimizer='rmsprop', loss='binary_crossentropy') AE.summary() encoder.compile(optimizer='rmsprop', loss='binary_crossentropy') decoder.compile(optimizer='rmsprop', loss='binary_crossentropy') self.model = AE self.encoder = encoder self.decoder = decoder self.define_flag = True
def build_deep_autoencoder(img_shape, code_size): """PCA's deeper brother. See instructions above. Use `code_size` in layer definitions.""" # encoder encoder = keras.models.Sequential() encoder.add(L.InputLayer(img_shape)) encoder.add( L.Conv2D(filters=32, kernel_size=3, padding='same', activation='elu')) encoder.add(L.MaxPooling2D(pool_size=2)) encoder.add( L.Conv2D(filters=64, kernel_size=3, padding='same', activation='elu')) encoder.add(L.MaxPooling2D(pool_size=2)) encoder.add( L.Conv2D(filters=128, kernel_size=3, padding='same', activation='elu')) encoder.add(L.MaxPooling2D(pool_size=2)) encoder.add( L.Conv2D(filters=256, kernel_size=3, padding='same', activation='elu')) encoder.add(L.MaxPooling2D(pool_size=2)) encoder.add(L.Flatten()) encoder.add(L.Dense(code_size)) # decoder decoder = keras.models.Sequential() decoder.add(L.InputLayer((code_size, ))) decoder.add(L.Dense(2 * 2 * 256)) decoder.add(L.Reshape((2, 2, 256))) decoder.add( L.Conv2DTranspose(filters=128, kernel_size=(3, 3), strides=2, activation='elu', padding='same')) decoder.add( L.Conv2DTranspose(filters=64, kernel_size=(3, 3), strides=2, activation='elu', padding='same')) decoder.add( L.Conv2DTranspose(filters=32, kernel_size=(3, 3), strides=2, activation='elu', padding='same')) decoder.add( L.Conv2DTranspose(filters=3, kernel_size=(3, 3), strides=2, activation=None, padding='same')) return encoder, decoder
def make_model(): model = models.Sequential() model.add(layers.Conv2D(1, (1, 1), activation='relu', input_shape=(64, 64, 1))) model.add(layers.Conv2D(1, (1, 1), activation='relu', input_shape=(64, 64, 1))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2DTranspose(1, (17, 17))) model.add(layers.Conv2DTranspose(1, (17, 17))) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) return model
def conv_model(x, y, name='', loss='mse', optimizer='adam'): inp = layers.Input(shape=x[0].shape) layer = layers.Dense(np.prod(y[0].shape), activation='relu')(inp) layer = layers.Reshape((y[0].shape[0], y[0].shape[1], 1))(layer) layer = layers.Conv2DTranspose(filters=5, kernel_size=5, padding='same')(layer) layer = layers.Conv2DTranspose(filters=1, kernel_size=5, padding='same')(layer) model = models.Model(inp, layer, name=name) model.compile(loss=loss, optimizer=optimizer) return model
def ResNet50Decode(img_input, concat_layers): concat1, concat2, concat3 = concat_layers[0], concat_layers[ 1], concat_layers[2] x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = layers.Conv2DTranspose(64, (7, 7), strides=(2, 2), padding='same', kernel_initializer='he_normal', name='conv1')(x) x.set_shape(x._keras_shape) x = layers.BatchNormalization(axis=3, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = convT_block(img_input, 3, [64, 64, 256], stage=2, block='a') x = convT_block(x, 3, [64, 64, 256], stage=2, block='a_2') 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 = trim_concat(x, concat1) x = convT_block(x, 3, [128, 128, 512], stage=3, block='a') x = convT_block(x, 3, [128, 128, 512], stage=3, block='a_2') 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 = trim_concat(x, concat2) x = convT_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 = trim_concat(x, concat3) x = convT_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 = layers.Conv2DTranspose(64, (7, 7), strides=(2, 2), padding='same', kernel_initializer='he_normal', name='conv1')(x) x.set_shape(x._keras_shape) return x
def deconv_block(input_tensor, kernel_size, filters, stage, branch, block, strides=(2, 2), use_bias=True): """conv_block is the block that has a conv layer at shortcut # Arguments input_tensor: input tensor kernel_size: defualt 3, the kernel size of middle conv layer at main path filters: list of integers, the nb_filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: 'a','b'..., current block label, used for generating layer names Note that from stage 3, the first conv layer at main path is with subsample=(2,2) And the shortcut should have subsample=(2,2) as well """ nb_filter1, nb_filter2, nb_filter3 = filters conv_name_base = 'res' + str(branch) + str(stage) + block + '_branch' bn_name_base = 'bn' + str(branch) + str(stage) + block + '_branch' x = KL.Conv2DTranspose(nb_filter1, (2, 2), strides=strides, padding='same', name=conv_name_base + '2a', use_bias=False)(input_tensor) x = KL.Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same', name=conv_name_base + '2b', use_bias=use_bias)(x) x = BatchNorm(axis=3, name=bn_name_base + '2b')(x) x = KL.Activation('relu')(x) x = KL.Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', use_bias=use_bias)(x) x = BatchNorm(axis=3, name=bn_name_base + '2c')(x) shortcut = KL.Conv2DTranspose(nb_filter3, (2, 2), strides=strides, name=conv_name_base + '1', use_bias=use_bias)(input_tensor) shortcut = BatchNorm(axis=3, name=bn_name_base + '1')(shortcut) x = KL.Add()([x, shortcut]) x = KL.Activation('relu', name='res' + str(branch) + str(stage) + block + '_out')(x) return x
def get_fcn16s_model(input_shape=(224, 224, 3), class_no=21): """ FCN 16 模型 :param input_shape: (输入图片长,输入图片宽,RGB层数),注意长宽最好是32的倍数 :param class_no: 类别数量 :return: Keras模型 """ input_tensor = layers.Input(shape=input_shape) x = layers.ZeroPadding2D(padding=(99, 99))(input_tensor) # Pad 100, 99 + 1 in first layer of vgg with tf.variable_scope("vgg_encoder"): encoder = VGG16(input_tensor=x, include_top=False, weights='imagenet') with tf.variable_scope("vgg_decoder"): with tf.variable_scope("fcn_32s"): x = encoder.get_layer('block5_pool').output # 卷积做降采用 x = layers.Conv2D(filters=4096, kernel_size=(7, 7), activation='relu', padding='valid', name='fc6')(x) x = layers.Dropout(0.5)(x) x = layers.Conv2D(filters=4096, kernel_size=(1, 1), activation='relu', padding='valid', name='fc7')(x) x = layers.Dropout(0.5)(x) # 使用 1x1卷积 做卷积操作,模拟全链接层操作 x = layers.Conv2D(filters=class_no, kernel_size=(1, 1), padding='valid')(x) # 使用反卷积做Upsampling到2倍 x = layers.Conv2DTranspose(filters=class_no, kernel_size=(4, 4), strides=(2, 2), padding='same', use_bias=False, name='upsampling1')(x) # print(tf.size_shape(x)) with tf.variable_scope("fcn_16s"): # 拿pool4的输出 pool4_output = encoder.get_layer('block4_pool').output pool4_output = layers.Conv2D(filters=class_no, kernel_size=(1, 1), padding='valid')(pool4_output) pool4_crop = FCN.center_crop(pool4_output, x) x = layers.add([x, pool4_crop]) # 使用反卷积做Upsampling到32倍 x = layers.Conv2DTranspose(filters=class_no, kernel_size=(32, 32), strides=(16, 16), padding='same', use_bias=False, name='upsampling2')(x) # 如果size不够,再做一个Bilinear的Upsampling(通常在图片size不为32的倍数时候需要) if K.int_shape(x)[1:3] != K.int_shape(input_tensor)[1:3]: print('Size different, do Bilinear Upsampling') x = layers.Lambda(lambda x: tf.image.resize_bilinear(x, size=K.int_shape(input_tensor)[1:3]))(x) # 对输出的每一个像素的各类别(即各通道)的输出使用softmax x = layers.Activation('softmax', name='output')(x) model = models.Model(inputs=input_tensor, outputs=x) return model