-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
310 lines (224 loc) · 9.22 KB
/
test.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
import pandas as pd
import numpy as np
import cv2
import os
import re
from PIL import Image
import albumentations as A
from albumentations.pytorch.transforms import ToTensorV2
import torch
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection import FasterRCNN
from torchvision.models.detection.rpn import AnchorGenerator
from dataset.wheat import WheatDataset,WheatTestDataset
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.sampler import SequentialSampler
from matplotlib import pyplot as plt
import Weighted_Boxes_Fusion.ensemble_boxes
DIR_INPUT = '/data1/jliang_data/dataset/wheat'
DIR_TRAIN = f'{DIR_INPUT}/train'
DIR_TEST = f'{DIR_INPUT}/test'
WEIGHTS_FILE = f'/data1/jliang_data/competition/first/global_wheat_detection/new_model/fasterrcnn_resnet152_fpn-30.pth'
test_df = pd.read_csv(f'{DIR_INPUT}/sample_submission.csv')
os.environ["CUDA_VISIBLE_DEVICES"] = '3'
# Albumentations
def get_test_transform():
return A.Compose([
# A.Resize(512, 512),
ToTensorV2(p=1.0)
])
from torchvision.models.detection.backbone_utils import resnet_fpn_backbone
def fasterrcnn_resnet101_fpn(pretrained=False, progress=True,
num_classes=91, pretrained_backbone=False,
trainable_backbone_layers=3, **kwargs):
assert trainable_backbone_layers <= 5 and trainable_backbone_layers >= 0
# dont freeze any layers if pretrained model or backbone is not used
if not (pretrained or pretrained_backbone):
trainable_backbone_layers = 5 #TODO: whta's mean of this trainable_backbone_layers
if pretrained:
# no need to download the backbone if pretrained is set
pretrained_backbone = False
backbone = resnet_fpn_backbone('resnet152', pretrained_backbone)
model = FasterRCNN(backbone, num_classes, **kwargs)
return model
def initialize_model():
model = fasterrcnn_resnet101_fpn(pretrained=False)
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, 2)
return model
# load a model; pre-trained on COCO
# model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False, pretrained_backbone=False)
model = initialize_model()
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
num_classes = 2 # 1 class (wheat) + background
# get number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features
# replace the pre-trained head with a new one
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
# Load the trained weights
model.load_state_dict(torch.load(WEIGHTS_FILE))
model.eval()
x = model.to(device)
def collate_fn(batch):
return tuple(zip(*batch))
test_dataset = WheatTestDataset(DIR_INPUT, get_test_transform())
test_data_loader = DataLoader(
test_dataset,
batch_size=4,
shuffle=False,
num_workers=0,
drop_last=False,
collate_fn=collate_fn
)
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings)
detection_threshold = 0.5
results = []
for images, image_ids in test_data_loader:
images = list(image.to(device) for image in images)
outputs = model(images)
for i, image in enumerate(images):
boxes = outputs[i]['boxes'].data.cpu().numpy()
scores = outputs[i]['scores'].data.cpu().numpy()
boxes = boxes[scores >= detection_threshold].astype(np.int32)
scores = scores[scores >= detection_threshold]
image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
}
results.append(result)
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
sample = images[1].permute(1,2,0).cpu().numpy()
boxes = outputs[1]['boxes'].data.cpu().numpy()
scores = outputs[1]['scores'].data.cpu().numpy()
boxes = boxes[scores >= detection_threshold].astype(np.int32)
for box in boxes:
cv2.rectangle(sample,
(box[0], box[1]),
(box[2], box[3]),
(220, 0, 0), 2)
sample = cv2.cvtColor(sample, cv2.COLOR_RGB2BGR).astype(np.float32)
cv2.imwrite('demo.jpg', sample*255)
test_df.to_csv('submission.csv', index=False)
class BaseWheatTTA:
""" author: @shonenkov """
image_size = 1024
def augment(self, image):
raise NotImplementedError
def batch_augment(self, images):
raise NotImplementedError
def deaugment_boxes(self, boxes):
raise NotImplementedError
class TTAHorizontalFlip(BaseWheatTTA):
""" author: @shonenkov """
def augment(self, image):
return image.flip(1)
def batch_augment(self, images):
return images.flip(2)
def deaugment_boxes(self, boxes):
boxes[:, [1, 3]] = self.image_size - boxes[:, [3, 1]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):
""" author: @shonenkov """
def augment(self, image):
return image.flip(2)
def batch_augment(self, images):
return images.flip(3)
def deaugment_boxes(self, boxes):
boxes[:, [0, 2]] = self.image_size - boxes[:, [2, 0]]
return boxes
class TTARotate90(BaseWheatTTA):
""" author: @shonenkov """
def augment(self, image):
return torch.rot90(image, 1, (1, 2))
def batch_augment(self, images):
return torch.rot90(images, 1, (2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0, 2]] = self.image_size - boxes[:, [1, 3]]
res_boxes[:, [1, 3]] = boxes[:, [2, 0]]
return res_boxes
class TTARotate180(BaseWheatTTA):
""" author: @shonenkov """
def augment(self, image):
return torch.rot90(image, 2, (1, 2))
def batch_augment(self, images):
return torch.rot90(images, 2, (2, 3))
def deaugment_boxes(self, boxes):
boxes[:, [0, 1, 2, 3]] = self.image_size - boxes[:, [2, 3, 0, 1]]
return boxes
class TTARotate270(BaseWheatTTA):
""" author: @shonenkov """
def augment(self, image):
return torch.rot90(image, 3, (1, 2))
def batch_augment(self, images):
return torch.rot90(images, 3, (2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0, 2]] = boxes[:, [1, 3]]
res_boxes[:, [1, 3]] = self.image_size - boxes[:, [2, 0]]
return res_boxes
class TTACompose(BaseWheatTTA):
""" author: @shonenkov """
def __init__(self, transforms):
self.transforms = transforms
def augment(self, image):
for transform in self.transforms:
image = transform.augment(image)
return image
def batch_augment(self, images):
for transform in self.transforms:
images = transform.batch_augment(images)
return images
def prepare_boxes(self, boxes):
result_boxes = boxes.copy()
result_boxes[:, 0] = np.min(boxes[:, [0, 2]], axis=1)
result_boxes[:, 2] = np.max(boxes[:, [0, 2]], axis=1)
result_boxes[:, 1] = np.min(boxes[:, [1, 3]], axis=1)
result_boxes[:, 3] = np.max(boxes[:, [1, 3]], axis=1)
return result_boxes
def deaugment_boxes(self, boxes):
for transform in self.transforms[::-1]:
boxes = transform.deaugment_boxes(boxes)
return self.prepare_boxes(boxes)
def process_det(index, det, score_threshold=0.5):
boxes = det[index]['boxes'].detach().cpu().numpy()
scores = det[index]['scores'].detach().cpu().numpy()
# boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
# boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
boxes = (boxes).clip(min=0, max=1023).astype(int) #TODO:511?
indexes = np.where(scores>score_threshold)
boxes = boxes[indexes]
scores = scores[indexes]
return boxes, scores
# you can try own combinations:
transform = TTACompose([
TTARotate90(),
TTAVerticalFlip(),
])
fig, ax = plt.subplots(1, 3, figsize=(16, 6))
image, image_id = test_dataset[8]
numpy_image = image.permute(1, 2, 0).cpu().numpy().copy()
ax[0].imshow(numpy_image)
ax[0].set_title('original')
tta_image = transform.augment(image)
tta_image_numpy = tta_image.permute(1, 2, 0).cpu().numpy().copy()
det = model(tta_image.unsqueeze(0).float().cuda())
boxes, scores = process_det(0, det)
for box in boxes:
cv2.rectangle(tta_image_numpy, (box[0], box[1]), (box[2], box[3]), (0, 1, 0), 2)
ax[1].imshow(tta_image_numpy)
ax[1].set_title('tta')
boxes = transform.deaugment_boxes(boxes)
for box in boxes:
cv2.rectangle(numpy_image, (box[0], box[1]), (box[2], box[3]), (0, 1, 0), 2)
ax[2].imshow(numpy_image)
ax[2].set_title('deaugment predictions')
plt.savefig('tta_demo.jpg')