-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
241 lines (195 loc) · 8.14 KB
/
main.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
import os
import sys
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# from skimage.data import imread
from skimage.io import imread
from skimage.morphology import label
TRAINING_VALIDATION_RATIO = 0.2
WORKING_DIR = 'G:/Programming/Python/Mask_RCNN_Ship_Segmentation'
INPUT_DIR = 'G:/Programming/Python/Mask_RCNN_Ship_Segmentation'
OUTPUT_DIR = 'G:/Programming/Python/Mask_RCNN_Ship_Segmentation/output'
LOGS_DIR = os.path.join(WORKING_DIR, "logs")
TRAIN_DATA_PATH = os.path.join(INPUT_DIR, 'airbus-ship-detection/train_v2')
TEST_DATA_PATH = os.path.join(INPUT_DIR, 'airbus-ship-detection/test_v2')
SAMPLE_SUBMISSION_PATH = os.path.join(INPUT_DIR, 'airbus-ship-detection/sample_submission_v2.csv')
TRAIN_SHIP_SEGMENTATIONS_PATH = os.path.join(INPUT_DIR, 'airbus-ship-detection/train_ship_segmentations_v2.csv')
MASK_RCNN_PATH = os.path.join(WORKING_DIR, 'Mask_RCNN-master')
COCO_WEIGHTS_PATH = os.path.join(WORKING_DIR, "mask_rcnn_coco.h5")
SHIP_CLASS_NAME = 'ship'
IMAGE_WIDTH = 768
IMAGE_HEIGHT = 768
SHAPE = (IMAGE_WIDTH, IMAGE_HEIGHT)
test_ds = os.listdir(TEST_DATA_PATH)
train_ds = os.listdir(TRAIN_DATA_PATH)
print('Working Dir:', WORKING_DIR, os.listdir(WORKING_DIR))
print('Input Dir:', INPUT_DIR, os.listdir(INPUT_DIR))
print('train dataset from: {}, {}'.format(TRAIN_DATA_PATH, len(train_ds)))
print('test dataset from: {}, {}'.format(TEST_DATA_PATH, len(test_ds)))
print(TRAIN_SHIP_SEGMENTATIONS_PATH)
masks = pd.read_csv(TRAIN_SHIP_SEGMENTATIONS_PATH)
masks.head()
def multi_rle_encode(img):
labels = label(img[:, :, 0])
return [rle_encode(labels == k) for k in np.unique(labels[labels > 0])]
def rle_encode(img):
pixels = img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x) for x in runs)
def rle_decode(mask_rle, shape=SHAPE):
s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int) for x in (s[0::2], s[1::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape).T
def masks_as_image(in_mask_list, shape=SHAPE):
all_masks = np.zeros(shape, dtype=np.int16)
for mask in in_mask_list:
if isinstance(mask, str):
all_masks += rle_decode(mask)
return np.expand_dims(all_masks, -1)
def shows_decode_encode(image_id, path=TRAIN_DATA_PATH):
fig, axarr = plt.subplots(1, 3, figsize=(10, 5))
img_0 = imread(os.path.join(path, image_id))
axarr[0].imshow(img_0)
axarr[0].set_title(image_id)
rle_1 = masks.query('ImageId=="{}"'.format(image_id))['EncodedPixels']
img_1 = masks_as_image(rle_1)
axarr[1].imshow(img_1[:, :, 0])
axarr[1].set_title('Ship Mask')
rle_2 = multi_rle_encode(img_1)
img_2 = masks_as_image(rle_2)
axarr[2].imshow(img_0)
axarr[2].imshow(img_2[:, :, 0], alpha=0.3)
axarr[2].set_title('Encoded & Decoded Mask')
plt.show()
print(image_id, ' Check Decoding->Encoding',
'RLE_0:', len(rle_1), '->',
'RLE_1:', len(rle_2))
# shows_decode_encode('000155de5.jpg')
# shows_decode_encode('00003e153.jpg')
# print('It could be different when there is no mask.')
# shows_decode_encode('00021ddc3.jpg')
# print('It could be different when there are masks overlapped.')
masks['ships'] = masks['EncodedPixels'].map(lambda encoded_pixels: 1 if isinstance(encoded_pixels, str) else 0)
start_time = time.time()
unique_img_ids = masks.groupby('ImageId').agg({'ships': 'sum'})
unique_img_ids['RleMaskList'] = masks.groupby('ImageId')['EncodedPixels'].apply(list)
unique_img_ids = unique_img_ids.reset_index()
end_time = time.time() - start_time
# print("unique_img_ids groupby took: {}".format(end_time))
unique_img_ids = unique_img_ids[unique_img_ids['ships'] > 0]
unique_img_ids['ships'].hist()
unique_img_ids.sample(3)
from sklearn.model_selection import train_test_split
train_ids, val_ids = train_test_split(unique_img_ids,
test_size=TRAINING_VALIDATION_RATIO,
stratify=unique_img_ids['ships'])
print(train_ids.shape[0], 'training masks')
print(val_ids.shape[0], 'validation masks')
train_ids['ships'].hist()
val_ids['ships'].hist()
os.chdir(WORKING_DIR)
sys.path.append(MASK_RCNN_PATH)
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
class AirbusShipDetectionChallengeDataset(utils.Dataset):
def __init__(self, image_file_dir, ids, masks, image_width=IMAGE_WIDTH, image_height=IMAGE_HEIGHT):
super().__init__(self)
self.image_file_dir = image_file_dir
self.ids = ids
self.masks = masks
self.image_width = image_width
self.image_height = image_height
self.add_class(SHIP_CLASS_NAME, 1, SHIP_CLASS_NAME)
self.load_dataset()
def load_dataset(self):
for index, row in self.ids.iterrows():
image_id = row['ImageId']
image_path = os.path.join(self.image_file_dir, image_id)
rle_mask_list = row['RleMaskList']
self.add_image(
SHIP_CLASS_NAME,
image_id=image_id,
path=image_path,
width=self.image_width, height=self.image_height,
rle_mask_list=rle_mask_list)
def load_mask(self, image_id):
info = self.image_info[image_id]
rle_mask_list = info['rle_mask_list']
mask_count = len(rle_mask_list)
mask = np.zeros([info['height'], info['width'], mask_count],
dtype=np.uint8)
i = 0
for rel in rle_mask_list:
if isinstance(rel, str):
np.copyto(mask[:, :, i], rle_decode(rel))
i += 1
# return mask.astype(np.bool), np.ones([mask.shape[-1]], dtype=np.int32)
return mask.astype(bool), np.ones([mask.shape[-1]], dtype=np.int32)
def image_reference(self, image_id):
info = self.image_info[image_id]
if info['source'] == SHIP_CLASS_NAME:
return info['path']
else:
super(self.__class__, self).image_reference(image_id)
class AirbusShipDetectionChallengeGPUConfig(Config):
NAME = 'ASDC_GPU'
GPU_COUNT = 1
IMAGES_PER_GPU = 2
NUM_CLASSES = 2
IMAGE_MIN_DIM = IMAGE_WIDTH
IMAGE_MAX_DIM = IMAGE_WIDTH
STEPS_PER_EPOCH = 300
VALIDATION_STEPS = 50
SAVE_BEST_ONLY = True
DETECTION_MIN_CONFIDENCE = 0.95
DETECTION_NMS_THRESHOLD = 0.05
config = AirbusShipDetectionChallengeGPUConfig()
config.display()
start_time = time.time()
dataset_train = AirbusShipDetectionChallengeDataset(image_file_dir=TRAIN_DATA_PATH, ids=train_ids, masks=masks)
dataset_train.prepare()
dataset_val = AirbusShipDetectionChallengeDataset(image_file_dir=TRAIN_DATA_PATH, ids=val_ids, masks=masks)
dataset_val.prepare()
image_ids = np.random.choice(dataset_train.image_ids, 3)
for image_id in image_ids:
image = dataset_train.load_image(image_id)
mask, class_ids = dataset_train.load_mask(image_id)
visualize.display_top_masks(image, mask, class_ids, dataset_train.class_names, limit=1)
end_time = time.time() - start_time
print("dataset prepare: {}".format(end_time))
start_time = time.time()
model = modellib.MaskRCNN(mode="training", config=config, model_dir=WORKING_DIR)
try:
weights_path = model.find_last()
print("Weights path is:", weights_path)
load_weights = True
except FileNotFoundError:
load_weights = True
weights_path = COCO_WEIGHTS_PATH
print("tried to download weights again...")
# utils.download_trained_weights(weights_path, verbose=1)
if load_weights:
print("Loading weights: ", weights_path)
model.load_weights(weights_path, by_name=True, exclude=[
"mrcnn_class_logits", "mrcnn_bbox_fc",
"mrcnn_bbox", "mrcnn_mask"])
end_time = time.time() - start_time
print("loading weights: {}".format(end_time))
start_time = time.time()
model.train(dataset_train, dataset_val,
learning_rate=config.LEARNING_RATE * 1.5,
epochs=2,
layers='all')
end_time = time.time() - start_time
print("Train model: {}".format(end_time))