Kaggle competition: Global Wheat Detection
for col in tqdm_notebook(train.columns):
if train[col].dtype == 'object':
le = LabelEncoder()
le.fit(list(train[col].astype(str ).values)+ list(test[col].astype(str ).values))
train[col] = le.transform(list(train[col].astype(str ).values))
test[col] = le.transform(list(test[col].astype(str ).values))<data_type_conversions>
weights = [".. /input/40efolds/best48fold0.pt", ".. /input/40efolds/best48fold1.pt", ".. /input/40efolds/best48fold2.pt", ".. /input/40efolds/best48fold3.pt", ".. /input/40efolds/best40fold4.pt"]
print(len(weights))
source = ".. /input/global-wheat-detection/test"
imgsz = 1024
conf_t = 0.5
iou_t = 0.8
built_in_tta = True
device = torch_utils.select_device("0" if torch.cuda.is_available() else "")
 half = device.type != 'cpu'
train['d2_d4_comb'] = train['D2'].astype(str)+ '_' +train['D4'].astype(str)
 test['d2_d4_comb'] = test['D2'].astype(str)+ '_' +test['D4'].astype(str )<data_type_conversions>
skip_box_thr = 0.43
iou_thr = 0.6
score_thr = 0.25
ws = [0.15, 0.15, 0.27, 0.15, 0.28]
def run_wbf(boxes,scores, image_size=1024, 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
for feature in ['id_02__id_20', 'id_02__D8', 'D11__DeviceInfo', 'DeviceInfo__P_emaildomain', 'P_emaildomain__C2',
'card2__dist1', 'card1__card5', 'card2__id_20', 'card5__P_emaildomain', 'addr1__card1']:
f1, f2 = feature.split('__')
 train[feature] = train[f1].astype(str)+ '_' + train[f2].astype(str)
 test[feature] = test[f1].astype(str)+ '_' + test[f2].astype(str)
 le = LabelEncoder()
le.fit(list(train[feature].astype(str ).values)+ list(test[feature].astype(str ).values))
train[feature] = le.transform(list(train[feature].astype(str ).values))
test[feature] = le.transform(list(test[feature].astype(str ).values))<data_type_conversions>
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(img, img0, model, device, aug):
img = img.transpose(2,0,1)
 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=aug)[0]
pred = non_max_suppression(pred, conf_t, iou_t, merge=True, classes=None, agnostic=False)
 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], img0.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 )
def uid_fe(df):
df['uid'] = df['card1'].astype(str)+'_'+df['card2'].astype(str)
 df['uid2'] = df['uid'].astype(str)+'_'+df['card3'].astype(str)+'_'+df['card5'].astype(str)
 df['uid3'] = df['uid2'].astype(str)+'_'+df['addr1'].astype(str)+'_'+df['addr2'].astype(str)
 df['uid4'] = df['uid3'].astype(str)+'_'+df['P_emaildomain'].astype(str)
 df['uid5'] = df['uid3'].astype(str)+'_'+df['R_emaildomain'].astype(str)
 return df
train = uid_fe(train)
 test = uid_fe(test )<categorify>
def detect() :
models = []
for w in weights if isinstance(weights, list)else [weights]:
models.append(torch.load(w, map_location=device)['model'].to(device ).float().eval())
 dataset = LoadImages(source, img_size=imgsz)
 img = torch.zeros(( 1, 3, imgsz, imgsz), device=device)
 for model in models:
if half:
model.half()
_ = model(img.half() if half else img)if device.type != 'cpu' else None
all_paths, all_bboxes, all_confs = [], [], []
for p, img, img0, _ in dataset:
print()
img = img.transpose(1,2,0)
 all_paths.append(p)
 m_box, m_score = [], []
for model in models:
enboxes = []
enscores = []
for i in range(4):
img1 = TTAImage(img, i)
 boxes, scores = detect1Image(img1, img0, model, device, aug=False)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(img, img0, model, device, aug=True)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = run_wbf(enboxes, enscores, image_size=1024, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=1023)
 indices = scores >= score_thr
boxes = boxes[indices]
scores = scores[indices]
m_box.append(boxes)
 m_score.append(scores)
 all_bboxes.append(m_box)
 all_confs.append(m_score)
 return all_paths, all_bboxes, all_confs
