Kaggle competition: Human Protein Atlas - Single Cell Classification
<SOS> metric: OpenImagesObjDetectionSegmentationAP Kaggle data source: human-protein-atlas-single-cell-classification<feature_engineering>
!pip install -q ".. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl"
!pip install -q ".. /input/hpapytorchzoozip/pytorch_zoo-master"
!pip install -q ".. /input/hpacellsegmentatormaster/HPA-Cell-Segmentation-master"
NUC_MODEL = '.. /input/hpacellsegmentatormodelweights/dpn_unet_nuclei_v1.pth'
CELL_MODEL = '.. /input/hpacellsegmentatormodelweights/dpn_unet_cell_3ch_v1.pth'
tfms = get_transforms(do_flip=False )<categorify>
segmentator = cellseg.CellSegmentator(
 NUC_MODEL,
CELL_MODEL,
scale_factor=0.25,
padding=True,
multi_channel_model=True 
)
data =(ImageList.from_folder('/data/')
.split_by_folder() 
.label_from_folder() 
.add_test_folder() 
.transform(tfms, size=64)
.databunch() )<define_variables>
gpus = tf.config.experimental.list_physical_devices('GPU')
 if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
 logical_gpus = tf.config.experimental.list_logical_devices('GPU')
 print(len(gpus), "...Physical GPUs,", len(logical_gpus), "Logical GPUs...
 ")
 except RuntimeError as e:
print(e )
data.show_batch(5,figsize=(6,6)) 
<find_best_params>
RGB_model = keras.models.load_model('.. /input/hpa-models-2021/ProteinModelRGB_rev_18.h5')
 G_model = keras.models.load_model('.. /input/hpa-models-2021/GreentileProteinModel_rev_2.h5')
 multicellmodel = keras.models.load_model('.. /input/hpa-models-2021/Full_image_greenModelRev9.h5', custom_objects={'FixedDropout':FixedDropout(rate=0.5)})
 img_type = 'g'
multicell2model = keras.models.load_model('.. /input/hpa-models-2021/Full_image_RYB_GModelRev11.h5', custom_objects={'FixedDropout':FixedDropout(rate=0.5)})
 img2_type = 'ryb_g'
data.classes, data.c, len(data.train_ds), len(data.valid_ds )<choose_model_class>
remove_small_holes, remove_small_objects)
 def label_cell(nuclei_pred, cell_pred):

 def __wsh(
 mask_img,
threshold,
border_img,
seeds,
threshold_adjustment=0.35,
small_object_size_cutoff=10, 
):
img_copy = np.copy(mask_img)
 m = seeds * border_img
img_copy[m <= threshold + threshold_adjustment] = 0
img_copy[m > threshold + threshold_adjustment] = 1
img_copy = img_copy.astype(np.bool)
 img_copy = remove_small_objects(img_copy, small_object_size_cutoff ).astype(
 np.uint8 
)
 mask_img[mask_img <= threshold] = 0
mask_img[mask_img > threshold] = 1
mask_img = mask_img.astype(np.bool)
 mask_img = remove_small_holes(mask_img, 63)
 mask_img = remove_small_objects(mask_img, 1 ).astype(np.uint8)
 markers = ndi.label(img_copy, output=np.uint32)[0]
labeled_array = segmentation.watershed(
 mask_img, markers, mask=mask_img, watershed_line=True 
)
 return labeled_array
nuclei_label = __wsh(
 nuclei_pred[..., 2] / 255.0,
0.4,
1 -(nuclei_pred[..., 1] + cell_pred[..., 1])/ 255.0 > 0.05,
nuclei_pred[..., 2] / 255,
threshold_adjustment=-0.25,
small_object_size_cutoff=32, 
)
 nuclei_label = remove_small_objects(nuclei_label, 157)
 nuclei_label = measure.label(nuclei_label)
 threshold_value = max(0.22, filters.threshold_otsu(cell_pred[..., 2] / 255)* 0.5)
 cell_region = np.multiply(
 cell_pred[..., 2] / 255 > threshold_value,
np.invert(np.asarray(cell_pred[..., 1] / 255 > 0.05, dtype=np.int8)) , 
)
 sk = np.asarray(cell_region, dtype=np.int8)
 distance = np.clip(cell_pred[..., 2], 255 * threshold_value, cell_pred[..., 2])
 cell_label = segmentation.watershed(-distance, nuclei_label, mask=sk)
 cell_label = remove_small_objects(cell_label, 344 ).astype(np.uint8)
 selem = disk(2)
 cell_label = closing(cell_label, selem)
 cell_label = __fill_holes(cell_label)
 sk = np.asarray(
 np.add(
 np.asarray(cell_label > 0, dtype=np.int8),
np.asarray(cell_pred[..., 1] / 255 > 0.05, dtype=np.int8), 
)
 > 0,
dtype=np.int8, 
)
 cell_label = segmentation.watershed(-distance, cell_label, mask=sk)
 cell_label = __fill_holes(cell_label)
 cell_label = np.asarray(cell_label > 0, dtype=np.uint8)
 cell_label = measure.label(cell_label)
 cell_label = remove_small_objects(cell_label, 344)
 cell_label = measure.label(cell_label)
 cell_label = np.asarray(cell_label, dtype=np.uint16)
 return nuclei_label, cell_label
