Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<save_to_csv>
!pip install.. /input/timm-repo/pytorch-image-models-master/ > /dev/null
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<install_modules>
!pip install.. /input/pretrainedmodels-pytorch/pretrained-models.pytorch-master/ > /dev/null
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<categorify>
!pip install.. /input/efficientnet-pyotrch/EfficientNet-PyTorch-master/ > /dev/null
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<data_type_conversions>
!pip install.. /input/segmentation-models-pytorch/segmentation_models.pytorch-master/ > /dev/null
DATA_ROOT_PATH = '.. /input/global-wheat-detection/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]<load_pretrained>
class RANZCRDataset(torch.utils.data.Dataset):
def __init__(
 self,
df,
root,
ext,
path_col,
use_timm_aug=False,
transforms=None,
augmentations=None, 
):
super().__init__()
df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.augmentations = augmentations
self.root = root
self.use_timm_aug = use_timm_aug
self.image_names = self._prepare_image_names(df[path_col].tolist() , root, ext)
 def __len__(self):
return len(self.image_names)
 def _prepare_image_names(self, basenames: List[str], root: str, ext: str):
return [pjoin(root, el)+ ext for el in basenames]
def _prepare_img_target_from_idx(self, idx: int):
image_name = self.image_names[idx]
img = Image.open(image_name)
 if not self.use_timm_aug:
img = np.array(img)
 if self.augmentations is not None:
if self.use_timm_aug:
img = self.augmentations(img)
 else:
img = self.augmentations(image=img)["image"]
if self.use_timm_aug:
img = np.array(img)
 if self.transforms is not None:
img = self.transforms(img)
 return img
def __getitem__(self, index: int):
img = self._prepare_img_target_from_idx(index)
 return img
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=2,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn
)<choose_model_class>
EFFNETB6_EMB_DIM = 2304
EFFNETB5_EMB_DIM = 2048
EFFNETB4_EMB_DIM = 1792
EFFNETB3_EMB_DIM = 1536
EFFNETB1_EMB_DIM = 1280
RESNET50_EMB_DIM = 2048
REXNET200_EMB_DIM = 2560
VIT_EMB_DIM = 768
NF_RESNET50_EMB_DIM = 2048
EPS = 1e-6
class TaylorSoftmax(nn.Module):
def __init__(self, dim=1, n=2):
super(TaylorSoftmax, self ).__init__()
assert n % 2 == 0
self.dim = dim
self.n = n
def forward(self, x):
fn = torch.ones_like(x)
 denor = 1.
 for i in range(1, self.n+1):
denor *= i
fn = fn + x.pow(i)/ denor
out = fn / fn.sum(dim=self.dim, keepdims=True)
 return out
class CNNModel(nn.Module):
def __init__(
 self,
classifiier_config: Mapping[str, Any],
encoder_type: str,
device: str,
use_pretrained_encoder: bool = False,
path_to_chkp: Optional[str] = None,
use_taylorsoftmax: bool = False,
one_channel: bool = True, 
):
super().__init__()
if path_to_chkp is not None:
use_pretrained_encoder = False
if encoder_type == "rexnet_200":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder 
)
 if one_channel:
self.encoder.stem.conv.in_channels = 1
weight = self.encoder.stem.conv.weight.mean(1, keepdim=True)
 self.encoder.stem.conv.weight = torch.nn.Parameter(weight)
 self.encoder.head.fc = nn.Identity()
nn_embed_size = REXNET200_EMB_DIM
elif encoder_type == "tf_efficientnet_b3_ns":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder 
)
 if one_channel:
self.encoder.conv_stem.in_channels = 1
weight = self.encoder.conv_stem.weight.mean(1, keepdim=True)
 self.encoder.conv_stem.weight = torch.nn.Parameter(weight)
 self.encoder.classifier = nn.Identity()
nn_embed_size = EFFNETB3_EMB_DIM
elif encoder_type == "tf_efficientnet_b5_ns":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder, num_classes=11 
)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_effnet_b5_start_point(path_to_chkp)
 self.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.conv_stem.in_channels = 1
weight = self.encoder.conv_stem.weight.mean(1, keepdim=True)
 self.encoder.conv_stem.weight = torch.nn.Parameter(weight)
 self.encoder.classifier = nn.Identity()
