Skip to content

Instantly share code, notes, and snippets.

@aiwithshekhar
Last active December 14, 2019 10:45
Show Gist options
  • Save aiwithshekhar/b75e65d083c4a73d3129515d7685892e to your computer and use it in GitHub Desktop.
Save aiwithshekhar/b75e65d083c4a73d3129515d7685892e to your computer and use it in GitHub Desktop.
loading images & mask and use transformation
'''when dataloader request for samples using index it fetches input image and target mask,
apply transformation and returns it'''
class CarDataset(Dataset):
def __init__(self,df,img_fol,mask_fol,mean,std,phase):
self.fname=df['img'].values.tolist()
self.img_fol=img_fol
self.mask_fol=mask_fol
self.mean=mean
self.std=std
self.phase=phase
self.trasnform=get_transform(phase,mean,std)
def __getitem__(self, idx):
name=self.fname[idx]
img_name_path=os.path.join(self.img_fol,name)
mask_name_path=img_name_path.split('.')[0].replace('train-128','train_masks-128')+'_mask.png'
img=cv2.imread(img_name_path)
mask=cv2.imread(mask_name_path,cv2.IMREAD_GRAYSCALE)
augmentation=self.trasnform(image=img, mask=mask)
img_aug=augmentation['image'] #[3,128,128] type:Tensor
mask_aug=augmentation['mask'] #[1,128,128] type:Tensor
return img_aug, mask_aug
def __len__(self):
return len(self.fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment