├── .gitignore ├── README.md ├── autoencoder.py ├── decoder.py ├── encoder.py └── input_data.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | data/ 3 | checkpoints/ 4 | __pycache__/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # unsupervised-image-segmentation 2 | Unsupervised Image Segmentation using WNet 3 | -------------------------------------------------------------------------------- /autoencoder.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | import tensorflow as tf 4 | from os.path import exists 5 | from encoder import encode 6 | from decoder import decode 7 | from input_data import input_data 8 | 9 | logdir = "checkpoints/model.ckpt" 10 | 11 | # network parameters 12 | learning_rate = 0.0001 13 | num_steps = 1000 14 | display_step = 1 15 | 16 | global_step = 0 17 | 18 | X = tf.placeholder(tf.float32, [None, 224, 224, 3]) 19 | 20 | with tf.name_scope("Encoding"): 21 | encoded_image = encode(X) 22 | 23 | with tf.name_scope("Decoding"): 24 | decoded_image = decode(encoded_image) 25 | 26 | with tf.name_scope("Loss"): 27 | y_pred = tf.reshape(decoded_image, [-1, 150528]) 28 | y_true = tf.reshape(X, [-1, 150528]) 29 | loss = tf.reduce_mean(tf.pow(y_pred - y_true, 2)) 30 | tf.summary.scalar("SEE loss", loss) 31 | 32 | with tf.name_scope("Optimization"): 33 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 34 | train_op = optimizer.minimize(loss=loss) 35 | 36 | merged_summary = tf.summary.merge_all() 37 | 38 | init = tf.global_variables_initializer() 39 | 40 | saver = tf.train.Saver() 41 | 42 | with tf.Session() as sess: 43 | sess.run(init) 44 | train_writer = tf.summary.FileWriter(logdir, graph=tf.get_default_graph()) 45 | 46 | # if exists(logdir): 47 | # saver.restore(sess, logdir) 48 | iterator = input_data() 49 | 50 | for i in range(num_steps + 1): 51 | next_items = iterator.get_next() 52 | batch_x = sess.run(next_items) 53 | _ = sess.run(train_op, feed_dict={X: batch_x}) 54 | 55 | if i % display_step == 0: 56 | loss_, summary = sess.run([loss, merged_summary], feed_dict={X: batch_x}) 57 | print("Iteration number: ", str(i), " Loss: ", str(loss_)) 58 | train_writer.add_summary(summary) 59 | saver.save(sess, logdir, global_step=global_step + i) 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /decoder.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | num_classes = 20 4 | 5 | variance_epsilon = 0.0001 6 | 7 | def decode(X): 8 | with tf.name_scope("rectangle10"): 9 | with tf.name_scope("input_layer"): 10 | input_layer = tf.reshape(X, [-1, 224, 224, num_classes]) 11 | with tf.name_scope("conv1"): 12 | rect1_conv1 = tf.layers.conv2d( 13 | input_layer, 14 | kernel_size=[3, 3], 15 | filters=64, 16 | strides=1, 17 | padding="same", 18 | activation=tf.nn.relu 19 | ) 20 | with tf.name_scope("batch_normalization1"): 21 | mean, variance = tf.nn.moments(rect1_conv1, [0, 1, 2]) 22 | rect1_normalized_conv1 = tf.nn.batch_normalization(rect1_conv1, mean, variance, None, None, variance_epsilon) 23 | with tf.name_scope("conv2"): 24 | rect1_conv2 = tf.layers.conv2d( 25 | rect1_normalized_conv1, 26 | kernel_size=[3, 3], 27 | strides=1, 28 | filters=64, 29 | padding="same", 30 | activation=tf.nn.relu 31 | ) 32 | with tf.name_scope("batch_normalization2"): 33 | mean, variance = tf.nn.moments(rect1_conv2, [0, 1, 2]) 34 | rect1_normalized_conv2 = tf.nn.batch_normalization(rect1_conv2, mean, variance, None, None, variance_epsilon) 35 | 36 | with tf.name_scope("rectangle11"): 37 | with tf.name_scope("down_sampling"): 38 | rect2_max_pool = tf.layers.max_pooling2d( 39 | rect1_normalized_conv2, 40 | pool_size=[2, 2], 41 | strides=2, 42 | padding="valid" 43 | ) 44 | with tf.name_scope("conv1"): 45 | rect2_conv1 = tf.layers.conv2d( 46 | rect2_max_pool, 47 | kernel_size=[3, 3], 48 | filters=128, 49 | strides=1, 50 | padding="same", 51 | activation=tf.nn.relu 52 | ) 53 | with tf.name_scope("batch_normalization1"): 54 | mean, variance = tf.nn.moments(rect2_conv1, [0, 1, 2]) 55 | rect2_normalized_conv1 = tf.nn.batch_normalization(rect2_conv1, mean, variance, None, None, variance_epsilon) 56 | with tf.name_scope("conv2"): 57 | rect2_conv2 = tf.layers.conv2d( 58 | rect2_normalized_conv1, 59 | kernel_size=[3, 3], 60 | filters=128, 61 | strides=1, 62 | padding="same", 63 | activation=tf.nn.relu 64 | ) 65 | with tf.name_scope("batch_normalization2"): 66 | mean, variance = tf.nn.moments(rect2_conv2, [0, 1, 2]) 67 | rect2_normalized_conv2 = tf.nn.batch_normalization(rect2_conv2, mean, variance, None, None, variance_epsilon) 68 | 69 | with tf.name_scope("rectangle12"): 70 | with tf.name_scope("down_sampling"): 71 | rect3_max_pool = tf.layers.max_pooling2d( 72 | rect2_normalized_conv2, 73 | pool_size=[2, 2], 74 | strides=2, 75 | padding="valid" 76 | ) 77 | with tf.name_scope("conv1"): 78 | rect3_conv1 = tf.layers.conv2d( 79 | rect3_max_pool, 80 | kernel_size=[3, 3], 81 | filters=256, 82 | strides=1, 83 | padding="same", 84 | activation=tf.nn.relu 85 | ) 86 | with tf.name_scope("batch_normalization1"): 87 | mean, variance = tf.nn.moments(rect3_conv1, [0, 1, 2]) 88 | rect3_normalized_conv1 = tf.nn.batch_normalization(rect3_conv1, mean, variance, None, None, variance_epsilon) 89 | with tf.name_scope("conv2"): 90 | rect3_conv2 = tf.layers.conv2d( 91 | rect3_normalized_conv1, 92 | kernel_size=[3, 3], 93 | filters=256, 94 | strides=1, 95 | padding="same", 96 | activation=tf.nn.relu 97 | ) 98 | with tf.name_scope("batch_normalization2"): 99 | mean, variance = tf.nn.moments(rect3_conv2, [0, 1, 2]) 100 | rect3_normalized_conv2 = tf.nn.batch_normalization(rect3_conv2, mean, variance, None, None, variance_epsilon) 101 | 102 | with tf.name_scope("rectangle13"): 103 | with tf.name_scope("down_sampling"): 104 | rect4_max_pool = tf.layers.max_pooling2d( 105 | rect3_normalized_conv2, 106 | pool_size=[2, 2], 107 | strides=2, 108 | padding="valid" 109 | ) 110 | with tf.name_scope("conv1"): 111 | rect4_conv1 = tf.layers.conv2d( 112 | rect4_max_pool, 113 | kernel_size=[3, 3], 114 | filters=512, 115 | strides=1, 116 | padding="same", 117 | activation=tf.nn.relu 118 | ) 119 | with tf.name_scope("batch_normalization1"): 120 | mean, variance = tf.nn.moments(rect4_conv1, [0, 1, 2]) 121 | rect4_normalized_conv1 = tf.nn.batch_normalization(rect4_conv1, mean, variance, None, None, variance_epsilon) 122 | with tf.name_scope("conv2"): 123 | rect4_conv2 = tf.layers.conv2d( 124 | rect4_normalized_conv1, 125 | kernel_size=[3, 3], 126 | filters=512, 127 | strides=1, 128 | padding="same", 129 | activation=tf.nn.relu 130 | ) 131 | with tf.name_scope("batch_normalization2"): 132 | mean, variance = tf.nn.moments(rect4_conv2, [0, 1, 2]) 133 | rect4_normalized_conv2 = tf.nn.batch_normalization(rect4_conv2, mean, variance, None, None, variance_epsilon) 134 | 135 | with tf.name_scope("rectangle14"): 136 | with tf.name_scope("down_sampling"): 137 | rect5_max_pool = tf.layers.max_pooling2d( 138 | rect4_normalized_conv2, 139 | pool_size=[2, 2], 140 | strides=2, 141 | padding="valid" 142 | ) 143 | with tf.name_scope("conv1"): 144 | rect5_conv1 = tf.layers.conv2d( 145 | rect5_max_pool, 146 | kernel_size=[3, 3], 147 | filters=1024, 148 | strides=1, 149 | padding="same", 150 | activation=tf.nn.relu 151 | ) 152 | with tf.name_scope("batch_normalization1"): 153 | mean, variance = tf.nn.moments(rect5_conv1, [0, 1, 2]) 154 | rect5_normalized_conv1 = tf.nn.batch_normalization(rect5_conv1, mean, variance, None, None, variance_epsilon) 155 | with tf.name_scope("conv2"): 156 | rect5_conv2 = tf.layers.conv2d( 157 | rect5_normalized_conv1, 158 | kernel_size=[3, 3], 159 | filters=1024, 160 | strides=1, 161 | padding="same", 162 | activation=tf.nn.relu 163 | ) 164 | with tf.name_scope("batch_normalization2"): 165 | mean, variance = tf.nn.moments(rect5_conv2, [0, 1, 2]) 166 | rect5_normalized_conv2 = tf.nn.batch_normalization(rect5_conv2, mean, variance, None, None, variance_epsilon) 167 | 168 | with tf.name_scope("rectangle15"): 169 | with tf.name_scope("upsampling"): 170 | rect6_transpose_conv = tf.layers.conv2d_transpose( 171 | rect5_normalized_conv2, 172 | filters=512, 173 | kernel_size=[2, 2], 174 | strides=2, 175 | padding="valid" 176 | ) 177 | rect6_conv1_input = tf.stack([rect4_normalized_conv2, rect6_transpose_conv]) 178 | rect6_conv1_input = tf.reshape(rect6_conv1_input, [-1, 28, 28, 1024]) 179 | with tf.name_scope("conv1"): 180 | rect6_conv1 = tf.layers.conv2d( 181 | rect6_conv1_input, 182 | filters=512, 183 | kernel_size=[3, 3], 184 | strides=1, 185 | padding="same", 186 | activation=tf.nn.relu 187 | ) 188 | with tf.name_scope("batch_normalization1"): 189 | mean, variance = tf.nn.moments(rect6_conv1, [0, 1, 2]) 190 | rect6_normalized_conv1 = tf.nn.batch_normalization(rect6_conv1, mean, variance, None, None, variance_epsilon) 191 | with tf.name_scope("conv2"): 192 | rect6_conv2 = tf.layers.conv2d( 193 | rect6_normalized_conv1, 194 | filters=512, 195 | kernel_size=[3, 3], 196 | strides=1, 197 | padding="same", 198 | activation=tf.nn.relu 199 | ) 200 | with tf.name_scope("batch_normalization2"): 201 | mean, variance = tf.nn.moments(rect6_conv2, [0, 1, 2]) 202 | rect6_normalized_conv2 = tf.nn.batch_normalization(rect6_conv2, mean, variance, None, None, variance_epsilon) 203 | 204 | with tf.name_scope("rectangle16"): 205 | with tf.name_scope("upsampling"): 206 | rect7_transpose_conv = tf.layers.conv2d_transpose( 207 | rect6_normalized_conv2, 208 | filters=256, 209 | kernel_size=[2, 2], 210 | strides=2, 211 | padding="valid" 212 | ) 213 | rect7_conv1_input = tf.stack([rect3_normalized_conv2, rect7_transpose_conv]) 214 | rect7_conv1_input = tf.reshape(rect7_conv1_input, [-1, 56, 56, 512]) 215 | with tf.name_scope("conv1"): 216 | rect7_conv1 = tf.layers.conv2d( 217 | rect7_conv1_input, 218 | filters=256, 219 | kernel_size=[3, 3], 220 | strides=1, 221 | padding="same", 222 | activation=tf.nn.relu 223 | ) 224 | with tf.name_scope("batch_normalization1"): 225 | mean, variance = tf.nn.moments(rect7_conv1, [0, 1, 2]) 226 | rect7_normalized_conv1 = tf.nn.batch_normalization(rect7_conv1, mean, variance, None, None, variance_epsilon) 227 | with tf.name_scope("conv2"): 228 | rect7_conv2 = tf.layers.conv2d( 229 | rect7_normalized_conv1, 230 | filters=256, 231 | kernel_size=[3, 3], 232 | strides=1, 233 | padding="same", 234 | activation=tf.nn.relu 235 | ) 236 | with tf.name_scope("batch_normalization2"): 237 | mean, variance = tf.nn.moments(rect7_conv2, [0, 1, 2]) 238 | rect7_normalized_conv2 = tf.nn.batch_normalization(rect7_conv2, mean, variance, None, None, variance_epsilon) 239 | 240 | with tf.name_scope("rectangle17"): 241 | with tf.name_scope("upsampling"): 242 | rect8_transpose_conv = tf.layers.conv2d_transpose( 243 | rect7_normalized_conv2, 244 | filters=128, 245 | kernel_size=[2, 2], 246 | strides=2, 247 | padding="valid" 248 | ) 249 | rect8_conv1_input = tf.stack([rect2_normalized_conv2, rect8_transpose_conv]) 250 | rect8_conv1_input = tf.reshape(rect8_conv1_input, [-1, 112, 112, 256]) 251 | with tf.name_scope("conv1"): 252 | rect8_conv1 = tf.layers.conv2d( 253 | rect8_conv1_input, 254 | filters=128, 255 | kernel_size=[3, 3], 256 | strides=1, 257 | padding="same", 258 | activation=tf.nn.relu 259 | ) 260 | with tf.name_scope("batch_normalization1"): 261 | mean, variance = tf.nn.moments(rect8_conv1, [0, 1, 2]) 262 | rect8_normalized_conv1 = tf.nn.batch_normalization(rect8_conv1, mean, variance, None, None, variance_epsilon) 263 | with tf.name_scope("conv2"): 264 | rect8_conv2 = tf.layers.conv2d( 265 | rect8_normalized_conv1, 266 | filters=128, 267 | kernel_size=[3, 3], 268 | strides=1, 269 | padding="same", 270 | activation=tf.nn.relu 271 | ) 272 | with tf.name_scope("batch_normalization2"): 273 | mean, variance = tf.nn.moments(rect8_conv2, [0, 1, 2]) 274 | rect8_normalized_conv2 = tf.nn.batch_normalization(rect8_conv2, mean, variance, None, None, variance_epsilon) 275 | 276 | with tf.name_scope("rectangle18"): 277 | with tf.name_scope("upsampling"): 278 | rect9_transpose_conv = tf.layers.conv2d_transpose( 279 | rect8_normalized_conv2, 280 | filters=64, 281 | kernel_size=[2, 2], 282 | strides=2, 283 | padding="valid" 284 | ) 285 | rect9_conv1_input = tf.stack([rect1_normalized_conv2, rect9_transpose_conv]) 286 | rect9_conv1_input = tf.reshape(rect9_conv1_input, [-1, 224, 224, 128]) 287 | with tf.name_scope("conv1"): 288 | rect9_conv1 = tf.layers.conv2d( 289 | rect9_conv1_input, 290 | filters=64, 291 | kernel_size=[3, 3], 292 | strides=1, 293 | padding="same", 294 | activation=tf.nn.relu 295 | ) 296 | with tf.name_scope("batch_normalization1"): 297 | mean, variance = tf.nn.moments(rect9_conv1, [0, 1, 2]) 298 | rect9_normalized_conv1 = tf.nn.batch_normalization(rect9_conv1, mean, variance, None, None, variance_epsilon) 299 | with tf.name_scope("conv2"): 300 | rect9_conv2 = tf.layers.conv2d( 301 | rect9_normalized_conv1, 302 | filters=64, 303 | kernel_size=[3, 3], 304 | strides=1, 305 | padding="same", 306 | activation=tf.nn.relu 307 | ) 308 | with tf.name_scope("batch_normalization2"): 309 | mean, variance = tf.nn.moments(rect9_conv2, [0, 1, 2]) 310 | rect9_normalized_conv2 = tf.nn.batch_normalization(rect9_conv2, mean, variance, None, None, variance_epsilon) 311 | with tf.name_scope("reconstruction"): 312 | output = tf.layers.conv2d( 313 | rect9_normalized_conv2, 314 | filters=3, 315 | kernel_size=[1, 1], 316 | strides=1, 317 | padding="same", 318 | activation=tf.nn.relu 319 | ) 320 | 321 | return output -------------------------------------------------------------------------------- /encoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | 4 | num_classes = 20 5 | variance_epsilon = 0.0001 6 | 7 | def encode(X): 8 | 9 | with tf.name_scope("rectangle1"): 10 | with tf.name_scope("input_layer"): 11 | input_layer = tf.reshape(X, [-1, 224, 224, 3]) 12 | with tf.name_scope("conv1"): 13 | rect1_conv1 = tf.layers.conv2d( 14 | input_layer, 15 | kernel_size=[3, 3], 16 | filters=64, 17 | strides=1, 18 | padding="same", 19 | activation=tf.nn.relu 20 | ) 21 | with tf.name_scope("batch_normalization1"): 22 | mean, variance = tf.nn.moments(rect1_conv1, [0, 1, 2]) 23 | rect1_normalized_conv1 = tf.nn.batch_normalization(rect1_conv1, mean, variance, None, None, variance_epsilon) 24 | with tf.name_scope("conv2"): 25 | rect1_conv2 = tf.layers.conv2d( 26 | rect1_normalized_conv1, 27 | kernel_size=[3, 3], 28 | strides=1, 29 | filters=64, 30 | padding="same", 31 | activation=tf.nn.relu 32 | ) 33 | with tf.name_scope("batch_normalization2"): 34 | mean, variance = tf.nn.moments(rect1_conv2, [0, 1, 2]) 35 | rect1_normalized_conv2 = tf.nn.batch_normalization(rect1_conv2, mean, variance, None, None, variance_epsilon) 36 | 37 | with tf.name_scope("rectangle2"): 38 | with tf.name_scope("down_sampling"): 39 | rect2_max_pool = tf.layers.max_pooling2d( 40 | rect1_normalized_conv2, 41 | pool_size=[2, 2], 42 | strides=2, 43 | padding="valid" 44 | ) 45 | with tf.name_scope("conv1"): 46 | rect2_conv1 = tf.layers.conv2d( 47 | rect2_max_pool, 48 | kernel_size=[3, 3], 49 | filters=128, 50 | strides=1, 51 | padding="same", 52 | activation=tf.nn.relu 53 | ) 54 | with tf.name_scope("batch_normalization1"): 55 | mean, variance = tf.nn.moments(rect2_conv1, [0, 1, 2]) 56 | rect2_normalized_conv1 = tf.nn.batch_normalization(rect2_conv1, mean, variance, None, None, variance_epsilon) 57 | with tf.name_scope("conv2"): 58 | rect2_conv2 = tf.layers.conv2d( 59 | rect2_normalized_conv1, 60 | kernel_size=[3, 3], 61 | filters=128, 62 | strides=1, 63 | padding="same", 64 | activation=tf.nn.relu 65 | ) 66 | with tf.name_scope("batch_normalization2"): 67 | mean, variance = tf.nn.moments(rect2_conv2, [0, 1, 2]) 68 | rect2_normalized_conv2 = tf.nn.batch_normalization(rect2_conv2, mean, variance, None, None, variance_epsilon) 69 | 70 | with tf.name_scope("rectangle3"): 71 | with tf.name_scope("down_sampling"): 72 | rect3_max_pool = tf.layers.max_pooling2d( 73 | rect2_normalized_conv2, 74 | pool_size=[2, 2], 75 | strides=2, 76 | padding="valid" 77 | ) 78 | with tf.name_scope("conv1"): 79 | rect3_conv1 = tf.layers.conv2d( 80 | rect3_max_pool, 81 | kernel_size=[3, 3], 82 | filters=256, 83 | strides=1, 84 | padding="same", 85 | activation=tf.nn.relu 86 | ) 87 | with tf.name_scope("batch_normalization1"): 88 | mean, variance = tf.nn.moments(rect3_conv1, [0, 1, 2]) 89 | rect3_normalized_conv1 = tf.nn.batch_normalization(rect3_conv1, mean, variance, None, None, variance_epsilon) 90 | with tf.name_scope("conv2"): 91 | rect3_conv2 = tf.layers.conv2d( 92 | rect3_normalized_conv1, 93 | kernel_size=[3, 3], 94 | filters=256, 95 | strides=1, 96 | padding="same", 97 | activation=tf.nn.relu 98 | ) 99 | with tf.name_scope("batch_normalization2"): 100 | mean, variance = tf.nn.moments(rect3_conv2, [0, 1, 2]) 101 | rect3_normalized_conv2 = tf.nn.batch_normalization(rect3_conv2, mean, variance, None, None, variance_epsilon) 102 | 103 | with tf.name_scope("rectangle4"): 104 | with tf.name_scope("down_sampling"): 105 | rect4_max_pool = tf.layers.max_pooling2d( 106 | rect3_normalized_conv2, 107 | pool_size=[2, 2], 108 | strides=2, 109 | padding="valid" 110 | ) 111 | with tf.name_scope("conv1"): 112 | rect4_conv1 = tf.layers.conv2d( 113 | rect4_max_pool, 114 | kernel_size=[3, 3], 115 | filters=512, 116 | strides=1, 117 | padding="same", 118 | activation=tf.nn.relu 119 | ) 120 | with tf.name_scope("batch_normalization1"): 121 | mean, variance = tf.nn.moments(rect4_conv1, [0, 1, 2]) 122 | rect4_normalized_conv1 = tf.nn.batch_normalization(rect4_conv1, mean, variance, None, None, variance_epsilon) 123 | with tf.name_scope("conv2"): 124 | rect4_conv2 = tf.layers.conv2d( 125 | rect4_normalized_conv1, 126 | kernel_size=[3, 3], 127 | filters=512, 128 | strides=1, 129 | padding="same", 130 | activation=tf.nn.relu 131 | ) 132 | with tf.name_scope("batch_normalization2"): 133 | mean, variance = tf.nn.moments(rect4_conv2, [0, 1, 2]) 134 | rect4_normalized_conv2 = tf.nn.batch_normalization(rect4_conv2, mean, variance, None, None, variance_epsilon) 135 | 136 | with tf.name_scope("rectangle5"): 137 | with tf.name_scope("down_sampling"): 138 | rect5_max_pool = tf.layers.max_pooling2d( 139 | rect4_normalized_conv2, 140 | pool_size=[2, 2], 141 | strides=2, 142 | padding="valid" 143 | ) 144 | with tf.name_scope("conv1"): 145 | rect5_conv1 = tf.layers.conv2d( 146 | rect5_max_pool, 147 | kernel_size=[3, 3], 148 | filters=1024, 149 | strides=1, 150 | padding="same", 151 | activation=tf.nn.relu 152 | ) 153 | with tf.name_scope("batch_normalization1"): 154 | mean, variance = tf.nn.moments(rect5_conv1, [0, 1, 2]) 155 | rect5_normalized_conv1 = tf.nn.batch_normalization(rect5_conv1, mean, variance, None, None, variance_epsilon) 156 | with tf.name_scope("conv2"): 157 | rect5_conv2 = tf.layers.conv2d( 158 | rect5_normalized_conv1, 159 | kernel_size=[3, 3], 160 | filters=1024, 161 | strides=1, 162 | padding="same", 163 | activation=tf.nn.relu 164 | ) 165 | with tf.name_scope("batch_normalization2"): 166 | mean, variance = tf.nn.moments(rect5_conv2, [0, 1, 2]) 167 | rect5_normalized_conv2 = tf.nn.batch_normalization(rect5_conv2, mean, variance, None, None, variance_epsilon) 168 | 169 | with tf.name_scope("rectangle6"): 170 | with tf.name_scope("upsampling"): 171 | rect6_transpose_conv = tf.layers.conv2d_transpose( 172 | rect5_normalized_conv2, 173 | filters=512, 174 | kernel_size=[2, 2], 175 | strides=2, 176 | padding="valid" 177 | ) 178 | rect6_conv1_input = tf.stack([rect4_normalized_conv2, rect6_transpose_conv]) 179 | rect6_conv1_input = tf.reshape(rect6_conv1_input, [-1, 28, 28, 1024]) 180 | with tf.name_scope("conv1"): 181 | rect6_conv1 = tf.layers.conv2d( 182 | rect6_conv1_input, 183 | filters=512, 184 | kernel_size=[3, 3], 185 | strides=1, 186 | padding="same", 187 | activation=tf.nn.relu 188 | ) 189 | with tf.name_scope("batch_normalization1"): 190 | mean, variance = tf.nn.moments(rect6_conv1, [0, 1, 2]) 191 | rect6_normalized_conv1 = tf.nn.batch_normalization(rect6_conv1, mean, variance, None, None, variance_epsilon) 192 | with tf.name_scope("conv2"): 193 | rect6_conv2 = tf.layers.conv2d( 194 | rect6_normalized_conv1, 195 | filters=512, 196 | kernel_size=[3, 3], 197 | strides=1, 198 | padding="same", 199 | activation=tf.nn.relu 200 | ) 201 | with tf.name_scope("batch_normalization2"): 202 | mean, variance = tf.nn.moments(rect6_conv2, [0, 1, 2]) 203 | rect6_normalized_conv2 = tf.nn.batch_normalization(rect6_conv2, mean, variance, None, None, variance_epsilon) 204 | 205 | with tf.name_scope("rectangle7"): 206 | with tf.name_scope("upsampling"): 207 | rect7_transpose_conv = tf.layers.conv2d_transpose( 208 | rect6_normalized_conv2, 209 | filters=256, 210 | kernel_size=[2, 2], 211 | strides=2, 212 | padding="valid" 213 | ) 214 | rect7_conv1_input = tf.stack([rect3_normalized_conv2, rect7_transpose_conv]) 215 | rect7_conv1_input = tf.reshape(rect7_conv1_input, [-1, 56, 56, 512]) 216 | with tf.name_scope("conv1"): 217 | rect7_conv1 = tf.layers.conv2d( 218 | rect7_conv1_input, 219 | filters=256, 220 | kernel_size=[3, 3], 221 | strides=1, 222 | padding="same", 223 | activation=tf.nn.relu 224 | ) 225 | with tf.name_scope("batch_normalization1"): 226 | mean, variance = tf.nn.moments(rect7_conv1, [0, 1, 2]) 227 | rect7_normalized_conv1 = tf.nn.batch_normalization(rect7_conv1, mean, variance, None, None, variance_epsilon) 228 | with tf.name_scope("conv2"): 229 | rect7_conv2 = tf.layers.conv2d( 230 | rect7_normalized_conv1, 231 | filters=256, 232 | kernel_size=[3, 3], 233 | strides=1, 234 | padding="same", 235 | activation=tf.nn.relu 236 | ) 237 | with tf.name_scope("batch_normalization2"): 238 | mean, variance = tf.nn.moments(rect7_conv2, [0, 1, 2]) 239 | rect7_normalized_conv2 = tf.nn.batch_normalization(rect7_conv2, mean, variance, None, None, variance_epsilon) 240 | 241 | with tf.name_scope("rectangle8"): 242 | with tf.name_scope("upsampling"): 243 | rect8_transpose_conv = tf.layers.conv2d_transpose( 244 | rect7_normalized_conv2, 245 | filters=128, 246 | kernel_size=[2, 2], 247 | strides=2, 248 | padding="valid" 249 | ) 250 | rect8_conv1_input = tf.stack([rect2_normalized_conv2, rect8_transpose_conv]) 251 | rect8_conv1_input = tf.reshape(rect8_conv1_input, [-1, 112, 112, 256]) 252 | with tf.name_scope("conv1"): 253 | rect8_conv1 = tf.layers.conv2d( 254 | rect8_conv1_input, 255 | filters=128, 256 | kernel_size=[3, 3], 257 | strides=1, 258 | padding="same", 259 | activation=tf.nn.relu 260 | ) 261 | with tf.name_scope("batch_normalization1"): 262 | mean, variance = tf.nn.moments(rect8_conv1, [0, 1, 2]) 263 | rect8_normalized_conv1 = tf.nn.batch_normalization(rect8_conv1, mean, variance, None, None, variance_epsilon) 264 | with tf.name_scope("conv2"): 265 | rect8_conv2 = tf.layers.conv2d( 266 | rect8_normalized_conv1, 267 | filters=128, 268 | kernel_size=[3, 3], 269 | strides=1, 270 | padding="same", 271 | activation=tf.nn.relu 272 | ) 273 | with tf.name_scope("batch_normalization2"): 274 | mean, variance = tf.nn.moments(rect8_conv2, [0, 1, 2]) 275 | rect8_normalized_conv2 = tf.nn.batch_normalization(rect8_conv2, mean, variance, None, None, variance_epsilon) 276 | 277 | with tf.name_scope("rectangle9"): 278 | with tf.name_scope("upsampling"): 279 | rect9_transpose_conv = tf.layers.conv2d_transpose( 280 | rect8_normalized_conv2, 281 | filters=64, 282 | kernel_size=[2, 2], 283 | strides=2, 284 | padding="valid" 285 | ) 286 | rect9_conv1_input = tf.stack([rect1_normalized_conv2, rect9_transpose_conv]) 287 | rect9_conv1_input = tf.reshape(rect9_conv1_input, [-1, 224, 224, 128]) 288 | with tf.name_scope("conv1"): 289 | rect9_conv1 = tf.layers.conv2d( 290 | rect9_conv1_input, 291 | filters=64, 292 | kernel_size=[3, 3], 293 | strides=1, 294 | padding="same", 295 | activation=tf.nn.relu 296 | ) 297 | with tf.name_scope("batch_normalization1"): 298 | mean, variance = tf.nn.moments(rect9_conv1, [0, 1, 2]) 299 | rect9_normalized_conv1 = tf.nn.batch_normalization(rect9_conv1, mean, variance, None, None, variance_epsilon) 300 | with tf.name_scope("conv2"): 301 | rect9_conv2 = tf.layers.conv2d( 302 | rect9_normalized_conv1, 303 | filters=64, 304 | kernel_size=[3, 3], 305 | strides=1, 306 | padding="same", 307 | activation=tf.nn.relu 308 | ) 309 | with tf.name_scope("logits"): 310 | logits = tf.layers.conv2d( 311 | rect9_conv2, 312 | filters=num_classes, 313 | kernel_size=[1, 1], 314 | strides=1, 315 | padding="same", 316 | activation=tf.nn.relu 317 | ) 318 | return logits -------------------------------------------------------------------------------- /input_data.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from os import listdir 3 | import numpy as np 4 | 5 | file_path = "data/VOC2012/SegmentationClass/" 6 | batch_size = 1 7 | 8 | def parse_image(filename): 9 | image_string = tf.read_file(file_path + filename) 10 | image_decoded = tf.image.decode_jpeg(image_string, channels=3) 11 | image_resized = tf.image.resize_images(image_decoded, [224, 224]) 12 | return image_resized 13 | 14 | def get_filenames(): 15 | filenames = listdir(file_path) 16 | return filenames 17 | 18 | def input_data(): 19 | filenames = get_filenames() 20 | print(len(filenames)) 21 | train_dataset = tf.data.Dataset.from_tensor_slices(filenames) 22 | train_dataset = train_dataset.shuffle(100).repeat() 23 | train_dataset = train_dataset.map(parse_image).batch(batch_size) 24 | return train_dataset.make_one_shot_iterator() 25 | 26 | if __name__ == '__main__': 27 | iterator = input_data() 28 | images = iterator.get_next() 29 | print(images) 30 | 31 | --------------------------------------------------------------------------------