Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<create_dataframe>
class CFG:
device = 'GPU'
cpu_workers = 2
debug = True
seed = 13353
batch_size = 50
num_tta = 2
num_folds = 3
fold_idx = False
fold_blend = 'pmean'
model_blend = 'pmean'
power = 1/11
w_public = 0.25
lgb_folds = 5
label_features = False
sort_targets = True
pred_as_feature = True
lgb_stop_rounds = 200
lgb_params = {'objective': 'binary',
'metrics': 'auc',
'n_estimators': 10000,
'learning_rate': 0.01,
'num_leaves': 8,
'max_depth': 5,
'min_child_samples': 20,
'subsample': 0.3,
'colsample_bytree': 0.5,
'reg_alpha': 0.1,
'reg_lambda': 0.1,
'silent': True,
'verbosity': -1,
'n_jobs' : -1,
'random_state': 13353}
data_path = '/kaggle/input/ranzcr-clip-catheter-line-classification/'
models = ['/kaggle/input/ranzcr-v12/',
'/kaggle/input/ranzcr-v15-pub/',
'/kaggle/input/ranzcr-v17-pub/',
'/kaggle/input/ranzcr-v13-pub/',
'/kaggle/input/ranzcr-v14-pub/']
y = effdetpreds.copy()
x = yolov5preds.copy()<statistical_test>
CFG = dict(vars(CFG))
for key in ['__dict__', '__doc__', '__module__', '__weakref__']:
del CFG[key]
def run_last_wbf(yolo,eff,weights=[2,1],iou_thr=0.5,skip_box_thr=0.397):
box1,scores1,labels1 = yolo
box2,scores2,labels2 = eff
box1 = box1/1023
box2 = box2/1023
boxes, scores, labels = weighted_boxes_fusion([box1,box2], [scores1,scores2],
[labels1,labels2],weights=[2,1],
iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes,scores<create_dataframe>
CFGs = []
for model in CFG['models']:
model_cfg = pickle.load(open(model + 'configuration.pkl', 'rb'))
CFGs.append(model_cfg)
 print('Numer of models:', len(CFGs))
iou_thr = 0.5
skip_box_thr = 0.0001
results = []
for row in range(len(all_path)) :
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes,scores = run_last_wbf(yolov5preds[image_id],effdetpreds[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)
 test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'] )<save_to_csv>
pd.set_option('display.max_columns', 100)
 ImageFile.LOAD_TRUNCATED_IMAGES = True
%matplotlib inline
warnings.filterwarnings('ignore')
 sys.path.append('.. /input/timm-pytorch-image-models/pytorch-image-models-master')
test_df.to_csv('submission.csv', index=False)
 test_df.head()<install_modules>
if CFG['device'] == 'GPU':
print('Training on GPU...')
 device = torch.device('cuda:0')
 if CFG['device'] == 'CPU':
print('Training on CPU...')
 device = torch.device('cpu' )
! pip install.. /input/mmdetectionv260/addict-2.4.0-py3-none-any.whl
! pip install.. /input/mmdetectionv260/mmcv_full-latesttorch1.6.0cu102-cp37-cp37m-manylinux1_x86_64.whl
! pip install.. /input/mmdetectionv260/mmpycocotools-12.0.3-cp37-cp37m-linux_x86_64.whl
! pip install.. /input/mmdetection-package/mmdet-2.7.0-py3-none-any.whl<install_modules>
def get_score(y_true, y_pred):
scores = []
for i in range(y_true.shape[1]):
score = roc_auc_score(y_true[:,i], y_pred[:,i])
 scores.append(score)
 avg_score = np.mean(scores)
 return avg_score, scores
def compute_blend(df, preds, blend, CFG, weights = None):
if weights is None:
weights = np.ones(len(preds)) / len(preds)
 if blend == 'amean':
out = np.sum(df[preds].values * weights, axis = 1)
 elif blend == 'median':
out = df[preds].median(axis = 1)
 elif blend == 'gmean':
out = np.prod(np.power(df[preds].values, weights), axis = 1)
 elif blend == 'pmean':
out = np.sum(np.power(df[preds].values, CFG['power'])* weights, axis = 1)**(1 / CFG['power'])
 elif blend == 'rmean':
out = np.sum(df[preds].rank(pct = True ).values * weights, axis = 1)
 return out
def seed_everything(seed):
os.environ['PYTHONHASHSEED'] = str(seed)
 random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.backends.cudnn.deterministic = True
print('setting random seed to {}...'.format(seed))
seed_everything(CFG['seed'] )
! pip install.. /input/mmdetection-package/torch-1.6.0-cp37-cp37m-linux_x86_64.whl<install_modules>
df = pd.read_csv(CFG['data_path'] + 'sample_submission.csv')
 CFG['targets'] = ['ETT - Abnormal',
'ETT - Borderline',
'ETT - Normal',
'NGT - Abnormal',
'NGT - Borderline',
'NGT - Incompletely Imaged',
'NGT - Normal',
'CVC - Abnormal',
'CVC - Borderline',
'CVC - Normal',
'Swan Ganz Catheter Present']
CFG['num_classes'] = len(CFG['targets'])
 if CFG['debug'] and len(df)== 3582:
print('Subsetting data in the debug mode...')
 df = df.head(CFG['batch_size'])
 print(df.shape )
<install_modules>
for m in CFG['models']:
tmp_train_preds = pd.read_csv(m + '/oof.csv')
 tmp_train_preds.columns = ['StudyInstanceUID'] + CFG['targets'] + ['PatientID', 'fold'] + [m + ' ' + c for c in CFG['targets']]
if m == CFG['models'][0]:
train_preds = tmp_train_preds
else:
train_preds = train_preds.merge(tmp_train_preds[['StudyInstanceUID'] + [m + ' ' + c for c in CFG['targets']]], how = 'left', on = 'StudyInstanceUID')
 weights = []
for model_idx, m in enumerate(CFG['models']):
score = 0
for fold_idx in range(5):
tmp_train_preds = train_preds.loc[train_preds['fold'] == fold_idx]
score += get_score(tmp_train_preds[CFG['targets']].values, tmp_train_preds[[m + ' ' + c for c in CFG['targets']]].values)[0] / 5
weights.append(score)
 sorted_ids = list(np.argsort(np.array(weights)))
 sorted_weights = [weights[i] for i in sorted_ids]
CFG['models'] = [CFG['models'][i] for i in sorted_ids]
CFGs = [CFGs[i] for i in sorted_ids]
print('-' * 45)
 print('{:<5}{:<33}{:>5}'.format('ID', 'Model', 'AUC'))
print('-' * 45)
 for model_idx, m in enumerate(CFG['models']):
print('{:<5}{:<33}{:.4f}'.format(model_idx + 1, m, sorted_weights[model_idx]))
print('-' * 45 )
! cp -r.. /input/mmdetection-wheat-models/attention_stage1/faster_rcnn_r50_fpn_attention_0010_dcn_albu_1x4_1x_bWheat_kaggle.py./config.py
! cp -r.. /input/mmdetection-wheat-models/attention_stage1/epoch_12.pth./model.pth<define_variables>
for c in CFG['targets']:
class_preds = train_preds.filter(like = 'kaggle' ).filter(like = c ).columns
for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
train_preds[blend + ' ' + c] = compute_blend(train_preds, class_preds, blend, CFG)
 for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
train_preds['w' + blend + ' ' + c] = compute_blend(train_preds, class_preds, blend, CFG, weights = np.array(sorted_weights))
print('-' * 18)
 print('{:<10}{:>5}'.format('Blend', 'AUC'))
print('-' * 18)
 for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
score = 0
for fold_idx in range(5):
tmp_train_preds = train_preds.loc[train_preds['fold'] == fold_idx]
score += get_score(tmp_train_preds[CFG['targets']].values, tmp_train_preds[[blend + ' ' + c for c in CFG['targets']]].values)[0] / 5
print('{:<10}{:>5.4f}'.format(blend, score))
print('-' * 18)
 for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
score = 0
for fold_idx in range(5):
tmp_train_preds = train_preds.loc[train_preds['fold'] == fold_idx]
score += get_score(tmp_train_preds[CFG['targets']].values, tmp_train_preds[['w' + blend + ' ' + c for c in CFG['targets']]].values)[0] / 5
print('{:<10}{:>5.4f}'.format('w' + blend, score))
print('-' * 18 )
CONFIG_FILE = './config.py'
CHECKPOINT_PATH = './model.pth'
TEST_IMG_DIR = '.. /input/global-wheat-detection/test'<import_modules>
def get_dataset(CFG):
class ImageData(Dataset):
def __init__(self,
df,
path,
transform = None,
labeled = False,
indexed = False):
self.df = df
self.path = path
self.transform = transform
self.labeled = labeled
self.indexed = indexed
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
path = os.path.join(self.path, self.df.iloc[idx]['StudyInstanceUID'] + '.jpg')
 image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
 if image is None:
raise FileNotFoundError(path)

mask = image > 0
image = image[np.ix_(mask.any(1), mask.any(0)) ]
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform is not None:
image = self.transform(image = image)['image']
if CFG['normalize'] == 'public':
image = image.astype(np.float32)
 image = image.transpose(2, 0, 1)
 image = torch.tensor(image ).float()
if self.labeled:
label = torch.tensor(self.df.iloc[idx][CFG['targets']] ).float()
if self.indexed:
idx = torch.tensor(idx)
 return idx, image, label
else:
return image, label
else:
return image
return ImageData
import os
import json
import pandas as pd
import numpy as np
import cv2
from tqdm import tqdm
import torch
import mmcv
from mmdet.apis import init_detector, inference_detector<define_variables>
def get_model(CFG, device, num_classes):
if CFG['weights'] != 'public':
model = timm.create_model(model_name = CFG['backbone'],
pretrained = False,
in_chans = CFG['channels'])
 if 'efficient' in CFG['backbone']:
model.classifier = nn.Linear(model.classifier.in_features, num_classes)
 else:
model.fc = nn.Linear(model.fc.in_features, num_classes)
 else:
class CustomModel(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
model = CustomModel(CFG['backbone'], num_classes, True)
 return model
TEST_ANN_FILE = './annotation_test.json'
anns = []
for img_name in tqdm(os.listdir(TEST_IMG_DIR)) :
if not img_name.endswith('.jpg'):
continue
anns.append(dict(filename=img_name, boxes=[]))<load_pretrained>
cv_start = time.time()
gc.collect()
all_counter = 0
fold_counter = 0 if not CFG['fold_idx'] else CFG['fold_idx']
all_cnn_preds = None
for model_idx in range(len(CFG['models'])) :
ImageData = get_dataset(CFGs[model_idx])
 test_dataset = ImageData(df = df,
path = CFG['data_path'] + 'test/',
transform = get_augs(CFGs[model_idx], image_size = CFGs[model_idx]['image_size']),
labeled = False,
indexed = False)
 test_loader = DataLoader(test_dataset,
batch_size = CFG['batch_size'],
shuffle = False,
num_workers = CFG['cpu_workers'],
pin_memory = True)
 for fold_idx in tqdm(range(CFG['num_folds'])) :
model = get_model(CFGs[model_idx], device = device, num_classes = CFG['num_classes'])
 model = model.to(device)
 model.load_state_dict(torch.load(CFG['models'][model_idx] + 'weights_fold{}.pth'.format(fold_counter),map_location = device))
model.eval()
PROBS = []
with torch.no_grad() :
for batch_idx, inputs in enumerate(test_loader):
inputs = inputs.to(device)
 probs = torch.zeros(( inputs.shape[0], CFG['num_classes']), device = device)
 for tta_idx in range(CFG['num_tta']):
preds = model(get_tta_flips(inputs, tta_idx))
probs += preds.sigmoid()
PROBS.append(probs.detach().cpu() / CFG['num_tta'])
 cnn_preds = pd.DataFrame(torch.cat(PROBS ).numpy() , columns = [CFG['models'][model_idx] + str(fold_idx)+ '/' + c for c in CFG['targets']])
 all_cnn_preds = pd.concat([all_cnn_preds, cnn_preds], axis = 1)
 all_counter += 1
if not CFG['fold_idx']:
fold_counter += 1
if fold_counter == CFG['num_folds']:
fold_counter = 0
del model, inputs, preds, probs, PROBS
gc.collect()
del test_loader, test_dataset
gc.collect()
print('Finished {} preds x {} TTA in {:.2f} hours'.format(all_counter, CFG['num_tta'],(time.time() - cv_start)/ 3600))
with open('./result.bbox.json', 'r')as f1, open(TEST_ANN_FILE, 'r')as f2:
result_info = json.load(f1)
 annotations_info = json.load(f2)
 for i, ann in tqdm(enumerate(result_info)) :
if ann['score'] < 0.5:
continue
annotation = ann
annotation['id'] = i
annotation['area'] = ann['bbox'][2] * ann['bbox'][3]
annotation['iscrowd'] = 0
annotations_info['annotations'].append(annotation)
 mmcv.dump(annotations_info, './annotation_new.json' )<set_options>
print('Blending fold predictions with: ' + CFG['fold_blend'])
 for m in CFG['models']:
for c in CFG['targets']:
class_preds = all_cnn_preds.filter(like = m ).filter(like = c ).columns
all_cnn_preds[m + c] = compute_blend(all_cnn_preds, class_preds, CFG['fold_blend'], CFG)
 all_cnn_preds.drop(class_preds, axis = 1, inplace = True)
 all_cnn_preds.head()
train_config = './train_config.py'
cfg = mmcv.Config.fromfile(CONFIG_FILE)
 cfg.data.samples_per_gpu = 8
cfg.data.workers_per_gpu = 4
cfg.data.train.ann_file = './annotation_new.json'
cfg.data.train.img_prefix = TEST_IMG_DIR
cfg.data.train.pipeline = [
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', with_bbox=True),
dict(
 type='Albu',
transforms=cfg.albu_train_transforms,
bbox_params=dict(
 type='BboxParams',
format='pascal_voc',
label_fields=['gt_labels'],
min_visibility=0.3,
filter_lost_elements=True),
keymap=dict(
 img='image',
gt_bboxes='bboxes'),
update_pad_shape=False,
skip_img_without_anno=True),
dict(type='Resize', img_scale=(1024, 1024), keep_ratio=True),
dict(type='RandomFlip', flip_ratio=0.5),
dict(type='Normalize', **cfg.img_norm_cfg),
dict(type='Pad', size_divisor=32),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
]
cfg.optimizer = dict(type='SGD', lr=0.005, momentum=0.9, weight_decay=0.0001)
 cfg.lr_config = dict(
 policy='fixed',
warmup=None 
)
 cfg.total_epochs = 2
cfg.model.pretrained = None
cfg.load_from = CHECKPOINT_PATH
cfg.log_config = dict(
 interval=1,
hooks=[
dict(type='TextLoggerHook')
 ])
 with open(train_config, 'w')as f:
f.write(cfg.pretty_text )<install_modules>
for m in CFG['models']:
tmp_train_preds = pd.read_csv(m + '/oof.csv')
 tmp_train_preds.columns = ['StudyInstanceUID'] + CFG['targets'] + ['PatientID', 'fold'] + [m + '' + c for c in CFG['targets']]
if m == CFG['models'][0]:
train_preds = tmp_train_preds
else:
train_preds = train_preds.merge(tmp_train_preds[['StudyInstanceUID'] + [m + '' + c for c in CFG['targets']]], how = 'left', on = 'StudyInstanceUID')
 train_preds.head()
! python./mmdetection/tools/train.py {train_config} --no-validate --work-dir./pseudo<define_variables>
test_preds = all_cnn_preds.copy()
test_preds = pd.concat([df['StudyInstanceUID'], test_preds], axis = 1)
 test_preds.head()
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 )<set_options>
X = train_preds.copy()
X_test = test_preds.copy()
drop_features = ['StudyInstanceUID', 'PatientID', 'fold'] + CFG['targets']
features = [f for f in X.columns if f not in drop_features]
print(len(features), 'features')
 display(features )
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'<load_pretrained>
folds = pd.read_csv('/kaggle/input/how-to-properly-split-folds/train_folds.csv')
 del X['fold']
X = X.merge(folds[['StudyInstanceUID', 'fold']], how = 'left', on = 'StudyInstanceUID' )
config = mmcv.Config.fromfile(CONFIG_FILE)
 config.model.pretrained = None
config.data.test.pipeline[1]['img_scale'] = [(1280, 1280),(1408, 1408)]
model = init_detector(config, './pseudo/epoch_1.pth', device=device)
 model.eval()<predict_on_test>
if CFG['sort_targets']:
sorted_targets = ['Swan Ganz Catheter Present',
'ETT - Normal',
'ETT - Abnormal',
'ETT - Borderline',
'NGT - Abnormal',
'NGT - Normal',
'NGT - Incompletely Imaged',
'NGT - Borderline',
'CVC - Abnormal',
'CVC - Normal',
'CVC - Borderline']
results = []
score_threshold = 0.3
with torch.no_grad() :
for img_name in tqdm(os.listdir(TEST_IMG_DIR)) :
img_pth = os.path.join(TEST_IMG_DIR, img_name)
 image = mmcv.imread(img_pth)
 result = inference_detector(model, image)
 boxes = result[0][:, :4]
scores = result[0][:, 4]
if len(boxes)> 0:
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
boxes = boxes[scores >= score_threshold]
scores = scores[scores >= score_threshold]
result = {
'image_id': img_name[:-4],
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result )<save_to_csv>
cnn_oof = np.zeros(( len(X), CFG['num_classes']))
lgb_oof = np.zeros(( len(X), CFG['num_classes']))
lgb_tst = np.zeros(( len(X_test), CFG['lgb_folds'], CFG['num_classes']))
all_lgb_preds = None
cv_start = time.time()
print('-' * 45)
 print('{:<28}{:<7}{:>5}'.format('Label', 'Model', 'AUC'))
print('-' * 45)
 for label in sorted_targets:
y = X[label]
label_features = [f for f in features if label in f] if CFG['label_features'] else features
lgb_auc = 0
cnn_auc = 0
for fold in range(CFG['lgb_folds']):
trn_idx = X.loc[X['fold'] != fold].index
val_idx = X.loc[X['fold'] == fold].index
X_train, y_train = X.iloc[trn_idx][label_features], y.iloc[trn_idx]
X_valid, y_valid = X.iloc[val_idx][label_features], y.iloc[val_idx]
X_test_label = X_test[label_features]
clf = lgb.LGBMClassifier(**CFG['lgb_params'])
 clf = clf.fit(X_train, y_train,
eval_set = [(X_valid, y_valid)],
early_stopping_rounds = CFG['lgb_stop_rounds'],
verbose = False)
 cnn_oof[val_idx, CFG['targets'].index(label)] = compute_blend(X_valid, list(X_valid.filter(like = label ).columns), CFG['fold_blend'], CFG)
 lgb_oof[val_idx, CFG['targets'].index(label)] = clf.predict_proba(X_valid)[:, 1]
lgb_tst[:, fold, CFG['targets'].index(label)] = clf.predict_proba(X_test_label)[:, 1]
cnn_auc += roc_auc_score(y_valid ,cnn_oof[val_idx, CFG['targets'].index(label)])/ CFG['lgb_folds']
lgb_auc += roc_auc_score(y_valid, lgb_oof[val_idx, CFG['targets'].index(label)])/ CFG['lgb_folds']
print('{:<29}{:<7}{:>5.4f}'.format(label, 'CNN', cnn_auc))
print('{:<29}{:<7}{:>5.4f}'.format(label, 'LGB', lgb_auc))
print('-' * 45)
 if cnn_auc >= lgb_auc:
for fold in range(CFG['lgb_folds']):
val_idx = X.loc[X['fold'] == fold].index
X_valid, y_valid = X.iloc[val_idx][label_features], y.iloc[val_idx]
lgb_oof[val_idx, CFG['targets'].index(label)] = compute_blend(X_valid, list(X_valid.filter(like = label ).columns), CFG['fold_blend'], CFG)
 lgb_tst[:, fold, CFG['targets'].index(label)] = compute_blend(X_test_label, list(X_test_label.filter(like = label ).columns), CFG['fold_blend'], CFG)
 for fold in range(CFG['lgb_folds']):
lgb_preds = pd.DataFrame(lgb_tst[:, fold, CFG['targets'].index(label)], columns = [str(fold)+ '/' + label])
 all_lgb_preds = pd.concat([all_lgb_preds, lgb_preds], axis = 1)
 if CFG['pred_as_feature']:
X['LGB ' + label] = lgb_oof[:, CFG['targets'].index(label)]
X_test['LGB ' + label] = np.mean(lgb_tst[:, :, CFG['targets'].index(label)], axis = 1)
 features.append('LGB ' + label)
 score_cnn = 0
score_lgb = 0
for fold in range(CFG['lgb_folds']):
val_idx = X.loc[X['fold'] == fold].index
score_cnn += get_score(X.iloc[val_idx][CFG['targets']].values, cnn_oof[val_idx, :])[0] / CFG['lgb_folds']
score_lgb += get_score(X.iloc[val_idx][CFG['targets']].values, lgb_oof[val_idx, :])[0] / CFG['lgb_folds']
print('{:<29}{:<7}{:>5.4f}'.format('OVERALL', 'CNN', score_cnn))
print('{:<29}{:<7}{:>5.4f}'.format('OVERALL', 'LGB', score_lgb))
print('-' * 45)
 print('Finished in {:.2f} minutes'.format(( time.time() - cv_start)/ 60))
del cnn_oof, lgb_tst, lgb_oof, clf, X_train, X_valid, X_test, X_test_label, y_train, y_valid
del features, label_features, trn_idx, val_idx, folds, train_preds, test_preds
gc.collect()
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<install_modules>
print('Blending fold predictions with: ' + CFG['fold_blend'])
 for c in CFG['targets']:
class_preds = all_lgb_preds.filter(like = c ).columns
all_lgb_preds[c] = compute_blend(all_lgb_preds, class_preds, CFG['fold_blend'], CFG)
 all_lgb_preds.drop(class_preds, axis = 1, inplace = True)
 all_lgb_preds.head()
!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>
if CFG['w_public'] > 0:
gc.collect()
BATCH_SIZE = 96
IMAGE_SIZE = 640
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH_resnet200d = '.. /input/resnet200d-public/resnet200d_320_CV9632.pth'
MODEL_PATH_seresnet152d = '.. /input/seresnet152d-cv9615/seresnet152d_320_CV96.15.pth'
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, cv2.IMREAD_GRAYSCALE)
 mask = image > 0
image = image[np.ix_(mask.any(1), mask.any(0)) ]
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
def get_transforms(CFG):
return A.Compose([A.Resize(IMAGE_SIZE, IMAGE_SIZE),
A.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
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.mean(avg_preds, axis = 0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs
models200D = []
model = ResNet200D()
model.load_state_dict(torch.load(MODEL_PATH_resnet200d)['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 del model
models152D = []
model = SeResNet152D()
model.load_state_dict(torch.load(MODEL_PATH_seresnet152d)['model'])
 model.eval()
model.to(device)
 models152D.append(model)
 del model
test_dataset = TestDataset(df, transform = get_transforms(CFG))
test_loader = DataLoader(test_dataset, batch_size = BATCH_SIZE, shuffle = False, num_workers = CFG['cpu_workers'], pin_memory = True)
 predictions200d = inference(models200D, test_loader, device)
 predictions152d = inference(models152D, test_loader, device)
 target_cols = df.iloc[:, 1:12].columns.tolist()
predictions200d = pd.DataFrame(predictions200d, columns = ['200d/' + c for c in target_cols])
 predictions152d = pd.DataFrame(predictions152d, columns = ['152d/' + c for c in target_cols])
 predictions = pd.concat([predictions200d, predictions152d], axis = 1)
 df_pub = predictions.copy()
display(df_pub.head() )
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>
if CFG['w_public'] == 0:
df_pub = all_lgb_preds.copy()
else:
for c in CFG['targets']:
class_preds = df_pub.filter(like = c ).columns
df_pub[c] = compute_blend(df_pub, class_preds, CFG['model_blend'], CFG, weights = np.array([2/3, 1/3]))
df_pub.drop(class_preds, axis = 1, inplace = True)
 df_pub.head()
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>
all_preds = all_lgb_preds.copy()
all_preds.columns = ['my/' + c for c in all_preds.columns]
df_pub.columns = ['public/' + c for c in df_pub.columns]
preds = pd.concat([all_preds, df_pub], axis = 1)
 for c in CFG['targets']:
class_preds = preds.filter(like = c ).columns
preds[c] = compute_blend(preds, class_preds, CFG['model_blend'], CFG, weights = np.array([1 - CFG['w_public'], CFG['w_public']]))
preds.drop(class_preds, axis = 1, inplace = True)
 preds.head()
<choose_model_class><EOS> 
if all_counter == len(CFG['models'] * CFG['num_folds']):
for c in CFG['targets']:
df[c] = preds[c].rank(pct = True)
 df.to_csv('submission.csv', index = False)
 display(df.head() )