nn_embed_size = EFFNETB5_EMB_DIM
elif encoder_type == "resnet200d":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder, num_classes=11 
)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_resnet200d_start_point(path_to_chkp)
 self.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.conv1[0].in_channels = 1
weight = self.encoder.conv1[0].weight.mean(1, keepdim=True)
 self.encoder.conv1[0].weight = torch.nn.Parameter(weight)
 self.encoder.fc = nn.Identity()
nn_embed_size = RESNET50_EMB_DIM
else:
raise ValueError(f"{encoder_type} is invalid model_type")
 classes_num = classifiier_config["classes_num"]
hidden_dims = classifiier_config["hidden_dims"]
second_dropout_rate = classifiier_config["second_dropout_rate"]
if classifiier_config["classifier_type"] == "relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=first_dropout_rate),
nn.Linear(hidden_dims, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "elu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "dima":
self.classifier = nn.Sequential(
 nn.BatchNorm1d(nn_embed_size),
nn.Linear(nn_embed_size, hidden_dims),
nn.BatchNorm1d(hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "prelu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "multiscale_relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.big_dropout = nn.Dropout(p=0.5)
 self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "drop_linear":
self.classifier = nn.Sequential(
 nn.Dropout(p=second_dropout_rate),
nn.Linear(nn_embed_size, classes_num), 
)
 else:
raise ValueError("Invalid classifier_type")
 self.use_taylorsoftmax = use_taylorsoftmax
if self.use_taylorsoftmax:
self.taylorsoftmax = TaylorSoftmax()
self.classifier_type = classifiier_config["classifier_type"]
self.encoder_type = encoder_type
self.device = device
self.to(self.device)
 def forward(self, image):
x = self.encoder(image)
 if self.classifier_type == "multiscale_relu":
logits = torch.mean(
 torch.stack(
 [self.classifier(self.big_dropout(x)) for _ in range(5)],
dim=0, 
),
dim=0, 
)
 else:
logits = self.classifier(x)
 if self.use_taylorsoftmax:
logits = self.taylorsoftmax(logits ).log()
return logits
class CNNSegModel(nn.Module):
def __init__(
 self,
classifiier_config: Mapping[str, Any],
encoder_type: str,
encoder_config: Mapping[str, Any],
device: str,
path_to_chkp: Optional[str] = None,
use_taylorsoftmax: bool = False,
one_channel: bool = True,
enable_inference_mode: bool = False 
):
super().__init__()
if path_to_chkp is not None:
use_pretrained_encoder = False
if encoder_type == "timm-efficientnet-b5_unet":
self.encoder = smp.Unet(**encoder_config)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_effnet_b5_start_point(path_to_chkp)
 self.encoder.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.encoder.conv_stem.in_channels = 1
weight = self.encoder.encoder.conv_stem.weight.mean(1, keepdim=True)
 self.encoder.encoder.conv_stem.weight = torch.nn.Parameter(weight)
 self.encoder.classification_head[3] = nn.Identity()
nn_embed_size = 512
elif encoder_type == "densenet121_unet":
self.encoder = smp.Unet(**encoder_config)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_densenet121_start_point(path_to_chkp)
 self.encoder.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.encoder.features.conv0.in_channels = 1
weight = self.encoder.encoder.features.conv0.weight.mean(
 1, keepdim=True 
)
 self.encoder.encoder.features.conv0.weight = torch.nn.Parameter(
 weight 
)
 self.encoder.classification_head[3] = nn.Identity()
nn_embed_size = 1024
else:
raise ValueError(f"{encoder_type} is invalid model_type")
 classes_num = classifiier_config["classes_num"]
hidden_dims = classifiier_config["hidden_dims"]
second_dropout_rate = classifiier_config["second_dropout_rate"]
if classifiier_config["classifier_type"] == "relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=first_dropout_rate),
nn.Linear(hidden_dims, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "elu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "dima":
self.classifier = nn.Sequential(
 nn.BatchNorm1d(nn_embed_size),
nn.Linear(nn_embed_size, hidden_dims),
nn.BatchNorm1d(hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "prelu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "multiscale_relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.big_dropout = nn.Dropout(p=0.5)
 self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "drop_linear":
self.classifier = nn.Sequential(
 nn.Dropout(p=second_dropout_rate),
nn.Linear(nn_embed_size, classes_num), 
)
 elif classifiier_config["classifier_type"] == "double_elu_mlp":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier_prenet = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() , 
)
 self.classifier_hidden_class = nn.Sequential(
 nn.Dropout(second_dropout_rate),
nn.Linear(
 hidden_dims, classifiier_config["hidden_classes_num"] 
), 
)
 self.classifier_class_final = nn.Sequential(
 nn.Dropout(second_dropout_rate),
nn.Linear(
 hidden_dims + classifiier_config["hidden_classes_num"],
classes_num, 
), 
)
 else:
raise ValueError("Invalid classifier_type")
 self.use_taylorsoftmax = use_taylorsoftmax
if self.use_taylorsoftmax:
self.taylorsoftmax = TaylorSoftmax()
self.classifier_type = classifiier_config["classifier_type"]
self.enable_inference_mode = enable_inference_mode
self.encoder_type = encoder_type
self.device = device
self.to(self.device)
 def forward(self, image, enable_inference_mode=False):
enable_inference_mode = enable_inference_mode or self.enable_inference_mode
if enable_inference_mode:
embs = self.encoder.encoder(image)
 x = self.encoder.classification_head(embs[-1])
 else:
mask, x = self.encoder(image)
 if self.classifier_type == "multiscale_relu":
logits = torch.mean(
 torch.stack(
 [self.classifier(self.big_dropout(x)) for _ in range(5)],
dim=0, 
),
dim=0, 
)
 elif self.classifier_type == "double_elu_mlp":
pre_logits = self.classifier_prenet(x)
 class_logits = self.classifier_hidden_class(pre_logits)
 logits = self.classifier_class_final(
 torch.cat([pre_logits, class_logits], axis=-1)
)
 else:
logits = self.classifier(x)
 if self.use_taylorsoftmax:
logits = self.taylorsoftmax(logits ).log()
if enable_inference_mode:
return logits
else:
if self.classifier_type == "double_elu_mlp":
return mask, class_logits, logits
else:
return mask, logits
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()
net = load_net('.. /input/wheateffdet5bestcheckpoint/best-checkpoint-038epoch.bin' )<predict_on_test>
def get_validation_models(
 model_initilizer: Callable,
model_config: Mapping[str, Any],
model_ckp_dicts: List[OrderedDict],
device: str, 
):
t_models = []
for mcd in model_ckp_dicts:
t_model = model_initilizer(**model_config, device=device)
 t_model.load_state_dict(mcd)
 t_model = t_model.to(device)
 t_model.eval()
t_models.append(t_model)
 return t_models
def create_val_loaders(
 loader_initilizer: object,
loader_config: Mapping[str, Any],
dfs: List[str],
batch_size: int, 
):
t_loaders = []
for df in dfs:
t_dataset = loader_initilizer(df=df, **loader_config)
 t_loader = torch.utils.data.DataLoader(
 t_dataset,
batch_size=batch_size,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2, 
)
 t_loaders.append(t_loader)
 return t_loaders
@torch.no_grad()
def cnn_model_predict(t_batch, t_model, t_device):
image = t_batch.to(t_device)
 logits = t_model(image)
 logits = logits.detach().cpu().numpy()
return logits
def predict_over_all_train(
 my_loaders, my_models, model_predict_func, device, do_concat=True 
):
logits = []
for loader, model in zip(my_loaders, my_models):
for batch in tqdm(loader):
logit = model_predict_func(batch, model, device)
 logits.append(logit)
 if do_concat:
logits = np.concatenate(logits)
 return logits
def make_predictions(images, score_threshold=0.22):
images = torch.stack(images ).cuda().float()
predictions = []
with torch.no_grad() :
det = net(images, torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
predictions.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
 return [predictions]
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.44, 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] ).tolist() 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<define_variables>
%matplotlib inline
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 )<predict_on_test>
SKIP_VAL = True
results = []
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 ).astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
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 )<save_to_csv>
def public_notebook() :
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 BATCH_SIZE = 64
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 class TestDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.file_names = df['StudyInstanceUID'].values
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{TEST_PATH}/{file_name}.jpg'
image = cv2.imread(file_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
def get_transforms(image_size=640):
return Compose([
Resize(image_size, image_size),
Normalize() ,
ToTensorV2() ,
])
 class ResNet200D(nn.Module):
def __init__(self, model_name='resnet200d'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
class SeResNet152D(nn.Module):
def __init__(self, model_name='seresnet152d'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
class RANZCRResNet200D(nn.Module):
def __init__(self, model_name='resnet200d', out_dim=11, pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, out_dim)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(
 y_preds1.sigmoid().to('cpu' ).numpy() +
y_preds2.sigmoid().to('cpu' ).numpy() 
)/ 2
avg_preds.append(y_preds)
 avg_preds = np.stack(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs, axis=1)
 return probs
models200D = []
model = ResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-public/resnet200d_320_CV9632.pth")['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 models200D_2 = []
model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold0_cv953.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold1_cv955.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold2_cv955.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold3_cv957.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold4_cv954.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = SeResNet152D()
model.load_state_dict(torch.load('.. /input/seresnet152d-cv9615/seresnet152d_320_CV96.15.pth')['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 test_dataset_512 = TestDataset(test, transform=get_transforms(image_size=512))
test_loader_512 = DataLoader(test_dataset_512, batch_size=BATCH_SIZE, shuffle=False, num_workers=os.cpu_count() // 2 , pin_memory=True)
 test_dataset_640 = TestDataset(test, transform=get_transforms(image_size=640))
test_loader_640 = DataLoader(test_dataset_640, batch_size=BATCH_SIZE, shuffle=False, num_workers=os.cpu_count() // 2 , pin_memory=True)
 predictions200d = inference(models200D, test_loader_640, device)
 predictions200d_2 = inference(models200D_2, test_loader_512, device)
 return predictions200d, predictions200d_2
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<install_modules>
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<categorify>
RESIZE_SIZE = 640
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<data_type_conversions>
PATH2DIR = '.. /input/ranzcr-clip-catheter-line-classification/'
os.listdir(PATH2DIR)
 train = pd.read_csv(pjoin(PATH2DIR, 'train.csv'))
sample_sub = pd.read_csv(pjoin(PATH2DIR, 'sample_submission.csv'))
split = np.load('.. /input/ranzcr-models/naive_cv_split.npy', allow_pickle=True )
DATA_ROOT_PATH = '.. /input/global-wheat-detection/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]<load_pretrained>
DEVICE = 'cuda'
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
)<choose_model_class>
models_512 = []
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_32bs_640res_lesslaugs_ls005_shedchanged_startpoint_difflrs_segbranch_125coefs_1e4noseg_bigholes_firstpseudo_swa_roc_auc_score/timm_efficientnet_b5_unet_32bs_640res_lesslaugs_ls005_shedchanged_startpoint_difflrs_segbranch_125coefs_1e4noseg_bigholes_firstpseudo_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
))
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_32bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_morepseudo_swa_roc_auc_score/timm_efficientnet_b5_unet_32bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_morepseudo_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
))
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_pseudo_swa_roc_auc_score/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_pseudo_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
))
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_swa_roc_auc_score/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
))
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()
net = load_net('.. /input/wheat-effdet5-fold0-best-checkpoint/fold0-best-all-states.bin' )<categorify>
if not SKIP_VAL:
val_dfs = [
train.iloc[split[i][1]] for i in range(5)
 ]
val_loaders = create_val_loaders(
 loader_initilizer=RANZCRDataset,
loader_config={
"root":'train_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor()
},
dfs=val_dfs,
batch_size=32 
)
 train_logits = predict_over_all_train(
 val_loaders,
models_512[0],
cnn_model_predict,
DEVICE 
)
 val_dfs = pd.concat(val_dfs ).reset_index(drop=True)
 oof_score = roc_auc_score(val_dfs.iloc[:,1:-1], train_logits)
 print(f"OOF score : {oof_score}" )
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[:, [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 )<categorify>
def predict_test_with_multiple_models(
 my_models: List[List[torch.nn.Module]],
my_loaders: List[torch.utils.data.DataLoader],
predict_func: Callable,
device: str, 
):
logits = []
for my_loader in my_loaders:
temp_logits = []
for batch in tqdm(my_loader):
temp_logits_inner = []
for exp_models in my_models:
logit = np.stack(
 [predict_func(batch, m, device)for m in exp_models], axis=0 
)
 temp_logits_inner.append(logit)
 temp_logits.append(np.stack(temp_logits_inner, axis=0))
logits.append(np.concatenate(temp_logits, axis=2))
return np.stack(logits, axis=0 )
def process_det(index, det, score_threshold=0.25):
boxes = det[index].detach().cpu().numpy() [:,:4]
scores = det[index].detach().cpu().numpy() [:,4]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes =(boxes ).clip(min=0, max=511 ).astype(int)
 indexes = np.where(scores>score_threshold)
 boxes = boxes[indexes]
scores = scores[indexes]
return boxes, scores<concatenate>
INF_BS = 32
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]))<categorify>
all_test_loaders_512 = []
test_original = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor()
})
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_original,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))
test_hf = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor() ,
"augmentations": albu.HorizontalFlip(p=1.0)
 })
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_hf,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))
test_left_rot = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor() ,
"augmentations": albu.ShiftScaleRotate(
 shift_limit=0,
scale_limit=0,
rotate_limit=(15,16),
p=1.0 
)
 })
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_left_rot,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))
test_right_rot = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor() ,
"augmentations": albu.ShiftScaleRotate(
 shift_limit=0,
scale_limit=0,
rotate_limit=(-16,-15),
p=1.0 
)
 })
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_right_rot,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))
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=512, iou_thr=0.44, 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<define_variables>
test_logits_512 = predict_test_with_multiple_models(
 models_512,
all_test_loaders_512,
cnn_model_predict,
DEVICE 
)
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 )<categorify>
from scipy.special import expit
results = []
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)
 boxes =(boxes*2 ).round().astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
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 )<save_to_csv>
test_logits = expit(test_logits_512 ).mean(0 ).mean(1 )
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<load_from_csv>
my_exp_1 = test_logits[0]
my_exp_2 = test_logits[1]
my_exp_3 = test_logits[2]
my_exp_4 = test_logits[3]
def load_dataset(root):
csv = pd.read_csv(os.path.join(root, "train.csv"))
data = {}
for i in csv.index:
key = csv["image_id"][i]
bbox = json.loads(csv["bbox"][i])
 bbox = [bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3], 0.0]