def andrew_fe(df):
df['TransactionAmt_to_mean_card1'] = df['TransactionAmt'] / df.groupby(['card1'])['TransactionAmt'].transform('mean')
 df['TransactionAmt_to_mean_card4'] = df['TransactionAmt'] / df.groupby(['card4'])['TransactionAmt'].transform('mean')
 df['TransactionAmt_to_std_card1'] = df['TransactionAmt'] / df.groupby(['card1'])['TransactionAmt'].transform('std')
 df['TransactionAmt_to_std_card4'] = df['TransactionAmt'] / df.groupby(['card4'])['TransactionAmt'].transform('std')
 df['id_02_to_mean_card1'] = df['id_02'] / df.groupby(['card1'])['id_02'].transform('mean')
 df['id_02_to_mean_card4'] = df['id_02'] / df.groupby(['card4'])['id_02'].transform('mean')
 df['id_02_to_std_card1'] = df['id_02'] / df.groupby(['card1'])['id_02'].transform('std')
 df['id_02_to_std_card4'] = df['id_02'] / df.groupby(['card4'])['id_02'].transform('std')
 df['D15_to_mean_card1'] = train['D15'] / df.groupby(['card1'])['D15'].transform('mean')
 df['D15_to_mean_card4'] = train['D15'] / df.groupby(['card4'])['D15'].transform('mean')
 df['D15_to_std_card1'] = train['D15'] / df.groupby(['card1'])['D15'].transform('std')
 df['D15_to_std_card4'] = train['D15'] / df.groupby(['card4'])['D15'].transform('std')
 df['D15_to_mean_addr1'] = df['D15'] / df.groupby(['addr1'])['D15'].transform('mean')
 df['D15_to_mean_addr2'] = df['D15'] / df.groupby(['addr2'])['D15'].transform('mean')
 df['D15_to_std_addr1'] = df['D15'] / df.groupby(['addr1'])['D15'].transform('std')
 df['D15_to_std_addr2'] = df['D15'] / df.groupby(['addr2'])['D15'].transform('std')
 return df
train = andrew_fe(train)
 test = andrew_fe(test )<categorify>
with torch.no_grad() :
res = detect()
paths,all_boxes,all_confs = res
def product_fe(df):
df["card1_count"] = df.groupby(["card1"])["ProductCD"].transform("size")
 df["card2_count"] = df.groupby(["card2"])["ProductCD"].transform("size")
 df["card5_count"] = df.groupby(["card5"])["ProductCD"].transform("size")
 df["R_email_count"] = df.groupby(["R_emaildomain"])["ProductCD"].transform("size")
 df["P_email_count"] = df.groupby(["P_emaildomain"])["ProductCD"].transform("size")
 df["P_R_emails_count"] = df.groupby(["P_emaildomain","R_emaildomain"])["ProductCD"].transform("size")
 df["addr1_count"] = df.groupby(["addr1"])["ProductCD"].transform("size")
 df["addr2_count"] = df.groupby(["addr2"])["ProductCD"].transform("size")
 df["joint_addr1_card1_count"] = df.groupby(["addr1","card1"])["ProductCD"].transform("size")
 df["joint_matches_count"] = df.groupby(["M4","M5", "M6"])["ProductCD"].transform("size")
 df["joint_d2_d4_count"] = df.groupby(['d2_d4_comb'])["ProductCD"].transform("size")
 df["joint_card1_d2_d4_count"] = df.groupby(["card1","d2_d4_comb"])["ProductCD"].transform("size")
 df["uid_pd5_count"] = df.groupby(["uid5"])["ProductCD"].transform("size")
 df["uid_pd5_count"] = df.groupby(["uid5"])["ProductCD"].transform("size")
 return df
train = product_fe(train)
 test = product_fe(test )<data_type_conversions>
resultsYOLO =dict()
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)
 bts = []
sts = []
for row in range(len(paths)) :
image_id = paths[row].split("/")[-1].split(".")[0]
boxes, scores = run_wbf(all_boxes[row],all_confs[row], skip_box_thr = skip_box_thr, iou_thr = iou_thr, weights = None)
 boxes =(boxes*1024/1024 ).astype(np.int32 ).clip(min=0, max=1023)
 resultsYOLO[image_id] = [boxes, scores]
!rm -rf *
def decimal_fe(df):
df['TransactionAmt_decimal'] =(( df['TransactionAmt'] - df['TransactionAmt'].astype(int)) * 1000 ).astype(int)
 df['TransactionAmt_decimal_to_mean_card1'] = df['TransactionAmt_decimal'] / train.groupby(['card1'])['TransactionAmt_decimal'].transform('mean')
 df['TransactionAmt_decimal_to_mean_card2'] = df['TransactionAmt_decimal'] / train.groupby(['card2'])['TransactionAmt_decimal'].transform('mean')
 df['TransactionAmt_decimal_to_mean_card4'] = df['TransactionAmt_decimal'] / train.groupby(['card4'])['TransactionAmt_decimal'].transform('mean')
 df['TransactionAmt_decimal_to_std_card1'] = df['TransactionAmt_decimal'] / train.groupby(['card1'])['TransactionAmt_decimal'].transform('std')
 df['TransactionAmt_decimal_to_std_card2'] = df['TransactionAmt_decimal'] / train.groupby(['card2'])['TransactionAmt_decimal'].transform('std')
 df['TransactionAmt_decimal_to_std_card4'] = df['TransactionAmt_decimal'] / train.groupby(['card4'])['TransactionAmt_decimal'].transform('std')
 return df
