Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
train_df['x'] = -1
train_df['y'] = -1
train_df['w'] = -1
train_df['h'] = -1
def expand_bbox(x):
r = np.array(re.findall("([0-9]+[.]?[0-9]*)", x))
if len(r)== 0:
r = [-1, -1, -1, -1]
return r
train_df[['x', 'y', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x)))
 train_df.drop(columns=['bbox'], inplace=True)
 train_df['x'] = train_df['x'].astype(np.float)
 train_df['y'] = train_df['y'].astype(np.float)
 train_df['w'] = train_df['w'].astype(np.float)
 train_df['h'] = train_df['h'].astype(np.float)
 train_df.head()<categorify>
class PAM_Module(nn.Module):

 def __init__(self, in_dim):
super(PAM_Module, self ).__init__()
self.chanel_in = in_dim
self.query_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
 self.key_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
 self.value_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1)
 self.gamma = nn.Parameter(torch.zeros(1))
def forward(self, x):

 m_batchsize, C, height, width = x.size()
proj_query = self.query_conv(x ).view(m_batchsize, -1, width*height ).permute(0, 2, 1)
 proj_key = self.key_conv(x ).view(m_batchsize, -1, width*height)
 energy = torch.bmm(proj_query, proj_key)
 attention = torch.softmax(energy, dim=-1)
 proj_value = self.value_conv(x ).view(m_batchsize, -1, width*height)
 out = torch.bmm(proj_value, attention.permute(0, 2, 1))
out = out.view(m_batchsize, C, height, width)
 out = self.gamma*out + x
return out
class CAM_Module(nn.Module):

 def __init__(self, in_dim):
super(CAM_Module, self ).__init__()
self.chanel_in = in_dim
self.gamma = nn.Parameter(torch.zeros(1))
def forward(self,x):

 m_batchsize, C, height, width = x.size()
proj_query = x.view(m_batchsize, C, -1)
 proj_key = x.view(m_batchsize, C, -1 ).permute(0, 2, 1)
 energy = torch.bmm(proj_query, proj_key)
 energy_new = torch.max(energy, -1, keepdim=True)[0].expand_as(energy)-energy
attention = torch.softmax(energy_new, dim=-1)
 proj_value = x.view(m_batchsize, C, -1)
 out = torch.bmm(attention, proj_value)
 out = out.view(m_batchsize, C, height, width)
 out = self.gamma*out + x
return out
class CBAM(nn.Module):
def __init__(self, in_channels):
super(CBAM, self ).__init__()
inter_channels = in_channels // 4
self.conv1_c = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
nn.BatchNorm2d(inter_channels),
nn.ReLU())
 self.conv1_s = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