def __fill_holes(image):

 boundaries = segmentation.find_boundaries(image)
 image = np.multiply(image, np.invert(boundaries))
image = ndi.binary_fill_holes(image > 0)
 image = ndi.label(image)[0]
return image
learn = cnn_learner(data, models.resnet50, metrics=[error_rate, accuracy], model_dir=Path('/kaggle/input/fast-ai-models'))<train_model>
def build_image_names(image_id: str)-> list:
mt = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_red.png'
er = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_yellow.png'
nu = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_blue.png'
high = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_green.png'
return [mt], [er], [nu], [high], [[mt], [er], [nu]],
def grab_contours(cnts):
if len(cnts)== 2:
cnts = cnts[0]
elif len(cnts)== 3:
cnts = cnts[1]
else:
raise Exception(( "Contours tuple must have length 2 or 3, "
"otherwise OpenCV changed their cv2.findContours return "
"signature yet again.Refer to OpenCV's documentation "
"in that case"))
return cnts
def create_cell_images(RGB, RYB_G, G, cell_masks, size):
def clipimgtosquare(group_img):
cnt_img = np.zeros_like(group_img)
 cnt_img[cover == 255] = group_img[cover == 255]
cnt_img = cnt_img[y:y+h, x:x+w]
old_size = cnt_img.shape[:2]
ratio = float(size)/max(old_size)
 new_size = tuple([int(x*ratio)for x in old_size])
 resized = cv2.resize(cnt_img,(new_size[1], new_size[0]))
delta_w = size - new_size[1]
delta_h = size - new_size[0]
top, bottom = delta_h//2, delta_h-(delta_h//2)
 left, right = delta_w//2, delta_w-(delta_w//2)
 color = [0, 0, 0]
square = cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
 square = img_to_array(square)
 return square
mask = cv2.convertScaleAbs(cell_masks)
 cnts = grab_contours(cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE))
RGBs = []
RYB_Gs = []
Gs = []
for i in range(1,cell_masks.max()):
mask = cv2.convertScaleAbs(np.where(cell_masks==i, 1, 0))
c = grab_contours(cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)) 
(x,y,w,h)= cv2.boundingRect(c[0])
 cover = np.zeros_like(RGB)
 cv2.drawContours(cover, [c[0]], 0,(255,255,255), -1)
 rgb = clipimgtosquare(RGB)
 ryb_g = clipimgtosquare(RYB_G)
 g = clipimgtosquare(G)
 RGBs.append(rgb)
 RYB_Gs.append(ryb_g)
 Gs.append(g)
 return RGBs, RYB_Gs, Gs
def image_predictions(images, model, TTArepeat=0, batch_size=8):
labels = []
confidences = []
images = np.vstack(images)
 confidence = model.predict(images,batch_size=batch_size)
 if TTArepeat > 0:
TTApred = []
TTApred.append(confidence)
 image = data_augmentation(images)
 for i in range(TTArepeat):
image = data_augmentation(image)
 TTApred.append(model.predict(image))
confidence = np.mean(TTApred,axis=0)
 confidences.append(confidence)
 return confidences
def create_cell_masks(mask):

 if mask.dtype != np.bool:
raise ValueError(
 "encode_binary_mask expects a binary mask, received dtype == %s" %
mask.dtype)
 mask = np.squeeze(mask)
 if len(mask.shape)!= 2:
