Kaggle competition: Global Wheat Detection
X_test = train_test_missing.loc[:, ['Season', 'NumOT', 'Score_Ratio', 'Score_Pct']]
n = X_train.shape[0]
train_test_merge = pd.concat([X_train, X_test], axis = 0 ).reset_index(drop = True)
 train_test_merge = pd.concat([pd.get_dummies(train_test_merge['Season'].astype(object)) ,
train_test_merge.drop('Season', axis = 1)], axis = 1)
 train_test_merge = pd.concat([pd.get_dummies(train_test_merge['NumOT'].astype(object)) ,
train_test_merge.drop('NumOT', axis = 1)], axis = 1)
 X_train = train_test_merge.loc[:(n - 1), :].reset_index(drop = True)
 X_test = train_test_merge.loc[n:, :].reset_index(drop = True )<feature_engineering>
all_path = glob('.. /input/global-wheat-detection/test/*')
 DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
x_max = X_train.max()
x_min = X_train.min()
X_train =(X_train - x_min)/(x_max - x_min + 1e-14)
 X_test =(X_test - x_min)/(x_max - x_min + 1e-14 )<train_on_grid>
def get_valid_transforms() :
return A.Compose([
A.Resize(height=1024, width=1024, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )
model = LogisticRegressionCV(cv=25)
 model.fit(X_train, train_labels )<predict_on_test>
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
train_test_inner['Pred1'] = model.predict_proba(X_train)[:,1]
train_test_missing['Pred1'] = model.predict_proba(X_test)[:,1]<merge>
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=4,
shuffle=False,
num_workers=2,
drop_last=False,
collate_fn=collate_fn
)
sub = pd.merge(df_sample_sub,
pd.concat([train_test_missing.loc[:, ['Season', 'TeamID1', 'TeamID2', 'Pred1']],
train_test_inner.loc[:, ['Season', 'TeamID1', 'TeamID2', 'Pred1']] ],
axis = 0 ).reset_index(drop = True),
on = ['Season', 'TeamID1', 'TeamID2'], how = 'outer' )<groupby>
class CrossEntropyLabelSmooth(nn.Module):

 def __init__(self, num_classes, epsilon=0.1, use_gpu=True):
super(CrossEntropyLabelSmooth, self ).__init__()
self.num_classes = num_classes
self.epsilon = epsilon
self.use_gpu = use_gpu
self.logsoftmax = nn.LogSoftmax(dim=1)
 def forward(self, inputs, targets):

 log_probs = self.logsoftmax(inputs)
 targets = torch.zeros(log_probs.size() ).scatter_(1, targets.unsqueeze(1 ).data.cpu() , 1)
 if self.use_gpu: targets = targets.cuda()
targets =(1 - self.epsilon)* targets + self.epsilon / self.num_classes
loss =(- targets * log_probs ).mean(0 ).sum()
return loss
team1_probs = sub.groupby('TeamID1')['Pred1'].apply(lambda x :(x ** -1.0 ).mean() ** -1.0 ).fillna(0.5 ).to_dict()
team2_probs = sub.groupby('TeamID2')['Pred1'].apply(lambda x :(x ** -1.0 ).mean() ** -1.0 ).fillna(0.5 ).to_dict()<feature_engineering>
def fastrcnn_loss(class_logits, box_regression, labels, regression_targets):

 labels = torch.cat(labels, dim=0)
 regression_targets = torch.cat(regression_targets, dim=0)
 labal_smooth_loss = CrossEntropyLabelSmooth(2)
 classification_loss = labal_smooth_loss(class_logits, labels)
 sampled_pos_inds_subset = torch.nonzero(labels > 0 ).squeeze(1)
 labels_pos = labels[sampled_pos_inds_subset]
N, num_classes = class_logits.shape
box_regression = box_regression.reshape(N, -1, 4)
 box_loss = det_utils.smooth_l1_loss(
 box_regression[sampled_pos_inds_subset, labels_pos],
regression_targets[sampled_pos_inds_subset],
beta=1 / 9,
size_average=False, 
)
 box_loss = box_loss / labels.numel()
return classification_loss, box_loss
sub['Pred'] = sub[['TeamID1', 'TeamID2','Pred1']]\ 
.apply(lambda x : team1_probs.get(x[0])*(1 - team2_probs.get(x[1])) if np.isnan(x[2])else x[2],
axis = 1 )<save_to_csv>
def fpn_backbone_269(pretrained, norm_layer=misc_nn_ops.FrozenBatchNorm2d, trainable_layers=3):
print(f'
Pretarined is {pretrained}')
 backbone = resnest269e(pretrained=pretrained)
 assert trainable_layers <= 5 and trainable_layers >= 0
layers_to_train = ['layer4', 'layer3', 'layer2', 'layer1', 'conv1'][:trainable_layers]
for name, parameter in backbone.named_parameters() :
if all([not name.startswith(layer)for layer in layers_to_train]):
parameter.requires_grad_(False)
 return_layers = {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'}
in_channels_stage2 = backbone.inplanes // 8
in_channels_list = [
in_channels_stage2,
in_channels_stage2 * 2,
in_channels_stage2 * 4,
in_channels_stage2 * 8,
]
out_channels = 256
return BackboneWithFPN(backbone, return_layers, in_channels_list, out_channels)
 class WheatDetector_269(nn.Module):
def __init__(self, **kwargs):
super(WheatDetector_269, self ).__init__()
self.backbone = fpn_backbone_269(pretrained=False)
 self.base = FasterRCNN(self.backbone, num_classes = 2, **kwargs)
 self.base.roi_heads.fastrcnn_loss = self.fastrcnn_loss
def fastrcnn_loss(self, class_logits, box_regression, labels, regression_targets):

 labels = torch.cat(labels, dim=0)
 regression_targets = torch.cat(regression_targets, dim=0)
 labal_smooth_loss = CrossEntropyLabelSmooth(2)
 classification_loss = labal_smooth_loss(class_logits, labels)
 sampled_pos_inds_subset = torch.nonzero(labels > 0 ).squeeze(1)
 labels_pos = labels[sampled_pos_inds_subset]
N, num_classes = class_logits.shape
box_regression = box_regression.reshape(N, -1, 4)
 box_loss = det_utils.smooth_l1_loss(
 box_regression[sampled_pos_inds_subset, labels_pos],
regression_targets[sampled_pos_inds_subset],
beta=1 / 9,
size_average=False, 
)
 box_loss = box_loss / labels.numel()
return classification_loss, box_loss
def forward(self, images, targets=None):
return self.base(images, targets )
sub = sub.drop_duplicates(subset=["ID"], keep='first')
 sub[['ID', 'Pred']].to_csv('sub.csv', index = False )<install_modules>
def fpn_backbone_101(pretrained, norm_layer=misc_nn_ops.FrozenBatchNorm2d, trainable_layers=3):
print(f'
Pretarined is {pretrained}')
 backbone = resnest101e(pretrained=pretrained)
 assert trainable_layers <= 5 and trainable_layers >= 0
layers_to_train = ['layer4', 'layer3', 'layer2', 'layer1', 'conv1'][:trainable_layers]
for name, parameter in backbone.named_parameters() :
if all([not name.startswith(layer)for layer in layers_to_train]):
parameter.requires_grad_(False)
 return_layers = {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'}
in_channels_stage2 = backbone.inplanes // 8
in_channels_list = [
in_channels_stage2,
in_channels_stage2 * 2,
in_channels_stage2 * 4,
in_channels_stage2 * 8,
]
out_channels = 256
return BackboneWithFPN(backbone, return_layers, in_channels_list, out_channels)
 class WheatDetector_101(nn.Module):
def __init__(self, **kwargs):
super(WheatDetector_101, self ).__init__()
self.backbone = fpn_backbone_101(pretrained=False)
 self.base = FasterRCNN(self.backbone, num_classes = 2, **kwargs)
 self.base.roi_heads.fastrcnn_loss = self.fastrcnn_loss
def fastrcnn_loss(self, class_logits, box_regression, labels, regression_targets):

 labels = torch.cat(labels, dim=0)
 regression_targets = torch.cat(regression_targets, dim=0)
 labal_smooth_loss = CrossEntropyLabelSmooth(2)
 classification_loss = labal_smooth_loss(class_logits, labels)
 sampled_pos_inds_subset = torch.nonzero(labels > 0 ).squeeze(1)
 labels_pos = labels[sampled_pos_inds_subset]
N, num_classes = class_logits.shape
box_regression = box_regression.reshape(N, -1, 4)
 box_loss = det_utils.smooth_l1_loss(
 box_regression[sampled_pos_inds_subset, labels_pos],
regression_targets[sampled_pos_inds_subset],
beta=1 / 9,
size_average=False, 
)
 box_loss = box_loss / labels.numel()
return classification_loss, box_loss
def forward(self, images, targets=None):
return self.base(images, targets )
!pip install -q efficientnet<import_modules>
def load_net_269(checkpoint_path):
model = WheatDetector_269()
checkpoint=torch.load(checkpoint_path)
 model.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
model.eval() ;
return model.cuda()
def load_net_101(checkpoint_path):
model = WheatDetector_101()
checkpoint=torch.load(checkpoint_path)
 model.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
model.eval() ;
return model.cuda()
models = [
load_net_101('.. /input/resnest101/fold0-best-checkpoint.bin'),
load_net_101('.. /input/resnest101/fold1-best-checkpoint.bin'),
load_net_101('.. /input/resnest101/fold2-best-checkpoint.bin'),
load_net_101('.. /input/resnest101/fold3-best-checkpoint.bin'),
load_net_101('.. /input/resnest101/fold4-best-checkpoint.bin'),
load_net_269('.. /input/resnest269/fold0-best-checkpoint.bin'),
load_net_269('.. /input/resnest269/fold1-best-checkpoint.bin'),
load_net_269('.. /input/resnest269/fold2-best-checkpoint.bin'),
load_net_269('.. /input/resnest269/fold3-best-checkpoint.bin'),
load_net_269('.. /input/resnest269/fold4-best-checkpoint.bin')
 ]
import math, re, os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.layers as L
import efficientnet.tfkeras as efn
from sklearn import metrics
from sklearn.model_selection import train_test_split<set_options>
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 )
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
def process_det(index, outputs, score_threshold=0.5):
boxes = outputs[index]['boxes'].data.cpu().numpy()
scores = outputs[index]['scores'].data.cpu().numpy()
boxes =(boxes ).clip(min=0, max=1023 ).astype(int)
 indexes = np.where(scores>score_threshold)
 boxes = boxes[indexes]
scores = scores[indexes]
return boxes, scores
AUTO = tf.data.experimental.AUTOTUNE
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 10
BATCH_SIZE = 16 * strategy.num_replicas_in_sync<concatenate>
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]))
def append_path(pre):
return np.vectorize(lambda file: os.path.join(GCS_DS_PATH, pre, file))<load_from_csv>
def make_tta_predictions(images,net, score_threshold=0.1):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
outputs = net(tta_transform.batch_augment(images.clone()))
for i, image in enumerate(images):
boxes = outputs[i]['boxes'].data.cpu().numpy()
scores = outputs[i]['scores'].data.cpu().numpy()
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
def run_wbf(predictions, image_index, image_size=1024, iou_thr=0.5, 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
sub = pd.read_csv('/kaggle/input/alaska2-image-steganalysis/sample_submission.csv')
 train_filenames = np.array(os.listdir("/kaggle/input/alaska2-image-steganalysis/Cover/"))<define_variables>
fold1 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[0])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold1[image_id] = [boxes, scores, labels]
print('
Completed')
 fold2 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[1])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold2[image_id] = [boxes, scores, labels]
print('
Completed')
 fold3 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[2])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold3[image_id] = [boxes, scores, labels]
print('
Completed')
 fold4 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[3])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold4[image_id] = [boxes, scores, labels]
print('
Completed')
np.random.seed(0)
 positives = train_filenames.copy()
negatives = train_filenames.copy()
np.random.shuffle(positives)
 np.random.shuffle(negatives)
 jmipod = append_path('JMiPOD' )(positives[:10000])
 juniward = append_path('JUNIWARD' )(positives[10000:20000])
 uerd = append_path('UERD' )(positives[20000:30000])
 pos_paths = np.concatenate([jmipod, juniward, uerd] )<define_variables>
fold5 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[4])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold5[image_id] = [boxes, scores, labels]
print('
Completed')
 fold6 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[5])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold6[image_id] = [boxes, scores, labels]
print('
Completed')
 fold7 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[6])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold7[image_id] = [boxes, scores, labels]
print('
Completed')
 fold8 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[7])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold8[image_id] = [boxes, scores, labels]
print('
Completed')
 fold9 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[8])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold9[image_id] = [boxes, scores, labels]
print('
Completed')
 fold10 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[9])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold10[image_id] = [boxes, scores, labels]
print('
Completed')
test_paths = append_path('Test' )(sub.Id.values)
 neg_paths = append_path('Cover' )(negatives[:30000] )<concatenate>
def run_last_wbf(model1,model2,model3,model4,
model5,model6,model7,model8,model9,model10,
iou_thr=0.5,skip_box_thr=0.43):
box1,scores1,labels1 = model1
box2,scores2,labels2 = model2
box3,scores3,labels3 = model3
box4,scores4,labels4 = model4
box1 = box1/1023
box2 = box2/1023
box3 = box3/1023
box4 = box4/1023
box5,scores5,labels5 = model5
box6,scores6,labels6 = model6
box7,scores7,labels7 = model7
box8,scores8,labels8 = model8
box9,scores9,labels9 = model9
box10,scores10,labels10 = model10
box5 = box5/1023
box6 = box6/1023
box7 = box7/1023
box8 = box8/1023
box9 = box9/1023
box10 = box10/1023
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion([box1,box2,box3,box4,box5,box6,box7,box8,box9,box10],
[scores1,scores2,scores3,scores4,scores5,scores6,scores7,scores8,scores9,scores10],
[labels1,labels2,labels3,labels4,labels5,labels6,labels7,labels8,labels9,labels10],
weights=None,iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes,scores,labels
train_paths = np.concatenate([pos_paths, neg_paths])
 train_labels = np.array([1] * len(pos_paths)+ [0] * len(neg_paths))<split>
w = {}
for row in range(len(all_path)) :
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes,scores,labels = run_last_wbf(fold1[image_id],fold2[image_id],fold3[image_id],fold4[image_id],
fold5[image_id],fold6[image_id],fold7[image_id],fold8[image_id],fold9[image_id],fold10[image_id])
 boxes =(boxes*1023)
 indexes = np.where(scores > 0.50)[0]
w[image_id] = [boxes,scores,labels]
train_paths, valid_paths, train_labels, valid_labels = train_test_split(
 train_paths, train_labels, test_size=0.15, random_state=2020 )<normalization>
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )
def decode_image(filename, label=None, image_size=(512, 512)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None):
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 if label is None:
return image
else:
return image, label<categorify>
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.cache() 
.repeat() 
.shuffle(1024)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( valid_paths, valid_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )<choose_model_class>
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=4,
shuffle=False,
num_workers=2,
drop_last=False,
collate_fn=collate_fn
)
def build_lrfn(lr_start=0.00001, lr_max=0.000075,
lr_min=0.000001, lr_rampup_epochs=20,
lr_sustain_epochs=0, lr_exp_decay=.8):
lr_max = lr_max * strategy.num_replicas_in_sync
def lrfn(epoch):
if epoch < lr_rampup_epochs:
lr =(lr_max - lr_start)/ lr_rampup_epochs * epoch + lr_start
elif epoch < lr_rampup_epochs + lr_sustain_epochs:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs)+ lr_min
return lr
return lrfn<choose_model_class>
class CrossEntropyLabelSmooth(nn.Module):

 def __init__(self, num_classes, epsilon=0.1, use_gpu=True):
