Kaggle competition: Global Wheat Detection
<SOS> metric: custom metric Kaggle data source: global-wheat-detection<find_best_params>
import numpy as np
import pandas as pd
import os
from tqdm.auto import tqdm
import shutil as sh
result_lgbm.best_params_<init_hyperparams>
!cp -r.. /input/yolov5train/* .
params = {
'num_leaves': result_lgbm.best_params_["num_leaves"],
'min_data_in_leaf': result_lgbm.best_params_["min_data_in_leaf"],
'max_depth': result_lgbm.best_params_["max_depth"],
'learning_rate': result_lgbm.best_params_["learning_rate"],
"boosting": "gbdt",
"feature_fraction": result_lgbm.best_params_["feature_fraction"],
"bagging_freq": 1,
"bagging_fraction": result_lgbm.best_params_["bagging_fraction"],
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": result_lgbm.best_params_["lambda_l1"],
"lambda_l2": result_lgbm.best_params_["lambda_l2"],
"verbosity": -1
}
xtrain, xvalid, ytrain, yvalid = train_test_split(X, y, random_state=42, test_size=0.3)
 predictions_test_lgb_tuned = np.zeros(len(test_df))
num_fold = 0
oof_rmse = 0
num_of_splits = kfold_splits
folds = KFold(n_splits=num_of_splits, shuffle=False, random_state = 42)
 for train_index, valid_index in folds.split(xtrain, ytrain):
xtrain_stra, xvalid_stra = xtrain.iloc[train_index,:], xtrain.iloc[valid_index,:]
ytrain_stra, yvalid_stra = ytrain.iloc[train_index], ytrain.iloc[valid_index]
print()
print("Fold:", num_fold)
 print()
model_lgb_tuned = lgb.LGBMRegressor(**params, n_estimators = 20000, nthread = 4, n_jobs = -1)
 model_lgb_tuned.fit(xtrain_stra, ytrain_stra,
eval_set=[(xtrain_stra, ytrain_stra),(xvalid_stra, yvalid_stra)], eval_metric='rmse',
verbose=100, early_stopping_rounds=1000)
 predictions_valid = model_lgb_tuned.predict(xvalid)
 rmse_valid = np.sqrt(mean_squared_error(yvalid, predictions_valid))
print("Fold" ,num_fold, "xvalid rmse:", rmse_valid)
 num_fold = num_fold + 1
oof_rmse += rmse_valid
predictions_test_lgb_tuned += model_lgb_tuned.predict(test_df[xtrain.columns])/num_of_splits
predictions_test_lgb_tuned = np.expm1(predictions_test_lgb_tuned)
 print()
print(predictions_test_lgb_tuned)
 print("OOF Out-of-fold rmse:", oof_rmse/num_of_splits)
 f, ax = plt.subplots(2, figsize=(12,7))
sns.set(rc={'figure.figsize':(9,14)})
 sns.distplot(train_df.revenue, ax=ax[0])
 ax[0].set_title("Train Set Revenue Histogram")
 sns.distplot(predictions_test_lgb_tuned, ax=ax[1])
 ax[1].set_title("Test Set Revenue Prediction Histogram")
 f.tight_layout() 
<features_selection>
!pip install --no-deps '.. /input/weightedboxesfusion/' > /dev/null
max_selected_features = 200
sel = SelectFromModel(clf_stra_xgb, max_features = max_selected_features, threshold=0.005, prefit=True)
 feature_idx = sel.get_support()
selected_features_xgb = X.columns[feature_idx]<split>
def convertTrainLabel() :
df = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
df[column] = bboxs[:,i]
df.drop(columns=['bbox'], inplace=True)
 df['x_center'] = df['x'] + df['w']/2
df['y_center'] = df['y'] + df['h']/2
df['classes'] = 0
df = df[['image_id','x', 'y', 'w', 'h','x_center','y_center','classes']]
index = list(set(df.image_id))
source = 'train'
if True:
for fold in [0]:
val_index = index[len(index)*fold//5:len(index)*(fold+1)//5]
for name,mini in tqdm(df.groupby('image_id')) :
if name in val_index:
path2save = 'val2017/'
else:
path2save = 'train2017/'
if not os.path.exists('convertor/fold{}/labels/'.format(fold)+path2save):
os.makedirs('convertor/fold{}/labels/'.format(fold)+path2save)
 with open('convertor/fold{}/labels/'.format(fold)+path2save+name+".txt", 'w+')as f:
row = mini[['classes','x_center','y_center','w','h']].astype(float ).values
row = row/1024
row = row.astype(str)
 for j in range(len(row)) :
text = ' '.join(row[j])
 f.write(text)
 f.write("
")
 if not os.path.exists('convertor/fold{}/images/{}'.format(fold,path2save)) :
os.makedirs('convertor/fold{}/images/{}'.format(fold,path2save))
sh.copy(".. /input/global-wheat-detection/{}/{}.jpg".format(source,name),'convertor/fold{}/images/{}/{}.jpg'.format(fold,path2save,name))
xtrain, xvalid, ytrain, yvalid = train_test_split(X[selected_features_xgb], y, random_state=42, test_size=0.3)
 start_time = time.time()
predictions_test_xgb_fs = np.zeros(len(test_df))
num_fold = 0
oof_rmse = 0
num_of_splits = kfold_splits
folds = KFold(n_splits=num_of_splits, shuffle=False, random_state = 42)
 for train_index, valid_index in folds.split(xtrain, ytrain):
xtrain_stra, xvalid_stra = xtrain.iloc[train_index,:], xtrain.iloc[valid_index,:]
ytrain_stra, yvalid_stra = ytrain.iloc[train_index], ytrain.iloc[valid_index]
print()
print("Fold:", num_fold)
 print()
clf_stra_fs_xgb = xgb.XGBRegressor(n_estimators=10000, seed=42, nthread = -1)
 clf_stra_fs_xgb.fit(xtrain_stra, ytrain_stra, eval_set=[(xtrain_stra, ytrain_stra),(xvalid_stra, yvalid_stra)],
early_stopping_rounds=1000, eval_metric='rmse', verbose=100)
 predictions_valid = clf_stra_fs_xgb.predict(xvalid)
 rmse_valid = np.sqrt(mean_squared_error(yvalid, predictions_valid))
print("Fold" ,num_fold, "xvalid rmse:", rmse_valid)
 num_fold = num_fold + 1
oof_rmse += rmse_valid
predictions_test_xgb_fs += clf_stra_fs_xgb.predict(test_df[xtrain.columns])/num_of_splits
predictions_test_xgb_fs = np.expm1(predictions_test_xgb_fs)
 print(predictions_test_xgb_fs)
 print("OOF Out-of-fold rmse:", oof_rmse/num_of_splits)
 f, ax = plt.subplots(2, figsize=(12,7))
sns.set(rc={'figure.figsize':(9,14)})
 sns.distplot(train_df.revenue, ax=ax[0])
 ax[0].set_title("Train Set Revenue Histogram")
 sns.distplot(predictions_test_xgb_fs, ax=ax[1])
 ax[1].set_title("Test Set Revenue Prediction Histogram")
 f.tight_layout()
xgb.plot_importance(clf_stra_fs_xgb )<compute_test_metric>
def run_wbf(boxes, scores, image_size=1023, iou_thr=0.5, skip_box_thr=0.7, weights=None):
labels = [np.zeros(score.shape[0])for score in scores]
boxes = [box/(image_size)for box in boxes]
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)
 return boxes, scores, labels
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)
 def detect1Image(im0, imgsz, model, device, conf_thres, iou_thres, aug = False):
img = letterbox(im0, new_shape=imgsz)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
 img = np.ascontiguousarray(img)
 img = torch.from_numpy(img ).to(device)
 img = img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 pred = model(img, augment=aug)[0]
pred = non_max_suppression(pred, conf_thres, iou_thres)
 boxes = []
scores = []
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:
boxes.append([int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])])
 scores.append(conf)
 return np.array(boxes), np.array(scores )
result = bayesian_tuning(xtrain, ytrain )<split>
class BaseWheatTTA:

 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):

 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):

 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):

 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[:, [3,1]]
res_boxes[:, [1,3]] = boxes[:, [0,2]]
return res_boxes
class TTACompose(BaseWheatTTA):

 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 )
xtrain, xvalid, ytrain, yvalid = train_test_split(X[selected_features_xgb], y, random_state=42, test_size=0.3)
 predictions_test_xgb_fs_tuned = np.zeros(len(test_df))
num_fold = 0
oof_rmse = 0
num_of_splits = kfold_splits
folds = KFold(n_splits=num_of_splits, shuffle=False, random_state = 42)
 for train_index, valid_index in folds.split(xtrain, ytrain):
xtrain_stra, xvalid_stra = xtrain.iloc[train_index,:], xtrain.iloc[valid_index,:]
ytrain_stra, yvalid_stra = ytrain.iloc[train_index], ytrain.iloc[valid_index]
print()
print("Fold:", num_fold)
 print()
clf_stra_fs_tuned_xgb = xgb.XGBRegressor(colsample_bytree = result.best_params_["colsample_bytree"],
gamma=result.best_params_["gamma"],
learning_rate=result.best_params_["learning_rate"],
max_depth=result.best_params_["max_depth"],
min_child_weight=result.best_params_["min_child_weight"],
n_estimators=10000,
reg_alpha=result.best_params_["reg_alpha"],
reg_lambda=result.best_params_["reg_lambda"],
subsample=result.best_params_["subsample"],
seed=42,
nthread = -1)
 clf_stra_fs_tuned_xgb.fit(xtrain_stra, ytrain_stra, eval_set=[(xtrain_stra, ytrain_stra),(xvalid_stra, yvalid_stra)],
early_stopping_rounds=1000, eval_metric='rmse', verbose=100)
 predictions_valid = clf_stra_fs_tuned_xgb.predict(xvalid)
 rmse_valid = np.sqrt(mean_squared_error(yvalid, predictions_valid))
print("Fold" ,num_fold, "xvalid rmse:", rmse_valid)
 num_fold = num_fold + 1
oof_rmse += rmse_valid
predictions_test_xgb_fs_tuned += clf_stra_fs_tuned_xgb.predict(test_df[xtrain.columns])/num_of_splits
predictions_test_xgb_fs_tuned = np.expm1(predictions_test_xgb_fs_tuned)
 print(predictions_test_xgb_fs_tuned)
 print("OOF Out-of-fold rmse:", oof_rmse/num_of_splits)
 f, ax = plt.subplots(2, figsize=(12,7))
sns.set(rc={'figure.figsize':(9,14)})
 sns.distplot(train_df.revenue, ax=ax[0])
 ax[0].set_title("Train Set Revenue Histogram")
 sns.distplot(predictions_test_xgb_fs_tuned, ax=ax[1])
 ax[1].set_title("Test Set Revenue Prediction Histogram")
 f.tight_layout()
xgb.plot_importance(clf_stra_fs_tuned_xgb )<features_selection>
transform = TTACompose([
TTAVerticalFlip() ,
TTARotate90() ,
])
 transform_img = A.Compose([
ToTensorV2(p=1.0), ] )
max_selected_features = 200
sel = SelectFromModel(model_lgb, max_features = max_selected_features, threshold=0.005, prefit=True)
 feature_idx = sel.get_support()
selected_features_lgb = X.columns[feature_idx]<split>
tta_transforms = []
for tta_combination in product([TTAHorizontalFlip() , None],
[TTAVerticalFlip() , None],
[TTARotate90() , None]):
tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))
params = {
"metric": 'rmse',
"verbosity": -1
}
xtrain, xvalid, ytrain, yvalid = train_test_split(X[selected_features_lgb], y, random_state=42, test_size=0.3)
 predictions_test_lgb_fs = np.zeros(len(test_df))
num_fold = 0
oof_rmse = 0
num_of_splits = kfold_splits
folds = KFold(n_splits=num_of_splits, shuffle=False, random_state = 42)
 for train_index, valid_index in folds.split(xtrain, ytrain):
xtrain_stra, xvalid_stra = xtrain.iloc[train_index,:], xtrain.iloc[valid_index,:]
ytrain_stra, yvalid_stra = ytrain.iloc[train_index], ytrain.iloc[valid_index]
print()
print("Fold:", num_fold)
 print()
model_lgb_fs = lgb.LGBMRegressor(**params, n_estimators = 20000, nthread = 4, n_jobs = -1)
 model_lgb_fs.fit(xtrain_stra, ytrain_stra,
eval_set=[(xtrain_stra, ytrain_stra),(xvalid_stra, yvalid_stra)], eval_metric='rmse',
verbose=100, early_stopping_rounds=1000)
 predictions_valid = model_lgb_fs.predict(xvalid)
 rmse_valid = np.sqrt(mean_squared_error(yvalid, predictions_valid))
print("Fold" ,num_fold, "xvalid rmse:", rmse_valid)
 num_fold = num_fold + 1
oof_rmse += rmse_valid
predictions_test_lgb_fs += model_lgb_fs.predict(test_df[xtrain.columns])/num_of_splits
predictions_test_lgb_fs = np.expm1(predictions_test_lgb_fs)
 print()
print(predictions_test_lgb_fs)
 print("OOF Out-of-fold rmse:", oof_rmse/num_of_splits)
 f, ax = plt.subplots(2, figsize=(12,7))
sns.set(rc={'figure.figsize':(9,14)})
 sns.distplot(train_df.revenue, ax=ax[0])
 ax[0].set_title("Train Set Revenue Histogram")
 sns.distplot(predictions_test_lgb_fs, ax=ax[1])
 ax[1].set_title("Test Set Revenue Prediction Histogram")
 f.tight_layout() 
<compute_test_metric>
def makePseudolabel() :
source = '.. /input/global-wheat-detection/test/'
weights = '.. /input/otherweight/best_yolov5x_fold0.pt'
imgsz = 1024
conf_thres = 0.5
iou_thres = 0.6
is_TTA = True
imagenames = os.listdir(source)
 device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
dataset = LoadImages(source, img_size=imgsz)
 path2save = 'train2017/'
if not os.path.exists('convertor/fold0/labels/'+path2save):
os.makedirs('convertor/fold0/labels/'+path2save)
 if not os.path.exists('convertor/fold0/images/{}'.format(path2save)) :
os.makedirs('convertor/fold0/images/{}'.format(path2save))
for name in imagenames:
image_id = name.split('.')[0]
im01 = cv2.imread('%s/%s.jpg'%(source,image_id))
if im01.shape[0]!=1024 or im01.shape[1]!=1024:
continue
assert im01 is not None, 'Image Not Found '
im_w, im_h = im01.shape[:2]
if is_TTA:
enboxes = []
enscores = []
for i in range(4):
im0 = TTAImage(im01, i)
 boxes, scores = detect1Image(im0, imgsz, model, device, conf_thres, iou_thres)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, im_w, im_h)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size = im_w, iou_thr=0.6, skip_box_thr=0.43)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=im_w)
 else:
boxes, scores = detect1Image(im01, imgsz, model, device, conf_thres, iou_thres)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
boxes = boxes[scores >= 0.05].astype(np.int32)
 scores = scores[scores >=float(0.05)]
lineo = ''
for box in boxes:
x1, y1, w, h = box
xc, yc, w, h =(x1+w/2)/1024,(y1+h/2)/1024, w/1024, h/1024
lineo += '0 %f %f %f %f
'%(xc, yc, w, h)
 fileo = open('convertor/fold0/labels/'+path2save+image_id+".txt", 'w+')
 fileo.write(lineo)
 fileo.close()
sh.copy(".. /input/global-wheat-detection/test/{}.jpg".format(image_id),'convertor/fold0/images/{}/{}.jpg'.format(path2save,image_id))
result_lgb = bayesian_tuning_lgb(xtrain, ytrain )<init_hyperparams>
!rm -rf convertor
params = {
'num_leaves': result_lgbm.best_params_["num_leaves"],
'min_data_in_leaf': result_lgbm.best_params_["min_data_in_leaf"],
'max_depth': result_lgbm.best_params_["max_depth"],
'learning_rate': result_lgbm.best_params_["learning_rate"],
"boosting": "gbdt",
"feature_fraction": result_lgbm.best_params_["feature_fraction"],
"bagging_freq": 1,
"bagging_fraction": result_lgbm.best_params_["bagging_fraction"],
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": result_lgbm.best_params_["lambda_l1"],
"lambda_l2": result_lgbm.best_params_["lambda_l2"],
"verbosity": -1
}
xtrain, xvalid, ytrain, yvalid = train_test_split(X[selected_features_lgb], y, random_state=42, test_size=0.3)
 predictions_test_lgb_fs_tuned = np.zeros(len(test_df))
num_fold = 0
oof_rmse = 0
num_of_splits = kfold_splits
folds = KFold(n_splits=num_of_splits, shuffle=False, random_state = 42)
 for train_index, valid_index in folds.split(xtrain, ytrain):
xtrain_stra, xvalid_stra = xtrain.iloc[train_index,:], xtrain.iloc[valid_index,:]
ytrain_stra, yvalid_stra = ytrain.iloc[train_index], ytrain.iloc[valid_index]
print()
print("Fold:", num_fold)
 print()
model_lgb_fs_tuned = lgb.LGBMRegressor(**params, n_estimators = 20000, nthread = 4, n_jobs = -1)
 model_lgb_fs_tuned.fit(xtrain_stra, ytrain_stra,
eval_set=[(xtrain_stra, ytrain_stra),(xvalid_stra, yvalid_stra)], eval_metric='rmse',
verbose=100, early_stopping_rounds=1000)
 predictions_valid = model_lgb_fs_tuned.predict(xvalid)
 rmse_valid = np.sqrt(mean_squared_error(yvalid, predictions_valid))
print("Fold" ,num_fold, "xvalid rmse:", rmse_valid)
 num_fold = num_fold + 1
oof_rmse += rmse_valid
predictions_test_lgb_fs_tuned += model_lgb_fs_tuned.predict(test_df[xtrain.columns])/num_of_splits
predictions_test_lgb_fs_tuned = np.expm1(predictions_test_lgb_fs_tuned)
 print()
print(predictions_test_lgb_fs_tuned)
 print("OOF Out-of-fold rmse:", oof_rmse/num_of_splits)
 f, ax = plt.subplots(2, figsize=(12,7))
sns.set(rc={'figure.figsize':(9,14)})
 sns.distplot(train_df.revenue, ax=ax[0])
 ax[0].set_title("Train Set Revenue Histogram")
 sns.distplot(predictions_test_lgb_fs_tuned, ax=ax[1])
 ax[1].set_title("Test Set Revenue Prediction Histogram")
 f.tight_layout() 
<define_variables>
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 )
predictions_test_xgb_extra_trees =(0.5 * predictions_test_xgb)+(0.5 * predictions_extra_trees_test)
 predictions_test_xgb_random_forest =(0.5 * predictions_test_xgb)+(0.5 * predictions_random_forest_test)
 predictions_test_extra_trees_random_forest =(0.5 * predictions_extra_trees_test)+(0.5 * predictions_random_forest_test)
 predictions_test_tuned_xgb_extra_trees =(0.5 * predictions_test_xgb_tuned)+(0.5 * predictions_extra_trees_tuned_test)
 predictions_test_tuned_xgb_random_forest =(0.5 * predictions_test_xgb_tuned)+(0.5 * predictions_random_forest_tuned_test)
 predictions_test_tuned_extra_trees_random_forest =(0.5 * predictions_extra_trees_tuned_test)+(0.5 * predictions_random_forest_tuned_test)
 predictions_test_xgb_fs_extra_trees =(0.5 * predictions_test_xgb_fs)+(0.5 * predictions_extra_trees_test)
 predictions_test_xgb_fs_tuned_extra_trees =(0.5 * predictions_test_xgb_fs_tuned)+(0.5 * predictions_extra_trees_test)
 predictions_test_baseline_xgb_tuned_extra_trees =(0.5 * predictions_test_xgb)+(0.5 * predictions_extra_trees_tuned_test)
 predictions_test_lgb_xgb =(0.5 * predictions_test_lgb)+(0.5 * predictions_test_xgb)
 predictions_test_tuned_lgb_xgb =(0.5 * predictions_test_lgb_tuned)+(0.5 * predictions_test_xgb_tuned)
 predictions_test_lgb_extra_trees =(0.5 * predictions_test_lgb)+(0.5 * predictions_extra_trees_test)
 predictions_test_lgb_random_forest =(0.5 * predictions_test_lgb)+(0.5 * predictions_random_forest_test)
 predictions_test_tuned_lgb_extra_trees =(0.5 * predictions_test_lgb_tuned)+(0.5 * predictions_extra_trees_tuned_test)
 predictions_test_tuned_lgb_random_forest =(0.5 * predictions_test_lgb_tuned)+(0.5 * predictions_random_forest_tuned_test)
 predictions_test_xgb_fs_lgb =(0.5 * predictions_test_xgb_fs)+(0.5 * predictions_test_lgb_fs)
 predictions_test_xgb_fs_tuned_lgb =(0.5 * predictions_test_xgb_fs_tuned)+(0.5 * predictions_test_lgb_fs_tuned)
 predictions_test_baseline_lgb_tuned_extra_trees =(0.5 * predictions_test_lgb)+(0.5 * predictions_extra_trees_tuned_test )<save_to_csv>
def detect() :
source = '.. /input/global-wheat-detection/test/'
weights = 'weights/best.pt'
if not os.path.exists(weights):
weights = '.. /input/weight/yolo5x.pt'
weight1 = ".. /input/otherweight/best_yolov5x_fold0.pt"
imgsz = 1024
conf_thres = 0.5
iou_thres = 0.6
is_TTA = True
is_FLIP = True
imagenames = os.listdir(source)
 device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
model1 = torch.load(weight1, map_location=device)['model'].float()
model1.to(device ).eval()
dataset = LoadImages(source, img_size=imgsz)
 results = []
fig, ax = plt.subplots(5, 2, figsize=(30, 70))
count = 0
for name in imagenames:
image_id = name.split('.')[0]
im01 = cv2.imread('%s/%s.jpg'%(source,image_id))
assert im01 is not None, 'Image Not Found '
im_w, im_h = im01.shape[:2]
if is_TTA:
enboxes = []
enscores = []
for i in range(4):
im0 = TTAImage(im01, i)
 boxes, scores = detect1Image(im0, imgsz, model, device, conf_thres, iou_thres)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, im_w, im_h)
 if 1:
enboxes.append(boxes)
 enscores.append(scores)
 model1
for i in range(4):
im0 = TTAImage(im01, i)
 boxes, scores = detect1Image(im0, imgsz, model1, device, conf_thres, iou_thres)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, im_w, im_h)
 if 1:
enboxes.append(boxes)
 enscores.append(scores)
 if is_FLIP:
for tta_transform in tta_transforms:
transformed = transform_img(image=im01)
 image = transformed['image']
image_aug = tta_transform.augment(image)
 im0 = image_aug.permute(1,2,0 ).cpu().numpy().copy()
boxes, scores = detect1Image(im0, imgsz, model, device, conf_thres, iou_thres)
 if boxes != []:
boxes = tta_transform.deaugment_boxes(boxes.copy())
 enboxes.append(boxes)
 enscores.append(scores)
 for tta_transform in tta_transforms:
transformed = transform_img(image=im01)
 image = transformed['image']
image_aug = tta_transform.augment(image)
 im0 = image_aug.permute(1,2,0 ).cpu().numpy().copy()
boxes, scores = detect1Image(im0, imgsz, model1, device, conf_thres, iou_thres)
 if boxes != []:
boxes = tta_transform.deaugment_boxes(boxes.copy())
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(im01, imgsz, model, device, conf_thres, iou_thres, aug = True)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(im01, imgsz, model1, device, conf_thres, iou_thres, aug = True)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size = im_w, iou_thr=0.6, skip_box_thr=0.5)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=im_w)
 else:
boxes, scores = detect1Image(im01, imgsz, model, device, conf_thres, iou_thres)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
boxes = boxes[scores >= 0.17].astype(np.int32)
 scores = scores[scores >=float(0.17)]
if count<10:
for box, score in zip(boxes,scores):
cv2.rectangle(im01, 
(box[0], box[1]), 
(box[2]+box[0], box[3]+box[1]), 
(220, 0, 0), 2)
 cv2.putText(im01, '%.2f'%(score),(box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX ,
0.5,(255,255,255), 2, cv2.LINE_AA)
 ax[count%5][count//5].imshow(im01)
 count+=1
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 return results
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb
submission.to_csv('clf_xgb_baseline.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_tuned
submission.to_csv('clf_xgb_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb
submission.to_csv('clf_lgb_baseline.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb_tuned
submission.to_csv('clf_lgb_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_extra_trees_test
submission.to_csv('clf_extra_trees_baseline.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_extra_trees_tuned_test
submission.to_csv('clf_extra_trees_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_fs
submission.to_csv('clf_xgb_fs_baseline.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_fs_tuned
submission.to_csv('clf_xgb_fs_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb_fs
submission.to_csv('clf_lgb_fs_baseline.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb_fs_tuned
submission.to_csv('clf_lgb_fs_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_extra_trees
submission.to_csv('blend_xgb_extra_trees_baselines.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_random_forest
submission.to_csv('blend_xgb_random_forest_baselines.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_extra_trees_random_forest
submission.to_csv('blend_extra_trees_random_forest_baselines.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_tuned_xgb_extra_trees
submission.to_csv('blend_xgb_extra_trees_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_tuned_xgb_random_forest
submission.to_csv('blend_xgb_random_forest_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_tuned_extra_trees_random_forest
submission.to_csv('blend_extra_trees_random_forest_tuned.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_baseline_xgb_tuned_extra_trees
submission.to_csv('blend_baseline_xgb_tuned_extra_trees.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_fs_extra_trees
submission.to_csv('blend_xgb_fs_extra_trees.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_fs_tuned_extra_trees
submission.to_csv('blend_xgb_fs_tuned_extra_trees.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb_xgb
submission.to_csv('blend_lgb_xgb.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_tuned_lgb_xgb
submission.to_csv('blend_tuned_lgb_xgb.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb_extra_trees
submission.to_csv('blend_tuned_lgb_extra_trees.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb_extra_trees
submission.to_csv('blend_tuned_lgb_extra_trees.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_lgb_random_forest
submission.to_csv('blend_tuned_lgb_random_forest.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_tuned_lgb_extra_trees
submission.to_csv('blend_tuned_lgb_extra_trees.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_tuned_lgb_random_forest
submission.to_csv('blend_tuned_lgb_random_forest.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_baseline_lgb_tuned_extra_trees
submission.to_csv('blend_baseline_lgb_tuned_extra_trees.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_fs_lgb
submission.to_csv('blend_xgb_fs_lgb.csv', index=False)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['revenue'] = predictions_test_xgb_fs_tuned_lgb
submission.to_csv('blend_xgb_fs_tuned_lgb.csv', index=False)
<set_options>
results = detect()
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()