train = decimal_fe(train)
 test = decimal_fe(test)
<drop_column>
sys.path.insert(0, "/kaggle/input/weightedboxesfusion")
train.drop(labels=['TransactionAmt_decimal'],axis=1,inplace=True)
 test.drop(labels=['TransactionAmt_decimal'],axis=1,inplace=True )<feature_engineering>
DATA_DIR = "/kaggle/input/global-wheat-detection"
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
for c in ['P_emaildomain', 'R_emaildomain']:
train[c + '_bin'] = train[c].map(emails)
 test[c + '_bin'] = test[c].map(emails)
 train[c + '_suffix'] = train[c].map(lambda x: str(x ).split('.')[-1])
 test[c + '_suffix'] = test[c].map(lambda x: str(x ).split('.')[-1])
 train[c + '_suffix'] = train[c + '_suffix'].map(lambda x: x if str(x)not in us_emails else 'us')
 test[c + '_suffix'] = test[c + '_suffix'].map(lambda x: x if str(x)not in us_emails else 'us' )<define_variables>
test_df = pd.read_csv(os.path.join(DATA_DIR, "sample_submission.csv"))
test_df.shape
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d' )<feature_engineering>
class WheatDataset(Dataset):
def __init__(self, dataframe, image_dir, transforms=None):
super().__init__()
self.image_ids = dataframe['image_id'].unique()
self.df = dataframe
self.image_dir = image_dir
self.transforms = transforms
def __len__(self)-> int:
return len(self.image_ids)
 def __getitem__(self, idx: int):
