def discriminator_z_model(size_z, num_Dz_channels): kernel_initializer = initializers.random_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) inputs = Input(shape=(size_z, )) # fully connection layer current = inputs for i in range(len(num_Dz_channels)): name = 'D_z_fc' + str(i) current = Dense(units=num_Dz_channels[i], kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) current = Lambda(lrelu, output_shape=(num_Dz_channels[i], ))(current) return Model(inputs=inputs, outputs=current)
def build_model(self): initializer = initializers.random_normal(stddev=0.02) input_img = Input(shape=(self.layers, 22, 80)) input_2 = Lambda(lambda x: x[:, :2, :, :], output_shape=lambda x: (None, 2, 22, 80))(input_img) # no map channel # whole map 10x1 tower_1 = ZeroPadding2D(padding=(1, 0), data_format="channels_first")(input_2) tower_1 = Conv2D(32, (10, 1), data_format="channels_first", strides=(7, 1), kernel_initializer=initializer, padding="valid")(tower_1) tower_1 = Flatten()(tower_1) # whole map 1x10 tower_2 = Conv2D(32, (1, 10), data_format="channels_first", strides=(1, 7), kernel_initializer=initializer, padding="valid")(input_2) tower_2 = Flatten()(tower_2) # whole map 3x3 then maxpool 22x80 tower_3 = Conv2D(32, (3, 3), data_format="channels_first", strides=(1, 1), kernel_initializer=initializer, padding="same")(input_2) tower_3 = MaxPooling2D(pool_size=(22, 80), data_format="channels_first")(tower_3) tower_3 = Flatten()(tower_3) merged_layers = concatenate([tower_1, tower_2, tower_3], axis=1) predictions = Dense(4, kernel_initializer=initializer)(merged_layers) model = Model(inputs=input_img, outputs=predictions) adam = Adam(lr=1e-6) model.compile(loss='mse', optimizer=adam) return model
def build_model(self): initializer = initializers.random_normal(stddev=0.02) model = Sequential() if self.padding: model.add( ZeroPadding2D(padding=(1, 0), data_format="channels_first", input_shape=(self.layers, self.rows, self.columns))) model.add( Conv2D(32, (8, 8), activation="relu", data_format="channels_first", strides=(4, 4), kernel_initializer=initializer, padding='same', input_shape=(self.layers, self.rows, self.columns))) model.add( Conv2D(64, (4, 4), activation="relu", data_format="channels_first", strides=(2, 2), kernel_initializer=initializer, padding='same')) model.add( Conv2D(64, (3, 3), activation="relu", data_format="channels_first", strides=(1, 1), kernel_initializer=initializer, padding='same')) model.add(Flatten()) model.add(Dense(512, activation="relu", kernel_initializer=initializer)) model.add(Dense(self.actions_num, kernel_initializer=initializer)) adam = Adam(lr=1e-6) model.compile(loss='mse', optimizer=adam) return model
def encoder_model(size_image, size_age_label, size_name_label, size_gender_label, num_input_channels, size_kernel, size_z, num_encoder_channels): # map the label of age + gender to size (size_image) kernel_initializer = initializers.truncated_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) # input of input images input_images = Input(shape=(size_image, size_image, num_input_channels)) # input of age labels (use {function dupilicate_conv} to time the age_labels to match with images, then concatenate ) input_ages_conv = Input(shape=(1, 1, size_age_label)) # (1, 1, 10*tile_ratio) input_ages_conv_repeat = Lambda( duplicate_conv, output_shape=(size_image, size_image, size_age_label), arguments={'times': size_image})(input_ages_conv) #(128, 128, 10*tile_ratio) input_names_conv = Input(shape=(1, 1, size_name_label)) input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_image, size_image, size_name_label), arguments={'times': size_image})(input_names_conv) input_genders_conv = Input(shape=(1, 1, size_gender_label)) input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_image, size_image, size_gender_label), arguments={'times': size_image })(input_genders_conv) current = Concatenate(axis=-1)([ input_images, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat ]) # E_conv layer + Batch Normalization num_layers = len(num_encoder_channels) for i in range(num_layers): name = 'E_conv' + str(i) current = Conv2D(filters=num_encoder_channels[i], kernel_size=(size_kernel, size_kernel), strides=(2, 2), padding='same', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) size_image = int(size_image / 2) current = Lambda(lrelu, output_shape=(size_image, size_image, int(current.shape[3])))(current) # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])), # arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current) # reshape current = Flatten()(current) # fully connection layer kernel_initializer = initializers.random_normal(stddev=0.02) name = 'E_fc' current = Dense(units=size_z, activation='tanh', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) # output return Model(inputs=[ input_images, input_ages_conv, input_names_conv, input_genders_conv ], outputs=current)
def create_critic_network(self, depth_front_size, grey_front_size, depth_bottom_size, grey_bottom_size, depth_back_size, grey_back_size, vel_size, pos_size, action_dim): print("Now we build the model") depth_front_image = Input(shape=depth_front_size) grey_front_image = Input(shape=grey_front_size) depth_bottom_image = Input(shape=depth_bottom_size) grey_bottom_image = Input(shape=grey_bottom_size) depth_back_image = Input(shape=depth_back_size) grey_back_image = Input(shape=grey_back_size) vel = Input(shape=vel_size) pos = Input(shape=pos_size) # build conv layer for grey image gfi = Conv2D( 32, (4, 4), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(grey_front_image) gfi = Activation('relu')(gfi) gfi = BatchNormalization()(gfi) gfi = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gfi) gfi = Activation('relu')(gfi) gfi = BatchNormalization()(gfi) gfi = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gfi) gfi = Activation('relu')(gfi) gfi = BatchNormalization()(gfi) gfi = Flatten()(gfi) # build conv layer for grey image gboti = Conv2D( 32, (4, 4), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(grey_bottom_image) gboti = Activation('relu')(gboti) gboti = BatchNormalization()(gboti) gboti = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gboti) gboti = Activation('relu')(gboti) gboti = BatchNormalization()(gboti) gboti = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gboti) gboti = Activation('relu')(gboti) gboti = BatchNormalization()(gboti) gboti = Flatten()(gboti) # build conv layer for grey image gbi = Conv2D( 32, (4, 4), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(grey_back_image) gbi = Activation('relu')(gbi) gbi = BatchNormalization()(gbi) gbi = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gbi) gbi = Activation('relu')(gbi) gbi = BatchNormalization()(gbi) gbi = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gbi) gbi = Activation('relu')(gbi) gbi = BatchNormalization()(gbi) gbi = Flatten()(gbi) # build conv layer for depth image dfi = Conv2D( 32, (3, 3), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(depth_front_image) dfi = Activation('relu')(dfi) dfi = BatchNormalization()(dfi) dfi = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dfi) dfi = Activation('relu')(dfi) dfi = BatchNormalization()(dfi) dfi = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dfi) dfi = BatchNormalization()(dfi) dfi = Flatten()(dfi) # build conv layer for depth image dboti = Conv2D( 32, (3, 3), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(depth_bottom_image) dboti = Activation('relu')(dboti) dboti = BatchNormalization()(dboti) dboti = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dboti) dboti = Activation('relu')(dboti) dboti = BatchNormalization()(dboti) dboti = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dboti) dboti = BatchNormalization()(dboti) dboti = Flatten()(dboti) # build conv layer for depth image dbi = Conv2D( 32, (3, 3), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(depth_back_image) dbi = Activation('relu')(dbi) dbi = BatchNormalization()(dbi) dbi = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dbi) dbi = Activation('relu')(dbi) dbi = BatchNormalization()(dbi) dbi = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dbi) dbi = BatchNormalization()(dbi) dbi = Flatten()(dbi) h0_vel = Dense( HIDDEN1_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(vel) h0_vel = BatchNormalization()(h0_vel) h1_vel = Dense( HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(h0_vel) h1_vel = BatchNormalization()(h1_vel) h1_vel = Flatten()(h1_vel) h0_pos = Dense( HIDDEN1_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(pos) h0_pos = BatchNormalization()(h0_pos) h1_pos = Dense( HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(h0_pos) h1_pos = BatchNormalization()(h1_pos) h1_pos = Flatten()(h1_pos) sensor_fused = concatenate( [dfi, gfi, dboti, gboti, dbi, gbi, h1_vel, h1_pos]) d1 = Dense(HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(sensor_fused) d1 = BatchNormalization()(d1) d2 = Dense(HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(d1) d2 = BatchNormalization()(d2) d3 = Dense(HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(d2) d3 = BatchNormalization()(d3) A = Input(shape=[action_dim], name='action2') a1 = Dense(HIDDEN2_UNITS, activation='linear', kernel_constraint=max_norm(0.07))(A) h2 = concatenate([d3, a1]) h3 = Dense(HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(h2) V = Dense(action_dim, activation='linear')(h3) model = Model(input=[ depth_front_image, grey_front_image, depth_bottom_image, grey_bottom_image, depth_back_image, grey_back_image, vel, pos, A ], output=V) print(model.summary()) adam = Adam(lr=self.LEARNING_RATE) model.compile(loss='mse', optimizer=adam) return model, A, depth_front_image, grey_front_image, depth_bottom_image, grey_bottom_image, depth_back_image, grey_back_image, vel, pos
def build_model(self): initializer = initializers.random_normal(stddev=0.02) input_img = Input(shape=(self.layers, 22, 80)) input_2 = Lambda(lambda x: x[:, 1:, :, :], output_shape=lambda x: (None, self.layers - 1, 22, 80))( input_img) # no map channel # whole map tower_1 = Conv2D(64, (3, 3), data_format="channels_first", strides=(1, 1), kernel_initializer=initializer, padding="same")(input_img) tower_1 = Conv2D(32, (3, 3), data_format="channels_first", strides=(1, 1), kernel_initializer=initializer, padding="same")(tower_1) tower_1 = MaxPooling2D(pool_size=(22, 80), data_format="channels_first")(tower_1) #tower2 tower_2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_first")(input_2) for _ in range(self.depth): tower_2 = Conv2D(32, (3, 3), data_format="channels_first", strides=(1, 1), kernel_initializer=initializer, padding="same", activation='relu')(tower_2) tower_2 = MaxPooling2D(pool_size=(11, 40), data_format="channels_first")(tower_2) #tower3 tower_3 = MaxPooling2D(pool_size=(3, 6), data_format="channels_first", padding='same')(input_2) for _ in range(self.depth): tower_3 = Conv2D(32, (3, 3), data_format="channels_first", strides=(1, 1), kernel_initializer=initializer, padding="same", activation='relu')(tower_3) tower_3 = MaxPooling2D(pool_size=(8, 14), data_format="channels_first", padding='same')(tower_3) merged_layers = concatenate([tower_1, tower_2, tower_3], axis=1) flat_layer = Flatten()(merged_layers) predictions = Dense(5, kernel_initializer=initializer)(flat_layer) model = Model(inputs=input_img, outputs=predictions) rmsprop = RMSprop(lr=0.00025) model.compile(loss='mse', optimizer=rmsprop) return model
def generator_model(): kernel_initializer = initializers.random_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) inputs_z = Input(shape=(100, )) # inputs_y = Input(shape=(10, )) # input_y_conv = Input(shape=(1, 1, 10)) current = Dense(64 * 8 * 4 * 4, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(inputs_z) current = Reshape(target_shape=(4, 4, 64 * 8))(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(4, 4, 64 * 8), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Activation(activation='relu')(current) current = Conv2DTranspose(filters=64 * 4, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(8, 8, 64*4), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(8, 8, 64 * 4), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Activation(activation='relu')(current) current = Conv2DTranspose(filters=64 * 2, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(16, 16, 64*2), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(16, 16, 64 * 2), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Activation(activation='relu')(current) current = Conv2DTranspose(filters=64 * 1, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(32, 32, 64*1), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(32, 32, 64 * 1), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Activation(activation='relu')(current) current = Conv2DTranspose(filters=3, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) current = Activation('tanh')(current) return Model(inputs=inputs_z, outputs=current)
def discriminator_model(): kernel_initializer = initializers.truncated_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) inputs_img = Input(shape=(64, 64, 3)) current = Conv2D(filters=64, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(inputs_img) current = Lambda(lrelu, output_shape=(32, 32, int(current.shape[3])))(current) current = Conv2D(filters=64 * 2, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(16, 16, int(current.shape[3])), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(16, 16, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Lambda(lrelu, output_shape=(16, 16, int(current.shape[3])))(current) current = Conv2D(filters=64 * 4, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(8, 8, int(current.shape[3])), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(8, 8, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Lambda(lrelu, output_shape=(8, 8, int(current.shape[3])))(current) current = Conv2D(filters=64 * 8, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(4, 4, int(current.shape[3])), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(4, 4, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Lambda(lrelu, output_shape=(4, 4, int(current.shape[3])))(current) kernel_initializer = initializers.random_normal(stddev=0.02) current = Reshape(target_shape=(4 * 4 * 512, ))(current) current = Dense(1, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) current = Activation('sigmoid')(current) return Model(inputs=inputs_img, outputs=current)
def generator_model(size_z, size_age_label, size_name_label, size_gender_label, size_mini_map, size_kernel, size_gen, num_input_channels, num_gen_channels): kernel_initializer = initializers.random_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) #Input layer input_z = Input(shape=(size_z, )) input_age_label = Input(shape=(size_age_label, )) input_name_label = Input(shape=(size_name_label, )) input_gender_label = Input(shape=(size_gender_label, )) current = Concatenate(axis=-1)( [input_z, input_age_label, input_name_label, input_gender_label]) # fc layer name = 'G_fc' current = Dense(units=size_mini_map * size_mini_map * size_gen, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) # Reshape current = Reshape(target_shape=(size_mini_map, size_mini_map, size_gen))(current) # BatchNormalization #current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_mini_map, size_mini_map, size_gen), #arguments={'decay': 0.9, 'epsilon': 1e-5, 'scale': True})(current) # Activation current = Activation(activation='relu')(current) # deconv layers with stride 2 num_layers = len(num_gen_channels) size_image = size_mini_map for i in range(num_layers - 1): name = 'G_deconv' + str(i) current = Conv2DTranspose(filters=num_gen_channels[i], kernel_size=(size_kernel, size_kernel), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) # size_image = size_image * 2 # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])), # arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current) current = Activation(activation='relu')(current) # final layer of generator---> activation: tanh name = 'G_deconv' + str(i + 1) current = Conv2DTranspose(filters=num_gen_channels[-1], kernel_size=(size_kernel, size_kernel), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) current = Activation('tanh')(current) # output return Model(inputs=[ input_z, input_age_label, input_name_label, input_gender_label ], outputs=current)
def generator_pix2pix_model(size_image, size_age_label, size_name_label, size_gender_label, size_kernel, num_input_channels, num_encoder_channels, num_gen_channels, G_net): from ops import duplicate_conv, lrelu kernel_initializer = initializers.random_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) # Encoder Input layer input_images = Input(shape=(size_image, size_image, num_input_channels)) input_ages_conv = Input(shape=(1, 1, size_age_label)) input_names_conv = Input(shape=(1, 1, size_name_label)) input_genders_conv = Input(shape=(1, 1, size_gender_label)) input_ages_conv_repeat = Lambda(duplicate_conv, output_shape=(size_image, size_image, size_age_label), arguments={'times': size_image})(input_ages_conv) input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_image, size_image, size_name_label), arguments={'times': size_image})(input_names_conv) input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_image, size_image, size_gender_label), arguments={'times': size_image })(input_genders_conv) current = Concatenate(axis=-1)([ input_images, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat ]) if G_net == 'Unet': # E_conv layer + lrelu + Batch Normalization res_list = [] size_current = current.shape[1].value for i in range(len(num_encoder_channels)): name = 'E_conv' + str(i) kernel_size_change = max(size_kernel - i, 2) current = Conv2D(filters=num_encoder_channels[i], kernel_size=(kernel_size_change, kernel_size_change), strides=(2, 2), padding='same', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) size_current = int(size_current / 2) current = Lambda(lrelu, output_shape=(size_current, size_current, int(current.shape[3])))(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_current, size_current, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) res_list.append(current) # if size_current > 1: # input_ages_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_age_label), # arguments={'times': size_current})(input_ages_conv) # input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_name_label), # arguments={'times': size_current})(input_names_conv) # input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_gender_label), # arguments={'times': size_current})(input_genders_conv) # current = Concatenate(axis=-1)([current, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat]) # else: # current = Concatenate(axis=-1)([current, input_ages_conv, input_names_conv, input_genders_conv]) # G_deconv layer + Batch Normalization + relu/tanh size_current = current.shape[1].value for i, (filter, dropout) in enumerate(num_gen_channels): # Residual Block ---------------> every E_conv layer has 3 mini layers(E_conv + lambda:lrelu + lambda:BN) if i > 0: current = Concatenate(axis=-1)([current, res_list[-1 - i]]) name = 'G_deconv' + str(i) kernel_size_change = max( size_kernel - (len(num_gen_channels) - 1 - i), 2) current = Conv2DTranspose(filters=filter, kernel_size=(kernel_size_change, kernel_size_change), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) size_current = size_current * 2 current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_current, size_current, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) if dropout > 0.0: current = Dropout(rate=dropout)(current) if i == len(num_gen_channels) - 1: current = Activation(activation='tanh')(current) else: current = Activation(activation='relu')(current) # if size_current > 1 and size_current < size_image: # input_ages_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_age_label), # arguments={'times': size_current})(input_ages_conv) # input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_name_label), # arguments={'times': size_current})(input_names_conv) # input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_gender_label), # arguments={'times': size_current})(input_genders_conv) # current = Concatenate(axis=-1)([current, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat]) # elif size_current == 1: # current = Concatenate(axis=-1)([current, input_ages_conv, input_names_conv, input_genders_conv]) elif G_net == 'Resnet': # E_conv layer + lrelu + Batch Normalization for i in range(len(num_encoder_channels)): name = 'E_conv' + str(i) kernel_size_change = max(size_kernel - i, 2) current = Conv2D(filters=num_encoder_channels[i], kernel_size=(kernel_size_change, kernel_size_change), strides=(2, 2), padding='same', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) size_image = int(size_image / 2) current = Lambda(lrelu, output_shape=(size_image, size_image, int(current.shape[3])))(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) # G_deconv layer + Batch Normalization + relu/tanh res_list = [] size_current = current.shape[1].value for i, (filter, dropout) in enumerate(num_gen_channels): # Residual Block ---------------> every E_conv layer has 3 mini layers(E_conv + lambda:lrelu + lambda:BN) if i > 1 and i < len(num_gen_channels) - 1: # res_output = res_list[-1 - i](inputs=[input_images, input_ages_conv, input_names_conv, input_genders_conv]) # current = Concatenate(axis=-1)([current, res_output]) num_padding = int(2**(i - 1) / 2) res_block = ZeroPadding2D(padding=(num_padding, num_padding))( res_list[i - 2]) current = Concatenate(axis=-1)([current, res_block]) name = 'G_deconv' + str(i) kernel_size_change = max( size_kernel - (len(num_gen_channels) - 1 - i), 2) current = Conv2DTranspose(filters=filter, kernel_size=(kernel_size_change, kernel_size_change), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) size_current = size_current * 2 current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_current, size_current, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) if dropout > 0.0: current = Dropout(rate=dropout)(current) if i == len(num_gen_channels) - 1: current = Activation(activation='tanh')(current) else: current = Activation(activation='relu')(current) res_list.append(current) # output return Model(inputs=[ input_images, input_ages_conv, input_names_conv, input_genders_conv ], outputs=current)
def discriminator_model(): kernel_initializer = initializers.truncated_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) inputs_img = Input(shape=(28, 28, 1)) inputs_y = Input(shape=(10, )) input_y_conv = Input(shape=(1, 1, 10)) # current = Reshape((28, 28, 1))(inputs_img) inputs_y_repeat = Lambda(concatenate, output_shape=(28, 28, 10), arguments={'times': 28})(input_y_conv) current = Concatenate(axis=-1)([inputs_img, inputs_y_repeat]) current = Conv2D(filters=1 + 10, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) current = Lambda(lrelu, output_shape=(14, 14, int(current.shape[3])))(current) inputs_y_repeat = Lambda(concatenate, output_shape=(14, 14, 10), arguments={'times': 14})(input_y_conv) current = Concatenate(axis=-1)([current, inputs_y_repeat]) current = Conv2D(filters=64 + 10, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(7, 7, int(current.shape[3])), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(7, 7, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Lambda(lrelu, output_shape=(7, 7, int(current.shape[3])))(current) kernel_initializer = initializers.random_normal(stddev=0.02) current = Reshape(target_shape=(7 * 7 * 74, ))(current) current = Concatenate(axis=-1)([current, inputs_y]) current = Dense(1024, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(int(current.shape[1]),), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(int(current.shape[1]), ), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Lambda(lrelu, output_shape=(int(current.shape[1]), ))(current) current = Concatenate(axis=-1)([current, inputs_y]) current = Dense(1, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) current = Activation('sigmoid')(current) # conv1 = Conv2D(filters=64, kernel_size=(5, 5), padding="same")(x) # conv1 = LeakyReLU(alpha=0.2)(conv1) # Convolution2D is another name of Conv2D # conv1 = Convolution2D(filters=64, kernel_size=(5, 5), padding="same", activation="relu")(x) # max1 = MaxPooling2D(pool_size=(2, 2))(conv1) # conv2 = Conv2D(filters=128, kernel_size=(5, 5), padding="same")(max1) # conv2 = LeakyReLU(alpha=0.2)(conv2) # conv2 = Convolution2D(filters=128, kernel_size=(5, 5), padding="same")(max1) # max2 = MaxPooling2D(pool_size=(2, 2))(conv2) # flat = Flatten()(max2) # dense1 = Dense(units=1024, activation="relu")(flat) # # dense1 = LeakyReLU(alpha=0.2)(dense1) # dense2 = Dense(units=1, activation="sigmoid")(dense1) return Model(inputs=[inputs_img, inputs_y, input_y_conv], outputs=current)
def generator_model(): kernel_initializer = initializers.random_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) inputs_img = Input(shape=(100, )) inputs_y = Input(shape=(10, )) input_y_conv = Input(shape=(1, 1, 10)) # current = tf.concat([current, inputs_y], axis=-1) # current = Merge(mode='concat', concat_axis=-1)([inputs_img, inputs_y]) current = Concatenate(axis=-1)([inputs_img, inputs_y]) current = Dense(1024, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization,output_shape=(int(current.shape[1]),), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(int(current.shape[1]), ), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) # current = tf.layers.batch_normalization(inputs=current) current = Activation(activation='relu')(current) current = Concatenate(axis=-1)([current, inputs_y]) current = Dense(128 * 7 * 7, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(int(current.shape[1]),), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(int(current.shape[1]), ), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Activation(activation='relu')(current) current = Reshape(target_shape=(7, 7, 128))(current) inputs_y_repeat = Lambda(concatenate, output_shape=(7, 7, 10), arguments={'times': 7})(input_y_conv) current = Concatenate(axis=-1)([current, inputs_y_repeat]) current = Conv2DTranspose(filters=64, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) # current = Lambda(tf.layers.batch_normalization, output_shape=(14, 14, int(current.shape[3])), # arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current) current = Lambda(tf.contrib.layers.batch_norm, output_shape=(14, 14, int(current.shape[3])), arguments={ 'decay': 0.9, 'epsilon': 1e-5, 'scale': True })(current) current = Activation(activation='relu')(current) current = Conv2DTranspose(filters=1, kernel_size=(5, 5), padding='same', strides=(2, 2), kernel_initializer=kernel_initializer, bias_initializer=bias_initializer)(current) current = Activation('sigmoid')(current) return Model(inputs=[inputs_img, inputs_y, input_y_conv], outputs=current)
def discriminator_img_model(size_image, size_kernel, size_age_label, size_name_label, size_gender_label, num_input_channels, num_Dimg_channels, num_Dimg_fc_channels): kernel_initializer = initializers.truncated_normal(stddev=0.02) bias_initializer = initializers.constant(value=0.0) # Dimg model input_images = Input(shape=(size_image, size_image, num_input_channels)) # the label of age + gender input_ages_conv = Input(shape=(1, 1, size_age_label)) # (1, 1, 10*tile_ratio) input_ages_conv_repeat = Lambda( duplicate_conv, output_shape=(size_image, size_image, size_age_label), arguments={'times': size_image})(input_ages_conv) #(128, 128, 10*tile_ratio) input_names_conv = Input(shape=(1, 1, size_name_label)) input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_image, size_image, size_name_label), arguments={'times': size_image})(input_names_conv) input_genders_conv = Input(shape=(1, 1, size_gender_label)) input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_image, size_image, size_gender_label), arguments={'times': size_image })(input_genders_conv) # concatenate current = Concatenate(axis=-1)([ input_images, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat ]) num_layers = len(num_Dimg_channels) # name = 'D_img_conv0' # current = Conv2D( # filters=num_Dimg_channels[0], # kernel_size=(size_kernel, size_kernel), # strides=(2, 2), # padding='same', # kernel_initializer=kernel_initializer, # bias_initializer=bias_initializer, # name=name)(current) # size_image = int(size_image / 2) # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])), # arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current) # current = Lambda(lrelu, output_shape=(size_image, size_image, int(current.shape[3])))(current) # conv layers with stride 2 for i in range(num_layers): name = 'D_img_conv' + str(i) current = Conv2D(filters=num_Dimg_channels[i], kernel_size=(size_kernel, size_kernel), strides=(2, 2), padding='same', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) size_image = int(size_image / 2) # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])), # arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current) current = Lambda(lrelu, output_shape=(size_image, size_image, int(current.shape[3])))(current) # current = Flatten()(current) current = Reshape(target_shape=(size_image * size_image * int(current.shape[3]), ))(current) # fully connection layer kernel_initializer = initializers.random_normal(stddev=0.02) name = 'D_img_fc1' current = Dense(units=num_Dimg_fc_channels, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) current = Lambda(lrelu, output_shape=(num_Dimg_fc_channels, ))(current) name = 'D_img_fc2' current = Dense(units=1, activation='sigmoid', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, name=name)(current) # output = Activation('sigmoid')(current) # output return Model(inputs=[ input_images, input_ages_conv, input_names_conv, input_genders_conv ], outputs=current)
def create_actor_network(self, depth_front_size, grey_front_size, depth_bottom_size, grey_bottom_size, depth_back_size, grey_back_size, vel_size, pos_size, action_dim): print("Now we build the model") depth_front_image = Input(shape=depth_front_size) grey_front_image = Input(shape=grey_front_size) depth_bottom_image = Input(shape=depth_bottom_size) grey_bottom_image = Input(shape=grey_bottom_size) depth_back_image = Input(shape=depth_back_size) grey_back_image = Input(shape=grey_back_size) vel = Input(shape=vel_size) pos = Input(shape=pos_size) # build conv layer for Grey front image # gfi : grey forward image gfi = Conv2D( 32, (4, 4), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(grey_front_image) gfi = Activation('relu')(gfi) gfi = BatchNormalization()(gfi) gfi = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gfi) gfi = Activation('relu')(gfi) gfi = BatchNormalization()(gfi) gfi = Conv2D( 64, (2, 2), strides=(1, 1), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gfi) gfi = Activation('relu')(gfi) gfi = BatchNormalization()(gfi) gfi = Flatten()(gfi) # build conv layer for Grey bottom image # gboti : grey bottom image gboti = Conv2D( 32, (4, 4), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(grey_bottom_image) gboti = Activation('relu')(gboti) gboti = BatchNormalization()(gboti) gboti = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gboti) gboti = Activation('relu')(gboti) gboti = BatchNormalization()(gboti) gboti = Conv2D( 64, (2, 2), strides=(1, 1), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gboti) gboti = Activation('relu')(gboti) gboti = BatchNormalization()(gboti) gboti = Flatten()(gboti) # build conv layer for Grey bottom image # gbi grey back image gbi = Conv2D( 32, (4, 4), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(grey_back_image) gbi = Activation('relu')(gbi) gbi = BatchNormalization()(gbi) gbi = Conv2D( 64, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gbi) gbi = Activation('relu')(gbi) gbi = BatchNormalization()(gbi) gbi = Conv2D( 64, (2, 2), strides=(1, 1), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(gbi) gbi = Activation('relu')(gbi) gbi = BatchNormalization()(gbi) gbi = Flatten()(gbi) # build conv layer for depth image dfi = Conv2D( 16, (3, 3), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(depth_front_image) dfi = Activation('relu')(dfi) dfi = BatchNormalization()(dfi) dfi = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dfi) dfi = Activation('relu')(dfi) dfi = BatchNormalization()(dfi) dfi = Flatten()(dfi) # build conv layer for depth image # dboti: depth bottom image dboti = Conv2D( 16, (3, 3), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(depth_bottom_image) dboti = Activation('relu')(dboti) dboti = BatchNormalization()(dboti) dboti = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dboti) dboti = Activation('relu')(dboti) dboti = BatchNormalization()(dboti) dboti = Flatten()(dboti) # build conv layer for depth image # dbi: depth back image dbi = Conv2D( 16, (3, 3), strides=(4, 4), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(depth_back_image) dbi = Activation('relu')(dbi) dbi = BatchNormalization()(dbi) dbi = Conv2D( 32, (3, 3), strides=(3, 3), kernel_initializer=initializers.random_normal(stddev=1e-4), padding='same', kernel_constraint=max_norm(0.07))(dbi) dbi = Activation('relu')(dbi) dbi = BatchNormalization()(dbi) dbi = Flatten()(dbi) h0_vel = Dense( HIDDEN1_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(vel) h0_vel = BatchNormalization()(h0_vel) h1_vel = Dense( HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(h0_vel) h1_vel = BatchNormalization()(h1_vel) h1_vel = Flatten()(h1_vel) h0_pos = Dense( HIDDEN1_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(pos) h0_pos = BatchNormalization()(h0_pos) h1_pos = Dense( HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(h0_pos) h1_pos = BatchNormalization()(h1_pos) h1_pos = Flatten()(h1_pos) sensor_fused = concatenate( [dfi, gfi, dboti, gboti, dbi, gbi, h1_vel, h1_pos]) d1 = Dense(HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(sensor_fused) d1 = BatchNormalization()(d1) d2 = Dense(HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(d1) d2 = BatchNormalization()(d2) d3 = Dense(HIDDEN2_UNITS, activation='relu', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(d2) pitch = Dense( 1, activation='tanh', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(d3) roll = Dense( 1, activation='tanh', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(d3) yaw_rate = Dense( 1, activation='tanh', kernel_initializer=initializers.random_normal(stddev=1e-4), kernel_constraint=max_norm(0.07))(d3) flight_control = concatenate([pitch, roll, yaw_rate]) model = Model(input=[ depth_front_image, grey_front_image, depth_bottom_image, grey_bottom_image, depth_back_image, grey_back_image, vel, pos ], output=flight_control) print(model.summary()) return model, model.trainable_weights, depth_front_image, grey_front_image, depth_bottom_image, grey_bottom_image, depth_back_image, grey_back_image, vel, pos