super(CrossEntropyLabelSmooth, self ).__init__()
self.num_classes = num_classes
self.epsilon = epsilon
self.use_gpu = use_gpu
self.logsoftmax = nn.LogSoftmax(dim=1)
 def forward(self, inputs, targets):

 log_probs = self.logsoftmax(inputs)
 targets = torch.zeros(log_probs.size() ).scatter_(1, targets.unsqueeze(1 ).data.cpu() , 1)
 if self.use_gpu: targets = targets.cuda()
targets =(1 - self.epsilon)* targets + self.epsilon / self.num_classes
loss =(- targets * log_probs ).mean(0 ).sum()
return loss
def fastrcnn_loss(class_logits, box_regression, labels, regression_targets):

 labels = torch.cat(labels, dim=0)
 regression_targets = torch.cat(regression_targets, dim=0)
 labal_smooth_loss = CrossEntropyLabelSmooth(2)
 classification_loss = labal_smooth_loss(class_logits, labels)
 sampled_pos_inds_subset = torch.nonzero(labels > 0 ).squeeze(1)
 labels_pos = labels[sampled_pos_inds_subset]
N, num_classes = class_logits.shape
box_regression = box_regression.reshape(N, -1, 4)
 box_loss = det_utils.smooth_l1_loss(
 box_regression[sampled_pos_inds_subset, labels_pos],
regression_targets[sampled_pos_inds_subset],
beta=1 / 9,
size_average=False, 
)
 box_loss = box_loss / labels.numel()
