Kaggle competition: Global Wheat Detection
<SOS> metric: custom metric Kaggle data source: global-wheat-detection<import_modules>
half = False
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, StratifiedKFold
import os
import math<choose_model_class>
device = torch.device('cuda:0')
 model = torch.load('/kaggle/input/wheat-submit/best_wheat1024.pt', map_location=device)['model'].to(device ).float().eval()
if half:
model.half()
class LN5(nn.Module):
def __init__(self):
super(LN5, self ).__init__()
self.lc = nn.Sequential(
 nn.Conv2d(1, 6, 5, padding=2),
nn.BatchNorm2d(6),
nn.ReLU(inplace=True),
nn.AvgPool2d(2, 2),
nn.Conv2d(6, 16, 5),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.AvgPool2d(2, 2), 
)
 self.ll = nn.Sequential(
 nn.Dropout(0.3),
nn.Linear(400, 120),
nn.BatchNorm1d(120),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Linear(120, 84),
nn.BatchNorm1d(84),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Linear(84, 10), 
)
 def forward(self, x):
x = self.lc(x)
 x = x.view(x.size(0), -1)
 x = self.ll(x)
 return x<train_on_grid>
img_paths = glob.glob('/kaggle/input/global-wheat-detection/test/*.jpg')
 print(img_paths )
def train_model(tt_loader, conv_model, optimizer, scheduler, criterion):
conv_model.train()
for batch_idx,(data, target)in enumerate(tt_loader):
data = data.unsqueeze(1)
 data, target = data, target
optimizer.zero_grad()
output = conv_model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
scheduler.step()
def evaluate(num_epoch, data_loader, conv_model):
conv_model.eval()
loss = 0
correct = 0
for data, target in data_loader:
data = data.unsqueeze(1)
 data, target = data, target
output = conv_model(data)
 loss += F.cross_entropy(output, target, reduction='sum' ).item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
loss /= len(data_loader.dataset)
 print('{} Average Val Loss: {:.4f}, Val Accuracy: {}/{}({:.3f}%)'.format(
 num_epoch, loss, correct, len(data_loader.dataset),
100.* correct / len(data_loader.dataset)))
 return loss 
<define_search_model>
def inference_detector(model, img_path):
dataset = LoadImages(img_path, img_size=1024)
 path, img, im0, vid_cap = next(iter(dataset))
img = torch.from_numpy(img ).to(device)
 img = img.half() if half else img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 pred = model(img, augment=True)[0]
pred = non_max_suppression(pred, conf_thres=0.4, iou_thres=0.5, classes=None, agnostic=True)
 bboxes = []
scores = []
clses = []
for i, det in enumerate(pred):
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape ).round()
for *xyxy, conf, cls in det:
xyxy = torch.tensor(xyxy ).view(-1 ).numpy()
bboxes.append([*xyxy, conf.item() ])
 return np.array(bboxes)
 sys.path.insert(0, ".. /input/weightedboxesfusion")
 def run_wbf(boxes,scores, iou_thr=0.4, skip_box_thr=0.34, weights=None):
labels0 = [np.ones(len(scores[idx])) for idx in range(len(scores)) ]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels0, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes, scores, labels
def inference_detector_wbf(model, img_path):
dataset = LoadImages(img_path, img_size=640)
 path, img, im0, vid_cap = next(iter(dataset))
img = torch.from_numpy(img ).to(device)
 img = img.half() if half else img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 pred = model(img, augment=True)[0]
pred = non_max_suppression(pred, conf_thres=0.4, iou_thres=0.95, classes=None, agnostic=False)
 bboxes = []
scores = []
clses = []
for i, det in enumerate(pred):
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape ).round()
for *xyxy, conf, cls in det:
xyxy = torch.tensor(xyxy ).view(-1 ).numpy()
bboxes.append(xyxy)
 scores.append(conf.item())
 bboxes = [bboxes]
scores = [scores]
bboxes, scores, labels = run_wbf(bboxes, scores)
 det = []
for bbox, score in zip(bboxes, scores):
det.append([*bbox, score])
 return np.array(det )
def kfold(num_model, num_epochs, conv, train_images, train_labels):
kf = StratifiedKFold(n_splits=num_model, shuffle=True, random_state=123)
 criterion = nn.CrossEntropyLoss()
for k,(tr_idx, val_idx)in enumerate(kf.split(train_images, train_labels)) :
print('start model {}'.format(k))
conv_model = LN5()
optimizer = optim.Adam(params=conv_model.parameters() , lr=0.005)
 scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)
 tt_images = train_images[tr_idx]
tt_labels = train_labels[tr_idx]
val_images = train_images[val_idx]
val_labels = train_labels[val_idx]
tt_images = tt_images.reshape(tt_images.shape[0], 28, 28)
 tt_images_tensor = torch.tensor(tt_images)/255.0
tt_labels_tensor = torch.tensor(tt_labels)
 tt_tensor = TensorDataset(tt_images_tensor, tt_labels_tensor)
 tt_loader = DataLoader(tt_tensor, batch_size=420, shuffle=True)
 val_images = val_images.reshape(val_images.shape[0], 28, 28)
 val_images_tensor = torch.tensor(val_images)/255.0
val_labels_tensor = torch.tensor(val_labels)
 val_tensor = TensorDataset(val_images_tensor, val_labels_tensor)
 val_loader = DataLoader(val_tensor, batch_size=420, shuffle=True)
 for n in range(num_epochs):
train_model(tt_loader, conv_model, optimizer, scheduler, criterion)
 evaluate(n, val_loader, conv_model)
 torch.save(conv_model.state_dict() , '.. /input/conv-10-100/conv_{}_{}_{}.pkl'.format(num_model,num_epochs,k))
conv.append(conv_model )<load_pretrained>
img_paths = glob.glob('/kaggle/input/global-wheat-detection/test/*.jpg')
 results = []
for img_path in tqdm(img_paths):
det = inference_detector_wbf(model, img_path)
 pred_strings = []
for bbox in det:
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(bbox[4], bbox[0], bbox[1], bbox[2]-bbox[0], bbox[3]-bbox[1]))
pred_strings = " ".join(pred_strings)
 result = {'image_id': img_path.split('/')[-1].split('.')[0], 'PredictionString': pred_strings}
results.append(result)
 test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'] )
<load_from_csv><EOS> 
test_df.to_csv('submission.csv', index=False )