if key in data:
data[key].append(bbox)
 else:
data[key] = [bbox]
return sorted(
 [(k, os.path.join(root, "train", k + ".jpg"), v)for k, v in data.items() ],
key=lambda x: x[0] 
)
 def load_image(path):
with open(path, "rb")as f:
buf = f.read()
return mx.image.imdecode(buf)
 def get_batches(dataset, batch_size, width=512, height=512, net=None, ctx=mx.cpu()):
batches = len(dataset)// batch_size
sampler = Sampler(dataset, width, height, net)
 stack_fn = [gcv.data.batchify.Stack() ]
pad_fn = [gcv.data.batchify.Pad(pad_val=-1)]
if net is None:
batchify_fn = gcv.data.batchify.Tuple(*(stack_fn + pad_fn))
else:
batchify_fn = gcv.data.batchify.Tuple(*(stack_fn * 6 + pad_fn))
with Pool(cpu_count() * 2)as p:
for i in range(batches):
start = i * batch_size
samples = p.map(sampler, range(start, start + batch_size))
batch = batchify_fn(samples)
 yield [x.as_in_context(ctx)for x in batch]
def gauss_blur(image, level):
return cv2.blur(image,(level * 2 + 1, level * 2 + 1))
def gauss_noise(image):
for i in range(image.shape[2]):
c = image[:, :, i]
diff = 255 - c.max() ;
noise = np.random.normal(0, random.randint(1, 6), c.shape)
 noise =(noise - noise.min())/(noise.max() - noise.min())
 noise = diff * noise