return classification_loss, box_loss
def fpn_backbone(pretrained, norm_layer=misc_nn_ops.FrozenBatchNorm2d, trainable_layers=3):
print(f'
Pretarined is {pretrained}')
 backbone = resnest101e(pretrained=pretrained)
 assert trainable_layers <= 5 and trainable_layers >= 0
layers_to_train = ['layer4', 'layer3', 'layer2', 'layer1', 'conv1'][:trainable_layers]
for name, parameter in backbone.named_parameters() :
if all([not name.startswith(layer)for layer in layers_to_train]):
parameter.requires_grad_(False)
 return_layers = {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'}
in_channels_stage2 = backbone.inplanes // 8
in_channels_list = [
in_channels_stage2,
in_channels_stage2 * 2,
in_channels_stage2 * 4,
in_channels_stage2 * 8,
]
out_channels = 256
return BackboneWithFPN(backbone, return_layers, in_channels_list, out_channels)
 class WheatDetector(nn.Module):
def __init__(self, **kwargs):
super(WheatDetector, self ).__init__()
self.backbone = fpn_backbone(pretrained=False)
 self.base = FasterRCNN(self.backbone, num_classes = 2, **kwargs)
 self.base.roi_heads.fastrcnn_loss = self.fastrcnn_loss
def fastrcnn_loss(self, class_logits, box_regression, labels, regression_targets):

 labels = torch.cat(labels, dim=0)
 regression_targets = torch.cat(regression_targets, dim=0)
 labal_smooth_loss = CrossEntropyLabelSmooth(2)
 classification_loss = labal_smooth_loss(class_logits, labels)
 sampled_pos_inds_subset = torch.nonzero(labels > 0 ).squeeze(1)
 labels_pos = labels[sampled_pos_inds_subset]
N, num_classes = class_logits.shape
box_regression = box_regression.reshape(N, -1, 4)
 box_loss = det_utils.smooth_l1_loss(
 box_regression[sampled_pos_inds_subset, labels_pos],
regression_targets[sampled_pos_inds_subset],
beta=1 / 9,
size_average=False, 
)
 box_loss = box_loss / labels.numel()
return classification_loss, box_loss
def forward(self, images, targets=None):
return self.base(images, targets)
 def load_net(checkpoint_path):
model = WheatDetector()
checkpoint=torch.load(checkpoint_path)
 model.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
model.eval() ;
return model.cuda()
models = [
load_net('.. /input/fold-1-resnest101-512/best-checkpoint.bin'),
load_net('.. /input/fold2resnest101512/best-checkpoint.bin'),
load_net('.. /input/fold3resnest101512/best-checkpoint.bin'),
load_net('.. /input/resnest101fold4/best-checkpoint.bin'),
load_net('.. /input/resnest101fold3/best-checkpoint.bin')
 ]
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB3(
 input_shape=(512, 512, 3),
weights='imagenet',
include_top=False 
),
L.GlobalAveragePooling2D() ,
L.Dense(1, activation='sigmoid')
 ])
 model.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy'] 
)
 model.summary()<train_model>
