File size: 2,050 Bytes
452723c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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)