def main(): mnist = input_data.read_data_sets('../data/MNIST_data', one_hot=True) # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # GAN Model model = cgan.CGAN(s, is_train=False) s.run(tf.global_variables_initializer()) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state('./model/') if ckpt and ckpt.model_checkpoint_path: ckpt_name = os.path.basename(ckpt.model_checkpoint_path) saver.restore(s, os.path.join('./model/', ckpt_name)) else: print("Cannot restore checkpoint!") return False sample_z = np.random.uniform( -1., 1., [model.sample_num, model.z_dim]).astype(np.float32) # Create conditional one-hot vector, with index 5 = 1 sample_y = np.zeros(shape=[model.sample_num, model.y_dim]) sample_y[:, 5] = 1 sample_x, _ = mnist.train.next_batch(model.sample_num) sample_x = np.reshape( sample_x, [-1, model.input_height, model.input_width, model.channel]) samples = s.run(model.g, feed_dict={ model.x: sample_x, model.z: sample_z, model.y: sample_y }) samples = np.reshape( samples, [-1, model.output_height, model.output_width, model.channel]) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'test.png' # Generated image save iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir) # Close tf.Session s.close()
def main(): start_time = time.time() # Clocking start # MNIST Dataset Load mnist = DataSet(ds_path="D:\\DataSet/mnist/").data # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # CGAN Model model = cgan.CGAN(s, batch_size=train_step['batch_size']) # initializing s.run(tf.global_variables_initializer()) # Load model & Graph & Weights saved_global_step = 0 ckpt = tf.train.get_checkpoint_state('./model/') if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint model.saver.restore(s, ckpt.model_checkpoint_path) saved_global_step = int( ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) print("[+] global step : %d" % saved_global_step, " successfully loaded") else: print('[-] No checkpoint file found') sample_y = np.zeros(shape=[model.sample_num, model.n_classes]) for i in range(10): sample_y[10 * i:10 * (i + 1), i] = 1 for global_step in range(saved_global_step, train_step['global_step']): batch_x, batch_y = mnist.train.next_batch(model.batch_size) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) # Update D network _, d_loss = s.run( [model.d_op, model.d_loss], feed_dict={ model.x: batch_x, model.c: batch_y, model.z: batch_z, model.do_rate: 0.5, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.c: batch_y, model.z: batch_z, model.do_rate: 0.5, }) # Logging if global_step % train_step['logging_interval'] == 0: batch_x, batch_y = mnist.test.next_batch(model.batch_size) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) d_loss, g_loss, summary = s.run( [model.d_loss, model.g_loss, model.merged], feed_dict={ model.x: batch_x, model.c: batch_y, model.z: batch_z, model.do_rate: 0.5, }) # Print Loss print("[+] Step %08d => " % global_step, " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) # Training G model with sample image and noise sample_z = np.random.uniform( -1., 1., [model.sample_num, model.z_dim]).astype(np.float32) samples = s.run(model.g, feed_dict={ model.c: sample_y, model.z: sample_z, model.do_rate: 0.0, }) samples = np.reshape(samples, [-1, 28, 28, 1]) # Summary saver model.writer.add_summary(summary, global_step) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'train_{:08d}.png'.format( global_step) # Generated image save iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir) # Model save model.saver.save(s, results['model'], global_step) end_time = time.time() - start_time # Clocking end # Elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # Close tf.Session s.close()
def main(): start_time = time.time() # Clocking start # MNIST Dataset Load mnist = DataSet().data # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # CGAN Model model = cgan.CGAN(s) # initializing s.run(tf.global_variables_initializer()) sample_x, _ = mnist.train.next_batch(model.sample_num) sample_y = np.zeros(shape=[model.sample_num, model.n_classes]) for i in range(10): sample_y[10 * i:10 * (i + 1), i] = 1 sample_z = np.random.uniform( -1., 1., [model.sample_num, model.z_dim]).astype(np.float32) d_overpowered = False for step in range(train_step['global_step']): batch_x, batch_y = mnist.train.next_batch(model.batch_size) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) # Update D network if not d_overpowered: _, d_loss = s.run([model.d_op, model.d_loss], feed_dict={ model.x: batch_x, model.c: batch_y, model.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.c: batch_y, model.z: batch_z, }) d_overpowered = d_loss < g_loss / 2 # Logging if step % train_step['logging_interval'] == 0: batch_x, batch_y = mnist.test.next_batch(model.batch_size) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) d_loss, g_loss, summary = s.run( [model.d_loss, model.g_loss, model.merged], feed_dict={ model.x: batch_x, model.c: batch_y, model.z: batch_z, }) # Update d_overpowered d_overpowered = d_loss < g_loss / 2 # Print Loss print("[+] Step %08d => " % step, " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) # Training G model with sample image and noise samples = s.run(model.g, feed_dict={ model.c: sample_y, model.z: sample_z, }) samples = np.reshape(samples, [-1, 28, 28, 1]) # Summary saver model.writer.add_summary(summary, step) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'train_{:08d}.png'.format( step) # Generated image save iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir) # Model save model.saver.save(s, results['model'], global_step=step) end_time = time.time() - start_time # Clocking end # Elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # Close tf.Session s.close()
def main(): start_time = time.time() # Clocking start # MNIST Dataset load mnist = input_data.read_data_sets('../data/MNIST_data', one_hot=True) # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # GAN Model model = cgan.CGAN(s) # Initializing s.run(tf.global_variables_initializer()) sample_x, sample_y = mnist.train.next_batch(model.sample_num) sample_x = np.reshape(sample_x,[-1,model.input_height,model.input_width,model.channel]) sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim]).astype(np.float32) d_overpowered = False for step in range(train_step['global_step']): batch_x, batch_y = mnist.train.next_batch(model.batch_size) batch_x = np.reshape(batch_x,[-1,model.input_height,model.input_width,model.channel]) batch_x = batch_x*2 - 1 batch_z = np.random.uniform(-1., 1., size=[model.batch_size, model.z_dim]).astype(np.float32) # Update D network if not d_overpowered: _, d_loss = s.run([model.d_op, model.d_loss], feed_dict={model.x: batch_x,model.z: batch_z, model.y: batch_y,}) # Update G network _, g_loss = s.run([model.g_op, model.g_loss],feed_dict={model.x: batch_x,model.z: batch_z,model.y: batch_y,}) d_overpowered = d_loss < (g_loss / 2) if step % train_step['logging_interval'] == 0: batch_x, _ = mnist.test.next_batch(model.batch_size) batch_x = np.reshape(batch_x,[-1,model.input_height,model.input_width,model.channel]) batch_x = batch_x*2 - 1 batch_z = np.random.uniform(-1., 1., [model.batch_size, model.z_dim]).astype(np.float32) d_loss, g_loss, summary = s.run([model.d_loss, model.g_loss, model.merged], feed_dict={model.x: batch_x,model.z: batch_z,model.y: batch_y,}) d_overpowered = d_loss < (g_loss / 2) # Print loss print("[+] Step %08d => " % step," D loss : {:.8f}".format(d_loss)," G loss : {:.8f}".format(g_loss)) # Training G model with sample image and noise samples = s.run(model.g,feed_dict={model.x: sample_x,model.z: sample_z,model.y: sample_y}) samples = np.reshape(samples, [-1, model.output_height, model.output_width, model.channel]) # Summary saver model.writer.add_summary(summary, step) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'train_{:08d}.png'.format(step) # Generated image save iu.save_images(samples,size=[sample_image_height, sample_image_width],image_path=sample_dir) # Model save model.saver.save(s, results['model'], global_step=step) end_time = time.time() - start_time # Clocking end # Elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # took about 370s on my machine # Close tf.Session s.close()