Kaggle competition: HuBMAP - Hacking the Kidney
<SOS> metric: Dice Kaggle data source: hubmap-hacking-the-kidney<set_options>
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index -q
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index -q
del net
gc.collect()
torch.cuda.empty_cache()<choose_model_class>
os.environ['SM_FRAMEWORK'] = 'tf.keras'
def load_net() :
config = get_efficientdet_config('tf_efficientdet_d6')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=SIZE
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
net = DetBenchEval(net, config)
 net.eval()
net.model.load_state_dict(torch.load('./pseudo2/best-checkpoint.bin')['model_state_dict'])
 return net.cuda()
net2 = load_net()<choose_model_class>
with open('.. /input/train-fpn-segmentation-model-no-43/hparams.json')as json_file:
hparams = json.load(json_file)
 hparams
def load_net() :
config = get_efficientdet_config('tf_efficientdet_d6')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=SIZE
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
net = DetBenchEval(net, config)
 net.eval()
net.model.load_state_dict(torch.load('./pseudo/best-checkpoint.bin')['model_state_dict'])
 return net.cuda()
net = load_net()<data_type_conversions>
IMG_SIZE = hparams['IMG_SIZE']
SCALE_FACTOR = hparams['SCALE_FACTOR']
K_SPLITS = hparams['K_SPLITS']
def read_tif_file(fname):
img = io.imread(fname)
 img = np.squeeze(img)
 if img.shape[0] == 3:
img = img.swapaxes(0,1)
 img = img.swapaxes(1,2)
 return img
def map_img2file(fname):
img = read_tif_file(fname)
 dims = np.array(img.shape)
 ch = 1 if len(dims)== 2 else dims[2]
for i in range(ch):
f = np.memmap('img{}.dat'.format(i), dtype=np.uint8, mode='w+', shape=(dims[0], dims[1]))
f[:] = img[:,:,i] if ch > 1 else img[:,:]
del f
return dims
def get_patch_from_file(dims, pos, psize):
ch = 1 if len(dims)== 2 else dims[2]
patch = np.zeros([psize[0], psize[1]], dtype=np.uint8)if ch == 1 else np.zeros([psize[0], psize[1], ch], dtype=np.uint8)
 for i in range(ch):
f = np.memmap('img{}.dat'.format(i), dtype=np.uint8, mode='r', shape=(dims[0], dims[1]))
p = f[pos[0]:pos[0]+psize[0], pos[1]:pos[1]+psize[1]]
crop = p.shape
if ch == 1:
patch[0:p.shape[0], 0:p.shape[1]] = p
else:
patch[0:p.shape[0], 0:p.shape[1],i] = p
del f
return patch, crop
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]
tta_combinations = [[TTARotate270() ],[None],[TTARotate90() ],[TTARotate180() ],[TTAVerticalFlip() ,TTARotate270() ],[TTAVerticalFlip() ,TTARotate90() ],[TTAVerticalFlip() ],[TTAHorizontalFlip() ] ]
tta_transforms = []
for tta_combination in tta_combinations:
print([tta_transform for tta_transform in tta_combination if tta_transform])
 tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))
def make_tta_predictions(images, score_threshold=STAGE2_THRESHOLD):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
predictions2 = []
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)
 for tta_transform in tta_transforms:
result = []
det = net2(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],
})
 predictions2.append(result)
 return predictions, predictions2
WEIGHTS = [3] * 16 + [2] * 16
def run_wbf(predictions, predictions2, image_index, image_size=SIZE, iou_thr=STAGE2_IOU_THR, skip_box_thr=STAGE2_SKIP_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] ).astype(int ).tolist() for prediction in predictions]
boxes2 = [(prediction[image_index]['boxes']/(image_size-1)).tolist() for prediction in predictions2]
scores2 = [prediction[image_index]['scores'].tolist() for prediction in predictions2]
labels2 = [np.ones(prediction[image_index]['scores'].shape[0] ).astype(int ).tolist() for prediction in predictions2]
if(len(boxes)==0)and(len(boxes2)==0): return boxes, scores, labels
try:
boxes, scores, labels = weighted_boxes_fusion(boxes+boxes2, scores+scores2, labels+labels2, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 except:
return [], [], []
boxes = boxes*(image_size-1)
 return boxes, scores, labels<set_options>
def rle_encode_less_memory(img):
pixels = img.T.flatten()
pixels[0] = 0
pixels[-1] = 0
runs = np.where(pixels[1:] != pixels[:-1])[0] + 2
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )
def get_valid_transforms() :
return A.Compose([
A.Resize(height=SIZE, width=SIZE, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 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=2,
drop_last=False,
collate_fn=collate_fn 
)
 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)
 config = get_efficientdet_config('tf_efficientdet_d6')
 config.num_classes = 1
config.image_size= SIZE
net.model.load_state_dict(torch.load('./pseudo/best-checkpoint.bin')['model_state_dict'])
 net = DetBenchEval(net.model, config ).cuda()
net.eval()
results = []
for images, image_ids in data_loader:
predictions, predictions2 = make_tta_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, predictions2, image_index=i)
 boxes =(boxes ).round().astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
score_threshold = STAGE2_SUPPRESS
indexes = np.where(scores>score_threshold)
 boxes = boxes[indexes]
