├── utils ├── __init__.py ├── inception_score.py └── utils.py ├── models └── readme ├── nn ├── activations.py ├── op.py └── layers.py ├── data ├── data_loader.py ├── tf_reader.py ├── ingest_cifar10.py ├── ingest_cub200.py ├── ingest_flower102.py ├── ingest_wikiart.py └── ingest_stl10.py ├── README.md ├── Flower128GANAEsample.py ├── CUB128GANAEsample.py ├── Genre128GANAEsample.py ├── Style128GANAEsample.py ├── Artist128GANAEsample.py ├── CIFAR64GANAEsample.py ├── STL128GANAEsample.py ├── Genre128GANAE.py ├── Artist128GANAE.py ├── Style128GANAE.py ├── CIFAR64GANAEtrick.py ├── Flower128GANAE.py ├── CUB128GANAE.py └── STL128GANAE.py /utils/__init__.py: -------------------------------------------------------------------------------- 1 | from utils import * 2 | -------------------------------------------------------------------------------- /models/readme: -------------------------------------------------------------------------------- 1 | 2 | Trained models go here.. 3 | -------------------------------------------------------------------------------- /nn/activations.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | def lrelu(x, alpha=0.1): 5 | return tf.maximum(alpha*x, x) 6 | -------------------------------------------------------------------------------- /nn/op.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | def label_smoothing(labels, smooth): 5 | return labels * smooth + (1 - labels) * (1 - smooth) / (labels.get_shape().as_list()[1] - 1) 6 | 7 | 8 | def log_sum_exp(x, axis=1): 9 | m = tf.reduce_max(x, axis=axis, keep_dims=True) 10 | return m + tf.log(tf.reduce_sum(tf.exp(x - m), axis=axis)) 11 | -------------------------------------------------------------------------------- /data/data_loader.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from neon.data.aeon_shim import AeonDataLoader 3 | from neon.data.dataloader_transformers import OneHot, TypeCast 4 | from neon.util.persist import get_data_cache_or_nothing 5 | 6 | 7 | def common_config(manifest_file, manifest_root, batch_size, subset_pct, h=96, w=96, scale=[1., 1.]): 8 | cache_root = get_data_cache_or_nothing('stl10-cache/') 9 | return { 10 | 'manifest_filename': manifest_file, 11 | 'manifest_root': manifest_root, 12 | 'minibatch_size': batch_size, 13 | 'subset_fraction': float(subset_pct/100.0), 14 | 'macrobatch_size': 5000, 15 | 'type': 'image,label', 16 | 'cache_directory': cache_root, 17 | 'image': {'height': h, 18 | 'width': w, 19 | 'scale': scale}, 20 | 'label': {'binary': False} 21 | } 22 | 23 | 24 | def wrap_dataloader(dl, ncls=10): 25 | dl = OneHot(dl, index=1, nclasses=ncls) 26 | dl = TypeCast(dl, index=0, dtype=np.float32) 27 | return dl 28 | 29 | 30 | def train_loader(manifest_file, manifest_root, backend_obj, subset_pct=100, random_seed=0, h=96, w=96, scale=[1., 1.], binary=False): 31 | aeon_config = common_config(manifest_file, manifest_root, backend_obj.bsz, subset_pct, h, w, scale) 32 | aeon_config['shuffle_manifest'] = True 33 | aeon_config['shuffle_every_epoch'] = True 34 | aeon_config['random_seed'] = random_seed 35 | aeon_config['image']['center'] = False 36 | aeon_config['image']['flip_enable'] = True 37 | aeon_config['label']['binary'] = binary 38 | # return wrap_dataloader(AeonDataLoader(aeon_config, backend_obj)) 39 | return AeonDataLoader(aeon_config, backend_obj) 40 | 41 | 42 | def validation_loader(manifest_file, manifest_root, backend_obj, subset_pct=100, h=96, w=96, scale=[1., 1.], ncls=10): 43 | aeon_config = common_config(manifest_file, manifest_root, backend_obj.bsz, subset_pct, h, w, scale) 44 | aeon_config['image']['center'] = True 45 | return wrap_dataloader(AeonDataLoader(aeon_config, backend_obj), ncls=ncls) 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArtGAN 2 | 3 | Please visit https://github.com/cs-chan/ArtGAN for the final version. 4 | 5 | # Citation 6 | This repository contains codes for the following paper (under review): 7 | 8 | ``` 9 | @article{tan2017learning, 10 | title={Learning a Generative Adversarial Network for High Resolution Artwork Synthesis}, 11 | author={Tan, Wei Ren and Chan, Chee Seng and Aguirre, Hernan and Tanaka, Kiyoshi}, 12 | journal={arXiv preprint arXiv:1708.09533}, 13 | year={2017} 14 | } 15 | ``` 16 | which is an extension to the following paper (ICIP 2017): 17 | ``` 18 | @article{tan2017artgan, 19 | title={ArtGAN: Artwork Synthesis with Conditional Categorial GANs}, 20 | author={Tan, Wei Ren and Chan, Chee Seng and Aguirre, Hernan and Tanaka, Kiyoshi}, 21 | journal={arXiv preprint arXiv:1702.03410}, 22 | year={2017} 23 | } 24 | ``` 25 | 26 | # Prerequisites 27 | - Python 2.7 28 | - [Tensorflow](https://github.com/tensorflow/tensorflow.git) 29 | - (Optional) [Nervana's Systems neon](https://github.com/NervanaSystems/neon.git) 30 | - (Optional) [Nervana's Systems aeon](https://github.com/NervanaSystems/aeon.git) 31 | 32 | \* Neon and aeon are required to load data. If other data loader is used, neon and aeon are not required. But, make sure that data format is 'NCHW'. 33 | 34 | # Trained models 35 | 36 | Each link below is the best trained model for the corresponding dataset. Download and extract to 'models' folder: 37 | 38 | - CIFAR-10 - http://www.cs-chan.com/source/ArtGAN/CIFAR64GANAE.zip 39 | 40 | - STL-10 - http://www.cs-chan.com/source/ArtGAN/STL128GANAE.zip 41 | 42 | - Flowers - http://www.cs-chan.com/source/ArtGAN/Flower128GANAE.zip 43 | 44 | - CUB-200 - http://www.cs-chan.com/source/ArtGAN/CUB128GANAE.zip 45 | 46 | - Wikiart Artist - http://www.cs-chan.com/source/ArtGAN/Artist128GANAE.zip 47 | 48 | - Wikiart Genre - http://www.cs-chan.com/source/ArtGAN/Genre128GANAE.zip 49 | 50 | - Wikiart Style - http://www.cs-chan.com/source/ArtGAN/Style128GANAE.zip 51 | 52 | # Feedback 53 | Suggestions and opinions of this work (both positive and negative) are greatly welcome. Please contact the authors by sending email to Wei Ren Tan at `wrtan.edu at gmail.com` or Chee Seng Chan at `cs.chan at um.edu.my` 54 | 55 | # License 56 | BSD-3, see LICENSE file for details. 57 | -------------------------------------------------------------------------------- /data/tf_reader.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import os 3 | 4 | 5 | records_default = [[''], [0]] 6 | IMAGENET_MEAN = [103.939, 116.779, 123.68] 7 | 8 | 9 | def read_csv(filename, directory, num_epoch=None, records=None): 10 | if records is None: 11 | records = records_default 12 | # Read examples and labels from file 13 | filename_queue = tf.train.string_input_producer([os.path.join(directory, filename)], 14 | num_epochs=num_epoch, shuffle=True) 15 | reader = tf.TextLineReader() 16 | _, value = reader.read(filename_queue) 17 | decoded = tf.decode_csv(value, record_defaults=records) 18 | im_path = tf.stack(decoded[0]) 19 | if len(decoded) > 1: 20 | label = tf.stack(decoded[1:]) 21 | else: 22 | label = [0] 23 | return im_path, label 24 | 25 | 26 | def decode_jpg(filename, directory, center=False, crop=None, flip=False, resize=None, ratio=False, filename_offset='', 27 | normalize='imagenet'): 28 | # Read image 29 | im_content = tf.read_file(directory + filename_offset + filename) 30 | example = tf.image.decode_jpeg(im_content, channels=3) 31 | # preprocessing 32 | example = tf.cast(example[:, :, ::-1], tf.float32) 33 | if normalize is 'imagenet': 34 | example = example - IMAGENET_MEAN 35 | elif normalize: 36 | example = example / 127.5 - 1. 37 | # cropping 38 | if crop: 39 | shape = tf.shape(example) 40 | if ratio: 41 | assert isinstance(crop, list) 42 | crop_h = crop[0] 43 | crop_w = crop[1] 44 | else: 45 | assert isinstance(crop, int) 46 | shortest = tf.cond(tf.less(shape[0], shape[1]), lambda: shape[0], lambda: shape[1]) 47 | crop_h = tf.cond(tf.less_equal(shortest, tf.constant(crop)), lambda: shortest, lambda: tf.constant(crop)) 48 | crop_w = crop_h 49 | if center: 50 | example = tf.image.resize_image_with_crop_or_pad(example, crop_h, crop_w) 51 | else: 52 | example = tf.random_crop(example, [crop_h, crop_w, 3]) 53 | # resize 54 | if resize: 55 | assert isinstance(resize, (int, float)) 56 | new_size = tf.stack([resize, resize]) 57 | example = tf.image.resize_images(example, new_size) 58 | # random horizontal flip 59 | if flip: 60 | example = tf.image.random_flip_left_right(example) 61 | return tf.transpose(example, [2, 0, 1]) 62 | 63 | 64 | def input_pipeline(filenames, directory, batchsize, num_epoch=None, records=None, center=False, crop=None, 65 | flip=False, resize=None, ratio=False, filename_offset='', normalize='imagenet'): 66 | if records is None: 67 | records = records_default 68 | imname, label = read_csv(filenames, directory, num_epoch, records) 69 | example = decode_jpg(imname, directory, center, crop, flip, resize, ratio, filename_offset, normalize) 70 | min_after_dequeue = batchsize * 3 71 | capacity = 10 * batchsize 72 | example_batch, label_batch = tf.train.shuffle_batch( 73 | [example, label], batch_size=batchsize, capacity=capacity, min_after_dequeue=min_after_dequeue, num_threads=4, 74 | shapes=((3, resize, resize), (1,)), allow_smaller_final_batch=True 75 | ) 76 | return example_batch, label_batch 77 | -------------------------------------------------------------------------------- /data/ingest_cifar10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | from tqdm import tqdm 4 | from neon.data import CIFAR10 5 | from neon.util.persist import ensure_dirs_exist 6 | from PIL import Image 7 | 8 | 9 | def ingest_cifar10(out_dir, padded_size, overwrite=False): 10 | """ 11 | Save CIFAR-10 dataset as PNG files 12 | """ 13 | dataset = dict() 14 | cifar10 = CIFAR10(path=out_dir, normalize=False) 15 | dataset['train'], dataset['val'], _ = cifar10.load_data() 16 | pad_size = (padded_size - 32) // 2 if padded_size > 32 else 0 17 | pad_width = ((0, 0), (pad_size, pad_size), (pad_size, pad_size)) 18 | 19 | set_names = ('train', 'val') 20 | manifest_files = [os.path.join(out_dir, setn + '-index.csv') for setn in set_names] 21 | 22 | cfg_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'train.cfg') 23 | log_file = os.path.join(out_dir, 'train.log') 24 | manifest_list_cfg = ', '.join([k+':'+v for k, v in zip(set_names, manifest_files)]) 25 | 26 | with open(cfg_file, 'w') as f: 27 | f.write('manifest = [{}]\n'.format(manifest_list_cfg)) 28 | f.write('manifest_root = {}\n'.format(out_dir)) 29 | f.write('log = {}\n'.format(log_file)) 30 | f.write('epochs = 165\nrng_seed = 0\nverbose = True\neval_freq = 1\n') 31 | f.write('backend = gpu\nbatch_size = 64\n') 32 | 33 | if all([os.path.exists(manifest) for manifest in manifest_files]) and not overwrite: 34 | return manifest_files 35 | 36 | # Write out label files and setup directory structure 37 | lbl_paths, img_paths = dict(), dict(train=dict(), val=dict()) 38 | for lbl in range(10): 39 | lbl_paths[lbl] = ensure_dirs_exist(os.path.join(out_dir, 'labels', str(lbl) + '.txt')) 40 | np.savetxt(lbl_paths[lbl], [lbl], fmt='%d') 41 | for setn in ('train', 'val'): 42 | img_paths[setn][lbl] = ensure_dirs_exist(os.path.join(out_dir, setn, str(lbl) + '/')) 43 | 44 | # Now write out image files and manifests 45 | for setn, manifest in zip(set_names, manifest_files): 46 | records = [] 47 | for idx, (img, lbl) in enumerate(tqdm(zip(*dataset[setn]))): 48 | img_path = os.path.join(img_paths[setn][lbl[0]], str(idx) + '.png') 49 | im = np.pad(img.reshape((3, 32, 32)), pad_width, mode='mean') 50 | im = Image.fromarray(np.uint8(np.transpose(im, axes=[1, 2, 0]).copy())) 51 | # im.save(os.path.join(out_dir, img_path), format='PNG') 52 | im.save(img_path, format='PNG') 53 | records.append((os.path.relpath(img_path, out_dir), 54 | os.path.relpath(lbl_paths[lbl[0]], out_dir))) 55 | np.savetxt(manifest, records, fmt='%s,%s') 56 | 57 | return manifest_files 58 | 59 | 60 | if __name__ == '__main__': 61 | from configargparse import ArgumentParser 62 | parser = ArgumentParser() 63 | parser.add_argument('--out_dir', required=True, help='path to extract files into') 64 | parser.add_argument('--input_dir', default=None, help='unused argument') 65 | parser.add_argument('--padded_size', type=int, default=32, 66 | help='Size of image after padding (each side)') 67 | args = parser.parse_args() 68 | 69 | generated_files = ingest_cifar10(args.out_dir, args.padded_size) 70 | 71 | print("Manifest files written to:\n" + "\n".join(generated_files)) 72 | -------------------------------------------------------------------------------- /Flower128GANAEsample.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, nnupsampling, batchnorm, pool 3 | from activations import lrelu 4 | import numpy as np 5 | from utils import drawblock, createfolders 6 | from scipy.misc import imsave 7 | import os 8 | 9 | 10 | # Create folders to store images 11 | gen_dir, gen_dir128 = createfolders("./genimgs/Flower128GANAEsample", "/gen", "/gen128") 12 | 13 | # Parameters 14 | batch_size = 102 15 | zdim = 100 16 | n_classes = 102 17 | im_size = [64, 64] 18 | gname = 'g_' 19 | tf.set_random_seed(5555) # use different seed to generate different set of images 20 | 21 | # Graph input 22 | z = tf.random_uniform([batch_size, zdim], -1, 1) 23 | iny = tf.constant(np.eye(n_classes, dtype=np.float32)[:batch_size, :]) 24 | 25 | 26 | # Generator 27 | def generator(inp_z, inp_y, reuse=False): 28 | with tf.variable_scope('Generator', reuse=reuse): 29 | inp = tf.concat([inp_z, inp_y], 1) 30 | sz = 4 31 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 32 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 33 | g1 = lrelu(g1, 0.2) 34 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 35 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 36 | 37 | g2 = nnupsampling(g1_reshaped, [8, 8]) 38 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 39 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 40 | g2 = lrelu(g2, 0.2) 41 | 42 | g3 = nnupsampling(g2, [16, 16]) 43 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 44 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 45 | g3 = lrelu(g3, 0.2) 46 | 47 | g4 = nnupsampling(g3, [32, 32]) 48 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 49 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 50 | g4 = lrelu(g4, 0.2) 51 | 52 | g5 = nnupsampling(g4, [64, 64]) 53 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 54 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 55 | g5 = lrelu(g5, 0.2) 56 | 57 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 58 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 59 | g5b = lrelu(g5b, 0.2) 60 | 61 | g6 = nnupsampling(g5b, [128, 128]) 62 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 63 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 64 | g6 = lrelu(g6, 0.2) 65 | 66 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 67 | g6b = tf.nn.tanh(g6b) 68 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 69 | return g6b_64, g6b 70 | 71 | # Call functions 72 | samples, samples128 = generator(z, iny) 73 | 74 | # Initialize the variables 75 | init = tf.global_variables_initializer() 76 | # Config for session 77 | config = tf.ConfigProto() 78 | config.gpu_options.allow_growth = True 79 | # Generate 80 | with tf.Session(config=config) as sess: 81 | sess.run(init) 82 | saver = tf.train.Saver(max_to_keep=None) 83 | saver.restore(sess=sess, save_path='./models/Flower128GANAE/cdgan29999.ckpt') 84 | 85 | gen_img, gen_img128 = sess.run([samples, samples128]) 86 | gen_img = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 87 | gen_img = np.uint8(gen_img[:, :, :, ::-1]) 88 | gen_img128 = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 89 | gen_img128 = np.uint8(gen_img128[:, :, :, ::-1]) 90 | 91 | gg = drawblock(gen_img, 10) 92 | imsave(os.path.join(gen_dir, 'sample.jpg'), gg) 93 | ggL = drawblock(gen_img128, 10) 94 | imsave(os.path.join(gen_dir128, 'sample.jpg'), ggL) 95 | -------------------------------------------------------------------------------- /CUB128GANAEsample.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, nnupsampling, batchnorm, pool 3 | from activations import lrelu 4 | import numpy as np 5 | from utils import drawblock, createfolders 6 | from scipy.misc import imsave 7 | import os 8 | 9 | 10 | # Create folders to store images 11 | gen_dir, gen_dir128 = createfolders("./genimgs/CUB128GANAEsample", "/gen", "/gen128") 12 | 13 | # Parameters 14 | batch_size = 100 15 | zdim = 100 16 | n_classes = 200 17 | im_size = [64, 64] 18 | gname = 'g_' 19 | tf.set_random_seed(5555) # use different seed to generate different set of images 20 | 21 | # Graph input 22 | z = tf.random_uniform([batch_size, zdim], -1, 1) 23 | # iny = tf.constant(np.eye(n_classes, dtype=np.float32)[:batch_size, :]) # uncomment to generate first 100 classes 24 | iny = tf.constant(np.eye(n_classes, dtype=np.float32)[batch_size:, :]) # uncomment to generate second 100 classes 25 | 26 | 27 | # Generator 28 | def generator(inp_z, inp_y, reuse=False): 29 | with tf.variable_scope('Generator', reuse=reuse): 30 | inp = tf.concat([inp_z, inp_y], 1) 31 | sz = 4 32 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 33 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 34 | g1 = lrelu(g1, 0.2) 35 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 36 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 37 | 38 | g2 = nnupsampling(g1_reshaped, [8, 8]) 39 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 40 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 41 | g2 = lrelu(g2, 0.2) 42 | 43 | g3 = nnupsampling(g2, [16, 16]) 44 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 45 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 46 | g3 = lrelu(g3, 0.2) 47 | 48 | g4 = nnupsampling(g3, [32, 32]) 49 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 50 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 51 | g4 = lrelu(g4, 0.2) 52 | 53 | g5 = nnupsampling(g4, [64, 64]) 54 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 55 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 56 | g5 = lrelu(g5, 0.2) 57 | 58 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 59 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 60 | g5b = lrelu(g5b, 0.2) 61 | 62 | g6 = nnupsampling(g5b, [128, 128]) 63 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 64 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 65 | g6 = lrelu(g6, 0.2) 66 | 67 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 68 | g6b = tf.nn.tanh(g6b) 69 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 70 | return g6b_64, g6b 71 | 72 | # Call functions 73 | samples, samples128 = generator(z, iny) 74 | 75 | # Initialize the variables 76 | init = tf.global_variables_initializer() 77 | # Config for session 78 | config = tf.ConfigProto() 79 | config.gpu_options.allow_growth = True 80 | # Generate 81 | with tf.Session(config=config) as sess: 82 | sess.run(init) 83 | saver = tf.train.Saver(max_to_keep=None) 84 | saver.restore(sess=sess, save_path='./models/CUB128GANAE/cdgan29999.ckpt') 85 | 86 | gen_img, gen_img128 = sess.run([samples, samples128]) 87 | gen_img = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 88 | gen_img = np.uint8(gen_img[:, :, :, ::-1]) 89 | gen_img128 = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 90 | gen_img128 = np.uint8(gen_img128[:, :, :, ::-1]) 91 | 92 | gg = drawblock(gen_img, 10) 93 | imsave(os.path.join(gen_dir, 'sample.jpg'), gg) 94 | ggL = drawblock(gen_img128, 10) 95 | imsave(os.path.join(gen_dir128, 'sample.jpg'), ggL) 96 | -------------------------------------------------------------------------------- /Genre128GANAEsample.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, nnupsampling, batchnorm, pool 3 | from activations import lrelu 4 | import numpy as np 5 | from utils import drawblock, createfolders 6 | from scipy.misc import imsave 7 | import os 8 | 9 | 10 | # Create folders to store images 11 | gen_dir, gen_dir128 = createfolders("./genimgs/Genre128GANAEsample", "/gen", "/gen128") 12 | 13 | # Parameters 14 | batch_size = 100 15 | zdim = 100 16 | n_classes = 10 17 | im_size = [64, 64] 18 | gname = 'g_' 19 | tf.set_random_seed(6666) # use different seed to generate different set of images 20 | 21 | # Graph input 22 | z = tf.random_uniform([batch_size, zdim], -1, 1) 23 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 24 | 25 | 26 | # Generator 27 | def generator(inp_z, inp_y, reuse=False): 28 | with tf.variable_scope('Generator', reuse=reuse): 29 | inp = tf.concat([inp_z, inp_y], 1) 30 | sz = 4 31 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 32 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 33 | g1 = lrelu(g1, 0.2) 34 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 35 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 36 | 37 | g2 = nnupsampling(g1_reshaped, [8, 8]) 38 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 39 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 40 | g2 = lrelu(g2, 0.2) 41 | 42 | g3 = nnupsampling(g2, [16, 16]) 43 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 44 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 45 | g3 = lrelu(g3, 0.2) 46 | 47 | g4 = nnupsampling(g3, [32, 32]) 48 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 49 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 50 | g4 = lrelu(g4, 0.2) 51 | 52 | g5 = nnupsampling(g4, [64, 64]) 53 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 54 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 55 | g5 = lrelu(g5, 0.2) 56 | 57 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 58 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 59 | g5b = lrelu(g5b, 0.2) 60 | 61 | g6 = nnupsampling(g5b, [128, 128]) 62 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 63 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 64 | g6 = lrelu(g6, 0.2) 65 | 66 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 67 | g6b = tf.nn.tanh(g6b) 68 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 69 | return g6b_64, g6b 70 | 71 | # Call functions 72 | samples, samples128 = generator(z, iny) 73 | 74 | # Initialize the variables 75 | init = tf.global_variables_initializer() 76 | # Config for session 77 | config = tf.ConfigProto() 78 | config.gpu_options.allow_growth = True 79 | # Generate 80 | with tf.Session(config=config) as sess: 81 | sess.run(init) 82 | saver = tf.train.Saver(max_to_keep=None) 83 | saver.restore(sess=sess, save_path='./models/Genre128GANAE/cdgan49999.ckpt') 84 | 85 | # generate 86 | gen_img, gen_img128 = sess.run([samples, samples128]) 87 | 88 | # Store Generated 89 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 90 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 91 | genmix_imgs = drawblock(genmix_imgs, n_classes) 92 | imsave(os.path.join(gen_dir, 'sample.jpg'), genmix_imgs) 93 | # Store Generated 128 94 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 95 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 96 | genmix_imgs = drawblock(genmix_imgs, n_classes) 97 | imsave(os.path.join(gen_dir128, 'sample1.jpg'), genmix_imgs) 98 | -------------------------------------------------------------------------------- /Style128GANAEsample.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, nnupsampling, batchnorm, pool 3 | from activations import lrelu 4 | import numpy as np 5 | from utils import drawblock, createfolders 6 | from scipy.misc import imsave 7 | import os 8 | 9 | 10 | # Create folders to store images 11 | gen_dir, gen_dir128 = createfolders("./genimgs/Style128GANAEsample", "/gen", "/gen128") 12 | 13 | # Parameters 14 | batch_size = 100 15 | zdim = 100 16 | n_classes = 27 17 | im_size = [64, 64] 18 | gname = 'g_' 19 | tf.set_random_seed(777) # use different seed to generate different set of images 20 | 21 | # Graph input 22 | z = tf.random_uniform([batch_size, zdim], -1, 1) 23 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 24 | 25 | 26 | # Generator 27 | def generator(inp_z, inp_y, reuse=False): 28 | with tf.variable_scope('Generator', reuse=reuse): 29 | inp = tf.concat([inp_z, inp_y], 1) 30 | sz = 4 31 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 32 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 33 | g1 = lrelu(g1, 0.2) 34 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 35 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 36 | 37 | g2 = nnupsampling(g1_reshaped, [8, 8]) 38 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 39 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 40 | g2 = lrelu(g2, 0.2) 41 | 42 | g3 = nnupsampling(g2, [16, 16]) 43 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 44 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 45 | g3 = lrelu(g3, 0.2) 46 | 47 | g4 = nnupsampling(g3, [32, 32]) 48 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 49 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 50 | g4 = lrelu(g4, 0.2) 51 | 52 | g5 = nnupsampling(g4, [64, 64]) 53 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 54 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 55 | g5 = lrelu(g5, 0.2) 56 | 57 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 58 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 59 | g5b = lrelu(g5b, 0.2) 60 | 61 | g6 = nnupsampling(g5b, [128, 128]) 62 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 63 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 64 | g6 = lrelu(g6, 0.2) 65 | 66 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 67 | g6b = tf.nn.tanh(g6b) 68 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 69 | return g6b_64, g6b 70 | 71 | # Call functions 72 | samples, samples128 = generator(z, iny) 73 | 74 | # Initialize the variables 75 | init = tf.global_variables_initializer() 76 | # Config for session 77 | config = tf.ConfigProto() 78 | config.gpu_options.allow_growth = True 79 | # Generate 80 | with tf.Session(config=config) as sess: 81 | sess.run(init) 82 | saver = tf.train.Saver(max_to_keep=None) 83 | saver.restore(sess=sess, save_path='./models/Style128GANAE/cdgan49999.ckpt') 84 | 85 | # run generator 86 | gen_img, gen_img128 = sess.run([samples, samples128]) 87 | 88 | # Store Generated 89 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 90 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 91 | genmix_imgs = drawblock(genmix_imgs, n_classes, fixed=3, flip=False) 92 | imsave(os.path.join(gen_dir, 'sample.jpg'), genmix_imgs) 93 | # Store Generated 128 94 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 95 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 96 | genmix_imgs = drawblock(genmix_imgs, n_classes, fixed=3, flip=False) 97 | imsave(os.path.join(gen_dir128, 'sample.jpg'), genmix_imgs) 98 | -------------------------------------------------------------------------------- /Artist128GANAEsample.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, nnupsampling, batchnorm, pool 3 | from activations import lrelu 4 | import numpy as np 5 | from utils import drawblock, createfolders 6 | from scipy.misc import imsave 7 | import os 8 | 9 | 10 | # Create folders to store images 11 | gen_dir, gen_dir128 = createfolders("./genimgs/Artist128GANAEsample", "/gen", "/gen128") 12 | 13 | # Parameters 14 | batch_size = 100 15 | zdim = 100 16 | n_classes = 23 17 | im_size = [64, 64] 18 | gname = 'g_' 19 | tf.set_random_seed(0) # use different seed to generate different set of images 20 | 21 | # Graph input 22 | z = tf.random_uniform([batch_size, zdim], -1, 1) 23 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 24 | 25 | 26 | # Generator 27 | def generator(inp_z, inp_y, reuse=False): 28 | with tf.variable_scope('Generator', reuse=reuse): 29 | inp = tf.concat([inp_z, inp_y], 1) 30 | sz = 4 31 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 32 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 33 | g1 = lrelu(g1, 0.2) 34 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 35 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 36 | 37 | g2 = nnupsampling(g1_reshaped, [8, 8]) 38 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 39 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 40 | g2 = lrelu(g2, 0.2) 41 | 42 | g3 = nnupsampling(g2, [16, 16]) 43 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 44 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 45 | g3 = lrelu(g3, 0.2) 46 | 47 | g4 = nnupsampling(g3, [32, 32]) 48 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 49 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 50 | g4 = lrelu(g4, 0.2) 51 | 52 | g5 = nnupsampling(g4, [64, 64]) 53 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 54 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 55 | g5 = lrelu(g5, 0.2) 56 | 57 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 58 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 59 | g5b = lrelu(g5b, 0.2) 60 | 61 | g6 = nnupsampling(g5b, [128, 128]) 62 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 63 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 64 | g6 = lrelu(g6, 0.2) 65 | 66 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 67 | g6b = tf.nn.tanh(g6b) 68 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 69 | return g6b_64, g6b 70 | 71 | # Call functions 72 | samples, samples128 = generator(z, iny) 73 | 74 | # Initialize the variables 75 | init = tf.global_variables_initializer() 76 | # Config for session 77 | config = tf.ConfigProto() 78 | config.gpu_options.allow_growth = True 79 | # Generate 80 | with tf.Session(config=config) as sess: 81 | sess.run(init) 82 | saver = tf.train.Saver(max_to_keep=None) 83 | saver.restore(sess=sess, save_path='./models/Artist128GANAE/cdgan49999.ckpt') 84 | 85 | # run generator 86 | gen_img, gen_img128 = sess.run([samples, samples128]) 87 | 88 | # Store Generated 89 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 90 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 91 | genmix_imgs = drawblock(genmix_imgs, n_classes, fixed=4, flip=False) 92 | imsave(os.path.join(gen_dir, 'sample1.jpg'), genmix_imgs) 93 | # Store Generated 128 94 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 95 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 96 | genmix_imgs = drawblock(genmix_imgs, n_classes, fixed=4, flip=False) 97 | imsave(os.path.join(gen_dir128, 'sample1.jpg'), genmix_imgs) 98 | -------------------------------------------------------------------------------- /CIFAR64GANAEsample.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, nnupsampling, batchnorm, pool 3 | from activations import lrelu 4 | import numpy as np 5 | from utils import drawblock, createfolders 6 | from scipy.misc import imsave 7 | import os 8 | 9 | 10 | # Create folders to store images 11 | gen_dir, gen_dir128 = createfolders("./genimgs/CIFAR64GANAEsample", "/gen", "/gen64") 12 | 13 | # Parameters 14 | batch_size = 100 15 | zdim = 100 16 | n_classes = 10 17 | gname = 'g_' 18 | tf.set_random_seed(5555) # use different seed to generate different set of images 19 | 20 | # Graph input 21 | z = tf.random_uniform([batch_size, zdim], -1, 1) 22 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 23 | 24 | 25 | # Generator 26 | def generator(inp_z, inp_y, reuse=False): 27 | with tf.variable_scope('Generator', reuse=reuse): 28 | inp = tf.concat([inp_z, inp_y], 1) 29 | 30 | g1 = linear(inp, 512 * 4 * 4, name=gname + 'deconv1') 31 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 32 | g1 = lrelu(g1, 0.2) 33 | g1_reshaped = tf.reshape(g1, [-1, 512, 4, 4]) 34 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 35 | 36 | g2 = nnupsampling(g1_reshaped, [8, 8]) 37 | g2 = conv2d(g2, nout=256, kernel=3, name=gname + 'deconv2') 38 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 39 | g2 = lrelu(g2, 0.2) 40 | 41 | g3 = nnupsampling(g2, [16, 16]) 42 | g3 = conv2d(g3, nout=128, kernel=3, name=gname + 'deconv3') 43 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 44 | g3 = lrelu(g3, 0.2) 45 | 46 | g3b = conv2d(g3, nout=128, kernel=3, name=gname + 'deconv3b') 47 | g3b = batchnorm(g3b, is_training=tf.constant(True), name=gname + 'bn3bg') 48 | g3b = lrelu(g3b, 0.2) 49 | 50 | g4 = nnupsampling(g3b, [32, 32]) 51 | g4 = conv2d(g4, nout=64, kernel=3, name=gname + 'deconv4') 52 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 53 | g4 = lrelu(g4, 0.2) 54 | 55 | g4b = conv2d(g4, nout=64, kernel=3, name=gname + 'deconv4b') 56 | g4b = batchnorm(g4b, is_training=tf.constant(True), name=gname + 'bn4bg') 57 | g4b = lrelu(g4b, 0.2) 58 | 59 | g5 = nnupsampling(g4b, [64, 64]) 60 | g5 = conv2d(g5, nout=32, kernel=3, name=gname + 'deconv5') 61 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 62 | g5 = lrelu(g5, 0.2) 63 | 64 | g5b = conv2d(g5, nout=3, kernel=3, name=gname + 'deconv5b') 65 | g5b = tf.nn.tanh(g5b) 66 | g5b_32 = pool(g5b, fsize=3, strides=2, op='avg', pad='SAME') 67 | return g5b_32, g5b 68 | 69 | # Call functions 70 | samples, samples128 = generator(z, iny) 71 | 72 | # Initialize the variables 73 | init = tf.global_variables_initializer() 74 | # Config for session 75 | config = tf.ConfigProto() 76 | config.gpu_options.allow_growth = True 77 | # Generate 78 | with tf.Session(config=config) as sess: 79 | sess.run(init) 80 | saver = tf.train.Saver(max_to_keep=None) 81 | saver.restore(sess=sess, save_path='./models/CIFAR64GANAE/cdgan50000.ckpt') 82 | coord = tf.train.Coordinator() 83 | threads = tf.train.start_queue_runners(coord=coord) 84 | 85 | # run generator 86 | gen_img, gen_img128 = sess.run([samples, samples128]) 87 | 88 | # Store Generated 89 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 90 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 91 | genmix_imgs = drawblock(genmix_imgs, n_classes) 92 | imsave(os.path.join(gen_dir, 'sample1.jpg'), genmix_imgs) 93 | # Store Generated 64 94 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 95 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 96 | genmix_imgs = drawblock(genmix_imgs, n_classes) 97 | imsave(os.path.join(gen_dir128, 'sample1.jpg'), genmix_imgs) 98 | 99 | coord.request_stop() 100 | coord.join(threads) 101 | -------------------------------------------------------------------------------- /STL128GANAEsample.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, nnupsampling, batchnorm, pool 3 | from activations import lrelu 4 | import numpy as np 5 | from utils import drawblock, createfolders 6 | from scipy.misc import imsave 7 | import os 8 | 9 | 10 | # Create folders to store images 11 | gen_dir, gen_dir128 = createfolders("./genimgs/STLGANAEsample", "/gen", "/gen128") 12 | 13 | # Parameters 14 | batch_size = 100 15 | zdim = 100 16 | n_classes = 10 17 | gname = 'g_' 18 | tf.set_random_seed(5555) # use different seed to generate different set of images 19 | 20 | # Graph input 21 | z = tf.random_uniform([batch_size, zdim], -1, 1) 22 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 23 | 24 | 25 | # Generator 26 | def generator(inp_z, inp_y, reuse=False): 27 | with tf.variable_scope('Generator', reuse=reuse): 28 | inp = tf.concat([inp_z, inp_y], 1) 29 | sz = 4 30 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 31 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 32 | g1 = lrelu(g1, 0.2) 33 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 34 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 35 | 36 | g2 = nnupsampling(g1_reshaped, [8, 8]) 37 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 38 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 39 | g2 = lrelu(g2, 0.2) 40 | 41 | g3 = nnupsampling(g2, [16, 16]) 42 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 43 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 44 | g3 = lrelu(g3, 0.2) 45 | 46 | g4 = nnupsampling(g3, [32, 32]) 47 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 48 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 49 | g4 = lrelu(g4, 0.2) 50 | 51 | g4b = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4b') 52 | g4b = batchnorm(g4b, is_training=tf.constant(True), name=gname + 'bn4bg') 53 | g4b = lrelu(g4b, 0.2) 54 | 55 | g5 = nnupsampling(g4b, [64, 64]) 56 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 57 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 58 | g5 = lrelu(g5, 0.2) 59 | 60 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 61 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 62 | g5b = lrelu(g5b, 0.2) 63 | 64 | g6 = nnupsampling(g5b, [128, 128]) 65 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 66 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 67 | g6 = lrelu(g6, 0.2) 68 | 69 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 70 | g6b = tf.nn.tanh(g6b) 71 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 72 | return g6b_64, g6b 73 | 74 | # Call functions 75 | samples, samples128 = generator(z, iny) 76 | 77 | # Initialize the variables 78 | init = tf.global_variables_initializer() 79 | # Config for session 80 | config = tf.ConfigProto() 81 | config.gpu_options.allow_growth = True 82 | # Generate 83 | with tf.Session(config=config) as sess: 84 | sess.run(init) 85 | saver = tf.train.Saver(max_to_keep=None) 86 | saver.restore(sess=sess, save_path='./models/STL128GANAE/cdgan49999.ckpt') 87 | 88 | # generate 89 | gen_img, gen_img128 = sess.run([samples, samples128]) 90 | 91 | # Store Generated 92 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 93 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 94 | genmix_imgs = drawblock(genmix_imgs, n_classes) 95 | imsave(os.path.join(gen_dir, 'sample1.jpg'), genmix_imgs) 96 | # Store Generated 128 97 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 98 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 99 | genmix_imgs = drawblock(genmix_imgs, n_classes) 100 | imsave(os.path.join(gen_dir128, 'sample1.jpg'), genmix_imgs) 101 | -------------------------------------------------------------------------------- /utils/inception_score.py: -------------------------------------------------------------------------------- 1 | # Code derived from tensorflow/tensorflow/models/image/imagenet/classify_image.py 2 | from __future__ import absolute_import 3 | from __future__ import division 4 | from __future__ import print_function 5 | 6 | import os.path 7 | import tarfile 8 | 9 | import numpy as np 10 | # from six.moves import urllib 11 | import urllib 12 | import tensorflow as tf 13 | import math 14 | import sys 15 | 16 | MODEL_DIR = '/tmp/imagenet' 17 | DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz' 18 | softmax = None 19 | 20 | 21 | # Call this function with list of images. Each of elements should be a 22 | # numpy array with values ranging from 0 to 255. 23 | def get_inception_score(images, splits=10, get_split=False): 24 | assert (type(images) == list) 25 | assert (type(images[0]) == np.ndarray) 26 | assert (len(images[0].shape) == 3) 27 | assert (np.max(images[0]) > 10) 28 | assert (np.min(images[0]) >= 0.0) 29 | inps = [] 30 | for img in images: 31 | img = img.astype(np.float32) 32 | inps.append(np.expand_dims(img, 0)) 33 | bs = 100 34 | with tf.Session() as sess: 35 | preds = [] 36 | n_batches = int(math.ceil(float(len(inps)) / float(bs))) 37 | for i in range(n_batches): 38 | sys.stdout.write(".") 39 | sys.stdout.flush() 40 | inp = inps[(i * bs):min((i + 1) * bs, len(inps))] 41 | inp = np.concatenate(inp, 0) 42 | pred = sess.run(softmax, {'ExpandDims:0': inp}) 43 | preds.append(pred) 44 | preds = np.concatenate(preds, 0) 45 | scores = [] 46 | objectness = [] 47 | diversity = [] 48 | for i in range(splits): 49 | part = preds[(i * preds.shape[0] // splits):((i + 1) * preds.shape[0] // splits), :] 50 | kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0))) 51 | o = -part * np.log(part) 52 | d = -part * np.log(np.expand_dims(np.mean(part, 0), 0)) 53 | kl = np.mean(np.sum(kl, 1)) 54 | objectness.append(np.exp(np.mean(np.sum(o, 1)))) 55 | diversity.append(np.exp(np.mean(np.sum(d, 1)))) 56 | scores.append(np.exp(kl)) 57 | if get_split: 58 | return np.mean(scores), np.std(scores), np.mean(objectness), np.mean(diversity) 59 | return np.mean(scores), np.std(scores) 60 | 61 | 62 | # This function is called automatically. 63 | def _init_inception(): 64 | global softmax 65 | if not os.path.exists(MODEL_DIR): 66 | os.makedirs(MODEL_DIR) 67 | filename = DATA_URL.split('/')[-1] 68 | filepath = os.path.join(MODEL_DIR, filename) 69 | if not os.path.exists(filepath): 70 | def _progress(count, block_size, total_size): 71 | sys.stdout.write('\r>> Downloading %s %.1f%%' % ( 72 | filename, float(count * block_size) / float(total_size) * 100.0)) 73 | sys.stdout.flush() 74 | 75 | filepath, _ = urllib.urlretrieve(DATA_URL, filepath, _progress) 76 | print() 77 | statinfo = os.stat(filepath) 78 | print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.') 79 | tarfile.open(filepath, 'r:gz').extractall(MODEL_DIR) 80 | with tf.gfile.FastGFile(os.path.join( 81 | MODEL_DIR, 'classify_image_graph_def.pb'), 'rb') as f: 82 | graph_def = tf.GraphDef() 83 | graph_def.ParseFromString(f.read()) 84 | _ = tf.import_graph_def(graph_def, name='') 85 | # Works with an arbitrary minibatch size. 86 | with tf.Session() as sess: 87 | pool3 = sess.graph.get_tensor_by_name('pool_3:0') 88 | ops = pool3.graph.get_operations() 89 | for op_idx, op in enumerate(ops): 90 | for o in op.outputs: 91 | shape = o.get_shape() 92 | shape = [s.value for s in shape] 93 | new_shape = [] 94 | for j, s in enumerate(shape): 95 | if s == 1 and j == 0: 96 | new_shape.append(None) 97 | else: 98 | new_shape.append(s) 99 | o._shape = tf.TensorShape(new_shape) 100 | w = sess.graph.get_operation_by_name("softmax/logits/MatMul").inputs[1] 101 | logits = tf.matmul(tf.squeeze(pool3), w) 102 | softmax = tf.nn.softmax(logits) 103 | 104 | 105 | if softmax is None: 106 | _init_inception() 107 | -------------------------------------------------------------------------------- /utils/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unicodedata 3 | import os 4 | 5 | 6 | class OneHot(object): 7 | def __init__(self, be, nclasses): 8 | self.be = be 9 | self.output = be.iobuf(nclasses, parallelism='Data') 10 | 11 | def transform(self, t): 12 | self.output[:] = self.be.onehot(t, axis=0) 13 | return self.output 14 | 15 | 16 | def image_reshape(img, shape, dtype=np.float32, data_format='NCHW', input_format='sigmoid'): 17 | if data_format is 'NCHW': 18 | df = (3, 0, 1, 2) 19 | elif data_format is 'NHWC': 20 | df = (3, 1, 2, 0) 21 | else: 22 | raise ValueError('data_format only support NCHW or NHWC!') 23 | if input_format is 'tanh': 24 | img = img.astype(dtype).reshape((3, shape[0], shape[1], -1)) / 127.5 - 1. 25 | elif input_format is 'sigmoid': 26 | img = img.astype(dtype).reshape((3, shape[0], shape[1], -1)) / 255. 27 | else: 28 | raise ValueError('Unsupported input format!') 29 | img = np.transpose(img, df) 30 | return img 31 | 32 | 33 | def drawblock(arr, num_class=10, fixed=False, flip=False, split=False): 34 | """ 35 | draw images in block 36 | :param arr: array of images. format='NHWC'. sequence=[cls1,cls2,cls3,...,clsN,cls1,cls2,...clsN] 37 | :param num_class: number of class. default as number of images across height. Use flip=True to set number of width as across width instead 38 | :param fixed: force number of number of width == number of height 39 | :param flip: flip 40 | :param split: set an int to split. currently only support split horizontally 41 | :return: blocks of images 42 | """ 43 | n_im = arr.shape[0] 44 | h_im = arr.shape[1] 45 | w_im = arr.shape[2] 46 | c_im = arr.shape[3] 47 | 48 | if flip: 49 | num_w = num_class 50 | num_h = np.ceil(np.float(n_im) / num_w) if not fixed else num_w 51 | if fixed and isinstance(fixed, int): 52 | num_h = fixed 53 | else: 54 | num_h = num_class 55 | num_w = np.ceil(np.float(n_im) / num_h) if not fixed else num_h 56 | if fixed and isinstance(fixed, int): 57 | num_w = fixed 58 | h_block = (h_im + 2) * num_h - 2 59 | w_block = (w_im + 2) * num_w - 2 60 | newarr = np.zeros((int(h_block), int(w_block), int(c_im)), dtype=np.uint8) 61 | for i in xrange(n_im): 62 | if i > num_w * num_h - 1: 63 | break 64 | if flip: 65 | wk = i % num_w 66 | hk = i // num_w 67 | else: 68 | hk = i % num_h 69 | wk = i // num_h 70 | wk = int(wk) 71 | hk = int(hk) 72 | newarr[hk*(h_im+2):hk*(h_im+2)+h_im, wk*(w_im+2):wk*(w_im+2)+w_im, :] = arr[i] 73 | 74 | if split: 75 | temp = newarr 76 | newnh = int(np.ceil(float(num_class) / split)) 77 | newh = (h_im + 2) * newnh - 2 78 | neww = int(w_block * split + 2) 79 | newarr = np.zeros((newh, neww, int(c_im)), dtype=np.uint8) 80 | for i in range(split): 81 | if not num_class % split == 0 and i == split - 1: 82 | newarr[:-h_im-2, i * w_block+i*2:(i + 1) * w_block+(i+1)*2, :] = temp[i * newh+i*2:, :, :] 83 | else: 84 | newarr[:, i*w_block+i*2:(i+1)*w_block, :] = temp[i*newh+i*2:(i+1)*newh, :, :] 85 | return newarr 86 | 87 | 88 | def readclasslabels(filepath): 89 | with open(filepath, 'r') as f: 90 | lines = f.readlines() 91 | labels = [] 92 | for line in lines: 93 | labels.append(line.split()[1]) 94 | return labels 95 | 96 | 97 | class Tee(object): 98 | def __init__(self, *files): 99 | self.files = files 100 | 101 | def write(self, obj): 102 | for f in self.files: 103 | f.write(obj) 104 | f.flush() 105 | 106 | def flush(self): 107 | for f in self.files: 108 | f.flush() 109 | 110 | 111 | def datasetweights(dataset): 112 | dataset.reset() 113 | weights = np.zeros(10) 114 | for x, t in dataset: 115 | t = t.get().argmax(axis=0) 116 | i, c = np.unique(t, return_counts=True) 117 | for ii, cc in zip(i, c): 118 | weights[ii] += cc 119 | weights /= dataset.ndata 120 | return weights 121 | 122 | 123 | def specialchar(s): 124 | return ''.join( 125 | c for c in unicodedata.normalize('NFD', s) 126 | if unicodedata.category(c) != 'Mn' 127 | ) 128 | 129 | 130 | def createfolders(par, *args): 131 | if not os.path.exists(par): 132 | os.mkdir(par) 133 | return_list = [] 134 | for arg in args: 135 | new_dir = par + arg 136 | if not os.path.exists(new_dir): 137 | os.mkdir(new_dir) 138 | return_list.append(new_dir) 139 | return return_list 140 | -------------------------------------------------------------------------------- /data/ingest_cub200.py: -------------------------------------------------------------------------------- 1 | from configargparse import ArgParser 2 | from PIL import Image 3 | import logging 4 | import numpy as np 5 | import os 6 | import shutil 7 | 8 | 9 | def transform_and_save(img_path, target_size, output_filename, skip=False): 10 | """ 11 | Takes an image and 12 | optionally transforms it and then writes it out to output_filename 13 | """ 14 | if skip and os.path.exists(output_filename): 15 | return 16 | img = Image.open(img_path) 17 | width, height = img.size 18 | 19 | # Take the smaller image dimension down to target_size 20 | # while retaining aspect_ration. Otherwise leave it alone 21 | if width < height: 22 | if width > target_size: 23 | scale_factor = float(target_size) / width 24 | width = target_size 25 | height = int(height*scale_factor) 26 | else: 27 | if height > target_size: 28 | scale_factor = float(target_size) / height 29 | height = target_size 30 | width = int(width*scale_factor) 31 | if img.size[0] != width or img.size[1] != height: 32 | img = img.resize((width, height), resample=Image.LANCZOS) 33 | img.save(output_filename, quality=100) 34 | else: 35 | # Avoid recompression by saving file out directly without transformation 36 | shutil.copy(img_path, output_filename) 37 | assert (os.stat(output_filename).st_size > 0), "{} has size 0".format(output_filename) 38 | 39 | 40 | class Ingest(object): 41 | 42 | def __init__(self, input_dir, out_dir, target_size=256, skipimg=False): 43 | np.random.seed(0) 44 | self.skipimg = skipimg 45 | self.out_dir = out_dir 46 | self.input_dir = input_dir 47 | self.input_img_dir = os.path.join(input_dir, 'images') 48 | 49 | self.manifests = dict() 50 | for setn in ('train', 'val'): 51 | self.manifests[setn] = os.path.join(self.out_dir, '{}-index.csv'.format(setn)) 52 | 53 | self.target_size = target_size 54 | self.ntrain = [] 55 | 56 | self.trainpairlist = {} 57 | self.valpairlist = {} 58 | self.labels = range(200) 59 | 60 | if not os.path.exists(self.out_dir): 61 | os.mkdir(self.out_dir) 62 | 63 | self.outimgdir = os.path.join(self.out_dir, 'images') 64 | if not os.path.exists(self.outimgdir): 65 | os.mkdir(self.outimgdir) 66 | 67 | self.outlabeldir = os.path.join(self.out_dir, 'labels') 68 | if not os.path.exists(self.outlabeldir): 69 | os.mkdir(self.outlabeldir) 70 | 71 | def collectdata(self,): 72 | print 'Start Collect Data...' 73 | with open(self.input_dir + '/images.txt', 'rb') as f: 74 | img_list = f.readlines() 75 | with open(self.input_dir + '/image_class_labels.txt', 'rb') as f: 76 | img_labels = f.readlines() 77 | with open(self.input_dir + '/train_test_split.txt', 'rb') as f: 78 | img_split = f.readlines() 79 | 80 | for img, label, trainval in zip(img_list, img_labels, img_split): 81 | img_pathsemi = img.split()[1] 82 | lab = int(label.split()[1]) - 1 83 | tv = int(trainval.split()[1]) 84 | 85 | sdir = os.path.join(self.outimgdir, img_pathsemi.split('/')[0]) 86 | if not os.path.exists(sdir): 87 | os.mkdir(sdir) 88 | 89 | imgpath = os.path.join(self.input_img_dir, img_pathsemi) 90 | outpath = os.path.join(self.outimgdir, img_pathsemi) 91 | transform_and_save(img_path=imgpath, output_filename=outpath, target_size=self.target_size, skip=self.skipimg) 92 | 93 | if tv: 94 | self.trainpairlist[os.path.join('images', img_pathsemi)] = os.path.join('labels', str(lab) + '.txt') 95 | else: 96 | self.valpairlist[os.path.join('images', img_pathsemi)] = os.path.join('labels', str(lab) + '.txt') 97 | 98 | print 'Finished Collect Data...' 99 | 100 | def write_label(self, ): 101 | for i, l in enumerate(self.labels): 102 | sdir = os.path.join(self.outlabeldir, str(i) + '.txt') 103 | np.savetxt(sdir, [l], '%d') 104 | 105 | def run(self): 106 | """ 107 | resize images then write manifest files to disk. 108 | """ 109 | self.write_label() 110 | self.collectdata() 111 | 112 | records = [(fname, tgt) 113 | for fname, tgt in self.trainpairlist.items()] 114 | np.savetxt(self.manifests['train'], records, fmt='%s,%s') 115 | 116 | records = [(fname, tgt) 117 | for fname, tgt in self.valpairlist.items()] 118 | np.savetxt(self.manifests['val'], records, fmt='%s,%s') 119 | 120 | 121 | if __name__ == "__main__": 122 | parser = ArgParser() 123 | parser.add_argument('--input_dir', help='Directory to find input', 124 | default='/hdd/Dataset/CUB200/CUB_200_2011') 125 | parser.add_argument('--out_dir', help='Directory to write ingested files', 126 | default='/home/william/PyProjects/TFcodes/dataset/cub200') 127 | parser.add_argument('--target_size', type=int, default=256, 128 | help='Size in pixels to scale shortest side DOWN to (0 means no scaling)') 129 | parser.add_argument('--ratio', type=float, default=0.3, 130 | help='Percentage of dataset to be used for validation') 131 | parser.add_argument('--skipImg', type=bool, default=True, 132 | help='True to skip processing and copying images') 133 | args = parser.parse_args() 134 | 135 | logger = logging.getLogger(__name__) 136 | 137 | bw = Ingest(input_dir=args.input_dir, out_dir=args.out_dir, target_size=args.target_size, 138 | skipimg=args.skipImg) 139 | bw.run() 140 | -------------------------------------------------------------------------------- /data/ingest_flower102.py: -------------------------------------------------------------------------------- 1 | from configargparse import ArgParser 2 | from PIL import Image 3 | import logging 4 | import numpy as np 5 | import os 6 | import shutil 7 | import scipy.io as sio 8 | 9 | 10 | def transform_and_save(img_path, target_size, output_filename, skip=False): 11 | """ 12 | Takes an image and 13 | optionally transforms it and then writes it out to output_filename 14 | """ 15 | if skip and os.path.exists(output_filename): 16 | return 17 | img = Image.open(img_path) 18 | width, height = img.size 19 | 20 | # Take the smaller image dimension down to target_size 21 | # while retaining aspect_ration. Otherwise leave it alone 22 | if width < height: 23 | if width > target_size: 24 | scale_factor = float(target_size) / width 25 | width = target_size 26 | height = int(height*scale_factor) 27 | else: 28 | if height > target_size: 29 | scale_factor = float(target_size) / height 30 | height = target_size 31 | width = int(width*scale_factor) 32 | if img.size[0] != width or img.size[1] != height: 33 | img = img.resize((width, height), resample=Image.LANCZOS) 34 | img.save(output_filename, quality=100) 35 | else: 36 | # Avoid recompression by saving file out directly without transformation 37 | shutil.copy(img_path, output_filename) 38 | assert (os.stat(output_filename).st_size > 0), "{} has size 0".format(output_filename) 39 | 40 | 41 | class Ingest(object): 42 | 43 | def __init__(self, input_dir, out_dir, target_size=256, skipimg=False): 44 | np.random.seed(0) 45 | self.skipimg = skipimg 46 | self.out_dir = out_dir 47 | self.input_dir = input_dir 48 | self.input_img_dir = os.path.join(input_dir, 'jpg') 49 | 50 | self.manifests = dict() 51 | for setn in ('train', 'val', 'test'): 52 | self.manifests[setn] = os.path.join(self.out_dir, '{}-index.csv'.format(setn)) 53 | 54 | self.target_size = target_size 55 | self.ntrain = [] 56 | 57 | self.trainpairlist = {} 58 | self.valpairlist = {} 59 | self.testpairlist = {} 60 | self.labels = range(102) 61 | 62 | if not os.path.exists(self.out_dir): 63 | os.mkdir(self.out_dir) 64 | 65 | self.outimgdir = os.path.join(self.out_dir, 'images') 66 | if not os.path.exists(self.outimgdir): 67 | os.mkdir(self.outimgdir) 68 | 69 | self.outlabeldir = os.path.join(self.out_dir, 'labels') 70 | if not os.path.exists(self.outlabeldir): 71 | os.mkdir(self.outlabeldir) 72 | 73 | def collectdata(self,): 74 | print 'Start Collect Data...' 75 | img_labels = sio.loadmat(self.input_dir + '/imagelabels.mat')['labels'][0] 76 | img_split = sio.loadmat(self.input_dir + '/setid.mat') 77 | img_train = img_split['trnid'] 78 | img_val = img_split['valid'] 79 | img_test = img_split['tstid'] 80 | 81 | for idx in img_train[0]: 82 | img_name = 'image_%05d.jpg' % idx 83 | imgpath = os.path.join(self.input_img_dir, img_name) 84 | outpath = os.path.join(self.outimgdir, img_name) 85 | transform_and_save(img_path=imgpath, output_filename=outpath, target_size=self.target_size, skip=self.skipimg) 86 | self.trainpairlist[os.path.join('images', img_name)] = os.path.join('labels', str(img_labels[idx-1] - 1) + '.txt') 87 | 88 | for idx in img_val[0]: 89 | img_name = 'image_%05d.jpg' % idx 90 | imgpath = os.path.join(self.input_img_dir, img_name) 91 | outpath = os.path.join(self.outimgdir, img_name) 92 | transform_and_save(img_path=imgpath, output_filename=outpath, target_size=self.target_size, 93 | skip=self.skipimg) 94 | self.valpairlist[os.path.join('images', img_name)] = os.path.join('labels', str(img_labels[idx-1] - 1) + '.txt') 95 | 96 | for idx in img_test[0]: 97 | img_name = 'image_%05d.jpg' % idx 98 | imgpath = os.path.join(self.input_img_dir, img_name) 99 | outpath = os.path.join(self.outimgdir, img_name) 100 | transform_and_save(img_path=imgpath, output_filename=outpath, target_size=self.target_size, 101 | skip=self.skipimg) 102 | self.testpairlist[os.path.join('images', img_name)] = os.path.join('labels', str(img_labels[idx-1] - 1) + '.txt') 103 | 104 | print 'Finished Collect Data...' 105 | 106 | def write_label(self, ): 107 | for i, l in enumerate(self.labels): 108 | sdir = os.path.join(self.outlabeldir, str(i) + '.txt') 109 | np.savetxt(sdir, [l], '%d') 110 | 111 | def run(self): 112 | """ 113 | resize images then write manifest files to disk. 114 | """ 115 | self.write_label() 116 | self.collectdata() 117 | 118 | records = [(fname, tgt) 119 | for fname, tgt in self.trainpairlist.items()] 120 | np.savetxt(self.manifests['train'], records, fmt='%s,%s') 121 | 122 | records = [(fname, tgt) 123 | for fname, tgt in self.valpairlist.items()] 124 | np.savetxt(self.manifests['val'], records, fmt='%s,%s') 125 | 126 | records = [(fname, tgt) 127 | for fname, tgt in self.testpairlist.items()] 128 | np.savetxt(self.manifests['test'], records, fmt='%s,%s') 129 | 130 | 131 | if __name__ == "__main__": 132 | parser = ArgParser() 133 | parser.add_argument('--input_dir', help='Directory to find input', 134 | default='/hdd/Dataset/Flower102') 135 | parser.add_argument('--out_dir', help='Directory to write ingested files', 136 | default='/home/william/PyProjects/TFcodes/dataset/flower102') 137 | parser.add_argument('--target_size', type=int, default=256, 138 | help='Size in pixels to scale shortest side DOWN to (0 means no scaling)') 139 | parser.add_argument('--ratio', type=float, default=0.3, 140 | help='Percentage of dataset to be used for validation') 141 | parser.add_argument('--skipImg', type=bool, default=True, 142 | help='True to skip processing and copying images') 143 | args = parser.parse_args() 144 | 145 | logger = logging.getLogger(__name__) 146 | 147 | bw = Ingest(input_dir=args.input_dir, out_dir=args.out_dir, target_size=args.target_size, 148 | skipimg=args.skipImg) 149 | bw.run() 150 | -------------------------------------------------------------------------------- /data/ingest_wikiart.py: -------------------------------------------------------------------------------- 1 | from configargparse import ArgParser 2 | from PIL import Image 3 | import logging 4 | import numpy as np 5 | import os 6 | import shutil 7 | import csv 8 | 9 | 10 | def transform_and_save(img_path, target_size, output_filename, skip=False): 11 | """ 12 | Takes an image and 13 | optionally transforms it and then writes it out to output_filename 14 | """ 15 | if skip and os.path.exists(output_filename): 16 | return 17 | img = Image.open(img_path) 18 | width, height = img.size 19 | 20 | # Take the smaller image dimension down to target_size 21 | # while retaining aspect_ration. Otherwise leave it alone 22 | if width < height: 23 | if width > target_size: 24 | scale_factor = float(target_size) / width 25 | width = target_size 26 | height = int(height*scale_factor) 27 | else: 28 | if height > target_size: 29 | scale_factor = float(target_size) / height 30 | height = target_size 31 | width = int(width*scale_factor) 32 | if img.size[0] != width or img.size[1] != height: 33 | img = img.resize((width, height), resample=Image.LANCZOS) 34 | img.save(output_filename, quality=100) 35 | else: 36 | # Avoid recompression by saving file out directly without transformation 37 | shutil.copy(img_path, output_filename) 38 | assert (os.stat(output_filename).st_size > 0), "{} has size 0".format(output_filename) 39 | 40 | 41 | class Ingest(object): 42 | 43 | def __init__(self, input_dir, out_dir, csv_file, target_size=256, skipimg=False): 44 | """ 45 | :param input_dir: input directory 46 | :param out_dir: output directory 47 | :param csv_file: style or artist or genre 48 | :param target_size: resized image size 49 | :param skipimg: skip saving image if true and the image exists 50 | """ 51 | np.random.seed(0) 52 | self.skipimg = skipimg 53 | self.out_dir = out_dir 54 | self.input_dir = input_dir 55 | self.input_img_dir = os.path.join(input_dir, 'style') 56 | self.input_csv_dir = os.path.join(input_dir, 'wikiart_csv') 57 | 58 | self.csv_train = self.input_csv_dir + '/' + csv_file + '_train.csv' 59 | self.csv_val = self.input_csv_dir + '/' + csv_file + '_val.csv' 60 | 61 | self.manifests = dict() 62 | for setn in ('train', 'val'): 63 | self.manifests[setn] = os.path.join(self.out_dir, csv_file + '-{}-index.csv'.format(setn)) 64 | 65 | self.target_size = target_size 66 | self.ntrain = [] 67 | 68 | self.trainpairlist = {} 69 | self.valpairlist = {} 70 | if csv_file is 'style': 71 | self.labels = range(27) 72 | elif csv_file is 'genre': 73 | self.labels = range(10) 74 | elif csv_file is 'artist': 75 | self.labels = range(23) 76 | else: 77 | raise ValueError('csv_file must be either [style, genre, artist]') 78 | 79 | if not os.path.exists(self.out_dir): 80 | os.mkdir(self.out_dir) 81 | 82 | self.outimgdir = os.path.join(self.out_dir, 'images') 83 | if not os.path.exists(self.outimgdir): 84 | os.mkdir(self.outimgdir) 85 | 86 | self.outlabeldir = os.path.join(self.out_dir, 'labels') 87 | if not os.path.exists(self.outlabeldir): 88 | os.mkdir(self.outlabeldir) 89 | 90 | def collectdata(self,): 91 | print 'Start Collect Data...' 92 | csvtrainfile = open(self.csv_train, 'rb') 93 | trainlist = csv.reader(csvtrainfile, delimiter=',') 94 | csvvalfile = open(self.csv_val, 'rb') 95 | vallist = csv.reader(csvvalfile, delimiter=',') 96 | 97 | for row in trainlist: 98 | img_name = row[0] 99 | label = row[1] 100 | 101 | imgpath = os.path.join(self.input_img_dir, img_name) 102 | outpath = os.path.join(self.outimgdir, img_name) 103 | sdir = os.path.join(self.outimgdir, imgpath.split('/')[-2]) 104 | if not os.path.exists(sdir): 105 | os.mkdir(sdir) 106 | transform_and_save(img_path=imgpath, output_filename=outpath, target_size=self.target_size, skip=self.skipimg) 107 | 108 | self.trainpairlist[os.path.join('images', img_name)] = os.path.join('labels', str(label) + '.txt') 109 | 110 | for row in vallist: 111 | img_name = row[0] 112 | label = row[1] 113 | 114 | imgpath = os.path.join(self.input_img_dir, img_name) 115 | outpath = os.path.join(self.outimgdir, img_name) 116 | transform_and_save(img_path=imgpath, output_filename=outpath, target_size=self.target_size, skip=self.skipimg) 117 | 118 | self.valpairlist[os.path.join('images', img_name)] = os.path.join('labels', str(label) + '.txt') 119 | 120 | print 'Finished Collect Data...' 121 | 122 | def write_label(self, ): 123 | for i, l in enumerate(self.labels): 124 | sdir = os.path.join(self.outlabeldir, str(i) + '.txt') 125 | np.savetxt(sdir, [l], '%d') 126 | 127 | def run(self): 128 | """ 129 | resize images then write manifest files to disk. 130 | """ 131 | self.write_label() 132 | self.collectdata() 133 | 134 | records = [(fname, tgt) 135 | for fname, tgt in self.trainpairlist.items()] 136 | np.savetxt(self.manifests['train'], records, fmt='%s,%s') 137 | 138 | records = [(fname, tgt) 139 | for fname, tgt in self.valpairlist.items()] 140 | np.savetxt(self.manifests['val'], records, fmt='%s,%s') 141 | 142 | 143 | if __name__ == "__main__": 144 | parser = ArgParser() 145 | parser.add_argument('--input_dir', help='Directory to find input', 146 | default='/hdd/Dataset/wikiart') 147 | parser.add_argument('--out_dir', help='Directory to write ingested files', 148 | default='/home/william/PyProjects/TFcodes/dataset/wikiart') 149 | parser.add_argument('--target_size', type=int, default=256, 150 | help='Size in pixels to scale shortest side DOWN to (0 means no scaling)') 151 | parser.add_argument('--ratio', type=float, default=0.3, 152 | help='Percentage of dataset to be used for validation') 153 | parser.add_argument('--skipImg', type=bool, default=True, 154 | help='True to skip processing and copying images') 155 | args = parser.parse_args() 156 | 157 | logger = logging.getLogger(__name__) 158 | 159 | bw = Ingest(input_dir=args.input_dir, out_dir=args.out_dir, csv_file='artist', target_size=args.target_size, 160 | skipimg=args.skipImg) 161 | bw.run() 162 | -------------------------------------------------------------------------------- /data/ingest_stl10.py: -------------------------------------------------------------------------------- 1 | from configargparse import ArgParser 2 | from PIL import Image 3 | import logging 4 | import numpy as np 5 | import os 6 | 7 | 8 | def transform_and_save(img_arr, output_filename): 9 | """ 10 | Takes an image and optionally transforms it and then writes it out to output_filename 11 | """ 12 | img = Image.fromarray(img_arr) 13 | img.save(output_filename) 14 | 15 | 16 | class Ingest(object): 17 | def __init__(self, input_dir, out_dir, target_size=96, skipimg=False): 18 | np.random.seed(0) 19 | self.skipimg = skipimg 20 | self.out_dir = out_dir 21 | self.input_dir = input_dir 22 | 23 | self.manifests = dict() 24 | for setn in ('train', 'val'): 25 | self.manifests[setn] = os.path.join(self.out_dir, '{}-index.csv'.format(setn)) 26 | 27 | self.target_size = target_size 28 | self.trainpairlist = {} 29 | self.valpairlist = {} 30 | self.labels = range(10) 31 | 32 | if not os.path.exists(self.out_dir): 33 | os.mkdir(self.out_dir) 34 | self.outimgdir = os.path.join(self.out_dir, 'images') 35 | if not os.path.exists(self.outimgdir): 36 | os.mkdir(self.outimgdir) 37 | os.mkdir(os.path.join(self.outimgdir, 'train')) 38 | os.mkdir(os.path.join(self.outimgdir, 'val')) 39 | self.outlabeldir = os.path.join(self.out_dir, 'labels') 40 | if not os.path.exists(self.outlabeldir): 41 | os.mkdir(self.outlabeldir) 42 | 43 | def collectdata(self,): 44 | print 'Start Collect Data...' 45 | 46 | train_x_path = os.path.join(self.input_dir, 'train_X.bin') 47 | train_y_path = os.path.join(self.input_dir, 'train_y.bin') 48 | test_x_path = os.path.join(self.input_dir, 'test_X.bin') 49 | test_y_path = os.path.join(self.input_dir, 'test_y.bin') 50 | 51 | train_xf = open(train_x_path, 'rb') 52 | train_x = np.fromfile(train_xf, dtype=np.uint8) 53 | train_x = np.reshape(train_x, (-1, 3, 96, 96)) 54 | train_x = np.transpose(train_x, (0, 3, 2, 1)) 55 | train_yf = open(train_y_path, 'rb') 56 | train_y = np.fromfile(train_yf, dtype=np.uint8) 57 | 58 | test_xf = open(test_x_path, 'rb') 59 | test_x = np.fromfile(test_xf, dtype=np.uint8) 60 | test_x = np.reshape(test_x, (-1, 3, 96, 96)) 61 | test_x = np.transpose(test_x, (0, 3, 2, 1)) 62 | test_yf = open(test_y_path, 'rb') 63 | test_y = np.fromfile(test_yf, dtype=np.uint8) 64 | 65 | idx = np.zeros(10, dtype=np.int) 66 | for i in xrange(train_x.shape[0]): 67 | outdir = os.path.join(self.outimgdir, 'train', str(train_y[i]-1)) 68 | if not os.path.exists(outdir): 69 | os.mkdir(outdir) 70 | 71 | if not self.skipimg: 72 | transform_and_save(img_arr=train_x[i], output_filename=os.path.join(outdir, str(idx[train_y[i]-1]) + '.jpg')) 73 | self.trainpairlist[os.path.join('images', 'train', str(train_y[i]-1), str(idx[train_y[i]-1]) + '.jpg')] = \ 74 | os.path.join('labels', str(train_y[i] - 1) + '.txt') 75 | idx[train_y[i]-1] += 1 76 | 77 | idx = np.zeros(10, dtype=np.int) 78 | for i in xrange(test_x.shape[0]): 79 | outdir = os.path.join(self.outimgdir, 'val', str(test_y[i]-1)) 80 | if not os.path.exists(outdir): 81 | os.mkdir(outdir) 82 | 83 | if not self.skipimg: 84 | transform_and_save(img_arr=test_x[i], 85 | output_filename=os.path.join(outdir, str(idx[test_y[i]-1]) + '.jpg')) 86 | self.valpairlist[os.path.join('images', 'val', str(test_y[i]-1), str(idx[test_y[i]-1]) + '.jpg')] = \ 87 | os.path.join('labels', str(test_y[i] - 1) + '.txt') 88 | idx[test_y[i]-1] += 1 89 | 90 | print 'Finished Collect Data...' 91 | 92 | def write_label(self, ): 93 | for i, l in enumerate(self.labels): 94 | sdir = os.path.join(self.outlabeldir, str(i) + '.txt') 95 | np.savetxt(sdir, [l], '%d') 96 | 97 | def run(self): 98 | """ 99 | resize images then write manifest files to disk. 100 | """ 101 | self.write_label() 102 | self.collectdata() 103 | 104 | records = [(fname, tgt) 105 | for fname, tgt in self.trainpairlist.items()] 106 | np.savetxt(self.manifests['train'], records, fmt='%s,%s') 107 | 108 | records = [(fname, tgt) 109 | for fname, tgt in self.valpairlist.items()] 110 | np.savetxt(self.manifests['val'], records, fmt='%s,%s') 111 | 112 | 113 | class IngestUnlabeled(object): 114 | def __init__(self, input_dir, out_dir, target_size=96, skipimg=False): 115 | np.random.seed(0) 116 | self.skipimg = skipimg 117 | self.out_dir = out_dir 118 | self.input_dir = input_dir 119 | 120 | self.manifests = dict() 121 | self.manifests = os.path.join(self.out_dir, 'unlabeled-index.csv') 122 | 123 | self.target_size = target_size 124 | self.trainpairlist = {} 125 | 126 | if not os.path.exists(self.out_dir): 127 | os.mkdir(self.out_dir) 128 | self.outimgdir = os.path.join(self.out_dir, 'images') 129 | if not os.path.exists(self.outimgdir): 130 | os.mkdir(self.outimgdir) 131 | self.unlabeldir = os.path.join(self.outimgdir, 'unlabeled') 132 | if not os.path.exists(self.unlabeldir): 133 | os.mkdir(self.unlabeldir) 134 | 135 | def collectdata(self,): 136 | print 'Start Collect Data...' 137 | 138 | train_x_path = os.path.join(self.input_dir, 'unlabeled_X.bin') 139 | 140 | train_xf = open(train_x_path, 'rb') 141 | train_x = np.fromfile(train_xf, dtype=np.uint8) 142 | train_x = np.reshape(train_x, (-1, 3, 96, 96)) 143 | train_x = np.transpose(train_x, (0, 3, 2, 1)) 144 | 145 | idx = 0 146 | for i in xrange(train_x.shape[0]): 147 | if not self.skipimg: 148 | transform_and_save(img_arr=train_x[i], output_filename=os.path.join(self.unlabeldir, str(idx) + '.jpg')) 149 | self.trainpairlist[os.path.join('images', 'unlabeled', str(idx) + '.jpg')] = 'labels/11.txt' 150 | idx += 1 151 | 152 | print 'Finished Collect Data...' 153 | 154 | def write_label(self, ): 155 | sdir = os.path.join(self.out_dir, 'labels', '11.txt') 156 | np.savetxt(sdir, [11], '%d') 157 | 158 | def run(self): 159 | """ 160 | resize images then write manifest files to disk. 161 | """ 162 | self.write_label() 163 | self.collectdata() 164 | 165 | records = [(fname, tgt) 166 | for fname, tgt in self.trainpairlist.items()] 167 | np.savetxt(self.manifests, records, fmt='%s,%s') 168 | 169 | 170 | if __name__ == "__main__": 171 | parser = ArgParser() 172 | parser.add_argument('--input_dir', help='Directory to find input', 173 | default='/hdd/Dataset/STL10') 174 | parser.add_argument('--out_dir', help='Directory to write ingested files', 175 | default='/home/william/PyProjects/TFcodes/dataset/stl10') 176 | parser.add_argument('--target_size', type=int, default=96, 177 | help='Size in pixels to scale shortest side DOWN to (0 means no scaling)') 178 | parser.add_argument('--skipImg', type=bool, default=False, 179 | help='True to skip processing and copying images') 180 | args = parser.parse_args() 181 | logger = logging.getLogger(__name__) 182 | bw = Ingest(input_dir=args.input_dir, out_dir=args.out_dir, target_size=args.target_size, skipimg=args.skipImg) 183 | # bw = IngestUnlabeled(input_dir=args.input_dir, out_dir=args.out_dir, target_size=args.target_size, skipimg=args.skipImg) 184 | bw.run() 185 | -------------------------------------------------------------------------------- /nn/layers.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | 5 | def instance_norm(net, train=False, epsilon=1e-8, name='in'): 6 | """use shape NCHW""" 7 | with tf.variable_scope(name): 8 | mu, sigma_sq = tf.nn.moments(net, [2, 3], keep_dims=True) 9 | normalized = (net - mu) / tf.sqrt(sigma_sq + epsilon) 10 | if train: 11 | var_shape = net.get_shape().as_list()[1] 12 | shift = tf.get_variable('shift', shape=[1, var_shape, 1, 1], initializer=tf.constant_initializer(0.)) 13 | scale = tf.get_variable('scale', shape=[1, var_shape, 1, 1], initializer=tf.constant_initializer(1.)) 14 | normalized = scale * normalized + shift 15 | return normalized 16 | 17 | 18 | def adain(net1, net2, epsilon=1e-9, name='in'): 19 | """use shape NCHW""" 20 | with tf.variable_scope(name): 21 | mu, sigma_sq = tf.nn.moments(net1, [2, 3], keep_dims=True) 22 | normalized = (net1 - mu) / tf.sqrt(sigma_sq + epsilon) 23 | 24 | shift, scale = tf.nn.moments(net2, [2, 3], keep_dims=True) 25 | normalized = tf.sqrt(scale + epsilon) * normalized + shift 26 | return normalized 27 | 28 | 29 | def adain2(net1, shift, scale, epsilon=1e-9, name='in'): 30 | """use shape NCHW""" 31 | with tf.variable_scope(name): 32 | mu, sigma_sq = tf.nn.moments(net1, [2, 3], keep_dims=True) 33 | normalized = (net1 - mu) / tf.sqrt(sigma_sq + epsilon) 34 | normalized = tf.sqrt(scale + epsilon) * normalized + shift 35 | return normalized 36 | 37 | 38 | def layernorm(x, epsilon=1e-5, name='lnconv'): 39 | """Layer Normalization for conv. x must be [NCHW]""" 40 | shape = x.get_shape().as_list() 41 | with tf.variable_scope(name): 42 | beta = tf.get_variable("beta", [1, shape[1], 1, 1], initializer=tf.constant_initializer(0.)) 43 | gamma = tf.get_variable("gamma", [1, shape[1], 1, 1], initializer=tf.constant_initializer(1.)) 44 | mean, var = tf.nn.moments(x, range(1, len(shape)), keep_dims=True) 45 | return beta * (x - mean) / tf.sqrt(var + epsilon) + gamma 46 | 47 | 48 | def batchnorm(inputT, is_training=tf.constant(True), name='bn'): 49 | # Note: is_training is tf.placeholder(tf.bool) type 50 | with tf.variable_scope(name) as vs: 51 | return tf.cond(is_training, 52 | lambda: tf.contrib.layers.batch_norm(inputT, is_training=True, decay=0.9, fused=True, 53 | updates_collections=None, center=True, scale=True, 54 | scope=vs, data_format='NCHW'), 55 | lambda: tf.contrib.layers.batch_norm(inputT, is_training=False, decay=0.9, fused=True, 56 | updates_collections=None, center=True, scale=True, 57 | scope=vs, data_format='NCHW', reuse=True)) 58 | 59 | 60 | def batchnorm_true(inputT, name='bn', fused=True): 61 | with tf.variable_scope(name) as vs: 62 | return tf.contrib.layers.batch_norm(inputT, is_training=True, decay=0.9, fused=fused, 63 | updates_collections=None, center=True, scale=True, 64 | scope=vs, data_format='NCHW') 65 | 66 | 67 | def featurenorm(x, epsilon=1e-8, name='fn'): 68 | """ 69 | Pixelwise feature vector normalization: PROGRESSIVE GROWING OF GANS FOR IMPROVED QUALITY, STABILITY, AND VARIATION 70 | Use "NCHW". Works same for FC layers. 71 | """ 72 | with tf.variable_scope(name): 73 | norm = tf.sqrt(tf.reduce_mean(tf.square(x), axis=1, keep_dims=True) + epsilon) 74 | return x / norm 75 | 76 | 77 | def minibatch_std(x, name='ministd'): 78 | shapes = x.get_shape().as_list() 79 | with tf.variable_scope(name): 80 | _, var = tf.nn.moments(x, [0], keep_dims=True) 81 | return tf.concat([x, tf.tile(tf.reduce_mean(var, keep_dims=True), [shapes[0], 1, shapes[2], shapes[3]])], axis=1) 82 | 83 | 84 | def conv2d(x, nout, kernel=3, std=0.02, use_b=False, strides=1, name='conv2d', print_struct=True, pad='SAME'): 85 | if pad == 0: 86 | pad = 'VALID' 87 | with tf.variable_scope(name): 88 | W = tf.get_variable('W', [kernel, kernel, x.get_shape()[1], nout], 89 | initializer=tf.random_normal_initializer(stddev=std)) 90 | conv = tf.nn.conv2d(x, W, strides=[1, 1, strides, strides], padding=pad, data_format='NCHW') 91 | if print_struct: 92 | print conv.name + ': ' + str(conv.get_shape().as_list()) 93 | if use_b: 94 | b = tf.get_variable('b', [nout], initializer=tf.constant_initializer(0.0)) 95 | conv = tf.nn.bias_add(conv, b, data_format='NCHW') 96 | return conv 97 | 98 | 99 | def deconv2d(x, nout, kernel=3, std=0.02, use_b=False, strides=2, name='deconv2d'): 100 | # Not tested yet! 101 | with tf.variable_scope(name): 102 | shape = x.get_shape().as_list() 103 | W = tf.get_variable('W', [kernel, kernel, nout, shape[1]], 104 | initializer=tf.random_normal_initializer(stddev=std)) 105 | deconv = tf.nn.conv2d_transpose(x, W, [shape[0], nout, shape[2] * strides, shape[3] * strides], 106 | [1, 1, strides, strides], data_format='NCHW') 107 | if use_b: 108 | b = tf.get_variable('b', [nout], initializer=tf.constant_initializer(0.0)) 109 | deconv = tf.nn.bias_add(x, b, data_format='NCHW') 110 | print deconv.name + ': ' + str(deconv.get_shape().as_list()) 111 | return deconv 112 | 113 | 114 | def pool(x, fsize=2, strides=2, op='max', pad='SAME'): 115 | assert pad in ['VALID', 'SAME'] 116 | if op is 'max': 117 | pooled = tf.nn.max_pool(x, ksize=[1, 1, fsize, fsize], strides=[1, 1, strides, strides], padding=pad, 118 | data_format='NCHW') 119 | elif op is 'avg': 120 | pooled = tf.nn.avg_pool(x, ksize=[1, 1, fsize, fsize], strides=[1, 1, strides, strides], padding=pad, 121 | data_format='NCHW') 122 | else: 123 | raise ValueError('op only supports max or avg!') 124 | print pooled.name + ': ' + str(pooled.get_shape().as_list()) 125 | return pooled 126 | 127 | 128 | def linear(x, nout, std=0.02, use_b=False, init_b=0.0, name='linear'): 129 | with tf.variable_scope(name): 130 | W = tf.get_variable('W', [x.get_shape()[-1], nout], initializer=tf.random_normal_initializer(stddev=std)) 131 | lout = tf.matmul(x, W) 132 | if use_b: 133 | b = tf.get_variable('b', [nout], initializer=tf.constant_initializer(init_b)) 134 | lout = tf.nn.bias_add(lout, b) 135 | print lout.name + ': ' + str(lout.get_shape().as_list()) 136 | return lout 137 | 138 | 139 | def flatten(x): 140 | return tf.reshape(x, [-1, np.prod(x.get_shape().as_list()[1:])]) 141 | 142 | 143 | def nnupsampling(inp, size): 144 | upsample = tf.transpose(tf.image.resize_nearest_neighbor(tf.transpose(inp, [0, 2, 3, 1]), size), [0, 3, 1, 2]) 145 | print upsample.name + ': ' + str(upsample.get_shape().as_list()) 146 | return upsample 147 | 148 | 149 | def subpixel(inp, nfm, upscale=2, name='subpixel'): 150 | # assert inp.get_shape().as_list()[1] % upscale == 0 151 | output = conv2d(inp, nout=nfm * (upscale ** 2), kernel=1, name=name, print_struct=False) 152 | output = tf.transpose(output, [0, 2, 3, 1]) 153 | output = tf.depth_to_space(output, upscale) 154 | output = tf.transpose(output, [0, 3, 1, 2]) 155 | print name + ': ' + str(output.get_shape().as_list()) 156 | return output 157 | 158 | 159 | def gaussnoise(x, std=0.2, wrt=False): 160 | if wrt: 161 | return x + x * tf.random_normal(tf.shape(x), stddev=std) 162 | else: 163 | return x + tf.random_normal(tf.shape(x), stddev=std) 164 | 165 | 166 | def total_variation(preds): 167 | # total variation denoising 168 | b, c, w, h = preds.get_shape().as_list() 169 | y_tv = tf.nn.l2_loss(preds[:, 1:, :, :] - preds[:, :w - 1, :, :]) 170 | x_tv = tf.nn.l2_loss(preds[:, :, 1:, :] - preds[:, :, :h - 1, :]) 171 | tv_loss = 2 * (x_tv + y_tv) / b / w / h / c 172 | return tv_loss 173 | 174 | 175 | def pullaway_loss(embeddings): 176 | """ 177 | Pull Away loss calculation 178 | :param embeddings: The embeddings to be orthogonalized for varied faces. Shape [batch_size, embeddings_dim] 179 | :return: pull away term loss 180 | """ 181 | norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True)) 182 | normalized_embeddings = embeddings / norm 183 | similarity = tf.matmul( 184 | normalized_embeddings, normalized_embeddings, transpose_b=True) 185 | similarity -= tf.diag(tf.diag_part(similarity)) 186 | batch_size = tf.cast(tf.shape(embeddings)[0], tf.float32) 187 | pt_loss = tf.reduce_sum(similarity) / (batch_size * (batch_size - 1)) 188 | return pt_loss 189 | 190 | 191 | def minibatch_discrimination(inp, num_kernels=5, kernel_dim=3): 192 | x = linear(inp, num_kernels * kernel_dim) 193 | activation = tf.reshape(x, [-1, num_kernels, kernel_dim]) 194 | diffs = tf.expand_dims(activation, 3) - tf.expand_dims(tf.transpose(activation, [1, 2, 0]), 0) 195 | abs_diffs = tf.reduce_sum(tf.abs(diffs), 2) 196 | minibatch_features = tf.reduce_sum(tf.exp(-abs_diffs), 2) 197 | return tf.concat([inp, minibatch_features], 1) 198 | 199 | 200 | def gramatrix(x, scale=0.1): 201 | shape = x.get_shape().as_list() 202 | x_reshape = tf.reshape(x, [-1, shape[1], shape[2] * shape[3]]) 203 | x_reshape = tf.matmul(x_reshape, x_reshape, transpose_b=True) * scale 204 | print 'gramatrix: ' + str(x_reshape.get_shape().as_list()) 205 | return x_reshape 206 | 207 | 208 | def _phase_shift(I, r): 209 | # Helper function with main phase shift operation 210 | bsize, a, b, c = I.get_shape().as_list() 211 | X = tf.reshape(I, (bsize, a, b, r, r)) 212 | X = tf.transpose(X, (0, 1, 2, 4, 3)) # bsize, a, b, 1, 1 213 | X = tf.split(1, a, X) # a, [bsize, b, r, r] 214 | X = tf.concat(2, [tf.squeeze(x) for x in X]) # bsize, b, a*r, r 215 | X = tf.split(1, b, X) # b, [bsize, a*r, r] 216 | X = tf.concat(2, [tf.squeeze(x) for x in X]) # 217 | # bsize, a*r, b*r 218 | return tf.reshape(X, (bsize, a * r, b * r, 1)) 219 | 220 | 221 | def PS(X, r, scale=2): 222 | # Main OP that you can arbitrarily use in you tensorflow code 223 | Xc = tf.split(tf.transpose(X, [0, 2, 3, 1]), scale, 3) 224 | X = tf.concat([_phase_shift(x, r) for x in Xc], 3) 225 | X = tf.transpose(X, [0, 3, 1, 2]) 226 | print 'PhaseShift: ' + str(X.get_shape().as_list()) 227 | return X 228 | -------------------------------------------------------------------------------- /Genre128GANAE.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, flatten, nnupsampling, batchnorm, gaussnoise, pool 3 | from activations import lrelu 4 | from op import log_sum_exp 5 | from data_loader import train_loader, validation_loader 6 | from neon.backends import gen_backend 7 | import numpy as np 8 | from utils import drawblock, createfolders, OneHot, image_reshape 9 | from scipy.misc import imsave 10 | import os 11 | 12 | 13 | # Create folders to store images 14 | gen_dir, real_dir, gen_dir128 = createfolders("./genimgs/Genre128GANAE", "/gen", "/real", "/gen128") 15 | # Create folder to store models 16 | dir_name = './models/Genre128GANAE' 17 | if not os.path.exists(dir_name): 18 | os.mkdir(dir_name) 19 | 20 | # Parameters 21 | init_iter, max_iter = 0, 50000 22 | display_iter = 100 23 | eval_iter = 100 24 | store_img_iter = 100 25 | save_iter = 1000 26 | 27 | lr_init = 0.0002 28 | batch_size = 100 29 | zdim = 100 30 | n_classes = 10 31 | dropout = 0.2 32 | im_size = [64, 64] 33 | dname, gname = 'd_', 'g_' 34 | tf.set_random_seed(1234) 35 | 36 | # DataLoader 37 | be = gen_backend(backend='cpu', batch_size=batch_size, datatype=np.float32) 38 | root_files = './dataset/wikiart' 39 | manifestfile = os.path.join(root_files, 'genre-train-index.csv') 40 | testmanifest = os.path.join(root_files, 'genre-val-index.csv') 41 | train = train_loader(manifestfile, root_files, be, h=im_size[0], w=im_size[1]) 42 | test = validation_loader(testmanifest, root_files, be, h=im_size[0], w=im_size[1]) 43 | OneHot = OneHot(be, n_classes) 44 | 45 | # Graph input 46 | is_train = tf.placeholder(tf.bool) 47 | keep_prob = tf.placeholder(tf.float32) 48 | x_n = tf.placeholder(tf.float32, [batch_size, 3, im_size[0], im_size[1]]) 49 | y = tf.placeholder(tf.float32, [batch_size, n_classes]) 50 | lr_tf = tf.placeholder(tf.float32) 51 | z = tf.random_uniform([batch_size, zdim], -1, 1) 52 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 53 | 54 | 55 | # Discriminator 56 | def discriminator(inp, reuse=False): 57 | with tf.variable_scope('Encoder', reuse=reuse): 58 | # 64 59 | inp = gaussnoise(inp, std=0.05) 60 | conv1 = conv2d(inp, 128, kernel=3, strides=2, name=dname + 'conv1') 61 | conv1 = lrelu(conv1, 0.2) 62 | # 32 63 | conv2 = tf.nn.dropout(conv1, keep_prob) 64 | conv2 = conv2d(conv2, 256, kernel=3, strides=2, name=dname + 'conv2') 65 | conv2 = batchnorm(conv2, is_training=is_train, name=dname + 'bn2') 66 | conv2 = lrelu(conv2, 0.2) 67 | # 16 68 | conv3 = tf.nn.dropout(conv2, keep_prob) 69 | conv3 = conv2d(conv3, 512, kernel=3, strides=2, name=dname + 'conv3') 70 | conv3 = batchnorm(conv3, is_training=is_train, name=dname + 'bn3') 71 | conv3 = lrelu(conv3, 0.2) 72 | # 8 73 | conv3b = conv2d(conv3, 512, kernel=3, strides=1, name=dname + 'conv3b') 74 | conv3b = batchnorm(conv3b, is_training=is_train, name=dname + 'bn3b') 75 | conv3b = lrelu(conv3b, 0.2) 76 | 77 | conv4 = tf.nn.dropout(conv3b, keep_prob) 78 | conv4 = conv2d(conv4, 1024, kernel=3, strides=2, name=dname + 'conv4') 79 | conv4 = batchnorm(conv4, is_training=is_train, name=dname + 'bn4') 80 | conv4 = lrelu(conv4, 0.2) 81 | # 4 82 | 83 | flat = flatten(conv4) 84 | # Classifier 85 | clspred = linear(flat, n_classes, name=dname + 'cpred') 86 | # Decoder 87 | g1 = conv2d(conv4, nout=512, kernel=3, name=dname + 'deconv1') 88 | g1 = batchnorm(g1, is_training=tf.constant(True), name=dname + 'bn1g') 89 | g1 = lrelu(g1, 0.2) 90 | 91 | g2 = nnupsampling(g1, [8, 8]) 92 | g2 = conv2d(g2, nout=256, kernel=3, name=dname + 'deconv2') 93 | g2 = batchnorm(g2, is_training=tf.constant(True), name=dname + 'bn2g') 94 | g2 = lrelu(g2, 0.2) 95 | 96 | g3 = nnupsampling(g2, [16, 16]) 97 | g3 = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3') 98 | g3 = batchnorm(g3, is_training=tf.constant(True), name=dname + 'bn3g') 99 | g3 = lrelu(g3, 0.2) 100 | 101 | g4 = nnupsampling(g3, [32, 32]) 102 | g4 = conv2d(g4, nout=64, kernel=3, name=dname + 'deconv4') 103 | g4 = batchnorm(g4, is_training=tf.constant(True), name=dname + 'bn4g') 104 | g4 = lrelu(g4, 0.2) 105 | 106 | g5 = nnupsampling(g4, [64, 64]) 107 | g5 = conv2d(g5, nout=32, kernel=3, name=dname + 'deconv5') 108 | g5 = batchnorm(g5, is_training=tf.constant(True), name=dname + 'bn5g') 109 | g5 = lrelu(g5, 0.2) 110 | 111 | g5b = conv2d(g5, nout=3, kernel=3, name=dname + 'deconv5b') 112 | g5b = tf.nn.tanh(g5b) 113 | return clspred, g5b 114 | 115 | 116 | # Generator 117 | def generator(inp_z, inp_y, reuse=False): 118 | with tf.variable_scope('Generator', reuse=reuse): 119 | inp = tf.concat([inp_z, inp_y], 1) 120 | sz = 4 121 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 122 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 123 | g1 = lrelu(g1, 0.2) 124 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 125 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 126 | 127 | g2 = nnupsampling(g1_reshaped, [8, 8]) 128 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 129 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 130 | g2 = lrelu(g2, 0.2) 131 | 132 | g3 = nnupsampling(g2, [16, 16]) 133 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 134 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 135 | g3 = lrelu(g3, 0.2) 136 | 137 | g4 = nnupsampling(g3, [32, 32]) 138 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 139 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 140 | g4 = lrelu(g4, 0.2) 141 | 142 | g5 = nnupsampling(g4, [64, 64]) 143 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 144 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 145 | g5 = lrelu(g5, 0.2) 146 | 147 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 148 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 149 | g5b = lrelu(g5b, 0.2) 150 | 151 | g6 = nnupsampling(g5b, [128, 128]) 152 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 153 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 154 | g6 = lrelu(g6, 0.2) 155 | 156 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 157 | g6b = tf.nn.tanh(g6b) 158 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 159 | return g6b_64, g6b 160 | 161 | # Call functions 162 | Opred_n, recon_n = discriminator(x_n) 163 | samples, samples128 = generator(z, iny) 164 | Opred_g, recon_g = discriminator(samples, reuse=True) 165 | 166 | # Get trainable variables and split 167 | t_vars = tf.trainable_variables() 168 | d_vars = [var for var in t_vars if dname in var.name] 169 | g_vars = [var for var in t_vars if gname in var.name] 170 | print [var.name for var in d_vars] 171 | print [var.name for var in g_vars] 172 | 173 | # Define D loss 174 | lreal = log_sum_exp(Opred_n) 175 | lfake = log_sum_exp(Opred_g) 176 | cost_On = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_n, labels=y)) 177 | cost_Dn = - tf.reduce_mean(lreal) + tf.reduce_mean(tf.nn.softplus(lreal)) 178 | cost_Dg_fake = tf.reduce_mean(tf.nn.softplus(lfake)) 179 | cost_msen = tf.reduce_mean(tf.square(recon_n - x_n)) * 0.5 180 | cost_mseg = tf.reduce_mean(tf.square(recon_g - samples)) * 0.5 181 | D_loss = cost_On + cost_Dn + cost_Dg_fake + cost_msen 182 | # Define G loss 183 | cost_Dg = - tf.reduce_mean(lfake) + tf.reduce_mean(tf.nn.softplus(lfake)) 184 | cost_Og = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_g, labels=iny)) 185 | G_loss = cost_Dg + cost_Og + cost_mseg 186 | 187 | # Define optimizer 188 | d_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(D_loss, var_list=d_vars) 189 | g_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(G_loss, var_list=g_vars) 190 | 191 | # Evaluate model 192 | Oaccuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Opred_n, 1), tf.argmax(y, 1)), tf.float32)) 193 | 194 | # Initialize the variables 195 | init = tf.global_variables_initializer() 196 | # Reset train dataset 197 | train.reset() 198 | # Config for session 199 | config = tf.ConfigProto() 200 | config.gpu_options.allow_growth = True 201 | # Train 202 | with tf.Session(config=config) as sess: 203 | sess.run(init) 204 | saver = tf.train.Saver(max_to_keep=None) 205 | coord = tf.train.Coordinator() 206 | threads = tf.train.start_queue_runners(coord=coord) 207 | for i_iter in range(init_iter, max_iter): 208 | # Control lr 209 | if i_iter < 30000: 210 | lr = lr_init 211 | else: 212 | lr = lr_init / 10. 213 | # Fetch minibatch 214 | batch_x, batch_y = train.next() 215 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 216 | batch_y = OneHot.transform(batch_y).get().transpose() 217 | # update discriminator 218 | _, lossDn, lossOn, lossFake = sess.run([d_optimizer, cost_Dn, cost_On, cost_Dg_fake], feed_dict={ 219 | x_n: batch_x, y: batch_y, 220 | keep_prob: 1. - dropout, is_train: True, lr_tf: lr 221 | }) 222 | # update generator 223 | _, gen_img, gen_img128 = sess.run([g_optimizer, samples, samples128], feed_dict={ 224 | keep_prob: 1., is_train: True, lr_tf: lr 225 | }) 226 | # print losses 227 | if i_iter % display_iter == 0 or i_iter == max_iter - 1: 228 | print 'Iteration: %i, lossDn: %.2f, lossOn: %.2f, lossFake: %.2f' % (i_iter, lossDn, lossOn, lossFake) 229 | # Evaluate classification accuracy 230 | if i_iter % eval_iter == 0 or i_iter == max_iter - 1: 231 | total_Oaccuracy = 0. 232 | test.reset() 233 | for mb_idx, (batch_x, batch_y) in enumerate(test): 234 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 235 | batch_y = batch_y.get().transpose() 236 | total_Oaccuracy += sess.run(Oaccuracy, 237 | feed_dict={x_n: batch_x, y: batch_y, keep_prob: 1., is_train: False}) 238 | print 'Iteration %i, Accuracy: %.2f' % (i_iter, total_Oaccuracy / mb_idx) 239 | # Store images 240 | if i_iter % store_img_iter == 0 or i_iter == max_iter - 1: 241 | # Store Generated 242 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 243 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 244 | genmix_imgs = drawblock(genmix_imgs, 10) 245 | imsave(os.path.join(gen_dir, '%i.jpg' % i_iter), genmix_imgs) 246 | # Store Generated 96 247 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 248 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 249 | genmix_imgs = drawblock(genmix_imgs, 10) 250 | imsave(os.path.join(gen_dir128, '%i.jpg' % i_iter), genmix_imgs) 251 | # Store Real 252 | real_imgs = (np.transpose(batch_x, [0, 2, 3, 1]) + 1.) * 127.5 253 | real_imgs = np.uint8(real_imgs[:, :, :, ::-1]) 254 | real_imgs = drawblock(real_imgs, 10) 255 | imsave(os.path.join(real_dir, '%i.jpg' % i_iter), real_imgs) 256 | # Store model 257 | if i_iter % save_iter == 0 or i_iter == max_iter - 1 or i_iter == max_iter: 258 | save_path = saver.save(sess, dir_name + '/cdgan%i.ckpt' % i_iter) 259 | coord.request_stop() 260 | coord.join(threads) 261 | -------------------------------------------------------------------------------- /Artist128GANAE.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, flatten, nnupsampling, batchnorm, gaussnoise, pool 3 | from activations import lrelu 4 | from op import log_sum_exp 5 | from data_loader import train_loader, validation_loader 6 | from neon.backends import gen_backend 7 | import numpy as np 8 | from utils import drawblock, createfolders, OneHot, image_reshape 9 | from scipy.misc import imsave 10 | import os 11 | 12 | 13 | # Create folders to store images 14 | gen_dir, real_dir, gen_dir128 = createfolders("./genimgs/Artist128GANAE", "/gen", "/real", "/gen128") 15 | # Create folder to store models 16 | dir_name = './models/Artist128GANAE' 17 | if not os.path.exists(dir_name): 18 | os.mkdir(dir_name) 19 | 20 | # Parameters 21 | init_iter, max_iter = 0, 50000 22 | display_iter = 100 23 | eval_iter = 100 24 | store_img_iter = 100 25 | save_iter = 1000 26 | 27 | lr_init = 0.0002 28 | batch_size = 100 29 | zdim = 100 30 | n_classes = 23 31 | dropout = 0.2 32 | im_size = [64, 64] 33 | dname, gname = 'd_', 'g_' 34 | tf.set_random_seed(1234) 35 | 36 | # DataLoader 37 | be = gen_backend(backend='cpu', batch_size=batch_size, datatype=np.float32) 38 | root_files = './dataset/wikiart' 39 | manifestfile = os.path.join(root_files, 'artist-train-index.csv') 40 | testmanifest = os.path.join(root_files, 'artist-val-index.csv') 41 | train = train_loader(manifestfile, root_files, be, h=im_size[0], w=im_size[1]) 42 | test = validation_loader(testmanifest, root_files, be, h=im_size[0], w=im_size[1], ncls=n_classes) 43 | OneHot = OneHot(be, n_classes) 44 | 45 | # Graph input 46 | is_train = tf.placeholder(tf.bool) 47 | keep_prob = tf.placeholder(tf.float32) 48 | x_n = tf.placeholder(tf.float32, [batch_size, 3, im_size[0], im_size[1]]) 49 | y = tf.placeholder(tf.float32, [batch_size, n_classes]) 50 | lr_tf = tf.placeholder(tf.float32) 51 | z = tf.random_uniform([batch_size, zdim], -1, 1) 52 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 53 | 54 | 55 | # Discriminator 56 | def discriminator(inp, reuse=False): 57 | with tf.variable_scope('Encoder', reuse=reuse): 58 | # 64 59 | inp = gaussnoise(inp, std=0.05) 60 | conv1 = conv2d(inp, 128, kernel=3, strides=2, name=dname + 'conv1') 61 | conv1 = lrelu(conv1, 0.2) 62 | # 32 63 | conv2 = tf.nn.dropout(conv1, keep_prob) 64 | conv2 = conv2d(conv2, 256, kernel=3, strides=2, name=dname + 'conv2') 65 | conv2 = batchnorm(conv2, is_training=is_train, name=dname + 'bn2') 66 | conv2 = lrelu(conv2, 0.2) 67 | # 16 68 | conv3 = tf.nn.dropout(conv2, keep_prob) 69 | conv3 = conv2d(conv3, 512, kernel=3, strides=2, name=dname + 'conv3') 70 | conv3 = batchnorm(conv3, is_training=is_train, name=dname + 'bn3') 71 | conv3 = lrelu(conv3, 0.2) 72 | # 8 73 | conv3b = conv2d(conv3, 512, kernel=3, strides=1, name=dname + 'conv3b') 74 | conv3b = batchnorm(conv3b, is_training=is_train, name=dname + 'bn3b') 75 | conv3b = lrelu(conv3b, 0.2) 76 | 77 | conv4 = tf.nn.dropout(conv3b, keep_prob) 78 | conv4 = conv2d(conv4, 1024, kernel=3, strides=2, name=dname + 'conv4') 79 | conv4 = batchnorm(conv4, is_training=is_train, name=dname + 'bn4') 80 | conv4 = lrelu(conv4, 0.2) 81 | # 4 82 | flat = flatten(conv4) 83 | # Classifier 84 | clspred = linear(flat, n_classes, name=dname + 'cpred') 85 | # Decoder 86 | g1 = conv2d(conv4, nout=512, kernel=3, name=dname + 'deconv1') 87 | g1 = batchnorm(g1, is_training=tf.constant(True), name=dname + 'bn1g') 88 | g1 = lrelu(g1, 0.2) 89 | 90 | g2 = nnupsampling(g1, [8, 8]) 91 | g2 = conv2d(g2, nout=256, kernel=3, name=dname + 'deconv2') 92 | g2 = batchnorm(g2, is_training=tf.constant(True), name=dname + 'bn2g') 93 | g2 = lrelu(g2, 0.2) 94 | 95 | g3 = nnupsampling(g2, [16, 16]) 96 | g3 = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3') 97 | g3 = batchnorm(g3, is_training=tf.constant(True), name=dname + 'bn3g') 98 | g3 = lrelu(g3, 0.2) 99 | 100 | g4 = nnupsampling(g3, [32, 32]) 101 | g4 = conv2d(g4, nout=64, kernel=3, name=dname + 'deconv4') 102 | g4 = batchnorm(g4, is_training=tf.constant(True), name=dname + 'bn4g') 103 | g4 = lrelu(g4, 0.2) 104 | 105 | g5 = nnupsampling(g4, [64, 64]) 106 | g5 = conv2d(g5, nout=32, kernel=3, name=dname + 'deconv5') 107 | g5 = batchnorm(g5, is_training=tf.constant(True), name=dname + 'bn5g') 108 | g5 = lrelu(g5, 0.2) 109 | 110 | g5b = conv2d(g5, nout=3, kernel=3, name=dname + 'deconv5b') 111 | g5b = tf.nn.tanh(g5b) 112 | return clspred, g5b 113 | 114 | 115 | # Generator 116 | def generator(inp_z, inp_y, reuse=False): 117 | with tf.variable_scope('Generator', reuse=reuse): 118 | inp = tf.concat([inp_z, inp_y], 1) 119 | sz = 4 120 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 121 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 122 | g1 = lrelu(g1, 0.2) 123 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 124 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 125 | 126 | g2 = nnupsampling(g1_reshaped, [8, 8]) 127 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 128 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 129 | g2 = lrelu(g2, 0.2) 130 | 131 | g3 = nnupsampling(g2, [16, 16]) 132 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 133 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 134 | g3 = lrelu(g3, 0.2) 135 | 136 | g4 = nnupsampling(g3, [32, 32]) 137 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 138 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 139 | g4 = lrelu(g4, 0.2) 140 | 141 | g5 = nnupsampling(g4, [64, 64]) 142 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 143 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 144 | g5 = lrelu(g5, 0.2) 145 | 146 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 147 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 148 | g5b = lrelu(g5b, 0.2) 149 | 150 | g6 = nnupsampling(g5b, [128, 128]) 151 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 152 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 153 | g6 = lrelu(g6, 0.2) 154 | 155 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 156 | g6b = tf.nn.tanh(g6b) 157 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 158 | return g6b_64, g6b 159 | 160 | # Call functions 161 | Opred_n, recon_n = discriminator(x_n) 162 | samples, samples128 = generator(z, iny) 163 | Opred_g, recon_g = discriminator(samples, reuse=True) 164 | 165 | # Get trainable variables and split 166 | t_vars = tf.trainable_variables() 167 | d_vars = [var for var in t_vars if dname in var.name] 168 | g_vars = [var for var in t_vars if gname in var.name] 169 | print [var.name for var in d_vars] 170 | print [var.name for var in g_vars] 171 | 172 | # Define D loss 173 | lreal = log_sum_exp(Opred_n) 174 | lfake = log_sum_exp(Opred_g) 175 | cost_On = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_n, labels=y)) 176 | cost_Dn = - tf.reduce_mean(lreal) + tf.reduce_mean(tf.nn.softplus(lreal)) 177 | cost_Dg_fake = tf.reduce_mean(tf.nn.softplus(lfake)) 178 | cost_msen = tf.reduce_mean(tf.square(recon_n - x_n)) * 0.5 179 | cost_mseg = tf.reduce_mean(tf.square(recon_g - samples)) * 0.5 180 | D_loss = cost_On + cost_Dn + cost_Dg_fake + cost_msen 181 | # Define G loss 182 | cost_Dg = - tf.reduce_mean(lfake) + tf.reduce_mean(tf.nn.softplus(lfake)) 183 | cost_Og = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_g, labels=iny)) 184 | G_loss = cost_Dg + cost_Og + cost_mseg 185 | 186 | # Define optimizer 187 | d_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(D_loss, var_list=d_vars) 188 | g_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(G_loss, var_list=g_vars) 189 | 190 | # Evaluate model 191 | Oaccuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Opred_n, 1), tf.argmax(y, 1)), tf.float32)) 192 | 193 | # Initialize the variables 194 | init = tf.global_variables_initializer() 195 | # Reset train dataset 196 | train.reset() 197 | # Config for session 198 | config = tf.ConfigProto() 199 | config.gpu_options.allow_growth = True 200 | # Train 201 | with tf.Session(config=config) as sess: 202 | sess.run(init) 203 | saver = tf.train.Saver(max_to_keep=None) 204 | coord = tf.train.Coordinator() 205 | threads = tf.train.start_queue_runners(coord=coord) 206 | for i_iter in range(init_iter, max_iter): 207 | # Control lr 208 | if i_iter < 30000: 209 | lr = lr_init 210 | else: 211 | lr = lr_init / 10. 212 | # Fetch minibatch 213 | batch_x, batch_y = train.next() 214 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 215 | batch_y = OneHot.transform(batch_y).get().transpose() 216 | # update discriminator 217 | _, lossDn, lossOn, lossFake = sess.run([d_optimizer, cost_Dn, cost_On, cost_Dg_fake], feed_dict={ 218 | x_n: batch_x, y: batch_y, 219 | keep_prob: 1. - dropout, is_train: True, lr_tf: lr 220 | }) 221 | # update generator 222 | _, gen_img, gen_img128 = sess.run([g_optimizer, samples, samples128], feed_dict={ 223 | keep_prob: 1., is_train: True, lr_tf: lr 224 | }) 225 | # print losses 226 | if i_iter % display_iter == 0 or i_iter == max_iter - 1: 227 | print 'Iteration: %i, lossDn: %.2f, lossOn: %.2f, lossFake: %.2f' % (i_iter, lossDn, lossOn, lossFake) 228 | # Evaluate classification accuracy 229 | if i_iter % eval_iter == 0 or i_iter == max_iter - 1: 230 | total_Oaccuracy = 0. 231 | test.reset() 232 | for mb_idx, (batch_x, batch_y) in enumerate(test): 233 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 234 | batch_y = batch_y.get().transpose() 235 | total_Oaccuracy += sess.run(Oaccuracy, 236 | feed_dict={x_n: batch_x, y: batch_y, keep_prob: 1., is_train: False}) 237 | print 'Iteration %i, Accuracy: %.2f' % (i_iter, total_Oaccuracy / mb_idx) 238 | # Store images 239 | if i_iter % store_img_iter == 0 or i_iter == max_iter - 1: 240 | # Store Generated 241 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 242 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 243 | genmix_imgs = drawblock(genmix_imgs, 10) 244 | imsave(os.path.join(gen_dir, '%i.jpg' % i_iter), genmix_imgs) 245 | # Store Generated 96 246 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 247 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 248 | genmix_imgs = drawblock(genmix_imgs, 10) 249 | imsave(os.path.join(gen_dir128, '%i.jpg' % i_iter), genmix_imgs) 250 | # Store Real 251 | real_imgs = (np.transpose(batch_x, [0, 2, 3, 1]) + 1.) * 127.5 252 | real_imgs = np.uint8(real_imgs[:, :, :, ::-1]) 253 | real_imgs = drawblock(real_imgs, 10) 254 | imsave(os.path.join(real_dir, '%i.jpg' % i_iter), real_imgs) 255 | # Store model 256 | if i_iter % save_iter == 0 or i_iter == max_iter - 1 or i_iter == max_iter: 257 | save_path = saver.save(sess, dir_name + '/cdgan%i.ckpt' % i_iter) 258 | coord.request_stop() 259 | coord.join(threads) 260 | -------------------------------------------------------------------------------- /Style128GANAE.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, flatten, nnupsampling, batchnorm, gaussnoise, pool 3 | from activations import lrelu 4 | from op import log_sum_exp 5 | from data_loader import train_loader, validation_loader 6 | from neon.backends import gen_backend 7 | import numpy as np 8 | from utils import drawblock, createfolders, OneHot, image_reshape 9 | from scipy.misc import imsave 10 | import os 11 | 12 | 13 | # Create folders to store images 14 | gen_dir, real_dir, gen_dir128 = createfolders("./genimgs/Style128GANAE", "/gen", "/real", "/gen128") 15 | # Create folder to store models 16 | dir_name = './models/Style128GANAE' 17 | if not os.path.exists(dir_name): 18 | os.mkdir(dir_name) 19 | 20 | # Parameters 21 | init_iter, max_iter = 0, 50000 22 | display_iter = 100 23 | eval_iter = 100 24 | store_img_iter = 100 25 | save_iter = 1000 26 | 27 | lr_init, lrg_init = 0.001, 0.0001 28 | batch_size = 100 29 | zdim = 100 30 | n_classes = 27 31 | dropout = 0.2 32 | im_size = [64, 64] 33 | dname, gname = 'd_', 'g_' 34 | tf.set_random_seed(1234) 35 | 36 | # DataLoader 37 | be = gen_backend(backend='cpu', batch_size=batch_size, datatype=np.float32) 38 | root_files = './dataset/wikiart' 39 | manifestfile = os.path.join(root_files, 'style-train-index.csv') 40 | testmanifest = os.path.join(root_files, 'style-val-index.csv') 41 | train = train_loader(manifestfile, root_files, be, h=im_size[0], w=im_size[1]) 42 | test = validation_loader(testmanifest, root_files, be, h=im_size[0], w=im_size[1], ncls=n_classes) 43 | OneHot = OneHot(be, n_classes) 44 | 45 | # Graph input 46 | is_train = tf.placeholder(tf.bool) 47 | keep_prob = tf.placeholder(tf.float32) 48 | x_n = tf.placeholder(tf.float32, [batch_size, 3, im_size[0], im_size[1]]) 49 | y = tf.placeholder(tf.float32, [batch_size, n_classes]) 50 | lr_tf = tf.placeholder(tf.float32) 51 | z = tf.random_uniform([batch_size, zdim], -1, 1) 52 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 53 | 54 | 55 | # Discriminator 56 | def discriminator(inp, reuse=False): 57 | with tf.variable_scope('Encoder', reuse=reuse): 58 | # 64 59 | inp = gaussnoise(inp, std=0.05) 60 | conv1 = conv2d(inp, 128, kernel=3, strides=2, name=dname + 'conv1') 61 | conv1 = lrelu(conv1, 0.2) 62 | # 32 63 | conv2 = tf.nn.dropout(conv1, keep_prob) 64 | conv2 = conv2d(conv2, 256, kernel=3, strides=2, name=dname + 'conv2') 65 | conv2 = batchnorm(conv2, is_training=is_train, name=dname + 'bn2') 66 | conv2 = lrelu(conv2, 0.2) 67 | # 16 68 | conv3 = tf.nn.dropout(conv2, keep_prob) 69 | conv3 = conv2d(conv3, 512, kernel=3, strides=2, name=dname + 'conv3') 70 | conv3 = batchnorm(conv3, is_training=is_train, name=dname + 'bn3') 71 | conv3 = lrelu(conv3, 0.2) 72 | # 8 73 | conv3b = conv2d(conv3, 512, kernel=3, strides=1, name=dname + 'conv3b') 74 | conv3b = batchnorm(conv3b, is_training=is_train, name=dname + 'bn3b') 75 | conv3b = lrelu(conv3b, 0.2) 76 | 77 | conv4 = tf.nn.dropout(conv3b, keep_prob) 78 | conv4 = conv2d(conv4, 1024, kernel=3, strides=2, name=dname + 'conv4') 79 | conv4 = batchnorm(conv4, is_training=is_train, name=dname + 'bn4') 80 | conv4 = lrelu(conv4, 0.2) 81 | # 4 82 | flat = flatten(conv4) 83 | # Classifier 84 | clspred = linear(flat, n_classes, name=dname + 'cpred') 85 | # Decoder 86 | g1 = conv2d(conv4, nout=512, kernel=3, name=dname + 'deconv1') 87 | g1 = batchnorm(g1, is_training=tf.constant(True), name=dname + 'bn1g') 88 | g1 = lrelu(g1, 0.2) 89 | 90 | g2 = nnupsampling(g1, [8, 8]) 91 | g2 = conv2d(g2, nout=256, kernel=3, name=dname + 'deconv2') 92 | g2 = batchnorm(g2, is_training=tf.constant(True), name=dname + 'bn2g') 93 | g2 = lrelu(g2, 0.2) 94 | 95 | g3 = nnupsampling(g2, [16, 16]) 96 | g3 = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3') 97 | g3 = batchnorm(g3, is_training=tf.constant(True), name=dname + 'bn3g') 98 | g3 = lrelu(g3, 0.2) 99 | 100 | g4 = nnupsampling(g3, [32, 32]) 101 | g4 = conv2d(g4, nout=64, kernel=3, name=dname + 'deconv4') 102 | g4 = batchnorm(g4, is_training=tf.constant(True), name=dname + 'bn4g') 103 | g4 = lrelu(g4, 0.2) 104 | 105 | g5 = nnupsampling(g4, [64, 64]) 106 | g5 = conv2d(g5, nout=32, kernel=3, name=dname + 'deconv5') 107 | g5 = batchnorm(g5, is_training=tf.constant(True), name=dname + 'bn5g') 108 | g5 = lrelu(g5, 0.2) 109 | 110 | g5b = conv2d(g5, nout=3, kernel=3, name=dname + 'deconv5b') 111 | g5b = tf.nn.tanh(g5b) 112 | return clspred, g5b, flat 113 | 114 | 115 | # Generator 116 | def generator(inp_z, inp_y, reuse=False): 117 | with tf.variable_scope('Generator', reuse=reuse): 118 | inp = tf.concat([inp_z, inp_y], 1) 119 | sz = 4 120 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 121 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 122 | g1 = lrelu(g1, 0.2) 123 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 124 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 125 | 126 | g2 = nnupsampling(g1_reshaped, [8, 8]) 127 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 128 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 129 | g2 = lrelu(g2, 0.2) 130 | 131 | g3 = nnupsampling(g2, [16, 16]) 132 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 133 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 134 | g3 = lrelu(g3, 0.2) 135 | 136 | g4 = nnupsampling(g3, [32, 32]) 137 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 138 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 139 | g4 = lrelu(g4, 0.2) 140 | 141 | g5 = nnupsampling(g4, [64, 64]) 142 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 143 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 144 | g5 = lrelu(g5, 0.2) 145 | 146 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 147 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 148 | g5b = lrelu(g5b, 0.2) 149 | 150 | g6 = nnupsampling(g5b, [128, 128]) 151 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 152 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 153 | g6 = lrelu(g6, 0.2) 154 | 155 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 156 | g6b = tf.nn.tanh(g6b) 157 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 158 | return g6b_64, g6b 159 | 160 | # Call functions 161 | Opred_n, recon_n, _ = discriminator(x_n) 162 | samples, samples128 = generator(z, iny) 163 | Opred_g, recon_g, embed = discriminator(samples, reuse=True) 164 | 165 | # Get trainable variables and split 166 | t_vars = tf.trainable_variables() 167 | d_vars = [var for var in t_vars if dname in var.name] 168 | g_vars = [var for var in t_vars if gname in var.name] 169 | print [var.name for var in d_vars] 170 | print [var.name for var in g_vars] 171 | 172 | # Define D loss 173 | lreal = log_sum_exp(Opred_n) 174 | lfake = log_sum_exp(Opred_g) 175 | cost_On = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_n, labels=y)) 176 | cost_Dn = - tf.reduce_mean(lreal) + tf.reduce_mean(tf.nn.softplus(lreal)) 177 | cost_Dg_fake = tf.reduce_mean(tf.nn.softplus(lfake)) 178 | cost_msen = tf.reduce_mean(tf.square(recon_n - x_n)) * 0.5 179 | cost_mseg = tf.reduce_mean(tf.square(recon_g - samples)) * 0.5 180 | D_loss = cost_On + cost_Dn + cost_Dg_fake + cost_msen 181 | # Define G loss 182 | cost_Dg = - tf.reduce_mean(lfake) + tf.reduce_mean(tf.nn.softplus(lfake)) 183 | cost_Og = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_g, labels=iny)) 184 | G_loss = cost_Dg + cost_Og + cost_mseg # + pullaway_loss(embed)*0.1 185 | 186 | # Define optimizer 187 | d_optimizer = tf.train.RMSPropOptimizer(learning_rate=lr_tf).minimize(D_loss, var_list=d_vars) 188 | g_optimizer = tf.train.RMSPropOptimizer(learning_rate=lr_tf).minimize(G_loss, var_list=g_vars) 189 | 190 | # Evaluate model 191 | Oaccuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Opred_n, 1), tf.argmax(y, 1)), tf.float32)) 192 | 193 | # Initialize the variables 194 | init = tf.global_variables_initializer() 195 | # Reset train dataset 196 | train.reset() 197 | # Config for session 198 | config = tf.ConfigProto() 199 | config.gpu_options.allow_growth = True 200 | # Train 201 | with tf.Session(config=config) as sess: 202 | sess.run(init) 203 | saver = tf.train.Saver(max_to_keep=None) 204 | coord = tf.train.Coordinator() 205 | threads = tf.train.start_queue_runners(coord=coord) 206 | for i_iter in range(init_iter, max_iter): 207 | # Control lr 208 | if i_iter < 30000: 209 | lr = lr_init 210 | lrg = lrg_init 211 | else: 212 | lr = lr_init / 10. 213 | lrg = lrg_init / 10. 214 | # Fetch minibatch 215 | batch_x, batch_y = train.next() 216 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 217 | batch_y = OneHot.transform(batch_y).get().transpose() 218 | # update discriminator 219 | _, lossDn, lossOn, lossFake = sess.run([d_optimizer, cost_Dn, cost_On, cost_Dg_fake], feed_dict={ 220 | x_n: batch_x, y: batch_y, 221 | keep_prob: 1. - dropout, is_train: True, lr_tf: lr 222 | }) 223 | # update generator 224 | _, gen_img, gen_img128 = sess.run([g_optimizer, samples, samples128], feed_dict={ 225 | keep_prob: 1., is_train: True, lr_tf: lrg 226 | }) 227 | # print losses 228 | if i_iter % display_iter == 0 or i_iter == max_iter - 1: 229 | print 'Iteration: %i, lossDn: %.2f, lossOn: %.2f, lossFake: %.2f' % (i_iter, lossDn, lossOn, lossFake) 230 | # Evaluate classification accuracy 231 | if i_iter % eval_iter == 0 or i_iter == max_iter - 1: 232 | total_Oaccuracy = 0. 233 | test.reset() 234 | for mb_idx, (batch_x, batch_y) in enumerate(test): 235 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 236 | batch_y = batch_y.get().transpose() 237 | total_Oaccuracy += sess.run(Oaccuracy, 238 | feed_dict={x_n: batch_x, y: batch_y, keep_prob: 1., is_train: False}) 239 | print 'Iteration %i, Accuracy: %.2f' % (i_iter, total_Oaccuracy / mb_idx) 240 | # Store images 241 | if i_iter % store_img_iter == 0 or i_iter == max_iter - 1: 242 | # Store Generated 243 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 244 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 245 | genmix_imgs = drawblock(genmix_imgs, n_classes, flip=True) 246 | imsave(os.path.join(gen_dir, '%i.jpg' % i_iter), genmix_imgs) 247 | # Store Generated 96 248 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 249 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 250 | genmix_imgs = drawblock(genmix_imgs, n_classes, flip=True) 251 | imsave(os.path.join(gen_dir128, '%i.jpg' % i_iter), genmix_imgs) 252 | # Store Real 253 | real_imgs = (np.transpose(batch_x, [0, 2, 3, 1]) + 1.) * 127.5 254 | real_imgs = np.uint8(real_imgs[:, :, :, ::-1]) 255 | real_imgs = drawblock(real_imgs, 10) 256 | imsave(os.path.join(real_dir, '%i.jpg' % i_iter), real_imgs) 257 | # Store model 258 | if i_iter % save_iter == 0 or i_iter == max_iter - 1 or i_iter == max_iter: 259 | save_path = saver.save(sess, dir_name + '/cdgan%i.ckpt' % i_iter) 260 | coord.request_stop() 261 | coord.join(threads) 262 | -------------------------------------------------------------------------------- /CIFAR64GANAEtrick.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, flatten, nnupsampling, batchnorm, gaussnoise, pool 3 | from activations import lrelu 4 | from op import log_sum_exp 5 | from data_loader import train_loader, validation_loader 6 | from neon.backends import gen_backend 7 | import numpy as np 8 | from utils import drawblock, createfolders, OneHot, image_reshape 9 | from scipy.misc import imsave 10 | import os 11 | 12 | 13 | # Create folders to store images 14 | gen_dir, real_dir, gen_dir64 = createfolders("./genimgs/CIFAR64GANAE", "/gen", "/real", "/gen64") 15 | # Create folder to store models 16 | dir_name = './models/CIFAR64GANAE' 17 | if not os.path.exists(dir_name): 18 | os.mkdir(dir_name) 19 | 20 | # Parameters 21 | init_iter, max_iter = 0, 70000 22 | display_iter = 100 23 | eval_iter = 100 24 | store_img_iter = 100 25 | save_iter = 1000 26 | 27 | lr_init = 0.0002 28 | batch_size = 100 29 | zdim = 100 30 | n_classes = 10 31 | dropout = 0.2 32 | im_size = [32, 32] 33 | dname, gname = 'd_', 'g_' 34 | tf.set_random_seed(1234) 35 | 36 | # DataLoader 37 | be = gen_backend(backend='cpu', batch_size=batch_size, datatype=np.float32) 38 | root_files = './dataset/Cifar10' 39 | manifestfile = os.path.join(root_files, 'train-index.csv') 40 | testmanifest = os.path.join(root_files, 'val-index.csv') 41 | train = train_loader(manifestfile, root_files, be, h=im_size[0], w=im_size[1]) 42 | test = validation_loader(testmanifest, root_files, be, h=im_size[0], w=im_size[1]) 43 | OneHot = OneHot(be, n_classes) 44 | 45 | # Graph input 46 | is_train = tf.placeholder(tf.bool) 47 | keep_prob = tf.placeholder(tf.float32) 48 | x_n = tf.placeholder(tf.float32, [batch_size, 3, im_size[0], im_size[1]]) 49 | y = tf.placeholder(tf.float32, [batch_size, n_classes]) 50 | lr_tf = tf.placeholder(tf.float32) 51 | z = tf.random_uniform([batch_size, zdim], -1, 1) 52 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 53 | 54 | 55 | # Discriminator 56 | def discriminator(inp, reuse=False): 57 | with tf.variable_scope('Encoder', reuse=reuse): 58 | # 32 59 | inp = gaussnoise(inp, std=0.05) 60 | conv1 = conv2d(inp, 96, kernel=3, strides=1, name=dname + 'conv1') 61 | conv1 = lrelu(conv1, 0.2) 62 | 63 | conv1b = conv2d(conv1, 96, kernel=3, strides=2, name=dname + 'conv1b') 64 | conv1b = batchnorm(conv1b, is_training=is_train, name=dname + 'bn1b') 65 | conv1b = lrelu(conv1b, 0.2) 66 | conv1b = tf.nn.dropout(conv1b, keep_prob) 67 | # 16 68 | conv2 = conv2d(conv1b, 192, kernel=3, strides=1, name=dname + 'conv2') 69 | conv2 = batchnorm(conv2, is_training=is_train, name=dname + 'bn2') 70 | conv2 = lrelu(conv2, 0.2) 71 | 72 | conv2b = conv2d(conv2, 192, kernel=3, strides=2, name=dname + 'conv2b') 73 | conv2b = batchnorm(conv2b, is_training=is_train, name=dname + 'bn2b') 74 | conv2b = lrelu(conv2b, 0.2) 75 | conv2b = tf.nn.dropout(conv2b, keep_prob) 76 | # 8 77 | conv3 = conv2d(conv2b, 256, kernel=3, strides=1, name=dname + 'conv3') 78 | conv3 = batchnorm(conv3, is_training=is_train, name=dname + 'bn3') 79 | conv3 = lrelu(conv3, 0.2) 80 | 81 | conv3b = conv2d(conv3, 256, kernel=1, strides=1, name=dname + 'conv3b') 82 | conv3b = batchnorm(conv3b, is_training=is_train, name=dname + 'bn3b') 83 | conv3b = lrelu(conv3b, 0.2) 84 | 85 | conv4 = conv2d(conv3b, 512, kernel=1, strides=1, name=dname + 'conv4') 86 | conv4 = batchnorm(conv4, is_training=is_train, name=dname + 'bn4') 87 | conv4 = lrelu(conv4, 0.2) 88 | 89 | flat = flatten(conv4) 90 | # Classifier 91 | clspred = linear(flat, n_classes, name=dname + 'cpred') 92 | # Decoder 93 | g2 = conv2d(conv4, nout=256, kernel=3, name=dname + 'deconv2') 94 | g2 = batchnorm(g2, is_training=tf.constant(True), name=dname + 'bn2g') 95 | g2 = lrelu(g2, 0.2) 96 | 97 | g3 = nnupsampling(g2, [16, 16]) 98 | g3 = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3') 99 | g3 = batchnorm(g3, is_training=tf.constant(True), name=dname + 'bn3g') 100 | g3 = lrelu(g3, 0.2) 101 | 102 | g3b = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3b') 103 | g3b = batchnorm(g3b, is_training=tf.constant(True), name=dname + 'bn3bg') 104 | g3b = lrelu(g3b, 0.2) 105 | 106 | g4 = nnupsampling(g3b, [32, 32]) 107 | g4 = conv2d(g4, nout=64, kernel=3, name=dname + 'deconv4') 108 | g4 = batchnorm(g4, is_training=tf.constant(True), name=dname + 'bn4g') 109 | g4 = lrelu(g4, 0.2) 110 | 111 | g4b = conv2d(g4, nout=3, kernel=3, name=dname + 'deconv4b') 112 | g4b = tf.nn.tanh(g4b) 113 | return clspred, g4b 114 | 115 | 116 | # Generator 117 | def generator(inp_z, inp_y): 118 | with tf.variable_scope('Generator'): 119 | inp = tf.concat([inp_z, inp_y], 1) 120 | 121 | g1 = linear(inp, 512*4*4, name=gname+'deconv1') 122 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 123 | g1 = lrelu(g1, 0.2) 124 | g1_reshaped = tf.reshape(g1, [-1, 512, 4, 4]) 125 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 126 | 127 | g2 = nnupsampling(g1_reshaped, [8, 8]) 128 | g2 = conv2d(g2, nout=256, kernel=3, name=gname+'deconv2') 129 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 130 | g2 = lrelu(g2, 0.2) 131 | 132 | g3 = nnupsampling(g2, [16, 16]) 133 | g3 = conv2d(g3, nout=128, kernel=3, name=gname+'deconv3') 134 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 135 | g3 = lrelu(g3, 0.2) 136 | 137 | g3b = conv2d(g3, nout=128, kernel=3, name=gname + 'deconv3b') 138 | g3b = batchnorm(g3b, is_training=tf.constant(True), name=gname + 'bn3bg') 139 | g3b = lrelu(g3b, 0.2) 140 | 141 | g4 = nnupsampling(g3b, [32, 32]) 142 | g4 = conv2d(g4, nout=64, kernel=3, name=gname + 'deconv4') 143 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 144 | g4 = lrelu(g4, 0.2) 145 | 146 | g4b = conv2d(g4, nout=64, kernel=3, name=gname + 'deconv4b') 147 | g4b = batchnorm(g4b, is_training=tf.constant(True), name=gname + 'bn4bg') 148 | g4b = lrelu(g4b, 0.2) 149 | 150 | g5 = nnupsampling(g4b, [64, 64]) 151 | g5 = conv2d(g5, nout=32, kernel=3, name=gname + 'deconv5') 152 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 153 | g5 = lrelu(g5, 0.2) 154 | 155 | g5b = conv2d(g5, nout=3, kernel=3, name=gname + 'deconv5b') 156 | g5b = tf.nn.tanh(g5b) 157 | g5b_32 = pool(g5b, fsize=3, strides=2, op='avg', pad='SAME') 158 | return g5b_32, g5b 159 | 160 | # Call functions 161 | Opred_n, recon_n = discriminator(x_n) 162 | samples, samples64 = generator(z, iny) 163 | Opred_g, recon_g = discriminator(samples, reuse=True) 164 | 165 | # Get trainable variables and split 166 | t_vars = tf.trainable_variables() 167 | d_vars = [var for var in t_vars if dname in var.name] 168 | g_vars = [var for var in t_vars if gname in var.name] 169 | print [var.name for var in d_vars] 170 | print [var.name for var in g_vars] 171 | 172 | # Define D loss 173 | lreal = log_sum_exp(Opred_n) 174 | lfake = log_sum_exp(Opred_g) 175 | cost_On = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_n, labels=y)) 176 | cost_Dn = - tf.reduce_mean(lreal) + tf.reduce_mean(tf.nn.softplus(lreal)) 177 | cost_Dg_fake = tf.reduce_mean(tf.nn.softplus(lfake)) 178 | cost_msen = tf.reduce_mean(tf.square(recon_n - x_n)) * 0.5 179 | cost_mseg = tf.reduce_mean(tf.square(recon_g - samples)) * 0.5 180 | D_loss = cost_On + cost_Dn + cost_Dg_fake + cost_msen 181 | # Define G loss 182 | cost_Dg = - tf.reduce_mean(lfake) + tf.reduce_mean(tf.nn.softplus(lfake)) 183 | cost_Og = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_g, labels=iny)) 184 | G_loss = cost_Dg + cost_Og + cost_mseg 185 | 186 | # Define optimizer 187 | d_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(D_loss, var_list=d_vars) 188 | g_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(G_loss, var_list=g_vars) 189 | 190 | # Evaluate model 191 | Oaccuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Opred_n, 1), tf.argmax(y, 1)), tf.float32)) 192 | 193 | # Initialize the variables 194 | init = tf.global_variables_initializer() 195 | # Reset train dataset 196 | train.reset() 197 | # Config for session 198 | config = tf.ConfigProto() 199 | config.gpu_options.allow_growth = True 200 | # Train 201 | with tf.Session(config=config) as sess: 202 | sess.run(init) 203 | saver = tf.train.Saver(max_to_keep=None) 204 | for i_iter in range(init_iter, max_iter): 205 | # Control lr 206 | if i_iter < 30000: 207 | lr = lr_init 208 | else: 209 | lr = lr_init / 10. 210 | # Fetch minibatch 211 | batch_x, batch_y = train.next() 212 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 213 | batch_y = OneHot.transform(batch_y).get().transpose() 214 | # update discriminator 215 | _, lossDn, lossOn, lossFake = sess.run([d_optimizer, cost_Dn, cost_On, cost_Dg_fake], feed_dict={ 216 | x_n: batch_x, y: batch_y, 217 | keep_prob: 1. - dropout, is_train: True, lr_tf: lr 218 | }) 219 | # update generator 220 | total_loss = (lossDn + lossFake) * 0.5 221 | if total_loss > 0.67: 222 | gen_iter = 1 223 | elif total_loss > 0.3: 224 | gen_iter = 3 225 | else: 226 | gen_iter = 5 227 | # gen_iter = 1 228 | for _ in xrange(gen_iter): 229 | _, gen_img, gen_img64 = sess.run([g_optimizer, samples, samples64], feed_dict={ 230 | keep_prob: 1., is_train: True, lr_tf: lr 231 | }) 232 | # print losses 233 | if i_iter % display_iter == 0 or i_iter == max_iter - 1: 234 | print 'Iteration: %i, lossDn: %.2f, lossOn: %.2f, lossFake: %.2f' % (i_iter, lossDn, lossOn, lossFake) 235 | # Evaluate classification accuracy 236 | if i_iter % eval_iter == 0 or i_iter == max_iter - 1: 237 | total_Oaccuracy = 0. 238 | test.reset() 239 | for mb_idx, (batch_x, batch_y) in enumerate(test): 240 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 241 | batch_y = batch_y.get().transpose() 242 | total_Oaccuracy += sess.run(Oaccuracy, 243 | feed_dict={x_n: batch_x, y: batch_y, keep_prob: 1., is_train: False}) 244 | print 'Iteration %i, Accuracy: %.2f' % (i_iter, total_Oaccuracy / mb_idx) 245 | # Store images 246 | if i_iter % store_img_iter == 0 or i_iter == max_iter - 1: 247 | # Store Generated 248 | genmix_imgs = sess.run(samples) 249 | genmix_imgs = (np.transpose(genmix_imgs, [0, 2, 3, 1]) + 1.) * 127.5 250 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 251 | genmix_imgs = drawblock(genmix_imgs, 10) 252 | imsave(os.path.join(gen_dir, '%i.jpg' % i_iter), genmix_imgs) 253 | # Store Generated 64x64 254 | genmix_imgs64 = (np.transpose(gen_img64, [0, 2, 3, 1]) + 1.) * 127.5 255 | genmix_imgs64 = np.uint8(genmix_imgs64[:, :, :, ::-1]) 256 | genmix_imgs64 = drawblock(genmix_imgs64, 10) 257 | imsave(os.path.join(gen_dir64, '%i.jpg' % i_iter), genmix_imgs64) 258 | # Store Real 259 | real_imgs = (np.transpose(batch_x, [0, 2, 3, 1]) + 1.) * 127.5 260 | real_imgs = np.uint8(real_imgs[:, :, :, ::-1]) 261 | real_imgs = drawblock(real_imgs, 10) 262 | imsave(os.path.join(real_dir, '%i.jpg' % i_iter), real_imgs) 263 | # Store model 264 | if i_iter % save_iter == 0 or i_iter == max_iter - 1 or i_iter == max_iter: 265 | save_path = saver.save(sess, dir_name + '/cdgan%i.ckpt' % i_iter) 266 | -------------------------------------------------------------------------------- /Flower128GANAE.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, flatten, nnupsampling, batchnorm, gaussnoise, pool 3 | from activations import lrelu 4 | from op import log_sum_exp 5 | from data_loader import train_loader, validation_loader 6 | from neon.backends import gen_backend 7 | import numpy as np 8 | from utils import drawblock, createfolders, OneHot, image_reshape 9 | from scipy.misc import imsave 10 | import os 11 | 12 | 13 | # Create folders to store images 14 | gen_dir, real_dir, gen_dir128 = createfolders("./genimgs/Flower128GANAE", "/gen", "/real", "/gen128") 15 | # Create folder to store models 16 | dir_name = './models/Flower128GANAE' 17 | if not os.path.exists(dir_name): 18 | os.mkdir(dir_name) 19 | 20 | # Parameters 21 | init_iter, max_iter = 0, 30000 22 | display_iter = 100 23 | eval_iter = 100 24 | store_img_iter = 100 25 | save_iter = 1000 26 | 27 | lr_init = 0.0002 28 | batch_size = 100 29 | zdim = 100 30 | n_classes = 102 31 | dropout = 0.2 32 | im_size = [64, 64] 33 | dname, gname = 'd_', 'g_' 34 | tf.set_random_seed(1234) 35 | 36 | # DataLoader 37 | be = gen_backend(backend='cpu', batch_size=batch_size, datatype=np.float32) 38 | root_files = './dataset/flower102' 39 | manifestfile = os.path.join(root_files, 'train-index.csv') 40 | testmanifest = os.path.join(root_files, 'val-index.csv') 41 | train = train_loader(manifestfile, root_files, be, h=im_size[0], w=im_size[1], scale=[0.875, 0.875]) 42 | test = validation_loader(testmanifest, root_files, be, h=im_size[0], w=im_size[1], scale=[0.875, 0.875], ncls=n_classes) 43 | OneHot = OneHot(be, n_classes) 44 | 45 | # Graph input 46 | is_train = tf.placeholder(tf.bool) 47 | keep_prob = tf.placeholder(tf.float32) 48 | x_n = tf.placeholder(tf.float32, [batch_size, 3, im_size[0], im_size[1]]) 49 | y = tf.placeholder(tf.float32, [batch_size, n_classes]) 50 | lr_tf = tf.placeholder(tf.float32) 51 | z = tf.random_uniform([batch_size, zdim], -1, 1) 52 | iny = tf.placeholder(tf.float32, [batch_size, n_classes]) 53 | 54 | 55 | # Discriminator 56 | def discriminator(inp, reuse=False): 57 | with tf.variable_scope('Encoder', reuse=reuse): 58 | # 64 59 | inp = gaussnoise(inp, std=0.05) 60 | conv1 = conv2d(inp, 64, kernel=3, strides=2, name=dname + 'conv1') 61 | conv1 = lrelu(conv1, 0.2) 62 | # 32 63 | conv2 = tf.nn.dropout(conv1, keep_prob) 64 | conv2 = conv2d(conv2, 128, kernel=3, strides=2, name=dname + 'conv2') 65 | conv2 = batchnorm(conv2, is_training=is_train, name=dname + 'bn2') 66 | conv2 = lrelu(conv2, 0.2) 67 | # 16 68 | conv3 = tf.nn.dropout(conv2, keep_prob) 69 | conv3 = conv2d(conv3, 256, kernel=3, strides=2, name=dname + 'conv3') 70 | conv3 = batchnorm(conv3, is_training=is_train, name=dname + 'bn3') 71 | conv3 = lrelu(conv3, 0.2) 72 | # 8 73 | conv3b = conv2d(conv3, 256, kernel=3, strides=1, name=dname + 'conv3b') 74 | conv3b = batchnorm(conv3b, is_training=is_train, name=dname + 'bn3b') 75 | conv3b = lrelu(conv3b, 0.2) 76 | 77 | conv4 = tf.nn.dropout(conv3b, keep_prob) 78 | conv4 = conv2d(conv4, 512, kernel=3, strides=2, name=dname + 'conv4') 79 | conv4 = batchnorm(conv4, is_training=is_train, name=dname + 'bn4') 80 | conv4 = lrelu(conv4, 0.2) 81 | # 4 82 | conv4b = conv2d(conv4, 512, kernel=3, strides=1, name=dname + 'conv4b') 83 | conv4b = batchnorm(conv4b, is_training=is_train, name=dname + 'bn4b') 84 | conv4b = lrelu(conv4b, 0.2) 85 | 86 | flat = flatten(conv4b) 87 | # Classifier 88 | clspred = linear(flat, n_classes, name=dname + 'cpred') 89 | # Decoder 90 | g1 = conv2d(conv4b, nout=512, kernel=3, name=dname + 'deconv1') 91 | g1 = batchnorm(g1, is_training=tf.constant(True), name=dname + 'bn1g') 92 | g1 = lrelu(g1, 0.2) 93 | 94 | g2 = nnupsampling(g1, [8, 8]) 95 | g2 = conv2d(g2, nout=256, kernel=3, name=dname + 'deconv2') 96 | g2 = batchnorm(g2, is_training=tf.constant(True), name=dname + 'bn2g') 97 | g2 = lrelu(g2, 0.2) 98 | 99 | g3 = nnupsampling(g2, [16, 16]) 100 | g3 = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3') 101 | g3 = batchnorm(g3, is_training=tf.constant(True), name=dname + 'bn3g') 102 | g3 = lrelu(g3, 0.2) 103 | 104 | g4 = nnupsampling(g3, [32, 32]) 105 | g4 = conv2d(g4, nout=64, kernel=3, name=dname + 'deconv4') 106 | g4 = batchnorm(g4, is_training=tf.constant(True), name=dname + 'bn4g') 107 | g4 = lrelu(g4, 0.2) 108 | 109 | g5 = nnupsampling(g4, [64, 64]) 110 | g5 = conv2d(g5, nout=32, kernel=3, name=dname + 'deconv5') 111 | g5 = batchnorm(g5, is_training=tf.constant(True), name=dname + 'bn5g') 112 | g5 = lrelu(g5, 0.2) 113 | 114 | g5b = conv2d(g5, nout=3, kernel=3, name=dname + 'deconv5b') 115 | g5b = tf.nn.tanh(g5b) 116 | return clspred, g5b, flat 117 | 118 | 119 | # Generator 120 | def generator(inp_z, inp_y, reuse=False): 121 | with tf.variable_scope('Generator', reuse=reuse): 122 | inp = tf.concat([inp_z, inp_y], 1) 123 | sz = 4 124 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 125 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 126 | g1 = lrelu(g1, 0.2) 127 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 128 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 129 | 130 | g2 = nnupsampling(g1_reshaped, [8, 8]) 131 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 132 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 133 | g2 = lrelu(g2, 0.2) 134 | 135 | g3 = nnupsampling(g2, [16, 16]) 136 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 137 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 138 | g3 = lrelu(g3, 0.2) 139 | 140 | g4 = nnupsampling(g3, [32, 32]) 141 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 142 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 143 | g4 = lrelu(g4, 0.2) 144 | 145 | g5 = nnupsampling(g4, [64, 64]) 146 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 147 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 148 | g5 = lrelu(g5, 0.2) 149 | 150 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 151 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 152 | g5b = lrelu(g5b, 0.2) 153 | 154 | g6 = nnupsampling(g5b, [128, 128]) 155 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 156 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 157 | g6 = lrelu(g6, 0.2) 158 | 159 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 160 | g6b = tf.nn.tanh(g6b) 161 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 162 | return g6b_64, g6b 163 | 164 | # Call functions 165 | Opred_n, recon_n, _ = discriminator(x_n) 166 | samples, samples128 = generator(z, iny) 167 | Opred_g, recon_g, embed = discriminator(samples, reuse=True) 168 | 169 | # Get trainable variables and split 170 | t_vars = tf.trainable_variables() 171 | d_vars = [var for var in t_vars if dname in var.name] 172 | g_vars = [var for var in t_vars if gname in var.name] 173 | print [var.name for var in d_vars] 174 | print [var.name for var in g_vars] 175 | 176 | # Define D loss 177 | lreal = log_sum_exp(Opred_n) 178 | lfake = log_sum_exp(Opred_g) 179 | cost_On = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_n, labels=y)) 180 | cost_Dn = - tf.reduce_mean(lreal) + tf.reduce_mean(tf.nn.softplus(lreal)) 181 | cost_Dg_fake = tf.reduce_mean(tf.nn.softplus(lfake)) 182 | cost_msen = tf.reduce_mean(tf.square(recon_n - x_n)) * 0.5 183 | cost_mseg = tf.reduce_mean(tf.square(recon_g - samples)) * 0.5 184 | D_loss = cost_On + cost_Dn + cost_Dg_fake + cost_msen 185 | # Define G loss 186 | cost_Dg = - tf.reduce_mean(lfake) + tf.reduce_mean(tf.nn.softplus(lfake)) 187 | cost_Og = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_g, labels=iny)) 188 | G_loss = cost_Dg + cost_Og + cost_mseg 189 | 190 | # Define optimizer 191 | d_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(D_loss, var_list=d_vars) 192 | g_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(G_loss, var_list=g_vars) 193 | 194 | # Evaluate model 195 | Oaccuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Opred_n, 1), tf.argmax(y, 1)), tf.float32)) 196 | 197 | # Initialize the variables 198 | init = tf.global_variables_initializer() 199 | # Reset train dataset 200 | train.reset() 201 | # Config for session 202 | config = tf.ConfigProto() 203 | config.gpu_options.allow_growth = True 204 | # Train 205 | with tf.Session(config=config) as sess: 206 | sess.run(init) 207 | saver = tf.train.Saver(max_to_keep=None) 208 | for i_iter in range(init_iter, max_iter): 209 | # Control lr 210 | if i_iter < 15000: 211 | lr = lr_init 212 | else: 213 | lr = lr_init / 10. 214 | # Set current fake label 215 | inds = np.repeat(np.random.permutation(n_classes)[:20], 5) 216 | fake_y = np.zeros((batch_size, n_classes)) 217 | fake_y[np.arange(batch_size), inds] = 1. 218 | # Fetch minibatch 219 | batch_x, batch_y = train.next() 220 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 221 | batch_y = OneHot.transform(batch_y).get().transpose() 222 | # update discriminator 223 | _, lossDn, lossOn, lossFake = sess.run([d_optimizer, cost_Dn, cost_On, cost_Dg_fake], feed_dict={ 224 | x_n: batch_x, y: batch_y, iny: fake_y, 225 | keep_prob: 1. - dropout, is_train: True, lr_tf: lr 226 | }) 227 | # update generator 228 | _, gen_img, gen_img128 = sess.run([g_optimizer, samples, samples128], feed_dict={ 229 | iny: fake_y, 230 | keep_prob: 1., is_train: True, lr_tf: lr 231 | }) 232 | # print losses 233 | if i_iter % display_iter == 0 or i_iter == max_iter - 1: 234 | print 'Iteration: %i, lossDn: %.2f, lossOn: %.2f, lossFake: %.2f' % (i_iter, lossDn, lossOn, lossFake) 235 | # Evaluate classification accuracy 236 | if i_iter % eval_iter == 0 or i_iter == max_iter - 1: 237 | total_Oaccuracy = 0. 238 | test.reset() 239 | for mb_idx, (batch_x, batch_y) in enumerate(test): 240 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 241 | batch_y = batch_y.get().transpose() 242 | total_Oaccuracy += sess.run(Oaccuracy, 243 | feed_dict={x_n: batch_x, y: batch_y, keep_prob: 1., is_train: False}) 244 | print 'Iteration %i, Accuracy: %.2f' % (i_iter, total_Oaccuracy / mb_idx) 245 | # Store images 246 | if i_iter % store_img_iter == 0 or i_iter == max_iter - 1: 247 | # Store Generated 248 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 249 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 250 | genmix_imgs = drawblock(genmix_imgs, 10) 251 | imsave(os.path.join(gen_dir, '%i.jpg' % i_iter), genmix_imgs) 252 | # Store Generated 96 253 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 254 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 255 | genmix_imgs = drawblock(genmix_imgs, 10) 256 | imsave(os.path.join(gen_dir128, '%i.jpg' % i_iter), genmix_imgs) 257 | # Store Real 258 | real_imgs = (np.transpose(batch_x, [0, 2, 3, 1]) + 1.) * 127.5 259 | real_imgs = np.uint8(real_imgs[:, :, :, ::-1]) 260 | real_imgs = drawblock(real_imgs, 10) 261 | imsave(os.path.join(real_dir, '%i.jpg' % i_iter), real_imgs) 262 | # Store model 263 | if i_iter % save_iter == 0 or i_iter == max_iter - 1 or i_iter == max_iter: 264 | save_path = saver.save(sess, dir_name + '/cdgan%i.ckpt' % i_iter) 265 | -------------------------------------------------------------------------------- /CUB128GANAE.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, flatten, nnupsampling, batchnorm, gaussnoise, pool 3 | from activations import lrelu 4 | from op import log_sum_exp 5 | from data_loader import train_loader, validation_loader 6 | from neon.backends import gen_backend 7 | import numpy as np 8 | from utils import drawblock, createfolders, OneHot, image_reshape 9 | from scipy.misc import imsave 10 | import os 11 | 12 | 13 | # Create folders to store images 14 | gen_dir, real_dir, gen_dir128 = createfolders("./genimgs/CUB128GANAE", "/gen", "/real", "/gen128") 15 | # Create folder to store models 16 | dir_name = './models/CUB128GANAE' 17 | if not os.path.exists(dir_name): 18 | os.mkdir(dir_name) 19 | 20 | # Parameters 21 | init_iter, max_iter = 0, 30000 22 | display_iter = 100 23 | eval_iter = 100 24 | store_img_iter = 100 25 | save_iter = 1000 26 | 27 | lr_init = 0.0002 28 | batch_size = 100 29 | zdim = 100 30 | n_classes = 200 31 | dropout = 0.2 32 | im_size = [64, 64] 33 | dname, gname = 'd_', 'g_' 34 | tf.set_random_seed(1234) 35 | 36 | # DataLoader 37 | be = gen_backend(backend='cpu', batch_size=batch_size, datatype=np.float32) 38 | root_files = './dataset/cub200' 39 | manifestfile = os.path.join(root_files, 'train-index.csv') 40 | testmanifest = os.path.join(root_files, 'val-index.csv') 41 | train = train_loader(manifestfile, root_files, be, h=im_size[0], w=im_size[1], scale=[0.875, 0.875]) 42 | test = validation_loader(testmanifest, root_files, be, h=im_size[0], w=im_size[1], scale=[0.875, 0.875], ncls=n_classes) 43 | OneHot = OneHot(be, n_classes) 44 | 45 | # Graph input 46 | is_train = tf.placeholder(tf.bool) 47 | keep_prob = tf.placeholder(tf.float32) 48 | x_n = tf.placeholder(tf.float32, [batch_size, 3, im_size[0], im_size[1]]) 49 | y = tf.placeholder(tf.float32, [batch_size, n_classes]) 50 | lr_tf = tf.placeholder(tf.float32) 51 | z = tf.random_uniform([batch_size, zdim], -1, 1) 52 | iny = tf.placeholder(tf.float32, [batch_size, n_classes]) 53 | 54 | 55 | # Discriminator 56 | def discriminator(inp, reuse=False): 57 | with tf.variable_scope('Encoder', reuse=reuse): 58 | # 64 59 | inp = gaussnoise(inp, std=0.05) 60 | conv1 = conv2d(inp, 64, kernel=3, strides=2, name=dname + 'conv1') 61 | conv1 = lrelu(conv1, 0.2) 62 | # 32 63 | conv2 = tf.nn.dropout(conv1, keep_prob) 64 | conv2 = conv2d(conv2, 128, kernel=3, strides=2, name=dname + 'conv2') 65 | conv2 = batchnorm(conv2, is_training=is_train, name=dname + 'bn2') 66 | conv2 = lrelu(conv2, 0.2) 67 | # 16 68 | conv3 = tf.nn.dropout(conv2, keep_prob) 69 | conv3 = conv2d(conv3, 256, kernel=3, strides=2, name=dname + 'conv3') 70 | conv3 = batchnorm(conv3, is_training=is_train, name=dname + 'bn3') 71 | conv3 = lrelu(conv3, 0.2) 72 | # 8 73 | conv3b = conv2d(conv3, 256, kernel=3, strides=1, name=dname + 'conv3b') 74 | conv3b = batchnorm(conv3b, is_training=is_train, name=dname + 'bn3b') 75 | conv3b = lrelu(conv3b, 0.2) 76 | 77 | conv4 = tf.nn.dropout(conv3b, keep_prob) 78 | conv4 = conv2d(conv4, 512, kernel=3, strides=2, name=dname + 'conv4') 79 | conv4 = batchnorm(conv4, is_training=is_train, name=dname + 'bn4') 80 | conv4 = lrelu(conv4, 0.2) 81 | # 4 82 | conv4b = conv2d(conv4, 512, kernel=3, strides=1, name=dname + 'conv4b') 83 | conv4b = batchnorm(conv4b, is_training=is_train, name=dname + 'bn4b') 84 | conv4b = lrelu(conv4b, 0.2) 85 | 86 | flat = flatten(conv4b) 87 | # Classifier 88 | clspred = linear(flat, n_classes, name=dname + 'cpred') 89 | # Decoder 90 | g1 = conv2d(conv4b, nout=512, kernel=3, name=dname + 'deconv1') 91 | g1 = batchnorm(g1, is_training=tf.constant(True), name=dname + 'bn1g') 92 | g1 = lrelu(g1, 0.2) 93 | 94 | g2 = nnupsampling(g1, [8, 8]) 95 | g2 = conv2d(g2, nout=256, kernel=3, name=dname + 'deconv2') 96 | g2 = batchnorm(g2, is_training=tf.constant(True), name=dname + 'bn2g') 97 | g2 = lrelu(g2, 0.2) 98 | 99 | g3 = nnupsampling(g2, [16, 16]) 100 | g3 = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3') 101 | g3 = batchnorm(g3, is_training=tf.constant(True), name=dname + 'bn3g') 102 | g3 = lrelu(g3, 0.2) 103 | 104 | g4 = nnupsampling(g3, [32, 32]) 105 | g4 = conv2d(g4, nout=64, kernel=3, name=dname + 'deconv4') 106 | g4 = batchnorm(g4, is_training=tf.constant(True), name=dname + 'bn4g') 107 | g4 = lrelu(g4, 0.2) 108 | 109 | g5 = nnupsampling(g4, [64, 64]) 110 | g5 = conv2d(g5, nout=32, kernel=3, name=dname + 'deconv5') 111 | g5 = batchnorm(g5, is_training=tf.constant(True), name=dname + 'bn5g') 112 | g5 = lrelu(g5, 0.2) 113 | 114 | g5b = conv2d(g5, nout=3, kernel=3, name=dname + 'deconv5b') 115 | g5b = tf.nn.tanh(g5b) 116 | return clspred, g5b, flat 117 | 118 | 119 | # Generator 120 | def generator(inp_z, inp_y, reuse=False): 121 | with tf.variable_scope('Generator', reuse=reuse): 122 | inp = tf.concat([inp_z, inp_y], 1) 123 | sz = 4 124 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 125 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 126 | g1 = lrelu(g1, 0.2) 127 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 128 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 129 | 130 | g2 = nnupsampling(g1_reshaped, [8, 8]) 131 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 132 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 133 | g2 = lrelu(g2, 0.2) 134 | 135 | g3 = nnupsampling(g2, [16, 16]) 136 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 137 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 138 | g3 = lrelu(g3, 0.2) 139 | 140 | g4 = nnupsampling(g3, [32, 32]) 141 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 142 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 143 | g4 = lrelu(g4, 0.2) 144 | 145 | g5 = nnupsampling(g4, [64, 64]) 146 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 147 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 148 | g5 = lrelu(g5, 0.2) 149 | 150 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 151 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 152 | g5b = lrelu(g5b, 0.2) 153 | 154 | g6 = nnupsampling(g5b, [128, 128]) 155 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 156 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 157 | g6 = lrelu(g6, 0.2) 158 | 159 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 160 | g6b = tf.nn.tanh(g6b) 161 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 162 | return g6b_64, g6b 163 | 164 | # Call functions 165 | Opred_n, recon_n, _ = discriminator(x_n) 166 | samples, samples128 = generator(z, iny) 167 | Opred_g, recon_g, embed = discriminator(samples, reuse=True) 168 | 169 | # Get trainable variables and split 170 | t_vars = tf.trainable_variables() 171 | d_vars = [var for var in t_vars if dname in var.name] 172 | g_vars = [var for var in t_vars if gname in var.name] 173 | print [var.name for var in d_vars] 174 | print [var.name for var in g_vars] 175 | 176 | # Define D loss 177 | lreal = log_sum_exp(Opred_n) 178 | lfake = log_sum_exp(Opred_g) 179 | cost_On = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_n, labels=y)) 180 | cost_Dn = - tf.reduce_mean(lreal) + tf.reduce_mean(tf.nn.softplus(lreal)) 181 | cost_Dg_fake = tf.reduce_mean(tf.nn.softplus(lfake)) 182 | cost_msen = tf.reduce_mean(tf.square(recon_n - x_n)) * 0.5 183 | cost_mseg = tf.reduce_mean(tf.square(recon_g - samples)) * 0.5 184 | D_loss = cost_On + cost_Dn + cost_Dg_fake + cost_msen 185 | # Define G loss 186 | cost_Dg = - tf.reduce_mean(lfake) + tf.reduce_mean(tf.nn.softplus(lfake)) 187 | cost_Og = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_g, labels=iny)) 188 | G_loss = cost_Dg + cost_Og + cost_mseg 189 | 190 | # Define optimizer 191 | d_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(D_loss, var_list=d_vars) 192 | g_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(G_loss, var_list=g_vars) 193 | 194 | # Evaluate model 195 | Oaccuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Opred_n, 1), tf.argmax(y, 1)), tf.float32)) 196 | 197 | # Initialize the variables 198 | init = tf.global_variables_initializer() 199 | # Reset train dataset 200 | train.reset() 201 | # Config for session 202 | config = tf.ConfigProto() 203 | config.gpu_options.allow_growth = True 204 | # Train 205 | with tf.Session(config=config) as sess: 206 | sess.run(init) 207 | saver = tf.train.Saver(max_to_keep=None) 208 | coord = tf.train.Coordinator() 209 | threads = tf.train.start_queue_runners(coord=coord) 210 | for i_iter in range(init_iter, max_iter): 211 | # Control lr 212 | if i_iter < 15000: 213 | lr = lr_init 214 | else: 215 | lr = lr_init / 10. 216 | # Set current fake label 217 | inds = np.repeat(np.random.permutation(n_classes)[:20], 5) 218 | fake_y = np.zeros((batch_size, n_classes)) 219 | fake_y[np.arange(batch_size), inds] = 1. 220 | # Fetch minibatch 221 | batch_x, batch_y = train.next() 222 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 223 | batch_y = OneHot.transform(batch_y).get().transpose() 224 | # update discriminator 225 | _, lossDn, lossOn, lossFake = sess.run([d_optimizer, cost_Dn, cost_On, cost_Dg_fake], feed_dict={ 226 | x_n: batch_x, y: batch_y, iny: fake_y, 227 | keep_prob: 1. - dropout, is_train: True, lr_tf: lr 228 | }) 229 | # update generator 230 | _, gen_img, gen_img128 = sess.run([g_optimizer, samples, samples128], feed_dict={ 231 | iny: fake_y, 232 | keep_prob: 1., is_train: True, lr_tf: lr 233 | }) 234 | # print losses 235 | if i_iter % display_iter == 0 or i_iter == max_iter - 1: 236 | print 'Iteration: %i, lossDn: %.2f, lossOn: %.2f, lossFake: %.2f' % (i_iter, lossDn, lossOn, lossFake) 237 | # Evaluate classification accuracy 238 | if i_iter % eval_iter == 0 or i_iter == max_iter - 1: 239 | total_Oaccuracy = 0. 240 | test.reset() 241 | for mb_idx, (batch_x, batch_y) in enumerate(test): 242 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 243 | batch_y = batch_y.get().transpose() 244 | total_Oaccuracy += sess.run(Oaccuracy, 245 | feed_dict={x_n: batch_x, y: batch_y, keep_prob: 1., is_train: False}) 246 | print 'Iteration %i, Accuracy: %.2f' % (i_iter, total_Oaccuracy / mb_idx) 247 | # Store images 248 | if i_iter % store_img_iter == 0 or i_iter == max_iter - 1: 249 | # Store Generated 250 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 251 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 252 | genmix_imgs = drawblock(genmix_imgs, 10) 253 | imsave(os.path.join(gen_dir, '%i.jpg' % i_iter), genmix_imgs) 254 | # Store Generated 96 255 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 256 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 257 | genmix_imgs = drawblock(genmix_imgs, 10) 258 | imsave(os.path.join(gen_dir128, '%i.jpg' % i_iter), genmix_imgs) 259 | # Store Real 260 | real_imgs = (np.transpose(batch_x, [0, 2, 3, 1]) + 1.) * 127.5 261 | real_imgs = np.uint8(real_imgs[:, :, :, ::-1]) 262 | real_imgs = drawblock(real_imgs, 10) 263 | imsave(os.path.join(real_dir, '%i.jpg' % i_iter), real_imgs) 264 | # Store model 265 | if i_iter % save_iter == 0 or i_iter == max_iter - 1 or i_iter == max_iter: 266 | save_path = saver.save(sess, dir_name + '/cdgan%i.ckpt' % i_iter) 267 | coord.request_stop() 268 | coord.join(threads) 269 | -------------------------------------------------------------------------------- /STL128GANAE.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from layers import conv2d, linear, flatten, nnupsampling, batchnorm, gaussnoise, pool 3 | from activations import lrelu 4 | from op import log_sum_exp 5 | from data_loader import train_loader, validation_loader 6 | from neon.backends import gen_backend 7 | import numpy as np 8 | from utils import drawblock, createfolders, OneHot, image_reshape 9 | from scipy.misc import imsave 10 | import os 11 | 12 | 13 | # Create folders to store images 14 | gen_dir, real_dir, gen_dir128 = createfolders("./genimgs/STL128GANAE", "/gen", "/real", "/gen128") 15 | # Create folder to store models 16 | dir_name = './models/STL128GANAE' 17 | if not os.path.exists(dir_name): 18 | os.mkdir(dir_name) 19 | 20 | # Parameters 21 | init_iter, max_iter = 0, 50000 22 | display_iter = 100 23 | eval_iter = 100 24 | store_img_iter = 100 25 | save_iter = 1000 26 | 27 | lr_init = 0.0002 28 | batch_size = 100 29 | zdim = 100 30 | n_classes = 10 31 | dropout = 0.2 32 | im_size = [64, 64] 33 | dname, gname = 'd_', 'g_' 34 | tf.set_random_seed(1234) 35 | 36 | # DataLoader 37 | be = gen_backend(backend='cpu', batch_size=batch_size, datatype=np.float32) 38 | root_files = './dataset/stl10' 39 | manifestfile = os.path.join(root_files, 'train-index.csv') 40 | testmanifest = os.path.join(root_files, 'val-index.csv') 41 | train = train_loader(manifestfile, root_files, be, h=im_size[0], w=im_size[1], scale=[0.875, 0.875]) 42 | test = validation_loader(testmanifest, root_files, be, h=im_size[0], w=im_size[1], scale=[0.875, 0.875]) 43 | OneHot = OneHot(be, n_classes) 44 | 45 | # Graph input 46 | is_train = tf.placeholder(tf.bool) 47 | keep_prob = tf.placeholder(tf.float32) 48 | x_n = tf.placeholder(tf.float32, [batch_size, 3, im_size[0], im_size[1]]) 49 | y = tf.placeholder(tf.float32, [batch_size, n_classes]) 50 | lr_tf = tf.placeholder(tf.float32) 51 | z = tf.random_uniform([batch_size, zdim], -1, 1) 52 | iny = tf.constant(np.tile(np.eye(n_classes, dtype=np.float32), [batch_size / n_classes + 1, 1])[:batch_size, :]) 53 | 54 | 55 | # Discriminator 56 | def discriminator(inp, reuse=False): 57 | with tf.variable_scope('Encoder', reuse=reuse): 58 | # 64 59 | inp = gaussnoise(inp, std=0.05) 60 | conv1 = conv2d(inp, 64, kernel=3, strides=1, name=dname + 'conv1') 61 | conv1 = lrelu(conv1, 0.2) 62 | 63 | conv1b = conv2d(conv1, 64, kernel=3, strides=2, name=dname + 'conv1b') 64 | conv1b = batchnorm(conv1b, is_training=is_train, name=dname + 'bn1b') 65 | conv1b = lrelu(conv1b, 0.2) 66 | # 32 67 | conv2 = tf.nn.dropout(conv1b, keep_prob) 68 | conv2 = conv2d(conv2, 128, kernel=3, strides=1, name=dname + 'conv2') 69 | conv2 = batchnorm(conv2, is_training=is_train, name=dname + 'bn2') 70 | conv2 = lrelu(conv2, 0.2) 71 | 72 | conv2b = conv2d(conv2, 128, kernel=3, strides=2, name=dname + 'conv2b') 73 | conv2b = batchnorm(conv2b, is_training=is_train, name=dname + 'bn2b') 74 | conv2b = lrelu(conv2b, 0.2) 75 | # 16 76 | conv3 = tf.nn.dropout(conv2b, keep_prob) 77 | conv3 = conv2d(conv3, 256, kernel=3, strides=1, name=dname + 'conv3') 78 | conv3 = batchnorm(conv3, is_training=is_train, name=dname + 'bn3') 79 | conv3 = lrelu(conv3, 0.2) 80 | 81 | conv3b = conv2d(conv3, 256, kernel=3, strides=2, name=dname + 'conv3b') 82 | conv3b = batchnorm(conv3b, is_training=is_train, name=dname + 'bn3b') 83 | conv3b = lrelu(conv3b, 0.2) 84 | # 8 85 | conv4 = tf.nn.dropout(conv3b, keep_prob) 86 | conv4 = conv2d(conv4, 512, kernel=3, strides=1, name=dname + 'conv4') 87 | conv4 = batchnorm(conv4, is_training=is_train, name=dname + 'bn4') 88 | conv4 = lrelu(conv4, 0.2) 89 | 90 | conv4b = conv2d(conv4, 512, kernel=3, strides=1, name=dname + 'conv4b') 91 | conv4b = batchnorm(conv4b, is_training=is_train, name=dname + 'bn4b') 92 | conv4b = lrelu(conv4b, 0.2) 93 | 94 | flat = flatten(conv4b) 95 | # Classifier 96 | clspred = linear(flat, n_classes, name=dname + 'cpred') 97 | # Decoder 98 | g1 = conv2d(conv4b, nout=512, kernel=3, name=dname + 'deconv1') 99 | g1 = batchnorm(g1, is_training=tf.constant(True), name=dname + 'bn1g') 100 | g1 = lrelu(g1, 0.2) 101 | 102 | g2 = nnupsampling(g1, [16, 16]) 103 | g2 = conv2d(g2, nout=256, kernel=3, name=dname + 'deconv2') 104 | g2 = batchnorm(g2, is_training=tf.constant(True), name=dname + 'bn2g') 105 | g2 = lrelu(g2, 0.2) 106 | 107 | g3 = nnupsampling(g2, [32, 32]) 108 | g3 = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3') 109 | g3 = batchnorm(g3, is_training=tf.constant(True), name=dname + 'bn3g') 110 | g3 = lrelu(g3, 0.2) 111 | 112 | g3b = conv2d(g3, nout=128, kernel=3, name=dname + 'deconv3b') 113 | g3b = batchnorm(g3b, is_training=tf.constant(True), name=dname + 'bn3bg') 114 | g3b = lrelu(g3b, 0.2) 115 | 116 | g4 = nnupsampling(g3b, [64, 64]) 117 | g4 = conv2d(g4, nout=64, kernel=3, name=dname + 'deconv4') 118 | g4 = batchnorm(g4, is_training=tf.constant(True), name=dname + 'bn4g') 119 | g4 = lrelu(g4, 0.2) 120 | 121 | g4b = conv2d(g4, nout=3, kernel=3, name=dname + 'deconv4b') 122 | g4b = tf.nn.tanh(g4b) 123 | return clspred, g4b 124 | 125 | 126 | # Generator 127 | def generator(inp_z, inp_y, reuse=False): 128 | with tf.variable_scope('Generator', reuse=reuse): 129 | inp = tf.concat([inp_z, inp_y], 1) 130 | sz = 4 131 | g1 = linear(inp, 512 * sz * sz, name=gname + 'deconv1') 132 | g1 = batchnorm(g1, is_training=tf.constant(True), name=gname + 'bn1g') 133 | g1 = lrelu(g1, 0.2) 134 | g1_reshaped = tf.reshape(g1, [-1, 512, sz, sz]) 135 | print 'genreshape: ' + str(g1_reshaped.get_shape().as_list()) 136 | 137 | g2 = nnupsampling(g1_reshaped, [8, 8]) 138 | g2 = conv2d(g2, nout=512, kernel=3, name=gname + 'deconv2') 139 | g2 = batchnorm(g2, is_training=tf.constant(True), name=gname + 'bn2g') 140 | g2 = lrelu(g2, 0.2) 141 | 142 | g3 = nnupsampling(g2, [16, 16]) 143 | g3 = conv2d(g3, nout=256, kernel=3, name=gname + 'deconv3') 144 | g3 = batchnorm(g3, is_training=tf.constant(True), name=gname + 'bn3g') 145 | g3 = lrelu(g3, 0.2) 146 | 147 | g4 = nnupsampling(g3, [32, 32]) 148 | g4 = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4') 149 | g4 = batchnorm(g4, is_training=tf.constant(True), name=gname + 'bn4g') 150 | g4 = lrelu(g4, 0.2) 151 | 152 | g4b = conv2d(g4, nout=128, kernel=3, name=gname + 'deconv4b') 153 | g4b = batchnorm(g4b, is_training=tf.constant(True), name=gname + 'bn4bg') 154 | g4b = lrelu(g4b, 0.2) 155 | 156 | g5 = nnupsampling(g4b, [64, 64]) 157 | g5 = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5') 158 | g5 = batchnorm(g5, is_training=tf.constant(True), name=gname + 'bn5g') 159 | g5 = lrelu(g5, 0.2) 160 | 161 | g5b = conv2d(g5, nout=64, kernel=3, name=gname + 'deconv5b') 162 | g5b = batchnorm(g5b, is_training=tf.constant(True), name=gname + 'bn5bg') 163 | g5b = lrelu(g5b, 0.2) 164 | 165 | g6 = nnupsampling(g5b, [128, 128]) 166 | g6 = conv2d(g6, nout=32, kernel=3, name=gname + 'deconv6') 167 | g6 = batchnorm(g6, is_training=tf.constant(True), name=gname + 'bn6g') 168 | g6 = lrelu(g6, 0.2) 169 | 170 | g6b = conv2d(g6, nout=3, kernel=3, name=gname + 'deconv6b') 171 | g6b = tf.nn.tanh(g6b) 172 | g6b_64 = pool(g6b, fsize=3, strides=2, op='avg') 173 | return g6b_64, g6b 174 | 175 | # Call functions 176 | Opred_n, recon_n = discriminator(x_n) 177 | samples, samples128 = generator(z, iny) 178 | Opred_g, recon_g = discriminator(samples, reuse=True) 179 | 180 | # Get trainable variables and split 181 | t_vars = tf.trainable_variables() 182 | d_vars = [var for var in t_vars if dname in var.name] 183 | g_vars = [var for var in t_vars if gname in var.name] 184 | print [var.name for var in d_vars] 185 | print [var.name for var in g_vars] 186 | 187 | # Define D loss 188 | lreal = log_sum_exp(Opred_n) 189 | lfake = log_sum_exp(Opred_g) 190 | cost_On = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_n, labels=y)) 191 | cost_Dn = - tf.reduce_mean(lreal) + tf.reduce_mean(tf.nn.softplus(lreal)) 192 | cost_Dg_fake = tf.reduce_mean(tf.nn.softplus(lfake)) 193 | cost_msen = tf.reduce_mean(tf.square(recon_n - x_n)) * 0.5 194 | cost_mseg = tf.reduce_mean(tf.square(recon_g - samples)) * 0.5 195 | D_loss = cost_On + cost_Dn + cost_Dg_fake + cost_msen 196 | # Define G loss 197 | cost_Dg = - tf.reduce_mean(lfake) + tf.reduce_mean(tf.nn.softplus(lfake)) 198 | cost_Og = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Opred_g, labels=iny)) 199 | G_loss = cost_Dg + cost_Og + cost_mseg 200 | 201 | # Define optimizer 202 | d_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(D_loss, var_list=d_vars) 203 | g_optimizer = tf.train.AdamOptimizer(learning_rate=lr_tf, beta1=0.5).minimize(G_loss, var_list=g_vars) 204 | 205 | # Evaluate model 206 | Oaccuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Opred_n, 1), tf.argmax(y, 1)), tf.float32)) 207 | 208 | # Initialize the variables 209 | init = tf.global_variables_initializer() 210 | # Reset train dataset 211 | train.reset() 212 | # Config for session 213 | config = tf.ConfigProto() 214 | config.gpu_options.allow_growth = True 215 | # Train 216 | with tf.Session(config=config) as sess: 217 | sess.run(init) 218 | saver = tf.train.Saver(max_to_keep=None) 219 | for i_iter in range(init_iter, max_iter): 220 | # Control lr 221 | if i_iter < 30000: 222 | lr = lr_init 223 | else: 224 | lr = lr_init / 10. 225 | # Fetch minibatch 226 | batch_x, batch_y = train.next() 227 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 228 | batch_y = OneHot.transform(batch_y).get().transpose() 229 | # update discriminator 230 | _, lossDn, lossOn, lossFake = sess.run([d_optimizer, cost_Dn, cost_On, cost_Dg_fake], feed_dict={ 231 | x_n: batch_x, y: batch_y, 232 | keep_prob: 1. - dropout, is_train: True, lr_tf: lr 233 | }) 234 | # update generator 235 | _, gen_img, gen_img128 = sess.run([g_optimizer, samples, samples128], feed_dict={ 236 | keep_prob: 1., is_train: True, lr_tf: lr 237 | }) 238 | # print losses 239 | if i_iter % display_iter == 0 or i_iter == max_iter - 1: 240 | print 'Iteration: %i, lossDn: %.2f, lossOn: %.2f, lossFake: %.2f' % (i_iter, lossDn, lossOn, lossFake) 241 | # Evaluate classification accuracy 242 | if i_iter % eval_iter == 0 or i_iter == max_iter - 1: 243 | total_Oaccuracy = 0. 244 | test.reset() 245 | for mb_idx, (batch_x, batch_y) in enumerate(test): 246 | batch_x = image_reshape(batch_x.get(), im_size, input_format='tanh') 247 | batch_y = batch_y.get().transpose() 248 | total_Oaccuracy += sess.run(Oaccuracy, 249 | feed_dict={x_n: batch_x, y: batch_y, keep_prob: 1., is_train: False}) 250 | print 'Iteration %i, Accuracy: %.2f' % (i_iter, total_Oaccuracy / mb_idx) 251 | # Store images 252 | if i_iter % store_img_iter == 0 or i_iter == max_iter - 1: 253 | # Store Generated 254 | genmix_imgs = (np.transpose(gen_img, [0, 2, 3, 1]) + 1.) * 127.5 255 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 256 | genmix_imgs = drawblock(genmix_imgs, 10) 257 | imsave(os.path.join(gen_dir, '%i.jpg' % i_iter), genmix_imgs) 258 | # Store Generated 128 259 | genmix_imgs = (np.transpose(gen_img128, [0, 2, 3, 1]) + 1.) * 127.5 260 | genmix_imgs = np.uint8(genmix_imgs[:, :, :, ::-1]) 261 | genmix_imgs = drawblock(genmix_imgs, 10) 262 | imsave(os.path.join(gen_dir128, '%i.jpg' % i_iter), genmix_imgs) 263 | # Store Real 264 | real_imgs = (np.transpose(batch_x, [0, 2, 3, 1]) + 1.) * 127.5 265 | real_imgs = np.uint8(real_imgs[:, :, :, ::-1]) 266 | real_imgs = drawblock(real_imgs, 10) 267 | imsave(os.path.join(real_dir, '%i.jpg' % i_iter), real_imgs) 268 | # Store model 269 | if i_iter % save_iter == 0 or i_iter == max_iter - 1 or i_iter == max_iter: 270 | save_path = saver.save(sess, dir_name + '/cdgan%i.ckpt' % i_iter) 271 | --------------------------------------------------------------------------------