| # Copyright (c) 2020, Roy Or-El. All rights reserved. | |
| # | |
| # This work is licensed under the Creative Commons | |
| # Attribution-NonCommercial-ShareAlike 4.0 International License. | |
| # To view a copy of this license, visit | |
| # http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to | |
| # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. | |
| import torch.utils.data as data | |
| import os | |
| from PIL import Image | |
| from src.utils.deeplab_util import preprocess_image | |
| from src.utils.constant_util import CLASSES | |
| class CelebASegmentation(data.Dataset): | |
| # CLASSES = [ | |
| # 'background', # 0 | |
| # 'skin', # 1 | |
| # 'nose', # 2 | |
| # 'eye_g', # 3 | |
| # 'l_eye', # 4 | |
| # 'r_eye', # 5 | |
| # 'l_brow', # 6 | |
| # 'r_brow', # 7 | |
| # 'l_ear', # 8 | |
| # 'r_ear', # 9 | |
| # 'mouth', # 10 | |
| # 'u_lip', # 11 | |
| # 'l_lip', # 12 | |
| # 'hair', # 13 | |
| # 'hat', # 14 | |
| # 'ear_r', # 15 | |
| # 'neck_l', # 16 | |
| # 'neck', # 17 | |
| # 'cloth' # 18 | |
| # ] | |
| CLASSES = CLASSES | |
| def __init__(self, root, transform=None, crop_size=None): | |
| self.root = root | |
| self.transform = transform | |
| self.crop_size = crop_size # ~! | |
| self.images = [] | |
| self.images = [ | |
| os.path.join(self.root, f) for f in os.listdir(self.root) | |
| ] | |
| # subdirs = next(os.walk(self.root))[1] #quick trick to get all subdirectories | |
| # for subdir in subdirs: | |
| # curr_images = [os.path.join(self.root,subdir,file) for file in os.listdir(os.path.join(self.root,subdir)) if file.endswith('.png')] | |
| # self.images += curr_images | |
| def __getitem__(self, index): | |
| _img = Image.open(self.images[index]).convert('RGB') | |
| _img=_img.resize((513,513),Image.BILINEAR) | |
| _img = preprocess_image(_img,flip=False,scale=None,crop=(self.crop_size, self.crop_size)) | |
| # _img = preprocess_image(_img,flip=False,scale=None) # ~! | |
| if self.transform is not None: | |
| _img = self.transform(_img) | |
| return _img | |
| def __len__(self): | |
| return len(self.images) | |