def _stack_fc(self, dimension): self._flatten() fan_in = self.current_layer.get_shape().as_list()[-1] fan_out = dimension if not self.code_built: # Encoder. with tf.name_scope('EncoderHidden_' + str(self.encoder_layers_counter)): W = self.encoder_get_weights((fan_in, fan_out)) next_layer = self.encoder_activation_fn(util.batch_norm( tf.matmul(self.current_layer, W), self.is_training), name='layer') self.encoder_layers_counter += 1 else: # Decoder. with tf.name_scope('DecoderHidden_' + str(self.decoder_layers_counter)): W = self.decoder_get_weights((fan_in, fan_out)) next_layer = self.decoder_activation_fn(util.batch_norm( tf.matmul(self.current_layer, W), self.is_training), name='layer') self.decoder_layers_counter += 1 self.current_layer = next_layer
def add_residual_pre(prev_layer, z_concat=None, text_filters=None, k_h=5, k_w=5, hidden_text_filters=None, hidden_filters=None, name_func=None): filters = prev_layer.get_shape()[3].value if hidden_filters == None: hidden_filters = filters * 4 if text_filters == None: text_filters = int(filters / 2) if hidden_text_filters == None: hidden_text_filters = int(filters / 8) s = prev_layer.get_shape()[1].value bn0 = util.batch_norm(name=g_name()) bn1 = util.batch_norm(name=g_name()) low_dim = util.conv2d(util.lrelu(bn0(prev_layer)), hidden_filters, k_h=k_h, k_w=k_w, name=name_func()) residual = util.deconv2d(util.lrelu(bn1(low_dim), name=name_func()), [batch_size, s, s, filters], k_h=k_h, k_w=k_w, name=name_func()) next_layer = prev_layer + residual return next_layer
def generator(z): bs = z.get_shape()[0].value g_bn0 = util.batch_norm(name=g_name()) g_bn1 = util.batch_norm(name=g_name()) hidden_g1 = project(z, weights['gen_h1'], biases['gen_h1']) hidden_g1 = tf.reshape(hidden_g1, [-1, 5, 5, n_channel3]) output_dim2 = tf.stack([bs, 10, 10, n_channel2]) hidden_g2 = tf.nn.relu( dropout( g_bn0( deconv2d(hidden_g1, weights['gen_h2'], biases['gen_h2'], output_dim2)), FLAGS.dropout_rate)) hidden_g2.set_shape([bs, 10, 10, n_channel2]) hidden_g2 = add_residual_pre(hidden_g2, name_func=g_name) output_dim3 = tf.stack([bs, 20, 20, n_channel1]) hidden_g3 = tf.nn.relu( dropout( g_bn1( deconv2d(hidden_g2, weights['gen_h3'], biases['gen_h3'], output_dim3)), FLAGS.dropout_rate)) hidden_g3.set_shape([bs, 20, 20, n_channel1]) hidden_g3 = add_residual_pre(hidden_g3, name_func=g_name) hidden_dae = tf.reshape(hidden_g3, [bs, -1]) X_rec = project(hidden_dae, weights['gen_dae'], biases['gen_dae']) DAE_loss = tf.reduce_mean(tf.square(X_rec - z)) output_dim4 = tf.stack([tf.shape(z)[0], 40, 40, 3]) hidden_g4 = tf.nn.tanh( deconv2d(hidden_g3, weights['gen_h4'], biases['gen_h4'], output_dim4)) return hidden_g4, DAE_loss
def __call__(self, x, is_training, reuse): h = x with tf.variable_scope(self.name_scope, reuse=reuse): for i, (in_dim, out_dim) in enumerate( zip(self.layer_list, self.layer_list[1:-1])): h = linear_layer(h, in_dim, out_dim, i) h = batch_norm(h, i, is_training=is_training) h = lrelu(h) h = linear_layer(h, self.layer_list[-2], self.layer_list[-1], 'output') h = batch_norm(h, 'output', is_training=is_training) ret = tf.nn.sigmoid(h) return ret
def _stack_conv(self, filter_shape, stride=2): assert (not self.code_built) with tf.name_scope('EncoderHidden_' + str(self.encoder_layers_counter)): W = self.encoder_get_weights(filter_shape) next_layer = self.encoder_activation_fn(util.batch_norm( tf.nn.conv2d(self.current_layer, W, [1, stride, stride, 1], 'SAME'), self.is_training), name='layer') self.current_layer = next_layer self.encoder_layers_counter += 1
def resnet_v2_bottleneck_block(self, input, num_conv, strides, name, projection_shortcut=False): shortcut = input bn1 = util.batch_norm(input, name + "/bn1", self.phase) bn1 = tf.nn.relu(bn1) # 1x1 Conv layer if projection_shortcut: shortcut = self.projection_shortcut(bn1, num_conv * 4, strides, name) conv1 = util.conv2d(bn1, [1, 1, num_conv], [1, 1, 1, 1], name=name + "/conv1", use_bias=False) bn2 = util.batch_norm(conv1, name + "/bn2", self.phase) bn2 = tf.nn.relu(bn2) if strides[1] > 1: bn2 = self.pad(bn2, 3) conv2 = util.conv2d(bn2, [3, 3, num_conv], strides, name=name + "/conv2", use_bias=False, padding=('SAME' if strides[1] == 1 else 'VALID')) bn3 = util.batch_norm(conv2, name + "/bn3", self.phase) bn3 = tf.nn.relu(bn3) conv3 = util.conv2d(bn3, [1, 1, num_conv * 4], [1, 1, 1, 1], name=name + "/conv3", use_bias=False) return conv3 + shortcut
def _build_encoder(self): last_layer = self.input_layer for i in range(1, len(self.encoder_layers_size) - 1): scope = 'EncoderHidden_' + str(i) with tf.name_scope(scope): W = self.encoder_get_weights((self.encoder_layers_size[i - 1], self.encoder_layers_size[i])) current_layer = self.encoder_activation_fn( util.batch_norm(tf.matmul(last_layer, W), self.is_training), name='layer') last_layer = current_layer return last_layer
def __call__(self, x, is_training, reuse): h = x with tf.variable_scope(self.name_scope, reuse=reuse): for i, (in_dim, out_dim) in enumerate( zip(self.layer_list, self.layer_list[1:-1])): h = linear_layer(h, in_dim, out_dim, i) h = batch_norm(h, i, is_training=is_training) h = lrelu(h) mu = linear_layer(h, self.layer_list[-2], self.layer_list[-1], 'mu') log_sigma = linear_layer(h, self.layer_list[-2], self.layer_list[-1], 'log_sigma') return mu, log_sigma
def resnet_v2(self, input): strides = [1, 2, 2, 2] blocks = [3, 4, 6, 3] num_conv = [64, 128, 256, 512] input = self.pad(input, 7) res = util.conv2d(input, [7, 7, 64], stride=[1, 2, 2, 1], padding='VALID', name="conv_pre", use_bias=False) res = tf.nn.max_pool(res, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME') for j, b in enumerate(blocks): block_stride = [1, strides[j], strides[j], 1] res = self.resnet_v2_bottleneck_block(res, num_conv=num_conv[j], strides=block_stride, name="block" + str(j + 1) + "-1", projection_shortcut=True) for i in range(1, b): res = self.resnet_v2_bottleneck_block( res, num_conv=num_conv[j], strides=[1, 1, 1, 1], name="block" + str(j + 1) + "-" + str(i + 1)) res = util.batch_norm(res, "post_bn", self.phase) res = tf.nn.relu(res) self.spatial = res # Average Pooling over both spatial dimensions res = tf.reduce_mean(res, axis=[1, 2]) # With ImageNet classifier if self.with_classifier: res = util.fc(res, 1001, "imagenet_dense") return res
def _stack_deconv(self, filter_shape, output_shape, stride=2): assert (self.code_built) assert (len(self.current_layer.get_shape().as_list()) == 4) with tf.name_scope('DecoderHidden_' + str(self.decoder_layers_counter)): W = self.decoder_get_weights(filter_shape) deconv_layer = tf.nn.conv2d_transpose(self.current_layer, W, output_shape, [1, stride, stride, 1], 'SAME') deconv_layer = tf.reshape(deconv_layer, output_shape) next_layer = self.decoder_activation_fn(util.batch_norm( deconv_layer, self.is_training), name='layer') self.current_layer = next_layer self.decoder_layers_counter += 1
def build_model(self, image, reuse=False): with tf.variable_scope('is_training', reuse=True): is_training = tf.get_variable('is_training', dtype=tf.bool) with tf.variable_scope("D_" + self.signature) as scope: if reuse: scope.reuse_variables() conv_num = self.conv_infos['conv_layer_number'] conv_filter = self.conv_infos['filter'] conv_stride = self.conv_infos['stride'] prev = image for i in range(conv_num): if i == 0 or i == conv_num - 1: prev = conv_layer(prev, conv_filter[i], "d_conv_{}".format(i), activation=lrelu, batch_norm=None, reuse=reuse) else: bn = batch_norm(name="d_bn_{}".format(i)) prev = conv_layer(prev, conv_filter[i], "d_conv_{}".format(i), activation=lrelu, batch_norm=bn, reuse=reuse) setattr(self, "conv_{}".format(i), prev) return tf.sigmoid(prev)