class BaseWheatTTA:

 image_size = 512
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)
 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]))
STEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE
history = model.fit(
 train_dataset,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset
)<save_model>
def make_tta_predictions(images,net, score_threshold=0.1):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
outputs = net(tta_transform.batch_augment(images.clone()))
for i, image in enumerate(images):
boxes = outputs[i]['boxes'].data.cpu().numpy()
scores = outputs[i]['scores'].data.cpu().numpy()
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
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.5, 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*1023
return boxes, scores, labels
model.save("model.h5" )<save_to_csv>
fold0 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[0])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold0[image_id] = [boxes, scores, labels]
print('
Completed')
 fold1 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[1])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold1[image_id] = [boxes, scores, labels]
print('
Completed')
 fold2 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[2])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold2[image_id] = [boxes, scores, labels]
print('
Completed')
 fold3 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[3])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold3[image_id] = [boxes, scores, labels]
print('
Completed')
 fold4 = {}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images,models[4])
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 image_id = image_ids[i]
fold4[image_id] = [boxes, scores, labels]
print('
Completed' )
sub.Label = model.predict(test_dataset, verbose=1)
 sub.to_csv('submission.csv', index=False)
 sub.head()<set_options>
def run_last_wbf(model1,model2,model3,model4,model5,
iou_thr=0.5,skip_box_thr=0.43):
box1,scores1,labels1 = model1
box2,scores2,labels2 = model2
box3,scores3,labels3 = model3
box4,scores4,labels4 = model4
box5,scores5,labels5 = model5
box1 = box1/1023
box2 = box2/1023
box3 = box3/1023
box4 = box4/1023
box5 = box5/1023
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion([box1,box2,box3,box4,box5],
[scores1,scores2,scores3,scores4,scores5],
[labels1,labels2,labels3,labels4,labels5],
weights=None,iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes,scores,labels
x = {}
for row in range(len(all_path)) :
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes,scores,labels = run_last_wbf(fold1[image_id],fold2[image_id],fold0[image_id],fold3[image_id],fold4[image_id])
 boxes =(boxes*1023)
 x[image_id] = [boxes,scores,labels]
warnings.simplefilter("ignore")
<train_model>
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()
models = [
load_net('.. /input/efficientdetd51/0.bin'),
load_net('.. /input/efficientdetd51/1.bin'),
load_net('.. /input/efficientdetd51/2.bin'),
load_net('.. /input/efficientdetd51/3.bin'),
load_net('.. /input/efficientdetd51/4.bin'),
load_net('.. /input/efficientdetd51/5.bin'),
load_net('.. /input/efficientdetd51/6.bin'),
load_net('.. /input/efficientdetd51/7.bin'),
]
print('Train images: %d' %len(os.listdir(
 os.path.join(WORK_DIR, "train_images"))))<load_from_disk>
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test/'
class TestDatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
def get_valid_transforms() :
return A.Compose(
 [
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
],
p=1.0, 
)
 dataset = TestDatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=2,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn 
)
 def make_predictions(images, score_threshold=0.35):
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
with open(os.path.join(WORK_DIR, "label_num_to_disease_map.json")) as file:
print(json.dumps(json.loads(file.read()), indent=4))<load_from_csv>
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.432, skip_box_thr=0.397, 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] ).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
train_labels = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
train_labels.head()<define_variables>
y={}
for images, image_ids in data_loader:
predictions = make_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i,)
 boxes =(boxes*2)
 y[image_ids[i]] = [boxes,scores,labels]
