用 Python 可以实现侧脸转正脸?我也要试一下( 二 )

下面还有判别器等函数定义,不加赘述 。
 
2、VGG程序设立:VGG模型网络层的搭建:
def build(self, rgb, include_fc=False):"""load variable from npy to build the VGGinput format: bgr image with shape [batch_size, h, w, 3]scale: (-1, 1)"""start_time = time.timergb_scaled = (rgb + 1) / 2 # [-1, 1] ~ [0, 1]# blue, green, red = tf.split(axis=3, num_or_size_splits=3, value=https://www.isolves.com/it/cxkf/yy/Python/2020-07-27/rgb_scaled)# bgr = tf.concat(axis=3, values=[blue - VGG_MEAN[0],# green - VGG_MEAN[1],# red - VGG_MEAN[2]])self.conv1_1 = self.conv_layer(rgb_scaled, "conv1_1")self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")self.pool1 = self.max_pool(self.conv1_2, 'pool1')self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")self.pool2 = self.max_pool(self.conv2_2, 'pool2')self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")self.conv3_2_no_activation = self.no_activation_conv_layer(self.conv3_1, "conv3_2")self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")self.conv3_4 = self.conv_layer(self.conv3_3, "conv3_4")self.pool3 = self.max_pool(self.conv3_4, 'pool3')self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")self.conv4_4_no_activation = self.no_activation_conv_layer(self.conv4_3, "conv4_4")self.conv4_4 = self.conv_layer(self.conv4_3, "conv4_4")self.pool4 = self.max_pool(self.conv4_4, 'pool4')self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")self.conv5_4_no_activation = self.no_activation_conv_layer(self.conv5_3, "conv5_4")self.conv5_4 = self.conv_layer(self.conv5_3, "conv5_4")self.pool5 = self.max_pool(self.conv5_4, 'pool5')if include_fc:self.fc6 = self.fc_layer(self.pool5, "fc6")assert self.fc6.get_shape.as_list[1:] == [4096]self.relu6 = tf.nn.relu(self.fc6)self.fc7 = self.fc_layer(self.relu6, "fc7")self.relu7 = tf.nn.relu(self.fc7)self.fc8 = self.fc_layer(self.relu7, "fc8")self.prob = tf.nn.softmax(self.fc8, name="prob")self.data_dict = Noneprint(("Finished building vgg19: %ds" % (time.time - start_time)))池化层、卷积层函数的定义:
def avg_pool(self, bottom, name):return tf.nn.avg_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)defmax_pool(self, bottom, name):return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)defconv_layer(self, bottom, name):with tf.variable_scope(name):filt = self.get_conv_filter(name)conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME')conv_biases = self.get_bias(name)bias = tf.nn.bias_add(conv, conv_biases)relu = tf.nn.relu(bias)return reludefno_activation_conv_layer(self, bottom, name):with tf.variable_scope(name):filt = self.get_conv_filter(name)conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME')conv_biases = self.get_bias(name)x = tf.nn.bias_add(conv, conv_biases)return xdeffc_layer(self, bottom, name):with tf.variable_scope(name):shape = bottom.get_shape.as_listdim = 1for d in shape[1:]:dim *= dx = tf.reshape(bottom, [-1, dim])weights = self.get_fc_weight(name)biases = self.get_bias(name)# Fully connected layer. Note that the '+' operation automatically# broadcasts the biases.fc = tf.nn.bias_add(tf.matmul(x, weights), biases)return fcdefget_conv_filter(self, name):return tf.constant(self.data_dict[name][0], name="filter")defget_bias(self, name):return tf.constant(self.data_dict[name][1], name="biases")defget_fc_weight(self, name):return tf.constant(self.data_dict[name][0], name="weights")

用 Python 可以实现侧脸转正脸?我也要试一下

文章插图
 
