-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_aerial_images.py
577 lines (487 loc) · 23 KB
/
tf_aerial_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# -*- coding: utf-8 -*-
"""
Baseline for machine learning project on road segmentation.
This simple baseline consits of a CNN with two convolutional+pooling layers with a soft-max loss
Credits: Aurelien Lucchi, ETH Zürich
"""
import gzip
import os
import sys
import urllib
import matplotlib.image as mpimg
from PIL import Image
import code
import tensorflow.python.platform
import numpy as np
import tensorflow as tf
import itertools
NUM_CHANNELS = 3 # RGB images
PIXEL_DEPTH = 255
NUM_LABELS = 2
TRAINING_SIZE = 64
TESTING_SIZE = 50
VALIDATION_SIZE = 36# Size of the validation set.
SEED = None #66478 # Set to None for random seed.
BATCH_SIZE = 32 # 64
NUM_EPOCHS = 20
RESTORE_MODEL = False # If True, restore existing model instead of training a new one
RECORDING_STEP = 1000
FILTER_SIZE = 8
NB_NEUR1 = 32
NB_NEUR2 = 64
# Set image patch size in pixels
# IMG_PATCH_SIZE should be a multiple of 4
# image size should be an integer multiple of this number!
IMG_PATCH_SIZE = 16
NB_NEUR3 = IMG_PATCH_SIZE**2*2
tf.app.flags.DEFINE_string('train_dir', '/tmp/mnist',
"""Directory where to write event logs """
"""and checkpoint.""")
FLAGS = tf.app.flags.FLAGS
# Extract patches from a given image
def img_crop(im, w, h):
list_patches = []
imgwidth = im.shape[0]
imgheight = im.shape[1]
is_2d = len(im.shape) < 3
for i in range(0,imgheight,h):
for j in range(0,imgwidth,w):
if is_2d:
im_patch = im[j:j+w, i:i+h]
else:
im_patch = im[j:j+w, i:i+h, :]
list_patches.append(im_patch)
return list_patches
def img_full_crop(im,w,h):
list_patches = []
imgwidth, imgheight = im.shape[0:2]
is_2d = len(im.shape) < 3
for i,j in itertools.product(range(imgheight-h),range(imgweight-w)):
if is_2d:
im_patch = im[j:j+w,i:i+h]
else:
im_patch = im[j:j+w,i:i+h,:]
list_patches.append(im_patch)
return list_patches
def extract_data(filename, num_images, full=False):
"""Extract the images into a 4D tensor [image index, y, x, channels].
Values are rescaled from [0, 255] down to [-0.5, 0.5].
"""
imgs = []
for i in range(1, num_images+1):
imageid = "satImage_%.3d" % i
image_filename = filename + imageid + ".png"
if os.path.isfile(image_filename):
print ('Loading ' + image_filename)
img = mpimg.imread(image_filename)
imgs.append(img)
else:
print ('File ' + image_filename + ' does not exist')
num_images = len(imgs)
IMG_WIDTH = imgs[0].shape[0]
IMG_HEIGHT = imgs[0].shape[1]
N_PATCHES_PER_IMAGE = (IMG_WIDTH/IMG_PATCH_SIZE)*(IMG_HEIGHT/IMG_PATCH_SIZE)
if(full):
img_patches = [img_full_crop(imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)]
else:
img_patches = [img_crop(imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)]
data = [img_patches[i][j] for i in range(len(img_patches)) for j in range(len(img_patches[i]))]
return np.asarray(data)#-0.5
# Assign a label to a patch v
def value_to_class(v):
foreground_threshold = 0.25#-0.25 # percentage of pixels > 1 required to assign a foreground label to a patch
df = np.sum(v)
if df > foreground_threshold:
return [0, 1]
else:
return [1, 0]
def value_to_class_full(v):
value=v[int(v.shape[0]/2),int(v.shape[1])]
return [value, ~value+2]
# Extract label images
def extract_labels(filename, num_images, full=False):
"""Extract the labels into a 1-hot matrix [image index, label index]."""
gt_imgs = []
for i in range(1, num_images+1):
imageid = "satImage_%.3d" % i
image_filename = filename + imageid + ".png"
if os.path.isfile(image_filename):
print ('Loading ' + image_filename)
img = mpimg.imread(image_filename)
gt_imgs.append(img)
else:
print ('File ' + image_filename + ' does not exist')
num_images = len(gt_imgs)
if(full):
gt_patches = [img_crop_full(gt_imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)]
data = np.asarray([gt_patches[i][j] for i in range(len(gt_patches)) for j in range(len(gt_patches[i]))])
labels = np.asarray([value_to_class_full(data[i]) for i in range(len(data))])
else:
gt_patches = [img_crop(gt_imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)]
data = np.asarray([gt_patches[i][j] for i in range(len(gt_patches)) for j in range(len(gt_patches[i]))])
labels = np.asarray([value_to_class(np.mean(data[i])) for i in range(len(data))])
# Convert to dense 1-hot representation.
return labels.astype(np.float32)
def error_rate(predictions, labels):
"""Return the error rate based on dense predictions and 1-hot labels."""
return 100.0 - (
100.0 *
np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) /
predictions.shape[0])
# Write predictions from neural network to a file
def write_predictions_to_file(predictions, labels, filename):
max_labels = np.argmax(labels, 1)
max_predictions = np.argmax(predictions, 1)
file = open(filename, "w")
n = predictions.shape[0]
for i in range(0, n):
file.write(max_labels(i) + ' ' + max_predictions(i))
file.close()
# Print predictions from neural network
def print_predictions(predictions, labels):
max_labels = np.argmax(labels, 1)
max_predictions = np.argmax(predictions, 1)
print (str(max_labels) + ' ' + str(max_predictions))
# Convert array of labels to an image
def label_to_img(imgwidth, imgheight, w, h, labels):
array_labels = np.zeros([imgwidth, imgheight])
idx = 0
for i in range(0,imgheight,h):
for j in range(0,imgwidth,w):
if labels[idx][0] > 0.5:
l = 1
else:
l = 0
array_labels[j:j+w, i:i+h] = l
idx = idx + 1
return array_labels
def img_float_to_uint8(img):
rimg = img - np.min(img)
rimg = (rimg / np.max(rimg) * PIXEL_DEPTH).round().astype(np.uint8)
return rimg
def concatenate_images(img, gt_img):
nChannels = len(gt_img.shape)
w = gt_img.shape[0]
h = gt_img.shape[1]
if nChannels == 3:
cimg = np.concatenate((img, gt_img), axis=1)
else:
gt_img_3c = np.zeros((w, h, 3), dtype=np.uint8)
gt_img8 = img_float_to_uint8(gt_img)
gt_img_3c[:,:,0] = gt_img8
gt_img_3c[:,:,1] = gt_img8
gt_img_3c[:,:,2] = gt_img8
img8 = img_float_to_uint8(img)
cimg = np.concatenate((img8, gt_img_3c), axis=1)
return cimg
def make_img_overlay(img, predicted_img):
w = img.shape[0]
h = img.shape[1]
color_mask = np.zeros((w, h, 3), dtype=np.uint8)
color_mask[:,:,0] = predicted_img*PIXEL_DEPTH
img8 = img_float_to_uint8(img)
background = Image.fromarray(img8, 'RGB').convert("RGBA")
overlay = Image.fromarray(color_mask, 'RGB').convert("RGBA")
new_img = Image.blend(background, overlay, 0.2)
return new_img
def main(argv=None): # pylint: disable=unused-argument
data_dir = 'dataset/training/'
train_data_filename = data_dir + 'images/'
train_labels_filename = data_dir + 'groundtruth/'
test_data_filename='dataset/test_set_images/test_'
# Extract it into np arrays.
train_data = extract_data(train_data_filename, TRAINING_SIZE+VALIDATION_SIZE)
train_labels = extract_labels(train_labels_filename, TRAINING_SIZE+VALIDATION_SIZE)
#print(train_data.shape)
#print(np.where(train_data<0))
#sys.exit()
valid_data = train_data[TRAINING_SIZE:,:,:,:]
train_data = train_data[:-VALIDATION_SIZE,:,:,:]
valid_labels = train_labels[TRAINING_SIZE:]
train_labels = train_labels[:-VALIDATION_SIZE]
num_epochs = NUM_EPOCHS
c0 = 0
c1 = 0
for i in range(len(train_labels)):
if train_labels[i][0] == 1:
c0 = c0 + 1
else:
c1 = c1 + 1
print ('Number of data points per class: c0 = ' + str(c0) + ' c1 = ' + str(c1))
print ('Balancing training data...') # udefull, but need maybe a shuffle
min_c = min(c0, c1)
idx0 = [i for i, j in enumerate(train_labels) if j[0] == 1]
idx1 = [i for i, j in enumerate(train_labels) if j[1] == 1]
new_indices = idx0[0:min_c] + idx1[0:min_c]
#print (len(new_indices))
#print (train_data.shape)
train_data = train_data[new_indices,:,:,:]
train_labels = train_labels[new_indices]
train_size = train_labels.shape[0]
c0 = 0
c1 = 0
for i in range(len(train_labels)):
if train_labels[i][0] == 1:
c0 = c0 + 1
else:
c1 = c1 + 1
print ('Number of data points per class: c0 = ' + str(c0) + ' c1 = ' + str(c1))
# This is where training samples and labels are fed to the graph.
# These placeholder nodes will be fed a batch of training data at each
# training step using the {feed_dict} argument to the Run() call below.
train_data_node = tf.placeholder(
tf.float32,
shape=(BATCH_SIZE, IMG_PATCH_SIZE, IMG_PATCH_SIZE, NUM_CHANNELS))
train_labels_node = tf.placeholder(tf.float32,
shape=(BATCH_SIZE, NUM_LABELS))
train_all_data_node = tf.constant(train_data)
# The variables below hold all the trainable weights. They are passed an
# initial value which will be assigned when when we call:
# {tf.initialize_all_variables().run()}
conv1_weights = tf.Variable(
tf.truncated_normal([FILTER_SIZE, FILTER_SIZE, NUM_CHANNELS, NB_NEUR1], # 5x5 filter, depth 32. (number of neurons for a kernel)
stddev=0.1,
seed=SEED))
conv1_biases = tf.Variable(tf.zeros([NB_NEUR1]))
conv2_weights = tf.Variable(
tf.truncated_normal([FILTER_SIZE, FILTER_SIZE, NB_NEUR1, NB_NEUR2],
stddev=0.1,
seed=SEED))
conv2_biases = tf.Variable(tf.constant(0.1, shape=[NB_NEUR2]))
fc1_weights = tf.Variable( # fully connected, depth 512.
tf.truncated_normal([int(IMG_PATCH_SIZE / 4 * IMG_PATCH_SIZE / 4 * 64), NB_NEUR3],
stddev=0.1,
seed=SEED))
fc1_biases = tf.Variable(tf.constant(0.1, shape=[NB_NEUR3]))
fc2_weights = tf.Variable(
tf.truncated_normal([NB_NEUR3, NUM_LABELS],
stddev=0.1,
seed=SEED))
fc2_biases = tf.Variable(tf.constant(0.1, shape=[NUM_LABELS]))
# Make an image summary for 4d tensor image with index idx
def get_image_summary(img, idx = 0):
V = tf.slice(img, (0, 0, 0, idx), (1, -1, -1, 1))
img_w = img.get_shape().as_list()[1]
img_h = img.get_shape().as_list()[2]
min_value = tf.reduce_min(V)
V = V - min_value
max_value = tf.reduce_max(V)
V = V / (max_value*PIXEL_DEPTH)
V = tf.reshape(V, (img_w, img_h, 1))
V = tf.transpose(V, (2, 0, 1))
V = tf.reshape(V, (-1, img_w, img_h, 1))
return V
# Make an image summary for 3d tensor image with index idx
def get_image_summary_3d(img):
V = tf.slice(img, (0, 0, 0), (1, -1, -1))
img_w = img.get_shape().as_list()[1]
img_h = img.get_shape().as_list()[2]
V = tf.reshape(V, (img_w, img_h, 1))
V = tf.transpose(V, (2, 0, 1))
V = tf.reshape(V, (-1, img_w, img_h, 1))
return V
# Get prediction for given input image
def get_prediction(img):
data = np.asarray(img_crop(img, IMG_PATCH_SIZE, IMG_PATCH_SIZE))
data_node = tf.constant(data)
output = tf.nn.softmax(model(data_node))
output_prediction = s.run(output)
img_prediction = label_to_img(img.shape[0], img.shape[1], IMG_PATCH_SIZE, IMG_PATCH_SIZE, output_prediction)
return img_prediction
# Get a concatenation of the prediction and groundtruth for given input file
def get_prediction_with_groundtruth(filename, image_idx):
imageid = "satImage_%.3d" % image_idx
image_filename = filename + imageid + ".png"
img = mpimg.imread(image_filename)
img_prediction = get_prediction(img)
cimg = concatenate_images(img, img_prediction)
return cimg
# Get prediction overlaid on the original image for given input file
def get_prediction_with_overlay(filename, image_idx):
imageid = "satImage_%.3d" % image_idx
image_filename = filename + imageid + ".png"
img = mpimg.imread(image_filename)
img_prediction = get_prediction(img)
oimg = make_img_overlay(img, img_prediction)
return oimg
# We will replicate the model structure for the training subgraph, as well
# as the evaluation subgraphs, while sharing the trainable parameters.
def model(data, train=False):
"""The Model definition."""
# 2D convolution, with 'SAME' padding (i.e. the output feature map has
# the same size as the input). Note that {strides} is a 4D array whose
# shape matches the data layout: [image index, y, x, depth].
conv = tf.nn.conv2d(data,
conv1_weights,
strides=[1, 1, 1, 1],
padding='SAME')
# Bias and rectified linear non-linearity.
relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))
# Max pooling. The kernel size spec {ksize} also follows the layout of
# the data. Here we have a pooling window of 2, and a stride of 2.
pool = tf.nn.max_pool(relu,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME')
conv2 = tf.nn.conv2d(pool,
conv2_weights,
strides=[1, 1, 1, 1],
padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
pool2 = tf.nn.max_pool(relu2,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME')
# Uncomment these lines to check the size of each layer
# print 'data ' + str(data.get_shape())
# print 'conv ' + str(conv.get_shape())
# print 'relu ' + str(relu.get_shape())
# print 'pool ' + str(pool.get_shape())
# print 'pool2 ' + str(pool2.get_shape())
# Reshape the feature map cuboid into a 2D matrix to feed it to the
# fully connected layers.
pool_shape = pool2.get_shape().as_list()
reshape = tf.reshape(
pool2,
[pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])
# Fully connected layer. Note that the '+' operation automatically
# broadcasts the biases.
hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
# Add a 50% dropout during training only. Dropout also scales
# activations such that no rescaling is needed at evaluation time.
#if train:
# hidden = tf.nn.dropout(hidden, 0.5, seed=SEED)
out = tf.matmul(hidden, fc2_weights) + fc2_biases
if train == True:
summary_id = '_0'
s_data = get_image_summary(data)
filter_summary0 = tf.summary.image('summary_data' + summary_id, s_data)
s_conv = get_image_summary(conv)
filter_summary2 = tf.summary.image('summary_conv' + summary_id, s_conv)
s_pool = get_image_summary(pool)
filter_summary3 = tf.summary.image('summary_pool' + summary_id, s_pool)
s_conv2 = get_image_summary(conv2)
filter_summary4 = tf.summary.image('summary_conv2' + summary_id, s_conv2)
s_pool2 = get_image_summary(pool2)
filter_summary5 = tf.summary.image('summary_pool2' + summary_id, s_pool2)
return out
# Training computation: logits + cross-entropy loss.
logits = model(train_data_node, True) # BATCH_SIZE*NUM_LABELS
# print 'logits = ' + str(logits.get_shape()) + ' train_labels_node = ' + str(train_labels_node.get_shape())
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=train_labels_node))
tf.summary.scalar('loss', loss)
all_params_node = [conv1_weights, conv1_biases, conv2_weights, conv2_biases, fc1_weights, fc1_biases, fc2_weights, fc2_biases]
all_params_names = ['conv1_weights', 'conv1_biases', 'conv2_weights', 'conv2_biases', 'fc1_weights', 'fc1_biases', 'fc2_weights', 'fc2_biases']
all_grads_node = tf.gradients(loss, all_params_node)
all_grad_norms_node = []
for i in range(0, len(all_grads_node)):
norm_grad_i = tf.global_norm([all_grads_node[i]])
all_grad_norms_node.append(norm_grad_i)
tf.summary.scalar(all_params_names[i], norm_grad_i)
# L2 regularization for the fully connected parameters.
regularizers = (tf.nn.l2_loss(fc1_weights) + tf.nn.l2_loss(fc1_biases) +
tf.nn.l2_loss(fc2_weights) + tf.nn.l2_loss(fc2_biases))
# Add the regularization term to the loss.
loss += 5e-4 * regularizers
# Optimizer: set up a variable that's incremented once per batch and
# controls the learning rate decay.
batch = tf.Variable(0)
# Decay once per epoch, using an exponential schedule starting at 0.01.
learning_rate = tf.train.exponential_decay(
0.01, # Base learning rate.
batch * BATCH_SIZE, # Current index into the dataset.
train_size, # Decay step.
0.95, # Decay rate.
staircase=True)
tf.summary.scalar('learning_rate', learning_rate)
# Use simple momentum for the optimization.
optimizer = tf.train.MomentumOptimizer(learning_rate,
0.0).minimize(loss,
global_step=batch)
# Predictions for the minibatch, validation set and test set.
train_prediction = tf.nn.softmax(logits)
# We'll compute them only once in a while by calling their {eval()} method.
train_all_prediction = tf.nn.softmax(model(train_all_data_node))
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Create a local session to run this computation.
with tf.Session() as s:
if RESTORE_MODEL:
# Restore variables from disk.
saver.restore(s, FLAGS.train_dir + "/model.ckpt")
print("Model restored.")
else:
# Run all the initializers to prepare the trainable parameters.
tf.global_variables_initializer().run()#initialize_all_variables().run()
# Build the summary operation based on the TF collection of Summaries.
summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(FLAGS.train_dir,
graph=s.graph)#_def=s.graph_def)
print ('Initialized!')
# Loop through training steps.
print ('Total number of iterations = ' + str(int(num_epochs * train_size / BATCH_SIZE)))
training_indices = range(train_size)
for iepoch in range(num_epochs):
# Permute training indices
perm_indices = np.random.permutation(training_indices)
for step in range (int(train_size / BATCH_SIZE)):
offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)
batch_indices = perm_indices[offset:(offset + BATCH_SIZE)]
# Compute the offset of the current minibatch in the data.
# Note that we could use better randomization across epochs.
batch_data = train_data[batch_indices, :, :, :]
batch_labels = train_labels[batch_indices]
# This dictionary maps the batch data (as a np array) to the
# node in the graph is should be fed to.
feed_dict = {train_data_node: batch_data,
train_labels_node: batch_labels}
if step % RECORDING_STEP == 0:
summary_str, _, l, lr, predictions = s.run(
[summary_op, optimizer, loss, learning_rate, train_prediction],
feed_dict=feed_dict)
#summary_str = s.run(summary_op, feed_dict=feed_dict)
summary_writer.add_summary(summary_str, step)
summary_writer.flush()
# print_predictions(predictions, batch_labels)
print ('Epoch {}: {:.2f}%'.format(iepoch,(float(step) * BATCH_SIZE / train_size)))
print ('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))
print ('Minibatch error: %.1f%%' % error_rate(predictions,
batch_labels))
sys.stdout.flush()
else:
# Run the graph and fetch some of the nodes.
_, l, lr, predictions = s.run(
[optimizer, loss, learning_rate, train_prediction],
feed_dict=feed_dict)
# Save the variables to disk.
save_path = saver.save(s, FLAGS.train_dir + "/model.ckpt")
print("Model saved in file: %s" % save_path)
print ("Running prediction on training set")
prediction_training_dir = "predictions_training/"
if not os.path.isdir(prediction_training_dir):
os.mkdir(prediction_training_dir)
for i in range(1, TRAINING_SIZE+VALIDATION_SIZE+1):
pimg = get_prediction_with_groundtruth(train_data_filename, i)
Image.fromarray(pimg).save(prediction_training_dir + "prediction_" + str(i) + ".png")
oimg = get_prediction_with_overlay(train_data_filename, i)
oimg.save(prediction_training_dir + "overlay_" + str(i) + ".png")
print ("Running prediction on testing set")
prediction_testing_dir = "predicitions_testing/"
if not os.path.isdir(prediction_testing_dir):
os.mkdir(prediction_testing_dir)
submission_filename = 'submission.csv'
with open(submission_filename, 'w') as f:
f.write('id,prediction\n')
for i in range(1, TESTING_SIZE+1):
image_filename = 'dataset/test_set_images/test_{}/test_{}.png'.format(i,i)
img = mpimg.imread(image_filename)
pred = get_prediction(img)
for j in range(0,pred.shape[1], IMG_PATCH_SIZE):
for k in range(0,pred.shape[0], IMG_PATCH_SIZE):
patch = pred[k:k+IMG_PATCH_SIZE,j:j+IMG_PATCH_SIZE]
label = int(np.mean(patch)<0.5)
f.writelines("{:03d}_{}_{},{}\n".format(i,j,k,label))
#pimg = get_test_prediction_with_groundtruth("dataset/test_set_images/", i)
if __name__ == '__main__':
tf.app.run()