image[:, :, i] = c + noise.astype(np.uint8)
 return image
class YOLO3TrainTransform:
def __init__(self, width, height, net, mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225), **kwargs):
self._width = width
self._height = height
self._mean = mean
self._std = std
self._fake_x = mx.nd.zeros(( 1, 3, height, width))
net = copy.deepcopy(net)
 net.collect_params().reset_ctx(None)
 with mx.autograd.train_mode() :
_, self._anchors, self._offsets, self._feat_maps, _, _, _, _ = net(self._fake_x)
 self._target_generator = gcv.model_zoo.yolo.yolo_target.YOLOV3PrefetchTargetGenerator(
 num_class=len(net.classes), **kwargs)
 def __call__(self, img, label):
if np.random.uniform(0, 1)> 0.5:
img, expand = gcv.data.transforms.image.random_expand(img, max_ratio=1.5, fill=114, keep_ratio=False)
 bbox = gcv.data.transforms.bbox.translate(label, x_offset=expand[0], y_offset=expand[1])
 else:
img, bbox = img, label
h, w, _ = img.shape
bbox, crop = gcv.data.transforms.experimental.bbox.random_crop_with_constraints(bbox,(w, h))
x0, y0, w, h = crop
img = mx.image.fixed_crop(img, x0, y0, w, h)
 h, w, _ = img.shape