nn.BatchNorm2d(inter_channels),
nn.ReLU())
 self.channel_gate = CAM_Module(inter_channels)
 self.spatial_gate = PAM_Module(inter_channels)
 self.conv2_c = nn.Sequential(nn.Conv2d(inter_channels, in_channels, 3, padding=1, bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU())
 self.conv2_a = nn.Sequential(nn.Conv2d(inter_channels, in_channels, 3, padding=1, bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU())
 def forward(self, x):
feat1 = self.conv1_c(x)
 chnl_att = self.channel_gate(feat1)
 chnl_att = self.conv2_c(chnl_att)
 feat2 = self.conv1_s(x)
 spat_att = self.spatial_gate(feat2)
 spat_att = self.conv2_a(spat_att)
 x_out = chnl_att + spat_att
return x_out
class WheatDatasetTest(torch.utils.data.Dataset):
def __init__(self, opt, image_dir, transforms=None,
mean=[0.315290, 0.317253, 0.214556],
std=[0.245211, 0.238036, 0.193879]):
self.opt = opt
self.image_dir = image_dir
self.img_id = os.listdir(self.image_dir)
 self.transforms = transforms
self.mean = np.array(mean, dtype=np.float32 ).reshape(1, 1, 3)
 self.std = np.array(std, dtype=np.float32 ).reshape(1, 1, 3)
 def __len__(self):
return len(self.img_id)
 def __getitem__(self, idx):
img_path = os.path.join(self.image_dir, self.img_id[idx])
 image = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
 h0, w0 = image.shape[0:2]
if self.transforms is not None:
image = self.transforms(image)
 image_resized = cv2.resize(image,(self.opt.img_size, self.opt.img_size))
return image_resized, self.img_id[idx], image, h0, w0<concatenate>
class EffNetWLF(nn.Module):
def __init__(self, model_name, target_size=11):
super().__init__()
self.backbone = EfficientNet.from_name(model_name)
 self.backbone._dropout = nn.Dropout(0.1)
 n_features = self.backbone._fc.in_features
self.backbone._fc = nn.Linear(n_features, target_size)
 self.local_fe = CBAM(n_features)
 self.dropout = nn.Dropout(0.1)
 self.classifier = nn.Sequential(nn.Linear(n_features + n_features, n_features),
nn.BatchNorm1d(n_features),
nn.Dropout(0.1),
nn.ReLU() ,
nn.Linear(n_features, target_size))
def forward(self, image):
enc_feas = self.backbone.extract_features(image)
 global_feas = self.backbone._avg_pooling(enc_feas)
 global_feas = global_feas.flatten(start_dim=1)
 global_feas = self.dropout(global_feas)
 local_feas = self.local_fe(enc_feas)
 local_feas = torch.sum(local_feas, dim=[2,3])
 local_feas = self.dropout(local_feas)
 all_feas = torch.cat([global_feas, local_feas], dim=1)
 outputs = self.classifier(all_feas)
 return outputs
def flip_lr(img):
return np.ascontiguousarray(img[:, ::-1, :])
 def deaug_lr(img, boxes):
h, w = img.shape[:2]
boxes[:,(0, 2)] = w - boxes[:,(2, 0)]
return boxes
def flip_ud(img):
return np.ascontiguousarray(img[::-1, :, :])
 def deaug_ud(img, boxes):
h, w = img.shape[:2]
boxes[:,(1, 3)] = w - boxes[:,(3, 1)]
return boxes<create_dataframe>
work_dir = ".. /input/ranzcr-clip-catheter-line-classification/"
df = pd.read_csv(os.path.join(work_dir, "sample_submission.csv"))
test_img_paths = glob.glob(os.path.join(work_dir, "test/*.jpg"))
print(len(test_img_paths))
model_weights = glob.glob(".. /input/effb2wlf/*.pth")
 print(model_weights )
testdataset = WheatDatasetTest(opt, DIR_TEST)
 print('Total number of images in test set: {}'.format(len(testdataset)))
 testdataset_lr = WheatDatasetTest(opt, DIR_TEST, transforms=flip_lr)
 testdataset_ud = WheatDatasetTest(opt, DIR_TEST, transforms=flip_ud )<find_best_model_class>
normalize = a_transform.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], p=1.0, max_pixel_value=255.0)
 test_transform = a_transform.Compose([a_transform.Resize(512, 512),
normalize,
ToTensorV2() ], p=1.0)
 test_ds = TestDataset(test_img_paths, test_transform)
 dataloader = DataLoader(test_ds, batch_size=16, num_workers=4, shuffle=False, drop_last=False )
<load_pretrained><EOS> 
final_pred = np.empty(( len(model_weights),len(test_ds), 11), dtype=np.float32)
 for model_idx, each_w in enumerate(model_weights):
print(f"running idx {model_idx}")
 model = EffNetWLF("efficientnet-b2")
 model = model.to(device)
 checkpoint = torch.load(f"{each_w}")
 model.load_state_dict(checkpoint["model"])
 uids = []
all_pred = []
for idx,(name, x_mb)in enumerate(dataloader):
x = x_mb.to(device)
 x = torch.stack([x,x.flip(-1)],0)
 x = x.view(-1, 3, 512, 512)
 with torch.no_grad() :
pred = model(x)
 pred = pred.view(2, x_mb.size(0), -1 ).mean(0)
 uids.append(name)
 all_pred.append(torch.sigmoid(pred))
all_pred = torch.cat(all_pred, dim=0 ).cpu().numpy()
final_pred[model_idx] = all_pred
final_pred = final_pred.mean(axis=0)
 print(final_pred.shape)
 target_cols = df.columns[1:]
df[target_cols] = final_pred
df["StudyInstanceUID"] = np.concatenate(uids)
 df.to_csv('submission.csv', index=False)
 df.head()