def decoder(self, input, speaker_id): net = self.arch['decoder'] c = net['channel'] k = net['kernel'] s = net['stride'] img_h, img_c = net['hc'] featureSize = self.arch['featureSize'] z_emb = tf.layers.dense(input, img_h * img_c, bias_initializer=tf.constant_initializer(0.1)) speaker_emb = tf.layers.dense( speaker_id, img_h * img_c, bias_initializer=tf.constant_initializer(0.1)) latent = z_emb + speaker_emb x = tf.reshape(latent, [-1, img_h, 1, img_c]) for l in range(len(c)): if (l != len(c)): x = layer.deconv2d(x, c[l], k[l], s[l], activation=layer.prelu, name='decoder-L{}'.format(l)) else: x = layer.deconv2d(x, c[l], k[l], s[l], activation=tf.nn.relu, name='decoder-L{}'.format(l)) x = tf.reshape(x, [-1, featureSize]) return x, speaker_emb
def decoder(self,input_z,name = 'generate_img',is_training = True): hidden_num = 64 output_dim = 64 with tf.variable_scope(name,reuse = tf.AUTO_REUSE): x = ly.fc(input_z, hidden_num * 8 * (output_dim // 16) * (output_dim // 16),name = 'gen_fc_0') x = tf.reshape(x, shape=[self.imle_deep, output_dim // 16, output_dim // 16, hidden_num * 8]) ## 4, 4, 8*64 x = ly.deconv2d(x,hidden_num * 4,name = 'g_deconv2d_0') ### 8,8, 256 x = ly.batch_normal(x,name = 'g_deconv_bn_0',is_training = is_training) x = ly.relu(x) x = ly.deconv2d(x,hidden_num * 2,name = 'g_deconv2d_1') ### 16,16, 128 x = ly.batch_normal(x,name = 'g_deconv_bn_1',is_training = is_training) x = ly.relu(x) x = ly.deconv2d(x,hidden_num,name = 'g_deconv2d_2') ### 32,32, 64 x = ly.batch_normal(x,name = 'g_deconv_bn_2',is_training = is_training) x = ly.relu(x) x = ly.deconv2d(x, 3, name = 'g_deconv2d_3') ### 64,64, 3 x = ly.batch_normal(x,name = 'g_deconv_bn_3',is_training = is_training) x = tf.nn.tanh(x) return x
def decoder(z, label): label = tf.tile(tf.expand_dims(label, 1), [1, tstep, 1]) label = tf.reshape(label, [-1, label.get_shape()[2]]) x = tf.layers.dense(z, 25 * latentSize, bias_initializer=tf.constant_initializer(0.1)) h = tf.layers.dense(label, 25 * latentSize, bias_initializer=tf.constant_initializer(0.1)) # x_emb = tf.concat([x, h], axis = 0) x_emb = x + h x = tf.reshape(x_emb, [-1, tstep, 25, latentSize]) unit = arch['decoder'] c = unit['channel'] k = unit['kernel'] s = unit['stride'] with tf.variable_scope('decoder', reuse=tf.AUTO_REUSE): for l in range(len(c)): x = layer.deconv2d(x, c[l], k[l], s[l], tf.nn.relu, name='decoder-L{}'.format(l)) x = tf.reshape(x, [-1, N]) return x, x_emb, h
def deconv_relu_drop(x, kernalshape, samefeture=False, scope=None): with tf.name_scope(scope): W = weight_xavier_init(shape=kernalshape, n_inputs=kernalshape[0] * kernalshape[1] * kernalshape[-1], n_outputs=kernalshape[-2], activefunction='relu', variable_name=str(scope) + 'W') B = bias_variable([kernalshape[-2]], variable_name=str(scope) + 'B') dconv = tf.nn.relu(deconv2d(x, W, samefeature=samefeture) + B) return dconv
def generator(self, z, label): net = self.arch['generator'] c = net['channel'] k = net['kernel'] s = net['stride'] # x = tf.reshape(x, [-1, tstep, N, 1]) label = tf.tile(tf.expand_dims(label, 1), [1, tstep, 1]) label = tf.reshape(label, [-1, 10]) x = tf.layers.dense(z, 25 * self.latentSize, activation=None, bias_initializer=tf.constant_initializer(0.1)) h = tf.layers.dense(label, 25 * self.latentSize, activation=None, bias_initializer=tf.constant_initializer(0.1)) z_emb = x + h x = z_emb x = tf.reshape(x, [-1, 25, 1, self.latentSize]) for i in range(len(c)): x = layer.deconv2d(x, c[i], k[i], s[i], tf.nn.relu, padding='SAME', name='generator-L{}'.format(i)) x = tf.reshape(x, [-1, N]) return x
def create_conv_net(x, keep_prob, channels, layers, features_root=16, filter_size=3, pool_size=2, training=True): """ 주어진 파라미터를 이용해서 convolution u-net 그래프 생성 함 :param x: input tensor, shape [?,nx,ny,channels] :param keep_prob: dropout probability tensor :param channels: number of channels in the input image :param layers: number of layers in the net :param features_root: number of features in the first layer :param filter_size: size of the convolution filter :param pool_size: size of the max pooling operation :param summaries: Flag if summaries should be created """ logging.info( "Layers {layers}, features {features}, filter size {filter_size}x{filter_size}, pool size: {pool_size}x{pool_size}" .format(layers=layers, features=features_root, filter_size=filter_size, pool_size=pool_size)) # Placeholder for the input image with tf.name_scope("preprocessing"): nx = tf.shape(x)[1] ny = tf.shape(x)[2] x_image = tf.reshape(x, tf.stack([-1, nx, ny, channels])) in_node = x_image batch_size = tf.shape(x_image)[0] weights = [] biases = [] convs = [] pools = OrderedDict() deconv = OrderedDict() dw_h_convs = OrderedDict() up_h_convs = OrderedDict() in_size = 1000 size = in_size # down layers for layer in range(0, layers): with tf.name_scope("down_conv_{}".format(str(layer))): features = 2**layer * features_root stddev = np.sqrt(2 / (filter_size**2 * features)) if layer == 0: w1 = weight_variable( [filter_size, filter_size, channels, features], stddev, name="w1") else: w1 = weight_variable( [filter_size, filter_size, features // 2, features], stddev, name="w1") w2 = weight_variable( [filter_size, filter_size, features, features], stddev, name="w2") b1 = bias_variable([features], name="b1") b2 = bias_variable([features], name="b2") conv1 = conv2d(in_node, w1, b1, keep_prob) tmp_h_conv = tf.nn.relu( tf.layers.batch_normalization(conv1, training=training)) conv2 = conv2d(tmp_h_conv, w2, b2, keep_prob) dw_h_convs[layer] = tf.nn.relu( tf.layers.batch_normalization(conv2, training=training)) weights.append((w1, w2)) biases.append((b1, b2)) convs.append((conv1, conv2)) size -= 4 if layer < layers - 1: pools[layer] = max_pool(dw_h_convs[layer], pool_size) in_node = pools[layer] size /= 2 in_node = dw_h_convs[layers - 1] # up layers for layer in range(layers - 2, -1, -1): with tf.name_scope("up_conv_{}".format(str(layer))): features = 2**(layer + 1) * features_root stddev = np.sqrt(2 / (filter_size**2 * features)) wd = weight_variable_devonc( [pool_size, pool_size, features // 2, features], stddev, name="wd") bd = bias_variable([features // 2], name="bd") h_deconv = tf.nn.relu(deconv2d(in_node, wd, pool_size) + bd) h_deconv_concat = crop_and_concat(dw_h_convs[layer], h_deconv) deconv[layer] = h_deconv_concat w1 = weight_variable( [filter_size, filter_size, features, features // 2], stddev, name="w1") w2 = weight_variable( [filter_size, filter_size, features // 2, features // 2], stddev, name="w2") b1 = bias_variable([features // 2], name="b1") b2 = bias_variable([features // 2], name="b2") conv1 = conv2d(h_deconv_concat, w1, b1, keep_prob) h_conv = tf.nn.relu( tf.layers.batch_normalization(conv1, training=training)) conv2 = conv2d(h_conv, w2, b2, keep_prob) in_node = tf.nn.relu( tf.layers.batch_normalization(conv2, training=training)) up_h_convs[layer] = in_node weights.append((w1, w2)) biases.append((b1, b2)) convs.append((conv1, conv2)) size *= 2 size -= 4 # Output Map with tf.name_scope("output_map"): weight = weight_variable( [1, 1, features_root, 1], stddev) # 불량 CLASS의 MAP만 Loss함수에 들어가므로 channel은 "1"이 됨. bias = bias_variable([1], name="bias") conv = conv2d(in_node, weight, bias, tf.constant(1.0)) output_map = tf.squeeze(tf.nn.sigmoid(conv), axis=-1) up_h_convs["out"] = output_map variables = [] for w1, w2 in weights: variables.append(w1) variables.append(w2) for b1, b2 in biases: variables.append(b1) variables.append(b2) return output_map, variables, int(in_size - size)
def __call__(self, input): with tf.variable_scope(self.name, reuse=self.reuse): input = ly.conv2d(input, 64, kernel_size=7, strides=1, name='g_conv2d_0') input = ly.batch_normal(input, name='g_bn_0') input = tf.nn.relu(input) input = ly.conv2d(input, 128, kernel_size=3, strides=2, name='g_conv2d_1') input = ly.batch_normal(input, name='g_bn_1') input = tf.nn.relu(input) input = ly.conv2d(input, 256, kernel_size=3, strides=2, name='g_conv2d_2') input = ly.batch_normal(input, name='g_bn_2') input = tf.nn.relu(input) ### resnet for i in range(8): cell = ly.conv2d(input, 256, kernel_size=3, strides=1, name='g_conv2d_res_%s' % i) cell = ly.batch_normal(cell, name='g_res_%s' % i) cell = tf.nn.relu(cell) input = cell input = ly.deconv2d(input, kernel_size=3, strides=2, name='g_deconv2d_0') input = ly.batch_normal(input, name='g_bn_3') input = tf.nn.relu(input) input = ly.deconv2d(input, kernel_size=3, strides=2, name='g_deconv2d_1') input = ly.batch_normal(input, name='g_bn_4') input = tf.nn.relu(input) input = ly.conv2d(input, 3, kernel_size=7, strides=1, name='g_conv2d_3') input = ly.batch_normal(input, name='g_bn_5') input = tf.nn.tanh(input) input = tf.image.resize_images(input, (299, 299)) return input ## (-1,28,28,1)