interp = np.random.randint(0, 5)
 img = gcv.data.transforms.image.imresize(img, self._width, self._height, interp=interp)
 bbox = gcv.data.transforms.bbox.resize(bbox,(w, h),(self._width, self._height))
h, w, _ = img.shape
img, flips = gcv.data.transforms.image.random_flip(img, px=0.5, py=0.5)
 bbox = gcv.data.transforms.bbox.flip(bbox,(w, h), flip_x=flips[0], flip_y=flips[1])
 img = gcv.data.transforms.experimental.image.random_color_distort(img)
 img = mx.nd.image.to_tensor(img)
 img = mx.nd.image.normalize(img, mean=self._mean, std=self._std)
 gt_bboxes = mx.nd.array(bbox[np.newaxis, :, :4])
 gt_ids = mx.nd.array(bbox[np.newaxis, :, 4:5])
 gt_mixratio = mx.nd.array(bbox[np.newaxis, :, -1:])
 objectness, center_targets, scale_targets, weights, class_targets = self._target_generator(
 self._fake_x, self._feat_maps, self._anchors, self._offsets,
gt_bboxes, gt_ids, gt_mixratio)
 return(img, objectness[0], center_targets[0], scale_targets[0], weights[0],
class_targets[0], gt_bboxes[0])
 class Sampler:
def __init__(self, dataset, width, height, net=None, **kwargs):
self._dataset = dataset
if net is None:
self._training_mode = False
self._transform = gcv.data.transforms.presets.yolo.YOLO3DefaultValTransform(width, height, **kwargs)
 else:
self._training_mode = True
self._transform = YOLO3TrainTransform(width, height, net, **kwargs)
 def __call__(self, idx):
if self._training_mode:
raw, bboxes = self._load_mixup(idx)
 raw = raw.asnumpy()
blur = random.randint(0, 3)
 if blur > 0:
raw = gauss_blur(raw, blur)
 raw = gauss_noise(raw)
 h, w, _ = raw.shape
rot = random.randint(0, 3)
 if rot > 0:
raw = np.rot90(raw, k=rot)
 if rot == 1:
raw_bboxes = bboxes.copy()
bboxes[:, [0, 2]] = raw_bboxes[:, [1, 3]]
bboxes[:, [1, 3]] = w - raw_bboxes[:, [2, 0]]
elif rot == 2:
bboxes[:, [0, 1, 2, 3]] = np.array([[w, h, w, h]])- bboxes[:, [2, 3, 0, 1]]
elif rot == 3:
raw_bboxes = bboxes.copy()
bboxes[:, [0, 2]] = h - raw_bboxes[:, [1, 3]]
bboxes[:, [1, 3]] = raw_bboxes[:, [2, 0]]
raw_bboxes = bboxes.copy()
bboxes[:, 0] = np.min(raw_bboxes[:, [0, 2]], axis=1)
 bboxes[:, 1] = np.min(raw_bboxes[:, [1, 3]], axis=1)
 bboxes[:, 2] = np.max(raw_bboxes[:, [0, 2]], axis=1)
 bboxes[:, 3] = np.max(raw_bboxes[:, [1, 3]], axis=1)
 raw = mx.nd.array(raw)
 else:
raw = load_image(self._dataset[idx][1])
 bboxes = np.array(self._dataset[idx][2])
 res = self._transform(raw, bboxes)
 return [mx.nd.array(x)for x in res]
def _load_mixup(self, idx1):
r = random.gauss(0.5, 0.5 / 1.96)
 if r > 0.0:
raw1 = load_image(self._dataset[idx1][1])
 bboxes1 = np.array(self._dataset[idx1][2])
 if r >= 1.0:
