def load_file(self, fname): raw = mne.io.read_raw_edf(fname, stim_channel=None, preload=True) name, _ = splitext(split(fname)[-1]) self.history.append("raw = mne.io.read_raw_edf('{}', " "stim_channel=None, preload=True)".format(fname)) self.datasets.insert_data(DataSet(name=name, fname=fname, raw=raw)) self._update_sidebar() self._update_main() self._add_recent(fname) self._update_statusbar() self._toggle_actions()
def __init__(self): PiVideoStream.__init__(self) self.ds = DataSet() self.clrSet_lab = self.ds.clrset_lab #self.clrSet_lab = unpickle("colorset_lab") self.panel = np.zeros([100, 700], np.uint8) cv2.namedWindow("Panel") cv2.createTrackbar("L", "Panel", 254, 508, self.nothing) cv2.createTrackbar("A", "Panel", 254, 508, self.nothing) cv2.createTrackbar("B", "Panel", 254, 508, self.nothing)
def filter_data(self): dialog = FilterDialog() if dialog.exec_(): low, high = dialog.low, dialog.high self.datasets.current.raw.filter(low, high) self.history.append("raw.filter({}, {})".format(low, high)) if QMessageBox.question(self, "Add new data set", "Store the current signals in a new data " "set?") == QMessageBox.Yes: new = DataSet(name="NEW", fname="", raw=self.datasets.current.raw) self.datasets.insert_data(new) self._update_sidebar() self._update_main() self._update_statusbar()
def train(modelPath, resultfile, buckets, batch_size, content, title, size, num_layers, do_decoder, num_gpus, exists, epoch, lr, min_lr, max_grad_norm): """ :param buckets: 桶 :param batch_size: :param content: encoder数据文件 :param title: decoder数据文件 :param size: lstm unit size :param num_layers: layers numbers :param do_decoder: :param num_gpus: numbers of gpu :param exists: 数据是否已经存在 :return: """ dataset = DataSet(content, title, batch_size, exists, buckets) vocab_size = dataset.vocab_size num_softmax_samples = int(dataset.vocab_size / 3) # python3需要转换成int do_decoder = False # 将参数存储成json格式文件 params = { "buckets": buckets, "batch_size": batch_size, "size": size, "num_layers": num_layers, "do_decoder": do_decoder, "num_gpus": num_gpus, "exists": exists, "epoch": epoch, "vocab_size": vocab_size, "num_softmax_samples": num_softmax_samples } conf_dir = os.path.abspath(os.path.join(os.path.curdir, 'textsum', "conf")) if not os.path.exists(conf_dir): os.mkdir(conf_dir) json.dump(params, open(conf_dir + "/params.json", "w")) model = seq2seqModel(vocab_size, buckets, size, num_layers, batch_size, num_softmax_samples, do_decoder, num_gpus) model.create_sess(sess=None) model.fit(checkpoint_dir=modelPath, resultfile=resultfile, dataset=dataset, lr=lr, min_lr=min_lr, max_grad_norm=max_grad_norm, epoch=epoch)
def main(): start_time = time.time() # Clocking start # loading CelebA DataSet ds = DataSet( height=64, width=64, channel=3, ds_image_path="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5", ds_label_path="/home/zero/hdd/DataSet/CelebA/Anno/list_attr_celeba.txt", # ds_image_path="/home/zero/hdd/DataSet/CelebA/Img/img_align_celeba/", ds_type="CelebA", use_save=False, save_file_name="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5", save_type="to_h5", use_img_scale=False, # img_scale="-1,1" ) # saving sample images test_images = np.reshape(iu.transform(ds.images[:100], inv_type='127'), (100, 64, 64, 3)) iu.save_images(test_images, size=[10, 10], image_path=results['output'] + 'sample.png', inv_type='127') ds_iter = DataIterator(x=ds.images, y=None, batch_size=train_step['batch_size'], label_off=True) # GPU configure gpu_config = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_config) with tf.Session(config=config) as s: # BEGAN Model model = began.BEGAN(s, batch_size=train_step['batch_size'], gamma=0.5) # BEGAN # Initializing s.run(tf.global_variables_initializer()) print("[*] Reading checkpoints...") 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') global_step = saved_global_step start_epoch = global_step // (ds.num_images // model.batch_size ) # recover n_epoch ds_iter.pointer = saved_global_step % ( ds.num_images // model.batch_size) # recover n_iter for epoch in range(start_epoch, train_step['epoch']): for batch_x in ds_iter.iterate(): batch_x = iu.transform(batch_x, inv_type='127') batch_x = np.reshape(batch_x, (model.batch_size, model.height, model.width, model.channel)) 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.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.z: batch_z, }) # Update k_t _, k, m_global = s.run( [model.k_update, model.k, model.m_global], feed_dict={ model.x: batch_x, model.z: batch_z, }) if global_step % train_step['logging_step'] == 0: summary = s.run(model.merged, feed_dict={ model.x: batch_x, model.z: batch_z, }) # Print loss print("[+] Epoch %03d Step %07d =>" % (epoch, global_step), " D loss : {:.6f}".format(d_loss), " G loss : {:.6f}".format(g_loss), " k : {:.6f}".format(k), " M : {:.6f}".format(m_global)) # Summary saver model.writer.add_summary(summary, global_step) # 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.z: sample_z, }) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'train_{0}.png'.format( global_step) # Generated image save iu.save_images( samples, size=[sample_image_height, sample_image_width], image_path=sample_dir, inv_type='127') # Model save model.saver.save(s, results['model'], global_step=global_step) # Learning Rate update if global_step and global_step % model.lr_update_step == 0: s.run([model.g_lr_update, model.d_lr_update]) global_step += 1 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: # InfoGAN Model model = infogan.InfoGAN(s) # Initializing s.run(tf.global_variables_initializer()) sample_x, sample_y = mnist.test.next_batch(model.sample_num) sample_x = np.reshape(sample_x, [-1] + model.image_shape[1:]) sample_z = np.random.uniform( -1., 1., [model.sample_num, model.z_dim]).astype(np.float32) sample_c = np.concatenate( (sample_y, np.zeros([model.sample_num, model.n_cont])), axis=1) 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.image_shape[1:]) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) batch_c = np.concatenate( (batch_y, np.random.uniform(-1., 1., [model.batch_size, 2])), axis=1) # 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.c: batch_c, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.x: batch_x, model.z: batch_z, model.c: batch_c, }) 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_x = np.reshape(batch_x, [-1] + model.image_shape[1:]) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) batch_c = np.concatenate( (batch_y, np.random.uniform(-1., 1., [model.batch_size, 2])), axis=1) 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.c: batch_c, }) 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.c: sample_c, }) # 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 train(): with tf.Graph().as_default(): # global step number global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) dataset = DataSet() # get training set print("The number of training images is: %d" % (dataset.cnt_samples(FLAGS.predictcsv))) csv_predict = FLAGS.predictcsv lines = dataset.load_csv(csv_predict) lines.sort() images_ph = tf.placeholder(tf.float32, [1, 229, 229, 3]) num_classes = FLAGS.num_classes restore_logits = not FLAGS.fine_tune # inference logits = model.inference(images_ph, num_classes, for_training=False, restore_logits=restore_logits) # Retain the summaries from the final tower. batchnorm_updates = tf.get_collection(slim.ops.UPDATE_OPS_COLLECTION) # saver saver = tf.train.Saver(tf.all_variables()) # Build the summary operation from the last tower summaries. summary_op = tf.merge_all_summaries() # initialization init = tf.initialize_all_variables() # session sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True, log_device_placement=FLAGS.log_device_placement)) sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir) if ckpt and ckpt.model_checkpoint_path: print("load: checkpoint %s" % (ckpt.model_checkpoint_path)) saver.restore(sess, ckpt.model_checkpoint_path) print("start to predict.") for step, line in enumerate(lines): pil_img = Image.open(line[0]) pil_img = pil_img.resize((250, 250)) img_array_r = np.asarray(pil_img) img_array_r = img_array_r[15:244, 15:244, :] img_array = img_array_r[None, ...] softmax_eval = sess.run([logits[2]], feed_dict={images_ph: img_array}) print("%s,%s,%s" % (line[0], line[1], np.argmax(softmax_eval))) print("finish to predict.") coord.request_stop() coord.join(threads) sess.close()
def main(): start_time = time.time() # Clocking start config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # DRAGAN model model = dragan.DRAGAN(s, batch_size=train_step['batch_size']) # Initializing variables 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: model.saver.restore(s, ckpt.model_checkpoint_path) saved_global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) print("[+] global step : %s" % saved_global_step, " successfully loaded") else: print('[-] No checkpoint file found') # MNIST DataSet images mnist = DataSet(ds_path="D:\\DataSet/mnist/").data for global_step in range(saved_global_step, train_step['global_step']): batch_x, _ = mnist.train.next_batch(model.batch_size) batch_x_p = get_perturbed_images(batch_x) batch_x = np.reshape(batch_x, [-1] + model.image_shape) batch_x_p = np.reshape(batch_x_p, [-1] + model.image_shape) 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.x_p: batch_x_p, model.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.z: batch_z, }) if global_step % train_step['logging_interval'] == 0: 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.x_p: batch_x_p, model.z: batch_z, }) # Print loss print("[+] Global Step %05d => " % 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.z: sample_z, }) samples = np.reshape(samples, [-1] + model.image_shape) # 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_{0}.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) global_step += 1 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 # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: image_size = crop_size = 128 # CycleGAN Model model = cyclegan.CycleGAN(s, input_height=image_size, input_width=image_size, input_channel=3, batch_size=train_step['batch_size']) # Celeb-A DataSet images data_set_name = 'vangogh2photo' ds = DataSet(input_height=image_size, input_width=image_size, input_channel=3, crop_size=crop_size, batch_size=train_step['batch_size'], name=data_set_name) img_a = ds.images_a img_b = ds.images_b print("[*] image A shape : ", img_a.shape) print("[*] image B shape : ", img_b.shape) n_sample = model.sample_num sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir_a = results['output'] + 'valid_a.png' sample_dir_b = results['output'] + 'valid_b.png' sample_a, sample_b = img_a[:n_sample], img_b[:n_sample] sample_a = np.reshape(sample_a, [-1] + model.image_shape[1:]) sample_b = np.reshape(sample_b, [-1] + model.image_shape[1:]) # Generated image save iu.save_images(sample_a, [sample_image_height, sample_image_width], sample_dir_a) iu.save_images(sample_b, [sample_image_height, sample_image_width], sample_dir_b) print("[+] pre-processing elapsed time : {:.8f}s".format(time.time() - start_time)) # Initializing s.run(tf.global_variables_initializer()) global_step = 0 for epoch in range(train_step['epochs']): # learning rate decay lr_decay = 1. if epoch >= 100 and epoch % 10 == 0: lr_decay = (train_step['epochs'] - epoch) / (train_step['epochs'] / 2.) # re-implement DataIterator for multi-input pointer = 0 num_images = min(ds.num_images_a, ds.num_images_b) for i in range(num_images // train_step['batch_size']): start = pointer pointer += train_step['batch_size'] if pointer > num_images: # if ended 1 epoch # Shuffle training DataSet perm_a, perm_b = np.arange(ds.num_images_a), np.arange( ds.num_images_b) np.random.shuffle(perm_a) np.random.shuffle(perm_b) img_a, img_b = img_a[perm_a], img_a[perm_b] start = 0 pointer = train_step['batch_size'] end = pointer batch_a = np.reshape(img_a[start:end], model.image_shape) batch_b = np.reshape(img_a[start:end], model.image_shape) for _ in range(model.n_train_critic): s.run(model.d_op, feed_dict={ model.a: batch_a, model.b: batch_b, model.lr_decay: lr_decay, }) w, gp, g_loss, cycle_loss, _ = s.run([ model.w, model.gp, model.g_loss, model.cycle_loss, model.g_op ], feed_dict={ model.a: batch_a, model.b: batch_b, model.lr_decay: lr_decay, }) if global_step % train_step['logging_step'] == 0: # Summary summary = s.run(model.merged, feed_dict={ model.a: batch_a, model.b: batch_b, model.lr_decay: lr_decay, }) # Print loss print("[+] Global Step %08d =>" % global_step, " G loss : {:.8f}".format(g_loss), " Cycle loss : {:.8f}".format(cycle_loss), " w : {:.8f}".format(w), " gp : {:.8f}".format(gp)) # Summary saver model.writer.add_summary(summary, global_step=global_step) # Training G model with sample image and noise samples_a2b = s.run(model.g_a2b, feed_dict={ model.a: sample_a, model.b: sample_b, model.lr_decay: lr_decay, }) samples_b2a = s.run(model.g_b2a, feed_dict={ model.a: sample_a, model.b: sample_b, model.lr_decay: lr_decay, }) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir_a2b = results[ 'output'] + 'train_a2b_{0}.png'.format(global_step) sample_dir_b2a = results[ 'output'] + 'train_b2a_{0}.png'.format(global_step) # Generated image save iu.save_images(samples_a2b, [sample_image_height, sample_image_width], sample_dir_a2b) iu.save_images(samples_b2a, [sample_image_height, sample_image_width], sample_dir_b2a) # Model save model.saver.save(s, results['model'], global_step=global_step) global_step += 1 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 # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # BEGAN Model model = began.BEGAN(s) # BEGAN # Initializing s.run(tf.global_variables_initializer()) # Celeb-A DataSet images ds = DataSet(input_height=32, input_width=32, input_channel=3, mode='r').images dataset_iter = DataIterator(ds, None, train_step['batch_size'], label_off=True) sample_x = ds[:model.sample_num] sample_x = np.reshape(sample_x, [-1] + model.image_shape[1:]) sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim]).astype(np.float32) # Export real image valid_image_height = model.sample_size valid_image_width = model.sample_size sample_dir = results['output'] + 'valid.png' # Generated image save iu.save_images(sample_x, size=[valid_image_height, valid_image_width], image_path=sample_dir) global_step = 0 for epoch in range(train_step['epoch']): for batch_images in dataset_iter.iterate(): batch_x = np.reshape(batch_images, [-1] + model.image_shape[1:]) 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.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.z: batch_z, }) # Update k_t _, k, m_global = s.run([model.k_update, model.k, model.m_global], feed_dict={ model.x: batch_x, model.z: batch_z, }) if global_step % train_step['logging_step'] == 0: batch_z = np.random.uniform(-1., 1., [model.batch_size, model.z_dim]).astype(np.float32) # Summary _, k, m_global, d_loss, g_loss, summary = s.run([model.k_update, model.k, model.m_global, model.d_loss, model.g_loss, model.merged], feed_dict={ model.x: batch_x, model.z: batch_z, }) # Print loss print("[+] Epoch %04d Step %07d =>" % (epoch, global_step), " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss), " k : {:.8f}".format(k), " M : {:.8f}".format(m_global)) # Summary saver model.writer.add_summary(summary, epoch) # Training G model with sample image and noise samples = s.run(model.g, feed_dict={ model.x: sample_x, model.z: sample_z, }) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'train_{0}_{1}.png'.format(epoch, 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=global_step) global_step += 1 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 # Training, Test data set # loading Cifar DataSet ds = DataSet(height=32, width=32, channel=3, ds_path='D:\\DataSet/cifar/cifar-10-batches-py/', ds_name='cifar-10') # saving sample images test_images = np.reshape(iu.transform(ds.test_images[:16], inv_type='127'), (16, 32, 32, 3)) iu.save_images(test_images, size=[4, 4], image_path=results['output'] + 'sample.png', inv_type='127') ds_iter = DataIterator(x=ds.train_images, y=None, batch_size=train_step['batch_size'], label_off=True) # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # GAN Model model = lsgan.LSGAN(s, train_step['batch_size']) # Initializing variables 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') global_step = saved_global_step start_epoch = global_step // (len(ds.train_images) // model.batch_size) ds_iter.pointer = saved_global_step % (len(ds.train_images) // model.batch_size) # recover n_iter for epoch in range(start_epoch, train_step['epoch']): for batch_x in ds_iter.iterate(): batch_x = iu.transform(batch_x, inv_type='127') batch_x = np.reshape(batch_x, [-1] + model.image_shape[1:]) 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.z: batch_z }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.x: batch_x, model.z: batch_z }) # Logging if global_step % train_step['logging_interval'] == 0: 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 }) # Print loss print("[+] Epoch %02d Step %08d => " % (epoch, 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.z: sample_z, }) # 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, inv_type='127') # Model save model.saver.save(s, results['model'], global_step) global_step += 1 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 # Loading MNIST DataSet mnist = DataSet(ds_path="D:\\DataSet/mnist/").data config = tf.ConfigProto() config.gpu_options.allow_growth = True idx = 1 divergences = ['GAN', 'KL', 'Reverse-KL', 'JS', 'JS-Weighted', 'Squared-Hellinger', 'Pearson', 'Neyman', 'Jeffrey', 'Total-Variation'] assert (0 <= idx < len(divergences)) results['output'] += '%s/' % divergences[idx] results['model'] += '%s/fGAN-model.ckpt' % divergences[idx] with tf.Session(config=config) as s: # f-GAN model model = fgan.FGAN(s, batch_size=train_step['batch_size'], divergence_method=divergences[idx], use_tricky_g_loss=True) # Initializing variables s.run(tf.global_variables_initializer()) # Load model & Graph & Weights saved_global_step = 0 ckpt = tf.train.get_checkpoint_state('./model/%s/' % divergences[idx]) 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') for global_step in range(saved_global_step, train_step['global_steps']): batch_x, _ = 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.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.x: batch_x, model.z: batch_z, }) if global_step % train_step['logging_interval'] == 0: summary = s.run(model.merged, feed_dict={ model.x: batch_x, model.z: batch_z, }) # Print loss print("[+] Global step %06d => " % 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]) samples = s.run(model.g, feed_dict={ model.z: sample_z, }) 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_{0}.png'.format(global_step) # Generated image save iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir, inv_type='255') # 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 # GPU configure gpu_config = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_config) with tf.Session(config=config) as s: # BEGAN Model model = began.BEGAN(s) # BEGAN # Initializing s.run(tf.global_variables_initializer()) # Celeb-A DataSet images ds = DataSet(height=64, width=64, channel=3, ds_path="/home/zero/hdd/DataSet/CelebA/", ds_type="CelebA").images ds_iter = DataIterator(ds, None, train_step['batch_size'], label_off=True) global_step = 0 for epoch in range(train_step['epoch']): for batch_images in ds_iter.iterate(): batch_x = np.reshape(batch_images, [-1] + model.image_shape[1:]) 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.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.z: batch_z, }) # Update k_t _, k, m_global = s.run([model.k_update, model.k, model.m_global], feed_dict={ model.x: batch_x, model.z: batch_z, }) if global_step % train_step['logging_step'] == 0: _, k, m_global, d_loss, g_loss, summary = s.run([model.k_update, model.k, model.m_global, model.d_loss, model.g_loss, model.merged], feed_dict={ model.x: batch_x, model.z: batch_z, }) # Print loss print("[+] Epoch %03d Step %07d =>" % (epoch, global_step), " D loss : {:.6f}".format(d_loss), " G loss : {:.6f}".format(g_loss), " k : {:.6f}".format(k), " M : {:.6f}".format(m_global)) # Summary saver model.writer.add_summary(summary, global_step) # 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.z: sample_z, }) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'train_{0}.png'.format(global_step) # Generated image save iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir, inv_type='127') # Model save model.saver.save(s, results['model'], global_step=global_step) # Learning Rate update if global_step and global_step % model.lr_update_step == 0: s.run([model.g_lr_update, model.d_lr_update]) global_step += 1 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(ds_path="./").data # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # WGAN Model model = wgan.WGAN( s, enable_bn=True, enable_adam=True, enable_gp=True) # Improved-WGAN with gradient penalty # Initializing s.run(tf.global_variables_initializer()) sample_x, _ = mnist.train.next_batch(model.sample_num) sample_x = sample_x.reshape([-1] + model.image_shape) sample_z = np.random.uniform( -1., 1., [model.sample_num, model.z_dim]).astype(np.float32) for step in range(train_step['global_step']): # Update critic model.critic = 5 if step % 500 == 0 or step < 25: model.critic = 100 if model.EnableGP: model.critic = 1 for _ in range(model.critic): batch_x, _ = mnist.train.next_batch(model.batch_size) batch_x = batch_x.reshape([-1] + model.image_shape) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) # Update d_clip if not model.EnableGP: s.run(model.d_clip) # Update D network _, d_loss = s.run([model.d_op, model.d_loss], feed_dict={ model.x: batch_x, model.z: batch_z }) batch_x, _ = mnist.train.next_batch(model.batch_size) batch_x = batch_x.reshape([-1] + model.image_shape) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.x: batch_x, model.z: batch_z, }) # Logging if step % train_step['logging_interval'] == 0: batch_x, _ = mnist.test.next_batch(model.batch_size) batch_x = batch_x.reshape([-1] + model.image_shape) 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, }) # 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, }) # 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 over 2hrs for 10k steps on my machine # Close tf.Session s.close()
def main(): start_time = time.time() # clocking start # Dataset dataset = DataSet(batch_size=paras['batch_size'], epoch=paras['epoch'], dataset_name="pix2pix_vangogh") config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # DiscoGAN model model = discogan.DiscoGAN(s) # load model & graph & weight 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) step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] print("[+] global step : %s" % step, " successfully loaded") else: step = 0 print('[-] No checkpoint file found') # initializing variables tf.global_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) d_overpowered = False # G loss > D loss * 2 for epoch in range(paras['epoch']): for step in range(1000): offsetA = (step * paras['batch_size']) % ( dataset.img_A.shape[0] - paras['batch_size']) offsetB = (step * paras['batch_size']) % ( dataset.img_B.shape[0] - paras['batch_size']) # batch data set batch_A = dataset.img_A[offsetA:(offsetA + paras['batch_size']), :] batch_B = dataset.img_B[offsetB:(offsetB + paras['batch_size']), :] # update D network if not d_overpowered: s.run(model.d_op, feed_dict={model.A: batch_A}) # update G network s.run(model.g_op, feed_dict={model.B: batch_B}) if epoch % paras['logging_interval'] == 0: d_loss, g_loss, summary = s.run( [model.d_loss, model.g_loss, model.merged], feed_dict={ model.A: batch_A, model.B: batch_B }) # print loss print("[+] Epoch %03d Step %04d => " % (epoch, step), "D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) # update overpowered d_overpowered = d_loss < g_loss / 2 # training G model with sample image and noise AB_samples = s.run(model.G_s2b, feed_dict={model.A: batch_A}) BA_samples = s.run(model.G_b2s, feed_dict={model.B: batch_B}) # summary saver model.writer.add_summary(summary, epoch) # export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_AB_dir = dirs[ 'sample_output'] + 'train_A_{0}_{1}.png'.format( epoch, step) sample_BA_dir = dirs[ 'sample_output'] + 'train_B_{0}_{1}.png'.format( epoch, step) # Generated image save iu.save_images( AB_samples, size=[sample_image_height, sample_image_width], image_path=sample_AB_dir) iu.save_images( BA_samples, size=[sample_image_height, sample_image_width], image_path=sample_BA_dir) # model save model.saver.save(s, dirs['model'], global_step=step) end_time = time.time() - start_time # elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # close tf.Session s.close() coord.request_stop() coord.join(threads)
def get_dataset(_d, name: str) -> DataSet: _x = _d.images n_samples = (_x.shape[0]) name = "MNIST-" + name return DataSet(_x.reshape(n_samples, 28, 28), _d.labels, name=name)
def main(): start_time = time.time() # Clocking start # Loading Cifar-10 DataSet ds = DataSet(height=32, width=32, channel=3, ds_path="D:/DataSet/cifar/cifar-10-batches-py/", ds_name='cifar-10') ds_iter = DataIterator( x=iu.transform(ds.train_images, '127'), y=ds.train_labels, batch_size=train_step['batch_size'], label_off=False ) # using label # maybe someday, i'll change this param's name # Generated image save test_images = iu.transform(ds.test_images[:100], inv_type='127') iu.save_images(test_images, size=[10, 10], image_path=results['output'] + 'sample.png', inv_type='127') # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # ACGAN Model model = acgan.ACGAN(s, batch_size=train_step['batch_size'], n_classes=ds.n_classes) # Initializing s.run(tf.global_variables_initializer()) 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 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') global_step = saved_global_step start_epoch = global_step // (len(ds.train_images) // model.batch_size ) # recover n_epoch ds_iter.pointer = saved_global_step % ( len(ds.train_images) // model.batch_size) # recover n_iter for epoch in range(start_epoch, train_step['epochs']): for batch_x, batch_y in ds_iter.iterate(): 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.y: batch_y, model.z: batch_z, }) # Update G/C networks _, g_loss, _, c_loss = s.run( [model.g_op, model.g_loss, model.c_op, model.c_loss], feed_dict={ model.x: batch_x, model.y: batch_y, model.z: batch_z, }) if global_step % train_step['logging_interval'] == 0: batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) d_loss, g_loss, c_loss, summary = s.run([ model.d_loss, model.g_loss, model.c_loss, model.merged ], feed_dict={ model.x: batch_x, model.y: batch_y, model.z: batch_z, }) # Print loss print( "[+] Epoch %04d Step %08d => " % (epoch, global_step), " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss), " C loss : {:.8f}".format(c_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.y: sample_y, model.z: sample_z, }) # 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, inv_type='127') # Model save model.saver.save(s, results['model'], global_step) global_step += 1 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 # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # AnoGAN Model # anomalies detect off (just training model) -> False # anomalies detect on (based on trained model-> True if not os.path.exists('./model'): detection = False else: detection = True model = anogan.AnoGAN(s, detect=detection) # AnoGAN global_step = 0 if detection: # Load model & Graph & Weights 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) global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] print("[+] global step : %s" % global_step, " successfully loaded") else: print('[-] No checkpoint file found') # Initializing s.run(tf.global_variables_initializer()) # Celeb-A DataSet images ds = DataSet( input_height=64, # in the paper, 108 input_width=64, # in the paper, 108 input_channel=3).images # To-Do # Getting anomaly data dataset_iter = DataIterator(ds, None, train_step['batch_size'], label_off=True) sample_x = ds[:model.sample_num] sample_x = np.reshape(sample_x, [-1] + model.image_shape[1:]) sample_z = np.random.uniform( -1., 1., [model.sample_num, model.z_dim]).astype(np.float32) # Export real image valid_image_height = model.sample_size valid_image_width = model.sample_size sample_dir = results['output'] + 'valid.png' # Generated image save iu.save_images(sample_x, size=[valid_image_height, valid_image_width], image_path=sample_dir) for epoch in range(train_step['epoch']): for batch_images in dataset_iter.iterate(): batch_x = np.reshape(batch_images, [-1] + model.image_shape[1:]) 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.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.z: batch_z, }) if global_step % train_step['logging_step'] == 0: batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) # Summary 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, }) # Print loss print("[+] Epoch %04d Step %07d =>" % (epoch, global_step), " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) # Summary saver model.writer.add_summary(summary, epoch) # Training G model with sample image and noise samples = s.run(model.g, feed_dict={ model.x: sample_x, model.z: sample_z, }) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results['output'] + 'train_{0}_{1}.png'.format( epoch, 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=global_step) global_step += 1 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(ds_path="D:/DataSet/mnist/").data # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # GAN Model model = gan.GAN(s) # 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: model.saver.restore(s, ckpt.model_checkpoint_path) saved_global_step = int( ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) print("[+] global step : %s" % saved_global_step, " successfully loaded") else: print('[-] No checkpoint file found') d_loss = 0. d_overpowered = False for global_step in range(saved_global_step, train_step['global_step']): batch_x, _ = mnist.train.next_batch(model.batch_size) batch_x = batch_x.reshape(-1, model.n_input) 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, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.x: batch_x, model.z: batch_z, }) d_overpowered = d_loss < (g_loss / 2.) if global_step % train_step['logging_interval'] == 0: batch_x, _ = 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.z: batch_z, }) d_overpowered = d_loss < (g_loss / 2.) # 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.z: sample_z, }) samples = np.reshape( samples, [-1, model.height, model.width, model.channel]) # 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)) # took about 370s on my machine # 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: # CoGAN Model model = cogan.CoGAN(s) # Initializing s.run(tf.global_variables_initializer()) sample_x, _ = mnist.test.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 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, model.image_shape) 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_1: batch_x, model.x_2: batch_x, # model.y: batch_y, model.z: batch_z, }) # Update G network _, g_loss = s.run( [model.g_op, model.g_loss], feed_dict={ model.x_1: batch_x, model.x_2: batch_x, # model.y: batch_y, model.z: batch_z, }) if step % train_step['logging_interval'] == 0: batch_x, batch_y = mnist.train.next_batch(model.batch_size) batch_x = np.reshape(batch_x, model.image_shape) 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_1: batch_x, model.x_2: batch_x, # model.y: batch_y, model.z: batch_z, }) # Print loss print("[+] Step %08d => " % step, " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) sample_z = np.random.uniform( -1., 1., [model.sample_num, model.z_dim]).astype(np.float32) # Training G model with sample image and noise samples_1 = s.run( model.g_sample_1, feed_dict={ # model.y: sample_y, model.z: sample_z, }) samples_2 = s.run( model.g_sample_2, feed_dict={ # model.y: sample_y, model.z: sample_z, }) samples_1 = np.reshape(samples_1, [-1] + model.image_shape[1:]) samples_2 = np.reshape(samples_2, [-1] + model.image_shape[1:]) # Summary saver model.writer.add_summary(summary, global_step=step) # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir_1 = results['output'] + 'train_1_{:08d}.png'.format( step) sample_dir_2 = results['output'] + 'train_2_{:08d}.png'.format( step) # Generated image save iu.save_images(samples_1, size=[sample_image_height, sample_image_width], image_path=sample_dir_1) iu.save_images(samples_2, size=[sample_image_height, sample_image_width], image_path=sample_dir_2) # 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 height, width, channel = 128, 128, 3 # loading CelebA DataSet # from 'raw images' or 'h5' use_h5 = True if not use_h5: ds = DataSet( height=height, width=height, channel=channel, # ds_image_path="D:\\DataSet/CelebA/CelebA-%d.h5" % height, ds_label_path=os.path.join(cfg.celeba, "Anno/list_attr_celeba.txt"), ds_image_path=os.path.join(cfg.celeba, "Img/img_align_celeba/"), ds_type="CelebA", use_save=True, save_file_name=os.path.join(cfg.celeba, "CelebA-%d.h5" % height), save_type="to_h5", use_img_scale=False, ) else: ds = DataSet( height=height, width=height, channel=channel, ds_image_path=os.path.join(cfg.celeba, "CelebA-%d.h5" % height), ds_label_path=os.path.join(cfg.celeba, "Anno/list_attr_celeba.txt"), # ds_image_path=os.path.join(cfg.celeba, "Img/img_align_celeba/"), ds_type="CelebA", use_save=False, # save_file_name=os.path.join(cfg.celeba, "CelebA-%d.h5" % height), # save_type="to_h5", use_img_scale=False, ) num_images = ds.num_images # saving sample images test_images = np.reshape(iu.transform(ds.images[:16], inv_type='127'), (16, height, width, channel)) iu.save_images(test_images, size=[4, 4], image_path=os.path.join(cfg.output, "sample.png"), inv_type='127') ds_iter = DataIterator(x=ds.images, y=None, batch_size=train_step['batch_size'], label_off=True) del ds # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # SAGAN Model model = sagan.SAGAN(s, height=height, width=width, channel=channel, batch_size=train_step['batch_size'], use_gp=False, use_hinge_loss=True) # Initializing s.run(tf.global_variables_initializer()) print("[*] Reading checkpoints...") saved_global_step = 0 ckpt = tf.train.get_checkpoint_state(cfg.model_path) 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') global_step = saved_global_step start_epoch = global_step // (num_images // model.batch_size ) # recover n_epoch ds_iter.pointer = saved_global_step % (num_images // model.batch_size ) # recover n_iter for epoch in range(start_epoch, train_step['epochs']): for batch_x in ds_iter.iterate(): batch_x = iu.transform(batch_x, inv_type='127') batch_x = np.reshape(batch_x, (model.batch_size, model.height, model.width, model.channel)) 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.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.x: batch_x, model.z: batch_z, }) if global_step % train_step['logging_interval'] == 0: summary = s.run(model.merged, feed_dict={ model.x: batch_x, model.z: batch_z, }) # 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_test, feed_dict={ model.z_test: sample_z, }) # is_mean, is_std = t.inception_score(iu.inverse_transform(samples, inv_type='127')) # fid_score = t.fid_score(real_img=batch_x, fake_img=samples[:model.batch_size]) # Print loss print( "[+] Epoch %04d Step %08d => " % (epoch, global_step), " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss), # " Inception Score : {:.2f} (±{:.2f})".format(is_mean, is_std), # " FID Score : {:.2f}".format(fid_score) ) # 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 = os.path.join( cfg.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, inv_type='127') # Model save model.saver.save( s, os.path.join(cfg.model_path, "SAGAN.ckpt"), global_step) global_step += 1 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 # loading CelebA DataSet ds = DataSet( height=64, width=64, channel=3, ds_image_path="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5", ds_label_path="/home/zero/hdd/DataSet/CelebA/Anno/list_attr_celeba.txt", # ds_image_path="/home/zero/hdd/DataSet/CelebA/Img/img_align_celeba/", ds_type="CelebA", use_save=False, save_file_name="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5", save_type="to_h5", use_img_scale=False, # img_scale="-1,1" ) # saving sample images test_images = np.reshape(iu.transform(ds.images[:16], inv_type='127'), (16, 64, 64, 3)) iu.save_images(test_images, size=[4, 4], image_path=results['output'] + 'sample.png', inv_type='127') ds_iter = DataIterator(x=ds.images, y=None, batch_size=train_step['batch_size'], label_off=True) # GPU configure config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # EBGAN Model model = ebgan.EBGAN( s, enable_pull_away=True) # using pull away loss # EBGAN-PT # 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: model.saver.restore(s, ckpt.model_checkpoint_path) saved_global_step = int( ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) print("[+] global step : %s" % saved_global_step, " successfully loaded") else: print('[-] No checkpoint file found') global_step = saved_global_step start_epoch = global_step // (ds.num_images // model.batch_size ) # recover n_epoch ds_iter.pointer = saved_global_step % ( ds.num_images // model.batch_size) # recover n_iter for epoch in range(start_epoch, train_step['epochs']): for batch_x in ds_iter.iterate(): batch_x = iu.transform(batch_x, inv_type='127') batch_x = np.reshape(batch_x, (model.batch_size, model.height, model.width, model.channel)) batch_z = np.random.uniform( -1., 1., [model.batch_size, model.z_dim]).astype(np.float32) _, d_loss = s.run([model.d_op, model.d_loss], feed_dict={ model.x: batch_x, model.z: batch_z, }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={ model.z: batch_z, }) # Logging if global_step % train_step['logging_interval'] == 0: summary = s.run(model.merged, feed_dict={ model.x: batch_x, model.z: batch_z, }) # Print loss print( "[+] Epoch %02d Step %08d => " % (epoch, 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.z: sample_z, }) # 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, inv_type='127') # Model save model.saver.save(s, results['model'], global_step) global_step += 1 end_time = time.time() - start_time # Clocking end # Elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # Close tf.Session s.close()
def train_test(buckets, batch_size, content, title, size, num_layers, do_decoder, num_gpus, exists, epoch, lr, min_lr, max_grad_norm): """ 支持一边训练一边测试结果 但是testmodel必须事先调用fit_predict_process :param buckets: :param batch_size: :param content: :param title: :param size: :param num_layers: :param do_decoder: :param num_gpus: :param exists: :param epoch: :param lr: :param min_lr: :param max_grad_norm: :return: """ dataset = DataSet(content, title, batch_size, exists, buckets) vocab_size = dataset.vocab_size num_softmax_samples = int(dataset.vocab_size / 3) # python3需要转换成int convert = DataConvert(buckets) test_str = '据悉,朝鲜最高领袖金正恩今日访华' bucket_id, encoder_length, encoder_inputs_ids, decoder_inputs_ids = convert.convert( test_str) do_decoder = False # 将参数存储成json格式文件 params = { "buckets": buckets, "batch_size": batch_size, "size": size, "num_layers": num_layers, "do_decoder": do_decoder, "num_gpus": num_gpus, "exists": exists, "epoch": epoch, "vocab_size": vocab_size, "num_softmax_samples": num_softmax_samples } if not os.path.exists("conf"): os.mkdir("conf") json.dump(params, open("conf/params.json", "w")) train_model = seq2seqModel(vocab_size, buckets, size, num_layers, batch_size, num_softmax_samples, do_decoder, num_gpus) train_model.create_sess(sess=None) all_batchs = sum(dataset.buckets_batch_nums.values()) train_model.fit_predict_process(lr, min_lr, max_grad_norm, epoch, all_batchs) # 必要的预处理工作 (主要部分是参数初始化) sess = train_model.sess # 获取第一个model的sess文件作为全局sess do_decoder = True # 注意 这里的train_and_test必须设置为True batch_size=1 test_model = seq2seqModel(vocab_size, buckets, size, num_layers, 1, num_softmax_samples, do_decoder, num_gpus, train_and_test=True) test_model.create_sess(sess=sess) save = False for i in range(epoch): # 训练一次 print("start train epoch:{}".format(i)) if i == epoch - 1: # 最后一次训练需要保存模型文件 train_model.fit_predict(dataset, 1, True) else: train_model.fit_predict(dataset, 1) print("start predict epoch:{}".format(i)) result = test_model.predict(bucket_id, encoder_length, encoder_inputs_ids, decoder_inputs_ids) print("输入数据:{}".format(test_str)) print("输出数据:{}".format(", ".join( list(map(lambda x: convert.all_words[x], result[0][0])))))
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 config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # DCGAN model model = dcgan.DCGAN(s, batch_size=train_step['batch_size']) # Load model & Graph & Weights 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) global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] print("[+] global step : %s" % global_step, " successfully loaded") else: global_step = 0 print('[-] No checkpoint file found') # Initializing variables s.run(tf.global_variables_initializer()) # Training, test data set dataset = DataSet(input_height=32, input_width=32, input_channel=3, name='cifar-100') dataset_iter = DataIterator(dataset.train_images, dataset.train_labels, train_step['batch_size']) sample_x = dataset.valid_images[:model.sample_num].astype( np.float32) / 225. sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim]) d_overpowered = False # G loss > D loss * 2 step = int(global_step) cont = int(step / 750) for epoch in range(cont, cont + train_step['epoch']): for batch_images, _ in dataset_iter.iterate(): batch_x = batch_images.astype(np.float32) / 225. batch_z = np.random.uniform( -1., 1., [train_step['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 }) # Update G network _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={model.z: batch_z}) d_overpowered = d_loss < g_loss / 2. if step % train_step['logging_interval'] == 0: batch_z = np.random.uniform( -1., 1., [train_step['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, }) d_overpowered = d_loss < g_loss / 2. # Print loss print("[+] Epoch %03d Step %05d => " % (epoch, 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, }) # 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_{0}_{1}.png'.format(epoch, 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) step += 1 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 config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # LAPGAN model # D/G Models are same as DCGAN model = lapgan.LAPGAN(s, batch_size=train_step['batch_size']) # Initializing variables s.run(tf.global_variables_initializer()) # Training, test data set dataset = DataSet(input_height=32, input_width=32, input_channel=3, name='cifar-10') dataset_iter = DataIterator(dataset.train_images, dataset.train_labels, train_step['batch_size']) step = 0 cont = int(step / 750) for epoch in range(cont, cont + train_step['epoch']): for batch_images, batch_labels in dataset_iter.iterate(): batch_images = batch_images.astype(np.float32) / 225. z = [] for i in range(3): z.append( np.random.uniform( -1., 1., [train_step['batch_size'], model.z_noises[i] ]).astype(np.float32)) # Update D/G networks img_fake, _, _, img_coarse, d_loss_1, g_loss_1, \ _, _, _, _, _, d_loss_2, g_loss_2, \ _, _, _, _, d_loss_3, g_loss_3, \ _, _, _, _, _, _ = s.run([ model.g[0], model.d_reals_prob[0], model.d_fakes_prob[0], model.x1_coarse, model.d_loss[0], model.g_loss[0], model.x2_fine, model.g[1], model.d_reals_prob[1], model.d_fakes_prob[1], model.x2_coarse, model.d_loss[1], model.g_loss[1], model.x3_fine, model.g[2], model.d_reals_prob[2], model.d_fakes_prob[2], model.d_loss[2], model.g_loss[2], model.d_op[0], model.g_op[0], model.d_op[1], model.g_op[1], model.d_op[2], model.g_op[2], # D/G ops ], feed_dict={ model.x1_fine: batch_images, # images model.y: batch_labels, # classes model.z[0]: z[0], model.z[1]: z[1], model.z[2]: z[2] # z-noises }) # Logging if step % train_step['logging_interval'] == 0: batch_x = batch_images[:model.sample_num] batch_y = batch_labels[:model.sample_num] z = [] for i in range(3): z.append( np.random.uniform( -1., 1., [model.sample_num, model.z_noises[i]]).astype( np.float32)) # Update D/G networks img_fake, _, _, img_coarse, d_loss_1, g_loss_1, \ _, _, _, _, _, d_loss_2, g_loss_2, \ _, _, _, _, d_loss_3, g_loss_3, \ _, _, _, _, _, _, summary = s.run([ model.g[0], model.d_reals_prob[0], model.d_fakes_prob[0], model.x1_coarse, model.d_loss[0], model.g_loss[0], model.x2_fine, model.g[1], model.d_reals_prob[1], model.d_fakes_prob[1], model.x2_coarse, model.d_loss[1], model.g_loss[1], model.x3_fine, model.g[2], model.d_reals_prob[2], model.d_fakes_prob[2], model.d_loss[2], model.g_loss[2], model.d_op[0], model.g_op[0], model.d_op[1], model.g_op[1], model.d_op[2], model.g_op[2], model.merged, ], feed_dict={ model.x1_fine: batch_x, # images model.y: batch_y, # classes model.z[0]: z[0], model.z[1]: z[1], model.z[2]: z[2] # z-noises }) # Print loss print("[+] Epoch %03d Step %05d => " % (epoch, step), " D loss : {:.8f}".format(d_loss_1.mean()), " G loss : {:.8f}".format(g_loss_1.mean())) # Training G model with sample image and noise samples = img_fake + img_coarse # Summary saver model.writer.add_summary(summary, step) # time saving # Export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_dir = results[ 'output'] + 'train_{0}_{1}.png'.format(epoch, 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) # time saving step += 1 end_time = time.time() - start_time # Clocking end # Elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # Close tf.Session s.close()
import numpy as np import tensorflow as tf import models.networks as nets from datasets import DataSet n_samples = 10000 dim_x = 50 # v = np.random.binomial(1, 0.5, size=[dim_x]) v = np.ones([dim_x]) v[np.random.choice(dim_x, dim_x - 2, replace=False)] = 0 print(v) dat_X = np.random.binomial(1, 0.5, size=[n_samples, dim_x]) parity_data = DataSet(dat_X, (dat_X @ v % 2)[:, np.newaxis]) parity_data = DataSet(parity_data.x, parity_data.get_y_1hot()) (d_train, d_test) = parity_data.random_split(ratio=0.8) data = d_train mb_size = 100 is_train = tf.placeholder(tf.bool, shape=()) x = tf.placeholder(tf.float32, shape=[None] + list(data.dim_x)) z = tf.placeholder(tf.float32, shape=[None] + list(data.dim_x)) # x = tf.cond(is_train, lambda: x + z, lambda: x) y = tf.placeholder(tf.float32, shape=[None] + list(data.dim_y)) # _flow = nets.cl.flatten(x) print(x.shape) y_hat_logits = nets.dense_net(x, [256, 64, 4, data.dim_y[0]], batch_norm=True,
def main(): start_time = time.time() # clocking start gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95) config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options) with tf.Session(config=config) as s: end_time = time.time() - start_time # BEGAN Model model = began.BEGAN(s) # initializing s.run(tf.global_variables_initializer()) # load model & graph & weight 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) global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] print("[+] global step : %s" % global_step, " successfully loaded") else: global_step = 0 print('[-] No checkpoint file found') # return # initializing variables tf.global_variables_initializer().run() # loading Celeb-A dataset ds = DataSet(input_height=64, input_width=64, input_channel=3, dataset_name="celeb-a") images = ds.images sample_z = np.random.uniform(-1., 1., size=(model.sample_num, model.z_dim)).astype(np.float32) d_overpowered = False kt = tf.Variable(0., dtype=tf.float32) # init K_0 value, 0 batch_per_epoch = int(len(images) / paras['batch_size']) for epoch in range(paras['epoch']): for step in range(batch_per_epoch): iter_ = datasets.DataIterator([images], paras['batch_size']) # k_t update # k_t+1 = K_t + lambda_k * (gamma * d_real - d_fake) kt = kt + model.lambda_k * (model.gamma * model.D_real - model.D_fake) # z update batch_z = np.random.uniform( -1., 1., [paras['batch_size'], model.z_dim]).astype(np.float32) # update D network if not d_overpowered: s.run(model.d_op, feed_dict={ model.x: 0, model.z: batch_z, model.kt: kt }) # update G network s.run(model.g_op, feed_dict={model.z: batch_z, model.kt: kt}) if global_step % paras['logging_interval'] == 0: batch_z = np.random.uniform( -1., 1., [paras['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: 0, model.z: batch_z }) # print loss print("[+] Epoch %03d Step %05d => " % (epoch, step), "D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) # update overpowered d_overpowered = d_loss < g_loss / 3 # training G model with sample image and noise samples = s.run(model.G, feed_dict={ model.x: 0, model.z: sample_z }) # 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 = dirs[ 'sample_output'] + 'train_{0}_{1}.png'.format( epoch, 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, dirs['model'], global_step=step) global_step += 1 # elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # close tf.Session s.close()
def create_dataset(self, params, data_format='json', compress=True): from datasets import DataSet dataset = DataSet() str_params = "-".join(["%s=%s" % item for item in params.iteritems()]) dataset.name = ("%s: %s" % (self.name, str_params))[:199] dataset.import_handler_id = self.id dataset.import_handler_type = self.TYPE dataset.import_handler_xml = self.data dataset.import_params = params dataset.format = data_format dataset.compress = compress dataset.save() dataset.set_file_path() return dataset
def main(): start_time = time.time() # Clocking start # Div2K - Track 1: Bicubic downscaling - x4 DataSet load with tf.device('/cpu:0'): ds = DataSet(ds_path="D:/DataSet/DIV2K/", ds_name="X4") hr, lr = ds.hr_images, ds.lr_images print("[+] Loaded HR image ", hr.shape) print("[+] Loaded LR image ", lr.shape) # GPU configure gpu_config = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, gpu_options=gpu_config) with tf.Session(config=config) as s: with tf.device("/gpu:1"): # SRGAN Model model = srgan.SRGAN(s, batch_size=train_step['batch_size']) # Load model & Graph & Weights 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) global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) print("[+] global step : %d" % global_step, " successfully loaded") else: global_step = 0 print('[-] No checkpoint file found') start_epoch = global_step // (ds.n_images // train_step['batch_size']) # Initializing s.run(tf.global_variables_initializer()) rnd = np.random.randint(0, ds.n_images_val) sample_x_hr, sample_x_lr = hr[rnd], lr[rnd] sample_x_hr, sample_x_lr = \ np.reshape(sample_x_hr, model.hr_image_shape[1:]),\ np.reshape(sample_x_lr, model.lr_image_shape[1:]) # Export real image # valid_image_height = model.sample_size # valid_image_width = model.sample_size sample_hr_dir, sample_lr_dir = results['output'] + 'valid_hr.png', results['output'] + 'valid_lr.png' # Generated image save with tf.device("/cpu:0"): iu.img_save(sample_x_hr, sample_hr_dir) iu.img_save(sample_x_lr, sample_lr_dir) for epoch in range(start_epoch, train_step['train_epochs']): if epoch >= train_step['init_epochs'] and epoch % model.lr_decay_epoch == 0: lr_decay_rate = model.lr_decay_rate ** (epoch // model.lr_decay_epoch) # Update learning rate model.d_lr *= lr_decay_rate model.g_lr *= lr_decay_rate pointer = 0 for i in range(ds.n_images // train_step['batch_size']): start = pointer pointer += train_step['batch_size'] if pointer > ds.n_images: # if 1 epoch is ended # Shuffle training DataSet perm = np.arange(ds.n_images) np.random.shuffle(perm) hr, lr = hr[perm], lr[perm] start = 0 pointer = train_step['batch_size'] end = pointer batch_x_hr, batch_x_lr = hr[start:end], lr[start:end] # reshape batch_x_hr = np.reshape(batch_x_hr, [train_step['batch_size']] + model.hr_image_shape[1:]) batch_x_lr = np.reshape(batch_x_lr, [train_step['batch_size']] + model.lr_image_shape[1:]) # Update Only G network d_loss, g_loss, g_init_loss = 0., 0., 0. if epoch <= train_step['init_epochs']: _, g_init_loss = s.run([model.g_init_op, model.g_mse_loss], feed_dict={ model.x_hr: batch_x_hr, model.x_lr: batch_x_lr, }) # Update G/D network else: _, d_loss = s.run([model.d_op, model.d_loss], feed_dict={ model.x_hr: batch_x_hr, model.x_lr: batch_x_lr, }) _, g_loss, _, _, = s.run([model.g_op, model.g_loss, model.g_adv_loss, model.g_mse_loss], feed_dict={ model.x_hr: batch_x_hr, model.x_lr: batch_x_lr, }) if i % train_step['logging_interval'] == 0: summary = s.run(model.merged, feed_dict={ model.x_hr: batch_x_hr, model.x_lr: batch_x_lr, }) # Print loss if epoch <= train_step['init_epochs']: print("[+] Epoch %04d Step %08d => " % (epoch, global_step), " G init loss : {:.8f}".format(g_init_loss)) else: print("[+] Epoch %04d Step %08d => " % (epoch, global_step), " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) # Training G model with sample image and noise sample_x_lr = np.reshape(sample_x_lr, [model.sample_num] + model.lr_image_shape[1:]) samples = s.run(model.g_test, feed_dict={ model.x_lr: sample_x_lr, }) samples = np.reshape(samples, model.hr_image_shape[1:]) # Summary saver model.writer.add_summary(summary, global_step) # Export image generated by model G # sample_image_height = model.output_height # sample_image_width = model.output_width sample_dir = results['output'] + 'train_{:08d}.png'.format(global_step) # Generated image save with tf.device("/cpu:0"): iu.img_save(samples, sample_dir) # Model save model.saver.save(s, results['model'], global_step=global_step) global_step += 1 end_time = time.time() - start_time # Clocking end # Elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # Close tf.Session s.close()
def train(): with tf.Graph().as_default(): # globalなstep数 global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) dataset = DataSet() # get trainsets print("The number of train images: %d", (dataset.cnt_samples(FLAGS.tfcsv))) images, labels = dataset.csv_inputs(FLAGS.tfcsv, FLAGS.batch_size, distorted=True) images_debug = datasets.debug(images) # get testsets #test_cnt = dataset.cnt_samples(FLAGS.testcsv) test_cnt = 100 #test_cnt = 5 print("The number of train images: %d", ()) images_test, labels_test = dataset.test_inputs(FLAGS.testcsv, test_cnt) images_test_debug = datasets.debug(images_test) input_summaries = copy.copy(tf.get_collection(tf.GraphKeys.SUMMARIES)) num_classes = FLAGS.num_classes restore_logits = not FLAGS.fine_tune # inference # logits is tuple (logits, aux_liary_logits, predictions) # logits: output of final layer, auxliary_logits: output of hidden layer, softmax: predictions logits = model.inference(images, num_classes, for_training=True, restore_logits=restore_logits) logits_test = model.inference(images_test, num_classes, for_training=False, restore_logits=restore_logits, reuse=True, dropout_keep_prob=1.0) # loss model.loss(logits, labels, batch_size=FLAGS.batch_size) model.loss_test(logits_test, labels_test, batch_size=test_cnt) losses = tf.get_collection(slim.losses.LOSSES_COLLECTION) losses_test = tf.get_collection(slim.losses.LOSSES_COLLECTION_TEST) # Calculate the total loss for the current tower. regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) total_loss = tf.add_n(losses + regularization_losses, name='total_loss') #total_loss = tf.add_n(losses, name='total_loss') total_loss_test = tf.add_n(losses_test, name='total_loss_test') # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') loss_averages_op = loss_averages.apply(losses + [total_loss]) loss_averages_test = tf.train.ExponentialMovingAverage(0.9, name='avg_test') loss_averages_op_test = loss_averages_test.apply(losses_test + [total_loss_test]) print "="*10 print "loss length:" print len(losses) print len(losses_test) print "="*10 # for l in losses + [total_loss]: # # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training # # session. This helps the clarity of presentation on TensorBoard. # loss_name = re.sub('%s_[0-9]*/' % model.TOWER_NAME, '', l.op.name) # # Name each loss as '(raw)' and name the moving average version of the loss # # as the original loss name. # tf.scalar_summary(loss_name + ' (raw)', l) # tf.scalar_summary(loss_name, loss_averages.average(l)) # loss to calcurate gradients # with tf.control_dependencies([loss_averages_op]): total_loss = tf.identity(total_loss) tf.scalar_summary("loss", total_loss) with tf.control_dependencies([loss_averages_op_test]): total_loss_test = tf.identity(total_loss_test) tf.scalar_summary("loss_eval", total_loss_test) # Reuse variables for the next tower. #tf.get_variable_scope().reuse_variables() # Retain the summaries from the final tower. summaries = tf.get_collection(tf.GraphKeys.SUMMARIES) # Retain the Batch Normalization updates operations only from the # final tower. Ideally, we should grab the updates from all towers # but these stats accumulate extremely fast so we can ignore the # other stats from the other towers without significant detriment. batchnorm_updates = tf.get_collection(slim.ops.UPDATE_OPS_COLLECTION) # add input summaries # summaries.extend(input_summaries) # train_operation and operation summaries train_op = train_operation.train(total_loss, global_step, summaries, batchnorm_updates) # trainable variables's summary #for var in tf.trainable_variables(): # summaries.append(tf.histogram_summary(var.op.name, var)) # saver saver = tf.train.Saver(tf.all_variables()) # Build the summary operation from the last tower summaries. #summary_op = tf.merge_summary(summaries) summary_op = tf.merge_all_summaries() # initialization init = tf.initialize_all_variables() # session sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True, log_device_placement=FLAGS.log_device_placement)) sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) if FLAGS.pretrained_model_checkpoint_path: assert tf.gfile.Exists(FLAGS.pretrained_model_checkpoint_path) variables_to_restore = tf.get_collection( slim.variables.VARIABLES_TO_RESTORE) restorer = tf.train.Saver(variables_to_restore) restorer.restore(sess, FLAGS.pretrained_model_checkpoint_path) print('%s: Pre-trained model restored from %s' % (datetime.now(), FLAGS.pretrained_model_checkpoint_path)) summary_writer = tf.train.SummaryWriter( FLAGS.train_dir, graph_def=sess.graph.as_graph_def(add_shapes=True)) for step in xrange(FLAGS.max_steps): start_time = time.time() _, logits_eval, loss_value, labels_eval, images_debug_eval = sess.run([train_op, logits[0], total_loss, labels, images_debug]) duration = time.time() - start_time dataset.output_images(images_debug_eval, "debug", "train") assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: examples_per_sec = FLAGS.batch_size / float(duration) format_str = ('train %s: step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)') print(format_str % (datetime.now(), step, loss_value, examples_per_sec, duration)) if step % 100 == 0: print("predict:") print type(logits_eval) print logits_eval.shape print logits_eval.argmax(1) print("target:") print labels_eval summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) test_start_time = time.time() logits_test_eval, total_loss_test_val, labels_test_eval, images_test_debug_eval = sess.run([logits_test[0], total_loss_test, labels_test, images_test_debug]) test_duration = time.time() - test_start_time dataset.output_images(images_test_debug_eval, "debug_test", "test") print("test predict:") print type(logits_test_eval) print logits_test_eval.shape print logits_test_eval.argmax(1) print("test target:") print labels_test_eval test_examples_per_sec = test_cnt / float(test_duration) format_str_test = ('test %s: step %d, loss = %.2f, (%.1f examples/sec; %.3f sec/batch)') print(format_str_test % (datetime.now(), step, total_loss_test_val, test_examples_per_sec, test_duration)) # Save the model checkpoint periodically. if step % 5000 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) coord.request_stop() coord.join(threads) sess.close()