BATCH_SIZE = 8
STEPS_PER_EPOCH = len(train_labels)*0.8 / BATCH_SIZE
VALIDATION_STEPS = len(train_labels)*0.2 / BATCH_SIZE
EPOCHS = 20
TARGET_SIZE = 512<data_type_conversions>
models = [
load_net('.. /input/efficientdetd52/0.bin'),
load_net('.. /input/efficientdetd52/1.bin'),
load_net('.. /input/efficientdetd52/2.bin'),
load_net('.. /input/efficientdetd52/3.bin'),
load_net('.. /input/efficientdetd52/4.bin'),
load_net('.. /input/kaggleeffnet/plabel_model/last-checkpoint1.bin'),
load_net('.. /input/kaggleeffnet/plabel_model/best-checkpoint-004epoch.bin')
 ]
train_labels.label = train_labels.label.astype('str')
 train_datagen = ImageDataGenerator(validation_split = 0.2,
preprocessing_function = None,
rotation_range = 45,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1)
 train_generator = train_datagen.flow_from_dataframe(train_labels,
directory = os.path.join(WORK_DIR, "train_images"),
subset = "training",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 validation_datagen = ImageDataGenerator(validation_split = 0.2)
 validation_generator = validation_datagen.flow_from_dataframe(train_labels,
directory = os.path.join(WORK_DIR, "train_images"),
subset = "validation",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse" )<prepare_x_and_y>
z={}
for images, image_ids in data_loader:
predictions = make_predictions(images,0.4337)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i,iou_thr=0.4637,skip_box_thr=0.12)
 boxes =(boxes*2)
 z[image_ids[i]] = [boxes,scores,labels]
generator = train_datagen.flow_from_dataframe(train_labels.iloc[20:21],
directory = os.path.join(WORK_DIR, "train_images"),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 aug_images = [generator[0][0][0]/255 for i in range(10)]
fig, axes = plt.subplots(2, 5, figsize =(20, 10))
axes = axes.flatten()
for img, ax in zip(aug_images, axes):
ax.imshow(img)
 ax.axis('off')
 plt.tight_layout()
plt.show()<choose_model_class>
def get_valid_transforms() :
return A.Compose(
 [
A.Resize(height=1024, width=1024, p=1.0),
ToTensorV2(p=1.0),
],
p=1.0, 
)
 dataset = TestDatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=2,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn 
)
 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 run_wbf(predictions, image_index, image_size=1024, iou_thr=0.432, skip_box_thr=0.397, 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] ).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