return raw1, np.hstack([bboxes1, np.full(( bboxes1.shape[0], 1), 1.0)])
 idx2 = random.randint(0, len(self._dataset)- 1)
 raw2 = load_image(self._dataset[idx2][1])
 bboxes2 = np.array(self._dataset[idx2][2])
 if r <= 0.0:
return raw2, np.hstack([bboxes2, np.full(( bboxes2.shape[0], 1), 1.0)])
 h = max(raw1.shape[0], raw2.shape[0])
 w = max(raw1.shape[1], raw2.shape[1])
 mix_raw = mx.nd.zeros(shape=(h, w, 3), dtype="float32")
 mix_raw[:raw1.shape[0], :raw1.shape[1], :] += raw1.astype("float32")* r
mix_raw[:raw2.shape[0], :raw2.shape[1], :] += raw2.astype("float32")*(1.0 - r)
 mix_bboxes = np.vstack([
np.hstack([bboxes1, np.full(( bboxes1.shape[0], 1), r)]),
np.hstack([bboxes2, np.full(( bboxes2.shape[0], 1), 1.0 - r)])
 ])
 return mix_raw.astype("uint8"), mix_bboxes 
<load_pretrained>
blend =(
 my_exp_1**0.5 +
my_exp_2**0.5 +
my_exp_3**0.5 +
my_exp_3**0.5
)
def load_model(path, ctx=mx.cpu()):
net = gcv.model_zoo.yolo3_darknet53_custom(["wheat"], pretrained_base=False)
 net.set_nms(post_nms=150)
 net.load_parameters(path, ctx=ctx)
 return net 
<normalization>
sample_sub.iloc[:,1:] = blend
sample_sub
def inference(path):
raw = load_image(path)
 rh, rw, _ = raw.shape
classes_list = []
scores_list = []
bboxes_list = []
for _ in range(5):
img, flips = gcv.data.transforms.image.random_flip(raw, px=0.5, py=0.5)
 x, _ = gcv.data.transforms.presets.yolo.transform_test(img, short=img_s)
 _, _, xh, xw = x.shape
rot = random.randint(0, 3)
 if rot > 0:
x = np.rot90(x.asnumpy() , k=rot, axes=(2, 3))
classes, scores, bboxes = model(mx.nd.array(x, ctx=context))
if rot > 0:
if rot == 1:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = xh - raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = raw_bboxes[0, :, [2, 0]]
elif rot == 2:
bboxes[0, :, [0, 1, 2, 3]] = mx.nd.array([[xw], [xh], [xw], [xh]], ctx=context)- bboxes[0, :, [2, 3, 0, 1]]
elif rot == 3:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = xw - raw_bboxes[0, :, [2, 0]]
raw_bboxes = bboxes.copy()
bboxes[0, :, 0] = raw_bboxes[0, :, [0, 2]].min(axis=0)
 bboxes[0, :, 1] = raw_bboxes[0, :, [1, 3]].min(axis=0)
 bboxes[0, :, 2] = raw_bboxes[0, :, [0, 2]].max(axis=0)
 bboxes[0, :, 3] = raw_bboxes[0, :, [1, 3]].max(axis=0)
 bboxes[0, :, :] = gcv.data.transforms.bbox.flip(bboxes[0, :, :],(xw, xh), flip_x=flips[0], flip_y=flips[1])
 bboxes[0, :, 0::2] =(bboxes[0, :, 0::2] /(xw - 1)).clip(0.0, 1.0)
 bboxes[0, :, 1::2] =(bboxes[0, :, 1::2] /(xh - 1)).clip(0.0, 1.0)
 classes_list.append([
int(classes[0, i].asscalar())for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 scores_list.append([
scores[0, i].asscalar() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes_list.append([
bboxes[0, i].asnumpy().tolist() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes, scores, classes = weighted_boxes_fusion(bboxes_list, scores_list, classes_list)
 bboxes[:, 0::2] *= rw - 1
bboxes[:, 1::2] *= rh - 1
return bboxes, scores, classes 
<import_modules>
sample_sub.nunique(axis=0 )
import numpy as np
import pandas as pd
import os
from tqdm.auto import tqdm
import shutil as sh<install_modules>
!rm -rf test_images_512_512
sample_sub.to_csv('submission.csv', index=False)
 os.listdir('./' )