-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
663 lines (541 loc) · 24.6 KB
/
train.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# IEEE's Signal Processing Society - Camera Model Identification
# https://www.kaggle.com/c/sp-society-camera-model-identification
#
# (C) 2018 Andres Torrubia, licensed under GNU General Public License v3.0
# See license.txt
import argparse
import glob
import numpy as np
import pandas as pd
import random
from os.path import isfile, join
from sklearn.model_selection import train_test_split
from sklearn.utils import class_weight
from keras.optimizers import Adam, Adadelta, SGD
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.models import load_model, Model
from keras.layers import concatenate, Lambda, Input, Dense, Dropout, Flatten, Conv2D, MaxPooling2D, \
BatchNormalization, Activation, GlobalAveragePooling2D, Reshape
from keras.utils import to_categorical
from keras.applications import *
from keras import backend as K
from keras.engine.topology import Layer
import keras
from multi_gpu_keras import multi_gpu_model
import skimage
from tqdm import tqdm
from PIL import Image
from io import BytesIO
import copy
import itertools
import re
import os
import sys
from tqdm import tqdm
import jpeg4py as jpeg
from scipy import signal
import cv2
import math
import csv
from multiprocessing import Pool, cpu_count
from functools import partial
from itertools import islice
from conditional import conditional
import subprocess
SEED = 42
np.random.seed(SEED)
random.seed(SEED)
# TODO tf seed
parser = argparse.ArgumentParser()
parser.add_argument('--max-epoch', type=int, default=200, help='Epoch to run')
parser.add_argument('-b', '--batch-size', type=int, default=16, help='Batch Size during training, e.g. -b 64')
parser.add_argument('-l', '--learning_rate', type=float, default=1e-4, help='Initial learning rate')
parser.add_argument('-m', '--model', help='load hdf5 model including weights (and continue training)')
parser.add_argument('-w', '--weights', help='load hdf5 weights only (and continue training)')
parser.add_argument('-do', '--dropout', type=float, default=0.3, help='Dropout rate for FC layers')
parser.add_argument('-doc', '--dropout-classifier', type=float, default=0., help='Dropout rate for classifier')
parser.add_argument('-t', '--test', action='store_true', help='Test model and generate CSV submission file')
parser.add_argument('-tt', '--test-train', action='store_true', help='Test model on the test_test set, also set -t as True')
parser.add_argument('-cs', '--crop-size', type=int, default=512, help='Crop size')
parser.add_argument('-g', '--gpus', type=int, default=1, help='Number of GPUs to use')
parser.add_argument('-p', '--pooling', type=str, default='avg', help='Type of pooling to use: avg|max|none')
parser.add_argument('-nfc', '--no-fcs', action='store_true', help='Dont add any FC at the end, just a softmax')
parser.add_argument('-kf', '--kernel-filter', action='store_true', help='Apply kernel filter')
parser.add_argument('-lkf', '--learn-kernel-filter', action='store_true', help='Add a trainable kernel filter before classifier')
parser.add_argument('-cm', '--classifier', type=str, default='ResNet50', help='Base classifier model to use')
parser.add_argument('-uiw', '--use-imagenet-weights', action='store_true', help='Use imagenet weights (transfer learning)')
parser.add_argument('-x', '--extra-dataset', action='store_true', help='Use dataset from https://www.kaggle.com/c/sp-society-camera-model-identification/discussion/47235')
parser.add_argument('-v', '--verbose', action='store_true', help='Pring debug/verbose info')
parser.add_argument('-e', '--ensembling', type=str, default='arithmetic', help='Type of ensembling: arithmetic|geometric for TTA')
parser.add_argument('-tta', action='store_true', help='Enable test time augmentation')
parser.add_argument('--check-train', action='store_true', default=False, help='Enable checking of all train JPEGs to remove broken')
args = parser.parse_args()
TRAIN_FOLDER = '../input/train'
EXTRA_TRAIN_FOLDER = '../input/external'
# NEW_TRAIN_FOLDER = '../input/raw/flickr_new'
# EXTRA_MOTOX_FOLDER = '../input/raw/moto_x_all'
EXTRA_VAL_FOLDER = '../input/raw/val_images'
if args.test_train:
TEST_FOLDER = '../input/test_test'
args.test_train = False
args.test = True
else:
TEST_FOLDER = '../input/test'
MODEL_FOLDER = '../output/models'
SUBMITS_FOLDER = '../output/submits'
PROBS_FOLDER = '../output/probs'
CROP_SIZE = args.crop_size
CLASSES = [
'HTC-1-M7',
'iPhone-6',
'Motorola-Droid-Maxx',
'Motorola-X',
'Samsung-Galaxy-S4',
'iPhone-4s',
'LG-Nexus-5x',
'Motorola-Nexus-6',
'Samsung-Galaxy-Note3',
'Sony-NEX-7']
EXTRA_CLASSES = [
'htc_m7',
'iphone_6',
'moto_maxx',
'moto_x',
'samsung_s4',
'iphone_4s',
'nexus_5x',
'nexus_6',
'samsung_note3',
'sony_nex7'
]
RESOLUTIONS = {
0: [[1520,2688]], # flips
1: [[3264,2448]], # no flips
2: [[2432,4320]], # flips
3: [[3120,4160]], # flips
4: [[4128,2322]], # no flips
5: [[3264,2448]], # no flips
6: [[3024,4032]], # flips
7: [[1040,780], # Motorola-Nexus-6 no flips
[3088,4130], [3120,4160]], # Motorola-Nexus-6 flips
8: [[4128,2322]], # no flips
9: [[6000,4000]], # no flips
}
ORIENTATION_FLIP_ALLOWED = [
True,
False,
True,
True,
False,
False,
True,
True,
False,
False
]
for class_id,resolutions in RESOLUTIONS.copy().items():
resolutions.extend([resolution[::-1] for resolution in resolutions])
RESOLUTIONS[class_id] = resolutions
MANIPULATIONS = ['jpg70', 'jpg90', 'gamma0.8', 'gamma1.2', 'bicubic0.5', 'bicubic0.8', 'bicubic1.5', 'bicubic2.0']
N_CLASSES = len(CLASSES)
# load_img_fast_jpg = lambda img_path: jpeg.JPEG(img_path).decode()
load_img = lambda img_path: np.array(Image.open(img_path))
def load_img_fast_jpg(img_path):
try:
x = jpeg.JPEG(img_path).decode()
return x
except Exception:
print('Decoding error:', img_path)
return load_img(img_path)
def check_remove_broken(img_path):
try:
x = jpeg.JPEG(img_path).decode()
except Exception:
print('Decoding error:', img_path)
os.remove(img_path)
def check_load_ids(train_folder):
if args.check_train:
ids = glob.glob(join(train_folder, '*/*.jpg'))
print('Checking files in {} folder'.format(train_folder))
p = Pool(cpu_count() - 2)
p.map(check_remove_broken, tqdm(ids))
ids = glob.glob(join(train_folder, '*/*.jpg'))
return ids
def random_manipulation(img, manipulation=None):
if manipulation == None:
manipulation = random.choice(MANIPULATIONS)
if manipulation.startswith('jpg'):
quality = int(manipulation[3:])
out = BytesIO()
im = Image.fromarray(img)
im.save(out, format='jpeg', quality=quality)
im_decoded = jpeg.JPEG(np.frombuffer(out.getvalue(), dtype=np.uint8)).decode()
del out
del im
elif manipulation.startswith('gamma'):
gamma = float(manipulation[5:])
# alternatively use skimage.exposure.adjust_gamma
# img = skimage.exposure.adjust_gamma(img, gamma)
im_decoded = np.uint8(cv2.pow(img / 255., gamma)*255.)
elif manipulation.startswith('bicubic'):
scale = float(manipulation[7:])
im_decoded = cv2.resize(img,(0,0), fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
else:
assert False
return im_decoded
def preprocess_image(img):
if args.kernel_filter:
# see slide 13
# http://www.lirmm.fr/~chaumont/publications/WIFS-2016_TUAMA_COMBY_CHAUMONT_Camera_Model_Identification_With_CNN_slides.pdf
kernel_filter = 1/12. * np.array([\
[-1, 2, -2, 2, -1], \
[ 2, -6, 8, -6, 2], \
[-2, 8, -12, 8, -2], \
[ 2, -6, 8, -6, 2], \
[-1, 2, -2, 2, -1]])
return cv2.filter2D(img.astype(np.float32),-1,kernel_filter)
# kernel filter already puts mean ~0 and roughly scales between [-1..1]
# no need to preprocess_input further
else:
# find `preprocess_input` function specific to the classifier
classifier_to_module = {
'NASNetLarge' : 'nasnet',
'NASNetMobile' : 'nasnet',
'DenseNet40' : 'densenet',
'DenseNet121' : 'densenet',
'DenseNet161' : 'densenet',
'DenseNet201' : 'densenet',
'InceptionResNetV2' : 'inception_resnet_v2',
'InceptionV3' : 'inception_v3',
'MobileNet' : 'mobilenet',
'ResNet50' : 'resnet50',
'VGG16' : 'vgg16',
'VGG19' : 'vgg19',
'Xception' : 'xception',
}
if args.classifier in classifier_to_module:
classifier_module_name = classifier_to_module[args.classifier]
else:
classifier_module_name = 'xception'
preprocess_input_function = getattr(globals()[classifier_module_name], 'preprocess_input')
return preprocess_input_function(img.astype(np.float32))
def get_crop(img, crop_size, random_crop=False):
center_x, center_y = img.shape[1] // 2, img.shape[0] // 2
half_crop = crop_size // 2
pad_x = max(0, crop_size - img.shape[1])
pad_y = max(0, crop_size - img.shape[0])
if (pad_x > 0) or (pad_y > 0):
img = np.pad(img, ((pad_y//2, pad_y - pad_y//2), (pad_x//2, pad_x - pad_x//2), (0,0)), mode='wrap')
center_x, center_y = img.shape[1] // 2, img.shape[0] // 2
if random_crop:
freedom_x, freedom_y = img.shape[1] - crop_size, img.shape[0] - crop_size
if freedom_x > 0:
center_x += np.random.randint(math.ceil(-freedom_x/2), freedom_x - math.floor(freedom_x/2) )
if freedom_y > 0:
center_y += np.random.randint(math.ceil(-freedom_y/2), freedom_y - math.floor(freedom_y/2) )
return img[center_y - half_crop : center_y + crop_size - half_crop, center_x - half_crop : center_x + crop_size - half_crop]
def get_class(class_name):
if class_name in CLASSES:
class_idx = CLASSES.index(class_name)
elif class_name in EXTRA_CLASSES:
class_idx = EXTRA_CLASSES.index(class_name)
else:
assert False
assert class_idx in range(N_CLASSES)
return class_idx
def process_item(item, training, transforms=[[]]):
class_name = item.split('/')[-2]
class_idx = get_class(class_name)
validation = not training
img = load_img_fast_jpg(item)
shape = list(img.shape[:2])
# # discard images that do not have right resolution
#if shape not in RESOLUTIONS[class_idx]:
# return None
# discard only too small images
#if np.max(shape) < 2000 or np.min(shape) < 1100:
# return None
# some images may not be downloaded correclty and are B/W, discard those
# some images may not be downloaded correctly and are B/W, discard those
if img.ndim != 3:
return None
if len(transforms) == 1:
_img = img
else:
_img = np.copy(img)
img_s = [ ]
manipulated_s = [ ]
class_idx_s = [ ]
for transform in transforms:
force_manipulation = 'manipulation' in transform
if ('orientation' in transform) and (ORIENTATION_FLIP_ALLOWED[class_idx] is False):
continue
force_orientation = ('orientation' in transform) and ORIENTATION_FLIP_ALLOWED[class_idx]
# some images are landscape, others are portrait, so augment training by randomly changing orientation
if ((np.random.rand() < 0.5) and training and ORIENTATION_FLIP_ALLOWED[class_idx]) or force_orientation:
img = np.rot90(_img, 1, (0,1))
# is it rot90(..3..), rot90(..1..) or both?
# for phones with landscape mode pics could be taken upside down too, although less likely
# most of the test images that are flipped are 1
# however,eg. img_4d7be4c_unalt looks 3
# and img_4df3673_manip img_6a31fd7_unalt looks 2!
else:
img = _img
img = get_crop(img, CROP_SIZE * 2, random_crop=True if training else False)
# * 2 bc may need to scale by 0.5x and still get a 512px crop
if args.verbose:
print("om: ", img.shape, item)
manipulated = 0.
if ((np.random.rand() < 0.5) and training) or force_manipulation:
img = random_manipulation(img)
manipulated = 1.
if args.verbose:
print("am: ", img.shape, item)
img = get_crop(img, CROP_SIZE, random_crop=True if training else False)
if args.verbose:
print("ac: ", img.shape, item)
img = preprocess_image(img)
if args.verbose:
print("ap: ", img.shape, item)
if len(transforms) > 1:
img_s.append(img)
manipulated_s.append(manipulated)
class_idx_s.append(class_idx)
if len(transforms) == 1:
return img, manipulated, class_idx
else:
return img_s, manipulated_s, class_idx_s
VALIDATION_TRANSFORMS = [ [], ['orientation'], ['manipulation'], ['orientation','manipulation']]
def gen(items, batch_size, training=True):
validation = not training
# during validation we store the unaltered images on batch_idx and a manip one on batch_idx + batch_size, hence the 2
valid_batch_factor = 1 # TODO: augment validation
# X holds image crops
X = np.empty((batch_size * valid_batch_factor, CROP_SIZE, CROP_SIZE, 3), dtype=np.float32)
# O whether the image has been manipulated (1.) or not (0.)
O = np.empty((batch_size * valid_batch_factor, 1), dtype=np.float32)
# class index
y = np.empty((batch_size * valid_batch_factor), dtype=np.int64)
p = Pool(cpu_count()-2)
transforms = VALIDATION_TRANSFORMS if validation else [[]]
while True:
if training:
random.shuffle(items)
process_item_func = partial(process_item, training=training, transforms=transforms)
batch_idx = 0
iter_items = iter(items)
for item_batch in iter(lambda:list(islice(iter_items, batch_size)), []):
batch_results = p.map(process_item_func, item_batch)
for batch_result in batch_results:
if batch_result is not None:
if len(transforms) == 1:
X[batch_idx], O[batch_idx], y[batch_idx] = batch_result
batch_idx += 1
else:
for _X,_O,_y in zip(*batch_result):
X[batch_idx], O[batch_idx], y[batch_idx] = _X,_O,_y
batch_idx += 1
if batch_idx == batch_size:
yield([X, O], [y])
batch_idx = 0
if batch_idx == batch_size:
yield([X, O], [y])
batch_idx = 0
# MAIN
if args.model:
print("Loading model " + args.model)
# e.g. DenseNet201_do0.3_doc0.0_avg-epoch128-val_acc0.964744.hdf5
match = re.search(r'(([a-zA-Z0-9]+)_[A-Za-z_\d\.]+)-epoch(\d+)-.*\.hdf5', args.model)
model_name = match.group(1)
if match.group(2) == 'MobileNet':
from keras.utils.generic_utils import CustomObjectScope
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,
'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
model = load_model(args.model, compile=False)
else:
model = load_model(args.model, compile=False)
args.classifier = match.group(2)
CROP_SIZE = args.crop_size = model.get_input_shape_at(0)[0][1]
print("Overriding classifier: {} and crop size: {}".format(args.classifier, args.crop_size))
last_epoch = int(match.group(3))
else:
last_epoch = 0
input_image = Input(shape=(CROP_SIZE, CROP_SIZE, 3))
manipulated = Input(shape=(1,))
classifier = globals()[args.classifier]
classifier_model = classifier(
include_top=False,
weights = 'imagenet' if args.use_imagenet_weights else None,
input_shape=(CROP_SIZE, CROP_SIZE, 3),
pooling=args.pooling if args.pooling != 'none' else None)
x = input_image
if args.learn_kernel_filter:
x = Conv2D(3, (7, 7), strides=(1,1), use_bias=False, padding='valid', name='filtering')(x)
x = classifier_model(x)
x = Reshape((-1,))(x)
if args.dropout_classifier != 0.:
x = Dropout(args.dropout_classifier, name='dropout_classifier')(x)
x = concatenate([x, manipulated])
if not args.no_fcs:
x = Dense(512, activation='relu', name='fc1')(x)
x = Dropout(args.dropout, name='dropout_fc1')(x)
x = Dense(128, activation='relu', name='fc2')(x)
x = Dropout(args.dropout, name='dropout_fc2')(x)
prediction = Dense(N_CLASSES, activation ="softmax", name="predictions")(x)
model = Model(inputs=(input_image, manipulated), outputs=prediction)
model_name = args.classifier + \
('_kf' if args.kernel_filter else '') + \
('_lkf' if args.learn_kernel_filter else '') + \
'_do' + str(args.dropout) + \
'_doc' + str(args.dropout_classifier) + \
'_' + args.pooling
if args.weights:
model.load_weights(args.weights, by_name=True, skip_mismatch=True)
match = re.search(r'([A-Za-z_\d\.]+)-epoch(\d+)-.*\.hdf5', args.weights)
last_epoch = int(match.group(2))
def print_distribution(ids, classes=None):
if classes is None:
classes = [get_class(idx.split('/')[-2]) for idx in ids]
classes_count = np.bincount(classes)
for class_name, class_count in zip(CLASSES, classes_count):
print('{:>22}: {:5d} ({:04.1f}%)'.format(class_name, class_count, 100. * class_count / len(classes)))
model.summary()
model = multi_gpu_model(model, gpus=args.gpus)
if not (args.test or args.test_train):
# TRAINING
ids = glob.glob(join(TRAIN_FOLDER,'*/*.jpg'))
ids.sort()
if not args.extra_dataset:
ids_train, ids_val = train_test_split(ids, test_size=0.1, random_state=SEED)
else:
ids_train = ids
ids_val = [ ]
df = pd.read_csv("common_image_info_additional.csv")
ids_train = [c.replace('\\',"/") for c in df[(df["valid_soft"]==1)&(df["valid_resolution_and_quality"]==1)]["filename"]]
for x in ids_train:
if not isfile(x):
print("Missing:", x)
extra_val_ids = glob.glob(join(EXTRA_VAL_FOLDER,'*/*.jpg'))
extra_val_ids.sort()
ids_val.extend(extra_val_ids)
classes_val = [get_class(idx.split('/')[-2]) for idx in ids_val]
classes_val_count = np.bincount(classes_val)
max_classes_val_count = max(classes_val_count)
# Balance validation dataset by filling up classes with less items from training set (and removing those from there)
for class_idx in range(N_CLASSES):
idx_to_transfer = [idx for idx in ids_train \
if get_class(idx.split('/')[-2]) == class_idx][:max_classes_val_count-classes_val_count[class_idx]]
ids_train = list(set(ids_train).difference(set(idx_to_transfer)))
ids_val.extend(idx_to_transfer)
random.shuffle(ids_train)
random.shuffle(ids_val)
print("Training set distribution:")
print_distribution(ids_train)
print("Validation set distribution:")
print_distribution(ids_val)
classes_train = [get_class(idx.split('/')[-2]) for idx in ids_train]
class_weight = class_weight.compute_class_weight('balanced', np.unique(classes_train), classes_train)
opt = Adam(lr=args.learning_rate)
#opt = SGD(lr=args.learning_rate, decay=1e-6, momentum=0.9, nesterov=True)
# TODO: implement this correctly.
def weighted_loss(weights):
def loss(y_true, y_pred):
return K.mean(K.square(y_pred - y_true) - K.square(y_true - noise), axis=-1)
return loss
model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
metric = "-val_acc{val_acc:.6f}"
monitor = 'val_acc'
save_checkpoint = ModelCheckpoint(
join(MODEL_FOLDER, model_name+"-epoch{epoch:03d}"+metric+".hdf5"),
monitor=monitor,
verbose=0, save_best_only=True, save_weights_only=False, mode='max', period=1)
reduce_lr = ReduceLROnPlateau(monitor=monitor, factor=0.5, patience=5, min_lr=1e-9, epsilon = 0.00001, verbose=1, mode='max')
model.fit_generator(
generator = gen(ids_train, args.batch_size),
steps_per_epoch = int(math.ceil(len(ids_train) // args.batch_size)),
validation_data = gen(ids_val, args.batch_size, training = False),
validation_steps = int(len(VALIDATION_TRANSFORMS) * math.ceil(len(ids_val) // args.batch_size)),
epochs = args.max_epoch,
callbacks = [save_checkpoint, reduce_lr],
initial_epoch = last_epoch,
max_queue_size = 10,
class_weight=class_weight)
else:
# TEST
if args.test:
ids = glob.glob(join(TEST_FOLDER,'*.*'))
elif args.test_train:
ids = glob.glob(join(TRAIN_FOLDER,'*/*.jpg'))
else:
assert False
ids.sort()
from conditional import conditional
submission_file = 'submission {}.csv'.format(args.model.split(sep='/')[-1])
with conditional(args.test, open(join(SUBMITS_FOLDER, submission_file), 'w')) as csvfile:
classes = []
if args.test:
csv_writer = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['fname','camera'])
classes = []
else:
correct_predictions = 0
fnames = []
labels = []
aug = []
probs = np.array([]*10).reshape((0,10))
for i, idx in enumerate(tqdm(ids)):
#fnames.append(idx.split("/")[-1])
img = np.array(Image.open(idx))
# брать кроп всегда, да и всё, зачем тут выбор?
img = get_crop(img, CROP_SIZE, random_crop=False)
manipulated = np.float32([1. if idx.find('manip') != -1 else 0.])
sx = img.shape[1] // CROP_SIZE
sy = img.shape[0] // CROP_SIZE
j = 0
k = 8
img_batch = np.zeros((k * sx * sy, CROP_SIZE, CROP_SIZE, 3), dtype=np.float32)
manipulated_batch = np.zeros((k * sx * sy, 1), dtype=np.float32)
timg = cv2.transpose(img)
for _img in [img, cv2.flip(img, 0), cv2.flip(img, 1), cv2.flip(img, -1),
timg, cv2.flip(timg, 0), cv2.flip(timg, 1), cv2.flip(timg, -1)]:
img_batch[j] = preprocess_image(_img)
manipulated_batch[j] = manipulated
fnames.append(idx.split("/")[-1])
aug.append(j)
j+=1
l = img_batch.shape[0]
batch_size = args.batch_size
for i in range(l//batch_size+1):
batch_pred = model.predict_on_batch([img_batch[i*batch_size:min(l,(i+1)*batch_size)],
manipulated_batch[i*batch_size:min(l,(i+1)*batch_size)]])
if i==0:
prediction = batch_pred
else:
prediction = np.concatenate((prediction, batch_pred),axis=0)
probs = np.vstack((probs, prediction))
if prediction.shape[0] != 1: # TTA
#prediction = np.mean(prediction, axis=0)
#prediction = np.max(prediction, axis=0)
prediction = np.sqrt(np.mean(prediction**2, axis=0))
#prediction = scipy.stats.mstats.gmean(prediction, axis=0)
#print(prediction)
prediction_class_idx = np.argmax(prediction)
#probs = np.vstack((probs, prediction))
if args.test_train:
class_idx = get_class(idx.split('/')[-2])
if class_idx == prediction_class_idx:
correct_predictions += 1
if args.test:
csv_writer.writerow([idx.split('/')[-1], CLASSES[prediction_class_idx]])
classes.append(prediction_class_idx)
ans = pd.DataFrame()
ans["name"] = fnames
ans["aug"] = aug
for i in range(10):
ans[CLASSES[i]] = probs[:,i]
pd.DataFrame(ans).to_hdf(PROBS_FOLDER + "/tta_8_"+args.model.split("/")[-1],"prob")
if args.test_train:
print("Accuracy: " + str(correct_predictions / len(ids)))
if args.test:
print("Test set predictions distribution:")
print_distribution(None, classes=classes)