def visual(self): init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) self.saver.restore(sess, self.model_path) realbatch_array, real_labels = self.data_ob.getNext_batch(0) batch_z = np.random.uniform(-1, 1, size=[self.batch_size, self.z_dim]) # visualize the weights 1 or you can change weights_2 . conv_weights = sess.run([tf.get_collection('weight_2')]) vis_square(self.vi_path, conv_weights[0][0].transpose(3, 0, 1, 2), type=1) # visualize the activation 1 ac = sess.run( [tf.get_collection('ac_2')], feed_dict={ self.images: realbatch_array[:64], self.z: batch_z, self.y: sample_label() }) vis_square(self.vi_path, ac[0][0].transpose(3, 1, 2, 0), type=0) print("the visualization finish!")
def visualize(img, otpt, grth): pred = vis_square(otpt) gt = vis_square(grth) ax1.imshow(pred) ax2.imshow(gt) ax3.imshow(pred>0.5) img = (img - img.min()) / (img.max() - img.min()) for j in range(grth.shape[0]): enlarge = cv2.resize(grth[j], (unisize, unisize)) img += np.random.rand(3) * enlarge[...,np.newaxis].repeat(3,axis=2) img = (img - img.min()) / (img.max() - img.min()) ax4.imshow(img) plt.draw() plt.pause(0.001)
def save_graphs(self): if self.iter % 150 == 0: filters = np.copy(self.net.params['conv1'][0].data) vis_square(filters.transpose(0, 2, 3, 1), im_name='conv1', batch=self.start_timestamp) del filters filters = np.copy(self.net.params['conv2'][0].data).reshape( 32 * # Filters 16, # Dimensions 4, 4) # h, w vis_square(filters, im_name='conv2', batch=self.start_timestamp) del filters if PLOT_LAYERS and self.iter % 15000 == 0: # TODO: Solve memory leak before saving more frequently by using # multi-process sandboxing. self.plot_layers()
def dcgan(operation, data_name, output_size, sample_path, log_dir, model_path, visua_path, sample_num=64): if data_name == "mnist": print("you use the mnist dataset") data_array, data_y = load_mnist(data_name) sample_z = np.random.uniform(-1, 1, size=[sample_num, 100]) y = tf.placeholder(tf.float32, [None, y_dim]) images = tf.placeholder( tf.float32, [batch_size, output_size, output_size, channel]) z = tf.placeholder(tf.float32, [None, sample_size]) z_sum = tf.summary.histogram("z", z) fake_images = gern_net(batch_size, z, y, output_size) G_image = tf.summary.image("G_out", fake_images) sample_img = sample_net(sample_num, z, y, output_size) ##the loss of gerenate network D_pro, D_logits = dis_net(images, y, weights, biases, False) D_pro_sum = tf.summary.histogram("D_pro", D_pro) G_pro, G_logits = dis_net(fake_images, y, weights, biases, True) G_pro_sum = tf.summary.histogram("G_pro", G_pro) D_fake_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( labels=tf.zeros_like(G_pro), logits=G_logits)) real_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(D_pro), logits=D_logits)) G_fake_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(G_pro), logits=G_logits)) loss = real_loss + D_fake_loss loss_sum = tf.summary.scalar("D_loss", loss) G_loss_sum = tf.summary.scalar("G_loss", G_fake_loss) merged_summary_op_d = tf.summary.merge([loss_sum, D_pro_sum]) merged_summary_op_g = tf.summary.merge( [G_loss_sum, G_pro_sum, G_image, z_sum]) t_vars = tf.trainable_variables() d_var = [var for var in t_vars if 'dis' in var.name] g_var = [var for var in t_vars if 'gen' in var.name] saver = tf.train.Saver() #if train if operation == 0: opti_D = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5).minimize(loss, var_list=d_var) opti_G = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5).minimize(G_fake_loss, var_list=g_var) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init) summary_writer = tf.summary.FileWriter(log_dir, graph=sess.graph) batch_num = 0 e = 0 step = 0 while e <= EPOCH: data_array, data_y = shuffle_data(data_array, data_y) while batch_num < len(data_array) / batch_size: step = step + 1 realbatch_array, real_labels = getNext_batch( data_array, data_y, batch_num) #Get the z batch_z = np.random.uniform( -1, 1, size=[batch_size, sample_size]) #batch_z = np.random.normal(0 , 0.2 , size=[batch_size , sample_size]) _, summary_str = sess.run( [opti_D, merged_summary_op_d], feed_dict={ images: realbatch_array, z: batch_z, y: real_labels }) summary_writer.add_summary(summary_str, step) _, summary_str = sess.run( [opti_G, merged_summary_op_g], feed_dict={ z: batch_z, y: real_labels }) summary_writer.add_summary(summary_str, step) batch_num += 1 # average_loss += loss_value if step % display_step == 0: D_loss = sess.run(loss, feed_dict={ images: realbatch_array, z: batch_z, y: real_labels }) fake_loss = sess.run(G_fake_loss, feed_dict={ z: batch_z, y: real_labels }) print( "EPOCH %d step %d: D: loss = %.7f G: loss=%.7f " % (e, step, D_loss, fake_loss)) if np.mod(step, 50) == 1: print("sample!") sample_images = sess.run(sample_img, feed_dict={ z: sample_z, y: sample_label() }) save_images( sample_images, [8, 8], './{}/train_{:02d}_{:04d}.png'.format( sample_path, e, step)) save_path = saver.save(sess, model_path) e = e + 1 batch_num = 0 save_path = saver.save(sess, model_path) print "Model saved in file: %s" % save_path #test elif operation == 1: print("Test") init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_path) sample_z = np.random.uniform(1, -1, size=[sample_num, 100]) output = sess.run(sample_img, feed_dict={ z: sample_z, y: sample_label() }) save_images( output, [8, 8], './{}/test{:02d}_{:04d}.png'.format(sample_path, 0, 0)) image = cv2.imread( './{}/test{:02d}_{:04d}.png'.format(sample_path, 0, 0), 0) cv2.imshow("test", image) cv2.waitKey(-1) print("Test finish!") #visualize else: print("Visualize") init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_path) # visualize the weights 1 or you can change weights_2 . conv_weights = sess.run([tf.get_collection('weight_2')]) vis_square(visua_path, conv_weights[0][0].transpose(3, 0, 1, 2), type=1) # visualize the activation 1 ac = sess.run([tf.get_collection('ac_2')], feed_dict={ images: data_array[:64], z: sample_z, y: sample_label() }) vis_square(visua_path, ac[0][0].transpose(3, 1, 2, 0), type=0) print("the visualization finish!") else: print("other dataset!")
# ============================================================================= # This is Vincent's code to visualize. # Right now this approach does not work, network needs to save weights # I also don't like the way it ad hoc generates output # ============================================================================= import numpy as np from load_data import load_mnist_4d from utils import vis_square _, test_data, _, test_label = load_mnist_4d('data') data = np.array([test_data[test_label == x][0] for x in np.arange(10)]) W = np.load('./weights-save/conv1-W-99.npy') b = np.load('./weights-save/conv1-b-99.npy') output = conv2d_forward(data, W, b, W.shape[2], W.shape[2] // 2).transpose( (1, 0, 2, 3)).clip(0) output = output.reshape(output.shape[0] * output.shape[1], output.shape[2], output.shape[3]) vis_square(W.squeeze(), n=1, figsize=5) vis_square(output, n=10, figsize=10)
def dcgan(operation, data_name, output_size, sample_path, log_dir, model_path, visua_path, sample_num=64): global data_array, data_y if data_name == "mnist": data_array, data_y = load_mnist(data_name) print("mnist") elif data_name == "celebA": print("celebA") data = glob(os.path.join("./data", "img_align_celeba", "*.jpg")) sample_files = data[0:64] sample = [ get_image_celebA(sample_file, input_height=108, input_width=108, resize_height=28, resize_width=28, is_crop=False, is_grayscale=False) for sample_file in sample_files ] #sample = tf.reshape(sample, [-1, 28, 28, 1]) data_array = np.array(sample).astype(np.float32) data_y = np.zeros(len(data_array)) else: print("other dataset!") #print(len(data_array)) #print("++++++++++++++++++++++++++++++++++++++++++++++") sample_z = np.random.uniform(-1, 1, size=[sample_num, 100]) y = tf.placeholder(tf.float32, [None, y_dim]) z = tf.placeholder(tf.float32, [None, sample_size]) images = tf.placeholder(tf.float32, [batch_size, output_size, output_size, channel]) fake_images = gern_net(batch_size, z, y, output_size) sample_img = sample_net(sample_num, z, y, output_size) """ the loss of gerenate network tf.zeros_like, tf.ones_like生成0和1的矩阵 discriminator: real images are labelled as 1 discriminator: images from generator (fake) are labelled as 0 generator: try to make the the fake images look real (1) sigmoid_cross_entropy_with_logits:可以对比1和(x,y)经过sigmoid后得出的概率,这里扩充多维。(某某分布属于标签1(0)的概率) """ D_pro, D_logits = dis_net(images, y, weights, biases, False) G_pro, G_logits = dis_net(fake_images, y, weights, biases, True) D_real_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(D_pro), logits=D_logits)) D_fake_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.zeros_like(G_pro), logits=G_logits)) # 判别器的loss,能分真和假 --> ones_like(D_pro) 和 zeros_like(G_pro) D_loss = D_real_loss + D_fake_loss # 生成器的loss,能生成逼真的图片 --> ones_like(G_pro) G_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(G_pro), logits=G_logits)) """ 公式表示的是: 对于判别器D : max --> E(log(D(x))) + E(log(1 - D(G(z)))) 代码是 ones_like(D_pro) + zeros_like(G_pro) 对于判别器D : min --> E(log(1 - D(G(z)))) 等价于 max --> log(D(G(Z))) 代码是ones_like(G_pro) 2014GAN论文原话:We train D to maximize the probability of assigning the correct label to both training examples and samples from G. We simultaneously train G to minimize log(1-D(G(Z))). D是max能正确区分标签的概率【max --> E(log(D(x))) + E(log(1 - D(G(z))))】,也就要使loss在训练不断最小,(这里的Max和Min代表的含义是不一样的) 同时也让G尽量去混淆它,也就是min E(log(1 - D(G(z)))) 用 max log(D(G(Z))) 替代。 .... Rather than training G to minimize log(1 - D(G(z))), we can train G to maximize log(D(G(Z))) """ """ tf.summary.histogram, tf.summary.scalar可视化显示 merge 合并在一起显示 """ z_sum = tf.summary.histogram("z", z) G_image = tf.summary.image("G_out", fake_images) D_pro_sum = tf.summary.histogram("D_pro", D_pro) G_pro_sum = tf.summary.histogram("G_pro", G_pro) loss_sum = tf.summary.scalar("D_loss", D_loss) G_loss_sum = tf.summary.scalar("G_loss", G_loss) merged_summary_op_d = tf.summary.merge([loss_sum, D_pro_sum]) merged_summary_op_g = tf.summary.merge( [G_loss_sum, G_pro_sum, G_image, z_sum]) t_vars = tf.trainable_variables() d_var = [var for var in t_vars if 'dis' in var.name] g_var = [var for var in t_vars if 'gen' in var.name] #定义保存模型变量 saver = tf.train.Saver() #if train if operation == 0: opti_D = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5).minimize(D_loss, var_list=d_var) opti_G = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5).minimize(G_loss, var_list=g_var) init = tf.global_variables_initializer() # 这句要在所有变量之后 config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init) summary_writer = tf.summary.FileWriter(log_dir, graph=sess.graph) batch_num = 0 #每次一批,就是控制一批一批的范围 step = 0 #每个batch的步长 e = 0 #epoch个数 while e <= EPOCH: #rand = np.random.randint(0, 100) rand = 0 while batch_num < len(data_array) / batch_size: step = step + 1 realbatch_array, real_labels = getNext_batch( rand, data_array, data_y, batch_num) batch_z = np.random.uniform(-1, 1, size=[batch_size, sample_size]) # batch_z = np.random.normal(0, 0.2, size=[batch_size, sample_size]) _, summary_str_D = sess.run([opti_D, merged_summary_op_d], feed_dict={ images: realbatch_array, z: batch_z, y: real_labels }) _, summary_str_G = sess.run([opti_G, merged_summary_op_g], feed_dict={ z: batch_z, y: real_labels }) batch_num += 1 # average_loss += loss_value """ 写日志和打印必要信息 """ summary_writer.add_summary(summary_str_D, step) summary_writer.add_summary(summary_str_G, step) if step % display_step == 0: D_loss_result = sess.run(D_loss, feed_dict={ images: realbatch_array, z: batch_z, y: real_labels }) G_loss_result = sess.run(G_loss, feed_dict={ z: batch_z, y: real_labels }) print( "EPOCH %d step %d: D: loss = %.7f G: loss=%.7f " % (e, step, D_loss_result, G_loss_result)) if np.mod(step, 50) == 1: sample_images = sess.run(sample_img, feed_dict={ z: sample_z, y: sample_label() }) save_images( sample_images, [8, 8], './{}/train_{:02d}_{:04d}.png'.format( sample_path, e, step)) #save_path = saver.save(sess, model_path) e = e + 1 batch_num = 0 save_path = saver.save(sess, model_path) print("Model saved in file: %s" % save_path) #test elif operation == 1: init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_path) sample_z = np.random.uniform(1, -1, size=[sample_num, 100]) output = sess.run(sample_img, feed_dict={ z: sample_z, y: sample_label() }) save_images(output, [8, 8], './{}/test{:02d}_{:04d}.png'.format(sample_path, 0, 0)) image = cv2.imread( './{}/test{:02d}_{:04d}.png'.format(sample_path, 0, 0), 0) cv2.imshow("test", image) cv2.waitKey(-1) print('./{}/test{:02d}_{:04d}.png'.format(sample_path, 0, 0)) print("Test finish!") #visualize else: init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_path) # visualize the weights 1 or you can change weights_2 . conv_weights = sess.run([tf.get_collection('weight_2')]) vis_square(visua_path, conv_weights[0][0].transpose(3, 0, 1, 2), type=1) # visualize the activation 1 ac = sess.run([tf.get_collection('ac_2')], feed_dict={ images: data_array[:64], z: sample_z, y: sample_label() }) vis_square(visua_path, ac[0][0].transpose(3, 1, 2, 0), type=0)