def main(): parser = argparse.ArgumentParser(description='Chainer example: DCGAN') parser.add_argument('--batchsize', '-b', type=int, default=50, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=1000, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') parser.add_argument('--dataset', '-i', default='', help='Directory of image files. Default is cifar-10.') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=1000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') # Set up a neural network to train gen = Generator(n_hidden=args.n_hidden) dis = Discriminator() if args.gpu >= 0: # Make a specified GPU current chainer.backends.cuda.get_device_from_id(args.gpu).use() gen.to_gpu() # Copy the model to the GPU dis.to_gpu() # Setup an optimizer def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer_hooks.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) if args.dataset == '': # Load the CIFAR10 dataset if args.dataset is not specified train, _ = chainer.datasets.get_cifar10(withlabel=False, scale=255.) else: all_files = os.listdir(args.dataset) image_files = [f for f in all_files if ('png' in f or 'jpg' in f)] print('{} contains {} image files'.format(args.dataset, len(image_files))) train = chainer.datasets\ .ImageDataset(paths=image_files, root=args.dataset) train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 10, 10, args.seed, args.out), trigger=snapshot_interval) if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
# optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) # Load the GMM dataset dataset, _ = chainer.datasets.get_svhn(withlabel=False, scale=255.) train_iter = chainer.iterators.SerialIterator(dataset, batch_size) print("# Data size: {}".format(len(dataset)), end="\n\n") # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=gpu) trainer = training.Trainer(updater, (epoch, 'epoch'), out=out) snapshot_interval = (5, 'epoch') display_interval = (1, 'epoch') trainer.extend(extensions.dump_graph("gen/loss", out_name="gen.dot")) trainer.extend(extensions.dump_graph("dis/loss", out_name="dis.dot")) trainer.extend( extensions.snapshot(filename='snapshot_epoch_{.updater.epoch}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_epoch_{.updater.epoch}.npz'), trigger=snapshot_interval)
def main(): config = get_config() # general general = config['general'] dataroot = general['dataroot'] workers = general['workers'] gpu = general['gpu'] batch_size = general['batch_size'] lr = general['lr'] beta = general['beta'] epoch = general['epoch'] image_size = general['image_size'] # model config model_config = config['model'] nz = model_config['nz'] ndf = model_config['ndf'] ngf = model_config['ngf'] dataset = datasets.ImageFolder(root=dataroot, transform=transforms.Compose([ transforms.Resize(image_size), transforms.CenterCrop(image_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) dataloader = data.DataLoader( dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=workers, ) net_D = Discriminator(ndf) net_G = Generator(nz, ngf) if gpu: device = torch.device('cuda') net_D = nn.DataParallel(net_D.to(device)) net_G = nn.DataParallel(net_G.to(device)) else: device = 'cpu' def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: nn.init.normal_(m.weight.data, 0.0, 0.02) elif classname.find('BatchNorm') != -1: nn.init.normal_(m.weight.data, 1.0, 0.02) nn.init.constant_(m.bias.data, 0) net_D.apply(weights_init) net_G.apply(weights_init) optimizers = { 'Discriminator': optim.Adam(net_D.parameters(), lr, betas=(beta, 0.999)), 'Generator': optim.Adam(net_G.parameters(), lr, betas=(beta, 0.999)) } models = { 'Discriminator': net_D, 'Generator': net_G, } updater = DCGANUpdater(optimizers, models, dataloader, device) trainer = Trainer(updater, {'epoch': epoch}, 'test') trainer.extend( LogReport([ 'iteration', 'training/D_real', 'training/D_fake', 'training/D_loss', 'training/G_loss', 'elapsed_time', ], {'iteration': 100})) trainer.extend(ProgressBar(10)) save_trigger = MinValueTrigger('training/G_loss', trigger={'iteration': 100}) trainer.extend(SnapshotModel(trigger=save_trigger)) trainer.run() print(config)
def main(): parser = argparse.ArgumentParser(description='DCGAN') parser.add_argument('--batchsize', '-b', type=int, default=64, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=500, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') parser.add_argument('--dataset', '-i', default='', help='Directory of image files.') parser.add_argument('--out', '-o', default='result', help='Directory to output the result image') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--gennum', '-v', default=10, help='visualize image rows and columns number') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=1000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') #学習モデルの作成 gen = Generator(n_hidden=args.n_hidden) dis = Discriminator() if args.gpu >= 0: #modelをGPU用に変換 chainer.cuda.get_device(args.gpu).use() gen.to_gpu() dis.to_gpu() #oputimizerのsetup def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) if args.dataset == '': #データセットの読み込み defaultはcifar10 train, _ = chainer.datasets.get_cifar10(withlabel=False, scale=255.) else: all_files = os.listdir(args.dataset) image_files = [f for f in all_files if ('png' in f or 'jpg' in f)] print('{} contains {} image files'.format(args.dataset, len(image_files))) train = chainer.datasets\ .ImageDataset(paths=image_files, root=args.dataset) train_iter = chainer.iterators.SerialIterator(train, args.batchsize) #trainerのセットアップ updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.epoch}.npz'), trigger=(100, 'epoch')) trainer.extend(extensions.snapshot_object(gen, 'gen_iter_{.updater.epoch}.npz'), trigger=(100, 'epoch')) trainer.extend(extensions.snapshot_object(dis, 'dis_iter_{.updater.epoch}.npz'), trigger=(100, 'epoch')) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, int(args.gennum), int(args.gennum), args.seed, args.out), trigger=snapshot_interval) if args.resume: #学習済みmodelの読み込み chainer.serializers.load_npz(args.resume, trainer) #学習の実行 trainer.run() chainer.serializers.save_npz("genmodel{0}".format(args.datasets), trainer)
def main(): parser = argparse.ArgumentParser(description='Train GAN') parser.add_argument('--batchsize', '-b', type=int, default=64, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=100, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') parser.add_argument('--dataset', '-i', default='data/celebA/', help='Directory of image files.') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=10000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=1000, help='Interval of displaying log to console') parser.add_argument('--unrolling_steps', type=int, default=0) args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# batchsize: {}'.format(args.batchsize)) print('# epoch: {}'.format(args.epoch)) print('') # Set up a neural network to train gen = Generator() dis = Discriminator(unrolling_steps=args.unrolling_steps) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() gen.to_gpu() dis.to_gpu() # Setup an optimizer def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) # Setup a dataset all_files = os.listdir(args.dataset) image_files = [f for f in all_files if ('png' in f or 'jpg' in f)] print('{} contains {} image files'.format(args.dataset, len(image_files))) train = CelebADataset(paths=image_files, root=args.dataset) train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') trainer.extend(extensions.snapshot( filename='snapshot_gan_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend( extensions.LogReport(trigger=display_interval, log_name='train_gan.log')) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend( extensions.PlotReport(['gen/loss', 'dis/loss'], trigger=display_interval, file_name='gan-loss.png')) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 10, 10, args.seed, args.out), trigger=snapshot_interval) if args.resume: chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
def main(): parser = argparse.ArgumentParser(description='LSGAN') parser.add_argument('--batchsize', '-b', type=int, default=20, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=1000, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') parser.add_argument('--dataset', '-i', default='', help='Directory of image files. Default is cifar-10.') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--image_size', type=int, default=64, help='Size of output image') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console (iter)') parser.add_argument('--preview_interval', type=int, default=1, help='Interval of preview (epoch)') parser.add_argument('--snapshot_interval', type=int, default=10, help='Interval of snapshot (epoch)') args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') # Set up a neural network to train gen = net.Generator(n_hidden=args.n_hidden, image_size=args.image_size) # dis = Discriminator() dis = net.Discriminator2() # dis = net.Discriminator3() if args.gpu >= 0: # Make a specified GPU current chainer.backends.cuda.get_device_from_id(args.gpu).use() gen.to_gpu() # Copy the model to the GPU dis.to_gpu() # Setup an optimizer def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) if args.dataset == '': # Load the CIFAR10 dataset if args.dataset is not specified train, _ = chainer.datasets.get_cifar10(withlabel=False, scale=255.) else: all_files = os.listdir(args.dataset) image_files = [f for f in all_files if ('png' in f or 'jpg' in f)] print('{} contains {} image files'.format(args.dataset, len(image_files))) train = chainer.datasets.ImageDataset(paths=image_files, root=args.dataset) # train_iter = chainer.iterators.SerialIterator(train, args.batchsize) train_iter = chainer.iterators.MultiprocessIterator(train, args.batchsize, n_processes=4) def resize_converter(batch, device=None, padding=None): new_batch = [] for image in batch: C, W, H = image.shape if C == 4: image = image[:3, :, :] if W < H: offset = (H - W) // 2 image = image[:, :, offset:offset + W] elif W > H: offset = (W - H) // 2 image = image[:, offset:offset + H, :] image = image.transpose(1, 2, 0) image = imresize(image, (args.image_size, args.image_size), interp='bilinear') image = image.transpose(2, 0, 1) image = image / 255. # 0. ~ 1. # Augumentation... Random vertical flip if np.random.rand() < 0.5: image = image[:, :, ::-1] # Augumentation... Tone correction mode = np.random.randint(4) # mode == 0 -> no correction if mode == 1: gain = 0.2 * np.random.rand() + 0.9 # 0.9 ~ 1.1 image = np.power(image, gain) elif mode == 2: gain = 1.5 * np.random.rand() + 1e-10 # 0 ~ 1.5 image = np.tanh(gain * (image - 0.5)) range_min = np.tanh(gain * (-0.5)) # @x=0.5 range_max = np.tanh(gain * 0.5) # @x=1.0 image = (image - range_min) / (range_max - range_min) elif mode == 3: gain = 2.0 * np.random.rand() + 1e-10 # 0 ~ 1.5 image = np.sinh(gain * (image - 0.5)) range_min = np.tanh(gain * (-0.5)) # @x=0.5 range_max = np.tanh(gain * 0.5) # @x=1.0 image = (image - range_min) / (range_max - range_min) image = 2. * image - 1. new_batch.append(image.astype(np.float32)) return concat_examples(new_batch, device=device, padding=padding) # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu, converter=resize_converter) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) display_interval = (args.display_interval, 'iteration') preview_interval = (args.preview_interval, 'epoch') snapshot_interval = (args.snapshot_interval, 'epoch') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 10, 10, args.seed, args.out), trigger=preview_interval) if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
def main(): parser = argparse.ArgumentParser(description='Chainer example: DCGAN') parser.add_argument('--batchsize', '-b', type=int, default=50, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=1000, help='Number of sweeps over the dataset to train') parser.add_argument('--device', '-d', type=str, default='-1', help='Device specifier. Either ChainerX device ' 'specifier or an integer. If non-negative integer, ' 'CuPy arrays with specified device id are used. If ' 'negative integer, NumPy arrays are used') parser.add_argument('--dataset', '-i', default='', help='Directory of image files. Default is cifar-10.') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', type=str, help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=10000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=1000, help='Interval of displaying log to console') parser.add_argument('--use-pixelshuffler', action='store_true') args = parser.parse_args() if chainer.get_dtype() == np.float16: warnings.warn( 'This example may cause NaN in FP16 mode.', RuntimeWarning) device = chainer.get_device(args.device) device.use() print('Device: {}'.format(device)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') # Set up a neural network to train gen = Generator(n_hidden=args.n_hidden, use_pixelshuffler=args.use_pixelshuffler) dis = Discriminator() gen.to_device(device) # Copy the model to the device dis.to_device(device) # Setup an optimizer def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook( chainer.optimizer_hooks.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) if args.dataset == '': # Load the CIFAR10 dataset if args.dataset is not specified train, _ = chainer.datasets.get_cifar10(withlabel=False, scale=255.) elif args.dataset.endswith('h5'): print('parsing as food-101 from kaggle') train = h5py.File(args.dataset, 'r')['images'].value.astype(np.float32).transpose(0, 3, 1, 2) elif args.dataset.endswith('npy'): print('parsing as preprocessed npy') train = np.load(args.dataset).astype(np.float32) else: all_files = os.listdir(args.dataset) image_files = [f for f in all_files if ('png' in f or 'jpg' in f)] print('{} contains {} image files' .format(args.dataset, len(image_files))) train = chainer.datasets\ .ImageDataset(paths=image_files, root=args.dataset) train = chainer.datasets.TransformDataset(train, DataAugmentation()) # Setup an iterator train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Setup an updater updater = DCGANUpdater( models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis}, device=device) # Setup a trainer trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=1000)) trainer.extend( out_generated_image( gen, dis, 10, 10, args.seed, args.out), trigger=snapshot_interval) if args.resume is not None: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
def main(): parser = argparse.ArgumentParser(description='Chainer example: DCGAN') parser.add_argument( '--batchsize', '-b', type=int, default=50, # default=50 help='Number of images in each mini-batch') parser.add_argument( '--epoch', '-e', type=int, default=10000, # defalt=500 help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=0, help='GPU ID (negative value indicates CPU)') parser.add_argument( '--dataset', '-i', default='train_illya.txt', # defalt='' help='Directory of image files. Default is cifar-10.') parser.add_argument( '--out', '-o', default='result_anime_illya4', # defalt='result' help='Directory to output the result') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument( '--snapshot_interval', type=int, default=1000, # defalt=1000 help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') parser.add_argument( '--noise_sigma', type=float, default=0.2, # best: 0.2 help='Std of noise added the descriminator') args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') # Set up a neural network to train gen = Generator(n_hidden=args.n_hidden, wscale=0.02) dis = Discriminator(init_sigma=args.noise_sigma, wscale=0.02) if args.gpu >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(args.gpu).use() gen.to_gpu() # Copy the model to the GPU dis.to_gpu() # Setup an optimizer # https://elix-tech.github.io/ja/2017/02/06/gan.html def make_optimizer(model, alpha=0.0002, beta1=0.5): # 元論文 optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.00001), 'hook_dec') return optimizer # # For WGAN # # Not good # def make_optimizer(model, alpha=0.0001, beta1=0.0, beta2=0.9): # optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1, beta2=beta2) # optimizer.setup(model) # return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) if args.dataset == '': # Load the CIFAR10 dataset if args.dataset is not specified train, _ = cifar.get_cifar10(withlabel=False, scale=255.) else: resized_paths = resize_data(args.dataset, insize + 16) train = PreprocessedDataset(resized_paths, crop_size=insize) print('{} contains {} image files'.format(args.dataset, train.__len__())) train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu) # updater = WGANGPUpdater( # models=(gen, dis), # iterator=train_iter, # optimizer={ # 'gen': opt_gen, 'dis': opt_dis}, # device=args.gpu, # l=10, # n_c=5 # ) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend( extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', #'epoch', 'iteration', 'gen/loss', 'dis/loss/gan', 'dis/loss/grad', # For WGAN ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 5, 5, args.seed, args.out), trigger=snapshot_interval) # 次第にDescriminatorのノイズを低減させる @training.make_extension(trigger=snapshot_interval) def shift_sigma(trainer): s = dis.shift_sigma(alpha_sigma=0.9) print('sigma={}'.format(s)) print('') trainer.extend(shift_sigma) # 通常のDCGANではコメントイン if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
def main(): parser = argparse.ArgumentParser(description='ChainerMN example: DCGAN') parser.add_argument('--batchsize', '-b', type=int, default=50, help='Number of images in each mini-batch') parser.add_argument('--communicator', type=str, default='hierarchical', help='Type of communicator') parser.add_argument('--epoch', '-e', type=int, default=1000, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', action='store_true', help='Use GPU') parser.add_argument('--dataset', '-i', default='', help='Directory of image files. Default is cifar-10.') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--gen_model', '-r', default='', help='Use pre-trained generator for training') parser.add_argument('--dis_model', '-d', default='', help='Use pre-trained discriminator for training') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=1000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') args = parser.parse_args() # Prepare ChainerMN communicator. if args.gpu: if args.communicator == 'naive': print("Error: 'naive' communicator does not support GPU.\n") exit(-1) comm = chainermn.create_communicator(args.communicator) device = comm.intra_rank else: if args.communicator != 'naive': print('Warning: using naive communicator ' 'because only naive supports CPU-only execution') comm = chainermn.create_communicator('naive') device = -1 if comm.mpi_comm.rank == 0: print('==========================================') print('Num process (COMM_WORLD): {}'.format(MPI.COMM_WORLD.Get_size())) if args.gpu: print('Using GPUs') print('Using {} communicator'.format(args.communicator)) print('Num hidden unit: {}'.format(args.n_hidden)) print('Num Minibatch-size: {}'.format(args.batchsize)) print('Num epoch: {}'.format(args.epoch)) print('==========================================') # Set up a neural network to train gen = Generator(n_hidden=args.n_hidden) dis = Discriminator() if device >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(device).use() gen.to_gpu() # Copy the model to the GPU dis.to_gpu() # Setup an optimizer def make_optimizer(model, comm, alpha=0.0002, beta1=0.5): # Create a multi node optimizer from a standard Chainer optimizer. optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.Adam(alpha=alpha, beta1=beta1), comm) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen, comm) opt_dis = make_optimizer(dis, comm) # Split and distribute the dataset. Only worker 0 loads the whole dataset. # Datasets of worker 0 are evenly split and distributed to all workers. if comm.rank == 0: if args.dataset == '': # Load the CIFAR10 dataset if args.dataset is not specified train, _ = chainer.datasets.get_cifar10(withlabel=False, scale=255.) else: all_files = os.listdir(args.dataset) image_files = [f for f in all_files if ('png' in f or 'jpg' in f)] print('{} contains {} image files'.format(args.dataset, len(image_files))) train = chainer.datasets\ .ImageDataset(paths=image_files, root=args.dataset) else: train = None train = chainermn.scatter_dataset(train, comm) train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=device) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) # Some display and output extensions are necessary only for one worker. # (Otherwise, there would just be repeated outputs.) if comm.rank == 0: snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') # Save only model parameters. # `snapshot` extension will save all the trainer module's attribute, # including `train_iter`. # However, `train_iter` depends on scattered dataset, which means that # `train_iter` may be different in each process. # Here, instead of saving whole trainer module, only the network models # are saved. trainer.extend(extensions.snapshot_object( gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', 'elapsed_time', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 10, 10, args.seed, args.out), trigger=snapshot_interval) # Start the training using pre-trained model, saved by snapshot_object if args.gen_model: chainer.serializers.load_npz(args.gen_model, gen) if args.dis_model: chainer.serializers.load_npz(args.dis_model, dis) # Run the training trainer.run()
def main(): parser = argparse.ArgumentParser(description='Chainer: DZGAN MNIST') parser.add_argument('--batchsize', '-b', type=int, default=50, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=50, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=0, help='GPU ID (negative value indicates CPU)') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', type=str, default='', help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=1000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') parser.add_argument('--disresume', '-d', type=str, default='', help='Resume the training from snapshot') parser.add_argument('--genresume', '-gen', type=str, default='', help='Resume the training from snapshot') parser.add_argument('--n_class', '-c', type=int, default=0, help='class num') parser.add_argument('--n_sigma', '-s', type=float, default=0, help='sigma param') args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') #add command temp if not os.path.exists(args.out): os.makedirs(args.out) path = str(args.out) + '/lastcommand.txt' with open(path, mode='w') as f: f.write(str(args)) f.close() # Set up a neural network to train gen = Generator(n_hidden=args.n_hidden, n_class=args.n_class) dis = Discriminator() if args.gpu >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(args.gpu).use() gen.to_gpu() # Copy the model to the GPU dis.to_gpu() # Setup an optimizer def make_optimizer_gen(model, alpha=2e-4, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) #optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer def make_optimizer_dis(model, alpha=2e-4, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) #optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer_gen(gen) opt_dis = make_optimizer_dis(dis) # Load the MNIST dataset train, _ = chainer.datasets.get_mnist( withlabel=True, ndim=3, scale=255.) # ndim=3 : (ch,width,height) train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu, n_hidden=args.n_hidden, n_sigma=args.n_sigma) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) epoch_interval = (1, 'epoch') visual_interval = (1, 'epoch') snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') # trainer.extend(extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) # trainer.extend(extensions.snapshot_object(gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) # trainer.extend(extensions.snapshot_object(dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend( extensions.snapshot(filename='snapshot_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 10, 20, args.seed, args.out, args.n_sigma), trigger=visual_interval) if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) if args.disresume: chainer.serializers.load_npz(args.disresume, dis) if args.genresume: chainer.serializers.load_npz(args.genresume, gen) # Run the training trainer.run()
shade_lowest=False) plot_scatter_real_data( chainer.dataset.concat_examples(dataset[:]), out, radius=args.radius, ) train_iter = chainer.iterators.SerialIterator(dataset, batch_size) # Set up a trainer if args.discriminator == 2: updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, scale=args.radius, device=gpu, lam=args.lam) else: updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, scale=args.radius, device=gpu) trainer = training.Trainer(updater, (epoch, 'epoch'), out=out)
def main(): parser = argparse.ArgumentParser(description='Chainer: DCGAN MNIST') parser.add_argument('--batchsize', '-b', type=int, default=20, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=100, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=0, help='GPU ID (negative value indicates CPU)') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=128, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=1000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') # Set up a neural network to train gen = Generator(n_hidden=args.n_hidden) dis = Discriminator() if args.gpu >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(args.gpu).use() gen.to_gpu() # Copy the model to the GPU dis.to_gpu() # Setup an optimizer def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) # Load the MNIST dataset train = load_image() train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Set up a trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) epoch_interval = (1, 'epoch') display_interval = (args.display_interval, 'iteration') # trainer.extend(extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) # trainer.extend(extensions.snapshot_object(gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) # trainer.extend(extensions.snapshot_object(dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend( extensions.snapshot(filename='snapshot_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 10, 10, args.seed, args.out), trigger=epoch_interval) if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
def main(): import numpy as np import argparse # パーサーを作る parser = argparse.ArgumentParser( prog='train', # プログラム名 usage='train DCGAN to dish', # プログラムの利用方法 description='description', # 引数のヘルプの前に表示 epilog='end', # 引数のヘルプの後で表示 add_help=True, # -h/–help オプションの追加 ) # 引数の追加 parser.add_argument('-s', '--seed', help='seed', type=int, required=True) parser.add_argument('-n', '--number', help='the number of experiments.', type=int, required=True) parser.add_argument('--hidden', help='the number of codes of Generator.', type=int, default=100) parser.add_argument('-e', '--epoch', help='the number of epoch, defalut value is 300', type=int, default=300) parser.add_argument('-bs', '--batch_size', help='batch size. defalut value is 128', type=int, default=128) parser.add_argument('-g', '--gpu', help='specify gpu by this number. defalut value is 0', choices=[0, 1], type=int, default=0) parser.add_argument( '-ks', '--ksize', help='specify ksize of generator by this number. any of following;' ' 4 or 6. defalut value is 6', choices=[4, 6], type=int, default=6) parser.add_argument( '-dis', '--discriminator', help='specify discriminator by this number. any of following;' ' 0: original, 1: minibatch discriminatio, 2: feature matching, 3: GAP. defalut value is 3', choices=[0, 1, 2, 3], type=int, default=3) parser.add_argument( '-ts', '--tensor_shape', help= 'specify Tensor shape by this numbers. first args denotes to B, seconds to C.' ' defalut value are B:32, C:8', type=int, default=[32, 8], nargs=2) parser.add_argument('-V', '--version', version='%(prog)s 1.0.0', action='version', default=False) # 引数を解析する args = parser.parse_args() gpu = args.gpu batch_size = args.batch_size n_hidden = args.hidden epoch = args.epoch seed = args.seed number = args.number # number of experiments if args.ksize == 6: pad = 2 else: pad = 1 out = pathlib.Path("result_{0}".format(number)) if not out.exists(): out.mkdir() out /= pathlib.Path("result_{0}_{1}".format(number, seed)) if not out.exists(): out.mkdir() # 引数(ハイパーパラメータの設定)の書き出し with open(out / "args.txt", "w") as f: f.write(str(args)) print('GPU: {}'.format(gpu)) print('# Minibatch-size: {}'.format(batch_size)) print('# n_hidden: {}'.format(n_hidden)) print('# epoch: {}'.format(epoch)) print('# out: {}'.format(out)) print('# seed: {}'.format(seed)) print('# ksize: {}'.format(args.ksize)) print('# pad: {}'.format(pad)) # fix seed np.random.seed(seed) if chainer.backends.cuda.available: chainer.backends.cuda.cupy.random.seed(seed) # import discrimination & set up # if args.discriminator == 0: # print("# Original Discriminator") # from discriminator import Discriminator # from updater import DCGANUpdater # dis = Discriminator() if args.discriminator == 1: print("# Discriminator applied Minibatch Discrimination") print('# Tensor shape is A x {0} x {1}'.format(args.tensor_shape[0], args.tensor_shape[1])) from discriminator_md import Discriminator from updater import DCGANUpdater dis = Discriminator(B=args.tensor_shape[0], C=args.tensor_shape[1]) elif args.discriminator == 3: print("# Discriminator applied GAP") from discriminator import Discriminator from updater import DCGANUpdater dis = Discriminator() """ elif args.discriminator == 2: print("# Discriminator applied matching") from discriminator_fm import Discriminator from updater_fm import DCGANUpdater """ print('') # Set up a neural network to train gen = Generator(n_hidden=n_hidden, ksize=args.ksize, pad=pad) if gpu >= 0: # Make a specified GPU current chainer.backends.cuda.get_device_from_id(gpu).use() gen.to_gpu() # Copy the model to the GPU dis.to_gpu() opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) # Prepare Dataset """ paths = ["rsize_data_128", "test_rsize_data_128", "unlabeled_rsize_data_128"] # resize data 128 """ paths = ["center_crop_data_128"] # center ctop data data_path = [] for path in paths: data_dir = pathlib.Path(path) abs_data_dir = data_dir.resolve() print("data dir path:", abs_data_dir) data_path += [path for path in abs_data_dir.glob("*.jpg")] print("data length:", len(data_path)) data = ImageDataset(paths=data_path) # dtype=np.float32 train_iter = chainer.iterators.SerialIterator(data, batch_size) # Set up a updater and trainer updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=gpu) trainer = training.Trainer(updater, (epoch, 'epoch'), out=out) snapshot_interval = (10, 'epoch') display_interval = (1, 'epoch') # storage method is hdf5 trainer.extend(extensions.snapshot( filename='snapshot_epoch_{.updater.epoch}.npz', savefun=save_npz), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object(gen, 'gen_epoch_{.updater.epoch}.npz', savefun=save_npz), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object(dis, 'dis_epoch_{.updater.epoch}.npz', savefun=save_npz), trigger=snapshot_interval) trainer.extend(extensions.LogReport()) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', 'elapsed_time', 'dis/accuracy' ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=20)) trainer.extend(out_generated_image(gen, dis, 5, 5, seed, out), trigger=display_interval) trainer.extend( extensions.PlotReport(['gen/loss', 'dis/loss'], x_key='epoch', file_name='loss_{0}_{1}.jpg'.format( number, seed), grid=False)) trainer.extend(extensions.dump_graph("gen/loss", out_name="gen.dot")) trainer.extend(extensions.dump_graph("dis/loss", out_name="dis.dot")) # Run the training trainer.run()
def main(): parser = argparse.ArgumentParser(description='Chainer: DCGAN MNIST') parser.add_argument('--batchsize', '-b', type=int, default=50, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=1000, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=0, help='GPU ID (negative value indicates CPU)') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=1000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') parser.add_argument('--image_dir', default='./img', help='Training image directory') args = parser.parse_args() print('GPU: {}'.format(args.gpu)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') gen = Generator() dis = Discriminator() if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() gen.to_gpu() dis.to_gpu() def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) def read_dataset(image_dir): fs = os.listdir(image_dir) dataset = [] for fn in fs: img = Image.open(f"{args.image_dir}/{fn}").convert('RGB') dataset.append((np.asarray(img, dtype=np.float32).transpose(2, 0, 1))) return dataset dataset = read_dataset(args.image_dir) train_iter = chainer.iterators.SerialIterator(dataset, args.batchsize, repeat=True, shuffle=True) updater = DCGANUpdater(models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis }, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) epoch_interval = (1, 'epoch') # snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') # trainer.extend(extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) # trainer.extend(extensions.snapshot_object(gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) # trainer.extend(extensions.snapshot_object(dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend( extensions.snapshot(filename='snapshot_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_epoch_{.updater.epoch}.npz'), trigger=epoch_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(out_generated_image(gen, dis, 10, 10, args.seed, args.out), trigger=epoch_interval) if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()