Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<install_modules>
warnings.simplefilter("ignore")
!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<init_hyperparams>
print('Train images: %d' %len(os.listdir(os.path.join(WORK_DIR, "train"))))
%%time
def detect(save_img=False):
weights, imgsz = opt.weights,opt.img_size
source = '.. /input/global-wheat-detection/test/'
device = torch_utils.select_device(opt.device)
 half = False
models = []
for w in weights:
models.append(torch.load(w, map_location=device)['model'].to(device ).float().eval())
 dataset = LoadImages(source, img_size=1024)
 t0 = time.time()
img = torch.zeros(( 1, 3, imgsz, imgsz), device=device)
 all_path=[]
all_bboxex =[]
all_score =[]
for path, img, im0s, vid_cap in dataset:
img = torch.from_numpy(img ).to(device)
 img = img.half() if half else img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 t1 = torch_utils.time_synchronized()
bboxes_2 = []
score_2 = []
for model in models:
pred = model(img, augment=opt.augment)[0]
pred = non_max_suppression(pred, 0.4, opt.iou_thres,merge=True, classes=None, agnostic=False)
 t2 = torch_utils.time_synchronized()
bboxes = []
score = []
for i, det in enumerate(pred):
p, s, im0 = path, '', im0s
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape ).round()
for c in det[:, -1].unique() :
n =(det[:, -1] == c ).sum()
for *xyxy, conf, cls in det:
if True:
xywh = torch.tensor(xyxy ).view(-1 ).numpy()
bboxes.append(xywh)
 score.append(conf)
 bboxes_2.append(bboxes)
 score_2.append(score)
 all_path.append(path)
 all_score.append(score_2)
 all_bboxex.append(bboxes_2)
 return all_path,all_score,all_bboxex
class opt:
weights = ['.. /input/foldogw/best.pt','.. /input/fold1gwnew/best.pt','.. /input/fold2gwnew//best.pt',
'.. /input/yolofold330epochs/weights/best_yolov5x_fold3.pt',
'.. /input/fold3wheat30/weights/best_yolov5x_fold4.pt']
img_size = 1024
conf_thres = 0.1
iou_thres = 0.94
augment = True
device = '0'
classes=None
agnostic_nms = True
opt.img_size = check_img_size(opt.img_size)
 with torch.no_grad() :