image_id = self.image_ids[idx]
image = cv2.imread(f'{self.image_dir}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
records = self.df[self.df['image_id'] == image_id]
if self.transforms:
sample = {"image": image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def setTime(df):
df['DT'] = df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 df['DT_M'] =(df['DT'].dt.year-2017)*12 + df['DT'].dt.month
df['DT_W'] =(df['DT'].dt.year-2017)*52 + df['DT'].dt.weekofyear
df['DT_D'] =(df['DT'].dt.year-2017)*365 + df['DT'].dt.dayofyear
df['DT_hour'] = df['DT'].dt.hour
df['DT_day_week'] = df['DT'].dt.dayofweek
df['DT_day'] = df['DT'].dt.day
df['TransactionAmt_to_mean_hour'] = df['TransactionAmt'] / df.groupby(['DT_hour'])['TransactionAmt'].transform('mean')
 df['TransactionAmt_to_mean_dow'] = df['TransactionAmt'] / df.groupby(['DT_day_week'])['TransactionAmt'].transform('mean')
 df['TransactionAmt_to_std_dow'] = df['TransactionAmt'] / df.groupby(['DT_day_week'])['TransactionAmt'].transform('std')
 df['TransactionAmt_to_std_hour'] = df['TransactionAmt'] / df.groupby(['DT_hour'])['TransactionAmt'].transform('std')
 return df
train = setTime(train)
 test= setTime(test )<categorify>
def fasterrcnn_resnet50_fpn(path,pretrained_backbone=False):
backbone = resnet_fpn_backbone('resnet50', pretrained_backbone)
 model = FasterRCNN(backbone, 2)
 model.load_state_dict(torch.load(path))
model.to(DEVICE)
 model.eval()
return model
for col in ['DT_M','DT_W','DT_D']:
temp_df = pd.concat([train[[col]], test[[col]]])
 fq_encode = temp_df[col].value_counts().to_dict()
train[col+'_total'] = train[col].map(fq_encode)
 test[col+'_total'] = test[col].map(fq_encode )<data_type_conversions>
def get_model_101(path,pretrained=False):
backbone = resnet_fpn_backbone('resnet101', pretrained=pretrained)
 model = FasterRCNN(backbone, num_classes=2)
 in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, 2)
 model.load_state_dict(torch.load(path))
model.to(DEVICE)
 model.eval()
return model
periods = ['DT_M','DT_W','DT_D']
i_cols = ['uid']
for period in periods:
for col in i_cols:
new_column = col + '_' + period
temp_df = pd.concat([train[[col,period]], test[[col,period]]])
 temp_df[new_column] = temp_df[col].astype(str)+ '_' +(temp_df[period] ).astype(str)
 fq_encode = temp_df[new_column].value_counts().to_dict()
train[new_column] =(train[col].astype(str)+ '_' + train[period].astype(str)).map(fq_encode)
 test[new_column] =(test[col].astype(str)+ '_' + test[period].astype(str)).map(fq_encode)
 train[new_column] /= train[period+'_total']
test[new_column] /= test[period+'_total']<concatenate>
model =get_model_101(".. /input/faster-cnn-final-model/fastercnn_resnet_101_145.pth")
 model2 =get_model_101(".. /input/faster-cnn-final-model/fastercnn_resnet_101_145.pth")
 model3=get_model_101(".. /input/faster-cnn-101-155-fix/fastercnn_resnet_101_fix_155.pth")
 model4= get_model_101(".. /input/fastercnn-155/fastercnn_101_155.pth")
 model5= get_model_101(".. /input/faster-cnn-125/fastercnn_resnet_101_125.pth")
 faster_CNN_50_90=fasterrcnn_resnet50_fpn(".. /input/isfix-model/fastercnn_50_fix.pth")
 faster_CNN_50_90_2=fasterrcnn_resnet50_fpn(".. /input/isfix-model/fastercnn_50_fix.pth")
 faster_CNN_50_105=fasterrcnn_resnet50_fpn(".. /input/faster-50-fix-115/fastercnn_50_fix_115.pth")
 models=[model,model3,model4,model5,faster_CNN_50_105,faster_CNN_50_90,faster_CNN_50_90_2
,model2
]
i_cols = ['card1','card2','card3','card5',
'C1','C2','C4','C5','C6','C7','C8','C9','C10','C11','C12','C13','C14',
'D1','D2','D3','D4','D5','D6','D7','D8',
'addr1','addr2',
'dist1',
'P_emaildomain', 'R_emaildomain',
'DeviceInfo',
'id_30','id_33',
'uid','uid2','uid3','uid4','uid5'
]
for col in i_cols:
temp_df = pd.concat([train[[col]], test[[col]]])
 fq_encode = temp_df[col].value_counts(dropna=False ).to_dict()
train[col+'_fq_enc'] = train[col].map(fq_encode)
 test[col+'_fq_enc'] = test[col].map(fq_encode )<data_type_conversions>
def make_ensemble_predictions(images):
images = list(image.to(DEVICE)for image in images)
 result = []
for model in models:
with torch.no_grad() :
outputs = model(images)
 result.append(outputs)
 del model
gc.collect()
torch.cuda.empty_cache()
return result
def run_wbf_ensemble(predictions, image_index, image_size=1024, iou_thr=0.45, skip_box_thr=0.43, weights=None):
boxes = [prediction[image_index]['boxes'].data.cpu().numpy() /(image_size-1)for prediction in predictions]
scores = [prediction[image_index]['scores'].data.cpu().numpy() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0])for prediction in predictions]
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
i_cols = ['M1','M2','M3','M5','M6','M7','M8','M9']
for df in [train, test]:
df['M_sum'] = df[i_cols].sum(axis=1 ).astype(np.int8 )<categorify>
def get_test_transforms() :
return A.Compose([
ToTensorV2(p=1.0)
 ], p=1.0 )
for col in ['ProductCD','M4']:
temp_dict = train.groupby([col])['isFraud'].agg(['mean'] ).reset_index().rename(
 columns={'mean': col+'_target_mean'})
 temp_dict.index = temp_dict[col].values
temp_dict = temp_dict[col+'_target_mean'].to_dict()
train[col+'_target_mean'] = train[col].map(temp_dict)
 test[col+'_target_mean'] = test[col].map(temp_dict )<categorify>
