-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask_rcnn.py
170 lines (161 loc) · 6.65 KB
/
mask_rcnn.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
import os
import sys
# Root directory of the project
ROOT_DIR = os.path.abspath("./")
ROOT_DIR = os.path.abspath("./Mask_RCNN")
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import random
import cv2
import imutils
import scipy.io as sio
from Mask_RCNN.mrcnn import utils
import Mask_RCNN.mrcnn.model as modellib
import coco
def preprocess_image(data_path):
def noisy(image):
mul = np.mean(image)
sigma = np.std(image)
gauss = np.random.normal(mul, sigma, image.shape)
gauss = gauss.reshape(image.shape)
masked_index = np.random.randint(0, 1, size=image.shape) < 0.1
image[masked_index] = image[masked_index] + 0.01 * gauss[masked_index]
return image
def create_two_views(data_path, image_name, view_1, view_2):
image = cv2.imread('{}/images/{}'.format(data_path, image_name))
dir = image_name.split('/')[0]
if not os.path.exists('{}/view_1/{}'.format(data_path, dir)):
os.mkdir('{}/view_1/{}'.format(data_path, dir))
os.mkdir('{}/view_2/{}'.format(data_path, dir))
angle = random.randint(-45, 45)
rotated_img = imutils.rotate(image, angle)
cv2.imwrite('{}/view_1/{}'.format(data_path, image_name), rotated_img)
view_1.append('{}/view_1/{}'.format(data_path, image_name))
noisy_img = noisy(image)
cv2.imwrite('{}/view_2/{}'.format(data_path, image_name), noisy_img)
view_2.append('{}/view_2/{}'.format(data_path, image_name))
view_1 = []
view_2 = []
labels = []
if not os.path.exists('{}/view_1'.format(data_path)):
os.mkdir('{}/view_1/'.format(data_path))
os.mkdir('{}/view_2/'.format(data_path))
with open('{}/lists/train.txt'.format(data_path)) as f:
for i in range(600):
a = f.readline()
labels.append(int(a[:3]))
create_two_views(data_path, a[:-1], view_1, view_2)
with open('{}/lists/test.txt'.format(data_path)) as f:
for i in range(588):
a = f.readline()
labels.append(int(a[:3]))
create_two_views(data_path, a[:-1], view_1, view_2)
sio.savemat('{}/birds_2_views.mat'.format(data_path), {'view_1': view_1, 'view_2': view_2, 'label': labels})
print('Finish generating training and test data!')
def load_masked_image(image, mask, name):
if len(mask) == 0:
print('Image {} does not have mask:'.format(name))
else:
count = 0
# pick the tok k components
k = 2
pixel_num = np.sum(np.sum(mask, axis=0), axis=0)
indices = pixel_num.argsort()[-k:][::-1]
for i in indices:
# check if the masked region is large enough to contain useful features
a = mask[:, :, i]
index = np.array([a, a, a]).transpose(1, 2, 0)
masked_image = np.zeros_like(image)
masked_image[index] = image[index]
# indices.append(True)
skimage.io.imsave('{}_{seg}.jpg'.format(name[:-4], seg=count), masked_image)
count += 1
if len(indices) >= 1:
a = np.sum(mask[:, :, indices], axis=2)
a[a > 0] = 1
a = 1 - a
a = np.array(a, dtype=np.bool)
index = np.array([a, a, a]).transpose(1, 2, 0)
masked_image = np.zeros_like(image)
masked_image[index] = image[index]
skimage.io.imsave('{}_{seg}.jpg'.format(name[:-4], seg=count), masked_image)
count += 1
for i in range(len(indices), k):
skimage.io.imsave('{}_{seg}.jpg'.format(name[:-4], seg=count), masked_image)
count += 1
# generate two views
if not os.path.exists('./dataset/birds/birds_2_views.mat'):
preprocess_image('./dataset/birds')
print('Finish generating two views')
# matplotlib inline
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
class InferenceConfig(coco.CocoConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
config.display()
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
model.load_weights(COCO_MODEL_PATH, by_name=True)
# read the input RGB image
images = sio.loadmat('./dataset/birds/birds_2_views.mat')
count = 0
for image_name in images['view_1']:
image_name = image_name.strip()
image = skimage.io.imread(image_name)
results = model.detect([image], verbose=0)
load_masked_image(image, results[0]['masks'], image_name)
count += 1
print(count)
# check if there are three segments for all images
for i in range(3):
if not os.path.exists('{}_{seg}.jpg'.format(image_name[:-4], seg=i)):
skimage.io.imsave('{}_{seg}.jpg'.format(image_name[:-4], seg=i), image)
count = 0
for image_name in images['view_2']:
image_name = image_name.strip()
image = skimage.io.imread(image_name)
results = model.detect([image], verbose=0)
load_masked_image(image, results[0]['masks'], image_name)
count += 1
print(count)
# check if there are three segments for all images
for i in range(3):
if not os.path.exists('{}_{seg}.jpg'.format(image_name[:-4], seg=i)):
skimage.io.imsave('{}_{seg}.jpg'.format(image_name[:-4], seg=i), image)
def load_masked_image(image, mask, name):
if len(mask) == 0:
print('Image {} does not have mask:'.format(name))
else:
count = 0
for i in range(mask.shape[2]):
# check if the masked region is large enough to contain useful features
a = mask[:, :, i]
index = np.array([a, a, a]).transpose(1, 2, 0)
masked_image = np.zeros_like(image)
masked_image[index] = image[index]
skimage.io.imsave('{}_{seg}.jpg'.format(name[:-4], seg=count), masked_image)
count += 1
a = np.sum(mask, axis=2)
a[a > 0] = 1
a = 1 - a
a = np.array(a, dtype=np.bool)
index = np.array([a, a, a]).transpose(1, 2, 0)
masked_image = np.zeros_like(image)
masked_image[index] = image[index]
skimage.io.imsave('{}_{seg}.jpg'.format(name[:-4], seg=count), masked_image)