def main(): # parse arguments args = parse_args() if args is None: exit() # open session with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: # declare instance for CGAN gan = CGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) # build graph gan.build_model() # show network architecture show_all_variables() # launch the graph in a session gan.train() print(" [*] Training finished!") # visualize learned generator gan.visualize_results(args.epoch-1) print(" [*] Testing finished!")
def main(): # parse arguments args = parse_args() if args is None: exit() # declare instance for GAN if args.gan_type == 'GAN': gan = GAN(args) elif args.gan_type == 'CGAN': gan = CGAN(args) elif args.gan_type == 'ACGAN': gan = ACGAN(args) elif args.gan_type == 'infoGAN': gan = infoGAN(args, SUPERVISED=True) elif args.gan_type == 'EBGAN': gan = EBGAN(args) elif args.gan_type == 'WGAN': gan = WGAN(args) elif args.gan_type == 'DRAGAN': gan = DRAGAN(args) elif args.gan_type == 'LSGAN': gan = LSGAN(args) elif args.gan_type == 'BEGAN': gan = BEGAN(args) else: raise Exception("[!] There is no option for " + args.gan_type) # launch the graph in a session gan.train() print(" [*] Training finished!") # visualize learned generator gan.visualize_results(args.epoch) print(" [*] Testing finished!")
def main(): # parse arguments args = parse_args() if args is None: exit() print(args) if args.benchmark_mode: torch.backends.cudnn.benchmark = True # declare instance for GAN if args.gan_type == 'GAN': gan = GAN(args) elif args.gan_type == 'CGAN': gan = CGAN(args) elif args.gan_type == 'ACGAN': gan = ACGAN(args) elif args.gan_type == 'infoGAN': gan = infoGAN(args, SUPERVISED=False) elif args.gan_type == 'EBGAN': gan = EBGAN(args) elif args.gan_type == 'WGAN': gan = WGAN(args) elif args.gan_type == 'WGAN_GP': gan = WGAN_GP(args) elif args.gan_type == 'DRAGAN': gan = DRAGAN(args) elif args.gan_type == 'LSGAN': generator = model.InfoGANGenerator(input_dim=62, output_dim=3, input_size=args.input_size) discriminator = model.InfoGANDiscriminator(input_dim=3, output_dim=1, input_size=args.input_size) gan = LSGAN(args, generator, discriminator) elif args.gan_type == 'LSGAN_classifier': generator = model.InfoGANGenerator(input_dim=62, output_dim=3, input_size=args.input_size) discriminator = model.InfoGANDiscriminatorClassifier( input_dim=3, output_dim=1, input_size=args.input_size, save_dir=args.save_dir, model_name=args.gan_type) gan = LSGAN(args, generator, discriminator) elif args.gan_type == 'BEGAN': gan = BEGAN(args) else: raise Exception("[!] There is no option for " + args.gan_type) # launch the graph in a session gan.train() print(" [*] Training finished!") # visualize learned generator gan.visualize_results(args.epoch) print(" [*] Testing finished!")
def main(): # parse arguments args = parse_args() if args is None: exit() if args.benchmark_mode: torch.backends.cudnn.benchmark = True ''' 如果网络的输入数据维度或类型上变化不大,设置 torch.backends.cudnn.benchmark = true 可以增加运行效率;如果网络的输入数据在每次 iteration都变化的话,会导致cudnn 每次都会去寻找一遍最优配置,这样反而会降低运行效率。 ''' # declare instance for GAN if args.gan_type == 'GAN': print("GAN is "+args.gan_type) gan = GAN(args) elif args.gan_type == 'CGAN': print("GAN is " + args.gan_type) gan = CGAN(args) elif args.gan_type == 'ACGAN': print("GAN is " + args.gan_type) gan = ACGAN(args) elif args.gan_type == 'infoGAN': print("GAN is " + args.gan_type) gan = infoGAN(args, SUPERVISED=False) elif args.gan_type == 'EBGAN': print("GAN is " + args.gan_type) gan = EBGAN(args) elif args.gan_type == 'WGAN': print("GAN is " + args.gan_type) gan = WGAN(args) elif args.gan_type == 'WGAN_GP': print("GAN is " + args.gan_type) gan = WGAN_GP(args) elif args.gan_type == 'DRAGAN': print("GAN is " + args.gan_type) gan = DRAGAN(args) elif args.gan_type == 'LSGAN': print("GAN is " + args.gan_type) gan = LSGAN(args) elif args.gan_type == 'BEGAN': print("GAN is " + args.gan_type) gan = BEGAN(args) else: raise Exception("[!] There is no option for " + args.gan_type) # launch the graph in a session gan.train() print(" [*] Training finished!") # visualize learned generator gan.visualize_results(args.epoch) print(" [*] Testing finished!")
def main(_): check_dir() print_config() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) run_option = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(config=run_option) as sess: cgan = CGAN(config=FLAGS, sess=sess) cgan.build_model() if FLAGS.is_training: cgan.train_model() if FLAGS.is_testing: cgan.test_model()
def main(): with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: cgan = CGAN(sess, epoch=100, batch_size=64, z_dim=64) cgan.bulid_model() cgan.train() print('---------Training finished!--------------')
def main(): args = parse_args() check_folder(args.dataset_path) check_folder(args.save_path) gan = CGAN(args.dataset_path, args.save_path, args.epochs, args.batch_size, args.z_dim, args.device, args.mode) if args.mode == "train": check_folder(args.save_training_img_path) gan.train() else: check_folder(args.save_testing_img_path) gan.infer()
def main(): model = 'CGAN' train = False epoch = 100 if model == 'GAN': kwargs = { 'epochs': 100, 'batch_size': 100, 'input_dim': 100, 'data_dim': 28 * 28, 'gpu_number': CUDA(gpu_idx=0), 'lrG': 2e-4, 'lrD': 2e-4, 'beta1': 0.5, 'beta2': 0.999, 'saving_epoch_interval': 2 } gan = GAN(**kwargs) elif model == 'CGAN': kwargs = { 'epochs': 100, 'batch_size': 100, 'input_dim': 100, 'data_dim': 28 * 28, 'condition_dim': 10, 'gpu_number': CUDA(gpu_idx=0), 'lrG': 2e-4, 'lrD': 2e-4, 'beta1': 0.5, 'beta2': 0.999, 'saving_epoch_interval': 2 } gan = CGAN(**kwargs) if train: gan.load(epoch=epoch) gan.train() else: gan.load(epoch=epoch) gan.test()
def main(): # parse arguments args = parse_args() if args is None: exit() if args.benchmark_mode: torch.backends.cudnn.benchmark = True # declare instance for GAN if args.gan_type == 'GAN': gan = GAN(args) elif args.gan_type == 'CGAN': gan = CGAN(args) else: raise Exception("[!] There is no option for " + args.gan_type) # launch the graph in a session gan.train() print(" [*] Training finished!") # visualize learned generator gan.visualize_results(args.epoch) print(" [*] Testing finished!")
batch_size = 32 epochs = 100 base_folder = "./PlotsForMaster/Results/" nr_batches = nr_examples // batch_size batch_log_step = nr_batches // 10 loss = "wasserstein" adv_steps = 5 feature_matching = False patchgan = True algorithm = loss + "_patch" + str(patchgan) + "_fm" + str(feature_matching) folder = initialize_folder(algorithm=algorithm, base_folder=base_folder) vanillgan = CGAN(x_dim=inpt_dim, y_dim=10, z_dim=z_dim, gen_architecture=gen_architecture, adversarial_architecture=disc_architecture, folder=folder, is_patchgan=patchgan, is_wasserstein=True) vanillgan.show_architecture() vanillgan.log_architecture() vanillgan.compile(loss="wasserstein", logged_images=logging_images, logged_labels=logging_labels, label_smoothing=0.9, feature_matching=feature_matching, optimizer=tf.train.AdamOptimizer) vanillgan.train(x_train=x_train, y_train=y_train, x_test=x_test[:500], y_test=y_test[:500],
def train(cfg): print(cfg) train_dataset = datasets.MNIST(root='data', train=True,download=True, transform=transforms.ToTensor()) train_loader = DataLoader(train_dataset, shuffle=True, batch_size=cfg.BATCH_SIZE) data_loader = {'train_loader':train_loader} device = torch.device("cuda" if torch.cuda.is_available() else "cpu") epochs = cfg.EPOCH continue_epoch = 0 model = CGAN( cfg = cfg ) model.setup() #training for epoch in range(continue_epoch + 1,continue_epoch + epochs+1): model.isTrain = True for index, data in enumerate(tqdm(data_loader['train_loader'])): model.set_input(data) # unpack data from dataset and apply preprocessing model.optimize_parameters(epoch) model.update_learning_rate(epoch) model.save_networks('last')
def main(): # parse arguments args = parse_args() if args is None: exit() # open session with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: # declare instance for GAN if args.gan_type == 'GAN': gan = GAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'CGAN': gan = CGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'ACGAN': gan = ACGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'infoGAN': gan = infoGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'EBGAN': gan = EBGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'WGAN': gan = WGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'WGAN_GP': gan = WGAN_GP(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'DRAGAN': gan = DRAGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'LSGAN': gan = LSGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'BEGAN': gan = BEGAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'VAE': gan = VAE(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'CVAE': gan = CVAE(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) elif args.gan_type == 'VAE_GAN': gan = VAE_GAN(sess, epoch=args.epoch, batch_size=args.batch_size, z_dim=args.z_dim, dataset_name=args.dataset, checkpoint_dir=args.checkpoint_dir, result_dir=args.result_dir, log_dir=args.log_dir) else: raise Exception("[!] There is no option for " + args.gan_type) # build graph gan.build_model() # show network architecture show_all_variables() # launch the graph in a session gan.train() print(" [*] Training finished!") # visualize learned generator gan.visualize_results(args.epoch - 1) print(" [*] Testing finished!")
def main(): # parse arguments args = parse_args() if args is None: exit() if args.benchmark_mode: torch.backends.cudnn.benchmark = True # declare instance for GAN if args.gan_type == 'GAN': gan = GAN(args) elif args.gan_type == 'CGAN': gan = CGAN(args) elif args.gan_type == 'ACGAN': gan = ACGAN(args) elif args.gan_type == 'infoGAN': gan = infoGAN(args, SUPERVISED=False) elif args.gan_type == 'EBGAN': gan = EBGAN(args) elif args.gan_type == 'WGAN': gan = WGAN(args) elif args.gan_type == 'WGAN_GP': gan = WGAN_GP(args) elif args.gan_type == 'DRAGAN': gan = DRAGAN(args) elif args.gan_type == 'LSGAN': gan = LSGAN(args) elif args.gan_type == 'BEGAN': gan = BEGAN(args) elif args.gan_type == 'TOGAN': gan = TOGAN(args) elif args.gan_type == 'CVAE': gan = CVAE(args) elif args.gan_type == None: pass else: raise Exception("[!] There is no option for " + args.gan_type) if args.use_fake_data: fakedata = gan.load() else: fakedata = None if args.clf_type == 'clf': clf = CLF(args, fakedata) clf.load() # clf.train() else: gan.train() # gan() # gan.load() # launch the graph in a session # clf.train() print(" [*] Training finished!") # visualize learned generator # gan.visualize_results(args.epoch) print(" [*] Testing finished!")
noise_test = np.random.normal(0, 1, [BATCH_SIZE, NOISE_DIM]).astype("float32") # constant noise to see its evolution over time y_test = np.random.randint(2, size=[BATCH_SIZE, 1]) # constant list of labels for testing # define placeholders im_pl = tf.placeholder(dtype=tf.float32, shape=[BATCH_SIZE, C, H, W]) # placeholder for real images fed to discriminator noise_pl = tf.placeholder(dtype=tf.float32, shape=[BATCH_SIZE, NOISE_DIM]) # placeholder for noise fed to generator y_pl_G = tf.placeholder(dtype=tf.float32, shape=[BATCH_SIZE, 1]) # placeholder for label fed to generator y_pl_D = tf.placeholder(dtype=tf.float32, shape=[BATCH_SIZE, 1]) # placeholder for label fed to discriminator training_pl = tf.placeholder(dtype=tf.bool, shape=[]) #model print("Building model ...") sys.stdout.flush() model= CGAN() fake_im, _ = model.generator_model(noise=noise_pl, y=y_pl_G, training=training_pl) # get fake images from generator fake_out_D, _ = model.discriminator_model(inp=fake_im, y=y_pl_G, training=training_pl) # get discriminator output on fake images real_out_D, _ = model.discriminator_model(inp=im_pl, y=y_pl_D, training=training_pl, reuse=True) # get discriminator output on real images # losses print("Losses ...") sys.stdout.flush() gen_loss = model.generator_loss(fake_out=fake_out_D, labels=tf.ones(shape=[BATCH_SIZE], dtype=tf.int32)) discr_loss = model.discriminator_loss(fake_out=fake_out_D, real_out=real_out_D, fake_labels=tf.zeros(shape=[BATCH_SIZE], dtype=tf.int32), real_labels=tf.ones(shape=[BATCH_SIZE], dtype=tf.int32)) # define trainer print("Train_op ...") sys.stdout.flush()
def main(): # parse arguments opts = parse_args() if opts is None: exit() # declare instance for GAN if opts.gan_type == 'GAN': gan = GAN(opts) elif opts.gan_type == 'CGAN': gan = CGAN(opts) elif opts.gan_type == 'ACGAN': gan = ACGAN(opts) elif opts.gan_type == 'infoGAN': gan = infoGAN(opts, SUPERVISED=True) elif opts.gan_type == 'EBGAN': gan = EBGAN(opts) elif opts.gan_type == 'WGAN': gan = WGAN(opts) elif opts.gan_type == 'WGAN_GP': gan = WGAN_GP(opts) elif opts.gan_type == 'DRAGAN': gan = DRAGAN(opts) elif opts.gan_type == 'LSGAN': gan = LSGAN(opts) elif opts.gan_type == 'BEGAN': gan = BEGAN(opts) elif opts.gan_type == 'DRGAN': gan = DRGAN(opts) elif opts.gan_type == 'AE': gan = AutoEncoder(opts) elif opts.gan_type == 'GAN3D': gan = GAN3D(opts) elif opts.gan_type == 'VAEGAN3D': gan = VAEGAN3D(opts) elif opts.gan_type == 'DRGAN3D': gan = DRGAN3D(opts) elif opts.gan_type == 'Recog3D': gan = Recog3D(opts) elif opts.gan_type == 'Recog2D': gan = Recog2D(opts) elif opts.gan_type == 'VAEDRGAN3D': gan = VAEDRGAN3D(opts) elif opts.gan_type == 'DRcycleGAN3D': gan = DRcycleGAN3D(opts) elif opts.gan_type == 'CycleGAN3D': gan = CycleGAN3D(opts) elif opts.gan_type == 'AE3D': gan = AutoEncoder3D(opts) elif opts.gan_type == 'DRGAN2D': gan = DRGAN2D(opts) elif opts.gan_type == 'DRecon3DGAN': gan = DRecon3DGAN(opts) elif opts.gan_type == 'DRecon2DGAN': gan = DRecon2DGAN(opts) elif opts.gan_type == 'DReconVAEGAN': gan = DReconVAEGAN(opts) else: raise Exception("[!] There is no option for " + opts.gan_type) if opts.resume or len(opts.eval) > 0: print(" [*] Loading saved model...") gan.load() print(" [*] Loading finished!") # launch the graph in a session if len(opts.eval) == 0: gan.train() print(" [*] Training finished!") else: print(" [*] Training skipped!") # visualize learned generator if len(opts.eval) == 0: print(" [*] eval mode is not specified!") else: if opts.eval == 'generate': gan.visualize_results(opts.epoch) elif opts.eval == 'interp_z': gan.interpolate_z(opts) elif opts.eval == 'interp_id': gan.interpolate_id(opts) elif opts.eval == 'interp_expr': gan.interpolate_expr(opts) elif opts.eval == 'recon': gan.reconstruct() elif opts.eval == 'control_expr': gan.control_expr() else: gan.manual_inference(opts) print(" [*] Testing finished!")
y_G = tf.ones([BATCH_SIZE, 1]) # labels fed to generator, always 1 # define placeholders noise_pl = tf.placeholder(dtype=tf.float32, shape=[ BATCH_SIZE, NOISE_DIM ]) # placeholder for noise fed to generator y_pl_G = tf.placeholder(dtype=tf.float32, shape=[BATCH_SIZE, 1 ]) # placeholder for label fed to generator training_pl = tf.placeholder(dtype=tf.bool, shape=[]) #model print("Building model ...") sys.stdout.flush() model = CGAN() fake_im, _ = model.generator_model( noise=noise_pl, y=y_pl_G, training=training_pl) # get fake images from generator print("Restoring latest model from {}\n".format(CHECKPOINTS_PATH)) saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint(CHECKPOINTS_PATH)) # sys.exit(0) if not os.path.exists(GENERATED_SAMPLES_DIR): os.makedirs(GENERATED_SAMPLES_DIR) counter = 0 while counter < TO_GENERATE: