Kaggle competition: Global Wheat Detection
y= data['revenue'].values
cols = [col for col in data.columns if col not in ['revenue', 'id']]
X= data[cols].values
y = np.log1p(y )<compute_train_metric>
models =[
load_net5('.. /input/efficientdetearlierversionf0/best-checkpoint-047epoch.bin'),
load_net5('.. /input/efficientdet5f0/best-checkpoint-028epoch.bin'),
load_net7('.. /input/training-efficientdet-f4/effdet5-cutmix-augmix/best-checkpoint-037epoch.bin'),
]
def make_predictions(images, score_threshold=0.1):
images = torch.stack(images ).cuda().float()
predictions = []
with torch.no_grad() :
det = net(images, torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
predictions.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
 return [predictions]
def make_tta_predictions(images, score_threshold=0.25):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
def make_tta_models_predictions(images, score_threshold=0.25):
images = torch.stack(images ).float().cuda()
with torch.no_grad() :
predictions = []
for tta_transform in tta_transforms:
for net in models:
result = []
det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
def make_models_predictions(images, score_threshold=0.1):
images = torch.stack(images ).cuda().float()
predictions = []
for net in models:
with torch.no_grad() :
det = net(images, torch.tensor([1]*images.shape[0] ).float().cuda())
 result = []
for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
result.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
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 )
clf = LinearRegression()
scores = cross_val_score(clf, X, y, scoring="neg_mean_squared_error", cv=10)
 rmse_scores = np.sqrt(-scores)
 print(rmse_scores.mean() )<compute_train_metric>
def resize_predicitions(predictions):

 for i in range(len(predictions)) :
for j in range(len(predictions[i])) :
predictions[i][j]['boxes']=predictions[i][j]['boxes']*2
return predictions
def effdet_organize(predictions, image_index):
boxes = [(prediction[image_index]['boxes'] ).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).astype(int ).tolist() for prediction in predictions]
if 0:
for i in range(len(scores)) :
scores[i]=normalize(scores[i])
 return boxes, scores, labels
def yolo_organize(boxes,scores):
boxes_output=[]
for i in range(len(boxes[0])) :
boxes_output.append(boxes[0][i].tolist())
 scores_output=[]
for i in range(len(scores[0])) :
scores_output.append(scores[0][i].cpu().float().item())
 if 0:
scores_output = calibrate(scores_output)
 if 0:
scores_output = normalize(scores_output)
 labels_output = [1]*len(scores_output)
 return [boxes_output], [scores_output], [labels_output]
def run_wbf_ensemble(boxes_effdet, boxes_yolo, scores_effdet, scores_yolo, labels_effdet, labels_yolo, iou_thr=0.6, skip_box_thr=0.5, weights=None):
boxes = boxes_effdet + boxes_yolo
scores = scores_effdet + scores_yolo
labels = labels_effdet + labels_yolo
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes, scores, labels
def run_wbf(boxes, scores, labels, iou_thr=0.5, skip_box_thr=0.3, weights=None):
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes, scores, labels
def run_wbf_initial(boxes,scores, image_size=1024, iou_thr=0.41, skip_box_thr=0.4, weights=None):
labels0 = [np.ones(len(scores[idx])) for idx in range(len(scores)) ]
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels0, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes, scores, labels
regr = RandomForestRegressor(max_depth=10, min_samples_split=5, random_state=0,
n_estimators=500)
 scores = cross_val_score(regr, X, y, scoring="neg_mean_squared_error", cv=10)
 rmse_scores = np.sqrt(-scores)
 print(rmse_scores.mean() )<prepare_x_and_y>
for images, image_ids, image_size in data_loader:
predictions = make_tta_models_predictions(images)
 predictions=resize_predicitions(predictions)
 for i, image in enumerate(images):
image_id=image_ids[i]
boxes_effdet, scores_effdet, labels_effdet = effdet_organize(predictions, image_index=i)
 boxes, scores, labels = run_wbf(boxes_effdet, scores_effdet, labels_effdet, iou_thr=0.6, skip_box_thr=0.5, weights=None)
 boxes =(boxes/2 ).round().astype(np.int32 ).clip(min=0, max=512)
 fig, ax = plt.subplots(1, 1, figsize=(10, 10))
sample = images[i].permute(1,2,0 ).cpu().numpy()
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color =(0, 0, 1)
 thickness = 2
for box,score in zip(boxes,scores):
cv2.rectangle(sample,(box[0], box[1]),(box[2], box[3]),(0, 0, 1), 1)
 cv2.putText(sample, '{:.2}'.format(score),(box[0]+np.random.randint(20),box[1]), font, fontScale, color, thickness, cv2.LINE_AA)
 ax.set_axis_off()
ax.imshow(sample);
break
cols = [col for col in test.columns if col not in ['id']]
X_test= test[cols].values<train_model>
sys.path.insert(0, ".. /input/yolov5tta/")
 sys.path.insert(0, ".. /input/configyolov5")
 def detect(save_img=False):
weights, imgsz = opt.weights,opt.img_size
source = '.. /input/global-wheat-detection/test/'
device = torch_utils.select_device(opt.device)
 half = True
model = torch.load(weights, map_location=device)['model'].to(device ).eval()
dataset = LoadImages(source, img_size=1024)
 t0 = time.time()
img = torch.zeros(( 1, 3, imgsz, imgsz), device=device)
 all_path=[]
all_bboxex =[]
all_score =[]
for path, img, im0s, vid_cap in dataset:
print(im0s.shape)
 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)
 t1 = torch_utils.time_synchronized()
bboxes_2 = []
score_2 = []
if True:
pred = model(img, augment=opt.augment)[0]
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres,merge=True, classes=None, agnostic=False)
 t2 = torch_utils.time_synchronized()
bboxes = []
score = []
for i, det in enumerate(pred):
p, s, im0 = path, '', im0s
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape ).round()
for c in det[:, -1].unique() :
n =(det[:, -1] == c ).sum()
for *xyxy, conf, cls in det:
if True:
xywh = torch.tensor(xyxy ).view(-1 ).numpy()
bboxes.append(xywh)
 score.append(conf)
 bboxes_2.append(bboxes)
 score_2.append(score)
 all_path.append(path)
 all_score.append(score_2)
 all_bboxex.append(bboxes_2)
 return all_path,all_score,all_bboxex
if __name__ == '__main__':
class opt:
weights = ".. /input/train-continuity-yolov5x/weights/best_yolov5_fold0.pt"
img_size = 1024
conf_thres = 0.3
iou_thres = 0.94
augment = True
device = '0'
classes=None
agnostic_nms = True
opt.img_size = check_img_size(opt.img_size)
 with torch.no_grad() :
res = detect()
all_path,all_score,all_bboxex = res
regr.fit(X,y)
 y_pred = regr.predict(X_test )<save_to_csv>
for images, image_ids, image_sizes in data_loader:
predictions = make_tta_models_predictions(images)
 predictions=resize_predicitions(predictions)
 for i, image in enumerate(images):
image_id=image_ids[i]
for row in range(len(all_path)) :
image_id_yolo = all_path[row].split("/")[-1].split(".")[0]
if image_id_yolo==image_id:
boxes_yolo = all_bboxex[row]
scores_yolo = all_score[row]
boxes_yolo, scores_yolo, labels_yolo = yolo_organize(boxes_yolo,scores_yolo)
 boxes, scores, labels =run_wbf(boxes_yolo, scores_yolo, labels_yolo, iou_thr=0.41, skip_box_thr=0.4, weights=None)
 boxes =(boxes/2 ).round().astype(np.int32 ).clip(min=0, max=512)
 fig, ax = plt.subplots(1, 1, figsize=(10, 10))
sample = images[i].permute(1,2,0 ).cpu().numpy()
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color =(0, 0, 1)
 thickness = 2
for box,score in zip(boxes,scores):
cv2.rectangle(sample,(box[0], box[1]),(box[2], box[3]),(0, 0, 1), 1)
 cv2.putText(sample, '{:.2}'.format(score),(box[0]+np.random.randint(20),box[1]), font, fontScale, color, thickness, cv2.LINE_AA)
 ax.set_axis_off()
ax.imshow(sample);
break
y_pred=np.expm1(y_pred)
 pd.DataFrame({'id': test.id, 'revenue': y_pred} ).to_csv('submission_RF.csv', index=False )<import_modules>
results = []
for images, image_ids, image_sizes in data_loader:
predictions = make_tta_models_predictions(images)
 predictions=resize_predicitions(predictions)
 for i, image in enumerate(images):
image_id=image_ids[i]
image_size= image_sizes[i]
for row in range(len(all_path)) :
image_id_yolo = all_path[row].split("/")[-1].split(".")[0]
if image_id_yolo==image_id:
boxes_yolo_init = all_bboxex[row]
scores_yolo_init = all_score[row]
boxes_yolo, scores_yolo, labels_yolo = yolo_organize(boxes_yolo_init,scores_yolo_init)
 boxes_effdet, scores_effdet, labels_effdet = effdet_organize(predictions, image_index=i)
 boxes, scores, labels = run_wbf_ensemble(boxes_effdet, boxes_yolo, scores_effdet, scores_yolo, labels_effdet,
labels_yolo, iou_thr=0.41, skip_box_thr=0.4, weights=None)
 boxes =(boxes*(image_size/1024)).round().astype(np.int32 ).clip(min=0, max=image_size-1)
 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)
import xgboost as xgb
import lightgbm as lgb<split>
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head(10 )
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2 )<choose_model_class>
for images, image_ids, image_sizes in data_loader:
predictions = make_tta_models_predictions(images)
 predictions=resize_predicitions(predictions)
 for i, image in enumerate(images):
image_id=image_ids[i]
image_size= image_sizes[i]
for row in range(len(all_path)) :
image_id_yolo = all_path[row].split("/")[-1].split(".")[0]
if image_id_yolo==image_id:
boxes_yolo = all_bboxex[row]
scores_yolo = all_score[row]
boxes_yolo, scores_yolo, labels_yolo = yolo_organize(boxes_yolo,scores_yolo)
 boxes_effdet, scores_effdet, labels_effdet = effdet_organize(predictions, image_index=i)
 boxes, scores, labels = run_wbf_ensemble(boxes_effdet, boxes_yolo, scores_effdet, scores_yolo, labels_effdet,
labels_yolo, iou_thr=0.41, skip_box_thr=0.4, weights=None)
 boxes =(boxes/2 ).round().astype(np.int32 ).clip(min=0, max=512)
 fig, ax = plt.subplots(1, 1, figsize=(10, 10))
sample = images[i].permute(1,2,0 ).cpu().numpy()
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color =(0, 0, 1)
 thickness = 2
for box,score in zip(boxes,scores):
cv2.rectangle(sample,(box[0], box[1]),(box[2], box[3]),(0, 0, 1), 1)
 cv2.putText(sample, '{:.2}'.format(score),(box[0]+np.random.randint(20),box[1]), font, fontScale, color, thickness, cv2.LINE_AA)
 ax.set_axis_off()
ax.imshow(sample);
break