Dataset Patchify

dataset will be patched here from whole side images

Reading train images and masks from folder

Training and test image and mask can be downloaded from the following command - curl https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/training_groundtruth.tif > mask_name.tif - curl https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/training.tif> train_images.tif - curl https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/testing_groundtruth.tif> test_mask.tif - curl https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/testing.tif> test_images.tif

root_path = Path.cwd().parent
Path('/home/hasan/workspace/git_data/fine_tune_SAM')
### test images
big_img_tst = tifffile.imread(test_imgs)
big_msk_tst = tifffile.imread(test_masks)
print(f' image tif has a shape and mask tif has a shape of {big_img_tst.shape} and {big_msk_tst.shape} respectively')
 image tif has a shape and mask tif has a shape of (165, 768, 1024) and (165, 768, 1024) respectively
### train images

Testing image and masks


source

show_rand_img

 show_rand_img (idx:int, im_path:str, msk_path:str)

Show random mask and image from together from path

Type Details
idx int in case of None, a random image is chosen
im_path str
msk_path str

Notebook is here

Saving the images in a single folder

  • for this project this part is not necessary.
  • I am doing it so that we can use patchify in any projects
  • Just tell where the full images are available, tell patch size and step size and it will created the patches with same name of the images, ony another index will be created to tell which patch number is that
for i in tqdm(range(len(big_img_tst))):
    cv2.imwrite(f'{actual_im_path_tst}/img_{i}.png', big_img_tst[i])
    cv2.imwrite(f'{actual_msk_path_tst}/img_{i}.png', big_msk_tst[i])
100%|██████████| 165/165 [00:02<00:00, 60.49it/s]
Path(actual_im_path_tst).ls()
(#165) [Path('/home/hasan/workspace/data/microscopy_data/test_images/img_51.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_98.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_133.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_107.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_16.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_150.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_158.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_145.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_48.png'),Path('/home/hasan/workspace/data/microscopy_data/test_images/img_40.png')...]
for i in tqdm(range(len(big_img))):
    cv2.imwrite(f'{actual_im_path}/img_{i}.png', big_img[i])
    cv2.imwrite(f'{actual_msk_path}/img_{i}.png', big_msk[i])
100%|██████████| 165/165 [00:02<00:00, 61.58it/s]
show_rand_img(idx=None, im_path=actual_im_path, msk_path=actual_msk_path)

Creating patches from the folder

patch_img_path_tst=Path(r'/home/hasan/workspace/data/microscopy_data/test_patch_images')
patch_img_path_tst.mkdir(exist_ok=True, parents=True)
patch_msk_path_tst=Path(r'/home/hasan/workspace/data/microscopy_data/test_patch_masks')
patch_msk_path_tst.mkdir(exist_ok=True, parents=True)

source

patch_img_and_mask

 patch_img_and_mask (im_path:Union[pathlib.Path,str],
                     msk_path:Union[pathlib.Path,str],
                     patch_im_path:Union[pathlib.Path,str],
                     patch_msk_path:Union[pathlib.Path,str],
                     PATCH_SIZE:int=256, STEP:int=256)

Create patch images and masks from original images and masks

delete patches with empty masks

  • There is possible some patch has no mask, we want to delete those mask
for i in tqdm(patch_msk_path_tst.ls()):
    name_ = Path(i).name
    img = cv2.imread(i.as_posix(), cv2.IMREAD_GRAYSCALE)
    if not img.max() > 0:
        i.unlink()
        im_ = Path(f'{patch_img_path_tst}/{name_}')
        im_.unlink()
100%|██████████| 1980/1980 [00:00<00:00, 2423.80it/s]
for i in tqdm(patch_msk_path.ls()):
    name_ = Path(i).name
    img = cv2.imread(i.as_posix(), cv2.IMREAD_GRAYSCALE)
    if not img.max() > 0:
        i.unlink()
        im_ = Path(f'{patch_img_path}/{name_}')
        im_.unlink()
100%|██████████| 1980/1980 [00:00<00:00, 2697.89it/s]
show_rand_img(
    im_path=patch_img_path_tst, 
    msk_path=patch_msk_path_tst, 
    idx=None)

show_rand_img(
    im_path=patch_img_path, 
    msk_path=patch_msk_path, 
    idx=None)

  • one can go further and check what is the percentage of mask, if mask has a defined percentage of pixels, then keep the patch, otherwise delete it
  • but for me, right now i will further with modelling
import nbdev; nbdev.nbdev_export()