-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaggle_512.py
388 lines (277 loc) · 11.8 KB
/
kaggle_512.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
import os
from pathlib import Path
import gc
import sys
import json
import glob
import random
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import itertools
from tqdm import tqdm
from imgaug import augmenters as iaa
from sklearn.model_selection import StratifiedKFold, KFold
""" Define Global Path """
ROOT_DIR = Path('D:\Kaggle\Fashion')
DATA_DIR = Path('D:\Kaggle\Fashion')
MASK_DIR = Path("D:\Kaggle\Fashion\Mask_RCNN")
COCO_WEIGHTS_PATH = "D:\Kaggle\Fashion\Mask_RCNN\mask_rcnn_coco.h5"
sys.path.append(r"D:\Kaggle\Fashion\Mask_RCNN")
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log
''' Set up the Mask RCNN model '''
NUM_CATS = 46
IMAGE_SIZE = 512
class FashionConfig(Config):
NAME = "fashion"
NUM_CLASSES = NUM_CATS + 1 # +1 for the background class
GPU_COUNT = 1
IMAGES_PER_GPU = 1 # a memory error occurs when IMAGES_PER_GPU is too high
BACKBONE = 'resnet50'
IMAGE_MIN_DIM = IMAGE_SIZE
IMAGE_MAX_DIM = IMAGE_SIZE
IMAGE_RESIZE_MODE = 'none'
RPN_ANCHOR_SCALES = (16, 32, 64, 128, 256)
# DETECTION_NMS_THRESHOLD = 0.0
# STEPS_PER_EPOCH should be the number of instances
# divided by (GPU_COUNT*IMAGES_PER_GPU), and so should VALIDATION_STEPS;
# however, due to the time limit, I set them so that this kernel can be run in 9 hours
STEPS_PER_EPOCH = 150
VALIDATION_STEPS = 5
config = FashionConfig()
config.display()
with open(ROOT_DIR / "label_descriptions.json") as f:
label_descriptions = json.load(f)
label_names = [x['name'] for x in label_descriptions['categories']]
segment_df = pd.read_csv(ROOT_DIR / "train.csv")
multilabel_percent = len(segment_df[segment_df['ClassId'].str.contains('_')]) / len(segment_df) * 100
print(f"Segments that have attributes: {multilabel_percent:.2f}%")
segment_df['CategoryId'] = segment_df['ClassId'].str.split('_').str[0]
print("Total segments: ", len(segment_df))
print(segment_df.head())
image_df = segment_df.groupby('ImageId')['EncodedPixels', 'CategoryId'].agg(lambda x: list(x))
size_df = segment_df.groupby('ImageId')['Height', 'Width'].mean()
image_df = image_df.join(size_df, on='ImageId')
print("Total images: ", len(image_df))
print(image_df.head())
def resize_image(image_path):
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_AREA)
return img
class FashionDataset(utils.Dataset):
def __init__(self, df):
super().__init__(self)
# Add classes
for i, name in enumerate(label_names):
self.add_class("fashion", i + 1, name)
# Add images
for i, row in df.iterrows():
self.add_image("fashion",
image_id=row.name,
path=str(DATA_DIR / 'train' / row.name),
labels=row['CategoryId'],
annotations=row['EncodedPixels'],
height=row['Height'], width=row['Width'])
def image_reference(self, image_id):
info = self.image_info[image_id]
return info['path'], [label_names[int(x)] for x in info['labels']]
def load_image(self, image_id):
return resize_image(self.image_info[image_id]['path'])
def load_mask(self, image_id):
info = self.image_info[image_id]
mask = np.zeros((IMAGE_SIZE, IMAGE_SIZE, len(info['annotations'])), dtype=np.uint8)
labels = []
for m, (annotation, label) in enumerate(zip(info['annotations'], info['labels'])):
sub_mask = np.full(info['height'] * info['width'], 0, dtype=np.uint8)
annotation = [int(x) for x in annotation.split(' ')]
for i, start_pixel in enumerate(annotation[::2]):
sub_mask[start_pixel: start_pixel + annotation[2 * i + 1]] = 1
sub_mask = sub_mask.reshape((info['height'], info['width']), order='F')
sub_mask = cv2.resize(sub_mask, (IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_NEAREST)
mask[:, :, m] = sub_mask
labels.append(int(label) + 1)
return mask, np.array(labels)
dataset = FashionDataset(image_df)
dataset.prepare()
for i in range(6):
image_id = random.choice(dataset.image_ids)
print(dataset.image_reference(image_id))
image = dataset.load_image(image_id)
mask, class_ids = dataset.load_mask(image_id)
visualize.display_top_masks(image, mask, class_ids, dataset.class_names, limit=4)
FOLD = 0
N_FOLDS = 5
kf = KFold(n_splits=N_FOLDS, random_state=42, shuffle=True)
splits = kf.split(image_df) # ideally, this should be multilabel stratification
def get_fold():
for i, (train_index, valid_index) in enumerate(splits):
if i == FOLD:
return image_df.iloc[train_index], image_df.iloc[valid_index]
train_df, valid_df = get_fold()
train_dataset = FashionDataset(train_df)
train_dataset.prepare()
valid_dataset = FashionDataset(valid_df)
valid_dataset.prepare()
train_segments = np.concatenate(train_df['CategoryId'].values).astype(int)
print("Total train images: ", len(train_df))
print("Total train segments: ", len(train_segments))
plt.figure()
values, counts = np.unique(train_segments, return_counts=True)
plt.bar(values, counts)
plt.xticks(values, label_names, rotation='vertical')
plt.show()
valid_segments = np.concatenate(valid_df['CategoryId'].values).astype(int)
print("Total validation images: ", len(valid_df))
print("Total validation segments: ", len(valid_segments))
plt.figure()
values, counts = np.unique(valid_segments, return_counts=True)
plt.bar(values, counts)
plt.xticks(values, label_names, rotation='vertical')
plt.show()
# Note that any hyperparameters here, such as LR, may still not be optimal
LR = 1e-4
# EPOCHS = [2, 6, 8]
EPOCHS = [20, 80, 100]
import warnings
warnings.filterwarnings("ignore")
model = modellib.MaskRCNN(mode='training', config=config, model_dir=ROOT_DIR)
model.load_weights(COCO_WEIGHTS_PATH, by_name=True, exclude=[
'mrcnn_class_logits', 'mrcnn_bbox_fc', 'mrcnn_bbox', 'mrcnn_mask'])
augmentation = iaa.Sequential([
iaa.Fliplr(0.5) # only horizontal flip here
])
''' Train the model'''
# 5.3.3.1 LR*2
print("done")
model.train(train_dataset, valid_dataset,
learning_rate=LR*5, # train heads with higher lr to speedup learning
epochs=EPOCHS[0],
layers='heads',
augmentation=None)
history = model.keras_model.history.history
# 5.3.3.2 LR
model.train(train_dataset, valid_dataset,
learning_rate=LR,
epochs=EPOCHS[1],
layers='all',
augmentation=augmentation)
new_history = model.keras_model.history.history
for k in new_history: history[k] = history[k] + new_history[k]
# 5.3.3.3 LR/5
model.train(train_dataset, valid_dataset,
learning_rate=LR/5,
# learning_rate=100*LR,
epochs=EPOCHS[2],
layers='all',
augmentation=augmentation)
new_history = model.keras_model.history.history
for k in new_history: history[k] = history[k] + new_history[k]
# 5.3.4 Loss Review
epochs = range(EPOCHS[-1])
plt.figure()
plt.subplot(131)
plt.plot(epochs, history['loss'], label="train loss")
plt.plot(epochs, history['val_loss'], label="valid loss")
plt.legend()
plt.subplot(132)
plt.plot(epochs, history['mrcnn_class_loss'], label="train class loss")
plt.plot(epochs, history['val_mrcnn_class_loss'], label="valid class loss")
plt.legend()
plt.subplot(133)
plt.plot(epochs, history['mrcnn_mask_loss'], label="train mask loss")
plt.plot(epochs, history['val_mrcnn_mask_loss'], label="valid mask loss")
plt.legend()
plt.savefig("loss_temp512.png")
best_epoch = np.argmin(history["val_loss"]) + 1
print("Best epoch: ", best_epoch)
print("Valid loss: ", history["val_loss"][best_epoch-1])
""" 6. Predict """
glob_list = glob.glob(f'D:/Kaggle/Fashion/fashion20190602T0037/mask_rcnn_fashion_{best_epoch:04d}.h5')
model_path = glob_list[0] if glob_list else ''
class InferenceConfig(FashionConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 1
inference_config = InferenceConfig()
model = modellib.MaskRCNN(mode='inference',
config=inference_config,
model_dir=ROOT_DIR)
assert model_path != '', "Provide path to trained weights"
print("Loading weights from ", model_path)
model.load_weights(model_path, by_name=True)
sample_df = pd.read_csv(ROOT_DIR/"sample_submission.csv")
sample_df.head()
# Convert data to run-length encoding
def to_rle(bits):
rle = []
pos = 0
for bit, group in itertools.groupby(bits):
group_list = list(group)
if bit:
rle.extend([pos, sum(group_list)])
pos += len(group_list)
return rle
# Since the submission system does not permit overlapped masks, we have to fix them
def refine_masks(masks, rois):
areas = np.sum(masks.reshape(-1, masks.shape[-1]), axis=0)
mask_index = np.argsort(areas)
union_mask = np.zeros(masks.shape[:-1], dtype=bool)
for m in mask_index:
masks[:, :, m] = np.logical_and(masks[:, :, m], np.logical_not(union_mask))
union_mask = np.logical_or(masks[:, :, m], union_mask)
for m in range(masks.shape[-1]):
mask_pos = np.where(masks[:, :, m]==True)
if np.any(mask_pos):
y1, x1 = np.min(mask_pos, axis=1)
y2, x2 = np.max(mask_pos, axis=1)
rois[m, :] = [y1, x1, y2, x2]
return masks, rois
sub_list = []
missing_count = 0
for i, row in tqdm(sample_df.iterrows(), total=len(sample_df)):
image = resize_image(str(ROOT_DIR/'test'/row['ImageId']))
result = model.detect([image])[0]
if result['masks'].size > 0:
masks, _ = refine_masks(result['masks'], result['rois'])
for m in range(masks.shape[-1]):
mask = masks[:, :, m].ravel(order='F')
rle = to_rle(mask)
label = result['class_ids'][m] - 1
sub_list.append([row['ImageId'], ' '.join(list(map(str, rle))), label])
else:
# The system does not allow missing ids, this is an easy way to fill them
sub_list.append([row['ImageId'], '1 1', 23])
missing_count += 1
submission_df = pd.DataFrame(sub_list, columns=sample_df.columns.values)
print("Total image results: ", submission_df['ImageId'].nunique())
print("Missing Images: ", missing_count)
submission_df.head()
submission_df.to_csv("submission.csv", index=False)
for i in range(9):
image_id = sample_df.sample()['ImageId'].values[0]
image_path = str(DATA_DIR / 'test' / image_id)
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = model.detect([resize_image(image_path)])
r = result[0]
if r['masks'].size > 0:
masks = np.zeros((img.shape[0], img.shape[1], r['masks'].shape[-1]), dtype=np.uint8)
for m in range(r['masks'].shape[-1]):
masks[:, :, m] = cv2.resize(r['masks'][:, :, m].astype('uint8'),
(img.shape[1], img.shape[0]), interpolation=cv2.INTER_NEAREST)
y_scale = img.shape[0] / IMAGE_SIZE
x_scale = img.shape[1] / IMAGE_SIZE
rois = (r['rois'] * [y_scale, x_scale, y_scale, x_scale]).astype(int)
masks, rois = refine_masks(masks, rois)
else:
masks, rois = r['masks'], r['rois']
visualize.display_instances(img, rois, masks, r['class_ids'],
['bg'] + label_names, r['scores'],
title=image_id, figsize=(12, 12))
plt.show()
print("done")