├── .gitignore ├── LICENSE ├── README.md ├── Test └── Set5 │ └── butterfly_GT.bmp ├── Train ├── t1.bmp ├── t10.bmp ├── t11.bmp ├── t12.bmp ├── t13.bmp ├── t14.bmp ├── t15.bmp ├── t16.bmp ├── t17.bmp ├── t18.bmp ├── t19.bmp ├── t2.bmp ├── t20.bmp ├── t21.bmp ├── t22.bmp ├── t23.bmp ├── t24.bmp ├── t25.bmp ├── t26.bmp ├── t27.bmp ├── t28.bmp ├── t29.bmp ├── t3.bmp ├── t30.bmp ├── t31.bmp ├── t32.bmp ├── t33.bmp ├── t34.bmp ├── t35.bmp ├── t36.bmp ├── t37.bmp ├── t38.bmp ├── t39.bmp ├── t4.bmp ├── t40.bmp ├── t42.bmp ├── t43.bmp ├── t44.bmp ├── t45.bmp ├── t46.bmp ├── t47.bmp ├── t48.bmp ├── t49.bmp ├── t5.bmp ├── t50.bmp ├── t51.bmp ├── t52.bmp ├── t53.bmp ├── t54.bmp ├── t55.bmp ├── t56.bmp ├── t57.bmp ├── t58.bmp ├── t59.bmp ├── t6.bmp ├── t60.bmp ├── t61.bmp ├── t62.bmp ├── t63.bmp ├── t64.bmp ├── t65.bmp ├── t66.bmp ├── t7.bmp ├── t8.bmp ├── t9.bmp ├── tt1.bmp ├── tt10.bmp ├── tt12.bmp ├── tt13.bmp ├── tt14.bmp ├── tt15.bmp ├── tt16.bmp ├── tt17.bmp ├── tt18.bmp ├── tt19.bmp ├── tt2.bmp ├── tt20.bmp ├── tt21.bmp ├── tt22.bmp ├── tt23.bmp ├── tt24.bmp ├── tt25.bmp ├── tt26.bmp ├── tt27.bmp ├── tt3.bmp ├── tt4.bmp ├── tt5.bmp ├── tt6.bmp ├── tt7.bmp ├── tt8.bmp └── tt9.bmp ├── main.py ├── model.py └── utils.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.vscode 3 | *.swp 4 | *.__pycache__ 5 | checkpoint 6 | result 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Sam 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # TensorFlow-SRDenseNet 3 | 4 | 5 | [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/kweisamx/VDSR/blob/master/LICENSE) 6 | ## Introduction 7 | 8 | ![](https://i.imgur.com/ZlNl6Zu.png) 9 | 10 | ![](https://i.imgur.com/8TJN97r.png) 11 | 12 | We present a highly accurate single-image super-resolution (SR) method, Use the DenseNet, and use deconvulotion to scaling, the network model of densenet is: 13 | ```python 14 | def desBlock(self, desBlock_layer, outlayer, filter_size=3 ): 15 | nextlayer = self.low_conv 16 | conv = list() 17 | for i in range(1, outlayer+1): 18 | conv_in = list() 19 | for j in range(1, desBlock_layer+1): 20 | # The first conv need connect with low level layer 21 | print(i,j) 22 | if j is 1: 23 | x = tf.nn.conv2d(nextlayer, self.weight_block['w_H_%d_%d' %(i, j)], strides=[1,1,1,1], padding='SAME') + self.biases_block['b_H_%d_%d' % (i, j)] 24 | x = tf.nn.relu(x) 25 | conv_in.append(x) 26 | else: 27 | x = Concatenation(conv_in) 28 | x = tf.nn.conv2d(x, self.weight_block['w_H_%d_%d' % (i, j)], strides=[1,1,1,1], padding='SAME')+ self.biases_block['b_H_%d_%d' % (i, j)] 29 | x = tf.nn.relu(x) 30 | conv_in.append(x) 31 | 32 | nextlayer = conv_in[-1] 33 | print(conv_in[-1]) 34 | conv.append(conv_in) 35 | print(conv) 36 | return conv 37 | ``` 38 | 39 | ## Dependency 40 | 41 | pip 42 | * TensorFlow 43 | * OpenCV 44 | * h5py 45 | 46 | ## Environment 47 | 48 | * Ubuntu 16.04 49 | * Python 2.7 50 | 51 | If you meet the problem with opencv when run the program 52 | 53 | ``` 54 | libSM.so.6: cannot open shared object file: No such file or directory 55 | ``` 56 | please install dependency package 57 | ``` 58 | sudo apt-get install libsm6 59 | sudo apt-get install libxrender1 60 | ``` 61 | 62 | ## All Parameter 63 | ``` 64 | usage: main.py [-h] [--epoch EPOCH] [--image_size IMAGE_SIZE] 65 | [--label_size LABEL_SIZE] [--c_dim C_DIM] 66 | [--is_train [IS_TRAIN]] [--nois_train] [--scale SCALE] 67 | [--stride STRIDE] [--checkpoint_dir CHECKPOINT_DIR] 68 | [--learning_rate LEARNING_RATE] [--batch_size BATCH_SIZE] 69 | [--des_block_H DES_BLOCK_H] [--des_block_ALL DES_BLOCK_ALL] 70 | [--result_dir RESULT_DIR] [--growth_rate GROWTH_RATE] 71 | [--test_img TEST_IMG] 72 | 73 | optional arguments: 74 | -h, --help show this help message and exit 75 | --epoch EPOCH Number of epoch 76 | --image_size IMAGE_SIZE 77 | The size of image input 78 | --label_size LABEL_SIZE 79 | The size of label 80 | --c_dim C_DIM The size of channel 81 | --is_train [IS_TRAIN] 82 | if the train 83 | --nois_train 84 | --scale SCALE the size of scale factor for preprocessing input image 85 | --stride STRIDE the size of stride 86 | --checkpoint_dir CHECKPOINT_DIR 87 | Name of checkpoint directory 88 | --learning_rate LEARNING_RATE 89 | The learning rate 90 | --batch_size BATCH_SIZE 91 | the size of batch 92 | --des_block_H DES_BLOCK_H 93 | the size dense_block layer number 94 | --des_block_ALL DES_BLOCK_ALL 95 | the size dense_block 96 | --result_dir RESULT_DIR 97 | Name of result directory 98 | --growth_rate GROWTH_RATE 99 | the size of growrate 100 | --test_img TEST_IMG test_img 101 | ``` 102 | if you want to see the flag 103 | ``` 104 | python main.py -h 105 | ``` 106 | 107 | ## How to train 108 | 109 | ``` 110 | python main.py 111 | ``` 112 | 113 | ## How to test 114 | ``` 115 | python main.py --is_train False --stride 50 116 | ``` 117 | 118 | If you want to Test your own iamge 119 | 120 | use test_img flag 121 | ``` 122 | python main.py --is_train False --stride 50 --test_img Train/t20.bmp 123 | ``` 124 | then result image also put in the result directory 125 | 126 | ## Result 127 | 128 | 129 | * Origin 130 | 131 | ![Imgur](https://i.imgur.com/hhXBTfC.png) 132 | 133 | 134 | * Bicbuic 135 | 136 | ![Imgur](https://i.imgur.com/7UAzDf6.png) 137 | 138 | * Result 139 | 140 | ![](https://i.imgur.com/oJzclAY.png) 141 | 142 | Because the stride is 50, some part are cut. 143 | -------------------------------------------------------------------------------- /Test/Set5/butterfly_GT.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Test/Set5/butterfly_GT.bmp -------------------------------------------------------------------------------- /Train/t1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t1.bmp -------------------------------------------------------------------------------- /Train/t10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t10.bmp -------------------------------------------------------------------------------- /Train/t11.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t11.bmp -------------------------------------------------------------------------------- /Train/t12.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t12.bmp -------------------------------------------------------------------------------- /Train/t13.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t13.bmp -------------------------------------------------------------------------------- /Train/t14.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t14.bmp -------------------------------------------------------------------------------- /Train/t15.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t15.bmp -------------------------------------------------------------------------------- /Train/t16.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t16.bmp -------------------------------------------------------------------------------- /Train/t17.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t17.bmp -------------------------------------------------------------------------------- /Train/t18.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t18.bmp -------------------------------------------------------------------------------- /Train/t19.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t19.bmp -------------------------------------------------------------------------------- /Train/t2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t2.bmp -------------------------------------------------------------------------------- /Train/t20.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t20.bmp -------------------------------------------------------------------------------- /Train/t21.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t21.bmp -------------------------------------------------------------------------------- /Train/t22.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t22.bmp -------------------------------------------------------------------------------- /Train/t23.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t23.bmp -------------------------------------------------------------------------------- /Train/t24.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t24.bmp -------------------------------------------------------------------------------- /Train/t25.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t25.bmp -------------------------------------------------------------------------------- /Train/t26.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t26.bmp -------------------------------------------------------------------------------- /Train/t27.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t27.bmp -------------------------------------------------------------------------------- /Train/t28.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t28.bmp -------------------------------------------------------------------------------- /Train/t29.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t29.bmp -------------------------------------------------------------------------------- /Train/t3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t3.bmp -------------------------------------------------------------------------------- /Train/t30.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t30.bmp -------------------------------------------------------------------------------- /Train/t31.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t31.bmp -------------------------------------------------------------------------------- /Train/t32.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t32.bmp -------------------------------------------------------------------------------- /Train/t33.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t33.bmp -------------------------------------------------------------------------------- /Train/t34.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t34.bmp -------------------------------------------------------------------------------- /Train/t35.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t35.bmp -------------------------------------------------------------------------------- /Train/t36.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t36.bmp -------------------------------------------------------------------------------- /Train/t37.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t37.bmp -------------------------------------------------------------------------------- /Train/t38.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t38.bmp -------------------------------------------------------------------------------- /Train/t39.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t39.bmp -------------------------------------------------------------------------------- /Train/t4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t4.bmp -------------------------------------------------------------------------------- /Train/t40.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t40.bmp -------------------------------------------------------------------------------- /Train/t42.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t42.bmp -------------------------------------------------------------------------------- /Train/t43.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t43.bmp -------------------------------------------------------------------------------- /Train/t44.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t44.bmp -------------------------------------------------------------------------------- /Train/t45.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t45.bmp -------------------------------------------------------------------------------- /Train/t46.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t46.bmp -------------------------------------------------------------------------------- /Train/t47.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t47.bmp -------------------------------------------------------------------------------- /Train/t48.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t48.bmp -------------------------------------------------------------------------------- /Train/t49.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t49.bmp -------------------------------------------------------------------------------- /Train/t5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t5.bmp -------------------------------------------------------------------------------- /Train/t50.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t50.bmp -------------------------------------------------------------------------------- /Train/t51.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t51.bmp -------------------------------------------------------------------------------- /Train/t52.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t52.bmp -------------------------------------------------------------------------------- /Train/t53.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t53.bmp -------------------------------------------------------------------------------- /Train/t54.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t54.bmp -------------------------------------------------------------------------------- /Train/t55.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t55.bmp -------------------------------------------------------------------------------- /Train/t56.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t56.bmp -------------------------------------------------------------------------------- /Train/t57.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t57.bmp -------------------------------------------------------------------------------- /Train/t58.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t58.bmp -------------------------------------------------------------------------------- /Train/t59.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t59.bmp -------------------------------------------------------------------------------- /Train/t6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t6.bmp -------------------------------------------------------------------------------- /Train/t60.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t60.bmp -------------------------------------------------------------------------------- /Train/t61.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t61.bmp -------------------------------------------------------------------------------- /Train/t62.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t62.bmp -------------------------------------------------------------------------------- /Train/t63.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t63.bmp -------------------------------------------------------------------------------- /Train/t64.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t64.bmp -------------------------------------------------------------------------------- /Train/t65.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t65.bmp -------------------------------------------------------------------------------- /Train/t66.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t66.bmp -------------------------------------------------------------------------------- /Train/t7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t7.bmp -------------------------------------------------------------------------------- /Train/t8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t8.bmp -------------------------------------------------------------------------------- /Train/t9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/t9.bmp -------------------------------------------------------------------------------- /Train/tt1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt1.bmp -------------------------------------------------------------------------------- /Train/tt10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt10.bmp -------------------------------------------------------------------------------- /Train/tt12.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt12.bmp -------------------------------------------------------------------------------- /Train/tt13.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt13.bmp -------------------------------------------------------------------------------- /Train/tt14.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt14.bmp -------------------------------------------------------------------------------- /Train/tt15.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt15.bmp -------------------------------------------------------------------------------- /Train/tt16.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt16.bmp -------------------------------------------------------------------------------- /Train/tt17.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt17.bmp -------------------------------------------------------------------------------- /Train/tt18.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt18.bmp -------------------------------------------------------------------------------- /Train/tt19.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt19.bmp -------------------------------------------------------------------------------- /Train/tt2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt2.bmp -------------------------------------------------------------------------------- /Train/tt20.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt20.bmp -------------------------------------------------------------------------------- /Train/tt21.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt21.bmp -------------------------------------------------------------------------------- /Train/tt22.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt22.bmp -------------------------------------------------------------------------------- /Train/tt23.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt23.bmp -------------------------------------------------------------------------------- /Train/tt24.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt24.bmp -------------------------------------------------------------------------------- /Train/tt25.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt25.bmp -------------------------------------------------------------------------------- /Train/tt26.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt26.bmp -------------------------------------------------------------------------------- /Train/tt27.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt27.bmp -------------------------------------------------------------------------------- /Train/tt3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt3.bmp -------------------------------------------------------------------------------- /Train/tt4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt4.bmp -------------------------------------------------------------------------------- /Train/tt5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt5.bmp -------------------------------------------------------------------------------- /Train/tt6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt6.bmp -------------------------------------------------------------------------------- /Train/tt7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt7.bmp -------------------------------------------------------------------------------- /Train/tt8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt8.bmp -------------------------------------------------------------------------------- /Train/tt9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kweisamx/TensorFlow-SR-DenseNet/f1cf8771f523c956c3346e5e8ba4849e5a3a1386/Train/tt9.bmp -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from model import SRDense 3 | flags = tf.app.flags 4 | FLAGS = flags.FLAGS 5 | flags.DEFINE_integer("epoch", 15000, "Number of epoch") 6 | flags.DEFINE_integer("image_size", 50, "The size of image input") 7 | flags.DEFINE_integer("label_size", 100, "The size of label") 8 | flags.DEFINE_integer("c_dim", 3, "The size of channel") 9 | flags.DEFINE_boolean("is_train", True, "if the train") 10 | flags.DEFINE_integer("scale", 2, "the size of scale factor for preprocessing input image") 11 | flags.DEFINE_integer("stride", 17, "the size of stride") 12 | flags.DEFINE_string("checkpoint_dir", "checkpoint", "Name of checkpoint directory") 13 | flags.DEFINE_float("learning_rate", 1e-5, "The learning rate") 14 | flags.DEFINE_integer("batch_size", 32, "the size of batch") 15 | flags.DEFINE_integer("des_block_H", 8, "the size dense_block layer number") 16 | flags.DEFINE_integer("des_block_ALL", 8,"the size dense_block") 17 | flags.DEFINE_string("result_dir", "result", "Name of result directory") 18 | flags.DEFINE_integer("growth_rate", 16, "the size of growrate") 19 | flags.DEFINE_string("test_img", "", "test_img") 20 | 21 | 22 | 23 | 24 | def main(_): #? 25 | with tf.Session() as sess: 26 | srdense = SRDense(sess, 27 | image_size = FLAGS.image_size, 28 | label_size = FLAGS.label_size, 29 | is_train = FLAGS.is_train, 30 | scale = FLAGS.scale, 31 | c_dim = FLAGS.c_dim, 32 | batch_size = FLAGS.batch_size, 33 | test_img = FLAGS.test_img, 34 | des_block_H = FLAGS.des_block_H, 35 | des_block_ALL = FLAGS.des_block_ALL, 36 | growth_rate = FLAGS.growth_rate, 37 | ) 38 | 39 | srdense.train(FLAGS) 40 | 41 | if __name__=='__main__': 42 | tf.app.run() # parse the command argument , the call the main function 43 | -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import time 4 | import os 5 | import cv2 6 | from utils import ( 7 | input_setup, 8 | checkpoint_dir, 9 | read_data, 10 | checkimage, 11 | imsave, 12 | imread, 13 | load_data, 14 | preprocess, 15 | modcrop, 16 | merge, 17 | ) 18 | def Concatenation(layers): 19 | return tf.concat(layers, axis=3) 20 | 21 | def SkipConnect(conv): 22 | skipconv = list() 23 | for i in conv: 24 | x = Concatenation(i) 25 | skipconv.append(x) 26 | return skipconv 27 | 28 | class SRDense(object): 29 | 30 | def __init__(self, 31 | sess, 32 | image_size, 33 | label_size, 34 | is_train, 35 | scale, 36 | batch_size, 37 | c_dim, 38 | test_img, 39 | des_block_H, 40 | des_block_ALL, 41 | growth_rate 42 | ): 43 | 44 | self.sess = sess 45 | self.image_size = image_size 46 | self.is_train = is_train 47 | self.c_dim = c_dim 48 | self.scale = scale 49 | self.label_size = label_size 50 | self.batch_size = batch_size 51 | self.test_img = test_img 52 | self.des_block_H = des_block_H 53 | self.des_block_ALL = des_block_ALL 54 | self.growth_rate = growth_rate 55 | 56 | self.build_model() 57 | 58 | # Create DenseNet init weight and biases, init_block mean the growrate 59 | def DesWBH(self, desBlock_layer, filter_size, outlayer): 60 | weightsH = {} 61 | biasesH = {} 62 | fs = filter_size 63 | for i in range(1, outlayer+1): 64 | for j in range(1, desBlock_layer+1): 65 | if j is 1: 66 | weightsH.update({'w_H_%d_%d' % (i, j): tf.Variable(tf.random_normal([fs, fs, 16, 16], stddev=np.sqrt(2.0/9/16)), name='w_H_%d_%d' % (i, j))}) 67 | else: 68 | weightsH.update({'w_H_%d_%d' % (i, j): tf.Variable(tf.random_normal([fs, fs, self.growth_rate * (j-1), self.growth_rate], stddev=np.sqrt(2.0/9/(self.growth_rate * (j-1)))), name='w_H_%d_%d' % (i, j))}) 69 | biasesH.update({'b_H_%d_%d' % (i, j): tf.Variable(tf.zeros([self.growth_rate], name='b_H_%d_%d' % (i, j)))}) 70 | return weightsH, biasesH 71 | 72 | 73 | # Create one Dense Block Convolution Layer 74 | def desBlock(self, desBlock_layer, outlayer, filter_size=3 ): 75 | nextlayer = self.low_conv 76 | conv = list() 77 | for i in range(1, outlayer+1): 78 | conv_in = list() 79 | for j in range(1, desBlock_layer+1): 80 | # The first conv need connect with low level layer 81 | if j is 1: 82 | x = tf.nn.conv2d(nextlayer, self.weight_block['w_H_%d_%d' %(i, j)], strides=[1,1,1,1], padding='SAME') + self.biases_block['b_H_%d_%d' % (i, j)] 83 | x = tf.nn.relu(x) 84 | conv_in.append(x) 85 | else: 86 | x = Concatenation(conv_in) 87 | x = tf.nn.conv2d(x, self.weight_block['w_H_%d_%d' % (i, j)], strides=[1,1,1,1], padding='SAME')+ self.biases_block['b_H_%d_%d' % (i, j)] 88 | x = tf.nn.relu(x) 89 | conv_in.append(x) 90 | 91 | nextlayer = conv_in[-1] 92 | conv.append(conv_in) 93 | return conv 94 | 95 | def bot_layer(self, input_layer): 96 | x = tf.nn.conv2d(input_layer, self.bot_weight, strides=[1,1,1,1], padding='SAME') + self.bot_biases 97 | x = tf.nn.relu(x) 98 | return x 99 | 100 | def deconv_layer(self, input_layer): 101 | x = tf.nn.conv2d_transpose(input_layer, self.deconv1_weight, output_shape=self.deconv_output, strides=[1,2,2,1], padding='SAME') + self.deconv1_biases 102 | x = tf.nn.relu(x) 103 | return x 104 | def reconv_layer(self, input_layer): 105 | x = tf.nn.conv2d(input_layer, self.reconv_weight, strides=[1,1,1,1], padding='SAME') + self.reconv_biases 106 | return x 107 | 108 | 109 | 110 | def model(self): 111 | x = self.desBlock(self.des_block_H, self.des_block_ALL, filter_size = 3) 112 | # NOTE: Cocate all dense block 113 | 114 | x = SkipConnect(x) 115 | x.append(self.low_conv) 116 | x = Concatenation(x) 117 | x = self.bot_layer(x) 118 | x = self.deconv_layer(x) 119 | x = self.reconv_layer(x) 120 | 121 | return x 122 | 123 | 124 | def build_model(self): 125 | self.images = tf.placeholder(tf.float32, [None, self.image_size, self.image_size, self.c_dim], name='images') 126 | self.labels = tf.placeholder(tf.float32, [None, self.label_size, self.label_size, self.c_dim], name='labels') 127 | self.deconv_output = tf.placeholder(dtype=tf.int32, shape=[4]) 128 | 129 | self.batch = tf.placeholder(tf.int32, shape=[], name='batch') 130 | 131 | # Low Level Layer 132 | self.low_weight = tf.Variable(tf.random_normal([3, 3, self.c_dim, 16], stddev= np.sqrt(2.0/9/self.c_dim)), name='w_low') 133 | self.low_biases = tf.Variable(tf.zeros([16], name='b_low')) 134 | self.low_conv = tf.nn.relu(tf.nn.conv2d(self.images, self.low_weight, strides=[1,1,1,1], padding='SAME') + self.low_biases) 135 | 136 | # NOTE: Init each block weight 137 | """ 138 | 16 -> 128 -> 1024 139 | """ 140 | # DenseNet blocks 141 | self.weight_block, self.biases_block = self.DesWBH(self.des_block_H, 3, self.des_block_ALL) 142 | 143 | # Bottleneck layer 144 | allfeature = self.growth_rate * self.des_block_H * self.des_block_ALL + 16 145 | 146 | self.bot_weight = tf.Variable(tf.random_normal([1, 1, allfeature, 256], stddev = np.sqrt(2.0/1/allfeature)), name='w_bot') 147 | self.bot_biases = tf.Variable(tf.zeros([256], name='b_bot')) 148 | 149 | # Deconvolution layer 150 | self.deconv1_weight = tf.Variable(tf.random_normal([2, 2, 256, 256], stddev = np.sqrt(2.0/4/256)), name='w_deconv1') 151 | self.deconv1_biases = tf.Variable(tf.zeros([256], name='b_deconv1')) 152 | 153 | # Reconstruction layer 154 | 155 | self.reconv_weight = tf.Variable(tf.random_normal([3, 3, 256, self.c_dim], stddev = np.sqrt(2.0/9/256)), name ='w_reconv') 156 | self.reconv_biases = tf.Variable(tf.zeros([self.c_dim], name='b_reconv')) 157 | 158 | 159 | self.pred = self.model() 160 | 161 | self.loss = tf.reduce_mean(tf.square(self.labels - self.pred)) 162 | 163 | self.saver = tf.train.Saver() # To save checkpoint 164 | 165 | def train(self, config): 166 | 167 | # NOTE : if train, the nx, ny are ingnored 168 | nx, ny = input_setup(config) 169 | 170 | data_dir = checkpoint_dir(config) 171 | 172 | input_, label_ = read_data(data_dir) 173 | 174 | # Stochastic gradient descent with tself.des_block_ALLhe standard backpropagation 175 | self.train_op = tf.train.AdamOptimizer(learning_rate=config.learning_rate).minimize(self.loss) 176 | tf.initialize_all_variables().run() 177 | 178 | counter = 0 179 | time_ = time.time() 180 | 181 | self.load(config.checkpoint_dir) 182 | # Train 183 | if config.is_train: 184 | print("Now Start Training...") 185 | for ep in range(config.epoch): 186 | # Run by batch images 187 | batch_idxs = len(input_) // config.batch_size 188 | for idx in range(0, batch_idxs): 189 | batch_images = input_[idx * config.batch_size : (idx + 1) * config.batch_size] 190 | batch_labels = label_[idx * config.batch_size : (idx + 1) * config.batch_size] 191 | counter += 1 192 | _, err = self.sess.run([self.train_op, self.loss], feed_dict={self.images: batch_images, self.labels: batch_labels, self.batch: 1, self.deconv_output:[self.batch_size, self.label_size, self.label_size, 256]}) 193 | if counter % 10 == 0: 194 | print("Epoch: [%2d], step: [%2d], time: [%4.4f], loss: [%.8f]" % ((ep+1), counter, time.time()-time_, err)) 195 | if counter % 500 == 0: 196 | self.save(config.checkpoint_dir, counter) 197 | # Test 198 | else: 199 | print("Now Start Testing...") 200 | res = list() 201 | for i in range(len(input_)): 202 | result = self.pred.eval({self.images: input_[i].reshape(1, input_[i].shape[0], input_[i].shape[1],3), self.deconv_output:[1, self.label_size, self.label_size, 256]}) 203 | # back to interval [0 , 1] 204 | x = np.squeeze(result) 205 | x = ( x + 1 ) / 2 206 | res.append(x) 207 | res = np.asarray(res) 208 | res = merge(res, [nx, ny], self.c_dim) 209 | 210 | if self.test_img is "": 211 | imsave(res, config.result_dir + '/result.png', config) 212 | else: 213 | string = self.test_img.split(".") 214 | print(string) 215 | imsave(res, config.result_dir + '/' + string[0] + '.png', config) 216 | 217 | def load(self, checkpoint_dir): 218 | """ 219 | To load the checkpoint use to test or pretrain 220 | """ 221 | print("\nReading Checkpoints.....\n\n") 222 | model_dir = "%s_%s_%s" % ("dnsr", self.image_size, self.scale)# give the model name by label_size 223 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 224 | ckpt = tf.train.get_checkpoint_state(checkpoint_dir) 225 | 226 | # Check the checkpoint is exist 227 | if ckpt and ckpt.model_checkpoint_path: 228 | ckpt_path = str(ckpt.model_checkpoint_path) # convert the unicode to string 229 | self.saver.restore(self.sess, os.path.join(os.getcwd(), ckpt_path)) 230 | print("\n Checkpoint Loading Success! %s\n\n"% ckpt_path) 231 | else: 232 | print("\n! Checkpoint Loading Failed \n\n") 233 | def save(self, checkpoint_dir, step): 234 | """ 235 | To save the checkpoint use to test or pretrain 236 | """ 237 | model_name = "DenseNetSR.model" 238 | model_dir = "%s_%s_%s" % ("dnsr", self.image_size, self.scale) 239 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 240 | 241 | if not os.path.exists(checkpoint_dir): 242 | os.makedirs(checkpoint_dir) 243 | 244 | self.saver.save(self.sess, 245 | os.path.join(checkpoint_dir, model_name), 246 | global_step=step) 247 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import tensorflow as tf 4 | import os 5 | import glob 6 | import h5py 7 | 8 | 9 | 10 | # Get the Image 11 | def imread(path): 12 | img = cv2.imread(path) 13 | return img 14 | 15 | def imsave(image, path, config): 16 | # Check the result dir, if not, create one 17 | if not os.path.isdir(os.path.join(os.getcwd(),config.result_dir)): 18 | os.makedirs(os.path.join(os.getcwd(),config.result_dir)) 19 | print(os.path.join(os.getcwd(), path)) 20 | # NOTE: because normial, we need mutlify 255 back 21 | cv2.imwrite(os.path.join(os.getcwd(),path),image * 255.) 22 | 23 | def checkimage(image): 24 | cv2.imshow("test",image) 25 | cv2.waitKey(0) 26 | 27 | def modcrop(img, scale =3): 28 | """ 29 | To scale down and up the original image, first thing to do is to have no remainder while scaling operation. 30 | """ 31 | # Check the image is grayscale 32 | if len(img.shape) ==3: 33 | h, w, _ = img.shape 34 | h = (h / scale) * scale 35 | w = (w / scale) * scale 36 | img = img[0:h, 0:w, :] 37 | else: 38 | h, w = img.shape 39 | h = (h / scale) * scale 40 | w = (w / scale) * scale 41 | img = img[0:h, 0:w] 42 | return img 43 | 44 | def checkpoint_dir(config): 45 | if config.is_train: 46 | return os.path.join('./{}'.format(config.checkpoint_dir), "train.h5") 47 | else: 48 | return os.path.join('./{}'.format(config.checkpoint_dir), "test.h5") 49 | 50 | def preprocess(path ,scale = 3): 51 | """ 52 | Args: 53 | path: the image directory path 54 | scale: the image need to scale 55 | """ 56 | img = imread(path) 57 | 58 | label_ = modcrop(img, scale) 59 | 60 | input_ = cv2.resize(label_, None, fx = 1.0/scale, fy = 1.0/scale, interpolation = cv2.INTER_AREA) # Resize by scaling factor 61 | 62 | 63 | return input_, label_ 64 | 65 | def prepare_data(dataset="Train",Input_img=""): 66 | """ 67 | Args: 68 | dataset: choose train dataset or test dataset 69 | For train dataset, output data would be ['.../t1.bmp', '.../t2.bmp',..., 't99.bmp'] 70 | """ 71 | if dataset == "Train": 72 | data_dir = os.path.join(os.getcwd(), dataset) # Join the Train dir to current directory 73 | data = glob.glob(os.path.join(data_dir, "*.bmp")) # make set of all dataset file path 74 | else: 75 | if Input_img !="": 76 | data = [os.path.join(os.getcwd(),Input_img)] 77 | else: 78 | data_dir = os.path.join(os.path.join(os.getcwd(), dataset), "Set5") 79 | data = glob.glob(os.path.join(data_dir, "*.bmp")) # make set of all dataset file path 80 | print(data) 81 | return data 82 | 83 | def load_data(is_train, test_img): 84 | if is_train: 85 | data = prepare_data(dataset="Train") 86 | else: 87 | if test_img != "": 88 | return prepare_data(dataset="Test",Input_img=test_img) 89 | data = prepare_data(dataset="Test") 90 | return data 91 | 92 | def make_sub_data(data, config): 93 | """ 94 | Make the sub_data set 95 | Args: 96 | data : the set of all file path 97 | config : the all flags 98 | """ 99 | sub_input_sequence = [] 100 | sub_label_sequence = [] 101 | for i in range(len(data)): 102 | input_, label_, = preprocess(data[i], config.scale) 103 | 104 | if len(input_.shape) == 3: # is color 105 | h, w, c = input_.shape 106 | else: 107 | h, w = input_.shape # is grayscale 108 | 109 | # NOTE: make subimage of LR and HR 110 | 111 | # Input 112 | nx, ny = 0, 0 113 | for x in range(0, h - config.image_size + 1, config.stride): 114 | nx += 1; ny = 0 115 | for y in range(0, w - config.image_size + 1, config.stride): 116 | ny += 1 117 | sub_input = input_[x: x + config.image_size, y: y + config.image_size] # 17 * 17 118 | #checkimage(sub_input) 119 | 120 | 121 | # Reshape the subinput and sublabel 122 | sub_input = sub_input.reshape([config.image_size, config.image_size, config.c_dim]) 123 | 124 | # Normialize 125 | sub_input = (sub_input / 255.0) * 2 - 1 126 | 127 | # Add to sequence 128 | sub_input_sequence.append(sub_input) 129 | 130 | # Label (the time of scale) 131 | for x in range(0, h * config.scale - config.image_size * config.scale + 1, config.stride * config.scale): 132 | for y in range(0, w * config.scale - config.image_size * config.scale + 1, config.stride * config.scale): 133 | sub_label = label_[x: x + config.image_size * config.scale, y: y + config.image_size * config.scale] # 17r * 17r 134 | #checkimage(sub_label) 135 | # Reshape the subinput and sublabel 136 | sub_label = sub_label.reshape([config.image_size * config.scale , config.image_size * config.scale, config.c_dim]) 137 | 138 | # Normialize 139 | sub_label = (sub_label / 255.0) * 2 - 1 140 | # Add to sequence 141 | sub_label_sequence.append(sub_label) 142 | 143 | return sub_input_sequence, sub_label_sequence , nx, ny 144 | 145 | 146 | def read_data(path): 147 | """ 148 | Read h5 format data file 149 | 150 | Args: 151 | path: file path of desired file 152 | data: '.h5' file format that contains input values 153 | label: '.h5' file format that contains label values 154 | """ 155 | with h5py.File(path, 'r') as hf: 156 | input_ = np.array(hf.get('input')) 157 | label_ = np.array(hf.get('label')) 158 | return input_, label_ 159 | 160 | def make_data_hf(input_, label_, config): 161 | """ 162 | Make input data as h5 file format 163 | Depending on "is_train" (flag value), savepath would be change. 164 | """ 165 | # Check the check dir, if not, create one 166 | if not os.path.isdir(os.path.join(os.getcwd(),config.checkpoint_dir)): 167 | os.makedirs(os.path.join(os.getcwd(),config.checkpoint_dir)) 168 | 169 | if config.is_train: 170 | savepath = os.path.join(os.getcwd(), config.checkpoint_dir + '/train.h5') 171 | else: 172 | savepath = os.path.join(os.getcwd(), config.checkpoint_dir + '/test.h5') 173 | 174 | with h5py.File(savepath, 'w') as hf: 175 | hf.create_dataset('input', data=input_) 176 | hf.create_dataset('label', data=label_) 177 | 178 | def input_setup(config): 179 | """ 180 | Read image files and make their sub-images and saved them as a h5 file format 181 | """ 182 | 183 | # Load data path, if is_train False, get test data 184 | data = load_data(config.is_train, config.test_img) 185 | 186 | 187 | # Make sub_input and sub_label, if is_train false more return nx, ny 188 | sub_input_sequence, sub_label_sequence, nx, ny = make_sub_data(data, config) 189 | 190 | 191 | # Make list to numpy array. With this transform 192 | arrinput = np.asarray(sub_input_sequence) 193 | arrlabel = np.asarray(sub_label_sequence) 194 | make_data_hf(arrinput, arrlabel, config) 195 | return nx, ny 196 | 197 | 198 | def merge(images, size, c_dim): 199 | """ 200 | images is the sub image set, merge it 201 | """ 202 | h, w = images.shape[1], images.shape[2] 203 | 204 | img = np.zeros((h*size[0], w*size[1], c_dim)) 205 | for idx, image in enumerate(images): 206 | i = idx % size[1] 207 | j = idx // size[1] 208 | img[j * h : j * h + h,i * w : i * w + w, :] = image 209 | 210 | return img 211 | --------------------------------------------------------------------------------