scores = scores[indexes]
if(len(boxes)== 0):
result = {
'image_id': image_id,
'PredictionString': ""
}
else:
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
indexes = np.where(boxes[:,2]*boxes[:,3] < MAX_BOX*MAX_BOX)
 boxes = boxes[indexes]
scores = scores[indexes]
indexes = np.where(boxes[:,2]*boxes[:,3] > MIN_BOX*MIN_BOX)
 boxes = boxes[indexes]
scores = scores[indexes]
if(len(boxes)== 0):
result = {
'image_id': image_id,
'PredictionString': ""
}
else:
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<set_options>
def create_TTA_batch(img):
if len(img.shape)< 4:
img = np.expand_dims(img, 0)
 batch=np.zeros(( img.shape[0]*8,img.shape[1],img.shape[2],img.shape[3]), dtype=np.float32)
 for i in range(img.shape[0]):
orig = tf.keras.preprocessing.image.img_to_array(img[i,:,:,:])/255.
 batch[i*8,:,:,:] = orig
batch[i*8+1,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)
 batch[i*8+2,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)
 batch[i*8+3,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)
 orig = orig[:, ::-1]
batch[i*8+4,:,:,:] = orig
batch[i*8+5,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)
 batch[i*8+6,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)
 batch[i*8+7,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)
 return batch
def mask_TTA(masks):
batch=np.zeros(( masks.shape[0],masks.shape[1],masks.shape[2],masks.shape[3]), dtype=np.float32)
 for i in range(masks.shape[0]//8):
batch[i*8,:,:,:] = masks[i*8]
batch[i*8+1,:,:,:] = np.rot90(masks[i*8+1], axes=(0, 1), k=3)
 batch[i*8+2,:,:,:] = np.rot90(masks[i*8+2], axes=(0, 1), k=2)
 batch[i*8+3,:,:,:] = np.rot90(masks[i*8+3], axes=(0, 1), k=1)
 batch[i*8+4,:,:,:] = masks[i*8+4][:, ::-1]
batch[i*8+5,:,:,:] = np.rot90(masks[i*8+5], axes=(0, 1), k=3)[:, ::-1]
batch[i*8+6,:,:,:] = np.rot90(masks[i*8+6], axes=(0, 1), k=2)[:, ::-1]
batch[i*8+7,:,:,:] = np.rot90(masks[i*8+7], axes=(0, 1), k=1)[:, ::-1]
return(batch )
!rm pseudo.csv
!rm -r pseudo 
<set_options>
MEANING_OF_LIFE = 42
MEANING_OF_LIFE_REV = int(str(MEANING_OF_LIFE)[::-1])
 PATH = '.. /input/hubmap-kidney-segmentation/test/'
filelist = glob.glob(PATH+'*.tiff')
 if len(filelist)== 5:
filelist = filelist[:1]
SUB_FILE = './submission.csv'
with open(SUB_FILE, 'w')as f:
f.write("id,predicted
")
 MODELS = ['.. /input/train-fpn-segmentation-model-no-43/FPN-model-2']
s_th = MEANING_OF_LIFE+K_SPLITS
p_th = IMG_SIZE*IMG_SIZE//32
size = int(IMG_SIZE * SCALE_FACTOR)
 OVERLAP = size//2
STEP = size-OVERLAP
for file in filelist:
fid = file.replace('\','.' ).replace('/','.' ).split('.')[-2]
print(fid)
 dims = map_img2file(file)
 pmask = np.zeros(dims[:2], dtype=np.uint8)
 x_shft, y_shft = 0,0
if fid == 'afa5e8098':
x_shft, y_shft = MEANING_OF_LIFE, MEANING_OF_LIFE_REV
for modl in range(len(MODELS)) :
print(MODELS[modl])
 mname = MODELS[modl]
with open(mname+'.json', 'r')as m:
lm = m.read()
model = model_from_json(lm)
 model.load_weights(mname+'.h5')
 for x in range(( dims[0]-OVERLAP-x_shft)//STEP + min(1,(dims[0]-OVERLAP-x_shft)% STEP)) :
for y in range(( dims[1]-OVERLAP-y_shft)//STEP + min(1,(dims[1]-OVERLAP-y_shft)% STEP)) :
tile, crop = get_patch_from_file(dims, [x*STEP+x_shft, y*STEP+y_shft], [size,size])
 patch = cv2.resize(tile,
dsize=(IMG_SIZE, IMG_SIZE),
interpolation = cv2.INTER_AREA)
 _, s, _ = cv2.split(cv2.cvtColor(patch, cv2.COLOR_BGR2HSV))
if(s>s_th ).sum() > p_th:
batch = create_TTA_batch(patch)
 preds = model.predict(batch)
 pred = mask_TTA(preds)
 mask = np.rint(np.sum(pred, axis=0))
pint =cv2.resize(mask.astype(int), dsize=(size, size), interpolation = cv2.INTER_NEAREST)
 pmask[x*STEP:x*STEP+crop[0], y*STEP:y*STEP+crop[1]] += pint[0:crop[0], 0:crop[1]].astype(np.uint8)
 pmask = pmask >= MEANING_OF_LIFE_REV - K_SPLITS
with open(SUB_FILE, 'a')as f:
f.write("{},".format(fid))
f.write(rle_encode_less_memory(pmask))
f.write("
" )
<define_variables><EOS> 
%rm -f *.dat