<choose_model_class>
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[:, [1,3]]
res_boxes[:, [1,3]] = boxes[:, [2,0]]
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)
 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]))
def create_model() :
conv_base = EfficientNetB0(include_top = False, weights = None,
input_shape =(TARGET_SIZE, TARGET_SIZE, 3))
model = conv_base.output
model = layers.GlobalAveragePooling2D()(model)
 model = layers.Dense(5, activation = "softmax" )(model)
 model = models.Model(conv_base.input, model)
 model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["acc"])
 return model<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=1024
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()
net = load_net('.. /input/effdet1024/best-checkpoint-1024.bin' )
print('Our EfficientNet CNN has %d layers' %len(model.layers))<load_pretrained>
l={}
for images, image_ids in data_loader:
predictions = make_tta_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 l[image_ids[i]] = [boxes, scores, labels]
model.load_weights('.. /input/cassava-leaf-disease-models/basic_EfNetB0_imagenet_512.h5' )<choose_model_class>
all_path = glob('.. /input/global-wheat-detection/test/*')
 a = {}
for row in range(len(all_path)) :
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes,scores,labels = run_last_wbf(y[image_id],z[image_id])
 boxes =(boxes*1023)
 a[image_id] = [boxes,scores,labels]
model_save = ModelCheckpoint('./EffNetB0_512_8_best_weights.h5',
save_best_only = True,
save_weights_only = True,
monitor = 'val_loss',
mode = 'min', verbose = 1)
 early_stop = EarlyStopping(monitor = 'val_loss', min_delta = 0.001,
patience = 5, mode = 'min', verbose = 1,
restore_best_weights = True)
 reduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3,
patience = 2, min_delta = 0.001,
mode = 'min', verbose = 1)
 history = model.fit(
 train_generator,
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
validation_data = validation_generator,
validation_steps = VALIDATION_STEPS,
callbacks = [model_save, early_stop, reduce_lr]
)<save_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)
 results = []
for row in range(len(all_path)) :
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes,scores,labels = run_last_wbf(w[image_id],g[image_id],l[image_id])
 boxes =(boxes*1023 ).astype(np.int32 ).clip(min=0, max=1023)
 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 )
<predict_on_test><EOS> 
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv',index=False)
 test_df