Kaggle competition: Global Wheat Detection
<SOS> metric: custom metric Kaggle data source: global-wheat-detection<train_model>
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null
def train(model: nn.Module, loss_function: F, params_to_update):
optimizer = torch.optim.SGD(params=params_to_update, lr=learning_rate, momentum=0.9)
 pbar=tqdm(total=len(train_loader))
model.train()
for epoch in range(epochs):
pbar.set_description('Epoch %d/%d' %(epoch + 1, epochs))
pbar.reset()
epoch_loss = 0
epoch_accuracy = 0
for data, labels in train_loader:
data = data.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
preds = model(data)
 error = loss_function(preds, labels)
 error.backward()
optimizer.step()
epoch_accuracy +=(( preds.argmax(dim=1)== labels ).float().mean())
 epoch_loss += error.item()
pbar.update()
epoch_accuracy /= len(train_loader)
 epoch_loss /= len(train_loader)
 print('Epoch %d finished with acc: %g, loss: %g' %(epoch+1, epoch_accuracy, epoch_loss))
pbar.close()<choose_model_class>
USE_OPTIMIZE = True
USE_TTA = True
use_pretrained = True
net = models.vgg16(pretrained=use_pretrained )<choose_model_class>
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size = 512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint["model_state_dict"])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
if USE_OPTIMIZE:
models = [
load_net('.. /input/effdet-fold0/best-checkpoint-023epoch.bin'),
load_net('.. /input/effdet-fold1/best-checkpoint-017epoch.bin')]
net.classifier[6] = nn.Linear(in_features=4096, out_features=2 )<find_best_params>
best_final_score = 0.7202
best_iou_thr = 0.460
best_skip_box_thr = 0.394
params_to_update = []
update_params_name = ['classifier.6.weight', 'classifier.6.bias']
for name, param in net.named_parameters() :
if name in update_params_name:
param.requires_grad = True
params_to_update.append(param)
 else:
param.requires_grad = False<train_model>
def TTAImage(image, index):
image1 = image.copy()
if index==0:
rotated_image = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image
elif index==1:
rotated_image2 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image2 = cv2.rotate(rotated_image2, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image2
elif index==2:
rotated_image3 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image3
elif index == 3:
return image1
def rotBoxes90(boxes, im_w, im_h):
ret_boxes =[]
for box in boxes:
x1, y1, x2, y2 = box
x1, y1, x2, y2 = x1-im_w//2, im_h//2 - y1, x2-im_w//2, im_h//2 - y2
x1, y1, x2, y2 = y1, -x1, y2, -x2
x1, y1, x2, y2 = int(x1+im_w//2), int(im_h//2 - y1), int(x2+im_w//2), int(im_h//2 - y2)
 x1a, y1a, x2a, y2a = min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)
 ret_boxes.append([x1a, y1a, x2a, y2a])
 return np.array(ret_boxes )
net = net.to(device)
 train(net, loss_function, params_to_update )<find_best_params>
def to_tensor(images):
tmp = []
for img in images:
img = img.astype(np.float32)
 img /= 255.0
img = torch.tensor(img, dtype=torch.float32)
 tmp.append(img.permute(2,0,1))
return torch.stack(tmp )
def evaluate_model(model: nn.Module, dataset: torch.utils.data.DataLoader, loss_function: F):
loss = 0
acc = 0
model.eval()
for data, labels in tqdm(dataset):
data = data.to(device)
 labels = labels.to(device)
 with torch.no_grad() :
preds = model(data)
 loss += loss_function(preds, labels ).item()
acc +=(( preds.argmax(dim=1)== labels ).float().mean())
 return acc / len(dataset), loss / len(dataset)
<train_model>
def run_wbf_tta(predictions, image_size=512, iou_thr=best_iou_thr, skip_box_thr=best_skip_box_thr, weights=None):
boxes_1 = []
scores_1 = []
labels_1 = []
for i in range(2):
bb =(predictions['boxes'][i]/(image_size-1)).tolist()
ss = predictions['scores'][i].tolist()
ls = predictions['labels'][i].tolist()
boxes_1.append(bb)
 scores_1.append(ss)
 labels_1.append(ls)
 boxes_1, scores_1, labels_1 = weighted_boxes_fusion(boxes_1, scores_1, labels_1,
weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes_2 = []
scores_2 = []
labels_2 = []
for i in range(2,4):
bb =(predictions['boxes'][i]/(image_size-1)).tolist()
ss = predictions['scores'][i].tolist()
ls = predictions['labels'][i].tolist()
boxes_2.append(bb)
 scores_2.append(ss)
 labels_2.append(ls)
 boxes_2, scores_2, labels_2 = weighted_boxes_fusion(boxes_2, scores_2, labels_2,
weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = [boxes_1.tolist() , boxes_2.tolist() ]
scores = [scores_1.tolist() , scores_2.tolist() ]
labels = [labels_1.tolist() , labels_2.tolist() ]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels,
weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels
acc_train, loss_train = evaluate_model(net, train_loader, loss_function)
 print('Training set: ')
 print('Loss: %g, Accuracy: %g' %(loss_train, acc_train))
print('')
 acc_val, loss_val = evaluate_model(net, val_loader, loss_function)
 print('Validation set: ')
 print('Loss: %g, Accuracy: %g' %(loss_val, acc_val))<save_to_csv>
results = []
for images, image_ids in data_loader:
if USE_TTA:
image = images[0]
predictions_tta = {
"boxes": [],
"scores": [],
"labels": []
}
for index in range(4):
roated = TTAImage(image, index)
 roated = to_tensor([roated] ).cuda()
predictions = make_predictions(roated)
 boxes, scores, labels = run_wbf(predictions, image_index=0)
 for _ in range(3-index):
boxes = rotBoxes90(boxes, 512, 512)
 if index == 3:
boxes = boxes.astype(np.int32)
 predictions_tta["boxes"].append(boxes)
 predictions_tta["scores"].append(scores)
 predictions_tta["labels"].append(labels)
 boxes, scores, labels = run_wbf_tta(predictions_tta)
 boxes =(boxes*2 ).astype(np.int32 ).clip(min=0, max=1023)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {
'image_id': image_ids[0],
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 else:
images = to_tensor(images ).cuda()
predictions = make_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).astype(np.int32 ).clip(min=0, max=1023)
 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 )
<install_modules><EOS> 
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False )