├── BEGAN ├── README.md ├── data │ ├── README.md │ └── raw │ │ └── README.md ├── figures │ ├── celeba_samples.png │ └── celeba_samples2.png ├── requirements.txt └── src │ ├── README.md │ ├── model │ ├── README.md │ ├── flags.py │ ├── main.py │ ├── models.py │ └── train.py │ └── utils │ ├── data_utils.py │ ├── inference_utils.py │ ├── layers.py │ ├── logging_utils.py │ ├── losses.py │ ├── training_utils.py │ └── visualization_utils.py ├── Colorful ├── README.md ├── data │ ├── README.md │ ├── processed │ │ ├── README.md │ │ └── pts_in_hull.npy │ └── raw │ │ └── README.md ├── figures │ ├── README.md │ └── celeba_colorized_small.png └── src │ ├── README.md │ ├── app │ ├── README.md │ └── webcam_colorizer.py │ ├── data │ ├── README.md │ └── make_dataset.py │ ├── model │ ├── README.md │ ├── eval_colorful.py │ ├── main.py │ ├── models_colorful.py │ └── train_colorful.py │ └── utils │ ├── batch_utils.py │ ├── batch_utils.pyc │ ├── general_utils.py │ └── general_utils.pyc ├── DFI ├── README.md ├── data │ ├── README.md │ ├── processed │ │ └── README.md │ └── raw │ │ └── README.md ├── figures │ ├── README.md │ └── example_figures │ │ └── example.png └── src │ ├── README.md │ ├── data │ ├── README.md │ └── make_dataset.py │ ├── model │ ├── README.md │ ├── dfi.py │ ├── dfi.pyc │ └── main.py │ └── utils │ └── general_utils.py ├── DeconvNet ├── KerasDeconv.py ├── README.md ├── VGG_deconv.py ├── data │ └── Img.tar.gz ├── figures │ ├── convolution2d_10.png │ └── convolution2d_13.png └── utils.py ├── DenseNet ├── README.md ├── densenet.py ├── figures │ ├── cifar10_results.png │ ├── dense_block.jpg │ ├── densenet_archi.png │ └── densenet_scheme.jpg ├── log │ ├── README.md │ └── experiment_log_cifar10.json ├── plot_results.py └── run_cifar10.py ├── Eve ├── Eve.py ├── README.md ├── eval.py ├── figures │ ├── cifar100_results_accs.png │ ├── cifar100_results_losses.png │ ├── cifar10_results_accs.png │ └── cifar10_results_losses.png ├── log │ ├── cifar10_CNN_Adam.json │ ├── cifar10_CNN_Eve.json │ └── cifar10_CNN_SGD.json ├── main.py ├── models.py └── train.py ├── GAN ├── README.md ├── data │ ├── README.md │ ├── processed │ │ ├── .gitkeep │ │ └── README.md │ └── raw │ │ ├── .gitkeep │ │ └── README.md ├── figures │ ├── img_gan.png │ ├── img_gan2.png │ ├── img_gan3.png │ ├── img_mnist.png │ └── img_mnist2.png ├── models │ ├── .gitkeep │ └── README.md └── src │ ├── README.md │ ├── data │ ├── README.md │ └── make_dataset.py │ ├── model │ ├── README.md │ ├── main.py │ ├── models_GAN.py │ └── train_GAN.py │ └── utils │ ├── batch_utils.py │ ├── data_utils.py │ └── general_utils.py ├── GAN_tf ├── README.md ├── data │ ├── README.md │ └── raw │ │ └── README.md ├── figures │ └── README.md ├── logs │ └── README.md ├── models │ └── README.md └── src │ ├── README.md │ ├── model │ ├── README.md │ ├── flags.py │ ├── input.py │ ├── main.py │ ├── models.py │ ├── train.py │ └── train_mnist.py │ └── utils │ ├── data_utils.py │ ├── layers.py │ ├── logging_utils.py │ ├── objectives.py │ ├── training_utils.py │ └── visualization_utils.py ├── InfoGAN ├── README.md ├── data │ ├── README.md │ ├── processed │ │ └── README.md │ └── raw │ │ └── README.md ├── figures │ ├── DCGAN_discriminator.png │ ├── generator_deconv.png │ ├── generator_upsampling.png │ ├── varying_categorical.png │ └── varying_continuous.png ├── models │ └── README.md └── src │ ├── README.md │ ├── data │ ├── README.md │ └── make_dataset.py │ ├── model │ ├── README.md │ ├── eval.py │ ├── main.py │ ├── models.py │ └── train.py │ └── utils │ ├── batch_utils.py │ ├── data_utils.py │ └── general_utils.py ├── LICENSE.md ├── README.md ├── SELU ├── README.md ├── figures │ ├── SELU_LR_1E-2.png │ ├── SELU_LR_1E-3.png │ └── SELU_LR_1E-5.png ├── main.py ├── models.py ├── plot_results.py ├── run_experiments.sh └── train.py ├── ScatteringTransform ├── README.md ├── data │ ├── README.md │ └── raw │ │ └── README.md ├── src │ ├── README.md │ ├── model │ │ ├── README.md │ │ ├── flags.py │ │ ├── main.py │ │ ├── models.py │ │ └── train_mnist.py │ └── utils │ │ ├── data_utils.py │ │ ├── filters_bank.py │ │ ├── layers.py │ │ ├── logging_utils.py │ │ ├── objectives.py │ │ ├── scattering.py │ │ ├── training_utils.py │ │ └── visualization_utils.py └── test │ ├── README.md │ ├── filters_bank_pytorch.py │ ├── run_pytorch.py │ ├── run_test.py │ ├── run_tf.py │ ├── scattering_pytorch.py │ └── utils_pytorch.py ├── Sobolev ├── README.md ├── figures │ ├── plot_10000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_10000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_1000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_1000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_100_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_100_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_10_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_10_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_50000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_50000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_5000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_5000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_500_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_500_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_50_epochs_100_npts_0.0001_LR_1_sobolev_weight.png │ ├── plot_50_epochs_20_npts_0.0001_LR_1_sobolev_weight.png │ ├── tang_100pts.gif │ └── tang_20pts.gif ├── main.py ├── make_gif.py ├── plot_results.py ├── run_experiments.sh ├── sobolev_training.py └── utils.py ├── WGAN-GP ├── README.md ├── data │ ├── README.md │ └── raw │ │ └── README.md └── src │ ├── README.md │ ├── model │ ├── README.md │ ├── flags.py │ ├── main.py │ ├── models.py │ └── train_wgan_GP.py │ └── utils │ ├── data_utils.py │ ├── layers.py │ ├── logging_utils.py │ ├── objectives.py │ ├── training_utils.py │ └── visualization_utils.py ├── WassersteinGAN ├── README.md ├── data │ ├── README.md │ ├── processed │ │ └── README.md │ └── raw │ │ └── README.md ├── figures │ ├── MoG_dataset.gif │ ├── img_celebA_1.png │ └── write_gif_script.py ├── models │ └── README.md └── src │ ├── README.md │ ├── data │ ├── README.md │ └── make_dataset.py │ ├── model │ ├── README.md │ ├── main.py │ ├── models_WGAN.py │ └── train_WGAN.py │ └── utils │ ├── batch_utils.py │ ├── data_utils.py │ └── general_utils.py └── pix2pix ├── README.md ├── data ├── README.md ├── processed │ └── README.md └── raw │ └── README.md ├── figures └── img_pix2pix.png ├── models └── README.md └── src ├── README.md ├── data ├── README.md └── make_dataset.py ├── model ├── README.md ├── main.py ├── models.py └── train.py └── utils ├── batch_utils.py ├── data_utils.py └── general_utils.py /BEGAN/README.md: -------------------------------------------------------------------------------- 1 | # BEGAN 2 | 3 | Tensorflow implementation of BEGAN. 4 | 5 | **Sources:** 6 | 7 | - [BEGAN](https://arxiv.org/pdf/1703.10717.pdf) 8 | 9 | # Requirements 10 | 11 | ## python modules 12 | 13 | pip install -r requirements.txt 14 | 15 | should install all the necessary dependencies. 16 | 17 | # Part 1. Getting the data 18 | 19 | - Go to http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 20 | - In the Downloads section, select Align&Cropped images. 21 | - In the dropbox page that follows, download the Anno, Eval and Img folders. 22 | - Copy these folders to `/data/raw`. 23 | - Extract the zip files. 24 | 25 | We are only going to use the images 26 | 27 | # Part 2. Running the code 28 | 29 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/BEGAN/src/model) 30 | 31 | # Part 3. Example results 32 | 33 | **CelebA example results** 34 | 35 | ![figure](./figures/celeba_samples.png) 36 | ![figure](./figures/celeba_samples2.png) 37 | 38 | - The first 2 rows are generated images 39 | - The last 2 rows are real images 40 | 41 | 42 | -------------------------------------------------------------------------------- /BEGAN/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /BEGAN/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /BEGAN/figures/celeba_samples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/BEGAN/figures/celeba_samples.png -------------------------------------------------------------------------------- /BEGAN/figures/celeba_samples2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/BEGAN/figures/celeba_samples2.png -------------------------------------------------------------------------------- /BEGAN/requirements.txt: -------------------------------------------------------------------------------- 1 | natsort==5.1.0 2 | tqdm==4.17.0 3 | terminaltables==3.1.0 4 | matplotlib==2.0.2 5 | colorama==0.3.9 6 | numpy==1.13.3 7 | tensorflow_gpu==1.3.0 8 | tensorflow==1.4.0rc1 9 | -------------------------------------------------------------------------------- /BEGAN/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/BEGAN/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/BEGAN/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/BEGAN/src/model) for training scripts 8 | -------------------------------------------------------------------------------- /BEGAN/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training WGAN 2 | 3 | ## Usage 4 | 5 | `python main.py` 6 | 7 | 8 | optional arguments: 9 | 10 | --run RUN Which operation to run. [train|inference] 11 | --nb_epoch NB_EPOCH Number of epochs 12 | --batch_size BATCH_SIZE 13 | Number of samples per batch. 14 | --nb_batch_per_epoch NB_BATCH_PER_EPOCH 15 | Number of batches per epoch 16 | --learning_rate LEARNING_RATE 17 | Learning rate used for AdamOptimizer 18 | --h_dim H_DIM AutoEncoder internal embedding dimension 19 | --z_dim Z_DIM Noise distribution dimension dimension 20 | --nb_filters_D NB_FILTERS_D 21 | Number of conv filters for D 22 | --nb_filters_G NB_FILTERS_G 23 | Number of conv filters for G 24 | 25 | --random_seed RANDOM_SEED 26 | Seed used to initialize rng. 27 | --max_to_keep MAX_TO_KEEP 28 | Maximum number of model/session files to keep 29 | --gamma GAMMA 30 | --lambdak LAMBDAK Proportional gain for k 31 | --use_XLA [USE_XLA] Whether to use XLA compiler. 32 | --num_threads NUM_THREADS 33 | Number of threads to fetch the data 34 | --capacity_factor CAPACITY_FACTOR 35 | Number of batches to store in queue 36 | --data_format DATA_FORMAT 37 | Tensorflow image data format. 38 | --celebA_path CELEBA_PATH 39 | Path to celebA images 40 | --channels CHANNELS Number of channels 41 | --central_fraction CENTRAL_FRACTION 42 | Central crop as a fraction of total image 43 | --img_size IMG_SIZE Image size 44 | --model_dir MODEL_DIR 45 | Output folder where checkpoints are dumped. 46 | --log_dir LOG_DIR Logs for tensorboard. 47 | --fig_dir FIG_DIR Where to save figures. 48 | --raw_dir RAW_DIR Where raw data is saved 49 | --data_dir DATA_DIR Where processed data is saved 50 | 51 | 52 | 53 | 54 | 55 | **Example:** 56 | 57 | python main.py --run train 58 | 59 | 60 | ## Results: 61 | 62 | Figures are saved in the `figures` folder. They show real images, fake images and reconstruction. One set of images is saved at each epoch. 63 | 64 | Call: 65 | 66 | tensorboard --logdir = /path/to/logs 67 | 68 | for visualization. 69 | 70 | ## Implementation notes: 71 | 72 | - Downsampling in D is done with strided convolutions 73 | - The current implementation is designed for 64x64 images. 74 | - Previous models / logs / figures are wiped out at each invocation of the code. THis behaviour can be modified in `src/utils/training_utils/setup_training_session()` -------------------------------------------------------------------------------- /BEGAN/src/model/flags.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | 4 | FLAGS = tf.app.flags.FLAGS 5 | 6 | 7 | def define_flags(): 8 | 9 | ############ 10 | # Run mode 11 | ############ 12 | # INFERENCE TODO 13 | tf.app.flags.DEFINE_string('run', None, "Which operation to run. [train|inference]") 14 | 15 | ########################## 16 | # Training parameters 17 | ########################### 18 | tf.app.flags.DEFINE_integer('nb_epoch', 500, "Number of epochs") 19 | tf.app.flags.DEFINE_integer('batch_size', 16, "Number of samples per batch.") 20 | tf.app.flags.DEFINE_integer('nb_batch_per_epoch', 100, "Number of batches per epoch") 21 | tf.app.flags.DEFINE_float('learning_rate', 1E-4, "Learning rate used for AdamOptimizer") 22 | tf.app.flags.DEFINE_integer('h_dim', 128, "AutoEncoder internal embedding dimension") 23 | tf.app.flags.DEFINE_integer('z_dim', 128, "Noise distribution dimension dimension") 24 | tf.app.flags.DEFINE_integer('nb_filters_D', 64, "Number of conv filters for D") 25 | tf.app.flags.DEFINE_integer('nb_filters_G', 64, "Number of conv filters for G") 26 | tf.app.flags.DEFINE_integer('random_seed', 0, "Seed used to initialize rng.") 27 | tf.app.flags.DEFINE_integer('max_to_keep', 500, "Maximum number of model/session files to keep") 28 | tf.app.flags.DEFINE_float('gamma', 0.5, "") 29 | tf.app.flags.DEFINE_float('lambdak', 1E-3, "Proportional gain for k") 30 | 31 | ############################################ 32 | # General tensorflow parameters parameters 33 | ############################################# 34 | tf.app.flags.DEFINE_bool('use_XLA', False, "Whether to use XLA compiler.") 35 | tf.app.flags.DEFINE_integer('num_threads', 2, "Number of threads to fetch the data") 36 | tf.app.flags.DEFINE_float('capacity_factor', 32, "Number of batches to store in queue") 37 | 38 | ########## 39 | # Datasets 40 | ########## 41 | tf.app.flags.DEFINE_string('data_format', "NCHW", "Tensorflow image data format.") 42 | tf.app.flags.DEFINE_string('celebA_path', "../../data/raw/img_align_celeba", "Path to celebA images") 43 | tf.app.flags.DEFINE_integer('channels', 3, "Number of channels") 44 | tf.app.flags.DEFINE_float('central_fraction', 0.6, "Central crop as a fraction of total image") 45 | tf.app.flags.DEFINE_integer('img_size', 64, "Image size") 46 | 47 | ############## 48 | # Directories 49 | ############## 50 | tf.app.flags.DEFINE_string('model_dir', '../../models', "Output folder where checkpoints are dumped.") 51 | tf.app.flags.DEFINE_string('log_dir', '../../logs', "Logs for tensorboard.") 52 | tf.app.flags.DEFINE_string('fig_dir', '../../figures', "Where to save figures.") 53 | tf.app.flags.DEFINE_string('raw_dir', '../../data/raw', "Where raw data is saved") 54 | tf.app.flags.DEFINE_string('data_dir', '../../data/processed', "Where processed data is saved") 55 | -------------------------------------------------------------------------------- /BEGAN/src/model/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | # Disable Tensorflow's INFO and WARNING messages 3 | # See http://stackoverflow.com/questions/35911252 4 | if 'TF_CPP_MIN_LOG_LEVEL' not in os.environ: 5 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 6 | import tensorflow as tf 7 | import flags 8 | import train 9 | 10 | FLAGS = tf.app.flags.FLAGS 11 | 12 | 13 | def launch_training(): 14 | 15 | train.train_model() 16 | 17 | 18 | def main(argv=None): 19 | 20 | assert FLAGS.run in ["train"], "Choose [train]" 21 | 22 | if FLAGS.run == 'train': 23 | launch_training() 24 | 25 | 26 | if __name__ == '__main__': 27 | 28 | flags.define_flags() 29 | tf.app.run() 30 | -------------------------------------------------------------------------------- /BEGAN/src/utils/data_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import glob 4 | import numpy as np 5 | import tensorflow as tf 6 | sys.path.append("../utils") 7 | import logging_utils as lu 8 | 9 | 10 | def normalize_image(image): 11 | 12 | image = tf.cast(image, tf.float32) / 255. 13 | image = (image - 0.5) / 0.5 14 | return image 15 | 16 | 17 | def unnormalize_image(image, name=None): 18 | 19 | image = (image * 0.5 + 0.5) * 255. 20 | image = tf.cast(image, tf.uint8, name=name) 21 | return image 22 | 23 | 24 | def read_celebA(): 25 | 26 | FLAGS = tf.app.flags.FLAGS 27 | 28 | list_images = glob.glob(os.path.join(FLAGS.celebA_path, "*.jpg")) 29 | 30 | # Read each JPEG file 31 | with tf.device('/cpu:0'): 32 | 33 | reader = tf.WholeFileReader() 34 | filename_queue = tf.train.string_input_producer(list_images) 35 | key, value = reader.read(filename_queue) 36 | channels = FLAGS.channels 37 | image = tf.image.decode_jpeg(value, channels=channels, name="dataset_image") 38 | image.set_shape([None, None, channels]) 39 | 40 | # Crop and other random augmentations 41 | image = tf.image.random_flip_left_right(image) 42 | image = tf.image.random_saturation(image, .95, 1.05) 43 | image = tf.image.random_brightness(image, .05) 44 | image = tf.image.random_contrast(image, .95, 1.05) 45 | 46 | # Center crop 47 | image = tf.image.central_crop(image, FLAGS.central_fraction) 48 | 49 | # Resize 50 | image = tf.image.resize_images(image, (FLAGS.img_size, FLAGS.img_size), method=tf.image.ResizeMethod.AREA) 51 | 52 | # Normalize 53 | image = normalize_image(image) 54 | 55 | # Format image to correct ordering 56 | if FLAGS.data_format == "NCHW": 57 | image = tf.transpose(image, (2,0,1)) 58 | 59 | # Using asynchronous queues 60 | img_batch = tf.train.batch([image], 61 | batch_size=FLAGS.batch_size, 62 | num_threads=FLAGS.num_threads, 63 | capacity=2 * FLAGS.num_threads * FLAGS.batch_size, 64 | name='X_real_input') 65 | 66 | return img_batch 67 | 68 | 69 | def sample_batch(X, batch_size): 70 | 71 | idx = np.random.choice(X.shape[0], batch_size, replace=False) 72 | return X[idx] 73 | 74 | 75 | def manage_queues(sess): 76 | 77 | coord = tf.train.Coordinator() 78 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 79 | 80 | lu.print_queues() 81 | 82 | return coord, threads 83 | -------------------------------------------------------------------------------- /BEGAN/src/utils/inference_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import random 4 | import numpy as np 5 | import tensorflow as tf 6 | sys.path.append("../utils") 7 | import logging_utils as lu 8 | 9 | FLAGS = tf.app.flags.FLAGS 10 | 11 | 12 | def setup_inference_session(): 13 | 14 | assert FLAGS.run == "inference", "Wrong run flag" 15 | 16 | ################## 17 | # Create session 18 | ################## 19 | config = tf.ConfigProto() 20 | if FLAGS.use_XLA: 21 | config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 22 | sess = tf.Session(config=config) 23 | # Print session parameters and flags 24 | lu.print_session("Inference") 25 | 26 | ############################################### 27 | # Initialize all RNGs with a deterministic seed 28 | ############################################### 29 | # with sess.graph.as_default(): 30 | # tf.set_random_seed(FLAGS.random_seed) 31 | 32 | # random.seed(FLAGS.random_seed) 33 | # np.random.seed(FLAGS.random_seed) 34 | 35 | ######################################## 36 | # Check models directory, inference mode 37 | ######################################## 38 | assert tf.gfile.Exists(FLAGS.model_dir), "Model directory (%s) does not exist" % FLAGS.model_dir 39 | list_files = tf.gfile.Glob(os.path.join(FLAGS.model_dir, "*")) 40 | assert len(list_files) > 0, "Model directory (%s) is empty" % FLAGS.model_dir 41 | 42 | return sess 43 | 44 | 45 | def restore_session(): 46 | 47 | # Get checkpoint 48 | checkpoint = tf.train.get_checkpoint_state(FLAGS.model_dir) 49 | checkpoint_path = checkpoint.model_checkpoint_path 50 | lu.print_checkpoint(checkpoint) 51 | 52 | # Retrieve the meta graph 53 | meta_graph = tf.train.import_meta_graph(checkpoint_path + '.meta') 54 | # Get default graph 55 | graph = tf.get_default_graph() 56 | lu.print_meta_graph(checkpoint_path + '.meta') 57 | 58 | # Setup sessions 59 | sess = setup_inference_session() 60 | # Restore from the checkpoint 61 | meta_graph.restore(sess, checkpoint_path) 62 | lu.print_restore() 63 | 64 | return sess, graph 65 | -------------------------------------------------------------------------------- /BEGAN/src/utils/logging_utils.py: -------------------------------------------------------------------------------- 1 | from colorama import init 2 | from colorama import Fore, Back, Style 3 | import tensorflow as tf 4 | from terminaltables import SingleTable 5 | from natsort import natsorted 6 | 7 | 8 | def print_table(TABLE_DATA): 9 | 10 | table_instance = SingleTable(TABLE_DATA, "") 11 | table_instance.justify_columns[2] = 'right' 12 | print(table_instance.table) 13 | print 14 | 15 | 16 | def print_bright(s): 17 | 18 | init() 19 | print(Style.BRIGHT + s + Style.RESET_ALL) 20 | 21 | 22 | def print_green(info, value=""): 23 | 24 | print(Fore.GREEN + "[%s] " % info + Style.RESET_ALL + str(value)) 25 | 26 | 27 | def print_red(info, value=""): 28 | 29 | print(Fore.RED + "[%s] " % info + Style.RESET_ALL + str(value)) 30 | 31 | 32 | def print_session(session_type): 33 | 34 | FLAGS = tf.app.flags.FLAGS 35 | 36 | print_bright("\nSetting up TF %s session:" % session_type) 37 | for key in natsorted(FLAGS.__dict__["__flags"].keys()): 38 | if "dir" not in key: 39 | print_green(key, FLAGS.__dict__["__flags"][key]) 40 | 41 | 42 | def print_directories(list_delete, list_create): 43 | 44 | print_bright("\nConfiguring directories:") 45 | 46 | for d in list_delete: 47 | print_red("Deleting", d) 48 | 49 | for d in list_create: 50 | print_green("Creating", d) 51 | 52 | 53 | def print_initialize(): 54 | 55 | print_bright("\nInitialization:") 56 | print_green("Created session saver") 57 | print_green("Ran init ops") 58 | 59 | 60 | def print_summaries(): 61 | 62 | print_bright("\nSummaries:") 63 | list_summaries = tf.get_collection(tf.GraphKeys.SUMMARIES) 64 | for t in list_summaries: 65 | print_green(t.name) 66 | 67 | 68 | def print_queues(): 69 | 70 | print_bright("\nQueues:") 71 | print_green("Created coordinator") 72 | print_green("Started queue runner") 73 | 74 | 75 | def print_check_data(out, list_data): 76 | 77 | print 78 | TABLE_DATA = (('Variable Name', 'Shape', "Min value", "Max value"),) 79 | for o, t in zip(out, list_data): 80 | TABLE_DATA += (tuple([t.name, str(o.shape), "%.3g" % o.min(), "%.3g" % o.max()]),) 81 | print_table(TABLE_DATA) 82 | 83 | 84 | def print_checkpoint(checkpoint): 85 | 86 | print_bright("\nCheckpointing:") 87 | print_green(checkpoint.model_checkpoint_path) 88 | 89 | 90 | def print_meta_graph(meta_graph_path): 91 | 92 | print_bright("\nMetagraph:") 93 | print_green("Loaded " + meta_graph_path) 94 | 95 | 96 | def print_restore(): 97 | 98 | print_bright("\nRestored session") 99 | -------------------------------------------------------------------------------- /BEGAN/src/utils/losses.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | def mae(pred, target, name='mae'): 5 | 6 | return tf.reduce_mean(tf.abs(pred - target), name=name) 7 | 8 | 9 | def mse(pred, target, name='mse'): 10 | 11 | return tf.reduce_mean(tf.square(pred - target), name=name) 12 | 13 | 14 | def rmse(pred, target, name='rmse'): 15 | 16 | return tf.sqrt(tf.reduce_mean(tf.square(pred - target), name=name)) 17 | 18 | 19 | def binary_cross_entropy_with_logits(pred, target): 20 | 21 | return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=target)) 22 | 23 | 24 | def wasserstein(pred, target): 25 | 26 | return tf.reduce_mean(pred * target) 27 | 28 | 29 | def sparse_cross_entropy(pred, target): 30 | 31 | return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=pred, labels=target)) 32 | 33 | 34 | def cross_entropy(pred, target): 35 | 36 | return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=target)) 37 | 38 | 39 | def KL(mu, logsigma): 40 | # KL Divergence between latents and Standard Normal prior 41 | kl_div = -0.5 * tf.reduce_mean(1 + 2 * logsigma - tf.sqrt(mu) - tf.exp(2 * logsigma)) 42 | 43 | return kl_div 44 | -------------------------------------------------------------------------------- /BEGAN/src/utils/training_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import random 4 | import numpy as np 5 | import tensorflow as tf 6 | sys.path.append("../utils") 7 | import logging_utils as lu 8 | 9 | FLAGS = tf.app.flags.FLAGS 10 | 11 | 12 | def setup_training_session(): 13 | 14 | assert FLAGS.run == "train", "Wrong run flag" 15 | 16 | ################## 17 | # Create session 18 | ################## 19 | config = tf.ConfigProto() 20 | config.gpu_options.allow_growth = True 21 | if FLAGS.use_XLA: 22 | config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 23 | sess = tf.Session(config=config) 24 | # Print session parameters and flags 25 | lu.print_session("Training") 26 | 27 | ############################################### 28 | # Initialize all RNGs with a deterministic seed 29 | ############################################### 30 | with sess.graph.as_default(): 31 | tf.set_random_seed(FLAGS.random_seed) 32 | 33 | random.seed(FLAGS.random_seed) 34 | np.random.seed(FLAGS.random_seed) 35 | 36 | ################################## 37 | # Setup directories, training mode 38 | ################################## 39 | list_delete = [] # keep track of directory creation/deletion 40 | list_create = [] # keep track of directory creation/deletion 41 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 42 | # Delete directory and its contents if it exists 43 | if tf.gfile.Exists(d): 44 | tf.gfile.DeleteRecursively(d) 45 | list_delete.append(d) 46 | # Recreate directory 47 | tf.gfile.MakeDirs(d) 48 | list_create.append(d) 49 | # Print directory creation / deletion 50 | lu.print_directories(list_delete, list_create) 51 | 52 | return sess 53 | 54 | 55 | def initialize_session(sess): 56 | 57 | saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep) 58 | 59 | init_op = tf.group(tf.global_variables_initializer(), 60 | tf.local_variables_initializer()) 61 | sess.run(init_op) 62 | 63 | lu.print_initialize() 64 | 65 | return saver 66 | 67 | 68 | def add_gradient_summary(list_gradvar): 69 | # Add summary for gradients 70 | for g,v in list_gradvar: 71 | if g is not None: 72 | tf.summary.histogram(v.name + "/gradient", g) 73 | 74 | 75 | def manage_summaries(sess): 76 | 77 | writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) 78 | 79 | lu.print_summaries() 80 | 81 | return writer 82 | 83 | 84 | def check_data(out, list_data): 85 | 86 | lu.print_check_data(out, list_data) 87 | -------------------------------------------------------------------------------- /BEGAN/src/utils/visualization_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pylab as plt 5 | import matplotlib as mp 6 | 7 | FLAGS = tf.app.flags.FLAGS 8 | 9 | 10 | def format_plot(X, epoch=None, title=None, figsize=(15, 10)): 11 | 12 | plt.figure(figsize=figsize) 13 | 14 | if X.shape[-1] == 1: 15 | plt.imshow(X[:, :, 0], cmap="gray") 16 | else: 17 | plt.imshow(X) 18 | 19 | plt.axis("off") 20 | plt.gca().xaxis.set_major_locator(mp.ticker.NullLocator()) 21 | plt.gca().yaxis.set_major_locator(mp.ticker.NullLocator()) 22 | 23 | if epoch is not None and title is None: 24 | save_path = os.path.join(FLAGS.fig_dir, "current_batch_%s.png" % epoch) 25 | elif epoch is not None and title is not None: 26 | save_path = os.path.join(FLAGS.fig_dir, "%s_%s.png" % (title, epoch)) 27 | elif title is not None: 28 | save_path = os.path.join(FLAGS.fig_dir, "%s.png" % title) 29 | plt.savefig(save_path, bbox_inches='tight', pad_inches=0) 30 | plt.clf() 31 | plt.close() 32 | 33 | 34 | def save_image(X1, X2, e=None, title=None): 35 | 36 | if FLAGS.data_format == "NCHW": 37 | X1 = X1.transpose((0, 2, 3, 1)) 38 | X2 = X2.transpose((0, 2, 3, 1)) 39 | 40 | Xup = X1[:32] 41 | Xdown = X2[:32] 42 | 43 | n_cols = 8 44 | 45 | list_rows_f = [] 46 | for i in range(Xup.shape[0] // n_cols): 47 | Xrow = np.concatenate([Xup[k] for k in range(n_cols * i, n_cols * (i + 1))], axis=1) 48 | list_rows_f.append(Xrow) 49 | list_rows_r = [] 50 | for i in range(Xup.shape[0] // n_cols): 51 | Xrow = np.concatenate([Xdown[k] for k in range(n_cols * i, n_cols * (i + 1))], axis=1) 52 | list_rows_r.append(Xrow) 53 | 54 | Xup = np.concatenate(list_rows_f, axis=0) 55 | Xdown = np.concatenate(list_rows_r, axis=0) 56 | 57 | X_ones = 255 * np.ones_like(Xup, dtype=np.uint8) 58 | X_ones = X_ones[:5, :, :] 59 | 60 | X = np.concatenate((Xup, X_ones, Xdown), axis=0) 61 | 62 | format_plot(X, epoch=e, title=title) 63 | -------------------------------------------------------------------------------- /Colorful/README.md: -------------------------------------------------------------------------------- 1 | # Colorful 2 | 3 | Keras implementation of [Colorful Image Colorization](https://arxiv.org/abs/1603.08511) by Richard Zhang, Phillip Isola and Alexei A. Efros 4 | 5 | The technique is applied on the [CelebA dataset](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html) with minor modifications. 6 | 7 | # Requirements 8 | 9 | ## python modules 10 | 11 | - scikit-learn==0.19.1 12 | - scikit-image==0.13.1 13 | - tqdm==4.17.0 14 | - opencv_python==3.3.0.10 15 | - numpy==1.13.3 16 | - matplotlib==2.0.2 17 | - Keras==2.0.8 18 | - Theano==0.9.0 or Tensorflow==1.3.0 19 | - h5py==2.7.0 20 | - parmap==1.5.1 21 | - scipy==1.0.0 22 | 23 | ## System requirements 24 | 25 | - Nvidia GPU with at least 2GB RAM 26 | - At least 4GB RAM (when using the on_demand option for training) 27 | 28 | The settings above should work well enough for small image size (32 x 32). 29 | Above that, better GPU and more RAM are required. 30 | 31 | # Part 1. Processing the data 32 | 33 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful/src/data). 34 | 35 | # Part 2. Running the code 36 | 37 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful/src/model) 38 | 39 | # Part 3. Example results 40 | 41 | ![figure](./figures/celeba_colorized_small.png) 42 | 43 | For each triplet: 44 | 45 | - First column is the original 46 | - Second column is the B&W version 47 | - Last column is the colorized output 48 | 49 | # Part 4. Live colorization with webcam 50 | 51 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful/src/app) -------------------------------------------------------------------------------- /Colorful/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /Colorful/data/processed/README.md: -------------------------------------------------------------------------------- 1 | # Repository for processed data -------------------------------------------------------------------------------- /Colorful/data/processed/pts_in_hull.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Colorful/data/processed/pts_in_hull.npy -------------------------------------------------------------------------------- /Colorful/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /Colorful/figures/README.md: -------------------------------------------------------------------------------- 1 | # Figures repository -------------------------------------------------------------------------------- /Colorful/figures/celeba_colorized_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Colorful/figures/celeba_colorized_small.png -------------------------------------------------------------------------------- /Colorful/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful/src/model) for training/evaluating scripts 8 | - [app](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful/src/app) for the webcam app -------------------------------------------------------------------------------- /Colorful/src/app/README.md: -------------------------------------------------------------------------------- 1 | # Colorizer app 2 | 3 | ## Usage 4 | 5 | `python webcam_colorizer.py` 6 | 7 | positional arguments: 8 | data_file Path to HDF5 containing the data 9 | epoch Epoch of saved weights 10 | 11 | optional arguments: 12 | 13 | -h, --help show this help message and exit 14 | --model_name MODEL_NAME Model name. Choose simple_colorful or colorful 15 | --T T Temperature to change color balance. If T = 1: desaturated. If T~0 vivid 16 | --out_h OUT_H Width of ouput image 17 | --out_w OUT_W Height of ouput image 18 | --video_path VIDEO_PATH Path to B&W video to colorize 19 | -------------------------------------------------------------------------------- /Colorful/src/data/README.md: -------------------------------------------------------------------------------- 1 | # Building the data 2 | 3 | # Step 1. Download CelebA 4 | 5 | - Go to http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 6 | - In the Downloads section, select Align&Cropped images. 7 | - In the dropbox page that follows, download the Anno, Eval and Img folders. 8 | - Copy these folders to `Colorful/data/raw`. 9 | - Extract the zip files. 10 | 11 | You should have the following folder structure: 12 | 13 | ├── Anno 14 | ├── list_attr_celeba.txt 15 | ├── list_bbox_celeba.txt 16 | ├── list_landmarks_align_celeba.txt 17 | ├── list_landmarks_celeba.txt 18 | ├── Eval 19 | ├──list_eval_partition.txt 20 | ├── img_align_celeba 21 | ├──lots of images 22 | 23 | 24 | # Step 2. Build HDF5 CelebA dataset 25 | 26 | `python make_dataset.py` 27 | 28 | positional arguments: 29 | 30 | list_datasets List of dataset names. Choose training, validation or 31 | test 32 | optional arguments: 33 | -h, --help show this help message and exit 34 | --img_size IMG_SIZE Desired Width == Height 35 | --do_plot DO_PLOT Whether to visualize statistics when computing color 36 | prior 37 | 38 | 39 | **Example:** 40 | 41 | `python make_dataset.py --img_size 64` -------------------------------------------------------------------------------- /Colorful/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training and evaluating 2 | 3 | ## Training 4 | 5 | `python main.py` 6 | 7 | positional arguments: 8 | 9 | mode Choose train or eval 10 | data_file Path to HDF5 containing the data 11 | 12 | optional arguments: 13 | 14 | -h, --help show this help message and exit 15 | --model_name MODEL_NAME 16 | Model name. Choose simple_colorful or colorful 17 | --training_mode TRAINING_MODE 18 | Training mode. Choose in_memory to load all the data 19 | in memory and train.Choose on_demand to load batches 20 | from disk at each step 21 | --batch_size BATCH_SIZE 22 | Batch size 23 | --n_batch_per_epoch N_BATCH_PER_EPOCH 24 | Number of training epochs 25 | --nb_epoch NB_EPOCH Number of batches per epoch 26 | --nb_resblocks NB_RESBLOCKS 27 | Number of residual blocks for simple model 28 | --nb_neighbors NB_NEIGHBORS 29 | Number of nearest neighbors for soft encoding 30 | --epoch EPOCH Epoch at which weights were saved for evaluation 31 | --T T Temperature to change color balance in evaluation 32 | phase.If T = 1: desaturated. If T~0 vivid 33 | 34 | **Example:** 35 | 36 | `python main.py train ../../data/processed/CelebA_32_data.h5` 37 | 38 | ### Expected outputs: 39 | 40 | - Create a directory in Colorful/models where weights are saved 41 | - Create a directory in Colorful/figures where figures are saved 42 | - Plot examples of the colorization results at each epoch 43 | - Plot model architecture. 44 | - Save model weights every few epochs 45 | 46 | 47 | ## Evaluating 48 | 49 | positional arguments: 50 | 51 | mode Choose train or eval 52 | data_file Path to HDF5 containing the data 53 | 54 | optional arguments: 55 | 56 | -h, --help show this help message and exit 57 | --model_name MODEL_NAME 58 | Model name. Choose simple_colorful or colorful 59 | --training_mode TRAINING_MODE 60 | Training mode. Choose in_memory to load all the data 61 | in memory and train.Choose on_demand to load batches 62 | from disk at each step 63 | --batch_size BATCH_SIZE 64 | Batch size 65 | --n_batch_per_epoch N_BATCH_PER_EPOCH 66 | Number of training epochs 67 | --nb_epoch NB_EPOCH Number of batches per epoch 68 | --nb_resblocks NB_RESBLOCKS 69 | Number of residual blocks for simple model 70 | --nb_neighbors NB_NEIGHBORS 71 | Number of nearest neighbors for soft encoding 72 | --epoch EPOCH Epoch at which weights were saved for evaluation 73 | --T T Temperature to change color balance in evaluation 74 | phase.If T = 1: desaturated. If T~0 vivid 75 | 76 | **Example:** 77 | 78 | `python main.py eval ../../data/processed/CelebA_64_data.h5 --epoch 10` 79 | 80 | ### Expected outputs: 81 | 82 | Randomly sample images from the validation set and plot the color, colorized and b&w version. -------------------------------------------------------------------------------- /Colorful/src/model/eval_colorful.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import numpy as np 4 | import models_colorful 5 | import sklearn.neighbors as nn 6 | # Utils 7 | sys.path.append("../utils") 8 | import batch_utils 9 | import general_utils 10 | 11 | 12 | def eval(**kwargs): 13 | 14 | data_file = kwargs["data_file"] 15 | model_name = kwargs["model_name"] 16 | epoch = kwargs["epoch"] 17 | T = kwargs["T"] 18 | batch_size = kwargs["batch_size"] 19 | nb_neighbors = kwargs["nb_neighbors"] 20 | 21 | img_size = int(os.path.basename(data_file).split("_")[1]) 22 | 23 | # Create a batch generator for the color data 24 | DataGen = batch_utils.DataGenerator(data_file, 25 | batch_size=batch_size, 26 | dset="training") 27 | c, h, w = DataGen.get_config()["data_shape"][1:] 28 | 29 | # Load the array of quantized ab value 30 | q_ab = np.load("../../data/processed/pts_in_hull.npy") 31 | nb_q = q_ab.shape[0] 32 | # Fit a NN to q_ab 33 | nn_finder = nn.NearestNeighbors(n_neighbors=nb_neighbors, algorithm='ball_tree').fit(q_ab) 34 | 35 | # Load the color prior factor that encourages rare colors 36 | prior_factor = np.load("../../data/processed/CelebA_%s_prior_factor.npy" % img_size) 37 | 38 | # Load colorization model 39 | color_model = models_colorful.load(model_name, nb_q, (1, h, w), batch_size) 40 | color_model.load_weights("../../models/%s/%s_weights_epoch%s.h5" % 41 | (model_name, model_name, epoch)) 42 | 43 | for batch in DataGen.gen_batch(nn_finder, nb_q, prior_factor): 44 | 45 | X_batch_black, X_batch_color, Y_batch = batch 46 | 47 | general_utils.plot_batch_eval(color_model, q_ab, X_batch_black, X_batch_color, 48 | batch_size, h, w, nb_q, T) 49 | -------------------------------------------------------------------------------- /Colorful/src/model/main.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import train_colorful 3 | import eval_colorful 4 | 5 | 6 | def launch_training(**kwargs): 7 | 8 | # Launch training 9 | train_colorful.train(**kwargs) 10 | 11 | 12 | def launch_eval(**kwargs): 13 | 14 | # Launch training 15 | eval_colorful.eval(**kwargs) 16 | 17 | 18 | if __name__ == "__main__": 19 | 20 | parser = argparse.ArgumentParser(description='Train model') 21 | parser.add_argument('mode', type=str, help="Choose train or eval") 22 | parser.add_argument('data_file', type=str, help="Path to HDF5 containing the data") 23 | parser.add_argument('--training_mode', default="in_memory", type=str, 24 | help=('Training mode. Choose in_memory to load all the data in memory and train.' 25 | 'Choose on_demand to load batches from disk at each step')) 26 | parser.add_argument('--batch_size', default=32, type=int, help='Batch size') 27 | parser.add_argument('--n_batch_per_epoch', default=100, type=int, help="Number of training epochs") 28 | parser.add_argument('--nb_epoch', default=400, type=int, help="Number of batches per epoch") 29 | parser.add_argument('--nb_resblocks', default=3, type=int, help="Number of residual blocks for simple model") 30 | parser.add_argument('--nb_neighbors', default=10, type=int, help="Number of nearest neighbors for soft encoding") 31 | parser.add_argument('--epoch', default=0, type=int, help="Epoch at which weights were saved for evaluation") 32 | parser.add_argument('--T', default=0.1, type=float, 33 | help=("Temperature to change color balance in evaluation phase." 34 | "If T = 1: desaturated. If T~0 vivid")) 35 | 36 | args = parser.parse_args() 37 | 38 | # Set default params 39 | d_params = {"data_file": args.data_file, 40 | "batch_size": args.batch_size, 41 | "n_batch_per_epoch": args.n_batch_per_epoch, 42 | "nb_epoch": args.nb_epoch, 43 | "nb_resblocks":args.nb_resblocks, 44 | "training_mode": args.training_mode, 45 | "nb_neighbors": args.nb_neighbors, 46 | "epoch": args.epoch, 47 | "T": args.T 48 | } 49 | 50 | assert args.mode in ["train", "eval"] 51 | assert args.training_mode in ["in_memory", "on_demand"] 52 | 53 | if args.mode == "train": 54 | # Launch training 55 | launch_training(**d_params) 56 | 57 | if args.mode == "eval": 58 | # Launch evaluation 59 | launch_eval(**d_params) 60 | -------------------------------------------------------------------------------- /Colorful/src/utils/batch_utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Colorful/src/utils/batch_utils.pyc -------------------------------------------------------------------------------- /Colorful/src/utils/general_utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Colorful/src/utils/general_utils.pyc -------------------------------------------------------------------------------- /DFI/README.md: -------------------------------------------------------------------------------- 1 | # DFI 2 | 3 | Keras implementation of [Deep Feature Interpolation for Image Content Changes](https://arxiv.org/pdf/1611.05507v1.pdf) by Paul Upchurch, Jacob Gardner, Kavita Bala, Robert Pless, Noah Snavely, Kilian Weinberger 4 | 5 | The technique is applied on the [LFW](http://vis-www.cs.umass.edu/lfw/) with minor modifications. 6 | 7 | # Requirements 8 | 9 | ## python modules 10 | 11 | - keras, theano backend 12 | - h5py 13 | - matplotlib 14 | - scikit-learn 15 | - pandas 16 | - opencv 3 17 | - numpy 18 | - scipy 19 | - tqdm 20 | - parmap 21 | 22 | ## keras repo 23 | 24 | Make sure to clone this repo: https://github.com/fchollet/deep-learning-models for easy access to the VGG19 model. 25 | 26 | 27 | # Part 1. Processing the data 28 | 29 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DFI/src/data). 30 | 31 | # Part 2. Running the code 32 | 33 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DFI/src/model) 34 | 35 | # Part 3. Example results 36 | 37 | ![figure](./figures/example_figures/example.png) 38 | 39 | For each pair: 40 | 41 | - First column is the original 42 | - Second column is the aged original -------------------------------------------------------------------------------- /DFI/data/README.md: -------------------------------------------------------------------------------- 1 | # Data directory -------------------------------------------------------------------------------- /DFI/data/processed/README.md: -------------------------------------------------------------------------------- 1 | #Processed data repository -------------------------------------------------------------------------------- /DFI/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Raw data directory -------------------------------------------------------------------------------- /DFI/figures/README.md: -------------------------------------------------------------------------------- 1 | # Figures directory -------------------------------------------------------------------------------- /DFI/figures/example_figures/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DFI/figures/example_figures/example.png -------------------------------------------------------------------------------- /DFI/src/README.md: -------------------------------------------------------------------------------- 1 | # Source code directory 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DFI/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DFI/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DFI/src/model) for DFI -------------------------------------------------------------------------------- /DFI/src/data/README.md: -------------------------------------------------------------------------------- 1 | # Building the data 2 | 3 | # Step 1. Download LFW 4 | 5 | - Go to http://vis-www.cs.umass.edu/lfw/lfw-deepfunneled.tgz to download aligned LFW images 6 | - Download the attributes file: http://www.cs.columbia.edu/CAVE/databases/pubfig/download/lfw_attributes.txt.. 7 | - Extract the zip files. 8 | - Move the data to data/raw 9 | 10 | In `data/raw` should have the following folder structure: 11 | 12 | ├──lfw_attributes.txt 13 | ├── lfw-deepfunneled 14 | ├──lots of images in folder with person's name 15 | 16 | 17 | # Step 2. Build HDF5 LFW dataset 18 | 19 | `python make_dataset.py` 20 | 21 | positional arguments: 22 | 23 | positional arguments: 24 | 25 | keras_model_path Path to keras deep-learning-models directory 26 | 27 | optional arguments: 28 | 29 | -h, --help show this help message and exit 30 | --img_size IMG_SIZE Desired Width == Height 31 | --do_plot DO_PLOT Whether to visualize saved images 32 | --batch_size BATCH_SIZE 33 | Batch size for VGG predictions 34 | 35 | 36 | **Example:** 37 | 38 | `python make_dataset.py /home/user/GitHub/deep-learning-models` -------------------------------------------------------------------------------- /DFI/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training and evaluating 2 | 3 | ## Training 4 | 5 | `python main.py` 6 | 7 | 8 | positional arguments: 9 | 10 | keras_model_path Path to keras deep-learning-models directory 11 | 12 | optional arguments: 13 | 14 | -h, --help show this help message and exit 15 | --data_file DATA_FILE 16 | Path to HDF5 containing the data 17 | --attributes_file ATTRIBUTES_FILE 18 | Path to csv file containing the attributes 19 | --nb_neighbors NB_NEIGHBORS 20 | Number of nearest neighbors to compute VGG 21 | representation 22 | --alpha ALPHA Interpolation coefficient 23 | --weight_reverse_mapping WEIGHT_REVERSE_MAPPING 24 | Weight of reverse mapping loss 25 | --weight_total_variation WEIGHT_TOTAL_VARIATION 26 | Weight of total variation loss 27 | --normalize_w NORMALIZE_W 28 | Whether to normalize w 29 | 30 | **Example:** 31 | 32 | `python main.py /home/user/GitHub/deep-learning-models` 33 | 34 | ### Expected outputs: 35 | 36 | - Create a copy of the source image in `figures` 37 | - Saves the generated image every few iteration in `figures` 38 | 39 | -------------------------------------------------------------------------------- /DFI/src/model/dfi.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DFI/src/model/dfi.pyc -------------------------------------------------------------------------------- /DFI/src/model/main.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import dfi 3 | 4 | 5 | def launch_dfi(**kwargs): 6 | 7 | # Launch training 8 | dfi.launch_dfi(**kwargs) 9 | 10 | 11 | if __name__ == "__main__": 12 | 13 | parser = argparse.ArgumentParser(description='Train model') 14 | parser.add_argument("keras_model_path", type=str, help="Path to keras deep-learning-models directory") 15 | parser.add_argument('--data_file', type=str, default="../../data/processed/lfw_200_data.h5", 16 | help="Path to HDF5 containing the data") 17 | parser.add_argument('--attributes_file', type=str, default="../../data/processed/lfw_processed_attributes.csv", 18 | help="Path to csv file containing the attributes") 19 | parser.add_argument('--nb_neighbors', default=100, type=int, 20 | help="Number of nearest neighbors to compute VGG representation") 21 | parser.add_argument('--alpha', default=4, type=float, help="Interpolation coefficient") 22 | parser.add_argument('--weight_reverse_mapping', default=1., type=float, help="Weight of reverse mapping loss") 23 | parser.add_argument('--weight_total_variation', default=1E3, type=float, help="Weight of total variation loss") 24 | parser.add_argument('--normalize_w', default=False, type=bool, help="Whether to normalize w") 25 | 26 | args = parser.parse_args() 27 | 28 | # Set default params 29 | d_params = {"keras_model_path": args.keras_model_path, 30 | "data_file": args.data_file, 31 | "attributes_file": args.attributes_file, 32 | "nb_neighbors": args.nb_neighbors, 33 | "alpha": args.alpha, 34 | "weight_total_variation":args.weight_total_variation, 35 | "weight_reverse_mapping": args.weight_reverse_mapping, 36 | "normalize_w": args.normalize_w 37 | } 38 | 39 | launch_dfi(**d_params) 40 | -------------------------------------------------------------------------------- /DFI/src/utils/general_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | from keras import backend as K 4 | 5 | 6 | def remove_files(files): 7 | """ 8 | Remove files from disk 9 | 10 | args: files (str or list) remove all files in 'files' 11 | """ 12 | 13 | if isinstance(files, (list, tuple)): 14 | for f in files: 15 | if os.path.isfile(os.path.expanduser(f)): 16 | os.remove(f) 17 | elif isinstance(files, str): 18 | if os.path.isfile(os.path.expanduser(files)): 19 | os.remove(files) 20 | 21 | 22 | def create_dir(dirs): 23 | """ 24 | Create directory 25 | 26 | args: dirs (str or list) create all dirs in 'dirs' 27 | """ 28 | 29 | if isinstance(dirs, (list, tuple)): 30 | for d in dirs: 31 | if not os.path.exists(os.path.expanduser(d)): 32 | os.makedirs(d) 33 | elif isinstance(dirs, str): 34 | if not os.path.exists(os.path.expanduser(dirs)): 35 | os.makedirs(dirs) 36 | 37 | 38 | def setup_logging(model_name): 39 | 40 | model_dir = "../../models" 41 | # Output path where we store experiment log and weights 42 | model_dir = os.path.join(model_dir, model_name) 43 | 44 | fig_dir = "../../figures" 45 | 46 | # Create if it does not exist 47 | create_dir([model_dir, fig_dir]) 48 | 49 | 50 | def preprocess_input(x, dim_ordering='default'): 51 | if dim_ordering == 'default': 52 | dim_ordering = K.image_dim_ordering() 53 | assert dim_ordering in {'tf', 'th'} 54 | 55 | if dim_ordering == 'th': 56 | x[:, 0, :, :] -= 103.939 57 | x[:, 1, :, :] -= 116.779 58 | x[:, 2, :, :] -= 123.68 59 | # 'RGB'->'BGR' 60 | x = x[:, ::-1, :, :] 61 | else: 62 | x[:, :, :, 0] -= 103.939 63 | x[:, :, :, 1] -= 116.779 64 | x[:, :, :, 2] -= 123.68 65 | # 'RGB'->'BGR' 66 | x = x[:, :, :, ::-1] 67 | return x 68 | 69 | 70 | def deprocess_image(x, img_nrows, img_ncols): 71 | if K.image_dim_ordering() == 'th': 72 | x = x.reshape((3, img_nrows, img_ncols)) 73 | x = x.transpose((1, 2, 0)) 74 | else: 75 | x = x.reshape((img_nrows, img_ncols, 3)) 76 | # Remove zero-center by mean pixel 77 | x[:, :, 0] += 103.939 78 | x[:, :, 1] += 116.779 79 | x[:, :, 2] += 123.68 80 | # 'BGR'->'RGB' 81 | x = x[:, :, ::-1] 82 | x = np.clip(x, 0, 255).astype('uint8') 83 | return x 84 | 85 | 86 | def color_correction(x, img_nrows, img_ncols, X_source): 87 | 88 | # save current generated image 89 | img = deprocess_image(x.copy(), img_nrows, img_ncols).astype(np.float64) 90 | X_sourceT = X_source[0].copy().transpose(1,2,0).astype(np.float64) 91 | # Color correction 92 | for k in range(3): 93 | mean, std = np.mean(X_sourceT[:, :, k]), np.std(X_sourceT[:, :, k]) 94 | img[:, :, k] *= std / np.std(img[:, :, k]) 95 | img[:, :, k] += mean - np.mean(img[:, :, k]) 96 | 97 | img = img.clip(0, 255) 98 | 99 | return img -------------------------------------------------------------------------------- /DeconvNet/README.md: -------------------------------------------------------------------------------- 1 | # Implementation of the Deconvnet algorithm 2 | 3 | N.B. It's still early days for this code. Suggestions for improvement are welcome ! 4 | 5 | # Requirements 6 | 7 | - Keras==2.0.8 8 | - numpy==1.13.3 9 | - opencv_python==3.3.0.10 10 | - matplotlib==2.0.2 11 | - Theano==0.9.0 12 | 13 | # Configure 14 | 15 | - Unzip the images in `./Data/Img` or add your own 16 | - Download the [VGG16 keras weights](https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3) and put them in `./Data` 17 | 18 | # How to use 19 | 20 | - Open VGG_deconv.py in your text editor 21 | 22 | ## Get the deconvolution result of random image, for a specific layer and feature map 23 | 24 | - Go to step 4, set the flag to `True` 25 | - Specify your target_layer (keras default naming, i.e `convolution2d_5`, `convolution2d_7` etc) 26 | - Specify the index of the filter map you want to look at 27 | - Specify num_img (must be a perfect square) ex: 25 => plot 5x5 28 | - run `python VGG_deconv.py` 29 | 30 | ## Get the deconvolution of the top 9 images that maximally activate a given target_layer/map 31 | 32 | - Go to step 1, set the flag to `True` 33 | - Specify which layer you want in `d_act` e.g. `d_act = {"convolution2d_13": {}}` 34 | - By default, the algorithm will look for the images maximally activating the first 10 feature maps of this layer 35 | - Go to step 2 and set the flag to `True` 36 | - Then run `python VGG_deconv.py`. You may have to change the batch size depending on your GPU 37 | - It will take a little while to run and find the top 9 images 38 | - Once this step is complete 39 | - Go to step 3 and set the flag to `True` 40 | - Select the layer for which you want to plot the top9 deconv images 41 | - run `python VGG_deconv.py` 42 | 43 | 44 | # Examples 45 | 46 | **N.B. The VGG weights have not been trained on the Kaggle dataset !** 47 | 48 | Below, the top 9 images activating a selection of filters in convolutional layer 10. 49 | Each block is 2x3x3 (1 block: original crop of the image, 1 block: deconvolution result) 50 | Each 2x3x3 block corresponds to a specific feature map. 51 | 52 | ![conv10](./Figures/convolution2d_10.png) 53 | 54 | Below, the top 9 images activating a selection of filters in convolutional layer 13. 55 | Each block is 2x3x3 (1 block: original crop of the image, 1 block: deconvolution result) 56 | Each 2x3x3 block corresponds to a specific feature map. 57 | 58 | ![conv13](./Figures/convolution2d_13.png) 59 | 60 | It's interesting to investigate what each feature map is looking for in the original image. For instance, the top right block of convolutional layer 13 strongly reacts to the steering wheel. -------------------------------------------------------------------------------- /DeconvNet/data/Img.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DeconvNet/data/Img.tar.gz -------------------------------------------------------------------------------- /DeconvNet/figures/convolution2d_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DeconvNet/figures/convolution2d_10.png -------------------------------------------------------------------------------- /DeconvNet/figures/convolution2d_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DeconvNet/figures/convolution2d_13.png -------------------------------------------------------------------------------- /DenseNet/README.md: -------------------------------------------------------------------------------- 1 | # Keras Implementation of DenseNet 2 | 3 | Original idea and implementation: 4 | 5 | [Densely Connected Convolutional Network](http://arxiv.org/abs/1608.06993) 6 | 7 | The figures below are taken from the paper above. 8 | 9 | ![Dense block](./figures/dense_block.jpg) 10 | 11 | Figure 1: A dense block with 5 layers and growth rate 4. 12 | 13 | ![Model scheme](./figures/densenet_scheme.jpg) 14 | 15 | 16 | Figure 2: A deep DenseNet with three dense blocks. 17 | 18 | # Results: 19 | 20 | Below, results obtained with a network of depth 40, growth rate 12, 3 dense blocks, dropout rate of 0.2 and trained with SGD for 276 epochs. 21 | 22 | All convolutional layer have `bias = False` meaning we don't use a bias parameter for them. 23 | 24 | Weight decay (1E-4) is applied to convolutional layers, batch norm parameters and the last dense layer. 25 | 26 | The initial learning rate is 0.1 and the learning rate is divided by 10 after 150 and 225 epochs. 27 | 28 | These settings lead to the same results as [Densely Connected Convolutional Network](http://arxiv.org/abs/1608.06993): 7 % misclassification rate on the CIFAR10 test set without data augmentation. 29 | 30 | ![Model scheme](./figures/cifar10_results.png) 31 | 32 | # Running a CIFAR10 experiment 33 | 34 | python run_cifar10.py 35 | 36 | optional arguments: 37 | ## Usage guide: 38 | 39 | python run_cifar10.py 40 | 41 | optional arguments: 42 | 43 | -h, --help show this help message and exit 44 | --batch_size BATCH_SIZE Batch size 45 | --nb_epoch NB_EPOCH Number of epochs 46 | --depth DEPTH Network depth 47 | --nb_dense_block NB_DENSE_BLOCK Number of dense blocks 48 | --nb_filter NB_FILTER Initial number of conv filters 49 | --growth_rate GROWTH_RATE Number of new filters added by conv layers 50 | --dropout_rate DROPOUT_RATE Dropout rate 51 | --learning_rate LEARNING_RATE Learning rate 52 | --weight_decay WEIGHT_DECAY L2 regularization on weights 53 | --plot_architecture PLOT_ARCHITECTURE Save a plot of the network architecture 54 | 55 | 56 | # Architecture 57 | 58 | With two dense blocks and 2 convolution operations within each block, the model looks like this: 59 | 60 | ![Model archi](./figures/densenet_archi.png) 61 | 62 | 63 | # Requirements 64 | 65 | - numpy==1.13.3 66 | - matplotlib==2.0.2 67 | - Keras==2.0.8 68 | - tensorflow==1.3.0 or theano==0.9.0 -------------------------------------------------------------------------------- /DenseNet/figures/cifar10_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DenseNet/figures/cifar10_results.png -------------------------------------------------------------------------------- /DenseNet/figures/dense_block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DenseNet/figures/dense_block.jpg -------------------------------------------------------------------------------- /DenseNet/figures/densenet_archi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DenseNet/figures/densenet_archi.png -------------------------------------------------------------------------------- /DenseNet/figures/densenet_scheme.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/DenseNet/figures/densenet_scheme.jpg -------------------------------------------------------------------------------- /DenseNet/log/README.md: -------------------------------------------------------------------------------- 1 | # Experiment log directory 2 | 3 | .json files with experiment metadata and losses are stored here -------------------------------------------------------------------------------- /DenseNet/plot_results.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pylab as plt 2 | import json 3 | import numpy as np 4 | 5 | 6 | def plot_cifar10(save=True): 7 | 8 | with open("./log/experiment_log_cifar10.json", "r") as f: 9 | d = json.load(f) 10 | 11 | train_accuracy = 100 * (np.array(d["train_loss"])[:, 1]) 12 | test_accuracy = 100 * (np.array(d["test_loss"])[:, 1]) 13 | 14 | fig = plt.figure() 15 | ax1 = fig.add_subplot(111) 16 | ax1.set_ylabel('Accuracy') 17 | ax1.plot(train_accuracy, color="tomato", linewidth=2, label='train_acc') 18 | ax1.plot(test_accuracy, color="steelblue", linewidth=2, label='test_acc') 19 | ax1.legend(loc=0) 20 | 21 | train_loss = np.array(d["train_loss"])[:, 0] 22 | test_loss = np.array(d["test_loss"])[:, 0] 23 | 24 | ax2 = ax1.twinx() 25 | ax2.set_ylabel('Loss') 26 | ax2.plot(train_loss, '--', color="tomato", linewidth=2, label='train_loss') 27 | ax2.plot(test_loss, '--', color="steelblue", linewidth=2, label='test_loss') 28 | ax2.legend(loc=1) 29 | 30 | ax1.grid(True) 31 | 32 | if save: 33 | fig.savefig('./figures/plot_cifar10.svg') 34 | 35 | plt.show() 36 | plt.clf() 37 | plt.close() 38 | 39 | if __name__ == '__main__': 40 | plot_cifar10() 41 | -------------------------------------------------------------------------------- /Eve/Eve.py: -------------------------------------------------------------------------------- 1 | import keras.backend as K 2 | from keras.optimizers import Optimizer 3 | 4 | 5 | class Eve(Optimizer): 6 | '''Eve optimizer. 7 | 8 | Default parameters follow those provided in the original paper. 9 | 10 | # Arguments 11 | lr: float >= 0. Learning rate. 12 | beta_1/beta_2/beta_3: floats, 0 < beta < 1. Generally close to 1. 13 | small_k/big_K: floats 14 | epsilon: float >= 0. Fuzz factor. 15 | 16 | # References 17 | - [Improving Stochastic Gradient Descent With FeedBack](http://arxiv.org/abs/1611.01505v1.pdf) 18 | ''' 19 | 20 | def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, 21 | beta_3=0.999, small_k=0.1, big_K=10, 22 | epsilon=1e-8, decay=0., **kwargs): 23 | super(Eve, self).__init__(**kwargs) 24 | self.__dict__.update(locals()) 25 | self.iterations = K.variable(0) 26 | self.lr = K.variable(lr) 27 | self.beta_1 = K.variable(beta_1) 28 | self.beta_2 = K.variable(beta_2) 29 | self.beta_3 = K.variable(beta_3) 30 | self.small_k = K.variable(small_k) 31 | self.big_K = K.variable(big_K) 32 | self.decay = K.variable(decay) 33 | self.inital_decay = decay 34 | 35 | def get_updates(self, params, loss): 36 | grads = self.get_gradients(loss, params) 37 | self.updates = [K.update_add(self.iterations, 1)] 38 | 39 | lr = self.lr 40 | if self.inital_decay > 0: 41 | lr *= (1. / (1. + self.decay * self.iterations)) 42 | 43 | t = self.iterations + 1 44 | lr_t = lr * K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t)) 45 | 46 | shapes = [K.get_variable_shape(p) for p in params] 47 | ms = [K.zeros(shape) for shape in shapes] 48 | vs = [K.zeros(shape) for shape in shapes] 49 | f = K.variable(0) 50 | d = K.variable(1) 51 | self.weights = [self.iterations] + ms + vs + [f, d] 52 | 53 | cond = K.greater(t, K.variable(1)) 54 | small_delta_t = K.switch(K.greater(loss, f), self.small_k + 1, 1. / (self.big_K + 1)) 55 | big_delta_t = K.switch(K.greater(loss, f), self.big_K + 1, 1. / (self.small_k + 1)) 56 | 57 | c_t = K.minimum(K.maximum(small_delta_t, loss / (f + self.epsilon)), big_delta_t) 58 | f_t = c_t * f 59 | r_t = K.abs(f_t - f) / (K.minimum(f_t, f)) 60 | d_t = self.beta_3 * d + (1 - self.beta_3) * r_t 61 | 62 | f_t = K.switch(cond, f_t, loss) 63 | d_t = K.switch(cond, d_t, K.variable(1.)) 64 | 65 | self.updates.append(K.update(f, f_t)) 66 | self.updates.append(K.update(d, d_t)) 67 | 68 | for p, g, m, v in zip(params, grads, ms, vs): 69 | m_t = (self.beta_1 * m) + (1. - self.beta_1) * g 70 | v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) 71 | p_t = p - lr_t * m_t / (d_t * K.sqrt(v_t) + self.epsilon) 72 | 73 | self.updates.append(K.update(m, m_t)) 74 | self.updates.append(K.update(v, v_t)) 75 | 76 | new_p = p_t 77 | self.updates.append(K.update(p, new_p)) 78 | return self.updates 79 | 80 | def get_config(self): 81 | config = {'lr': float(K.get_value(self.lr)), 82 | 'beta_1': float(K.get_value(self.beta_1)), 83 | 'beta_2': float(K.get_value(self.beta_2)), 84 | 'beta_3': float(K.get_value(self.beta_3)), 85 | 'small_k': float(K.get_value(self.small_k)), 86 | 'big_K': float(K.get_value(self.big_K)), 87 | 'epsilon': self.epsilon} 88 | base_config = super(Eve, self).get_config() 89 | return dict(list(base_config.items()) + list(config.items())) 90 | -------------------------------------------------------------------------------- /Eve/README.md: -------------------------------------------------------------------------------- 1 | # Implementation of the Eve optimizer 2 | 3 | 4 | This is a keras implementation of [Improving Stochastic Gradient Descent With Feedback](https://arxiv.org/pdf/1611.01505v1.pdf). 5 | 6 | Check [this page](https://github.com/jayanthkoushik/sgd-feedback/blob/master/src/eve.py) for the authors' original implementation of Eve. 7 | 8 | ## Requirements 9 | 10 | - numpy==1.13.3 11 | - matplotlib==2.0.2 12 | - Keras==2.0.8 13 | - Theano==0.9.0 14 | 15 | ## Usage 16 | 17 | You can either import this optimizer: 18 | 19 | from Eve import Eve 20 | Eve_instance = Eve(lr=0.001, beta_1=0.9, beta_2=0.999, 21 | beta_3=0.999, small_k=0.1, big_K=10, 22 | epsilon=1e-8) 23 | 24 | 25 | Or copy the Eve class to keras/optimizers.py and use it as any other optimizer. 26 | 27 | ## Experiments: 28 | 29 | Run: `python main.py` 30 | 31 | 32 | positional arguments: 33 | 34 | list_experiments List of experiment names. E.g. Eve SGD Adam --> will 35 | run a training session with each optimizer 36 | 37 | optional arguments: 38 | 39 | -h, --help show this help message and exit 40 | --model_name MODEL_NAME 41 | Model name: CNN, Big_CNN or FCN 42 | --batch_size BATCH_SIZE 43 | Batch size 44 | --nb_epoch NB_EPOCH Number of epochs 45 | --dataset DATASET Dataset, cifar10, cifar100 or mnist 46 | 47 | 48 | ## Results 49 | 50 | ### CIFAR10 51 | 52 | Running `python main.py SGD Adam Eve --model_name CNN --nb_epoch 200 --dataset cifar10`, we obtained: 53 | 54 | For this experiment, the configuration of the optimizers was: 55 | 56 | opt = SGD(lr=1E-3, decay=0, momentum=0.9, nesterov=True) 57 | opt = Adam(lr=1E-3, decay=0, beta_1=0.9, beta_2=0.999, epsilon=1e-08) 58 | opt = Eve(lr=1E-3, decay=0, beta_1=0.9, beta_2=0.999, beta_3=0.999, small_k=0.1, big_K=10, epsilon=1e-08) 59 | 60 | ![CIFAR10](./figures/cifar10_results_losses.png) 61 | 62 | ![CIFAR10](./figures/cifar10_results_accs.png) 63 | 64 | which shows that for this one experiment (no hyperparameter grid search) the training metric converges faster and to a better optimum with Eve. 65 | 66 | **Dashed lines show the validation error** 67 | 68 | ### CIFAR100 69 | 70 | Running `python main.py SGD Adam Eve --model_name Big_CNN --nb_epoch 100 --dataset cifar100 --nb_epoch 40`, we obtained: 71 | 72 | For this experiment, the configuration of the optimizers was: 73 | 74 | opt = SGD(lr=1E-2, decay=1E-4, momentum=0.9, nesterov=True) 75 | opt = Adam(lr=1E-4, decay=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08) 76 | opt = Eve(lr=1E-4, decay=1E-4, beta_1=0.9, beta_2=0.999, beta_3=0.999, small_k=0.1, big_K=10, epsilon=1e-08) 77 | 78 | Batch normalization was not used. 79 | The parameters (learning rate and learning rate decay) of the optimizers were chosen to match the [best values found by the authors](https://github.com/jayanthkoushik/sgd-feedback/issues/2). 80 | 81 | In this one experiment, Eve again outperforms the other optimizers. 82 | 83 | ![CIFAR100](./figures/cifar100_results_losses.png) 84 | 85 | ![CIFAR100](./figures/cifar100_results_accs.png) 86 | 87 | **Dashed lines show the validation error** -------------------------------------------------------------------------------- /Eve/eval.py: -------------------------------------------------------------------------------- 1 | import json 2 | import glob 3 | import argparse 4 | import matplotlib.pylab as plt 5 | 6 | 7 | def plot_results(list_log, to_plot="losses"): 8 | 9 | list_color = [u'#E24A33', 10 | u'#348ABD', 11 | u'#FBC15E', 12 | u'#777777', 13 | u'#988ED5', 14 | u'#8EBA42', 15 | u'#FFB5B8'] 16 | 17 | plt.figure() 18 | for idx, log in enumerate(list_log): 19 | with open(log, "r") as f: 20 | d = json.load(f) 21 | 22 | experiment_name = d["experiment_name"] 23 | color = list_color[idx] 24 | 25 | plt.plot(d["train_%s" % to_plot], 26 | color=color, 27 | linewidth=3, 28 | label="Train %s" % experiment_name) 29 | plt.plot(d["val_%s" % to_plot], 30 | color=color, 31 | linestyle="--", 32 | linewidth=3,) 33 | plt.ylabel(to_plot, fontsize=20) 34 | if to_plot == "losses": 35 | plt.yscale("log") 36 | if to_plot == "accs": 37 | plt.ylim([0, 1.1]) 38 | plt.xlabel("Number of epochs", fontsize=20) 39 | plt.title("%s experiment" % dataset, fontsize=22) 40 | plt.legend(loc="best") 41 | plt.tight_layout() 42 | plt.savefig("./figures/%s_results_%s.png" % (dataset, to_plot)) 43 | plt.show() 44 | 45 | 46 | if __name__ == '__main__': 47 | 48 | list_log = glob.glob("./log/*.json") 49 | 50 | parser = argparse.ArgumentParser(description='Plot results of experiments') 51 | parser.add_argument('dataset', type=str, 52 | help='name of the dataset: cifar10, cifar100 or mnist') 53 | parser.add_argument('--to_plot', type=str, default="losses", 54 | help='metric to plot: losses (log loss) or accs (accuracies)') 55 | args = parser.parse_args() 56 | dataset = args.dataset 57 | 58 | list_log = [l for l in list_log if dataset in l] 59 | 60 | plot_results(list_log, to_plot=args.to_plot) 61 | -------------------------------------------------------------------------------- /Eve/figures/cifar100_results_accs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Eve/figures/cifar100_results_accs.png -------------------------------------------------------------------------------- /Eve/figures/cifar100_results_losses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Eve/figures/cifar100_results_losses.png -------------------------------------------------------------------------------- /Eve/figures/cifar10_results_accs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Eve/figures/cifar10_results_accs.png -------------------------------------------------------------------------------- /Eve/figures/cifar10_results_losses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Eve/figures/cifar10_results_losses.png -------------------------------------------------------------------------------- /Eve/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | import train 3 | import argparse 4 | 5 | 6 | def launch_training(model_name, **kwargs): 7 | 8 | # Launch training 9 | train.train(model_name, **d_params) 10 | 11 | 12 | if __name__ == "__main__": 13 | 14 | parser = argparse.ArgumentParser(description='Experiments for Eve') 15 | parser.add_argument('list_experiments', type=str, nargs='+', 16 | help='List of experiment names. E.g. Eve SGD Adam --> will run a training session with each optimizer') 17 | parser.add_argument('--model_name', default='CNN', type=str, 18 | help='Model name: CNN, Big_CNN or FCN') 19 | parser.add_argument('--batch_size', default=128, type=int, 20 | help='Batch size') 21 | parser.add_argument('--nb_epoch', default=30, type=int, 22 | help='Number of epochs') 23 | parser.add_argument('--dataset', type=str, default="cifar10", 24 | help='Dataset, cifar10, cifar100 or mnist') 25 | 26 | args = parser.parse_args() 27 | 28 | list_dir = ["figures", "log"] 29 | for d in list_dir: 30 | if not os.path.exists(d): 31 | os.makedirs(d) 32 | 33 | for experiment_name in args.list_experiments: 34 | optimizer = experiment_name.split("_")[0] 35 | assert optimizer in ["Eve", "Adam", "SGD"], "Invalid optimizer" 36 | assert args.model_name in ["CNN", "Big_CNN", "FCN"], "Invalid model name" 37 | 38 | # Set default params 39 | d_params = {"optimizer": optimizer, 40 | "experiment_name": experiment_name, 41 | "batch_size": args.batch_size, 42 | "nb_epoch": args.nb_epoch, 43 | "dataset": args.dataset 44 | } 45 | 46 | # Launch training 47 | launch_training(args.model_name, **d_params) 48 | -------------------------------------------------------------------------------- /Eve/models.py: -------------------------------------------------------------------------------- 1 | from keras.models import Model 2 | from keras.layers.core import Activation, Flatten, Dense, Dropout 3 | from keras.layers.convolutional import Conv2D 4 | from keras.layers.pooling import MaxPooling2D 5 | from keras.layers import Input 6 | from keras.layers.normalization import BatchNormalization 7 | from keras.regularizers import l2 8 | 9 | 10 | def standard_conv_block(x, nb_filter, strides=(1,1), pooling=False, bn=False, dropout_rate=None, weight_decay=0): 11 | x = Conv2D(nb_filter, (3, 3), 12 | strides=strides, 13 | padding="same", 14 | kernel_regularizer=l2(weight_decay))(x) 15 | if bn: 16 | x = BatchNormalization(mode=2, axis=1)(x) 17 | x = Activation("relu")(x) 18 | if pooling: 19 | x = MaxPooling2D()(x) 20 | if dropout_rate: 21 | x = Dropout(dropout_rate)(x) 22 | return x 23 | 24 | 25 | def FCN(img_dim, nb_classes, model_name="FCN"): 26 | 27 | x_input = Input(shape=img_dim, name="input") 28 | 29 | x = Flatten()(x_input) 30 | 31 | for i in range(20): 32 | x = Dense(50, activation="relu")(x) 33 | 34 | x = Dense(nb_classes, activation="softmax")(x) 35 | 36 | FCN = Model(inputs=[x_input], outputs=[x]) 37 | FCN.name = model_name 38 | 39 | return FCN 40 | 41 | 42 | def CNN(img_dim, nb_classes, model_name="CNN"): 43 | 44 | x_input = Input(shape=img_dim, name="input") 45 | 46 | x = standard_conv_block(x_input, 32) 47 | x = standard_conv_block(x, 32, pooling=True, dropout_rate=0.25) 48 | x = standard_conv_block(x, 64) 49 | x = standard_conv_block(x, 64, pooling=True, dropout_rate=0.25) 50 | 51 | # FC part 52 | x = Flatten()(x) 53 | x = Dense(512, activation="relu")(x) 54 | x = Dense(nb_classes, activation="softmax")(x) 55 | 56 | CNN = Model(inputs=[x_input], outputs=[x]) 57 | CNN.name = model_name 58 | 59 | return CNN 60 | 61 | 62 | def Big_CNN(img_dim, nb_classes, model_name="Big_CNN"): 63 | 64 | x_input = Input(shape=img_dim, name="input") 65 | 66 | x = standard_conv_block(x_input, 64) 67 | x = standard_conv_block(x, 64) 68 | x = standard_conv_block(x, 64, pooling=True, dropout_rate=0.5) 69 | 70 | x = standard_conv_block(x, 128) 71 | x = standard_conv_block(x, 128) 72 | x = standard_conv_block(x, 128, pooling=True, dropout_rate=0.5) 73 | 74 | x = standard_conv_block(x, 256) 75 | x = standard_conv_block(x, 256) 76 | x = standard_conv_block(x, 256, pooling=True, dropout_rate=0.5) 77 | 78 | # FC part 79 | x = Flatten()(x) 80 | x = Dense(512, activation="relu")(x) 81 | x = Dropout(0.5)(x) 82 | x = Dense(512, activation="relu")(x) 83 | x = Dropout(0.5)(x) 84 | x = Dense(nb_classes, activation="softmax")(x) 85 | 86 | Big_CNN = Model(inputs=[x_input], outputs=[x]) 87 | Big_CNN.name = model_name 88 | 89 | return Big_CNN 90 | 91 | 92 | def load(model_name, img_dim, nb_classes): 93 | 94 | if model_name == "CNN": 95 | model = CNN(img_dim, nb_classes, model_name=model_name) 96 | if model_name == "Big_CNN": 97 | model = Big_CNN(img_dim, nb_classes, model_name=model_name) 98 | elif model_name == "FCN": 99 | model = FCN(img_dim, nb_classes, model_name=model_name) 100 | 101 | return model 102 | -------------------------------------------------------------------------------- /Eve/train.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import models 4 | import numpy as np 5 | from keras.utils import np_utils 6 | from keras.datasets import cifar10, cifar100, mnist 7 | from keras.optimizers import Adam, SGD 8 | from Eve import Eve 9 | 10 | 11 | def train(model_name, **kwargs): 12 | """ 13 | Train model 14 | 15 | args: model_name (str, keras model name) 16 | **kwargs (dict) keyword arguments that specify the model hyperparameters 17 | """ 18 | 19 | # Roll out the parameters 20 | batch_size = kwargs["batch_size"] 21 | nb_epoch = kwargs["nb_epoch"] 22 | dataset = kwargs["dataset"] 23 | optimizer = kwargs["optimizer"] 24 | experiment_name = kwargs["experiment_name"] 25 | 26 | # Compile model. 27 | if optimizer == "SGD": 28 | opt = SGD(lr=1E-2, decay=1E-4, momentum=0.9, nesterov=True) 29 | if optimizer == "Adam": 30 | opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=1E-4) 31 | if optimizer == "Eve": 32 | opt = Eve(lr=1E-4, decay=1E-4, beta_1=0.9, beta_2=0.999, beta_3=0.999, small_k=0.1, big_K=10, epsilon=1e-08) 33 | 34 | if dataset == "cifar10": 35 | (X_train, y_train), (X_test, y_test) = cifar10.load_data() 36 | if dataset == "cifar100": 37 | (X_train, y_train), (X_test, y_test) = cifar100.load_data() 38 | if dataset == "mnist": 39 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 40 | X_train = X_train.reshape((X_train.shape[0], 1, 28, 28)) 41 | X_test = X_test.reshape((X_test.shape[0], 1, 28, 28)) 42 | 43 | X_train = X_train.astype('float32') 44 | X_test = X_test.astype('float32') 45 | X_train /= 255. 46 | X_test /= 255. 47 | 48 | img_dim = X_train.shape[-3:] 49 | nb_classes = len(np.unique(y_train)) 50 | 51 | # convert class vectors to binary class matrices 52 | Y_train = np_utils.to_categorical(y_train, nb_classes) 53 | Y_test = np_utils.to_categorical(y_test, nb_classes) 54 | 55 | # Compile model 56 | model = models.load(model_name, img_dim, nb_classes) 57 | model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"]) 58 | 59 | train_losses, train_accs = [], [] 60 | val_losses, val_accs = [], [] 61 | 62 | for e in range(nb_epoch): 63 | 64 | loss = model.fit(X_train, Y_train, 65 | batch_size=batch_size, 66 | validation_data=(X_test, Y_test), 67 | epochs=1) 68 | 69 | train_losses.append(loss.history["loss"]) 70 | val_losses.append(loss.history["val_loss"]) 71 | train_accs.append(loss.history["acc"]) 72 | val_accs.append(loss.history["val_acc"]) 73 | 74 | # Save experimental log 75 | d_log = {} 76 | d_log["experiment_name"] = experiment_name 77 | d_log["img_dim"] = img_dim 78 | d_log["batch_size"] = batch_size 79 | d_log["nb_epoch"] = nb_epoch 80 | d_log["train_losses"] = train_losses 81 | d_log["val_losses"] = val_losses 82 | d_log["train_accs"] = train_accs 83 | d_log["val_accs"] = val_accs 84 | d_log["optimizer"] = opt.get_config() 85 | # Add model architecture 86 | json_string = json.loads(model.to_json()) 87 | for key in json_string.keys(): 88 | d_log[key] = json_string[key] 89 | json_file = os.path.join("log", '%s_%s_%s.json' % (dataset, model.name, experiment_name)) 90 | with open(json_file, 'w') as fp: 91 | json.dump(d_log, fp, indent=4, sort_keys=True) 92 | -------------------------------------------------------------------------------- /GAN/README.md: -------------------------------------------------------------------------------- 1 | # Generative Adversarial Networks 2 | 3 | Keras implementation of some GAN models. 4 | 5 | **Sources:** 6 | 7 | - [Improved Techniques for Training GANs](https://arxiv.org/abs/1606.03498) 8 | - [Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks](https://arxiv.org/abs/1511.06434) 9 | - [GANHacks](https://github.com/soumith/ganhacks) 10 | 11 | # Requirements 12 | 13 | ## python modules 14 | 15 | - tqdm==4.17.0 16 | - opencv_python==3.3.0.10 17 | - numpy==1.13.3 18 | - matplotlib==2.0.2 19 | - Keras==2.0.8 20 | - Theano==0.9.0 or Tensorflow==1.3.0 21 | - h5py==2.7.0 22 | - parmap==1.5.1 23 | 24 | 25 | # Part 1. Processing the data 26 | 27 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN/src/data). 28 | 29 | # Part 2. Running the code 30 | 31 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN/src/model) 32 | 33 | # Part 3. Example results 34 | 35 | **CelebA example results** 36 | 37 | ![figure](./figures/img_gan.png) 38 | ![figure](./figures/img_gan2.png) 39 | ![figure](./figures/img_gan3.png) 40 | 41 | **MNIST example results** 42 | 43 | ![figure](./figures/img_mnist.png) 44 | ![figure](./figures/img_mnist2.png) 45 | 46 | For each image: 47 | 48 | - The first 2 rows are generated images 49 | - The last 2 rows are real images 50 | -------------------------------------------------------------------------------- /GAN/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /GAN/data/processed/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/data/processed/.gitkeep -------------------------------------------------------------------------------- /GAN/data/processed/README.md: -------------------------------------------------------------------------------- 1 | # Repository for processed data -------------------------------------------------------------------------------- /GAN/data/raw/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/data/raw/.gitkeep -------------------------------------------------------------------------------- /GAN/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /GAN/figures/img_gan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/figures/img_gan.png -------------------------------------------------------------------------------- /GAN/figures/img_gan2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/figures/img_gan2.png -------------------------------------------------------------------------------- /GAN/figures/img_gan3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/figures/img_gan3.png -------------------------------------------------------------------------------- /GAN/figures/img_mnist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/figures/img_mnist.png -------------------------------------------------------------------------------- /GAN/figures/img_mnist2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/figures/img_mnist2.png -------------------------------------------------------------------------------- /GAN/models/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/GAN/models/.gitkeep -------------------------------------------------------------------------------- /GAN/models/README.md: -------------------------------------------------------------------------------- 1 | # Directory to store model weights -------------------------------------------------------------------------------- /GAN/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN/src/model) for training scripts 8 | -------------------------------------------------------------------------------- /GAN/src/data/README.md: -------------------------------------------------------------------------------- 1 | # Building the data 2 | 3 | # Step 1. Download CelebA 4 | 5 | - Go to http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 6 | - In the Downloads section, select Align&Cropped images. 7 | - In the dropbox page that follows, download the Anno, Eval and Img folders. 8 | - Copy these folders to `Colorful/data/raw`. 9 | - Extract the zip files. 10 | 11 | You should have the following folder structure: 12 | 13 | ├── Anno 14 | ├── list_attr_celeba.txt 15 | ├── list_bbox_celeba.txt 16 | ├── list_landmarks_align_celeba.txt 17 | ├── list_landmarks_celeba.txt 18 | ├── Eval 19 | ├──list_eval_partition.txt 20 | ├── img_align_celeba 21 | ├──lots of images 22 | 23 | At this stage, annotations are not used. 24 | 25 | 26 | # Step 2. Build HDF5 CelebA dataset 27 | 28 | `python make_dataset.py` 29 | 30 | positional arguments: 31 | jpeg_dir path to celeba jpeg images 32 | optional arguments: 33 | -h, --help show this help message and exit 34 | --img_size IMG_SIZE Desired Width == Height 35 | --do_plot DO_PLOT Plot the images to make sure the data processing went 36 | OK 37 | 38 | 39 | **Example:** 40 | 41 | `python make_dataset.py --img_size 64` -------------------------------------------------------------------------------- /GAN/src/data/make_dataset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | import h5py 4 | import glob 5 | import parmap 6 | import argparse 7 | import numpy as np 8 | from tqdm import tqdm as tqdm 9 | import matplotlib.pylab as plt 10 | 11 | 12 | def format_image(img_path, size): 13 | """ 14 | Load img with opencv and reshape 15 | """ 16 | 17 | img_color = cv2.imread(img_path) 18 | img_color = img_color[:, :, ::-1] 19 | 20 | # Slice image to center around face 21 | img_color = img_color[30:-30, 20:-20, :] 22 | 23 | img_color = cv2.resize(img_color, (size, size), interpolation=cv2.INTER_AREA) 24 | 25 | img_color = img_color.reshape((1, size, size, 3)).transpose(0, 3, 1, 2) 26 | 27 | return img_color 28 | 29 | 30 | def build_HDF5(jpeg_dir, size=64): 31 | """ 32 | Gather the data in a single HDF5 file. 33 | """ 34 | 35 | # Put train data in HDF5 36 | hdf5_file = os.path.join(data_dir, "CelebA_%s_data.h5" % size) 37 | with h5py.File(hdf5_file, "w") as hfw: 38 | 39 | list_img = glob.glob(os.path.join(jpeg_dir, "*.jpg")) 40 | list_img = np.array(list_img) 41 | 42 | data_color = hfw.create_dataset("data", 43 | (0, 3, size, size), 44 | maxshape=(None, 3, size, size), 45 | dtype=np.uint8) 46 | 47 | num_files = len(list_img) 48 | chunk_size = 2000 49 | num_chunks = num_files / chunk_size 50 | arr_chunks = np.array_split(np.arange(num_files), num_chunks) 51 | 52 | for chunk_idx in tqdm(arr_chunks): 53 | 54 | list_img_path = list_img[chunk_idx].tolist() 55 | output = parmap.map(format_image, list_img_path, size, pm_parallel=True) 56 | 57 | arr_img_color = np.concatenate(output, axis=0) 58 | 59 | # Resize HDF5 dataset 60 | data_color.resize(data_color.shape[0] + arr_img_color.shape[0], axis=0) 61 | 62 | data_color[-arr_img_color.shape[0]:] = arr_img_color.astype(np.uint8) 63 | 64 | 65 | def check_HDF5(size=64): 66 | """ 67 | Plot images with landmarks to check the processing 68 | """ 69 | 70 | # Get hdf5 file 71 | hdf5_file = os.path.join(data_dir, "CelebA_%s_data.h5" % size) 72 | 73 | with h5py.File(hdf5_file, "r") as hf: 74 | data_color = hf["data"] 75 | for i in range(data_color.shape[0]): 76 | plt.figure() 77 | img = data_color[i, :, :, :].transpose(1,2,0) 78 | plt.imshow(img) 79 | plt.show() 80 | plt.clf() 81 | plt.close() 82 | 83 | 84 | if __name__ == '__main__': 85 | 86 | parser = argparse.ArgumentParser(description='Build dataset') 87 | parser.add_argument('jpeg_dir', type=str, help='path to celeba jpeg images') 88 | parser.add_argument('--img_size', default=64, type=int, 89 | help='Desired Width == Height') 90 | parser.add_argument('--do_plot', default=False, type=bool, 91 | help='Plot the images to make sure the data processing went OK') 92 | args = parser.parse_args() 93 | 94 | data_dir = "../../data/processed" 95 | 96 | build_HDF5(args.jpeg_dir, size=args.img_size) 97 | 98 | if args.do_plot: 99 | check_HDF5(args.img_size) 100 | -------------------------------------------------------------------------------- /GAN/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training and evaluating 2 | 3 | ## Training 4 | 5 | `python main.py` 6 | 7 | 8 | optional arguments: 9 | 10 | -h, --help show this help message and exit 11 | --backend BACKEND theano or tensorflow 12 | --generator GENERATOR 13 | upsampling or deconv 14 | --dset DSET mnist or celebA 15 | --batch_size BATCH_SIZE 16 | Batch size 17 | --n_batch_per_epoch N_BATCH_PER_EPOCH 18 | Number of training epochs 19 | --nb_epoch NB_EPOCH Number of batches per epoch 20 | --epoch EPOCH Epoch at which weights were saved for evaluation 21 | --nb_classes NB_CLASSES 22 | Number of classes 23 | --do_plot DO_PLOT Debugging plot 24 | --bn_mode BN_MODE Batch norm mode 25 | --img_dim IMG_DIM Image width == height 26 | --noise_scale NOISE_SCALE 27 | variance of the normal from which we sample the noise 28 | --label_smoothing smooth the positive labels when training D 29 | --use_mbd use mini batch disc 30 | --label_flipping LABEL_FLIPPING 31 | Probability (0 to 1.) to flip the labels when training 32 | D 33 | 34 | 35 | **Example:** 36 | 37 | `python main.py` 38 | 39 | **N.B.** If using the CelebA dataset, make sure to specify the corresponding img_dim value. For instance, if you saved a 64x64 CelebA hdf5 dataset, call `python main.py --img_dim 64` 40 | 41 | 42 | ### Expected outputs: 43 | 44 | - Weights are saved in GAN/models 45 | - Figures are saved in GAN/figures 46 | - Save model weights every few epochs 47 | 48 | 49 | ### Additional notes 50 | 51 | You can choose the type of generator: 52 | 53 | - `upsampling:` generate the image with a series of `Upsampling2D` and `Convolution2D` operations 54 | - `deconv:` use keras' transposed convolutions `Deconvolution2D`. This is closer to the original DCGAN implementation. 55 | 56 | At this stage, `deconv` only works with the `tensorflow` backend. -------------------------------------------------------------------------------- /GAN/src/model/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | import argparse 3 | 4 | 5 | def launch_training(**kwargs): 6 | 7 | # Launch training 8 | train_GAN.train(**kwargs) 9 | 10 | 11 | if __name__ == "__main__": 12 | 13 | parser = argparse.ArgumentParser(description='Train model') 14 | parser.add_argument('--backend', type=str, default="theano", help="theano or tensorflow") 15 | parser.add_argument('--generator', type=str, default="upsampling", help="upsampling or deconv") 16 | parser.add_argument('--dset', type=str, default="mnist", help="mnist or celebA") 17 | parser.add_argument('--batch_size', default=32, type=int, help='Batch size') 18 | parser.add_argument('--n_batch_per_epoch', default=200, type=int, help="Number of training epochs") 19 | parser.add_argument('--nb_epoch', default=400, type=int, help="Number of batches per epoch") 20 | parser.add_argument('--epoch', default=10, type=int, help="Epoch at which weights were saved for evaluation") 21 | parser.add_argument('--nb_classes', default=2, type=int, help="Number of classes") 22 | parser.add_argument('--do_plot', default=False, type=bool, help="Debugging plot") 23 | parser.add_argument('--bn_mode', default=2, type=int, help="Batch norm mode") 24 | parser.add_argument('--img_dim', default=64, type=int, help="Image width == height") 25 | parser.add_argument('--noise_scale', default=0.5, type=float, help="variance of the normal from which we sample the noise") 26 | parser.add_argument('--label_smoothing', action="store_true", help="smooth the positive labels when training D") 27 | parser.add_argument('--use_mbd', action="store_true", help="use mini batch disc") 28 | parser.add_argument('--label_flipping', default=0, type=float, help="Probability (0 to 1.) to flip the labels when training D") 29 | 30 | args = parser.parse_args() 31 | 32 | assert args.dset in ["mnist", "celebA"] 33 | 34 | # Set the backend by modifying the env variable 35 | if args.backend == "theano": 36 | os.environ["KERAS_BACKEND"] = "theano" 37 | elif args.backend == "tensorflow": 38 | os.environ["KERAS_BACKEND"] = "tensorflow" 39 | 40 | # Import the backend 41 | import keras.backend as K 42 | 43 | # manually set dim ordering otherwise it is not changed 44 | if args.backend == "theano": 45 | image_data_format = "channels_first" 46 | K.set_image_data_format(image_data_format) 47 | elif args.backend == "tensorflow": 48 | image_data_format = "channels_last" 49 | K.set_image_data_format(image_data_format) 50 | 51 | import train_GAN 52 | 53 | # Set default params 54 | d_params = {"mode": "train_GAN", 55 | "dset": args.dset, 56 | "generator": args.generator, 57 | "batch_size": args.batch_size, 58 | "n_batch_per_epoch": args.n_batch_per_epoch, 59 | "nb_epoch": args.nb_epoch, 60 | "model_name": "CNN", 61 | "epoch": args.epoch, 62 | "nb_classes": args.nb_classes, 63 | "do_plot": args.do_plot, 64 | "image_data_format": image_data_format, 65 | "bn_mode": args.bn_mode, 66 | "img_dim": args.img_dim, 67 | "label_smoothing": args.label_smoothing, 68 | "label_flipping": args.label_flipping, 69 | "noise_scale": args.noise_scale, 70 | "use_mbd": args.use_mbd, 71 | } 72 | 73 | # Launch training 74 | launch_training(**d_params) 75 | -------------------------------------------------------------------------------- /GAN/src/utils/batch_utils.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import multiprocessing 4 | import os 5 | import h5py 6 | import matplotlib.pylab as plt 7 | import matplotlib.gridspec as gridspec 8 | from matplotlib.pyplot import cm 9 | 10 | 11 | class DataGenerator(object): 12 | """ 13 | Generate minibatches with real-time data parallel augmentation on CPU 14 | 15 | args : 16 | hdf5_file (str) path to data in HDF5 format 17 | batch_size (int) Minibatch size 18 | dset (str) train/test/valid, the name of the dset to iterate over 19 | maxproc (int) max number of processes to spawn in parallel 20 | num_cached (int) max number of batches to keep in queue 21 | 22 | yields : 23 | X, y (minibatch data and labels as np arrays) 24 | """ 25 | 26 | def __init__(self, 27 | hdf5_file, 28 | batch_size=32, 29 | nb_classes=12, 30 | dset="training", 31 | maxproc=8, 32 | num_cached=10): 33 | 34 | # Check file exists 35 | assert os.path.isfile(hdf5_file), hdf5_file + " doesn't exist" 36 | 37 | # Initialize class internal variables 38 | self.dset = dset 39 | self.maxproc = maxproc 40 | self.hdf5_file = hdf5_file 41 | self.batch_size = batch_size 42 | self.num_cached = num_cached 43 | self.nb_classes = nb_classes 44 | 45 | # Dict that will store all transformations and their parameters 46 | self.d_transform = {} 47 | 48 | # Read the data file to get dataset shape information 49 | with h5py.File(self.hdf5_file, "r") as hf: 50 | self.X_shape = hf["data"].shape 51 | assert len(self.X_shape) == 4,\ 52 | ("\n\nImg data should be formatted as: \n" 53 | "(n_samples, n_channels, Height, Width)") 54 | self.n_samples = hf["data"].shape[0] 55 | # Verify n_channels is at index 1 56 | assert self.X_shape[-3] < min(self.X_shape[-2:]),\ 57 | ("\n\nImg data should be formatted as: \n" 58 | "(n_samples, n_channels, Height, Width)") 59 | 60 | # Save the class internal variables to a config dict 61 | self.d_config = {} 62 | self.d_config["hdf5_file"] = hdf5_file 63 | self.d_config["batch_size"] = batch_size 64 | self.d_config["dset"] = dset 65 | self.d_config["num_cached"] = num_cached 66 | self.d_config["maxproc"] = maxproc 67 | self.d_config["data_shape"] = self.X_shape 68 | 69 | def get_config(self): 70 | 71 | return self.d_config 72 | 73 | def gen_batch_inmemory_GAN(self, X_real, batch_size=None): 74 | """Generate batch, assuming X is loaded in memory in the main program""" 75 | 76 | while True: 77 | 78 | bs = self.batch_size 79 | if batch_size is not None: 80 | bs = batch_size 81 | 82 | # Select idx at random for the batch 83 | idx = np.random.choice(X_real.shape[0], bs, replace=False) 84 | X_batch_real = X_real[idx] 85 | 86 | yield X_batch_real 87 | -------------------------------------------------------------------------------- /GAN/src/utils/general_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def remove_files(files): 5 | """ 6 | Remove files from disk 7 | 8 | args: files (str or list) remove all files in 'files' 9 | """ 10 | 11 | if isinstance(files, (list, tuple)): 12 | for f in files: 13 | if os.path.isfile(os.path.expanduser(f)): 14 | os.remove(f) 15 | elif isinstance(files, str): 16 | if os.path.isfile(os.path.expanduser(files)): 17 | os.remove(files) 18 | 19 | 20 | def create_dir(dirs): 21 | """ 22 | Create directory 23 | 24 | args: dirs (str or list) create all dirs in 'dirs' 25 | """ 26 | 27 | if isinstance(dirs, (list, tuple)): 28 | for d in dirs: 29 | if not os.path.exists(os.path.expanduser(d)): 30 | os.makedirs(d) 31 | elif isinstance(dirs, str): 32 | if not os.path.exists(os.path.expanduser(dirs)): 33 | os.makedirs(dirs) 34 | 35 | 36 | def setup_logging(model_name): 37 | 38 | model_dir = "../../models" 39 | # Output path where we store experiment log and weights 40 | model_dir = os.path.join(model_dir, model_name) 41 | 42 | fig_dir = "../../figures" 43 | 44 | # Create if it does not exist 45 | create_dir([model_dir, fig_dir]) 46 | -------------------------------------------------------------------------------- /GAN_tf/README.md: -------------------------------------------------------------------------------- 1 | # Generative Adversarial Networks 2 | 3 | Pure tensorflow implementation of DCGAN 4 | 5 | **Sources:** 6 | 7 | - [Improved Techniques for Training GANs](https://arxiv.org/abs/1606.03498) 8 | - [Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks](https://arxiv.org/abs/1511.06434) 9 | - [GANHacks](https://github.com/soumith/ganhacks) 10 | 11 | # Requirements 12 | 13 | ## python modules 14 | 15 | - numpy==1.13.3 16 | - matplotlib==2.0.2 17 | - terminaltables==3.1.0 18 | - ipdb==0.10.3 19 | - tensorflow_gpu==1.3.0 20 | - colorama==0.3.9 21 | - tqdm==4.17.0 22 | - tensorflow==1.4.0rc1 23 | 24 | 25 | ## Data 26 | 27 | CelebA data: 28 | 29 | - Go to http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 30 | - In the Downloads section, select Align&Cropped images. 31 | - In the dropbox page that follows, download the Anno, Eval and Img folders. 32 | - Copy these folders to `/data/raw`. 33 | - Extract the zip files. 34 | 35 | 36 | # Running the code 37 | 38 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN_tf/src/model) 39 | 40 | -------------------------------------------------------------------------------- /GAN_tf/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /GAN_tf/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /GAN_tf/figures/README.md: -------------------------------------------------------------------------------- 1 | # Figure repository -------------------------------------------------------------------------------- /GAN_tf/logs/README.md: -------------------------------------------------------------------------------- 1 | # Tensorboard log repository -------------------------------------------------------------------------------- /GAN_tf/models/README.md: -------------------------------------------------------------------------------- 1 | # Tensorflow model and checkpoint repository -------------------------------------------------------------------------------- /GAN_tf/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN_tf/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN_tf/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN_tf/src/model) for training scripts 8 | -------------------------------------------------------------------------------- /GAN_tf/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training GAN 2 | 3 | ## Usage 4 | 5 | `python main.py` 6 | 7 | 8 | optional arguments: 9 | 10 | --run RUN Which operation to run. [train|inference] 11 | --nb_epoch NB_EPOCH Number of epochs 12 | --batch_size BATCH_SIZE 13 | Number of samples per batch. 14 | --nb_batch_per_epoch NB_BATCH_PER_EPOCH 15 | Number of batches per epoch 16 | --learning_rate LEARNING_RATE 17 | Learning rate used for AdamOptimizer 18 | --noise_dim NOISE_DIM 19 | Noise dimension for GAN generation 20 | --random_seed RANDOM_SEED 21 | Seed used to initialize rng. 22 | --use_XLA [USE_XLA] Whether to use XLA compiler. 23 | --nouse_XLA 24 | --num_threads NUM_THREADS 25 | Number of threads to fetch the data 26 | --capacity_factor CAPACITY_FACTOR 27 | Nuumber of batches to store in queue 28 | --data_format DATA_FORMAT 29 | Tensorflow image data format. 30 | --celebA_path CELEBA_PATH 31 | Path to celebA images 32 | --channels CHANNELS Number of channels 33 | --central_fraction CENTRAL_FRACTION 34 | Central crop as a fraction of total image 35 | --img_size IMG_SIZE Image size 36 | --model_dir MODEL_DIR 37 | Output folder where checkpoints are dumped. 38 | --log_dir LOG_DIR Logs for tensorboard. 39 | --fig_dir FIG_DIR Where to save figures. 40 | --raw_dir RAW_DIR Where raw data is saved 41 | --data_dir DATA_DIR Where processed data is saved -------------------------------------------------------------------------------- /GAN_tf/src/model/flags.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | 4 | FLAGS = tf.app.flags.FLAGS 5 | 6 | 7 | def define_flags(): 8 | 9 | ############ 10 | # Run mode 11 | ############ 12 | tf.app.flags.DEFINE_string('run', None, "Which operation to run. [train|inference]") 13 | 14 | ########################## 15 | # Training parameters 16 | ########################### 17 | tf.app.flags.DEFINE_integer('nb_epoch', 400, "Number of epochs") 18 | tf.app.flags.DEFINE_integer('batch_size', 64, "Number of samples per batch.") 19 | tf.app.flags.DEFINE_integer('nb_batch_per_epoch', 500, "Number of batches per epoch") 20 | tf.app.flags.DEFINE_float('learning_rate', 2E-4, "Learning rate used for AdamOptimizer") 21 | tf.app.flags.DEFINE_integer('noise_dim', 100, "Noise dimension for GAN generation") 22 | tf.app.flags.DEFINE_integer('random_seed', 0, "Seed used to initialize rng.") 23 | 24 | ############################################ 25 | # General tensorflow parameters parameters 26 | ############################################# 27 | tf.app.flags.DEFINE_bool('use_XLA', False, "Whether to use XLA compiler.") 28 | tf.app.flags.DEFINE_integer('num_threads', 2, "Number of threads to fetch the data") 29 | tf.app.flags.DEFINE_float('capacity_factor', 32, "Nuumber of batches to store in queue") 30 | 31 | ########## 32 | # Datasets 33 | ########## 34 | tf.app.flags.DEFINE_string('data_format', "NCHW", "Tensorflow image data format.") 35 | tf.app.flags.DEFINE_string('celebA_path', "../../data/raw/img_align_celeba", "Path to celebA images") 36 | tf.app.flags.DEFINE_integer('channels', 3, "Number of channels") 37 | tf.app.flags.DEFINE_float('central_fraction', 0.8, "Central crop as a fraction of total image") 38 | tf.app.flags.DEFINE_integer('img_size', 64, "Image size") 39 | 40 | ############## 41 | # Directories 42 | ############## 43 | tf.app.flags.DEFINE_string('model_dir', '../../models', "Output folder where checkpoints are dumped.") 44 | tf.app.flags.DEFINE_string('log_dir', '../../logs', "Logs for tensorboard.") 45 | tf.app.flags.DEFINE_string('fig_dir', '../../figures', "Where to save figures.") 46 | tf.app.flags.DEFINE_string('raw_dir', '../../data/raw', "Where raw data is saved") 47 | tf.app.flags.DEFINE_string('data_dir', '../../data/processed', "Where processed data is saved") 48 | -------------------------------------------------------------------------------- /GAN_tf/src/model/input.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import glob 3 | import os 4 | 5 | 6 | def normalize_image(image): 7 | 8 | image = tf.cast(image, tf.float32) / 255. 9 | image = (image - 0.5) / 0.5 10 | return image 11 | 12 | 13 | def unnormalize_image(image): 14 | 15 | image = (image * 0.5 + 0.5) * 255. 16 | image = tf.cast(image, tf.uint8) 17 | return image 18 | 19 | 20 | def input_data(sess): 21 | 22 | FLAGS = tf.app.flags.FLAGS 23 | 24 | list_images = glob.glob(os.path.join(FLAGS.dataset, "*.jpg")) 25 | 26 | # Read each JPEG file 27 | reader = tf.WholeFileReader() 28 | filename_queue = tf.train.string_input_producer(list_images) 29 | key, value = reader.read(filename_queue) 30 | channels = FLAGS.channels 31 | image = tf.image.decode_jpeg(value, channels=channels, name="dataset_image") 32 | image.set_shape([None, None, channels]) 33 | 34 | # Crop and other random augmentations 35 | image = tf.image.random_flip_left_right(image) 36 | # image = tf.image.random_saturation(image, .95, 1.05) 37 | # image = tf.image.random_brightness(image, .05) 38 | # image = tf.image.random_contrast(image, .95, 1.05) 39 | 40 | # Center crop 41 | image = tf.image.central_crop(image, FLAGS.central_fraction) 42 | 43 | # Resize 44 | image = tf.image.resize_images(image, (FLAGS.img_size, FLAGS.img_size), method=tf.image.ResizeMethod.AREA) 45 | 46 | # Normalize 47 | image = normalize_image(image) 48 | 49 | # Format image to correct ordering 50 | if FLAGS.data_format == "NCHW": 51 | image = tf.transpose(image, (2,0,1)) 52 | 53 | # Using asynchronous queues 54 | img_batch = tf.train.batch([image], 55 | batch_size=FLAGS.batch_size, 56 | num_threads=FLAGS.num_threads, 57 | capacity=FLAGS.capacity_factor * FLAGS.batch_size, 58 | name='batch_input') 59 | 60 | return img_batch 61 | -------------------------------------------------------------------------------- /GAN_tf/src/model/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | # Disable Tensorflow's INFO and WARNING messages 3 | # See http://stackoverflow.com/questions/35911252 4 | if 'TF_CPP_MIN_LOG_LEVEL' not in os.environ: 5 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 6 | import flags 7 | import tensorflow as tf 8 | import train 9 | import train_mnist 10 | 11 | FLAGS = tf.app.flags.FLAGS 12 | 13 | 14 | def launch_training(): 15 | 16 | train.train_model() 17 | # train_mnist.train_model() 18 | 19 | 20 | def main(argv=None): 21 | 22 | assert FLAGS.run in ["train", "inference"], "Choose [train|inference]" 23 | 24 | if FLAGS.run == 'train': 25 | launch_training() 26 | 27 | 28 | if __name__ == '__main__': 29 | flags.define_flags() 30 | tf.app.run() 31 | -------------------------------------------------------------------------------- /GAN_tf/src/utils/data_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | import numpy as np 4 | import tensorflow as tf 5 | 6 | 7 | def normalize_image(image): 8 | 9 | image = tf.cast(image, tf.float32) / 255. 10 | image = (image - 0.5) / 0.5 11 | return image 12 | 13 | 14 | def unnormalize_image(image, name=None): 15 | 16 | image = (image * 0.5 + 0.5) * 255. 17 | image = tf.cast(image, tf.uint8, name=name) 18 | return image 19 | 20 | 21 | def input_data(sess): 22 | 23 | FLAGS = tf.app.flags.FLAGS 24 | 25 | list_images = glob.glob(os.path.join(FLAGS.celebA_path, "*.jpg")) 26 | 27 | # Read each JPEG file 28 | 29 | with tf.device('/cpu:0'): 30 | 31 | reader = tf.WholeFileReader() 32 | filename_queue = tf.train.string_input_producer(list_images) 33 | key, value = reader.read(filename_queue) 34 | channels = FLAGS.channels 35 | image = tf.image.decode_jpeg(value, channels=channels, name="dataset_image") 36 | image.set_shape([None, None, channels]) 37 | 38 | # Crop and other random augmentations 39 | image = tf.image.random_flip_left_right(image) 40 | # image = tf.image.random_saturation(image, .95, 1.05) 41 | # image = tf.image.random_brightness(image, .05) 42 | # image = tf.image.random_contrast(image, .95, 1.05) 43 | 44 | # Center crop 45 | image = tf.image.central_crop(image, FLAGS.central_fraction) 46 | 47 | # Resize 48 | image = tf.image.resize_images(image, (FLAGS.img_size, FLAGS.img_size), method=tf.image.ResizeMethod.AREA) 49 | 50 | # Normalize 51 | image = normalize_image(image) 52 | 53 | # Format image to correct ordering 54 | if FLAGS.data_format == "NCHW": 55 | image = tf.transpose(image, (2,0,1)) 56 | 57 | # Using asynchronous queues 58 | img_batch = tf.train.batch([image], 59 | batch_size=FLAGS.batch_size, 60 | num_threads=FLAGS.num_threads, 61 | capacity=2 * FLAGS.num_threads * FLAGS.batch_size, 62 | name='X_real_input') 63 | 64 | return img_batch 65 | 66 | 67 | def sample_batch(X, batch_size): 68 | 69 | idx = np.random.choice(X.shape[0], batch_size, replace=False) 70 | return X[idx] 71 | -------------------------------------------------------------------------------- /GAN_tf/src/utils/logging_utils.py: -------------------------------------------------------------------------------- 1 | from colorama import init 2 | from colorama import Fore, Back, Style 3 | import tensorflow as tf 4 | from terminaltables import SingleTable 5 | 6 | 7 | def print_table(TABLE_DATA): 8 | 9 | table_instance = SingleTable(TABLE_DATA, "") 10 | table_instance.justify_columns[2] = 'right' 11 | print(table_instance.table) 12 | print 13 | 14 | 15 | def print_bright(s): 16 | 17 | init() 18 | print(Style.BRIGHT + s + Style.RESET_ALL) 19 | 20 | 21 | def print_green(info, value=""): 22 | 23 | print(Fore.GREEN + "[%s] " % info + Style.RESET_ALL + str(value)) 24 | 25 | 26 | def print_red(info, value=""): 27 | 28 | print(Fore.RED + "[%s] " % info + Style.RESET_ALL + str(value)) 29 | 30 | 31 | def print_session(): 32 | 33 | FLAGS = tf.app.flags.FLAGS 34 | 35 | print_bright("\nSetting up TF session:") 36 | for key in FLAGS.__dict__["__flags"].keys(): 37 | if "dir" not in key: 38 | print_green(key, FLAGS.__dict__["__flags"][key]) 39 | 40 | print_bright("\nConfiguring directories:") 41 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 42 | # Clear directories by default 43 | if tf.gfile.Exists(d): 44 | print_red("Deleting", d) 45 | tf.gfile.DeleteRecursively(d) 46 | 47 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 48 | print_green("Creating", d) 49 | tf.gfile.MakeDirs(d) 50 | 51 | 52 | def print_initialize(): 53 | 54 | print_bright("\nInitialization:") 55 | print_green("Created session saver") 56 | print_green("Ran init ops") 57 | 58 | 59 | def print_summaries(): 60 | 61 | print_bright("\nSummaries:") 62 | list_summaries = tf.get_collection(tf.GraphKeys.SUMMARIES) 63 | for t in list_summaries: 64 | print_green(t.name) 65 | 66 | 67 | def print_queues(): 68 | 69 | print_bright("\nQueues:") 70 | print_green("Created coordinator") 71 | print_green("Started queue runner") 72 | 73 | 74 | def print_check_data(out, list_data): 75 | 76 | print 77 | TABLE_DATA = (('Variable Name', 'Shape', "Min value", "Max value"),) 78 | for o, t in zip(out, list_data): 79 | TABLE_DATA += (tuple([t.name, str(o.shape), "%.3g" % o.min(), "%.3g" % o.max()]),) 80 | print_table(TABLE_DATA) 81 | -------------------------------------------------------------------------------- /GAN_tf/src/utils/objectives.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | def pixel_mae(pred, target, name='mae'): 5 | 6 | return tf.reduce_mean(tf.abs(pred - target), name=name) 7 | 8 | 9 | def pixel_mse(pred, target, name='mse'): 10 | 11 | return tf.reduce_mean(tf.square(pred - target), name=name) 12 | 13 | 14 | def pixel_rmse(pred, target, name='rmse'): 15 | 16 | return tf.sqrt(tf.reduce_mean(tf.square(pred - target), name=name)) 17 | 18 | 19 | def binary_cross_entropy_with_logits(pred, target): 20 | 21 | return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=target)) 22 | 23 | 24 | def wasserstein(pred, target): 25 | 26 | return tf.reduce_mean(pred * target) 27 | -------------------------------------------------------------------------------- /GAN_tf/src/utils/training_utils.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import random 3 | import numpy as np 4 | import sys 5 | sys.path.append("../utils") 6 | import logging_utils as lu 7 | 8 | 9 | def setup_session(): 10 | 11 | lu.print_session() 12 | 13 | FLAGS = tf.app.flags.FLAGS 14 | 15 | # Create session 16 | config = tf.ConfigProto() 17 | if FLAGS.use_XLA: 18 | config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 19 | sess = tf.Session(config=config) 20 | 21 | # Setup directory to save model 22 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 23 | # Clear directories by default 24 | if tf.gfile.Exists(d): 25 | tf.gfile.DeleteRecursively(d) 26 | tf.gfile.MakeDirs(d) 27 | 28 | # Initialize all RNGs with a deterministic seed 29 | with sess.graph.as_default(): 30 | tf.set_random_seed(FLAGS.random_seed) 31 | 32 | random.seed(FLAGS.random_seed) 33 | np.random.seed(FLAGS.random_seed) 34 | 35 | return sess 36 | 37 | 38 | def initialize_session(sess): 39 | 40 | saver = tf.train.Saver() 41 | 42 | init_op = tf.group(tf.global_variables_initializer(), 43 | tf.local_variables_initializer()) 44 | sess.run(init_op) 45 | 46 | lu.print_initialize() 47 | 48 | return saver 49 | 50 | 51 | def add_gradient_summary(list_gradvar): 52 | # Add summary for gradients 53 | for g,v in list_gradvar: 54 | if g is not None: 55 | tf.summary.histogram(v.name + "/gradient", g) 56 | 57 | 58 | def manage_queues(sess): 59 | 60 | coord = tf.train.Coordinator() 61 | tf.train.start_queue_runners(sess=sess, coord=coord) 62 | 63 | lu.print_queues() 64 | 65 | return coord 66 | 67 | 68 | def manage_summaries(sess): 69 | 70 | FLAGS = tf.app.flags.FLAGS 71 | writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) 72 | 73 | lu.print_summaries() 74 | 75 | return writer 76 | 77 | 78 | def check_data(out, list_data): 79 | 80 | lu.print_check_data(out, list_data) 81 | -------------------------------------------------------------------------------- /GAN_tf/src/utils/visualization_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pylab as plt 5 | 6 | FLAGS = tf.app.flags.FLAGS 7 | 8 | 9 | def save_image(data, data_format, e): 10 | """Saves a picture showing the current progress of the model""" 11 | 12 | X_G, X_real = data 13 | 14 | Xg = X_G[:8] 15 | Xr = X_real[:8] 16 | 17 | if data_format == "NHWC": 18 | X = np.concatenate((Xg, Xr), axis=0) 19 | list_rows = [] 20 | for i in range(int(X.shape[0] / 4)): 21 | Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=1) 22 | list_rows.append(Xr) 23 | 24 | Xr = np.concatenate(list_rows, axis=0) 25 | 26 | if data_format == "NCHW": 27 | X = np.concatenate((Xg, Xr), axis=0) 28 | list_rows = [] 29 | for i in range(int(X.shape[0] / 4)): 30 | Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=2) 31 | list_rows.append(Xr) 32 | 33 | Xr = np.concatenate(list_rows, axis=1) 34 | Xr = Xr.transpose(1,2,0) 35 | 36 | if Xr.shape[-1] == 1: 37 | plt.imshow(Xr[:, :, 0], cmap="gray") 38 | else: 39 | plt.imshow(Xr) 40 | plt.axis("off") 41 | plt.savefig(os.path.join(FLAGS.fig_dir, "current_batch_%s.png" % e)) 42 | plt.clf() 43 | plt.close() 44 | -------------------------------------------------------------------------------- /InfoGAN/README.md: -------------------------------------------------------------------------------- 1 | # InfoGAN 2 | 3 | Keras implementation of [InfoGAN: Interpretable Representation Learning by Information Maximizing Generative Adversarial Nets](https://arxiv.org/abs/1606.03657) 4 | 5 | # Requirements 6 | 7 | ## python modules 8 | 9 | - keras, theano or tensorflow backend 10 | - h5py 11 | - matplotlib 12 | - opencv 3 13 | - numpy 14 | - tqdm 15 | - parmap 16 | 17 | 18 | # Part 1. Processing the data 19 | 20 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/InfoGAN/src/data). 21 | 22 | # Part 2. Running the code 23 | 24 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/InfoGAN/src/model) 25 | 26 | # Part 3. Example results 27 | 28 | **MNIST example results** 29 | 30 | **Note 1** The figures below were obtained with a slight modification of the original InfoGAN paper: supervised categorical cross entropy loss for the discriminator and simple MSE loss for the continuous variables. Credits to [@burisuriburi](https://github.com/buriburisuri/supervised_infogan) for the original idea. 31 | 32 | **Note 2** The code in this repository matches OpenAI's original implementation, without the trick of Note 1. 33 | 34 | Varying the categorical code along each row: 35 | 36 | ![figure](./figures/varying_categorical.png) 37 | 38 | Varying the continuous code along rows and columns: 39 | 40 | ![figure](./figures/varying_continuous.png) -------------------------------------------------------------------------------- /InfoGAN/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /InfoGAN/data/processed/README.md: -------------------------------------------------------------------------------- 1 | # Repository for processed data -------------------------------------------------------------------------------- /InfoGAN/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /InfoGAN/figures/DCGAN_discriminator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/InfoGAN/figures/DCGAN_discriminator.png -------------------------------------------------------------------------------- /InfoGAN/figures/generator_deconv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/InfoGAN/figures/generator_deconv.png -------------------------------------------------------------------------------- /InfoGAN/figures/generator_upsampling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/InfoGAN/figures/generator_upsampling.png -------------------------------------------------------------------------------- /InfoGAN/figures/varying_categorical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/InfoGAN/figures/varying_categorical.png -------------------------------------------------------------------------------- /InfoGAN/figures/varying_continuous.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/InfoGAN/figures/varying_continuous.png -------------------------------------------------------------------------------- /InfoGAN/models/README.md: -------------------------------------------------------------------------------- 1 | # Directory to store model weights -------------------------------------------------------------------------------- /InfoGAN/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/InfoGAN/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/InfoGAN/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/InfoGAN/src/model) for training scripts 8 | -------------------------------------------------------------------------------- /InfoGAN/src/data/README.md: -------------------------------------------------------------------------------- 1 | # Building the data 2 | 3 | # Step 1. Download CelebA 4 | 5 | - Go to http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 6 | - In the Downloads section, select Align&Cropped images. 7 | - In the dropbox page that follows, download the Anno, Eval and Img folders. 8 | - Copy these folders to `Colorful/data/raw`. 9 | - Extract the zip files. 10 | 11 | You should have the following folder structure: 12 | 13 | ├── Anno 14 | ├── list_attr_celeba.txt 15 | ├── list_bbox_celeba.txt 16 | ├── list_landmarks_align_celeba.txt 17 | ├── list_landmarks_celeba.txt 18 | ├── Eval 19 | ├──list_eval_partition.txt 20 | ├── img_align_celeba 21 | ├──lots of images 22 | 23 | At this stage, annotations are not used. 24 | 25 | 26 | # Step 2. Build HDF5 CelebA dataset 27 | 28 | `python make_dataset.py` 29 | 30 | positional arguments: 31 | jpeg_dir path to celeba jpeg images 32 | optional arguments: 33 | -h, --help show this help message and exit 34 | --img_size IMG_SIZE Desired Width == Height 35 | --do_plot DO_PLOT Plot the images to make sure the data processing went 36 | OK 37 | 38 | 39 | **Example:** 40 | 41 | `python make_dataset.py --img_size 64` -------------------------------------------------------------------------------- /InfoGAN/src/data/make_dataset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | import h5py 4 | import glob 5 | import parmap 6 | import argparse 7 | import numpy as np 8 | from tqdm import tqdm as tqdm 9 | import matplotlib.pylab as plt 10 | 11 | 12 | def format_image(img_path, size): 13 | """ 14 | Load img with opencv and reshape 15 | """ 16 | 17 | img_color = cv2.imread(img_path) 18 | img_color = img_color[:, :, ::-1] 19 | 20 | # Slice image to center around face 21 | img_color = img_color[30:-30, 20:-20, :] 22 | 23 | img_color = cv2.resize(img_color, (size, size), interpolation=cv2.INTER_AREA) 24 | 25 | img_color = img_color.reshape((1, size, size, 3)).transpose(0, 3, 1, 2) 26 | 27 | return img_color 28 | 29 | 30 | def build_HDF5(jpeg_dir, size=64): 31 | """ 32 | Gather the data in a single HDF5 file. 33 | """ 34 | 35 | # Put train data in HDF5 36 | hdf5_file = os.path.join(data_dir, "CelebA_%s_data.h5" % size) 37 | with h5py.File(hdf5_file, "w") as hfw: 38 | 39 | list_img = glob.glob(os.path.join(jpeg_dir, "*.jpg")) 40 | list_img = np.array(list_img) 41 | 42 | data_color = hfw.create_dataset("data", 43 | (0, 3, size, size), 44 | maxshape=(None, 3, size, size), 45 | dtype=np.uint8) 46 | 47 | num_files = len(list_img) 48 | chunk_size = 2000 49 | num_chunks = num_files / chunk_size 50 | arr_chunks = np.array_split(np.arange(num_files), num_chunks) 51 | 52 | for chunk_idx in tqdm(arr_chunks): 53 | 54 | list_img_path = list_img[chunk_idx].tolist() 55 | output = parmap.map(format_image, list_img_path, size, pm_parallel=True) 56 | 57 | arr_img_color = np.concatenate(output, axis=0) 58 | 59 | # Resize HDF5 dataset 60 | data_color.resize(data_color.shape[0] + arr_img_color.shape[0], axis=0) 61 | 62 | data_color[-arr_img_color.shape[0]:] = arr_img_color.astype(np.uint8) 63 | 64 | 65 | def check_HDF5(size=64): 66 | """ 67 | Plot images with landmarks to check the processing 68 | """ 69 | 70 | # Get hdf5 file 71 | hdf5_file = os.path.join(data_dir, "CelebA_%s_data.h5" % size) 72 | 73 | with h5py.File(hdf5_file, "r") as hf: 74 | data_color = hf["data"] 75 | for i in range(data_color.shape[0]): 76 | plt.figure() 77 | img = data_color[i, :, :, :].transpose(1,2,0) 78 | plt.imshow(img) 79 | plt.show() 80 | plt.clf() 81 | plt.close() 82 | 83 | 84 | if __name__ == '__main__': 85 | 86 | parser = argparse.ArgumentParser(description='Build dataset') 87 | parser.add_argument('jpeg_dir', type=str, help='path to celeba jpeg images') 88 | parser.add_argument('--img_size', default=64, type=int, 89 | help='Desired Width == Height') 90 | parser.add_argument('--do_plot', default=False, type=bool, 91 | help='Plot the images to make sure the data processing went OK') 92 | args = parser.parse_args() 93 | 94 | data_dir = "../../data/processed" 95 | 96 | build_HDF5(args.jpeg_dir, size=args.img_size) 97 | 98 | if args.do_plot: 99 | check_HDF5(args.img_size) 100 | -------------------------------------------------------------------------------- /InfoGAN/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training and evaluating 2 | 3 | ## Training 4 | 5 | `python main.py` 6 | 7 | 8 | positional arguments: 9 | 10 | mode train or eval 11 | 12 | optional arguments: 13 | 14 | -h, --help show this help message and exit 15 | --backend BACKEND theano or tensorflow 16 | --generator GENERATOR 17 | upsampling or deconv 18 | --dset DSET mnist or celebA 19 | --batch_size BATCH_SIZE 20 | Batch size 21 | --n_batch_per_epoch N_BATCH_PER_EPOCH 22 | Number of training epochs 23 | --nb_epoch NB_EPOCH Number of batches per epoch 24 | --epoch EPOCH Epoch at which weights were saved for evaluation 25 | --nb_classes NB_CLASSES 26 | Number of classes 27 | --do_plot DO_PLOT Debugging plot 28 | --bn_mode BN_MODE Batch norm mode 29 | --img_dim IMG_DIM Image width == height (only specify for CelebA) 30 | --noise_dim NOISE_DIM 31 | noise dimension 32 | --cont_dim CONT_DIM Latent continuous dimensions 33 | --cat_dim CAT_DIM Latent categorical dimension 34 | --noise_scale NOISE_SCALE 35 | variance of the normal from which we sample the noise 36 | --label_smoothing smooth the positive labels when training D 37 | --use_mbd use mini batch disc 38 | --label_flipping LABEL_FLIPPING 39 | Probability (0 to 1.) to flip the labels when training 40 | D 41 | 42 | 43 | 44 | **Example:** 45 | 46 | `python main.py train` 47 | 48 | **N.B.** If using the CelebA dataset, make sure to specify the corresponding img_dim value. For instance, if you saved a 64x64 CelebA hdf5 dataset, call `python main.py --img_dim 64` 49 | 50 | 51 | ### Expected outputs: 52 | 53 | - Weights are saved in InfoGAN/models 54 | - Figures are saved in InfoGAN/figures 55 | - Save model weights every few epochs 56 | 57 | 58 | ### Additional notes 59 | 60 | You can choose the type of generator: 61 | 62 | - `upsampling:` generate the image with a series of `Upsampling2D` and `Convolution2D` operations 63 | - `deconv:` use keras' transposed convolutions `Deconvolution2D`. This is closer to the original DCGAN implementation. 64 | 65 | At this stage, `deconv` only works with the `tensorflow` backend. -------------------------------------------------------------------------------- /InfoGAN/src/utils/batch_utils.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import multiprocessing 4 | import os 5 | import h5py 6 | import matplotlib.pylab as plt 7 | import matplotlib.gridspec as gridspec 8 | from matplotlib.pyplot import cm 9 | 10 | 11 | class DataGenerator(object): 12 | """ 13 | Generate minibatches with real-time data parallel augmentation on CPU 14 | 15 | args : 16 | hdf5_file (str) path to data in HDF5 format 17 | batch_size (int) Minibatch size 18 | dset (str) train/test/valid, the name of the dset to iterate over 19 | maxproc (int) max number of processes to spawn in parallel 20 | num_cached (int) max number of batches to keep in queue 21 | 22 | yields : 23 | X, y (minibatch data and labels as np arrays) 24 | """ 25 | 26 | def __init__(self, 27 | hdf5_file, 28 | batch_size=32, 29 | nb_classes=12, 30 | dset="training", 31 | maxproc=8, 32 | num_cached=10): 33 | 34 | # Check file exists 35 | assert os.path.isfile(hdf5_file), hdf5_file + " doesn't exist" 36 | 37 | # Initialize class internal variables 38 | self.dset = dset 39 | self.maxproc = maxproc 40 | self.hdf5_file = hdf5_file 41 | self.batch_size = batch_size 42 | self.num_cached = num_cached 43 | self.nb_classes = nb_classes 44 | 45 | # Dict that will store all transformations and their parameters 46 | self.d_transform = {} 47 | 48 | # Read the data file to get dataset shape information 49 | with h5py.File(self.hdf5_file, "r") as hf: 50 | self.X_shape = hf["data"].shape 51 | assert len(self.X_shape) == 4,\ 52 | ("\n\nImg data should be formatted as: \n" 53 | "(n_samples, n_channels, Height, Width)") 54 | self.n_samples = hf["data"].shape[0] 55 | # Verify n_channels is at index 1 56 | assert self.X_shape[-3] < min(self.X_shape[-2:]),\ 57 | ("\n\nImg data should be formatted as: \n" 58 | "(n_samples, n_channels, Height, Width)") 59 | 60 | # Save the class internal variables to a config dict 61 | self.d_config = {} 62 | self.d_config["hdf5_file"] = hdf5_file 63 | self.d_config["batch_size"] = batch_size 64 | self.d_config["dset"] = dset 65 | self.d_config["num_cached"] = num_cached 66 | self.d_config["maxproc"] = maxproc 67 | self.d_config["data_shape"] = self.X_shape 68 | 69 | def get_config(self): 70 | 71 | return self.d_config 72 | 73 | def gen_batch_inmemory_GAN(self, X_real, batch_size=None): 74 | """Generate batch, assuming X is loaded in memory in the main program""" 75 | 76 | while True: 77 | 78 | bs = self.batch_size 79 | if batch_size is not None: 80 | bs = batch_size 81 | 82 | # Select idx at random for the batch 83 | idx = np.random.choice(X_real.shape[0], bs, replace=False) 84 | X_batch_real = X_real[idx] 85 | 86 | yield X_batch_real 87 | -------------------------------------------------------------------------------- /InfoGAN/src/utils/general_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def remove_files(files): 5 | """ 6 | Remove files from disk 7 | 8 | args: files (str or list) remove all files in 'files' 9 | """ 10 | 11 | if isinstance(files, (list, tuple)): 12 | for f in files: 13 | if os.path.isfile(os.path.expanduser(f)): 14 | os.remove(f) 15 | elif isinstance(files, str): 16 | if os.path.isfile(os.path.expanduser(files)): 17 | os.remove(files) 18 | 19 | 20 | def create_dir(dirs): 21 | """ 22 | Create directory 23 | 24 | args: dirs (str or list) create all dirs in 'dirs' 25 | """ 26 | 27 | if isinstance(dirs, (list, tuple)): 28 | for d in dirs: 29 | if not os.path.exists(os.path.expanduser(d)): 30 | os.makedirs(d) 31 | elif isinstance(dirs, str): 32 | if not os.path.exists(os.path.expanduser(dirs)): 33 | os.makedirs(dirs) 34 | 35 | 36 | def setup_logging(model_name): 37 | 38 | model_dir = "../../models" 39 | # Output path where we store experiment log and weights 40 | model_dir = os.path.join(model_dir, model_name) 41 | 42 | fig_dir = "../../figures" 43 | 44 | # Create if it does not exist 45 | create_dir([model_dir, fig_dir]) 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | The MIT License 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Implementation of recent Deep Learning papers 2 | 3 | # Papers 4 | 5 | - [Densely Connected Convolutional Network](http://arxiv.org/abs/1608.06993) implemented in the [DenseNet folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DenseNet) 6 | 7 | - [Visualizing and Understanding Convolutional Networks](https://arxiv.org/pdf/1311.2901v1) implemented in the [DeconvNet folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DeconvNet) 8 | 9 | - [Improving Stochastic Gradient Descent With Feedback](https://arxiv.org/pdf/1611.01505v1.pdf) implemented in the [Eve folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Eve) 10 | 11 | - [Colorful Image Colorization](https://arxiv.org/abs/1603.08511) implemented in the [Colorful folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Colorful) 12 | 13 | - [Deep Feature Interpolation for Image Content Changes](https://arxiv.org/pdf/1611.05507v1.pdf) implemented in the [DFI folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/DFI) 14 | 15 | - Several Generative Adversarial Networks (GAN) models and techniques in the [GAN folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/GAN) 16 | 17 | - [pix2pix](https://arxiv.org/pdf/1611.07004v1.pdf) in the [pix2pix folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/pix2pix) 18 | 19 | - [InfoGAN](https://arxiv.org/abs/1606.03657) in the [InfoGAN folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/InfoGAN) 20 | 21 | - [WassersteinGAN](https://arxiv.org/abs/1701.07875) in the [WassersteinGAN folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WassersteinGAN) and [WGAN-GP folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WGAN-GP) for a tensorflow implementation. 22 | 23 | - [BEGAN](https://arxiv.org/pdf/1703.10717.pdf) in the [BEGAN folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/BEGAN) 24 | 25 | - [Scaling the Scattering Transform: Deep Hybrid Networks](https://arxiv.org/abs/1703.08961) in the [ScatteringTransform folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/ScatteringTransform) 26 | 27 | - [Sobolev Training for Neural Networks](https://arxiv.org/abs/1706.04859) in the [Sobolev folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/Sobolev) 28 | 29 | - [Self-Normalizing Networks](https://arxiv.org/pdf/1706.02515.pdf) in the [SELU folder](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/SELU) -------------------------------------------------------------------------------- /SELU/README.md: -------------------------------------------------------------------------------- 1 | # Experiments with MNIST and SELU 2 | 3 | Pytorch implementation of some experiments from [Self-Normalizing Networks](https://arxiv.org/pdf/1706.02515.pdf) 4 | 5 | ## Dependencies 6 | 7 | - python (tested on Anaconda python 3.6.1) 8 | - pytorch (tested on 0.1.12_2) 9 | - sklearn (tested on 0.18.1) 10 | - matplotlib (tested on 2.0.1) 11 | - tqdm 12 | - numpy 13 | 14 | 15 | ## Uage 16 | 17 | Main command: 18 | 19 | python main.py 20 | 21 | Arguments: 22 | 23 | --model MODEL Model name, RELUNet or SELUNet 24 | --n_inner_layers N_INNER_LAYERS 25 | Number of inner hidden layers 26 | --hidden_dim HIDDEN_DIM 27 | Hidden layer dimension 28 | --dropout DROPOUT Dropout rate 29 | --use_cuda Use CUDA 30 | --nb_epoch NB_EPOCH Number of training epochs 31 | --batchnorm Whether to use BN for RELUNet 32 | --batch_size BATCH_SIZE 33 | Batch size 34 | --optimizer OPTIMIZER 35 | Optimizer 36 | --learning_rate LEARNING_RATE 37 | Learning rate 38 | 39 | 40 | ## Run a batch of experiments 41 | 42 | Modify `run_experiments.sh` as needed then run 43 | 44 | bash run_experiments.sh 45 | 46 | 47 | ## Plot results 48 | 49 | Run a few experiments. Results are saved in a `results` folder. 50 | Modify `plot_results.py` to select your experiments, then run: 51 | 52 | 53 | python plot_results.py 54 | 55 | 56 | ## Results 57 | 58 | - The architecture of the NN is the same as in the original paper. 59 | - The depths are a bit different (powers of 2 for inner layers, not counting first and last layer) 60 | - N.B. The number of epochs vary between plots. 61 | - At LR = 1E-5, the results are consistent with the paper albeit the minimum training loss is quite a bit higher than in the paper. 62 | - The Adam optimizer was also used (far less epochs though) for comparison. SELU still seems to work better but is more unstable 63 | - At higher learning rates, Adam no longer works really well for SELU. (Note that the conclusion may change with more epochs but 2000 epochs is quite a lot of time...) 64 | 65 | ![learning rate 1e-2](figures/SELU_LR_1E-2.png) 66 | ![learning rate 1e-3](figures/SELU_LR_1E-3.png) 67 | ![learning rate 1e-5](figures/SELU_LR_1E-5.png) 68 | 69 | 70 | 71 | ## Conclusion 72 | 73 | - SELU is definitely better behaved with SGD. 74 | - Depending on learning rate, it may also be better with Adam. 75 | - However a fair bit of fine tuning seems to be needed to get best performance (even with SGD) and training may be quite slow (lots of epochs needed for low learning rate). -------------------------------------------------------------------------------- /SELU/figures/SELU_LR_1E-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/SELU/figures/SELU_LR_1E-2.png -------------------------------------------------------------------------------- /SELU/figures/SELU_LR_1E-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/SELU/figures/SELU_LR_1E-3.png -------------------------------------------------------------------------------- /SELU/figures/SELU_LR_1E-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/SELU/figures/SELU_LR_1E-5.png -------------------------------------------------------------------------------- /SELU/main.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import os 3 | import argparse 4 | import torchvision.datasets as dset 5 | import train 6 | 7 | # Training settings 8 | parser = argparse.ArgumentParser(description='MNIST SELU experiments') 9 | 10 | # Neural Net archi 11 | parser.add_argument('--model', default="RELUNet", type=str, help="Model name, RELUNet or SELUNet") 12 | parser.add_argument('--n_inner_layers', default=4, type=int, help="Number of inner hidden layers") 13 | parser.add_argument('--hidden_dim', default=-1, type=int, help="Hidden layer dimension") 14 | parser.add_argument('--dropout', default=0, type=float, help="Dropout rate") 15 | # Training params 16 | parser.add_argument('--use_cuda', action="store_true", help="Use CUDA") 17 | parser.add_argument('--nb_epoch', default=100, type=int, help="Number of training epochs") 18 | parser.add_argument('--batchnorm', action="store_true", help="Whether to use BN for RELUNet") 19 | parser.add_argument('--batch_size', default=128, type=int, help='Batch size') 20 | parser.add_argument('--optimizer', default="SGD", type=str, help="Optimizer") 21 | parser.add_argument('--learning_rate', default=1E-5, type=float, help="Learning rate") 22 | args = parser.parse_args() 23 | 24 | 25 | assert args.model in ["RELUNet", "SELUNet"] 26 | 27 | # Download mnist if it does not exist 28 | if not os.path.isfile("processed/training.pt"): 29 | dset.MNIST(root=".", download=True) 30 | 31 | if not os.path.exists("results"): 32 | os.makedirs("results") 33 | 34 | # Launch training 35 | train.train(args) 36 | -------------------------------------------------------------------------------- /SELU/plot_results.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pylab as plt 2 | import matplotlib.gridspec as gridspec 3 | import json 4 | import os 5 | 6 | 7 | def plot(): 8 | 9 | ############################ 10 | # Adapt to existing experiments 11 | ############################ 12 | list_width = [0.5, 1, 1.5, 2] 13 | list_depth = [6, 10, 18, 34] 14 | learning_rate = "1E-5" 15 | result_folder = "results_1e-5" 16 | 17 | plt.figure(figsize=(12,5)) 18 | gs = gridspec.GridSpec(1,2) 19 | 20 | ################## 21 | # SGD results 22 | ################## 23 | ax0 = plt.subplot(gs[0]) 24 | 25 | for i in range(len(list_depth)): 26 | depth = list_depth[i] 27 | exp = "%s/RELUNet_depth_%s_opt_SGD_drop_0_bn_True.json" % (result_folder, depth) 28 | with open(exp, "r") as f: 29 | d_losses = json.load(f) 30 | ax0.plot(d_losses["train_loss"], 31 | linewidth=list_width[i], 32 | color="C0", 33 | label="RELU, Depth: %s" % depth) 34 | 35 | for i in range(len(list_depth)): 36 | depth = list_depth[i] 37 | exp = "%s/SELUNet_depth_%s_opt_SGD_drop_0_bn_False.json" % (result_folder, depth) 38 | with open(exp, "r") as f: 39 | d_losses = json.load(f) 40 | ax0.plot(d_losses["train_loss"], 41 | linewidth=list_width[i], 42 | color="C1", 43 | label="SELU, Depth: %s" % depth) 44 | ax0.legend(loc="best") 45 | ax0.set_title("SGD, Learning Rate = %s" % learning_rate, fontsize=16) 46 | ax0.set_yscale("log") 47 | ax0.set_ylim([1E-6, 10]) 48 | ax0.set_xlabel("Epochs", fontsize=18) 49 | ax0.set_ylabel("Train logloss", fontsize=18) 50 | 51 | ################## 52 | # Adam results 53 | ################## 54 | ax1 = plt.subplot(gs[1]) 55 | 56 | for i in range(len(list_depth)): 57 | depth = list_depth[i] 58 | exp = "%s/RELUNet_depth_%s_opt_Adam_drop_0_bn_True.json" % (result_folder, depth) 59 | with open(exp, "r") as f: 60 | d_losses = json.load(f) 61 | ax1.plot(d_losses["train_loss"], 62 | linewidth=list_width[i], 63 | color="C0", 64 | label="RELU, Depth: %s" % depth) 65 | 66 | for i in range(len(list_depth)): 67 | depth = list_depth[i] 68 | exp = "%s/SELUNet_depth_%s_opt_Adam_drop_0_bn_False.json" % (result_folder, depth) 69 | with open(exp, "r") as f: 70 | d_losses = json.load(f) 71 | ax1.plot(d_losses["train_loss"], 72 | linewidth=list_width[i], 73 | color="C1", 74 | label="SELU, Depth: %s" % depth) 75 | ax1.legend(loc="best") 76 | ax1.set_title("Adam, Learning Rate = %s" % learning_rate, fontsize=16) 77 | ax1.set_yscale("log") 78 | ax1.set_ylim([1E-6, 10]) 79 | ax1.set_xlabel("Epochs", fontsize=18) 80 | ax1.set_ylabel("Train logloss", fontsize=18) 81 | 82 | if not os.path.exists("figures"): 83 | os.makedirs("figures") 84 | 85 | plt.savefig("figures/SELU_LR_%s.png" % learning_rate) 86 | 87 | if __name__ == '__main__': 88 | 89 | plot() 90 | -------------------------------------------------------------------------------- /SELU/run_experiments.sh: -------------------------------------------------------------------------------- 1 | # RELU 2 | python main.py --use_cuda --n_inner_layers 4 --model RELUNet --learning_rate 1E-5 --nb_epoch 20 --batchnorm --optimizer Adam 3 | python main.py --use_cuda --n_inner_layers 8 --model RELUNet --learning_rate 1E-5 --nb_epoch 20 --batchnorm --optimizer Adam 4 | python main.py --use_cuda --n_inner_layers 16 --model RELUNet --learning_rate 1E-5 --nb_epoch 20 --batchnorm --optimizer Adam 5 | python main.py --use_cuda --n_inner_layers 32 --model RELUNet --learning_rate 1E-5 --nb_epoch 20 --batchnorm --optimizer Adam 6 | # SELU 7 | python main.py --use_cuda --n_inner_layers 4 --model SELUNet --learning_rate 1E-5 --nb_epoch 20 --optimizer Adam 8 | python main.py --use_cuda --n_inner_layers 8 --model SELUNet --learning_rate 1E-5 --nb_epoch 20 --optimizer Adam 9 | python main.py --use_cuda --n_inner_layers 16 --model SELUNet --learning_rate 1E-5 --nb_epoch 20 --optimizer Adam 10 | python main.py --use_cuda --n_inner_layers 32 --model SELUNet --learning_rate 1E-5 --nb_epoch 20 --optimizer Adam 11 | # SELU + dropout 12 | python main.py --use_cuda --n_inner_layers 4 --model SELUNet --learning_rate 1E-5 --nb_epoch 200 --dropout 0.05 13 | python main.py --use_cuda --n_inner_layers 8 --model SELUNet --learning_rate 1E-5 --nb_epoch 200 --dropout 0.05 14 | python main.py --use_cuda --n_inner_layers 16 --model SELUNet --learning_rate 1E-5 --nb_epoch 200 --dropout 0.05 15 | python main.py --use_cuda --n_inner_layers 32 --model SELUNet --learning_rate 1E-5 --nb_epoch 200 --dropout 0.05 16 | -------------------------------------------------------------------------------- /ScatteringTransform/README.md: -------------------------------------------------------------------------------- 1 | # Scaling the Scattering Transform: Deep Hybrid Networks 2 | 3 | - Pure tensorflow implementation of the scattering transform and hybrid networks. 4 | - No need for external dependencies, only tensorflow functions are used 5 | - Depending on the settings (e.g. use_XLA), the code is between 3x to 5x slower than [pyscatwave](https://github.com/edouardoyallon/pyscatwave). 6 | - Since only tensorflow primitives are involved, it is possible to backprop through the Scattering Transform (this functionality will soon be added to [pyscatwave](https://github.com/edouardoyallon/pyscatwave)). 7 | 8 | 9 | **Sources:** 10 | 11 | - [Scaling the Scattering Transform: Deep Hybrid Networks](https://arxiv.org/abs/1703.08961) 12 | - [pyscatwave](https://github.com/edouardoyallon/pyscatwave) 13 | 14 | This repository is a simple adapatation of [pyscatwave](https://github.com/edouardoyallon/pyscatwave) with tensorflow primitives only. 15 | 16 | **Copyright (c) 2017, Eugene Belilovsky (INRIA), Edouard Oyallon (ENS) and Sergey Zagoruyko (ENPC)** 17 | **All rights reserved.** 18 | 19 | # Requirements 20 | 21 | ## python modules 22 | 23 | - scipy==1.0.0 24 | - torch==0.2.0.post4 25 | - numpy==1.13.3 26 | - terminaltables==3.1.0 27 | - matplotlib==2.0.2 28 | - tqdm==4.17.0 29 | - colorama==0.3.9 30 | - tensorflow_gpu==1.3.0 31 | - cupy==3.0.0a1 32 | - asposebarcode==1.0.0 33 | - pynvrtc==8.0 34 | - tensorflow==1.4.0rc1 35 | - scikit-cuda==0.5.1 36 | 37 | You should also install the dependencies of [pyscatwave](https://github.com/edouardoyallon/pyscatwave) to run the tests. 38 | 39 | 40 | # Running the code 41 | 42 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/ScatteringTransform/src/model) 43 | 44 | 45 | # Running tests 46 | 47 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/ScatteringTransform/test) -------------------------------------------------------------------------------- /ScatteringTransform/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /ScatteringTransform/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /ScatteringTransform/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/ScatteringTransform/src/utils) for the various scripting utilities used throughout 6 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/ScatteringTransform/src/model) for training scripts 7 | -------------------------------------------------------------------------------- /ScatteringTransform/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training a Hybrid Scattering + NN model. 2 | 3 | ## Usage 4 | 5 | `python main.py` 6 | 7 | 8 | optional arguments: 9 | 10 | -h, --help show this help message and exit 11 | --run RUN Which operation to run. [train|inference] 12 | --nb_epoch NB_EPOCH Number of epochs 13 | --batch_size BATCH_SIZE 14 | Number of samples per batch. 15 | --nb_batch_per_epoch NB_BATCH_PER_EPOCH 16 | Number of batches per epoch 17 | --learning_rate LEARNING_RATE 18 | Learning rate used for AdamOptimizer 19 | --noise_dim NOISE_DIM 20 | Noise dimension for GAN generation 21 | --random_seed RANDOM_SEED 22 | Seed used to initialize rng. 23 | --use_XLA [USE_XLA] Whether to use XLA compiler. 24 | --nouse_XLA 25 | --num_threads NUM_THREADS 26 | Number of threads to fetch the data 27 | --capacity_factor CAPACITY_FACTOR 28 | Nuumber of batches to store in queue 29 | --data_format DATA_FORMAT 30 | Tensorflow image data format. 31 | --channels CHANNELS Number of channels 32 | --central_fraction CENTRAL_FRACTION 33 | Central crop as a fraction of total image 34 | --img_size IMG_SIZE Image size 35 | --model_dir MODEL_DIR 36 | Output folder where checkpoints are dumped. 37 | --log_dir LOG_DIR Logs for tensorboard. 38 | --fig_dir FIG_DIR Where to save figures. 39 | --raw_dir RAW_DIR Where raw data is saved 40 | --data_dir DATA_DIR Where processed data is saved 41 | 42 | 43 | # To train a MNIST classification model 44 | 45 | python main.py --run train 46 | 47 | Accuracy should reach 99 % on the validation set in a little less than 10 epochs. -------------------------------------------------------------------------------- /ScatteringTransform/src/model/flags.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | 4 | FLAGS = tf.app.flags.FLAGS 5 | 6 | 7 | def define_flags(): 8 | 9 | ############ 10 | # Run mode 11 | ############ 12 | tf.app.flags.DEFINE_string('run', None, "Which operation to run. [train|inference]") 13 | 14 | ########################## 15 | # Training parameters 16 | ########################### 17 | tf.app.flags.DEFINE_integer('nb_epoch', 400, "Number of epochs") 18 | tf.app.flags.DEFINE_integer('batch_size', 128, "Number of samples per batch.") 19 | tf.app.flags.DEFINE_integer('nb_batch_per_epoch', 30, "Number of batches per epoch") 20 | tf.app.flags.DEFINE_float('learning_rate', 1E-5, "Learning rate used for AdamOptimizer") 21 | tf.app.flags.DEFINE_integer('noise_dim', 100, "Noise dimension for GAN generation") 22 | tf.app.flags.DEFINE_integer('random_seed', 0, "Seed used to initialize rng.") 23 | 24 | ############################################ 25 | # General tensorflow parameters parameters 26 | ############################################# 27 | tf.app.flags.DEFINE_bool('use_XLA', False, "Whether to use XLA compiler.") 28 | tf.app.flags.DEFINE_integer('num_threads', 2, "Number of threads to fetch the data") 29 | tf.app.flags.DEFINE_float('capacity_factor', 32, "Nuumber of batches to store in queue") 30 | 31 | ########## 32 | # Datasets 33 | ########## 34 | tf.app.flags.DEFINE_string('data_format', "NCHW", "Tensorflow image data format.") 35 | tf.app.flags.DEFINE_integer('channels', 1, "Number of channels") 36 | tf.app.flags.DEFINE_float('central_fraction', 0.8, "Central crop as a fraction of total image") 37 | tf.app.flags.DEFINE_integer('img_size', 28, "Image size") 38 | 39 | ############## 40 | # Directories 41 | ############## 42 | tf.app.flags.DEFINE_string('model_dir', '../../models', "Output folder where checkpoints are dumped.") 43 | tf.app.flags.DEFINE_string('log_dir', '../../logs', "Logs for tensorboard.") 44 | tf.app.flags.DEFINE_string('fig_dir', '../../figures', "Where to save figures.") 45 | tf.app.flags.DEFINE_string('raw_dir', '../../data/raw', "Where raw data is saved") 46 | tf.app.flags.DEFINE_string('data_dir', '../../data/processed', "Where processed data is saved") 47 | -------------------------------------------------------------------------------- /ScatteringTransform/src/model/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | # Disable Tensorflow's INFO and WARNING messages 3 | # See http://stackoverflow.com/questions/35911252 4 | if 'TF_CPP_MIN_LOG_LEVEL' not in os.environ: 5 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 6 | import flags 7 | import train_mnist 8 | import tensorflow as tf 9 | FLAGS = tf.app.flags.FLAGS 10 | 11 | 12 | def launch_training(): 13 | 14 | train_mnist.train_model() 15 | 16 | 17 | def main(argv=None): 18 | 19 | # Only training is implemented at this stage 20 | assert FLAGS.run in ["train", "inference"], "Choose [train|inference]" 21 | 22 | if FLAGS.run == 'train': 23 | launch_training() 24 | 25 | 26 | if __name__ == '__main__': 27 | flags.define_flags() 28 | tf.app.run() 29 | -------------------------------------------------------------------------------- /ScatteringTransform/src/utils/logging_utils.py: -------------------------------------------------------------------------------- 1 | from colorama import init 2 | from colorama import Fore, Back, Style 3 | import tensorflow as tf 4 | from terminaltables import SingleTable 5 | 6 | 7 | def print_table(TABLE_DATA): 8 | 9 | table_instance = SingleTable(TABLE_DATA, "") 10 | table_instance.justify_columns[2] = 'right' 11 | print(table_instance.table) 12 | print 13 | 14 | 15 | def print_bright(s): 16 | 17 | init() 18 | print(Style.BRIGHT + s + Style.RESET_ALL) 19 | 20 | 21 | def print_green(info, value=""): 22 | 23 | print(Fore.GREEN + "[%s] " % info + Style.RESET_ALL + str(value)) 24 | 25 | 26 | def print_red(info, value=""): 27 | 28 | print(Fore.RED + "[%s] " % info + Style.RESET_ALL + str(value)) 29 | 30 | 31 | def print_bright_green(info, value=""): 32 | 33 | print(Style.BRIGHT + "[%s] " % info + Style.RESET_ALL + Fore.GREEN + str(value) + Style.RESET_ALL) 34 | 35 | 36 | def print_bright_red(info, value=""): 37 | 38 | print(Style.BRIGHT + "[%s] " % info + Style.RESET_ALL + Fore.RED + str(value) + Style.RESET_ALL) 39 | 40 | 41 | def print_session(): 42 | 43 | FLAGS = tf.app.flags.FLAGS 44 | 45 | print_bright("\nSetting up TF session:") 46 | for key in FLAGS.__dict__["__flags"].keys(): 47 | if "dir" not in key: 48 | print_green(key, FLAGS.__dict__["__flags"][key]) 49 | 50 | print_bright("\nConfiguring directories:") 51 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 52 | # Clear directories by default 53 | if tf.gfile.Exists(d): 54 | print_red("Deleting", d) 55 | tf.gfile.DeleteRecursively(d) 56 | 57 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 58 | print_green("Creating", d) 59 | tf.gfile.MakeDirs(d) 60 | 61 | 62 | def print_initialize(): 63 | 64 | print_bright("\nInitialization:") 65 | print_green("Created session saver") 66 | print_green("Ran init ops") 67 | 68 | 69 | def print_summaries(): 70 | 71 | print_bright("\nSummaries:") 72 | list_summaries = tf.get_collection(tf.GraphKeys.SUMMARIES) 73 | for t in list_summaries: 74 | print_green(t.name) 75 | 76 | 77 | def print_queues(): 78 | 79 | print_bright("\nQueues:") 80 | print_green("Created coordinator") 81 | print_green("Started queue runner") 82 | 83 | 84 | def print_check_data(out, list_data): 85 | 86 | print 87 | TABLE_DATA = (('Variable Name', 'Shape', "Min value", "Max value"),) 88 | for o, t in zip(out, list_data): 89 | TABLE_DATA += (tuple([t.name, str(o.shape), "%.3g" % o.min(), "%.3g" % o.max()]),) 90 | print_table(TABLE_DATA) 91 | -------------------------------------------------------------------------------- /ScatteringTransform/src/utils/objectives.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | def mae(pred, target, name='mae'): 5 | 6 | return tf.reduce_mean(tf.abs(pred - target), name=name) 7 | 8 | 9 | def mse(pred, target, name='mse'): 10 | 11 | return tf.reduce_mean(tf.square(pred - target), name=name) 12 | 13 | 14 | def pixel_rmse(pred, target, name='rmse'): 15 | 16 | return tf.sqrt(tf.reduce_mean(tf.square(pred - target), name=name)) 17 | 18 | 19 | def binary_cross_entropy_with_logits(pred, target): 20 | 21 | return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=target)) 22 | 23 | 24 | def wasserstein(pred, target): 25 | 26 | return tf.reduce_mean(pred * target) 27 | -------------------------------------------------------------------------------- /ScatteringTransform/src/utils/training_utils.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import random 3 | import numpy as np 4 | import sys 5 | sys.path.append("../utils") 6 | import logging_utils as lu 7 | 8 | 9 | def setup_session(): 10 | 11 | lu.print_session() 12 | 13 | FLAGS = tf.app.flags.FLAGS 14 | 15 | # Create session 16 | config = tf.ConfigProto() 17 | config.gpu_options.allow_growth = True 18 | if FLAGS.use_XLA: 19 | config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 20 | sess = tf.Session(config=config) 21 | 22 | # Setup directory to save model 23 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 24 | # Clear directories by default 25 | if tf.gfile.Exists(d): 26 | tf.gfile.DeleteRecursively(d) 27 | tf.gfile.MakeDirs(d) 28 | 29 | # Initialize all RNGs with a deterministic seed 30 | with sess.graph.as_default(): 31 | tf.set_random_seed(FLAGS.random_seed) 32 | 33 | random.seed(FLAGS.random_seed) 34 | np.random.seed(FLAGS.random_seed) 35 | 36 | return sess 37 | 38 | 39 | def initialize_session(sess): 40 | 41 | saver = tf.train.Saver() 42 | 43 | init_op = tf.group(tf.global_variables_initializer(), 44 | tf.local_variables_initializer()) 45 | sess.run(init_op) 46 | 47 | lu.print_initialize() 48 | 49 | return saver 50 | 51 | 52 | def add_gradient_summary(list_gradvar): 53 | # Add summary for gradients 54 | for g,v in list_gradvar: 55 | if g is not None: 56 | tf.summary.histogram(v.name + "/gradient", g) 57 | 58 | 59 | def manage_queues(sess): 60 | 61 | coord = tf.train.Coordinator() 62 | tf.train.start_queue_runners(sess=sess, coord=coord) 63 | 64 | lu.print_queues() 65 | 66 | return coord 67 | 68 | 69 | def manage_summaries(sess): 70 | 71 | FLAGS = tf.app.flags.FLAGS 72 | writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) 73 | 74 | lu.print_summaries() 75 | 76 | return writer 77 | 78 | 79 | def check_data(out, list_data): 80 | 81 | lu.print_check_data(out, list_data) 82 | -------------------------------------------------------------------------------- /ScatteringTransform/src/utils/visualization_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pylab as plt 5 | 6 | FLAGS = tf.app.flags.FLAGS 7 | 8 | 9 | def save_image(data, data_format, e, suffix=None): 10 | """Saves a picture showing the current progress of the model""" 11 | 12 | X_G, X_real = data 13 | 14 | Xg = X_G[:8] 15 | Xr = X_real[:8] 16 | 17 | if data_format == "NHWC": 18 | X = np.concatenate((Xg, Xr), axis=0) 19 | list_rows = [] 20 | for i in range(int(X.shape[0] / 4)): 21 | Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=1) 22 | list_rows.append(Xr) 23 | 24 | Xr = np.concatenate(list_rows, axis=0) 25 | 26 | if data_format == "NCHW": 27 | X = np.concatenate((Xg, Xr), axis=0) 28 | list_rows = [] 29 | for i in range(int(X.shape[0] / 4)): 30 | Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=2) 31 | list_rows.append(Xr) 32 | 33 | Xr = np.concatenate(list_rows, axis=1) 34 | Xr = Xr.transpose(1,2,0) 35 | 36 | if Xr.shape[-1] == 1: 37 | plt.imshow(Xr[:, :, 0], cmap="gray") 38 | else: 39 | plt.imshow(Xr) 40 | plt.axis("off") 41 | if suffix is None: 42 | plt.savefig(os.path.join(FLAGS.fig_dir, "current_batch_%s.png" % e)) 43 | else: 44 | plt.savefig(os.path.join(FLAGS.fig_dir, "current_batch_%s_%s.png" % (suffix, e))) 45 | plt.clf() 46 | plt.close() 47 | 48 | 49 | def get_stacked_tensor(X1, X2): 50 | 51 | X = tf.concat((X1[:16], X2[:16]), axis=0) 52 | list_rows = [] 53 | for i in range(8): 54 | Xr = tf.concat([X[k] for k in range(4 * i, 4 * (i + 1))], axis=2) 55 | list_rows.append(Xr) 56 | 57 | X = tf.concat(list_rows, axis=1) 58 | X = tf.transpose(X, (1,2,0)) 59 | X = tf.expand_dims(X, 0) 60 | 61 | return X 62 | -------------------------------------------------------------------------------- /ScatteringTransform/test/README.md: -------------------------------------------------------------------------------- 1 | # Check implementation of the Scattering Transform 2 | 3 | The pytorch code has been copied from [pyscatwave](https://github.com/edouardoyallon/pyscatwave). 4 | 5 | The dependencies of the above library should be installed prior to running the tests. 6 | 7 | # Test principle 8 | 9 | - Since this repository is an exact port of [pyscatwave](https://github.com/edouardoyallon/pyscatwave), we make sure we get the same outputs for all the main functions. 10 | 11 | # Running tests 12 | 13 | python run_test.py 14 | 15 | optional arguments: 16 | 17 | -h, --help show this help message and exit 18 | --M M 19 | --N N 20 | --J J 21 | 22 | 23 | # Tests contents 24 | 25 | - We check that the filter banks are equal for all filters 26 | - Given a random input image, we check that the Scattering transform output is the same up to a tolerance of 1E-6. -------------------------------------------------------------------------------- /ScatteringTransform/test/run_pytorch.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import filters_bank_pytorch as filters_bank 3 | import scattering_pytorch as scattering 4 | 5 | 6 | def run_filter_bank(M, N, J): 7 | 8 | filters = filters_bank.filters_bank(M, N, J) 9 | d_save = {} 10 | # Save phi 11 | d_save["phi"] = {} 12 | for key in filters["phi"].keys(): 13 | val = filters["phi"][key] 14 | if isinstance(val, torch.FloatTensor): 15 | val_numpy = val.cpu().numpy() 16 | d_save["phi"][key] = val_numpy 17 | # Save psi 18 | d_save["psi"] = [] 19 | for elem in filters["psi"]: 20 | d = {} 21 | for key in elem.keys(): 22 | val = elem[key] 23 | if isinstance(val, torch.FloatTensor): 24 | val_numpy = val.cpu().numpy() 25 | d[key] = val_numpy 26 | d_save["psi"].append(d) 27 | 28 | return d_save 29 | 30 | 31 | def run_scattering(X, use_cuda=False): 32 | 33 | # Ensure NCHW format 34 | assert X.shape[1] < min(X.shape[2:]) 35 | 36 | M, N = X.shape[2:] 37 | 38 | if use_cuda: 39 | 40 | scat = scattering.Scattering(M=M, N=N, J=2, check=True).cuda() 41 | list_S = scat.forward(torch.FloatTensor(X).cuda()) 42 | 43 | else: 44 | scat = scattering.Scattering(M=M, N=N, J=2, check=True) 45 | list_S = scat.forward(torch.FloatTensor(X)) 46 | 47 | return list_S 48 | -------------------------------------------------------------------------------- /ScatteringTransform/test/run_test.py: -------------------------------------------------------------------------------- 1 | import run_tf 2 | import run_pytorch 3 | import argparse 4 | import numpy as np 5 | import sys 6 | sys.path.append("../src/utils") 7 | import logging_utils as lu 8 | 9 | 10 | def test_filters_bank(M, N, J): 11 | 12 | d_tf = run_tf.run_filter_bank(M, N, J) 13 | d_pytorch = run_pytorch.run_filter_bank(M, N, J) 14 | 15 | for key in d_tf["phi"].keys(): 16 | arr_tf = d_tf["phi"][key] 17 | arr_pytorch = d_pytorch["phi"][key] 18 | try: 19 | assert np.all(np.isclose(arr_tf, arr_pytorch)) 20 | lu.print_bright_green("phi, key: %s" % key, "OK") 21 | except AssertionError: 22 | lu.print_bright_red("phi, key: %s" % key, "FAIL") 23 | return 24 | 25 | for idx, (elem_tf, elem_pytorch) in enumerate(zip(d_tf["psi"], d_pytorch["psi"])): 26 | for key in elem_tf: 27 | arr_tf = elem_tf[key] 28 | arr_pytorch = elem_pytorch[key] 29 | try: 30 | assert np.all(np.isclose(arr_tf, arr_pytorch)) 31 | lu.print_bright_green("psi, index: %s, key: %s" % (idx, key), "OK") 32 | except AssertionError: 33 | lu.print_bright_red("psi, index: %s, key: %s" % (idx, key), "FAIL") 34 | return 35 | 36 | lu.print_green("All filters equal") 37 | 38 | 39 | def test_scattering(M, N, J, use_cuda, use_XLA): 40 | 41 | np.random.seed(0) 42 | X = np.random.normal(0, 1, size=(16, 3, M, N)) 43 | 44 | S_pytorch = run_pytorch.run_scattering(X, use_cuda) 45 | S_tf = run_tf.run_scattering(X, use_XLA) 46 | 47 | for idx, (Stf, Spytorch) in enumerate(zip(S_tf, S_pytorch)): 48 | try: 49 | assert np.all(np.isclose(Stf, Spytorch, atol=1E-6)) 50 | lu.print_bright_green("S coeff, index: %s" % idx, "OK") 51 | except AssertionError: 52 | lu.print_bright_red("S coeff, index: %s" % idx, "FAIL") 53 | return 54 | 55 | lu.print_green("Scattering results equal") 56 | 57 | 58 | if __name__ == '__main__': 59 | 60 | parser = argparse.ArgumentParser() 61 | parser.add_argument('--M', type=int, default=32) 62 | parser.add_argument('--N', type=int, default=32) 63 | parser.add_argument('--J', type=int, default=2) 64 | parser.add_argument('--use_cuda', action="store_true") 65 | parser.add_argument('--use_XLA', action="store_true") 66 | 67 | args = parser.parse_args() 68 | 69 | test_filters_bank(args.M, args.N, args.J) 70 | test_scattering(args.M, args.N, args.J, args.use_cuda, args.use_XLA) 71 | -------------------------------------------------------------------------------- /ScatteringTransform/test/run_tf.py: -------------------------------------------------------------------------------- 1 | import os 2 | # Disable Tensorflow's INFO and WARNING messages 3 | # See http://stackoverflow.com/questions/35911252 4 | if 'TF_CPP_MIN_LOG_LEVEL' not in os.environ: 5 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 6 | import tensorflow as tf 7 | import sys 8 | sys.path.append("../src/utils") 9 | import filters_bank 10 | import scattering 11 | 12 | 13 | def run_filter_bank(M, N, J): 14 | 15 | filters = filters_bank.filters_bank(M, N, J) 16 | d_save = {} 17 | # Save phi 18 | d_save["phi"] = {} 19 | for key in filters["phi"].keys(): 20 | val = filters["phi"][key] 21 | if isinstance(val, tf.Tensor): 22 | val_numpy = val.eval(session=tf.Session()) 23 | d_save["phi"][key] = val_numpy 24 | # Save psi 25 | d_save["psi"] = [] 26 | for elem in filters["psi"]: 27 | d = {} 28 | for key in elem.keys(): 29 | val = elem[key] 30 | if isinstance(val, tf.Tensor): 31 | val_numpy = val.eval(session=tf.Session()) 32 | d[key] = val_numpy 33 | d_save["psi"].append(d) 34 | 35 | return d_save 36 | 37 | 38 | def run_scattering(X, use_XLA=False): 39 | 40 | # Ensure NCHW format 41 | assert X.shape[1] < min(X.shape[2:]) 42 | 43 | M, N = X.shape[2:] 44 | 45 | X_tf = tf.placeholder(tf.float32, shape=[None,] + list(X.shape[1:])) 46 | 47 | scat = scattering.Scattering(M=M, N=N, J=2, check=True) 48 | S = scat(X_tf) 49 | 50 | # Create session 51 | config = tf.ConfigProto() 52 | if use_XLA: 53 | config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 54 | sess = tf.Session(config=config) 55 | S_out = sess.run(S, feed_dict={X_tf: X}) 56 | 57 | return S_out 58 | -------------------------------------------------------------------------------- /Sobolev/README.md: -------------------------------------------------------------------------------- 1 | # Sobolev training for neural networks 2 | 3 | theano + lasagne implementation of [Sobolev Training for Neural Networks](https://arxiv.org/abs/1706.04859) 4 | 5 | ![Sobolev with 20 pts](figures/tang_20pts.gif) 6 | ![Sobolev with 100 pts](figures/tang_100pts.gif) 7 | 8 | 9 | ## Dependencies 10 | 11 | - Lasagne==0.2.dev1 12 | - Theano==0.9.0 13 | - numpy==1.13.3 14 | - tqdm==4.17.0 15 | - natsort==5.1.0 16 | - matplotlib==2.0.2 17 | 18 | 19 | ## Uage 20 | 21 | Main command: 22 | 23 | python main,py 24 | 25 | Arguments: 26 | 27 | 28 | --nb_epoch NB_EPOCH Number of training epochs 29 | --batch_size BATCH_SIZE 30 | Batch size 31 | --npts NPTS Number of training points 32 | --learning_rate LEARNING_RATE 33 | Learning rate 34 | --sobolev_weight SOBOLEV_WEIGHT 35 | How much do we weight the Sobolev function 36 | 37 | 38 | ## Run a batch of experiments 39 | 40 | bash run_experiments.sh 41 | 42 | 43 | ## Create gif 44 | 45 | bash run_experiments 46 | python make_gif.py 47 | 48 | 49 | ## Notes 50 | 51 | - The architecture of the NN is the same as in the original paper. 52 | - We plot the loss curves to give some more perspective. 53 | - Initially had a hard time reproducing results. Inspection of loss curves show you just have to train longer until Soboleb loss and MSE loss have similar magnitude. Or increase the weight on the Sobolev loss. 54 | -------------------------------------------------------------------------------- /Sobolev/figures/plot_10000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_10000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_10000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_10000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_1000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_1000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_1000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_1000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_100_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_100_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_100_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_100_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_10_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_10_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_10_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_10_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_50000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_50000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_50000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_50000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_5000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_5000_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_5000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_5000_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_500_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_500_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_500_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_500_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_50_epochs_100_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_50_epochs_100_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/plot_50_epochs_20_npts_0.0001_LR_1_sobolev_weight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/plot_50_epochs_20_npts_0.0001_LR_1_sobolev_weight.png -------------------------------------------------------------------------------- /Sobolev/figures/tang_100pts.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/tang_100pts.gif -------------------------------------------------------------------------------- /Sobolev/figures/tang_20pts.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/Sobolev/figures/tang_20pts.gif -------------------------------------------------------------------------------- /Sobolev/main.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import sobolev_training 3 | 4 | # Training settings 5 | parser = argparse.ArgumentParser(description='Sobolev experiments') 6 | 7 | # Training params 8 | parser.add_argument('--nb_epoch', default=100, type=int, help="Number of training epochs") 9 | parser.add_argument('--batch_size', default=8, type=int, help='Batch size') 10 | parser.add_argument('--npts', default=20, type=int, help="Number of training points") 11 | parser.add_argument('--learning_rate', default=1E-4, type=float, help="Learning rate") 12 | parser.add_argument('--sobolev_weight', default=1, type=float, help="How much do we weight the Sobolev function") 13 | args = parser.parse_args() 14 | 15 | 16 | sobolev_training.launch_experiments(args) 17 | -------------------------------------------------------------------------------- /Sobolev/make_gif.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import shlex 3 | import subprocess 4 | from natsort import natsorted 5 | 6 | 7 | list_files = glob.glob("figures/*") 8 | list_files_20pts = natsorted([f for f in list_files if "20_npts" in f]) 9 | list_files_100pts = natsorted([f for f in list_files if "100_npts" in f]) 10 | 11 | str_20pts = " ".join(list_files_20pts) 12 | str_100pts = " ".join(list_files_100pts) 13 | 14 | cmd = "convert -delay 80 -resize 600x600 -loop 0 %s figures/tang_20pts.gif" % str_20pts 15 | subprocess.call(shlex.split(cmd)) 16 | 17 | cmd = "convert -delay 80 -resize 600x600 -loop 0 %s figures/tang_100pts.gif" % str_100pts 18 | subprocess.call(shlex.split(cmd)) 19 | -------------------------------------------------------------------------------- /Sobolev/run_experiments.sh: -------------------------------------------------------------------------------- 1 | # python main.py --nb_epoch 10 --npts 20 2 | # python main.py --nb_epoch 50 --npts 20 3 | # python main.py --nb_epoch 100 --npts 20 4 | # python main.py --nb_epoch 500 --npts 20 5 | # python main.py --nb_epoch 1000 --npts 20 6 | # python main.py --nb_epoch 5000 --npts 20 7 | # python main.py --nb_epoch 10000 --npts 20 8 | # python main.py --nb_epoch 50000 --npts 20 9 | # python main.py --nb_epoch 50000 --npts 20 10 | 11 | # python main.py --nb_epoch 10 --npts 100 12 | # python main.py --nb_epoch 50 --npts 100 13 | # python main.py --nb_epoch 100 --npts 100 14 | # python main.py --nb_epoch 500 --npts 100 15 | # python main.py --nb_epoch 1000 --npts 100 16 | python main.py --nb_epoch 5000 --npts 100 17 | python main.py --nb_epoch 10000 --npts 100 18 | python main.py --nb_epoch 50000 --npts 100 -------------------------------------------------------------------------------- /Sobolev/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from tqdm import tqdm 3 | import lasagne 4 | import theano 5 | 6 | 7 | def create_dataset(npts): 8 | """ Sample data uniformly in a [-5,5] x [-5, 5] window""" 9 | 10 | # Create data 11 | np.random.seed(20) # set seed for reproducibility 12 | X = np.random.uniform(-5,5, (npts, 2)).astype(np.float32) 13 | 14 | return X 15 | 16 | 17 | def get_list_batches(npts, batch_size): 18 | """Create batches (i.e a list of index) such that an array of size npts 19 | is split in batches of size batch_size""" 20 | 21 | num_elem = npts 22 | num_batches = num_elem / batch_size 23 | # list_batches is a list of array. Each array contains the indeces of the batch elements. 24 | list_batches = np.array_split(np.arange(num_elem), num_batches) 25 | 26 | return list_batches 27 | 28 | 29 | def train_network(train_fn, X, list_batches, nb_epoch): 30 | 31 | # Store train loss for each epoch 32 | list_loss = [] 33 | 34 | for epoch in tqdm(range(nb_epoch), desc="Training normally"): 35 | 36 | # Store train loss for each batch 37 | epoch_losses = [] 38 | 39 | # Loop over batches 40 | for batch_idxs in list_batches: 41 | X_batch = X[batch_idxs] 42 | epoch_losses.append(train_fn(X_batch)) 43 | 44 | list_loss.append(np.mean(epoch_losses)) 45 | 46 | return list_loss 47 | 48 | 49 | def train_network_sobolev(train_fn, X, list_batches, nb_epoch): 50 | 51 | # Store train loss for each epoch 52 | list_loss = [] 53 | list_loss_J = [] 54 | 55 | for epoch in tqdm(range(nb_epoch), desc="Training with Sobolev"): 56 | 57 | epoch_losses = [] 58 | epoch_losses_J = [] 59 | 60 | for batch_idxs in list_batches: 61 | X_batch = X[batch_idxs] 62 | loss, J_loss = train_fn(X_batch) 63 | 64 | epoch_losses.append(loss) 65 | epoch_losses_J.append(J_loss) 66 | 67 | list_loss.append(np.mean(epoch_losses)) 68 | list_loss_J.append(np.mean(epoch_losses_J)) 69 | 70 | return list_loss, list_loss_J 71 | 72 | 73 | def get_prediction_fn(input_var, network): 74 | 75 | # Create a prediction function 76 | test_prediction = lasagne.layers.get_output(network, deterministic=True) 77 | predict_fn = theano.function([input_var], test_prediction) 78 | 79 | return predict_fn 80 | -------------------------------------------------------------------------------- /WGAN-GP/README.md: -------------------------------------------------------------------------------- 1 | # WGAN-GP 2 | 3 | Pure tensorflow implementation of WGAN-GP 4 | 5 | **Sources:** 6 | 7 | - [Improved Training of Wasserstein GANs](https://arxiv.org/abs/1704.00028) 8 | 9 | # Requirements 10 | 11 | ## python modules 12 | 13 | - terminaltables==3.1.0 14 | - colorama==0.3.9 15 | - matplotlib==2.0.2 16 | - tensorflow_gpu==1.3.0 17 | - tqdm==4.17.0 18 | - numpy==1.13.3 19 | - tensorflow==1.4.0rc1 20 | 21 | # Part 1. Getting the data 22 | 23 | - Go to http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 24 | - In the Downloads section, select Align&Cropped images. 25 | - In the dropbox page that follows, download the Anno, Eval and Img folders. 26 | - Copy these folders to `/data/raw`. 27 | - Extract the zip files. 28 | 29 | We are only going to use the images 30 | 31 | # Part 2. Running the code 32 | 33 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WGAN-GP/src/model) 34 | 35 | -------------------------------------------------------------------------------- /WGAN-GP/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /WGAN-GP/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /WGAN-GP/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WGAN-GP/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WGAN-GP/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WGAN-GP/src/model) for training scripts 8 | -------------------------------------------------------------------------------- /WGAN-GP/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training GAN 2 | 3 | ## Usage 4 | 5 | `python main.py` 6 | 7 | 8 | optional arguments: 9 | 10 | -h, --help show this help message and exit 11 | --run RUN Which operation to run. [train|inference] 12 | --nb_epoch NB_EPOCH Number of epochs 13 | --batch_size BATCH_SIZE 14 | Number of samples per batch. 15 | --nb_batch_per_epoch NB_BATCH_PER_EPOCH 16 | Number of batches per epoch 17 | --learning_rate LEARNING_RATE 18 | Learning rate used for AdamOptimizer 19 | --noise_dim NOISE_DIM 20 | Noise dimension for GAN generation 21 | --random_seed RANDOM_SEED 22 | Seed used to initialize rng. 23 | --use_XLA [USE_XLA] Whether to use XLA compiler. 24 | --nouse_XLA 25 | --num_threads NUM_THREADS 26 | Number of threads to fetch the data 27 | --capacity_factor CAPACITY_FACTOR 28 | Nuumber of batches to store in queue 29 | --data_format DATA_FORMAT 30 | Tensorflow image data format. 31 | --celebA_path CELEBA_PATH 32 | Path to celebA images 33 | --channels CHANNELS Number of channels 34 | --central_fraction CENTRAL_FRACTION 35 | Central crop as a fraction of total image 36 | --img_size IMG_SIZE Image size 37 | --model_dir MODEL_DIR 38 | Output folder where checkpoints are dumped. 39 | --log_dir LOG_DIR Logs for tensorboard. 40 | --fig_dir FIG_DIR Where to save figures. 41 | --raw_dir RAW_DIR Where raw data is saved 42 | --data_dir DATA_DIR Where processed data is saved 43 | 44 | ## Example 45 | 46 | python main.py --run train --use_XLA 47 | 48 | *N.B.* At this stage, only `data_format = NHWC` has been tested. It is recommended to use the `XLA` option to improve runtime speed. 49 | 50 | ## To Do: 51 | 52 | Check validity of both data format and batch norm implementation -------------------------------------------------------------------------------- /WGAN-GP/src/model/flags.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | 4 | FLAGS = tf.app.flags.FLAGS 5 | 6 | 7 | def define_flags(): 8 | 9 | ############ 10 | # Run mode 11 | ############ 12 | tf.app.flags.DEFINE_string('run', None, "Which operation to run. [train|inference]") 13 | 14 | ########################## 15 | # Training parameters 16 | ########################### 17 | tf.app.flags.DEFINE_integer('nb_epoch', 400, "Number of epochs") 18 | tf.app.flags.DEFINE_integer('batch_size', 256, "Number of samples per batch.") 19 | tf.app.flags.DEFINE_integer('nb_batch_per_epoch', 50, "Number of batches per epoch") 20 | tf.app.flags.DEFINE_float('learning_rate', 2E-4, "Learning rate used for AdamOptimizer") 21 | tf.app.flags.DEFINE_integer('noise_dim', 100, "Noise dimension for GAN generation") 22 | tf.app.flags.DEFINE_integer('random_seed', 0, "Seed used to initialize rng.") 23 | 24 | ############################################ 25 | # General tensorflow parameters parameters 26 | ############################################# 27 | tf.app.flags.DEFINE_bool('use_XLA', False, "Whether to use XLA compiler.") 28 | tf.app.flags.DEFINE_integer('num_threads', 2, "Number of threads to fetch the data") 29 | tf.app.flags.DEFINE_float('capacity_factor', 32, "Nuumber of batches to store in queue") 30 | 31 | ########## 32 | # Datasets 33 | ########## 34 | tf.app.flags.DEFINE_string('data_format', "NHWC", "Tensorflow image data format.") 35 | tf.app.flags.DEFINE_string('celebA_path', "../../data/raw/img_align_celeba", "Path to celebA images") 36 | tf.app.flags.DEFINE_integer('channels', 3, "Number of channels") 37 | tf.app.flags.DEFINE_float('central_fraction', 0.8, "Central crop as a fraction of total image") 38 | tf.app.flags.DEFINE_integer('img_size', 64, "Image size") 39 | 40 | ############## 41 | # Directories 42 | ############## 43 | tf.app.flags.DEFINE_string('model_dir', '../../models', "Output folder where checkpoints are dumped.") 44 | tf.app.flags.DEFINE_string('log_dir', '../../logs', "Logs for tensorboard.") 45 | tf.app.flags.DEFINE_string('fig_dir', '../../figures', "Where to save figures.") 46 | tf.app.flags.DEFINE_string('raw_dir', '../../data/raw', "Where raw data is saved") 47 | tf.app.flags.DEFINE_string('data_dir', '../../data/processed', "Where processed data is saved") 48 | -------------------------------------------------------------------------------- /WGAN-GP/src/model/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | # Disable Tensorflow's INFO and WARNING messages 3 | # See http://stackoverflow.com/questions/35911252 4 | if 'TF_CPP_MIN_LOG_LEVEL' not in os.environ: 5 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 6 | import flags 7 | import tensorflow as tf 8 | import train_wgan_GP 9 | 10 | FLAGS = tf.app.flags.FLAGS 11 | 12 | 13 | def launch_training(): 14 | 15 | train_wgan_GP.train_model() 16 | 17 | 18 | def main(argv=None): 19 | 20 | assert FLAGS.run in ["train", "inference"], "Choose [train|inference]" 21 | 22 | if FLAGS.run == 'train': 23 | launch_training() 24 | 25 | 26 | if __name__ == '__main__': 27 | flags.define_flags() 28 | tf.app.run() 29 | -------------------------------------------------------------------------------- /WGAN-GP/src/utils/data_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | import numpy as np 4 | import tensorflow as tf 5 | 6 | 7 | def normalize_image(image): 8 | 9 | image = tf.cast(image, tf.float32) / 255. 10 | image = (image - 0.5) / 0.5 11 | return image 12 | 13 | 14 | def unnormalize_image(image, name=None): 15 | 16 | image = (image * 0.5 + 0.5) * 255. 17 | image = tf.cast(image, tf.uint8, name=name) 18 | return image 19 | 20 | 21 | def input_data(sess): 22 | 23 | FLAGS = tf.app.flags.FLAGS 24 | 25 | list_images = glob.glob(os.path.join(FLAGS.celebA_path, "*.jpg")) 26 | 27 | # Read each JPEG file 28 | 29 | with tf.device('/cpu:0'): 30 | 31 | reader = tf.WholeFileReader() 32 | filename_queue = tf.train.string_input_producer(list_images) 33 | key, value = reader.read(filename_queue) 34 | channels = FLAGS.channels 35 | image = tf.image.decode_jpeg(value, channels=channels, name="dataset_image") 36 | image.set_shape([None, None, channels]) 37 | 38 | # Crop and other random augmentations 39 | image = tf.image.random_flip_left_right(image) 40 | # image = tf.image.random_saturation(image, .95, 1.05) 41 | # image = tf.image.random_brightness(image, .05) 42 | # image = tf.image.random_contrast(image, .95, 1.05) 43 | 44 | # Center crop 45 | image = tf.image.central_crop(image, FLAGS.central_fraction) 46 | 47 | # Resize 48 | image = tf.image.resize_images(image, (FLAGS.img_size, FLAGS.img_size), method=tf.image.ResizeMethod.AREA) 49 | 50 | # Normalize 51 | image = normalize_image(image) 52 | 53 | # Format image to correct ordering 54 | if FLAGS.data_format == "NCHW": 55 | image = tf.transpose(image, (2,0,1)) 56 | 57 | # Using asynchronous queues 58 | img_batch = tf.train.batch([image], 59 | batch_size=FLAGS.batch_size, 60 | num_threads=FLAGS.num_threads, 61 | capacity=2 * FLAGS.num_threads * FLAGS.batch_size, 62 | name='X_real_input') 63 | 64 | return img_batch 65 | 66 | 67 | def sample_batch(X, batch_size): 68 | 69 | idx = np.random.choice(X.shape[0], batch_size, replace=False) 70 | return X[idx] 71 | -------------------------------------------------------------------------------- /WGAN-GP/src/utils/logging_utils.py: -------------------------------------------------------------------------------- 1 | from colorama import init 2 | from colorama import Fore, Back, Style 3 | import tensorflow as tf 4 | from terminaltables import SingleTable 5 | 6 | 7 | def print_table(TABLE_DATA): 8 | 9 | table_instance = SingleTable(TABLE_DATA, "") 10 | table_instance.justify_columns[2] = 'right' 11 | print(table_instance.table) 12 | print 13 | 14 | 15 | def print_bright(s): 16 | 17 | init() 18 | print(Style.BRIGHT + s + Style.RESET_ALL) 19 | 20 | 21 | def print_green(info, value=""): 22 | 23 | print(Fore.GREEN + "[%s] " % info + Style.RESET_ALL + str(value)) 24 | 25 | 26 | def print_red(info, value=""): 27 | 28 | print(Fore.RED + "[%s] " % info + Style.RESET_ALL + str(value)) 29 | 30 | 31 | def print_session(): 32 | 33 | FLAGS = tf.app.flags.FLAGS 34 | 35 | print_bright("\nSetting up TF session:") 36 | for key in FLAGS.__dict__["__flags"].keys(): 37 | if "dir" not in key: 38 | print_green(key, FLAGS.__dict__["__flags"][key]) 39 | 40 | print_bright("\nConfiguring directories:") 41 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 42 | # Clear directories by default 43 | if tf.gfile.Exists(d): 44 | print_red("Deleting", d) 45 | tf.gfile.DeleteRecursively(d) 46 | 47 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 48 | print_green("Creating", d) 49 | tf.gfile.MakeDirs(d) 50 | 51 | 52 | def print_initialize(): 53 | 54 | print_bright("\nInitialization:") 55 | print_green("Created session saver") 56 | print_green("Ran init ops") 57 | 58 | 59 | def print_summaries(): 60 | 61 | print_bright("\nSummaries:") 62 | list_summaries = tf.get_collection(tf.GraphKeys.SUMMARIES) 63 | for t in list_summaries: 64 | print_green(t.name) 65 | 66 | 67 | def print_queues(): 68 | 69 | print_bright("\nQueues:") 70 | print_green("Created coordinator") 71 | print_green("Started queue runner") 72 | 73 | 74 | def print_check_data(out, list_data): 75 | 76 | print 77 | TABLE_DATA = (('Variable Name', 'Shape', "Min value", "Max value"),) 78 | for o, t in zip(out, list_data): 79 | TABLE_DATA += (tuple([t.name, str(o.shape), "%.3g" % o.min(), "%.3g" % o.max()]),) 80 | print_table(TABLE_DATA) 81 | -------------------------------------------------------------------------------- /WGAN-GP/src/utils/objectives.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | def mae(pred, target, name='mae'): 5 | 6 | return tf.reduce_mean(tf.abs(pred - target), name=name) 7 | 8 | 9 | def mse(pred, target, name='mse'): 10 | 11 | return tf.reduce_mean(tf.square(pred - target), name=name) 12 | 13 | 14 | def pixel_rmse(pred, target, name='rmse'): 15 | 16 | return tf.sqrt(tf.reduce_mean(tf.square(pred - target), name=name)) 17 | 18 | 19 | def binary_cross_entropy_with_logits(pred, target): 20 | 21 | return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=target)) 22 | 23 | 24 | def wasserstein(pred, target): 25 | 26 | return tf.reduce_mean(pred * target) 27 | -------------------------------------------------------------------------------- /WGAN-GP/src/utils/training_utils.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import random 3 | import numpy as np 4 | import sys 5 | sys.path.append("../utils") 6 | import logging_utils as lu 7 | 8 | 9 | def setup_session(): 10 | 11 | lu.print_session() 12 | 13 | FLAGS = tf.app.flags.FLAGS 14 | 15 | # Create session 16 | config = tf.ConfigProto() 17 | if FLAGS.use_XLA: 18 | config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 19 | sess = tf.Session(config=config) 20 | 21 | # Setup directory to save model 22 | for d in [FLAGS.log_dir, FLAGS.model_dir, FLAGS.fig_dir]: 23 | # Clear directories by default 24 | if tf.gfile.Exists(d): 25 | tf.gfile.DeleteRecursively(d) 26 | tf.gfile.MakeDirs(d) 27 | 28 | # Initialize all RNGs with a deterministic seed 29 | with sess.graph.as_default(): 30 | tf.set_random_seed(FLAGS.random_seed) 31 | 32 | random.seed(FLAGS.random_seed) 33 | np.random.seed(FLAGS.random_seed) 34 | 35 | return sess 36 | 37 | 38 | def initialize_session(sess): 39 | 40 | saver = tf.train.Saver() 41 | 42 | init_op = tf.group(tf.global_variables_initializer(), 43 | tf.local_variables_initializer()) 44 | sess.run(init_op) 45 | 46 | lu.print_initialize() 47 | 48 | return saver 49 | 50 | 51 | def add_gradient_summary(list_gradvar): 52 | # Add summary for gradients 53 | for g,v in list_gradvar: 54 | if g is not None: 55 | tf.summary.histogram(v.name + "/gradient", g) 56 | 57 | 58 | def manage_queues(sess): 59 | 60 | coord = tf.train.Coordinator() 61 | tf.train.start_queue_runners(sess=sess, coord=coord) 62 | 63 | lu.print_queues() 64 | 65 | return coord 66 | 67 | 68 | def manage_summaries(sess): 69 | 70 | FLAGS = tf.app.flags.FLAGS 71 | writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) 72 | 73 | lu.print_summaries() 74 | 75 | return writer 76 | 77 | 78 | def check_data(out, list_data): 79 | 80 | lu.print_check_data(out, list_data) 81 | -------------------------------------------------------------------------------- /WGAN-GP/src/utils/visualization_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pylab as plt 5 | 6 | FLAGS = tf.app.flags.FLAGS 7 | 8 | 9 | def save_image(data, data_format, e, suffix=None): 10 | """Saves a picture showing the current progress of the model""" 11 | 12 | X_G, X_real = data 13 | 14 | Xg = X_G[:8] 15 | Xr = X_real[:8] 16 | 17 | if data_format == "NHWC": 18 | X = np.concatenate((Xg, Xr), axis=0) 19 | list_rows = [] 20 | for i in range(int(X.shape[0] / 4)): 21 | Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=1) 22 | list_rows.append(Xr) 23 | 24 | Xr = np.concatenate(list_rows, axis=0) 25 | 26 | if data_format == "NCHW": 27 | X = np.concatenate((Xg, Xr), axis=0) 28 | list_rows = [] 29 | for i in range(int(X.shape[0] / 4)): 30 | Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=2) 31 | list_rows.append(Xr) 32 | 33 | Xr = np.concatenate(list_rows, axis=1) 34 | Xr = Xr.transpose(1,2,0) 35 | 36 | if Xr.shape[-1] == 1: 37 | plt.imshow(Xr[:, :, 0], cmap="gray") 38 | else: 39 | plt.imshow(Xr) 40 | plt.axis("off") 41 | if suffix is None: 42 | plt.savefig(os.path.join(FLAGS.fig_dir, "current_batch_%s.png" % e)) 43 | else: 44 | plt.savefig(os.path.join(FLAGS.fig_dir, "current_batch_%s_%s.png" % (suffix, e))) 45 | plt.clf() 46 | plt.close() 47 | 48 | 49 | def get_stacked_tensor(X1, X2): 50 | 51 | X = tf.concat((X1[:16], X2[:16]), axis=0) 52 | list_rows = [] 53 | for i in range(8): 54 | Xr = tf.concat([X[k] for k in range(4 * i, 4 * (i + 1))], axis=2) 55 | list_rows.append(Xr) 56 | 57 | X = tf.concat(list_rows, axis=1) 58 | X = tf.transpose(X, (1,2,0)) 59 | X = tf.expand_dims(X, 0) 60 | 61 | return X 62 | -------------------------------------------------------------------------------- /WassersteinGAN/README.md: -------------------------------------------------------------------------------- 1 | # Generative Adversarial Networks 2 | 3 | Keras implementation of WassersteinGAN. 4 | 5 | **Sources:** 6 | 7 | - [Wasserstein GAN](https://arxiv.org/abs/1701.07875) 8 | 9 | # Requirements 10 | 11 | ## python modules 12 | 13 | numpy==1.13.3 14 | natsort==5.1.0 15 | matplotlib==2.0.2 16 | opencv_python==3.3.0.10 17 | scipy==1.0.0 18 | tqdm==4.17.0 19 | Keras==2.0.8 20 | parmap==1.5.1 21 | h5py==2.7.0 22 | Theano==0.9.0 or tensorflow==1.3.0 23 | 24 | # Part 1. Processing the data 25 | 26 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WassersteinGAN/src/data). 27 | 28 | # Part 2. Running the code 29 | 30 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WassersteinGAN/src/model) 31 | 32 | # Part 3. Example results 33 | 34 | **CelebA example results** 35 | 36 | ![figure](./figures/img_celebA_1.png) 37 | ![figure](./figures/img_celebA_2.png) 38 | 39 | For each image: 40 | 41 | - The first 2 rows are generated images 42 | - The last 2 rows are real images 43 | 44 | 45 | **MoG** 46 | 47 | Results on the unrolled GAN paper to dataset: 48 | 49 | ![figure](./figures/MoG_dataset.gif) 50 | 51 | -------------------------------------------------------------------------------- /WassersteinGAN/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /WassersteinGAN/data/processed/README.md: -------------------------------------------------------------------------------- 1 | # Repository for processed data -------------------------------------------------------------------------------- /WassersteinGAN/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /WassersteinGAN/figures/MoG_dataset.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/WassersteinGAN/figures/MoG_dataset.gif -------------------------------------------------------------------------------- /WassersteinGAN/figures/img_celebA_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/WassersteinGAN/figures/img_celebA_1.png -------------------------------------------------------------------------------- /WassersteinGAN/figures/write_gif_script.py: -------------------------------------------------------------------------------- 1 | import glob 2 | from natsort import natsorted 3 | 4 | list_toy = natsorted(glob.glob("*toy_dataset_iter*")) 5 | list_toy_str = " ".join(list_toy) 6 | 7 | with open("make_gif.sh", "w") as f: 8 | 9 | cmd = "convert -delay 15 -resize 300x300 -loop 0 %s MoG_dataset.gif" % list_toy_str 10 | f.write(cmd) 11 | -------------------------------------------------------------------------------- /WassersteinGAN/models/README.md: -------------------------------------------------------------------------------- 1 | # Directory to store model weights -------------------------------------------------------------------------------- /WassersteinGAN/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WassersteinGAN/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WassersteinGAN/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/WassersteinGAN/src/model) for training scripts 8 | -------------------------------------------------------------------------------- /WassersteinGAN/src/data/README.md: -------------------------------------------------------------------------------- 1 | # Building the data 2 | 3 | # Step 1. Download CelebA 4 | 5 | - Go to http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 6 | - In the Downloads section, select Align&Cropped images. 7 | - In the dropbox page that follows, download the Anno, Eval and Img folders. 8 | - Copy these folders to `Colorful/data/raw`. 9 | - Extract the zip files. 10 | 11 | You should have the following folder structure: 12 | 13 | ├── Anno 14 | ├── list_attr_celeba.txt 15 | ├── list_bbox_celeba.txt 16 | ├── list_landmarks_align_celeba.txt 17 | ├── list_landmarks_celeba.txt 18 | ├── Eval 19 | ├──list_eval_partition.txt 20 | ├── img_align_celeba 21 | ├──lots of images 22 | 23 | At this stage, annotations are not used. 24 | 25 | 26 | # Step 2. Build HDF5 CelebA dataset 27 | 28 | `python make_dataset.py` 29 | 30 | positional arguments: 31 | jpeg_dir path to celeba jpeg images 32 | optional arguments: 33 | -h, --help show this help message and exit 34 | --img_size IMG_SIZE Desired Width == Height 35 | --do_plot DO_PLOT Plot the images to make sure the data processing went 36 | OK 37 | 38 | 39 | **Example:** 40 | 41 | `python make_dataset.py --img_size 64` -------------------------------------------------------------------------------- /WassersteinGAN/src/data/make_dataset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | import h5py 4 | import glob 5 | import parmap 6 | import argparse 7 | import numpy as np 8 | from tqdm import tqdm as tqdm 9 | import matplotlib.pylab as plt 10 | 11 | 12 | def format_image(img_path, size): 13 | """ 14 | Load img with opencv and reshape 15 | """ 16 | 17 | img_color = cv2.imread(img_path) 18 | img_color = img_color[:, :, ::-1] 19 | 20 | # Slice image to center around face 21 | img_color = img_color[30:-30, 20:-20, :] 22 | 23 | img_color = cv2.resize(img_color, (size, size), interpolation=cv2.INTER_AREA) 24 | 25 | img_color = img_color.reshape((1, size, size, 3)).transpose(0, 3, 1, 2) 26 | 27 | return img_color 28 | 29 | 30 | def build_HDF5(jpeg_dir, size=64): 31 | """ 32 | Gather the data in a single HDF5 file. 33 | """ 34 | 35 | # Put train data in HDF5 36 | hdf5_file = os.path.join(data_dir, "CelebA_%s_data.h5" % size) 37 | with h5py.File(hdf5_file, "w") as hfw: 38 | 39 | list_img = glob.glob(os.path.join(jpeg_dir, "*.jpg")) 40 | list_img = np.array(list_img) 41 | 42 | data_color = hfw.create_dataset("data", 43 | (0, 3, size, size), 44 | maxshape=(None, 3, size, size), 45 | dtype=np.uint8) 46 | 47 | num_files = len(list_img) 48 | chunk_size = 2000 49 | num_chunks = num_files / chunk_size 50 | arr_chunks = np.array_split(np.arange(num_files), num_chunks) 51 | 52 | for chunk_idx in tqdm(arr_chunks): 53 | 54 | list_img_path = list_img[chunk_idx].tolist() 55 | output = parmap.map(format_image, list_img_path, size, pm_parallel=True) 56 | 57 | arr_img_color = np.concatenate(output, axis=0) 58 | 59 | # Resize HDF5 dataset 60 | data_color.resize(data_color.shape[0] + arr_img_color.shape[0], axis=0) 61 | 62 | data_color[-arr_img_color.shape[0]:] = arr_img_color.astype(np.uint8) 63 | 64 | 65 | def check_HDF5(size=64): 66 | """ 67 | Plot images with landmarks to check the processing 68 | """ 69 | 70 | # Get hdf5 file 71 | hdf5_file = os.path.join(data_dir, "CelebA_%s_data.h5" % size) 72 | 73 | with h5py.File(hdf5_file, "r") as hf: 74 | data_color = hf["data"] 75 | for i in range(data_color.shape[0]): 76 | plt.figure() 77 | img = data_color[i, :, :, :].transpose(1,2,0) 78 | plt.imshow(img) 79 | plt.show() 80 | plt.clf() 81 | plt.close() 82 | 83 | 84 | if __name__ == '__main__': 85 | 86 | parser = argparse.ArgumentParser(description='Build dataset') 87 | parser.add_argument('jpeg_dir', type=str, help='path to celeba jpeg images') 88 | parser.add_argument('--img_size', default=64, type=int, 89 | help='Desired Width == Height') 90 | parser.add_argument('--do_plot', default=False, type=bool, 91 | help='Plot the images to make sure the data processing went OK') 92 | args = parser.parse_args() 93 | 94 | data_dir = "../../data/processed" 95 | 96 | build_HDF5(args.jpeg_dir, size=args.img_size) 97 | 98 | if args.do_plot: 99 | check_HDF5(args.img_size) 100 | -------------------------------------------------------------------------------- /WassersteinGAN/src/utils/batch_utils.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import multiprocessing 4 | import os 5 | import h5py 6 | import matplotlib.pylab as plt 7 | import matplotlib.gridspec as gridspec 8 | from matplotlib.pyplot import cm 9 | 10 | 11 | class DataGenerator(object): 12 | """ 13 | Generate minibatches with real-time data parallel augmentation on CPU 14 | 15 | args : 16 | hdf5_file (str) path to data in HDF5 format 17 | batch_size (int) Minibatch size 18 | dset (str) train/test/valid, the name of the dset to iterate over 19 | maxproc (int) max number of processes to spawn in parallel 20 | num_cached (int) max number of batches to keep in queue 21 | 22 | yields : 23 | X, y (minibatch data and labels as np arrays) 24 | """ 25 | 26 | def __init__(self, 27 | hdf5_file, 28 | batch_size=32, 29 | nb_classes=12, 30 | dset="training", 31 | maxproc=8, 32 | num_cached=10): 33 | 34 | # Check file exists 35 | assert os.path.isfile(hdf5_file), hdf5_file + " doesn't exist" 36 | 37 | # Initialize class internal variables 38 | self.dset = dset 39 | self.maxproc = maxproc 40 | self.hdf5_file = hdf5_file 41 | self.batch_size = batch_size 42 | self.num_cached = num_cached 43 | self.nb_classes = nb_classes 44 | 45 | # Dict that will store all transformations and their parameters 46 | self.d_transform = {} 47 | 48 | # Read the data file to get dataset shape information 49 | with h5py.File(self.hdf5_file, "r") as hf: 50 | self.X_shape = hf["data"].shape 51 | assert len(self.X_shape) == 4,\ 52 | ("\n\nImg data should be formatted as: \n" 53 | "(n_samples, n_channels, Height, Width)") 54 | self.n_samples = hf["data"].shape[0] 55 | # Verify n_channels is at index 1 56 | assert self.X_shape[-3] < min(self.X_shape[-2:]),\ 57 | ("\n\nImg data should be formatted as: \n" 58 | "(n_samples, n_channels, Height, Width)") 59 | 60 | # Save the class internal variables to a config dict 61 | self.d_config = {} 62 | self.d_config["hdf5_file"] = hdf5_file 63 | self.d_config["batch_size"] = batch_size 64 | self.d_config["dset"] = dset 65 | self.d_config["num_cached"] = num_cached 66 | self.d_config["maxproc"] = maxproc 67 | self.d_config["data_shape"] = self.X_shape 68 | 69 | def get_config(self): 70 | 71 | return self.d_config 72 | 73 | def gen_batch_inmemory_GAN(self, X_real, batch_size=None): 74 | """Generate batch, assuming X is loaded in memory in the main program""" 75 | 76 | while True: 77 | 78 | bs = self.batch_size 79 | if batch_size is not None: 80 | bs = batch_size 81 | 82 | # Select idx at random for the batch 83 | idx = np.random.choice(X_real.shape[0], bs, replace=False) 84 | X_batch_real = X_real[idx] 85 | 86 | yield X_batch_real 87 | -------------------------------------------------------------------------------- /WassersteinGAN/src/utils/general_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def remove_files(files): 5 | """ 6 | Remove files from disk 7 | 8 | args: files (str or list) remove all files in 'files' 9 | """ 10 | 11 | if isinstance(files, (list, tuple)): 12 | for f in files: 13 | if os.path.isfile(os.path.expanduser(f)): 14 | os.remove(f) 15 | elif isinstance(files, str): 16 | if os.path.isfile(os.path.expanduser(files)): 17 | os.remove(files) 18 | 19 | 20 | def create_dir(dirs): 21 | """ 22 | Create directory 23 | 24 | args: dirs (str or list) create all dirs in 'dirs' 25 | """ 26 | 27 | if isinstance(dirs, (list, tuple)): 28 | for d in dirs: 29 | if not os.path.exists(os.path.expanduser(d)): 30 | os.makedirs(d) 31 | elif isinstance(dirs, str): 32 | if not os.path.exists(os.path.expanduser(dirs)): 33 | os.makedirs(dirs) 34 | 35 | 36 | def setup_logging(model_name): 37 | 38 | model_dir = "../../models" 39 | # Output path where we store experiment log and weights 40 | model_dir = os.path.join(model_dir, model_name) 41 | 42 | fig_dir = "../../figures" 43 | 44 | # Create if it does not exist 45 | create_dir([model_dir, fig_dir]) 46 | -------------------------------------------------------------------------------- /pix2pix/README.md: -------------------------------------------------------------------------------- 1 | # pix2pix 2 | 3 | Keras implementation of [Image-to-Image Translation with Conditional Adversarial Networks](https://arxiv.org/pdf/1611.07004v1.pdf) by Phillip Isola, Jun-Yan Zhu, Tinghui Zhou, Alexei A. Efros 4 | 5 | 6 | # Requirements 7 | 8 | ## python modules 9 | 10 | - numpy==1.13.3 11 | - Keras==2.0.8 12 | - parmap==1.5.1 13 | - matplotlib==2.0.2 14 | - tqdm==4.17.0 15 | - opencv_python==3.3.0.10 16 | - h5py==2.7.0 17 | - theano==0.9.0 or tensorflow==1.3.0 18 | 19 | 20 | # Part 1. Processing the data 21 | 22 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/pix2pix/src/data). 23 | 24 | # Part 2. Running the code 25 | 26 | Follow [these instructions](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/pix2pix/src/model) 27 | 28 | # Part 3. Example results 29 | 30 | **Facade example results (validation set)** 31 | 32 | ![figure](./figures/img_pix2pix.png) 33 | 34 | For each image: 35 | 36 | - The first row is the generator input 37 | - The second row is the generator output 38 | - The last row is the original image 39 | -------------------------------------------------------------------------------- /pix2pix/data/README.md: -------------------------------------------------------------------------------- 1 | # Data repository -------------------------------------------------------------------------------- /pix2pix/data/processed/README.md: -------------------------------------------------------------------------------- 1 | # Repository for processed data -------------------------------------------------------------------------------- /pix2pix/data/raw/README.md: -------------------------------------------------------------------------------- 1 | # Repository for raw data -------------------------------------------------------------------------------- /pix2pix/figures/img_pix2pix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tdeboissiere/DeepLearningImplementations/5c4094880fc6992cca2d6e336463b2525026f6e2/pix2pix/figures/img_pix2pix.png -------------------------------------------------------------------------------- /pix2pix/models/README.md: -------------------------------------------------------------------------------- 1 | # Directory to store model weights -------------------------------------------------------------------------------- /pix2pix/src/README.md: -------------------------------------------------------------------------------- 1 | # src folder 2 | 3 | Go to: 4 | 5 | - [data](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/pix2pix/src/data) for the data processing scripts 6 | - [utils](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/pix2pix/src/utils) for the various scripting utilities used throughout 7 | - [model](https://github.com/tdeboissiere/DeepLearningImplementations/tree/master/pix2pix/src/model) for training scripts 8 | -------------------------------------------------------------------------------- /pix2pix/src/data/README.md: -------------------------------------------------------------------------------- 1 | # Building the data 2 | 3 | # Step 1. Download facades dataset 4 | 5 | - `git clone https://github.com/phillipi/pix2pix.git` 6 | - `cd pix2pix` 7 | - `bash ./datasets/download_dataset.sh facades` 8 | 9 | 10 | You should have the following folder structure: 11 | 12 | ├── facades 13 | ├── train 14 | ├── test 15 | ├── val 16 | 17 | 18 | # Step 2. Build HDF5 facades dataset 19 | 20 | `python make_dataset.py` 21 | 22 | positional arguments: 23 | 24 | jpeg_dir path to jpeg images 25 | nb_channels number of image channels 26 | 27 | optional arguments: 28 | 29 | -h, --help show this help message and exit 30 | --img_size IMG_SIZE Desired Width == Height 31 | --do_plot Plot the images to make sure the data processing went 32 | OK 33 | 34 | 35 | 36 | **Example:** 37 | 38 | `python make_dataset.py /home/user/GitHub/pix2pix/datasets/facades 3 --img_size 256 --do_plot True` -------------------------------------------------------------------------------- /pix2pix/src/model/README.md: -------------------------------------------------------------------------------- 1 | # Training and evaluating 2 | 3 | ## Training 4 | 5 | `python main.py` 6 | 7 | positional arguments: 8 | 9 | patch_size Patch size for D 10 | 11 | optional arguments: 12 | 13 | -h, --help show this help message and exit 14 | --backend BACKEND theano or tensorflow 15 | --generator GENERATOR 16 | upsampling or deconv 17 | --dset DSET facades 18 | --batch_size BATCH_SIZE 19 | Batch size 20 | --n_batch_per_epoch N_BATCH_PER_EPOCH 21 | Number of batches per epoch 22 | --nb_epoch NB_EPOCH Number of training epochs 23 | --epoch EPOCH Epoch at which weights were saved for evaluation 24 | --nb_classes NB_CLASSES 25 | Number of classes 26 | --do_plot Debugging plot 27 | --bn_mode BN_MODE Batch norm mode 28 | --img_dim IMG_DIM Image width == height 29 | --use_mbd Whether to use minibatch discrimination 30 | --use_label_smoothing 31 | Whether to smooth the positive labels when training D 32 | --label_flipping LABEL_FLIPPING 33 | Probability (0 to 1.) to flip the labels when training D 34 | --logging_dir LOGGING_DIR 35 | Path to logging directory 36 | 37 | **Example:** 38 | 39 | `python main.py 64 64` 40 | 41 | 42 | ### Expected outputs: 43 | 44 | - Weights are saved in pix2pix/models 45 | - Figures are saved in pix2pix/figures 46 | - Save model weights every few epochs 47 | 48 | ### Additional notes 49 | 50 | You can choose the type of generator: 51 | 52 | - The image dimension must be a multiple of the patch size (e.g. 256 is a multiple of 64) 53 | - In the discriminator, each patch goes through the same feature extractor. Then the outputs are combined with a new dense layer + softmax 54 | - `upsampling:` generate the image with a series of `Upsampling2D` and `Convolution2D` operations 55 | - `deconv:` use keras' transposed convolutions `Deconvolution2D`. This is closer to the original DCGAN implementation. 56 | 57 | At this stage, `deconv` only works with the `tensorflow` backend. -------------------------------------------------------------------------------- /pix2pix/src/model/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | import argparse 3 | 4 | 5 | def launch_training(**kwargs): 6 | 7 | # Launch training 8 | train.train(**kwargs) 9 | 10 | 11 | if __name__ == "__main__": 12 | 13 | parser = argparse.ArgumentParser(description='Train model') 14 | parser.add_argument('patch_size', type=int, nargs=2, action="store", help="Patch size for D") 15 | parser.add_argument('--backend', type=str, default="theano", help="theano or tensorflow") 16 | parser.add_argument('--generator', type=str, default="upsampling", help="upsampling or deconv") 17 | parser.add_argument('--dset', type=str, default="facades", help="facades") 18 | parser.add_argument('--batch_size', default=4, type=int, help='Batch size') 19 | parser.add_argument('--n_batch_per_epoch', default=100, type=int, help="Number of batches per epoch") 20 | parser.add_argument('--nb_epoch', default=400, type=int, help="Number of training epochs") 21 | parser.add_argument('--epoch', default=10, type=int, help="Epoch at which weights were saved for evaluation") 22 | parser.add_argument('--nb_classes', default=2, type=int, help="Number of classes") 23 | parser.add_argument('--do_plot', action="store_true", help="Debugging plot") 24 | parser.add_argument('--bn_mode', default=2, type=int, help="Batch norm mode") 25 | parser.add_argument('--img_dim', default=64, type=int, help="Image width == height") 26 | parser.add_argument('--use_mbd', action="store_true", help="Whether to use minibatch discrimination") 27 | parser.add_argument('--use_label_smoothing', action="store_true", help="Whether to smooth the positive labels when training D") 28 | parser.add_argument('--label_flipping', default=0, type=float, help="Probability (0 to 1.) to flip the labels when training D") 29 | parser.add_argument('--logging_dir', default='../..', type=str, help="Path to logging directory") 30 | 31 | args = parser.parse_args() 32 | 33 | # Set the backend by modifying the env variable 34 | if args.backend == "theano": 35 | os.environ["KERAS_BACKEND"] = "theano" 36 | elif args.backend == "tensorflow": 37 | os.environ["KERAS_BACKEND"] = "tensorflow" 38 | 39 | # Import the backend 40 | import keras.backend as K 41 | 42 | # manually set dim ordering otherwise it is not changed 43 | if args.backend == "theano": 44 | image_data_format = "channels_first" 45 | K.set_image_data_format(image_data_format) 46 | elif args.backend == "tensorflow": 47 | image_data_format = "channels_last" 48 | K.set_image_data_format(image_data_format) 49 | 50 | import train 51 | 52 | # Set default params 53 | d_params = {"dset": args.dset, 54 | "generator": args.generator, 55 | "batch_size": args.batch_size, 56 | "n_batch_per_epoch": args.n_batch_per_epoch, 57 | "nb_epoch": args.nb_epoch, 58 | "model_name": "CNN", 59 | "epoch": args.epoch, 60 | "nb_classes": args.nb_classes, 61 | "do_plot": args.do_plot, 62 | "image_data_format": image_data_format, 63 | "bn_mode": args.bn_mode, 64 | "img_dim": args.img_dim, 65 | "use_label_smoothing": args.use_label_smoothing, 66 | "label_flipping": args.label_flipping, 67 | "patch_size": args.patch_size, 68 | "use_mbd": args.use_mbd, 69 | "logging_dir": args.logging_dir 70 | } 71 | 72 | # Launch training 73 | launch_training(**d_params) 74 | -------------------------------------------------------------------------------- /pix2pix/src/utils/batch_utils.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import multiprocessing 4 | import os 5 | import h5py 6 | import matplotlib.pylab as plt 7 | import matplotlib.gridspec as gridspec 8 | from matplotlib.pyplot import cm 9 | 10 | 11 | class DataGenerator(object): 12 | """ 13 | Generate minibatches with real-time data parallel augmentation on CPU 14 | 15 | args : 16 | hdf5_file (str) path to data in HDF5 format 17 | batch_size (int) Minibatch size 18 | dset (str) train/test/valid, the name of the dset to iterate over 19 | maxproc (int) max number of processes to spawn in parallel 20 | num_cached (int) max number of batches to keep in queue 21 | 22 | yields : 23 | X, y (minibatch data and labels as np arrays) 24 | """ 25 | 26 | def __init__(self, 27 | hdf5_file, 28 | batch_size=32, 29 | nb_classes=12, 30 | dset="training", 31 | maxproc=8, 32 | num_cached=10): 33 | 34 | # Check file exists 35 | assert os.path.isfile(hdf5_file), hdf5_file + " doesn't exist" 36 | 37 | # Initialize class internal variables 38 | self.dset = dset 39 | self.maxproc = maxproc 40 | self.hdf5_file = hdf5_file 41 | self.batch_size = batch_size 42 | self.num_cached = num_cached 43 | self.nb_classes = nb_classes 44 | 45 | # Dict that will store all transformations and their parameters 46 | self.d_transform = {} 47 | 48 | # Read the data file to get dataset shape information 49 | with h5py.File(self.hdf5_file, "r") as hf: 50 | self.X_shape = hf["data"].shape 51 | assert len(self.X_shape) == 4,\ 52 | ("\n\nImg data should be formatted as: \n" 53 | "(n_samples, n_channels, Height, Width)") 54 | self.n_samples = hf["data"].shape[0] 55 | # Verify n_channels is at index 1 56 | assert self.X_shape[-3] < min(self.X_shape[-2:]),\ 57 | ("\n\nImg data should be formatted as: \n" 58 | "(n_samples, n_channels, Height, Width)") 59 | 60 | # Save the class internal variables to a config dict 61 | self.d_config = {} 62 | self.d_config["hdf5_file"] = hdf5_file 63 | self.d_config["batch_size"] = batch_size 64 | self.d_config["dset"] = dset 65 | self.d_config["num_cached"] = num_cached 66 | self.d_config["maxproc"] = maxproc 67 | self.d_config["data_shape"] = self.X_shape 68 | 69 | def get_config(self): 70 | 71 | return self.d_config 72 | 73 | def gen_batch_inmemory_GAN(self, X_real, batch_size=None): 74 | """Generate batch, assuming X is loaded in memory in the main program""" 75 | 76 | while True: 77 | 78 | bs = self.batch_size 79 | if batch_size is not None: 80 | bs = batch_size 81 | 82 | # Select idx at random for the batch 83 | idx = np.random.choice(X_real.shape[0], bs, replace=False) 84 | X_batch_real = X_real[idx] 85 | 86 | yield X_batch_real 87 | -------------------------------------------------------------------------------- /pix2pix/src/utils/general_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def remove_files(files): 5 | """ 6 | Remove files from disk 7 | 8 | args: files (str or list) remove all files in 'files' 9 | """ 10 | 11 | if isinstance(files, (list, tuple)): 12 | for f in files: 13 | if os.path.isfile(os.path.expanduser(f)): 14 | os.remove(f) 15 | elif isinstance(files, str): 16 | if os.path.isfile(os.path.expanduser(files)): 17 | os.remove(files) 18 | 19 | 20 | def create_dir(dirs): 21 | """ 22 | Create directory 23 | 24 | args: dirs (str or list) create all dirs in 'dirs' 25 | """ 26 | 27 | if isinstance(dirs, (list, tuple)): 28 | for d in dirs: 29 | if not os.path.exists(os.path.expanduser(d)): 30 | os.makedirs(d) 31 | elif isinstance(dirs, str): 32 | if not os.path.exists(os.path.expanduser(dirs)): 33 | os.makedirs(dirs) 34 | 35 | 36 | def setup_logging(model_name, logging_dir='../../'): 37 | 38 | # Output path where we store experiment log and weights 39 | model_dir = os.path.join(logging_dir, 'models', model_name) 40 | 41 | fig_dir = os.path.join(logging_dir, 'figures') 42 | 43 | # Create if it does not exist 44 | create_dir([model_dir, fig_dir]) --------------------------------------------------------------------------------