def encoder(num_filters, ch, rows, cols): model = Sequential() X = Input(shape=(rows[-1], cols[-1], ch)) model = Conv2D(num_filters, kernel_size=(5,5), strides=(2,2), padding='same', name='enc_conv2D_01', input_shape=(rows, cols, ch))(X) model = BN(axis=3, name="enc_bn_01", epsilon=1e-5)(model) model = LeakyReLU(.2)(model) model = Conv2D(num_filters*2,kernel_size=(5,5), strides=(2,2), padding='same', name='enc_conv2D_02')(model) model = BN(axis=3, name="enc_bn_02", epsilon=1e-5)(model) model = LeakyReLU(.2)(model) model = Conv2D(num_filters*4,kernel_size=(5,5), strides=(2,2), padding='same', name='enc_conv2D_03')(model) model = BN(axis=3, name="enc_bn_03", epsilon=1e-5)(model) model = LeakyReLU(.2)(model) #model = Reshape((8,8,256))(model) model = Flatten()(model) model = Dense(2048, name="enc_dense_01")(model) model = BN(name="enc_bn_04", epsilon=1e-5)(model) encoded_model = LeakyReLU(.2)(model) mean = Dense(z_dim, name="e_h3_lin")(encoded_model) logsigma = Dense(z_dim, name="e_h4_lin", activation="tanh")(encoded_model) meansigma = Model([X], [mean, logsigma]) #X_decode = Input(shape=(8,8,256)) #model = Dense(256, name="dec_dense_01")(encoded_model) # enc_model = Model(X, encoded_model) # dec_model = Model(X, model) return meansigma
def build(num_hidden_nodes, dropout_rate, l1_reg_weight): num_hidden_nodes = num_hidden_nodes dropout_rate = dropout_rate l1_reg_weight = l1_reg_weight BN_momentum = 0.99 inputs = Input(shape=(585, )) # Encoder FC1 = Dense(num_hidden_nodes, activation='relu', activity_regularizer=regularizers.l1(l1_reg_weight))(inputs) FC1 = BN(momentum=BN_momentum)(FC1) FC1 = Dropout(dropout_rate)(FC1) FC1 = Dense(num_hidden_nodes, activation='relu', activity_regularizer=regularizers.l1(l1_reg_weight))(FC1) FC1 = BN(momentum=BN_momentum)(FC1) FC1 = Dropout(dropout_rate)(FC1) FC1 = Dense(num_hidden_nodes, activation='relu', activity_regularizer=regularizers.l1(l1_reg_weight))(FC1) FC1 = BN(momentum=BN_momentum)(FC1) FC1 = Dropout(dropout_rate)(FC1) # Decoder out = Dense(39, activation='linear')(FC1) out = BN(momentum=BN_momentum)(out) autoencoder = Model(inputs, out, name=NAME) return autoencoder
def discriminator_l2(num_filters, ch, rows, cols, z_dim, kernel_size=(5, 5), strides=(2, 2)): model = Sequential() X = Input(shape=(rows[-1], cols[-1], ch)) model = Conv2D(num_filters, kernel_size=kernel_size, strides=1, padding='same', name='disc_conv2D_01')(X) model = BN(name="disc_bn_01")(model) model = LeakyReLU(0.2)(model) #model = Activation('relu')(model) model = Conv2D(num_filters * 4, kernel_size=kernel_size, strides=strides, padding='same', name='disc_conv2D_02')(model) model = BN(name="disc_bn_02")(model) model = LeakyReLU(0.2)(model) #model = Activation('relu')(model) model = Conv2D(num_filters * 8, kernel_size=kernel_size, strides=strides, padding='same', name='disc_conv2D_03')(model) model = BN(name="disc_bn_03")(model) model = LeakyReLU(0.2)(model) #model = Activation('relu')(model) model_l = Conv2D(num_filters * 8, kernel_size=kernel_size, strides=strides, padding='same', name='disc_conv2D_04')(model) d_model = BN(name="disc_bn_04")(model_l) d_model = LeakyReLU(0.2)(d_model) #model = Activation('relu')(model) #model = Reshape((8,8,256))(model) d_model = Flatten()(d_model) d_model = Dense(512, name="disc_dense_01")(d_model) d_model = BN(name="disc_bn_05")(d_model) d_model = LeakyReLU(0.2)(d_model) #model = Activation('relu')(model) d_model = Dense(1, name="disc_dense_02")(d_model) d_model = Activation('sigmoid', name='disc_sigmoid')(d_model) disc_model = Model(X, d_model, name="discriminator") disc_model_l2 = Model(X, model_l, name="discriminator-l2") return disc_model, disc_model_l2
def build_s1(self): np.random.seed(42) set_random_seed(42) # Build the encoder network # ------------ Input ----------------- inp = Input(shape=(self.args.s1_input_size,)) # ------------ Concat Layer ----------------- x = Dense(self.args.ds, activation=self.args.act)(inp) x = BN()(x) # ------------ Embedding Layer -------------- z_mean = Dense(self.args.ds // 2, name='z_mean')(x) z_log_sigma = Dense(self.args.ds // 2, name='z_log_sigma', kernel_initializer='zeros')(x) z = Lambda(sampling, output_shape=(self.args.ds // 2,), name='z')([z_mean, z_log_sigma]) self.encoder = Model(inp, [z_mean, z_log_sigma, z], name='encoder') self.encoder.summary() # Build the decoder network # ------------ Dense out ----------------- latent_inputs = Input(shape=(self.args.ds // 2,), name='z_sampling') x = latent_inputs x = Dense(self.args.ds, activation=self.args.act)(x) x = BN()(x) x=Dropout(self.args.dropout)(x) # ------------ Out ----------------------- s1_out = Dense(self.args.s1_input_size, activation='sigmoid')(x) decoder = Model(latent_inputs, s1_out, name='decoder') decoder.summary() output = decoder(self.encoder(inp)[2]) self.vae = Model(inp, output, name='vae_s1') self.reconstruction_loss = binary_crossentropy(inp, output) # Define the loss if self.args.distance == "mmd": true_samples = K.random_normal(K.stack([self.args.bs, self.args.ds // 2])) distance = mmd(true_samples, z) if self.args.distance == "kl": distance = kl_regu(z_mean,z_log_sigma) vae_loss = K.mean(self.reconstruction_loss + self.args.beta * distance) self.vae.add_loss(vae_loss) adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.001, amsgrad=False) self.vae.compile(optimizer=adam) self.vae.summary()
def create_model(): inputs = Input(shape=(None, None, 1)) edges = Lambda(sobel, output_shape=sobel_shape, name='sobel-edge')(inputs) input_edge = concatenate([inputs, edges], axis=3) conv1 = Conv2D(64, (5, 5), activation='relu', padding='same')(input_edge) conv2 = Conv2D(64, (3, 3), padding='same', dilation_rate=(2, 2))(conv1) conv2 = BN()(conv2) conv2 = Activation('relu')(conv2) conv3 = Conv2D(64, (3, 3), padding='same', dilation_rate=(3, 3))(conv2) conv3 = BN()(conv3) conv3 = Activation('relu')(conv3) conv4 = Conv2D(64, (3, 3), padding='same', dilation_rate=(4, 4))(conv3) conv4 = BN()(conv4) conv4 = Activation('relu')(conv4) conv5 = Conv2D(64, (3, 3), padding='same', dilation_rate=(3, 3))(conv4) conv5 = BN()(conv5) conv5 = Activation('relu')(conv5) conv6 = concatenate([conv5, conv2], axis=3) conv6 = Conv2D(64, (3, 3), padding='same', dilation_rate=(2, 2))(conv6) conv6 = BN()(conv6) conv6 = Activation('relu')(conv6) conv7 = concatenate([conv6, conv1], axis=3) conv7 = Conv2D(1, (3, 3), padding='same')(conv7) conv8 = concatenate([conv7, input_edge], axis=3) outputs = Conv2D(3, (3, 3), padding='same')(conv8) return Model(inputs=[inputs], outputs=[outputs, outputs])