├── _config.yml ├── images └── ae.png ├── VAE ├── images │ ├── vae.jpg │ ├── true_imgs.png │ └── generated_imgs.png ├── README.md ├── visualize.py ├── utils.py ├── data.py └── model.py ├── AE-With-SSCs ├── images │ ├── in_imgs.png │ ├── no_noise.png │ ├── noise_ratio0.2.png │ ├── noise_ratio0.4.png │ └── skip-connection.png ├── utils.py ├── visualize.py ├── pipeline.py ├── README.md └── model.py ├── Denoising-Autoenocders ├── images │ ├── dae.png │ ├── in_imgs.png │ ├── dae_no_noise.png │ ├── dae_noise_ratio0.2.png │ ├── dae_noise_ratio0.4.png │ ├── dae_vae_no_noise.png │ ├── dae_vae_noise_ratio0.2.png │ └── dae_vae_noise_ratio0.4.png ├── utils.py ├── visualize.py ├── pipeline.py ├── README.md └── denoising_model.py ├── Simple-Autoecnoders ├── images │ ├── mnist.png │ ├── decoder_out.png │ ├── encoder_out.png │ ├── generated_imgs.png │ └── autoencoder_tensorboard_graph.png ├── utils.py ├── train.py ├── README.md ├── data.py ├── plot_decoder_res.py ├── plot_encoder_res.py └── model.py ├── README.md └── LICENSE /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /images/ae.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/images/ae.png -------------------------------------------------------------------------------- /VAE/images/vae.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/VAE/images/vae.jpg -------------------------------------------------------------------------------- /VAE/images/true_imgs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/VAE/images/true_imgs.png -------------------------------------------------------------------------------- /VAE/images/generated_imgs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/VAE/images/generated_imgs.png -------------------------------------------------------------------------------- /AE-With-SSCs/images/in_imgs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/AE-With-SSCs/images/in_imgs.png -------------------------------------------------------------------------------- /AE-With-SSCs/images/no_noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/AE-With-SSCs/images/no_noise.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/dae.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/dae.png -------------------------------------------------------------------------------- /Simple-Autoecnoders/images/mnist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Simple-Autoecnoders/images/mnist.png -------------------------------------------------------------------------------- /AE-With-SSCs/images/noise_ratio0.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/AE-With-SSCs/images/noise_ratio0.2.png -------------------------------------------------------------------------------- /AE-With-SSCs/images/noise_ratio0.4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/AE-With-SSCs/images/noise_ratio0.4.png -------------------------------------------------------------------------------- /AE-With-SSCs/images/skip-connection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/AE-With-SSCs/images/skip-connection.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/in_imgs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/in_imgs.png -------------------------------------------------------------------------------- /Simple-Autoecnoders/images/decoder_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Simple-Autoecnoders/images/decoder_out.png -------------------------------------------------------------------------------- /Simple-Autoecnoders/images/encoder_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Simple-Autoecnoders/images/encoder_out.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/dae_no_noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/dae_no_noise.png -------------------------------------------------------------------------------- /Simple-Autoecnoders/images/generated_imgs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Simple-Autoecnoders/images/generated_imgs.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/dae_noise_ratio0.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/dae_noise_ratio0.2.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/dae_noise_ratio0.4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/dae_noise_ratio0.4.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/dae_vae_no_noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/dae_vae_no_noise.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/dae_vae_noise_ratio0.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/dae_vae_noise_ratio0.2.png -------------------------------------------------------------------------------- /Denoising-Autoenocders/images/dae_vae_noise_ratio0.4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Denoising-Autoenocders/images/dae_vae_noise_ratio0.4.png -------------------------------------------------------------------------------- /Simple-Autoecnoders/images/autoencoder_tensorboard_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyush2896/Autoencoder-Implementations/HEAD/Simple-Autoecnoders/images/autoencoder_tensorboard_graph.png -------------------------------------------------------------------------------- /Simple-Autoecnoders/utils.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def get_data(): 4 | mnist = tf.contrib.learn.datasets.load_dataset("mnist") 5 | train_imgs = mnist.train.images 6 | train_labels = mnist.train.labels 7 | eval_imgs = mnist.test.images 8 | eval_labels = mnist.test.labels 9 | return (train_imgs, train_labels), (eval_imgs, eval_labels) 10 | -------------------------------------------------------------------------------- /Simple-Autoecnoders/train.py: -------------------------------------------------------------------------------- 1 | from data import train_input_func, eval_input_fn, predict_input_fn 2 | from model import model_func 3 | import tensorflow as tf 4 | import os 5 | 6 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 7 | 8 | mnist_autoecnoder = tf.estimator.Estimator(model_fn=model_func, 9 | params={'n_encode': 3, 'n_outs': 784}, model_dir='./autoencoder') 10 | 11 | mnist_autoecnoder.train(input_fn=train_input_func, 12 | steps=2000) 13 | 14 | eval_res = mnist_autoecnoder.evaluate(input_fn=eval_input_fn) 15 | 16 | print(eval_res) 17 | 18 | -------------------------------------------------------------------------------- /Simple-Autoecnoders/README.md: -------------------------------------------------------------------------------- 1 | # Autoencoder 2 | An autoencoder is a combination of two sub-netowrks - encoder and decoder. As the name suggests and encoder encodes the input information and the decoder decodes the encodings into the information that was feeded into the encoder. 3 | 4 | ## Dataset 5 | Dataset used is MNIST 6 | 7 | | ![MNIST dataset](./images/mnist.png)| 8 | |:-----------------------------------:| 9 | | *MNIST Dataset* | 10 | 11 | 12 | ## Output 13 | 14 | | ![MNIST Generated](./images/generated_imgs.png)| 15 | |:----------------------------------------------:| 16 | | *Decoder Output* | 17 | -------------------------------------------------------------------------------- /Simple-Autoecnoders/data.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from utils import get_data 3 | 4 | (train_data, train_labels), (eval_data, eval_labels) = get_data() 5 | 6 | train_input_func = tf.estimator.inputs.numpy_input_fn( 7 | x={"x": train_data}, 8 | y=train_data, 9 | batch_size=100, 10 | num_epochs=None, 11 | shuffle=True) 12 | 13 | eval_input_fn = tf.estimator.inputs.numpy_input_fn( 14 | x={"x": eval_data}, 15 | y=eval_data, 16 | num_epochs=1, 17 | shuffle=False) 18 | 19 | predict_input_fn = tf.estimator.inputs.numpy_input_fn( 20 | x={'x': train_data}, 21 | num_epochs=1, 22 | shuffle=False) 23 | 24 | pred_labels = train_labels -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Autoencoders 2 | Autoencoders come under the category of Unsupervised Deep Learning. This means that there are no labels for the input. They usually are generative models, i.e., they can create new images. 3 | 4 | | ![AE](./images/ae.png) | 5 | |:----------------------:| 6 | | *Schematic structure of an autoencoder with 3 fully-connected hidden layers.* | 7 | | *Source: [Wikipedia](https://en.wikipedia.org/wiki/Autoencoder)* | 8 | 9 | This repository contains my implementations for autoencoders. Currently there are 3 autoencoders: 10 | 11 | * [Vanilla Autoencoder](./Simple-Autoecnoders) 12 | * [Variational Autoecnoder](./VAE) 13 | * [Denosing Autoecnoder](./Denoising-Autoenocders) 14 | * [Convolutional Autoencoders with Symmetric Skip Connections](./AE-With-SSCs) 15 | 16 | ## Dependencies 17 | * TensorFlow 18 | * Numpy 19 | * Pandas 20 | * Matplotlib 21 | * OpenCV 22 | -------------------------------------------------------------------------------- /Simple-Autoecnoders/plot_decoder_res.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import tensorflow as tf 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | from data import predict_input_fn, pred_labels 8 | from model import model_func 9 | 10 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 11 | 12 | mnist_autoecnoder = tf.estimator.Estimator(model_fn=model_func, 13 | params={'n_encode': 3, 'n_outs': 784}, model_dir='./autoencoder') 14 | 15 | imgs = [] 16 | 17 | preds = mnist_autoecnoder.predict(input_fn=predict_input_fn) 18 | for i in preds: 19 | imgs.append(i['decoder_out']) 20 | 21 | imgs = np.asarray(imgs) 22 | 23 | indices = np.random.randint(pred_labels.shape[0], size=100) 24 | 25 | plt.figure(figsize=(10, 10)) 26 | for i, index in enumerate(indices): 27 | plt.subplot(10, 10, i+1) 28 | plt.imshow(imgs[index].reshape(28, 28), cmap='gray') 29 | plt.axis('off') 30 | plt.tight_layout() 31 | plt.subplots_adjust(wspace=0, hspace=0) 32 | plt.show() 33 | -------------------------------------------------------------------------------- /AE-With-SSCs/utils.py: -------------------------------------------------------------------------------- 1 | from glob import glob 2 | import cv2 3 | import numpy as np 4 | import os 5 | 6 | def get_filenames(path, shuffle=True): 7 | filenames = glob(os.path.join(path, '*')) 8 | 9 | if shuffle: 10 | np.random.shuffle(filenames) 11 | return filenames 12 | 13 | def load_img(path): 14 | img = cv2.imread(path) 15 | return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 16 | 17 | def train_dev_split(source, dev_path, split=0.1): 18 | filenames = get_filenames(source, shuffle=True) 19 | 20 | get_filename = lambda x: x.split('/')[-1] 21 | 22 | if not os.path.isdir(dev_path): 23 | os.makedirs(dev_path) 24 | 25 | print('Moving {} files'.format(int(len(filenames) * split))) 26 | split = len(filenames) - int(len(filenames) * split) 27 | files_to_move = filenames[split:] 28 | 29 | for file in files_to_move: 30 | dst = os.path.join(dev_path, 31 | get_filename(file)) 32 | os.rename(file, dst) 33 | -------------------------------------------------------------------------------- /Denoising-Autoenocders/utils.py: -------------------------------------------------------------------------------- 1 | from glob import glob 2 | import cv2 3 | import numpy as np 4 | import os 5 | 6 | def get_filenames(path, shuffle=True): 7 | filenames = glob(os.path.join(path, '*')) 8 | 9 | if shuffle: 10 | np.random.shuffle(filenames) 11 | return filenames 12 | 13 | def load_img(path): 14 | img = cv2.imread(path) 15 | return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 16 | 17 | def train_dev_split(source, dev_path, split=0.1): 18 | filenames = get_filenames(source, shuffle=True) 19 | 20 | get_filename = lambda x: x.split('/')[-1] 21 | 22 | if not os.path.isdir(dev_path): 23 | os.makedirs(dev_path) 24 | 25 | print('Moving {} files'.format(int(len(filenames) * split))) 26 | split = len(filenames) - int(len(filenames) * split) 27 | files_to_move = filenames[split:] 28 | 29 | for file in files_to_move: 30 | dst = os.path.join(dev_path, 31 | get_filename(file)) 32 | os.rename(file, dst) 33 | -------------------------------------------------------------------------------- /Simple-Autoecnoders/plot_encoder_res.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import tensorflow as tf 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from mpl_toolkits.mplot3d import Axes3D 7 | 8 | from data import predict_input_fn, pred_labels 9 | from model import model_func 10 | 11 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 12 | 13 | mnist_autoecnoder = tf.estimator.Estimator(model_fn=model_func, 14 | params={'n_encode': 3, 'n_outs': 784}, model_dir='./autoencoder') 15 | 16 | imgs_3d = [] 17 | 18 | preds = mnist_autoecnoder.predict(input_fn=predict_input_fn) 19 | for i in preds: 20 | imgs_3d.append(i['encoder_out']) 21 | 22 | imgs_3d = np.asarray(imgs_3d) 23 | 24 | colors = ['b', 'g', 'r', 'c', 'm', 25 | 'y', 'k', '#bde123', '#123456', '#998855'] 26 | 27 | fig = plt.figure(figsize=(20, 10)) 28 | ax = fig.add_subplot(111, projection='3d') 29 | for i, color in enumerate(colors): 30 | ax.scatter(imgs_3d[pred_labels==i, 0], imgs_3d[pred_labels==i, 1], imgs_3d[pred_labels==i, 2], color=color) 31 | plt.show() 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Piyush Malhotra 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /AE-With-SSCs/visualize.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | from utils import get_filenames, load_img 3 | import numpy as np 4 | import cv2 5 | 6 | def get_preds(pred_generator, key): 7 | preds = [] 8 | 9 | for pred in pred_generator: 10 | preds.append(pred[key]) 11 | return np.asarray(preds).astype('int') 12 | 13 | def plot_some_imgs(root): 14 | print('Displaying 100 images') 15 | 16 | filenames = get_filenames(root) 17 | indices = np.random.choice(len(filenames), size=100) 18 | plt.figure(figsize=(10, 10)) 19 | for i, index in enumerate(indices): 20 | plt.subplot(10, 10, i+1) 21 | plt.imshow(load_img(filenames[index])) 22 | plt.axis('off') 23 | plt.tight_layout() 24 | plt.subplots_adjust(wspace=0, hspace=0) 25 | plt.show() 26 | 27 | def plot_decoder_out(pred_generator, n_imgs=5000, n_cols=10, n_rows=10): 28 | indices = np.random.randint(n_imgs, size=n_cols * n_rows) 29 | 30 | imgs_decoded = get_preds(pred_generator, 'decoder_out') 31 | 32 | plt.figure(figsize=(10, 10)) 33 | for i, index in enumerate(indices): 34 | plt.subplot(n_rows, n_cols, i+1) 35 | plt.imshow(imgs_decoded[index]) 36 | plt.axis('off') 37 | plt.tight_layout() 38 | plt.subplots_adjust(wspace=0, hspace=0) 39 | plt.show() 40 | -------------------------------------------------------------------------------- /Denoising-Autoenocders/visualize.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | from utils import get_filenames, load_img 3 | import numpy as np 4 | import cv2 5 | 6 | def get_preds(pred_generator, key): 7 | preds = [] 8 | 9 | for pred in pred_generator: 10 | preds.append(pred[key]) 11 | return np.asarray(preds).astype('int') 12 | 13 | def plot_some_imgs(root): 14 | print('Displaying 100 images') 15 | 16 | filenames = get_filenames(root) 17 | indices = np.random.choice(len(filenames), size=100) 18 | plt.figure(figsize=(10, 10)) 19 | for i, index in enumerate(indices): 20 | plt.subplot(10, 10, i+1) 21 | plt.imshow(load_img(filenames[index])) 22 | plt.axis('off') 23 | plt.tight_layout() 24 | plt.subplots_adjust(wspace=0, hspace=0) 25 | plt.show() 26 | 27 | def plot_decoder_out(pred_generator, n_imgs=5000, n_cols=10, n_rows=10): 28 | indices = np.random.randint(n_imgs, size=n_cols * n_rows) 29 | 30 | imgs_decoded = get_preds(pred_generator, 'decoder_out') 31 | 32 | plt.figure(figsize=(10, 10)) 33 | for i, index in enumerate(indices): 34 | plt.subplot(n_rows, n_cols, i+1) 35 | plt.imshow(imgs_decoded[index]) 36 | plt.axis('off') 37 | plt.tight_layout() 38 | plt.subplots_adjust(wspace=0, hspace=0) 39 | plt.show() 40 | -------------------------------------------------------------------------------- /VAE/README.md: -------------------------------------------------------------------------------- 1 | # Variational Autoencoders 2 | Simple autoencoders can be considered as a data structures that memorize an image and regenerate it. But we want to generate new images not a data structure to memorize the image. To do it we add stochastic nature to our model by introducing a latent vector that is generated using the outputs of the encoder network. Where outputs of the encoder network represent the mean and standard deviation of the latent gaussian vector 3 | 4 | | ![vae](./images/vae.jpg) | 5 | |:------------------------:| 6 | | *Variational Autoencoder* | 7 | | *Source: [Variational Autoencoders Explained](http://kvfrans.com/variational-autoencoders-explained/)* | 8 | 9 | ## Dataset 10 | The VAE is trained on [Cifar-10](https://www.cs.toronto.edu/~kriz/cifar.html) dataset 11 | for about 30 epochs. 12 | 13 | | ![cifar-10](./images/true_imgs.png)| 14 | |:----------------------------------:| 15 | | *CIFAR-10 Images* | 16 | 17 | ## Output 18 | The VAE generated vague images on start but got better with time. To see the results of your training from the VAE-demo notebook - Start a new terminal and execute command: 19 | ``` 20 | tensorboard --logdir=./vae_400 21 | ``` 22 | 23 | and then go to ``:6000`` (if training on an instance) or to ``:6000`` (if training on local system). 24 | 25 | 26 | ### Final Output 27 | 28 | | ![VAE output](./images/generated_imgs.png) | 29 | |:------------------------------------------:| 30 | | *VAE outputs* | 31 | -------------------------------------------------------------------------------- /VAE/visualize.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | from utils import get_filenames 3 | import numpy as np 4 | import cv2 5 | 6 | def get_n_colors(n): 7 | colors = list(zip(np.random.randint(256, size=n), 8 | np.random.randint(256, size=n), 9 | np.random.randint(256, size=n))) 10 | colors = ['#{:02x}{:02x}{:02x}'.format(color[0], 11 | color[1], 12 | color[2]) for color in colors] 13 | return colors 14 | 15 | def get_preds(pred_generator, key): 16 | preds = [] 17 | 18 | for pred in pred_generator: 19 | preds.append(pred[key]) 20 | return np.asarray(preds).astype('int') 21 | 22 | def plot_decoder_out(pred_generator, n_imgs=5000, n_cols=10, n_rows=10): 23 | indices = np.random.randint(n_imgs, size=n_cols * n_rows) 24 | 25 | imgs_decoded = get_preds(pred_generator, 'decoder_out') 26 | 27 | plt.figure(figsize=(10, 10)) 28 | for i, index in enumerate(indices): 29 | plt.subplot(n_rows, n_cols, i+1) 30 | plt.imshow(imgs_decoded[index]) 31 | plt.axis('off') 32 | plt.tight_layout() 33 | plt.subplots_adjust(wspace=0, hspace=0) 34 | plt.show() 35 | 36 | def plot_some_imgs(root): 37 | print('Displaying 100 images') 38 | 39 | filenames = get_filenames(root) 40 | indices = np.random.choice(len(filenames), size=100) 41 | plt.figure(figsize=(10, 10)) 42 | for i, index in enumerate(indices): 43 | plt.subplot(10, 10, i+1) 44 | plt.imshow(cv2.cvtColor(cv2.imread(filenames[index]), cv2.COLOR_BGR2RGB)) 45 | plt.axis('off') 46 | plt.tight_layout() 47 | plt.subplots_adjust(wspace=0, hspace=0) 48 | plt.show() -------------------------------------------------------------------------------- /Simple-Autoecnoders/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def dense(in_, units, name=None): 4 | return tf.layers.dense(inputs=in_, 5 | units=units, 6 | activation=tf.nn.relu, 7 | kernel_regularizer=tf.contrib.layers.xavier_initializer, 8 | name=name) 9 | 10 | def encoder(features, n_encode): 11 | dense1 = dense(features['x'], 512, name='en_dense1') 12 | dense2 = dense(dense1, 256, name='en_dense2') 13 | dense3 = dense(dense2, 120, name='en_dense3') 14 | dense4 = dense(dense3, n_encode, name='encoder_out') 15 | return dense4 16 | 17 | def decoder(encoder_out, n_outs): 18 | dense1 = dense(encoder_out, 120, name='de_dense1') 19 | dense2 = dense(dense1, 256, name='de_dense2') 20 | dense3 = dense(dense2, 512, name='de_dense3') 21 | dense4 = dense(dense3, n_outs, name='decoder_out') 22 | return dense4 23 | 24 | def model_func(features, labels, mode, params): 25 | encoder_out = encoder(features, params['n_encode']) 26 | decoder_out = decoder(encoder_out, params['n_outs']) 27 | 28 | if mode == tf.estimator.ModeKeys.PREDICT: 29 | predictions = { 30 | 'encoder_out': encoder_out, 31 | 'decoder_out': decoder_out 32 | } 33 | spec = tf.estimator.EstimatorSpec(mode=mode, 34 | predictions=predictions) 35 | else: 36 | loss = tf.losses.mean_squared_error(labels, decoder_out) 37 | optimizer = tf.train.AdamOptimizer(1e-4) 38 | train_op = optimizer.minimize(loss, 39 | global_step=tf.train.get_global_step()) 40 | 41 | spec= tf.estimator.EstimatorSpec(mode=mode, 42 | loss=loss, train_op=train_op) 43 | 44 | return spec 45 | -------------------------------------------------------------------------------- /AE-With-SSCs/pipeline.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def corrupt(x): 4 | return tf.multiply(x, tf.random_normal(shape=tf.shape(x), 5 | mean=0, stddev=2, dtype=tf.float32)) 6 | 7 | def shuffle_repeat_applier(dataset, buffer_size, shuffle, repeat): 8 | if shuffle: 9 | dataset = dataset.shuffle(buffer_size) 10 | else: 11 | dataset = dataset.prefetch(buffer_size) 12 | 13 | if repeat: 14 | dataset = dataset.repeat() 15 | else: 16 | dataset = dataset.repeat(1) 17 | 18 | return dataset 19 | 20 | def input_fn(filenames, 21 | img_size, 22 | noising_ratio=0.4, 23 | batch_size=32, 24 | buffer_size=2000, 25 | shuffle=True, 26 | repeat=True): 27 | 28 | def load_and_add_noise(filename): 29 | bytes = tf.read_file(filename) 30 | img_decoded = tf.image.decode_jpeg(bytes, channels=3) 31 | img_resized = tf.image.resize_images(img_decoded, img_size) 32 | img_normalized = tf.divide(tf.cast(img_resized, 'float'), tf.constant(255.)) 33 | 34 | img_corrupted_part = tf.multiply(corrupt(img_normalized), 35 | tf.constant(noising_ratio)) 36 | img_safe_part = tf.multiply(img_normalized, tf.constant(1-noising_ratio)) 37 | img_noised = tf.add(img_corrupted_part, img_safe_part) 38 | img_noised = tf.divide(img_noised, tf.reduce_max(img_noised)) 39 | 40 | return img_noised, img_normalized 41 | 42 | dataset = tf.data.Dataset.from_tensor_slices((filenames, )) 43 | dataset = dataset.map(load_and_add_noise) 44 | 45 | dataset = shuffle_repeat_applier(dataset, buffer_size, shuffle, repeat) 46 | 47 | dataset = dataset.batch(batch_size) 48 | 49 | images_x, images_y = dataset.make_one_shot_iterator().get_next() 50 | 51 | features_dic = {'image': images_x} 52 | return features_dic, images_y 53 | -------------------------------------------------------------------------------- /Denoising-Autoenocders/pipeline.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def corrupt(x): 4 | return tf.multiply(x, tf.random_normal(shape=tf.shape(x), 5 | mean=0, stddev=2, dtype=tf.float32)) 6 | 7 | def shuffle_repeat_applier(dataset, buffer_size, shuffle, repeat): 8 | if shuffle: 9 | dataset = dataset.shuffle(buffer_size) 10 | else: 11 | dataset = dataset.prefetch(buffer_size) 12 | 13 | if repeat: 14 | dataset = dataset.repeat() 15 | else: 16 | dataset = dataset.repeat(1) 17 | 18 | return dataset 19 | 20 | def input_fn(filenames, 21 | img_size, 22 | noising_ratio=0.4, 23 | batch_size=32, 24 | buffer_size=2000, 25 | shuffle=True, 26 | repeat=True): 27 | 28 | def load_and_add_noise(filename): 29 | bytes = tf.read_file(filename) 30 | img_decoded = tf.image.decode_jpeg(bytes, channels=3) 31 | img_resized = tf.image.resize_images(img_decoded, img_size) 32 | img_normalized = tf.divide(tf.cast(img_resized, 'float'), tf.constant(255.)) 33 | 34 | img_corrupted_part = tf.multiply(corrupt(img_normalized), 35 | tf.constant(noising_ratio)) 36 | img_safe_part = tf.multiply(img_normalized, tf.constant(1-noising_ratio)) 37 | img_noised = tf.add(img_corrupted_part, img_safe_part) 38 | img_noised = tf.divide(img_noised, tf.reduce_max(img_noised)) 39 | 40 | return img_noised, img_normalized 41 | 42 | dataset = tf.data.Dataset.from_tensor_slices((filenames, )) 43 | dataset = dataset.map(load_and_add_noise) 44 | 45 | dataset = shuffle_repeat_applier(dataset, buffer_size, shuffle, repeat) 46 | 47 | dataset = dataset.batch(batch_size) 48 | 49 | images_x, images_y = dataset.make_one_shot_iterator().get_next() 50 | 51 | features_dic = {'image': images_x} 52 | return features_dic, images_y 53 | -------------------------------------------------------------------------------- /AE-With-SSCs/README.md: -------------------------------------------------------------------------------- 1 | # Convolutional Autoencoders with Symmetric Skip Connections 2 | Fully convolutional networks for autoencoders with very deep connections are succeptible two things: 3 | * Vanishing Gradients 4 | * Significant amount of corruption of image details 5 | 6 | To over come this paper, [Image Restoration Using Convolutional Auto-encoders with Symmetric Skip Connections](./https://arxiv.org/pdf/1606.08921.pdf) 7 | used skip connections between the mirrored convolutional and deconvolutional layers. Relying on this we can overcome the above mentioned issues. 8 | 9 | | ![Skip Connections](./images/skip-connection.png) | 10 | |:-------------------------------------------------:| 11 | | *An example of a building block in the proposed framework.* | 12 | 13 | ## Dataset 14 | The AE is trained on [Cifar-10](https://www.cs.toronto.edu/~kriz/cifar.html) dataset for about 100 epochs. 15 | 16 | | ![cifar-10](./images/in_imgs.png)| 17 | |:----------------------------------:| 18 | | *CIFAR-10 Images* | 19 | 20 | ## Output 21 | 22 | To see the results of your training from the [SSC_demo notebook](./SSC_Demo.ipynb) - Start a new terminal and execute command: 23 | ``` 24 | tensorboard --logdir=./ae_ssc 25 | ``` 26 | 27 | and then go to ``:6006`` (if training on an instance) or to ``:6006`` (if training on local system). 28 | 29 | 30 | * Noising Ratio - 0.4: 40% noisy image + 60% image 31 | 32 | | ![AE output 0.4](./images/noise_ratio0.4.png) | 33 | |:------------------------------------------:| 34 | | *Outputs Noising Ratio 0.4* | 35 | 36 | * Noising Ratio - 0.2: 20% noisy image + 80% image 37 | 38 | | ![AE output 0.2](./images/noise_ratio0.2.png) | 39 | |:------------------------------------------:| 40 | | *Outputs Noising Ratio 0.2* | 41 | 42 | 43 | * No Noise 44 | 45 | | ![AE output](./images/no_noise.png) | 46 | |:------------------------------------------:| 47 | | *Outputs no Noise* | 48 | 49 | ## Issue Encountered 50 | The AE is strangely getting stuck with blue tint in case of no noise input. -------------------------------------------------------------------------------- /VAE/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | from tqdm import tqdm 4 | import os 5 | from glob import glob 6 | import random 7 | import matplotlib.pyplot as plt 8 | 9 | def read_img(path, size=(64, 64)): 10 | img = cv2.imread(path) 11 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 12 | return cv2.resize(img, size) 13 | 14 | def get_train_data(path): 15 | raw_labels = os.listdir(path) 16 | imgs = [] 17 | labels = [] 18 | for i, raw_label in enumerate(tqdm(raw_labels)): 19 | img_names = os.listdir(os.path.join(path, raw_label)) 20 | for img_name in img_names: 21 | if img_name in ['.DS_Store', '._.DS_Store']: 22 | continue 23 | imgs.append(read_img(os.path.join(path, raw_label, img_name))) 24 | labels = labels + [i] * len(img_names) 25 | return np.asarray(imgs, dtype=np.float32), np.asarray(labels), raw_labels 26 | 27 | def get_test_data(path): 28 | img_names = os.listdir(path) 29 | imgs = [] 30 | labels = [] 31 | for img_name in tqdm(img_names): 32 | if img_name in ['.DS_Store', '._.DS_Store']: 33 | continue 34 | imgs.append(read_img(os.path.join(path, img_name))) 35 | labels.append('_'.join(img_name.split('_')[:-1])) 36 | return np.asarray(imgs, dtype=np.float32), labels 37 | 38 | def get_filenames(root, shuffle=False, is_test=False): 39 | if is_test: 40 | path = os.path.join(root, '*') 41 | else: 42 | path = os.path.join(root, '*') 43 | 44 | filenames = glob(path, recursive=True) 45 | 46 | if shuffle: 47 | random.shuffle(filenames) 48 | return filenames 49 | 50 | def train_dev_split(source, dev_path, split=0.1): 51 | filenames = get_filenames(source, shuffle=True, is_test=True) 52 | 53 | labels = glob(os.path.join(source, '*')) 54 | 55 | get_filename = lambda x: x.split('/')[-1] 56 | 57 | if not os.path.isdir(dev_path): 58 | os.makedirs(dev_path) 59 | 60 | print('Moving {} files'.format(int(len(filenames) * split))) 61 | split = len(filenames) - int(len(filenames) * split) 62 | files_to_move = filenames[split:] 63 | 64 | for file in files_to_move: 65 | dst = os.path.join(dev_path, 66 | #get_label(file), 67 | get_filename(file)) 68 | os.rename(file, dst) 69 | 70 | def get_labels(root): 71 | get_label = lambda x: ' '.join(x.split('\\')[1].split('_')[:-1]) 72 | preds_labels = list(map(get_label, glob(os.path.join(root, '*')))) 73 | return preds_labels 74 | -------------------------------------------------------------------------------- /Denoising-Autoenocders/README.md: -------------------------------------------------------------------------------- 1 | # Denoising Autoencoders 2 | Autoencoders can be used to remove noise (denoise) from an image. But how? Train the encoder part to encode a noisy image and the decoder part to reconstruct an image that is more similar to the image without noise. 3 | 4 | | ![Denoising Autoencoder](./images/dae.png) | 5 | |:------------------------------------------:| 6 | | * Denoising Autoencoder Results* | 7 | 8 | ## Dataset 9 | The DAE is trained on [Cifar-10](https://www.cs.toronto.edu/~kriz/cifar.html) dataset for about 30 epochs. 10 | 11 | | ![cifar-10](./images/in_imgs.png)| 12 | |:----------------------------------:| 13 | | *CIFAR-10 Images* | 14 | 15 | ## Output 16 | The VAE generated vague images on start but got better with time. 17 | 18 | * To see the results of your training from the [DAE_demo notebook](./DAE_Demo.ipynb) - Start a new terminal and execute command: 19 | ``` 20 | tensorboard --logdir=./dae 21 | ``` 22 | 23 | * To see the results of your training from the [DAE_VAE_DEMO notebook](./DAE_VAE_DEMO.ipynb) - Start a new terminal and execute command: 24 | ``` 25 | tensorboard --logdir=./dae_vae 26 | ``` 27 | 28 | and then go to ``:6000`` (if training on an instance) or to ``:6000`` (if training on local system). 29 | 30 | 31 | ### DAE without Variational Space 32 | * Noising Ratio - 0.4: 40% noisy image + 60% image 33 | 34 | | ![DAE output 0.4](./images/dae_noise_ratio0.4.png) | 35 | |:------------------------------------------:| 36 | | *DAE outputs Noising Ratio 0.4* | 37 | 38 | * Noising Ratio - 0.2: 20% noisy image + 80% image 39 | 40 | | ![DAE output 0.2](./images/dae_noise_ratio0.2.png) | 41 | |:------------------------------------------:| 42 | | *DAE outputs Noising Ratio 0.2* | 43 | 44 | 45 | * No Noise 46 | 47 | | ![DAE output](./images/dae_no_noise.png) | 48 | |:------------------------------------------:| 49 | | *DAE outputs no Noise* | 50 | 51 | ### Variational DAE 52 | * Noising Ratio - 0.4: 40% noisy image + 60% image 53 | 54 | | ![DAE output 0.4](./images/dae_vae_noise_ratio0.4.png) | 55 | |:------------------------------------------:| 56 | | *Variational DAE outputs Noising Ratio 0.4* | 57 | 58 | * Noising Ratio - 0.2: 20% noisy image + 80% image 59 | 60 | | ![DAE output 0.2](./images/dae_vae_noise_ratio0.2.png) | 61 | |:------------------------------------------:| 62 | | *Variational DAE outputs Noising Ratio 0.2* | 63 | 64 | 65 | * No Noise 66 | 67 | | ![DAE output](./images/dae_vae_no_noise.png) | 68 | |:------------------------------------------:| 69 | | *Variational DAE outputs no Noise* | 70 | -------------------------------------------------------------------------------- /VAE/data.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from utils import get_train_data, get_test_data, get_filenames 3 | 4 | def input_fn_helper(filenames, 5 | img_size, 6 | is_test=False): 7 | def load(filename): 8 | bytes = tf.read_file(filename) 9 | img_decoded = tf.image.decode_jpeg(bytes, channels=3) 10 | img_resized = tf.image.resize_images(img_decoded, img_size) 11 | img_normalized = tf.divide(tf.cast(img_resized, 'float'), tf.constant(255.)) 12 | img_flipped = tf.image.random_flip_left_right(img_normalized) 13 | 14 | if is_test: 15 | return img_flipped 16 | return img_flipped, img_flipped 17 | 18 | dataset = tf.data.Dataset.from_tensor_slices((filenames, )) 19 | dataset = dataset.map(load) 20 | return dataset 21 | 22 | def train_input_fn(root, 23 | img_size, 24 | batch_size, 25 | buffer_size): 26 | filenames = get_filenames(root, 27 | shuffle=True, 28 | is_test=True) 29 | 30 | dataset = input_fn_helper(filenames, img_size) 31 | 32 | dataset = dataset.shuffle(buffer_size).batch(batch_size) 33 | dataset = dataset.repeat() 34 | 35 | images_x, images_y = dataset.make_one_shot_iterator().get_next() 36 | 37 | features_dic = {'image': images_x} 38 | return features_dic, images_y 39 | 40 | def eval_input_fn(root, 41 | img_size, 42 | batch_size, 43 | buffer_size): 44 | filenames = get_filenames(root, 45 | shuffle=False, 46 | is_test=True) 47 | 48 | dataset = input_fn_helper(filenames, img_size) 49 | dataset = dataset.prefetch(buffer_size).batch(batch_size) 50 | dataset = dataset.repeat(1) 51 | 52 | images_x, images_y = dataset.make_one_shot_iterator().get_next() 53 | 54 | features_dic = {'image': images_x} 55 | return features_dic, images_y 56 | 57 | def predict_input_fn(root, 58 | img_size, 59 | batch_size, 60 | buffer_size): 61 | filenames = get_filenames(root, 62 | shuffle=False, 63 | is_test=True) 64 | dataset = input_fn_helper(filenames, img_size, is_test=True) 65 | dataset = dataset.prefetch(buffer_size).batch(batch_size) 66 | dataset = dataset.repeat(1) 67 | 68 | images_x = dataset.make_one_shot_iterator().get_next() 69 | 70 | features_dic = {'image': images_x} 71 | return features_dic 72 | -------------------------------------------------------------------------------- /AE-With-SSCs/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def conv2d(in_, filters, k_size, strides=2, padding='same'): 4 | return tf.layers.conv2d(inputs=in_, 5 | filters=filters, 6 | kernel_size=k_size, 7 | strides=strides, 8 | padding=padding, 9 | activation=tf.nn.leaky_relu, 10 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 11 | 12 | def deconv2d(in_, filters, k_size, strides=2, padding='same'): 13 | return tf.layers.conv2d_transpose(inputs=in_, 14 | filters=filters, 15 | kernel_size=k_size, 16 | strides=strides, 17 | padding=padding, 18 | activation=tf.nn.leaky_relu, 19 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 20 | 21 | def merge(X1, X2, channels=None, act=tf.nn.leaky_relu): 22 | with tf.name_scope('merger'): 23 | if channels is not None: 24 | re_channel = conv2d(X1, channels, 1, 1) 25 | return act(tf.add(re_channel, X2)) 26 | return act(tf.add(X1, X2)) 27 | 28 | def model_fn(features, labels, mode, params): 29 | conv_balancer = conv2d(features['image'], params['out_channels'], 1, 1) 30 | 31 | conv1 = conv2d(conv_balancer, 32, 3, 2) 32 | conv2 = conv2d(conv1, 32, 3, 2) 33 | 34 | conv3 = conv2d(conv2, 64, 3, 2) 35 | conv4 = conv2d(conv3, 64, 3, 2) 36 | 37 | conv5 = conv2d(conv4, 128, 3, 2) 38 | 39 | deconv1 = deconv2d(conv5, 128, 3, 2) 40 | # skip connection - connect input of conv5 to output of deconv1 41 | # make sure to take care of those extra channels using 1x1 convs 42 | merge41 = merge(conv4, deconv1, 128) 43 | 44 | deconv2 = deconv2d(merge41, 64, 3, 2) 45 | merge32 = merge(conv3, deconv2) 46 | 47 | deconv3 = deconv2d(merge32, 64, 3, 2) 48 | merge23 = merge(conv2, deconv3, 64) 49 | 50 | deconv4 = deconv2d(merge23, 32, 3, 2) 51 | merge14 = merge(conv1, deconv4) 52 | 53 | deconv6 = deconv2d(merge14, params['out_channels'], 3, 2) 54 | merge_balancer6 = merge(conv_balancer, deconv6, act=tf.nn.sigmoid) 55 | 56 | out = tf.multiply(merge_balancer6, tf.constant(255.)) 57 | 58 | if mode == tf.estimator.ModeKeys.PREDICT: 59 | predictions = { 60 | 'decoder_out': out 61 | } 62 | spec = tf.estimator.EstimatorSpec(mode=mode, 63 | predictions=predictions) 64 | else: 65 | 66 | tf.summary.image('input', labels * 255.) 67 | tf.summary.image('out', out) 68 | loss = tf.losses.mean_squared_error(labels, merge_balancer6) 69 | optimizer = tf.train.AdamOptimizer(params['lr']) 70 | train_op = optimizer.minimize(loss, 71 | global_step=tf.train.get_global_step()) 72 | 73 | spec= tf.estimator.EstimatorSpec(mode=mode, 74 | loss=loss, train_op=train_op) 75 | 76 | return spec 77 | -------------------------------------------------------------------------------- /VAE/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | def dense(in_, units, name=None): 5 | return tf.layers.dense(inputs=in_, 6 | units=units, 7 | activation=tf.nn.leaky_relu, 8 | name=name, 9 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 10 | 11 | def conv2d(in_, filters, strides=2, padding='valid'): 12 | return tf.layers.conv2d(inputs=in_, 13 | filters=filters, 14 | kernel_size=3, 15 | strides=strides, 16 | padding=padding, 17 | activation=tf.nn.leaky_relu, 18 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 19 | 20 | def deconv2d(in_, filters, strides=2, padding='valid'): 21 | return tf.layers.conv2d_transpose(inputs=in_, 22 | filters=filters, 23 | kernel_size=3, 24 | strides=strides, 25 | padding=padding, 26 | activation=tf.nn.leaky_relu, 27 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 28 | 29 | def encoder(features, latent_dims): 30 | conv1 = conv2d(features['image'], 32, 1, 'same') 31 | conv2 = conv2d(conv1, 32, 1, 'same') 32 | norm1 = tf.layers.batch_normalization(conv2) 33 | 34 | conv3 = conv2d(norm1, 32, 1) 35 | conv4 = conv2d(conv3, 32, 1) 36 | norm2 = tf.layers.batch_normalization(conv4) 37 | 38 | conv5 = conv2d(norm2, 64, 1, 'same') 39 | conv6 = conv2d(conv5, 64, 1, 'same') 40 | norm3 = tf.layers.batch_normalization(conv6) 41 | 42 | conv7 = conv2d(norm3, 64, 1) 43 | conv8 = conv2d(conv7, 64, 1) 44 | norm4 = tf.layers.batch_normalization(conv8) 45 | 46 | conv9 = conv2d(norm4, 128, 1) 47 | conv10 = conv2d(conv9, 128, 1) 48 | norm5 = tf.layers.batch_normalization(conv10) 49 | 50 | flatten = tf.contrib.layers.flatten(norm5) 51 | 52 | dense1 = dense(flatten, 2048) 53 | dense2 = dense(dense1, 2048) 54 | 55 | z_mean = dense(dense2, latent_dims) 56 | z_stddev = dense(dense2, latent_dims) 57 | return z_mean, z_stddev, norm5.get_shape().as_list()[1:] 58 | 59 | def decoder(dist_out, output_channels, reshape_dim): 60 | dense1 = dense(dist_out, 1024) 61 | dense2 = dense(dense1, np.product(reshape_dim)) 62 | reshape = tf.reshape(dense2, [-1] + reshape_dim) 63 | 64 | deconv1 = deconv2d(reshape, 128, 1, 'same') 65 | deconv2 = deconv2d(deconv1, 128, 1) 66 | norm1 = tf.layers.batch_normalization(deconv2) 67 | 68 | deconv3 = deconv2d(norm1, 64, 1) 69 | deconv4 = deconv2d(deconv3, 64, 1) 70 | norm2 = tf.layers.batch_normalization(deconv4) 71 | 72 | deconv5 = deconv2d(norm2, 32, 1) 73 | deconv6 = deconv2d(deconv5, 32, 1) 74 | norm3 = tf.layers.batch_normalization(deconv6) 75 | 76 | deconv7 = tf.layers.conv2d_transpose(inputs=norm3, 77 | filters=output_channels, 78 | kernel_size=3, 79 | strides=1, 80 | padding='valid', 81 | activation=tf.nn.sigmoid) 82 | return deconv7 83 | 84 | def model_func(features, labels, mode, params): 85 | NAN_ELIMINATOR = 1e-10 86 | z_mean, z_stddev, reshape_params = encoder(features, params['latent_dims']) 87 | 88 | sample = tf.random_normal(tf.shape(z_mean), 89 | 0, 0.1, 90 | dtype='float') 91 | dist_out = z_mean + (z_stddev * sample) 92 | 93 | decoder_out = decoder(dist_out, 94 | params['out_channels'], 95 | reshape_params) 96 | 97 | if mode == tf.estimator.ModeKeys.PREDICT: 98 | predictions = { 99 | 'sampled_out': dist_out, 100 | 'decoder_out': decoder_out * 255. 101 | } 102 | spec = tf.estimator.EstimatorSpec(mode=mode, 103 | predictions=predictions) 104 | else: 105 | 106 | tf.summary.image('input', features['image'] * 255.) 107 | tf.summary.image('output', decoder_out * 255.) 108 | 109 | generation_loss = -tf.reduce_sum(labels * tf.log(NAN_ELIMINATOR + decoder_out) + 110 | (1 - labels) * tf.log(NAN_ELIMINATOR + (1 - decoder_out)), 111 | [1, 2, 3]) 112 | divergence_loss = -0.5 * tf.reduce_sum(1 + tf.log(NAN_ELIMINATOR + tf.square(z_stddev)) - 113 | tf.square(z_mean) - tf.square(z_stddev), 1) 114 | 115 | loss = tf.reduce_mean(tf.add(generation_loss , divergence_loss)) 116 | tf.summary.scalar('loss/KLdivergence', tf.reduce_mean(divergence_loss)) 117 | tf.summary.scalar('loss/reconstruction', tf.reduce_mean(generation_loss)) 118 | 119 | global_step = tf.train.get_global_step() 120 | if params['lr_decay']: 121 | start_lr = params['lr'] 122 | lr = tf.train.cosine_decay_restarts(start_lr, 123 | global_step, 124 | params['decay_steps']) 125 | #_mul=6/7.) 126 | else: 127 | lr = params['lr'] 128 | tf.summary.scalar('lr', lr) 129 | optimizer = tf.train.AdamOptimizer(lr) 130 | train_op = optimizer.minimize(loss, 131 | global_step=global_step) 132 | 133 | spec= tf.estimator.EstimatorSpec(mode=mode, 134 | loss=loss, train_op=train_op) 135 | 136 | return spec 137 | -------------------------------------------------------------------------------- /Denoising-Autoenocders/denoising_model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | def dense(in_, units, name=None): 5 | return tf.layers.dense(inputs=in_, 6 | units=units, 7 | activation=tf.nn.leaky_relu, 8 | name=name, 9 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 10 | 11 | def conv2d(in_, filters, strides=2, padding='valid'): 12 | return tf.layers.conv2d(inputs=in_, 13 | filters=filters, 14 | kernel_size=3, 15 | strides=strides, 16 | padding=padding, 17 | activation=tf.nn.leaky_relu, 18 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 19 | 20 | def deconv2d(in_, filters, strides=2, padding='valid'): 21 | return tf.layers.conv2d_transpose(inputs=in_, 22 | filters=filters, 23 | kernel_size=3, 24 | strides=strides, 25 | padding=padding, 26 | activation=tf.nn.leaky_relu, 27 | kernel_initializer=tf.contrib.layers.xavier_initializer()) 28 | 29 | def vae_loss(encoder_out, decoder_out, labels): 30 | NAN_ELIMINATOR = 1e-7 31 | z_mean, z_stddev = encoder_out 32 | generation_loss = -tf.reduce_sum(labels * tf.log(NAN_ELIMINATOR + decoder_out) + 33 | (1 - labels) * tf.log(NAN_ELIMINATOR + (1 - decoder_out)), 34 | [1, 2, 3]) 35 | divergence_loss = -0.5 * tf.reduce_sum(1 + tf.log(NAN_ELIMINATOR + tf.square(z_stddev)) - 36 | tf.square(z_mean) - tf.square(z_stddev), 1) 37 | 38 | loss = tf.reduce_mean(tf.add(generation_loss , divergence_loss)) 39 | return loss 40 | 41 | def dae_loss(decoder_out, labels): 42 | NAN_ELIMINATOR = 1e-7 43 | loss = -tf.reduce_sum(labels * tf.log(NAN_ELIMINATOR + decoder_out) + 44 | (1 - labels) * tf.log(NAN_ELIMINATOR + (1 - decoder_out)), 45 | axis=[1, 2, 3]) 46 | return tf.reduce_mean(loss) 47 | 48 | def encoder(features, latent_dims, is_vae=True): 49 | conv1 = conv2d(features['image'], 32, 1, 'same') 50 | conv2 = conv2d(conv1, 32, 1, 'same') 51 | norm1 = tf.layers.batch_normalization(conv2) 52 | 53 | conv3 = conv2d(norm1, 32, 1) 54 | conv4 = conv2d(conv3, 32, 1) 55 | norm2 = tf.layers.batch_normalization(conv4) 56 | 57 | conv5 = conv2d(norm2, 64, 1, 'same') 58 | conv6 = conv2d(conv5, 64, 1, 'same') 59 | norm3 = tf.layers.batch_normalization(conv6) 60 | 61 | conv7 = conv2d(norm3, 64, 1) 62 | conv8 = conv2d(conv7, 64, 1) 63 | norm4 = tf.layers.batch_normalization(conv8) 64 | 65 | conv9 = conv2d(norm4, 128, 1) 66 | conv10 = conv2d(conv9, 128, 1) 67 | norm5 = tf.layers.batch_normalization(conv10) 68 | 69 | flatten = tf.contrib.layers.flatten(norm5) 70 | 71 | dense1 = dense(flatten, latent_dims) 72 | 73 | if is_vae: 74 | z_mean = dense(dense1, latent_dims) 75 | z_stddev = dense(dense1, latent_dims) 76 | out = (z_mean, z_stddev) 77 | else: 78 | out = dense1 79 | 80 | return out, norm5.get_shape().as_list()[1:] 81 | 82 | def decoder(encoder_out, output_channels, reshape_dim): 83 | dense1 = dense(encoder_out, np.product(reshape_dim)) 84 | reshape = tf.reshape(dense1, [-1] + reshape_dim) 85 | 86 | deconv1 = deconv2d(reshape, 128, 1, 'same') 87 | deconv2 = deconv2d(deconv1, 128, 1) 88 | norm1 = tf.layers.batch_normalization(deconv2) 89 | 90 | deconv3 = deconv2d(norm1, 64, 1) 91 | deconv4 = deconv2d(deconv3, 64, 1) 92 | norm2 = tf.layers.batch_normalization(deconv4) 93 | 94 | deconv5 = deconv2d(norm2, 32, 1) 95 | deconv6 = deconv2d(deconv5, 32, 1) 96 | norm3 = tf.layers.batch_normalization(deconv6) 97 | 98 | deconv7 = tf.layers.conv2d_transpose(inputs=norm3, 99 | filters=output_channels, 100 | kernel_size=3, 101 | strides=1, 102 | padding='valid', 103 | activation=tf.nn.sigmoid) 104 | return deconv7 105 | 106 | def model_fn(features, labels, mode, params, ): 107 | NAN_ELIMINATOR = 1e-7 108 | encoder_out, reshape_params = encoder(features, 109 | params['latent_dims'], 110 | is_vae=params['is_vae']) 111 | 112 | if params['is_vae']: 113 | z_mean, z_stddev = encoder_out 114 | sample = tf.random_normal(tf.shape(z_mean), 115 | 0, 0.1, 116 | dtype='float') 117 | dist_out = z_mean + (z_stddev * sample) 118 | else: 119 | dist_out = encoder_out 120 | 121 | decoder_out = decoder(dist_out, 122 | params['out_channels'], 123 | reshape_params) 124 | 125 | if mode == tf.estimator.ModeKeys.PREDICT: 126 | predictions = { 127 | 'encoder_out': dist_out, 128 | 'decoder_out': decoder_out * 255. 129 | } 130 | spec = tf.estimator.EstimatorSpec(mode=mode, 131 | predictions=predictions) 132 | else: 133 | 134 | tf.summary.image('input/noised_img', features['image'] * 255.) 135 | tf.summary.image('input/label_img', labels * 255.) 136 | tf.summary.image('output', decoder_out * 255.) 137 | 138 | if params['is_vae']: 139 | loss = vae_loss(encoder_out, decoder_out, labels) 140 | else: 141 | loss = dae_loss(decoder_out, labels) 142 | 143 | global_step = tf.train.get_global_step() 144 | if params['lr_decay']: 145 | start_lr = params['lr'] 146 | lr = tf.train.cosine_decay_restarts(start_lr, 147 | global_step, 148 | params['decay_steps']) 149 | else: 150 | lr = params['lr'] 151 | tf.summary.scalar('lr', lr) 152 | optimizer = tf.train.AdamOptimizer(lr) 153 | train_op = optimizer.minimize(loss, 154 | global_step=global_step) 155 | 156 | spec= tf.estimator.EstimatorSpec(mode=mode, 157 | loss=loss, train_op=train_op) 158 | 159 | return spec 160 | --------------------------------------------------------------------------------