res = detect()<compute_train_metric>
train = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
train_images = WORK_DIR + "/train/" + train['StudyInstanceUID'] + '.jpg'
ss = pd.read_csv(os.path.join(WORK_DIR, 'sample_submission.csv'))
test_images = WORK_DIR + "/test/" + ss['StudyInstanceUID'] + '.jpg'
label_cols = ss.columns[1:]
labels = train[label_cols].values
train_annot = pd.read_csv(os.path.join(WORK_DIR, "train_annotations.csv"))
print('Labels:
', '*'*20, '
', label_cols.values)
 print('*'*50)
 train.head()
def run_wbf_yolo(boxes,scores, image_size=1024, iou_thr=0.4, skip_box_thr=0.34, weights=None):
labels0 = [np.ones(len(scores[idx])) for idx in range(len(scores)) ]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels0, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 return boxes, scores, labels<compute_train_metric>
BATCH_SIZE = 8 * 1
STEPS_PER_EPOCH = len(train)* 0.85 / BATCH_SIZE
VALIDATION_STEPS = len(train)* 0.15 / BATCH_SIZE
EPOCHS = 30
TARGET_SIZE = 750
all_path,all_score,all_bboxex = res
yolov5preds = {}
for row in range(len(all_path)) :
preds = {}
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes = all_bboxex[row]
scores = all_score[row]
boxes, scores, labels = run_wbf_yolo(boxes,scores)
 yolov5preds[image_id] = [boxes,scores,labels]<choose_model_class>
def build_decoder(with_labels = True,
target_size =(TARGET_SIZE, TARGET_SIZE),
ext = 'jpg'):
def decode(path):
file_bytes = tf.io.read_file(path)
 if ext == 'png':
img = tf.image.decode_png(file_bytes, channels = 3)
 elif ext in ['jpg', 'jpeg']:
img = tf.image.decode_jpeg(file_bytes, channels = 3)
 else:
raise ValueError("Image extension not supported")
 img = tf.cast(img, tf.float32)/ 255.0
img = tf.image.resize(img, target_size)
 return img
def decode_with_labels(path, label):
return decode(path), label
return decode_with_labels if with_labels else decode
def build_augmenter(with_labels = True):
def augment(img):
img = tf.image.random_flip_left_right(img)
 img = tf.image.random_flip_up_down(img)
 img = tf.image.adjust_brightness(img, 0.1)
 return img
def augment_with_labels(img, label):
return augment(img), label
return augment_with_labels if with_labels else augment
def build_dataset(paths, labels = None, bsize = 32, cache = True,
decode_fn = None, augment_fn = None,
augment = True, repeat = True, shuffle = 1024,
cache_dir = ""):
if cache_dir != "" and cache is True:
os.makedirs(cache_dir, exist_ok=True)
 if decode_fn is None:
decode_fn = build_decoder(labels is not None)
 if augment_fn is None:
augment_fn = build_augmenter(labels is not None)
 AUTO = tf.data.experimental.AUTOTUNE
slices = paths if labels is None else(paths, labels)
 dset = tf.data.Dataset.from_tensor_slices(slices)
 dset = dset.map(decode_fn, num_parallel_calls = AUTO)
 dset = dset.cache(cache_dir)if cache else dset
dset = dset.map(augment_fn, num_parallel_calls = AUTO)if augment else dset
dset = dset.repeat() if repeat else dset
dset = dset.shuffle(shuffle)if shuffle else dset
dset = dset.batch(bsize ).prefetch(AUTO)
 return dset
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()<define_variables>
test_df = build_dataset(
 test_images, bsize = BATCH_SIZE, repeat = False,
shuffle = False, augment = False, cache = False)
 test_df
best_iou_thr = 0.432
best_skip_box_thr = 0.397<load_pretrained>
models = [
load_net('.. /input/kernel5c5dc38533/effdet5-cutmix-augmix-bboxaug-0/last-checkpoint.bin'),
load_net('.. /input/fold-1-global-wheat/effdet5-cutmix-augmix-bboxaug-1/last-checkpoint.bin'),
load_net('.. /input/fold-2-global-wheat/effdet5-cutmix-augmix-bboxaug-2/last-checkpoint.bin'),
load_net('.. /input/fold-3-bbox-aug-50/effdet5-cutmix-augmix-bboxaug-3/last-checkpoint.bin'),
load_net('.. /input/fold-4-bbox-aug-50/effdet5-cutmix-augmix-bboxaug-4/last-checkpoint.bin'),
load_net('.. /input/fold-0/effdet5-cutmix-augmix-0/last-checkpoint.bin'),
load_net('.. /input/fold-1/effdet5-cutmix-augmix-1/last-checkpoint.bin'),
load_net('.. /input/fold-2-augmix/effdet5-cutmix-augmix-2/last-checkpoint.bin'),
load_net('.. /input/fold-4/effdet5-cutmix-augmix-4/last-checkpoint.bin')
 ]<data_type_conversions>
print('Our Xception CNN has %d layers' %len(model.layers))
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.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.25, 
):
images = torch.stack(images ).cuda().float()
predictions = []
for fold_number, net in enumerate(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
def run_wbf(predictions, image_index, image_size=512, iou_thr=best_iou_thr, skip_box_thr=best_skip_box_thr, 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 
<predict_on_test>
img_tensor = build_dataset(
 pd.Series(test_images[0]), bsize = 1,repeat = False,
shuffle = False, augment = False, cache = False )
<define_variables><EOS> 
ss[label_cols] = model.predict(test_df, verbose = 1)
 ss.to_csv('submission.csv', index = False )