raise ValueError(
 "encode_binary_mask expects a 2d mask, received shape == %s" %
mask.shape)
 mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
 mask_to_encode = mask_to_encode.astype(np.uint8)
 mask_to_encode = np.asfortranarray(mask_to_encode)
 encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]
binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
 base64_str = base64.b64encode(binary_str)
 return base64_str
def get_image_masks(image_id):
mt, er, nu, high, images = build_image_names(image_id=image_id)
 nuc_segmentations = segmentator.pred_nuclei(images[2])
 cell_segmentations = segmentator.pred_cells(images)
 nuclei_mask, cell_mask = label_cell(nuc_segmentations[0], cell_segmentations[0])
 blue = normalization(plt.imread(nu[0]))
green = normalization(plt.imread(high[0]))
red = normalization(plt.imread(mt[0]))
yellow = normalization(plt.imread(er[0]))
RGB = np.dstack(( red, green, blue))
RYB_G =img = np.dstack(( red+green, yellow+green, blue+green))
G = np.stack(( green,)*3, axis=-1)
 return RGB, RYB_G, G, cell_mask
def flatten_list_of_lists(l_o_l, to_string=False):
if not to_string:
return [item for sublist in l_o_l for item in sublist]
else:
return [str(item)for sublist in l_o_l for item in sublist]
def image_prediction_string(confidences, masks, labelqty = 19, threshold =.00):
labels = []
probs = []
codes = []
predictionstring = []
for pred, mask in zip(confidences, masks):
neglabel = 1-pred.max()
for label in range(0,labelqty):
if pred[label]>threshold:
labels.append(label)
 probs.append(pred[label])
 codes.append(mask.decode('UTF-8'))
labels.append(labelqty)
 probs.append(neglabel)
 codes.append(mask.decode('UTF-8'))
predictionstring = [" ".join(flatten_list_of_lists(zip([label, pred, mask]), to_string=True)) for label, pred, mask in zip(labels, probs, codes)]
return(" ".join(predictionstring))
def data_aug_exp(img, modeltype, size):
images = []
images.append(img)
 image = tf.image.rot90(img, k=1)
 images.append(image)
 image = tf.image.central_crop(image, central_fraction=.6)
 image = tf.image.resize(image, [size,size])
 images.append(image)
 image = tf.image.flip_left_right(img)
 images.append(image)
 image = tf.image.central_crop(image, central_fraction=.6)
 image = tf.image.resize(image, [size,size])
 images.append(image)
 image = tf.image.flip_up_down(img)
 images.append(image)
 image = tf.image.central_crop(image, central_fraction=.6)
 image = tf.image.resize(image, [size,size])
 images.append(image)
 if modeltype == 'ryb_g':
image = tf.image.adjust_contrast(img, 0.55)
 images.append(image)
 images = tf.expand_dims(images, axis=0)
 images = np.vstack(images)
 return images
def image_predictions_exp(images, model, modeltype, TTArepeat=False, batch_size=8, size=128):
images = np.expand_dims(images, axis=0)
 images = np.vstack(images)
 confidence = model.predict(images,batch_size=batch_size)
 if TTArepeat:
TTApred = []
aug_images = data_aug_exp(images,modeltype,size)
 for img in aug_images:
TTApred.append(model.predict(img))
confidence = np.mean(TTApred,axis=0)
 return confidence
def weighted_predictions(pred1, wt1, pred2=0, wt2=0, pred3=0, wt3=0):
new_pred = pred1*wt1+pred2*wt2+pred3*wt3
return np.array(new_pred)
 def normalization(array):
a =(array - array.min())/(array.max() -array.min())
 return a
def add_neglabel(array, labelqty):
arraynew = []
maximum = array.max(axis = 1)
 array = np.insert(array, labelqty, 1-maximum, axis = 1)
 return array
def print_cell(image, title, index):
plt.subplot(1,10,index)
 plt.imshow(image)
 plt.title(title)
 plt.axis('off')
learn.fit_one_cycle(5 )<find_best_params>
start = time.time()
test_dir = '.. /input/hpa-single-cell-image-classification/test/'
test_images = os.listdir(test_dir)
 images = [i.split("_")[0] for i in test_images]
names = np.unique(images)
 public = len(names)==559
if public:
print('...only public testset...')
 names = names[0:2]
<define_variables><EOS> 
sub.to_csv("/kaggle/working/submission.csv", index=False )