├── save_para └── READMEN.txt ├── IMAGES ├── method.jpg ├── noised1.jpg ├── noised2.jpg ├── noised3.jpg ├── noised4.jpg ├── noised5.jpg ├── noised6.jpg ├── noised7.jpg ├── denoised1.jpg ├── denoised2.jpg ├── denoised3.jpg ├── denoised4.jpg ├── denoised5.jpg ├── denoised6.jpg └── denoised7.jpg ├── TestingSet ├── 01.png ├── 02.png ├── 03.png ├── 04.png ├── 05.png ├── 06.png ├── 07.png ├── 08.png ├── 09.png ├── 10.png ├── 11.png └── 12.png ├── TrainingSet ├── 1_17.jpg ├── 1_18.jpg ├── 1_19.jpg ├── 1_20.jpg ├── 1_25.jpg ├── 1_26.jpg ├── 1_27.jpg └── 1_28.jpg ├── TrainingResults ├── 0_1440.jpg ├── 0_1520.jpg └── 0_1600.jpg ├── config.py ├── network.py ├── LICENSE ├── DnCNN.py ├── README.md └── ops.py /save_para/READMEN.txt: -------------------------------------------------------------------------------- 1 | Save the weights of the model. -------------------------------------------------------------------------------- /IMAGES/method.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/method.jpg -------------------------------------------------------------------------------- /IMAGES/noised1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/noised1.jpg -------------------------------------------------------------------------------- /IMAGES/noised2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/noised2.jpg -------------------------------------------------------------------------------- /IMAGES/noised3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/noised3.jpg -------------------------------------------------------------------------------- /IMAGES/noised4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/noised4.jpg -------------------------------------------------------------------------------- /IMAGES/noised5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/noised5.jpg -------------------------------------------------------------------------------- /IMAGES/noised6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/noised6.jpg -------------------------------------------------------------------------------- /IMAGES/noised7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/noised7.jpg -------------------------------------------------------------------------------- /TestingSet/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/01.png -------------------------------------------------------------------------------- /TestingSet/02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/02.png -------------------------------------------------------------------------------- /TestingSet/03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/03.png -------------------------------------------------------------------------------- /TestingSet/04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/04.png -------------------------------------------------------------------------------- /TestingSet/05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/05.png -------------------------------------------------------------------------------- /TestingSet/06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/06.png -------------------------------------------------------------------------------- /TestingSet/07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/07.png -------------------------------------------------------------------------------- /TestingSet/08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/08.png -------------------------------------------------------------------------------- /TestingSet/09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/09.png -------------------------------------------------------------------------------- /TestingSet/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/10.png -------------------------------------------------------------------------------- /TestingSet/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/11.png -------------------------------------------------------------------------------- /TestingSet/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TestingSet/12.png -------------------------------------------------------------------------------- /IMAGES/denoised1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/denoised1.jpg -------------------------------------------------------------------------------- /IMAGES/denoised2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/denoised2.jpg -------------------------------------------------------------------------------- /IMAGES/denoised3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/denoised3.jpg -------------------------------------------------------------------------------- /IMAGES/denoised4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/denoised4.jpg -------------------------------------------------------------------------------- /IMAGES/denoised5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/denoised5.jpg -------------------------------------------------------------------------------- /IMAGES/denoised6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/denoised6.jpg -------------------------------------------------------------------------------- /IMAGES/denoised7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/IMAGES/denoised7.jpg -------------------------------------------------------------------------------- /TrainingSet/1_17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_17.jpg -------------------------------------------------------------------------------- /TrainingSet/1_18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_18.jpg -------------------------------------------------------------------------------- /TrainingSet/1_19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_19.jpg -------------------------------------------------------------------------------- /TrainingSet/1_20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_20.jpg -------------------------------------------------------------------------------- /TrainingSet/1_25.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_25.jpg -------------------------------------------------------------------------------- /TrainingSet/1_26.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_26.jpg -------------------------------------------------------------------------------- /TrainingSet/1_27.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_27.jpg -------------------------------------------------------------------------------- /TrainingSet/1_28.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingSet/1_28.jpg -------------------------------------------------------------------------------- /TrainingResults/0_1440.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingResults/0_1440.jpg -------------------------------------------------------------------------------- /TrainingResults/0_1520.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingResults/0_1520.jpg -------------------------------------------------------------------------------- /TrainingResults/0_1600.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/DnCNN-Denoise-Gaussian-noise-TensorFlow/HEAD/TrainingResults/0_1600.jpg -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | IMG_H = 40 2 | IMG_W = 40 3 | IMG_C = 1 4 | DEPTH = 17 5 | BATCH_SIZE = 32 6 | EPOCHS = 50 7 | SIGMA = 25 8 | EPSILON = 1e-10 9 | -------------------------------------------------------------------------------- /network.py: -------------------------------------------------------------------------------- 1 | from ops import * 2 | from config import * 3 | import numpy as np 4 | 5 | class net: 6 | def __init__(self, name): 7 | self.name = name 8 | 9 | def __call__(self, inputs, train_phase): 10 | with tf.variable_scope(self.name): 11 | inputs = tf.nn.relu(conv("conv0", inputs, 64, 3, 1)) 12 | for d in np.arange(1, DEPTH - 1): 13 | inputs = tf.nn.relu(batchnorm(conv("conv_" + str(d + 1), inputs, 64, 3, 1), train_phase, "bn" + str(d))) 14 | inputs = conv("conv" + str(DEPTH - 1), inputs, IMG_C, 3, 1) 15 | return inputs -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 MingtaoGuo 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 | -------------------------------------------------------------------------------- /DnCNN.py: -------------------------------------------------------------------------------- 1 | from network import * 2 | from PIL import Image 3 | import scipy.misc as misc 4 | import os 5 | 6 | 7 | class DnCNN: 8 | def __init__(self): 9 | self.clean_img = tf.placeholder(tf.float32, [None, None, None, IMG_C]) 10 | self.noised_img = tf.placeholder(tf.float32, [None, None, None, IMG_C]) 11 | self.train_phase = tf.placeholder(tf.bool) 12 | dncnn = net("DnCNN") 13 | self.res = dncnn(self.noised_img, self.train_phase) 14 | self.denoised_img = self.noised_img - self.res 15 | self.loss = tf.reduce_mean(tf.reduce_sum(tf.square(self.res - (self.noised_img - self.clean_img)), [1, 2, 3])) 16 | self.Opt = tf.train.AdamOptimizer(1e-3).minimize(self.loss) 17 | self.sess = tf.Session() 18 | self.sess.run(tf.global_variables_initializer()) 19 | 20 | def train(self): 21 | filepath = "./TrainingSet//" 22 | filenames = os.listdir(filepath) 23 | saver = tf.train.Saver() 24 | for epoch in range(50): 25 | for i in range(filenames.__len__()//BATCH_SIZE): 26 | cleaned_batch = np.zeros([BATCH_SIZE, IMG_H, IMG_W, IMG_C]) 27 | for idx, filename in enumerate(filenames[i*BATCH_SIZE:i*BATCH_SIZE+BATCH_SIZE]): 28 | cleaned_batch[idx, :, :, 0] = np.array(Image.open(filepath+filename)) 29 | noised_batch = cleaned_batch + np.random.normal(0, SIGMA, cleaned_batch.shape) 30 | self.sess.run(self.Opt, feed_dict={self.clean_img: cleaned_batch, self.noised_img: noised_batch, self.train_phase: True}) 31 | if i % 10 == 0: 32 | [loss, denoised_img] = self.sess.run([self.loss, self.denoised_img], feed_dict={self.clean_img: cleaned_batch, self.noised_img: noised_batch, self.train_phase: False}) 33 | print("Epoch: %d, Step: %d, Loss: %g"%(epoch, i, loss)) 34 | compared = np.concatenate((cleaned_batch[0, :, :, 0], noised_batch[0, :, :, 0], denoised_img[0, :, :, 0]), 1) 35 | Image.fromarray(np.uint8(compared)).save("./TrainingResults//"+str(epoch)+"_"+str(i)+".jpg") 36 | if i % 500 == 0: 37 | saver.save(self.sess, "./save_para//DnCNN.ckpt") 38 | np.random.shuffle(filenames) 39 | 40 | def test(self, cleaned_path="./TestingSet//02.png"): 41 | saver = tf.train.Saver() 42 | saver.restore(self.sess, "./save_para/DnCNN.ckpt") 43 | cleaned_img = np.reshape(np.array(misc.imresize(np.array(Image.open(cleaned_path)), [256, 256])), [1, 256, 256, 1]) 44 | noised_img = cleaned_img + np.random.normal(0, SIGMA, cleaned_img.shape) 45 | [denoised_img] = self.sess.run([self.denoised_img], feed_dict={self.clean_img: cleaned_img, self.noised_img: noised_img, self.train_phase: False}) 46 | compared = np.concatenate((cleaned_img[0, :, :, 0], noised_img[0, :, :, 0], denoised_img[0, :, :, 0]), 1) 47 | Image.fromarray(np.uint8(compared)).show() 48 | 49 | 50 | if __name__ == "__main__": 51 | dncnn = DnCNN() 52 | dncnn.train() 53 | 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DnCNN-TensorFlow 2 | Simple implementation of the paper (DnCNN)'Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising' 3 | ## Introduction 4 | This code just simplely implement the paper [Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising](http://cn.arxiv.org/pdf/1608.03981), but there are some details of the code are different from the paper. 5 | ![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/method.jpg) 6 | ## DataSets 7 | The datasets include 400 gray images, but i have croped them into 40x40 patches. the croped datasets can be downloaded from my [BaiduYun](https://pan.baidu.com/s/1Uiq29K2WLvOyeGlnRu8j_A) 8 | 9 | Examples of TrainingSet 10 | 11 | ||||||||| 12 | |-|-|-|-|-|-|-|-| 13 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_17.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_18.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_19.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_20.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_25.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_26.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_27.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TrainingSet/1_28.jpg)| 14 | ## Python packages 15 | ==================== 16 | 1. python3.5 17 | 2. tensorflow1.4.0 18 | 3. pillow 19 | 4. numpy 20 | 5. scipy 21 | 22 | ==================== 23 | ## Results of the code 24 | Trained about 1 epoch 25 | 26 | |Raw|Noised|Denoised| 27 | |-|-|-| 28 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TestingSet/01.png)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/noised1.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/denoised1.jpg)| 29 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TestingSet/02.png)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/noised2.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/denoised2.jpg)| 30 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TestingSet/03.png)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/noised3.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/denoised3.jpg)| 31 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TestingSet/04.png)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/noised4.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/denoised4.jpg)| 32 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TestingSet/05.png)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/noised5.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/denoised5.jpg)| 33 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TestingSet/06.png)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/noised6.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/denoised6.jpg)| 34 | |![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/TestingSet/07.png)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/noised7.jpg)|![](https://github.com/MingtaoGuo/DnCNN-TensorFlow/blob/master/IMAGES/denoised7.jpg)| 35 | -------------------------------------------------------------------------------- /ops.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | 5 | def batchnorm(x, train_phase, scope_bn): 6 | #Batch Normalization 7 | #Ioffe S, Szegedy C. Batch normalization: accelerating deep network training by reducing internal covariate shift[J]. 2015:448-456. 8 | with tf.variable_scope(scope_bn, reuse=tf.AUTO_REUSE): 9 | beta = tf.get_variable(name='beta', shape=[x.shape[-1]], initializer=tf.constant_initializer([0.]), trainable=True) 10 | gamma = tf.get_variable(name='gamma', shape=[x.shape[-1]], initializer=tf.constant_initializer([1.]), trainable=True) 11 | batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2], name='moments') 12 | ema = tf.train.ExponentialMovingAverage(decay=0.5) 13 | 14 | def mean_var_with_update(): 15 | ema_apply_op = ema.apply([batch_mean, batch_var]) 16 | with tf.control_dependencies([ema_apply_op]): 17 | return tf.identity(batch_mean), tf.identity(batch_var) 18 | 19 | mean, var = tf.cond(train_phase, mean_var_with_update, 20 | lambda: (ema.average(batch_mean), ema.average(batch_var))) 21 | normed = tf.nn.batch_normalization(x, mean, var, beta, gamma, 1e-3) 22 | return normed 23 | 24 | def InstanceNorm(inputs, name): 25 | with tf.variable_scope(name): 26 | mean, var = tf.nn.moments(inputs, axes=[1, 2], keep_dims=True) 27 | scale = tf.get_variable("scale", shape=mean.shape[-1], initializer=tf.constant_initializer([1.])) 28 | shift = tf.get_variable("shift", shape=mean.shape[-1], initializer=tf.constant_initializer([0.])) 29 | return (inputs - mean) * scale / tf.sqrt(var + 1e-10) + shift 30 | 31 | def conv(name, inputs, nums_out, ksize, strides, padding="SAME", is_SN=False): 32 | with tf.variable_scope(name): 33 | W = tf.get_variable("W", shape=[ksize, ksize, int(inputs.shape[-1]), nums_out], initializer=tf.truncated_normal_initializer(stddev=0.02)) 34 | b = tf.get_variable("b", shape=[nums_out], initializer=tf.constant_initializer(0.)) 35 | if is_SN: 36 | return tf.nn.conv2d(inputs, spectral_norm(name, W), [1, strides, strides, 1], padding) + b 37 | else: 38 | return tf.nn.conv2d(inputs, W, [1, strides, strides, 1], padding) + b 39 | 40 | def uconv(name, inputs, nums_out, ksize, strides, padding="SAME"): 41 | with tf.variable_scope(name): 42 | w = tf.get_variable("W", shape=[ksize, ksize, nums_out, int(inputs.shape[-1])], initializer=tf.truncated_normal_initializer(stddev=0.02)) 43 | b = tf.get_variable("b", [nums_out], initializer=tf.constant_initializer(0.)) 44 | # inputs = tf.image.resize_nearest_neighbor(inputs, [H*strides, W*strides]) 45 | # return tf.nn.conv2d(inputs, w, [1, 1, 1, 1], padding) + b 46 | return tf.nn.conv2d_transpose(inputs, w, [tf.shape(inputs)[0], int(inputs.shape[1])*strides, int(inputs.shape[2])*strides, nums_out], [1, strides, strides, 1], padding=padding) + b 47 | 48 | 49 | def fully_connected(name, inputs, nums_out): 50 | with tf.variable_scope(name, reuse=tf.AUTO_REUSE): 51 | W = tf.get_variable("W", [int(inputs.shape[-1]), nums_out], initializer=tf.truncated_normal_initializer(stddev=0.02)) 52 | b = tf.get_variable("b", [nums_out], initializer=tf.constant_initializer(0.)) 53 | return tf.matmul(inputs, W) + b 54 | 55 | 56 | def spectral_norm(name, w, iteration=1): 57 | #Spectral normalization which was published on ICLR2018,please refer to "https://www.researchgate.net/publication/318572189_Spectral_Normalization_for_Generative_Adversarial_Networks" 58 | #This function spectral_norm is forked from "https://github.com/taki0112/Spectral_Normalization-Tensorflow" 59 | w_shape = w.shape.as_list() 60 | w = tf.reshape(w, [-1, w_shape[-1]]) 61 | with tf.variable_scope(name, reuse=False): 62 | u = tf.get_variable("u", [1, w_shape[-1]], initializer=tf.truncated_normal_initializer(), trainable=False) 63 | u_hat = u 64 | v_hat = None 65 | 66 | def l2_norm(v, eps=1e-12): 67 | return v / (tf.reduce_sum(v ** 2) ** 0.5 + eps) 68 | 69 | for i in range(iteration): 70 | v_ = tf.matmul(u_hat, tf.transpose(w)) 71 | v_hat = l2_norm(v_) 72 | u_ = tf.matmul(v_hat, w) 73 | u_hat = l2_norm(u_) 74 | sigma = tf.matmul(tf.matmul(v_hat, w), tf.transpose(u_hat)) 75 | w_norm = w / sigma 76 | with tf.control_dependencies([u.assign(u_hat)]): 77 | w_norm = tf.reshape(w_norm, w_shape) 78 | return w_norm 79 | 80 | def leaky_relu(x, slope=0.2): 81 | return tf.maximum(x, slope*x) 82 | 83 | --------------------------------------------------------------------------------