模型的训练设置GPU加速训练,需要配置好CUDA环境,并按照tensorflow-gpu版本 。
os.environ["CUDA_VISIBLE_DEVICES"] = "0"config = tf.ConfigProtoconfig.gpu_options.allow_growth = Truetf.reset_default_graphmodel = Sequential #创建一个神经网络对象#添加一个卷积层,传入固定宽高三通道的数据集读取和训练批次的划分:imagedir = './data/'img_label_org, label_trg, img = reader.images_list(imagedir)epoch = 800batch_size = 10total_sample_num = len(img_label_org)if total_sample_num % batch_size == 0:n_batch = int(total_sample_num / batch_size)else:n_batch = int(total_sample_num / batch_size) + 1输入输出神经元和判别器等初始化:
org_image = tf.placeholder(tf.float32,[None,128,128,3], name='org_image')trg_image = tf.placeholder(tf.float32,[None,128,128,3], name='trg_image')org_pose = tf.placeholder(tf.float32,[None,9], name='org_pose')trg_pose = tf.placeholder(tf.float32,[None,9], name='trg_pose')gen_trg, feat = model.mixgenerator(org_image, 32, org_pose, trg_pose)out_trg = model.generator(feat, 32, trg_pose)#D_abD_r, real_logit, real_pose = model.snpixdiscriminator(trg_image)D_f, fake_logit, fake_pose = model.snpixdiscriminator(gen_trg)D_f_, fake_logit_, fake_pose_ = model.snpixdiscriminator(out_trg)# fake or real D_LOSSloss_pred_r = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=real_logit, labels=tf.ones_like(D_r)))loss_pred_f = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=fake_logit_, labels=tf.zeros_like(D_f_)))loss_d_pred = loss_pred_r + loss_pred_f#pose lossloss_d_pose = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=real_pose, labels=trg_pose))loss_g_pose_ = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=fake_pose_, labels=trg_pose))loss_g_pose = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=fake_pose, labels=trg_pose))#G_LOSSloss_g_pred = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=fake_logit_, labels=tf.ones_like(D_f_)))out_pix_loss = ops.L2_loss(out_trg, trg_image)out_pre_loss, out_feat_texture = ops.vgg_loss(out_trg, trg_image)out_loss_texture = ops.texture_loss(out_feat_texture)out_loss_tv = 0.0002 * tf.reduce_mean(ops.tv_loss(out_trg))gen_pix_loss = ops.L2_loss(gen_trg, trg_image)out_g_loss = 100*gen_pix_loss + 100*out_pix_loss + loss_g_pred + out_pre_loss + out_loss_texture + out_loss_tv + loss_g_pose_gen_g_loss = 100 * gen_pix_loss + loss_g_pose# d_lossdisc_loss = loss_d_pred + loss_d_poseout_global_step = tf.Variable(0, trainable=False)gen_global_step = tf.Variable(0, trainable=False)disc_global_step = tf.Variable(0, trainable=False)start_decay_step = 500000start_learning_rate = 0.0001decay_steps = 500000end_learning_rate = 0.0out_lr = (tf.where(tf.greater_equal(out_global_step, start_decay_step), tf.train.polynomial_decay(start_learning_rate, out_global_step-start_decay_step, decay_steps, end_learning_rate, power=1.0),start_learning_rate))gen_lr = (tf.where(tf.greater_equal(gen_global_step, start_decay_step), tf.train.polynomial_decay(start_learning_rate, gen_global_step-start_decay_step, decay_steps, end_learning_rate, power=1.0),start_learning_rate))disc_lr = (tf.where(tf.greater_equal(disc_global_step, start_decay_step), tf.train.polynomial_decay(start_learning_rate, disc_global_step-start_decay_step, decay_steps, end_learning_rate, power=1.0),start_learning_rate))t_vars = tf.trainable_variablesg_gen_vars = [var for var in t_vars if 'generator' in var.name]g_out_vars = [var for var in t_vars if 'generator_1' in var.name]d_vars = [var for var in t_vars if 'discriminator' in var.name]train_gen = tf.train.AdamOptimizer(gen_lr, beta1=0.5, beta2=0.999).minimize(gen_g_loss, var_list = g_gen_vars, global_step = gen_global_step)train_out = tf.train.AdamOptimizer(out_lr, beta1=0.5, beta2=0.999).minimize(out_g_loss, var_list = g_out_vars, global_step = out_global_step)train_disc = tf.train.AdamOptimizer(disc_lr, beta1=0.5, beta2=0.999).minimize(disc_loss, var_list = d_vars, global_step = disc_global_step)saver = tf.train.Saver(tf.global_variables)


推荐阅读