-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneShot.py
251 lines (192 loc) · 6.49 KB
/
OneShot.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
# -*- coding: utf-8 -*-
"""Sia.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1CY9w2Y6dvTY9Rpi9B3nIpKzGId3e4SxR
#Run Always
Connect to your GDrive (with atleast 8 GB free space) and move to project directory
#Change accordingly
cd drive/My Drive/*Directory with project in it *
"""
from google.colab import drive
drive.mount('/content/drive')
cd drive/My\ Drive/Colab Notebooks/
"""#Run Once
**Download Project Only once**
(If you don't have the folder siamese-mask-rcnn already)
"""
!git clone https://github.com/bethgelab/siamese-mask-rcnn.git
"""#Run Always
**Move to project folder**
"""
cd siamese-mask-rcnn
"""#Run Once
Download COCO DATASET and COCO API
Download pre-trained model
"""
cd data
!wget http://images.cocodataset.org/zips/val2017.zip
!unzip val2017 -d coco
!wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
!unzip annotations_trainval2017.zip -d coco
!git clone https://github.com/cocodataset/cocoapi.git
cd ..
!mkdir checkpoints
cd checkpoints
!wget https://github.com/bethgelab/siamese-mask-rcnn/releases/download/v0.1/small_siamese_mrcnn_0160.h5
cd ..
"""#Run Always
**Install COCO API and Dependency**
From siamese-mask-rcnn folder
"""
! sudo pip install numpy scipy Pillow cython matplotlib scikit-image tensorflow==1.5.0 keras==2.1.6 opencv-python h5py
#(Run from siamese-mask-rcnn folder)
! (cd data/cocoapi/PythonAPI/ && sudo make install)
"""#Run Always
Import Libraries
"""
# Commented out IPython magic to ensure Python compatibility.
# %load_ext autoreload
# %autoreload 2
# %matplotlib inline
#%load_ext line_profiler
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
sess_config = tf.ConfigProto()
import sys
import os
COCO_DATA = 'data/coco/'
MASK_RCNN_MODEL_PATH = 'lib/Mask_RCNN/'
if MASK_RCNN_MODEL_PATH not in sys.path:
sys.path.append(MASK_RCNN_MODEL_PATH)
from samples.coco import coco
from mrcnn import utils
from mrcnn import model as modellib
from mrcnn import visualize
from lib import utils as siamese_utils
from lib import model as siamese_model
from lib import config as siamese_config
import time
import datetime
import random
import numpy as np
import skimage.io
import imgaug
import pickle
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from collections import OrderedDict
from PIL import Image
# Root directory of the project
ROOT_DIR = os.getcwd()
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
coco_val = siamese_utils.IndexedCocoDataset()
coco_object = coco_val.load_coco(COCO_DATA, "val", year="2017", return_coco=True)
coco_val.prepare()
coco_val.build_indices()
"""#Small/Large"""
class SmallEvalConfig(siamese_config.Config):
# 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
NUM_CLASSES = 1 + 1
NAME = 'coco'
EXPERIMENT = 'evaluation'
CHECKPOINT_DIR = 'checkpoints/'
NUM_TARGETS = 1
class LargeEvalConfig(siamese_config.Config):
# 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
NUM_CLASSES = 1 + 1
NAME = 'coco'
EXPERIMENT = 'evaluation'
CHECKPOINT_DIR = 'checkpoints/'
NUM_TARGETS = 1
# Large image sizes
TARGET_MAX_DIM = 192
TARGET_MIN_DIM = 150
IMAGE_MIN_DIM = 800
IMAGE_MAX_DIM = 1024
# Large model size
FPN_CLASSIF_FC_LAYERS_SIZE = 1024
FPN_FEATUREMAPS = 256
# Large number of rois at all stages
RPN_ANCHOR_STRIDE = 1
RPN_TRAIN_ANCHORS_PER_IMAGE = 256
POST_NMS_ROIS_TRAINING = 2000
POST_NMS_ROIS_INFERENCE = 1000
TRAIN_ROIS_PER_IMAGE = 200
DETECTION_MAX_INSTANCES = 100
MAX_GT_INSTANCES = 100
"""#Download Large model to work with it"""
cd checkpoints
!wget https://github.com/bethgelab/siamese-mask-rcnn/releases/download/v0.2/large_siamese_mrcnn_coco_full_0320.h5
cd ..
"""#Select small or large"""
model_size = 'small' #or'small' # or 'large'
if model_size == 'small':
config = SmallEvalConfig()
elif model_size == 'large':
config = LargeEvalConfig()
config.display()
# Provide training schedule of the model
# When evaluationg intermediate steps the tranining schedule must be provided
train_schedule = OrderedDict()
if model_size == 'small':
train_schedule[1] = {"learning_rate": config.LEARNING_RATE, "layers": "heads"}
train_schedule[120] = {"learning_rate": config.LEARNING_RATE, "layers": "4+"}
train_schedule[160] = {"learning_rate": config.LEARNING_RATE/10, "layers": "all"}
elif model_size == 'large':
train_schedule[1] = {"learning_rate": config.LEARNING_RATE, "layers": "heads"}
train_schedule[240] = {"learning_rate": config.LEARNING_RATE, "layers": "all"}
train_schedule[320] = {"learning_rate": config.LEARNING_RATE/10, "layers": "all"}
if model_size == 'small':
checkpoint = 'checkpoints/small_siamese_mrcnn_0160.h5'
elif model_size == 'large':
checkpoint = 'checkpoints/large_siamese_mrcnn_coco_full_0320.h5'
model = siamese_model.SiameseMaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
model.load_checkpoint(checkpoint, training_schedule=train_schedule)
"""#Important Cell
This will select any random image from siamese-mask-rcnn>data>coco>val2017
And gives output
This cell can be re-runs as many times as you want, it will give different result each time
Debug this and our project is done
#For large, target size should be (192,192)
#For small target size is (96,96)
"""
# Select category
category = 1
image_id = np.random.choice(coco_val.category_image_index[category])
# Load target
#target = siamese_utils.get_one_target(category, coco_val, config)
from google.colab import files
from io import BytesIO
uploaded = files.upload()
for i in uploaded.keys():
ti = uploaded[i]
print(ti)
ti2 = Image.open(BytesIO(ti))
ti3 = ti2.resize((96, 96), Image.ANTIALIAS)
target=np.array(ti3)
imgplot=plt.imshow(target)
print(target.shape)
plt.show()
# Load image
#image = coco_val.load_image(image_id)
#print("image_id", image_id)
uploaded = files.upload()
for i in uploaded.keys():
ti = uploaded[i]
image=np.array(Image.open(BytesIO(ti)))
imgplot = plt.imshow(image)
plt.show()
print(image.shape)
# Run detection
results = model.detect([[target]], [image], verbose=1)
r = results[0]
# Display results
siamese_utils.display_results(target, image, r['rois'], r['masks'], r['class_ids'], r['scores'])