def collate_fn(batch):
return tuple(zip(*batch))
test_dataset = WheatDataset(test_df, os.path.join(DATA_DIR, "test"), get_test_transforms())
 test_data_loader = DataLoader(
 test_dataset,
batch_size=4,
shuffle=False,
num_workers=1,
drop_last=False,
collate_fn=collate_fn
)
for feature in ['card1', 'card2', 'card3', 'card4', 'card5', 'card6', 'id_36']:
train[feature + '_count_full'] = train[feature].map(pd.concat([train[feature], test[feature]], ignore_index=True ).value_counts(dropna=False))
test[feature + '_count_full'] = test[feature].map(pd.concat([train[feature], test[feature]], ignore_index=True ).value_counts(dropna=False))<train_model>
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 )
train.drop(labels=['uid'],axis=1,inplace=True)
 test.drop(labels=['uid'],axis=1,inplace=True )<categorify>
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 )
for feature in ['d2_d4_comb', 'uid2', 'uid3', 'uid4', 'uid5', 'P_emaildomain_bin', 'P_emaildomain_suffix', 'R_emaildomain_bin', 'R_emaildomain_suffix']:
le = LabelEncoder()
le.fit(list(train[feature].astype(str ).values)+ list(test[feature].astype(str ).values))
train[feature] = le.transform(list(train[feature].astype(str ).values))
test[feature] = le.transform(list(test[feature].astype(str ).values))<prepare_x_and_y>
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]))
X = train.sort_values('TransactionDT' ).drop(['isFraud', 'DT','TransactionDT','TransactionID'], axis=1)
 Y = train.sort_values('TransactionDT')['isFraud']
X_test = test.drop(['TransactionDT', 'DT','TransactionID'], axis=1 )<drop_column>
def make_tta_predictions(images, score_threshold=0.57):
with torch.no_grad() :
images = torch.stack(images ).float().to(DEVICE)
 predictions = []
for tta_transform in tta_transforms:
result = []
outputs = make_ensemble_predictions(tta_transform.batch_augment(images.clone()))
for i, image in enumerate(images):
boxes, scores, labels = run_wbf_ensemble(outputs, image_index=i)
 indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
del train, test<init_hyperparams>
def run_wbf(predictions, image_index, image_size=1024, iou_thr=0.4, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes']/(image_size-1)).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]
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.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
params = {'num_leaves': 493,
'min_child_weight': 0.03454472573214212,
'feature_fraction': 0.3797454081646243,
'bagging_fraction': 0.4181193142567742,
'min_data_in_leaf': 106,
'objective': 'binary',
'max_depth': -1,
'learning_rate': 0.006883242363721497,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'auc',
"verbosity": -1,
'reg_alpha': 0.3899927210061127,
'reg_lambda': 0.6485237330340494,
'random_state': 47
}<train_model>
resultsFASTER = {}
for images, image_ids in test_data_loader:
predictions = make_tta_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes = boxes.round().astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
resultsFASTER[image_id] = [boxes, scores]
folds = StratifiedKFold(n_splits=5)
 evals_result = {}
trainings = list()
aucs = list()
feature_importances = pd.DataFrame()
feature_importances['feature'] = X.columns
training_start_time = time()
for fold,(trn_idx, test_idx)in enumerate(folds.split(X, Y)) :
start_time = time()
print('Training on fold {}'.format(fold + 1))
trn_data = lgb.Dataset(X.iloc[trn_idx], label=Y.iloc[trn_idx])
 val_data = lgb.Dataset(X.iloc[test_idx], label=Y.iloc[test_idx])
 clf = lgb.train(params, trn_data, 10000, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds=500, evals_result=evals_result)
 feature_importances['fold_{}'.format(fold + 1)] = clf.feature_importance()
aucs.append(clf.best_score['valid_1']['auc'])
 trainings.append(clf.best_score['training']['auc'])
 print('Fold {} finished in {}'.format(fold + 1, str(datetime.timedelta(seconds=time() - start_time))))
print('-' * 30)
 print('Training has finished.')
 print('Total training time is {}'.format(str(datetime.timedelta(seconds=time() - training_start_time))))
print('Mean AUC:', np.mean(aucs))
print('STD AUC:', np.std(aucs))
print('Training AUC:', np.mean(trainings))
print('Difference is:', np.mean(trainings)-np.mean(aucs))
print('-' * 30 )<find_best_params>
def run_wbf(boxes,scores, image_size=1024, iou_thr=0.6, skip_box_thr=0.43, 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
best_iter = clf.best_iteration<train_model>
pred_string = []
bts, sts, ids = [], [], []
results = dict()
for image_id, faster in resultsFASTER.items() :
yolo = resultsYOLO[image_id]
scores_ = [faster[1], yolo[1],yolo[1]]
boxes_ = [faster[0], yolo[0],yolo[0]]
boxes, scores = run_wbf(boxes_, scores_, iou_thr = 0.45, skip_box_thr=0.01)
 boxes = boxes.round().astype(np.int32 ).clip(min=0, max=1023)
 indices = scores >= 0.3
boxes = boxes[indices]
scores = scores[indices]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {'image_id': image_id, 'PredictionString': format_prediction_string(boxes, scores)}
pred_string.append(result)
 ids.append(image_id)
 bts.append(boxes)
 sts.append(scores)
 test_df = pd.DataFrame(pred_string, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False )