| 6 | |
| 7 | |
| 8 | class TestDataset(Dataset): |
| 9 | def __init__(self, |
| 10 | foreground_paths, |
| 11 | mask_paths, |
| 12 | background_paths=None, |
| 13 | load_size=256): |
| 14 | ''' |
| 15 | foreground_paths: [folder, imagepath_list, image path] |
| 16 | mask_paths: [folter, imagepath_list, image path] |
| 17 | background_paths: [folter, imagepath_list, image path] |
| 18 | ''' |
| 19 | self.foreg_paths, self.mask_paths, self.backg_paths = \ |
| 20 | foreground_paths, mask_paths, background_paths |
| 21 | |
| 22 | self._load_images_paths() |
| 23 | self._load_sizez = load_size |
| 24 | self.transform_image = transforms.Compose([ |
| 25 | transforms.Resize([load_size, load_size]), |
| 26 | transforms.ToTensor(), |
| 27 | transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) |
| 28 | ]) |
| 29 | self.transform_mask = transforms.Compose([ |
| 30 | transforms.Resize([load_size, load_size]), |
| 31 | transforms.ToTensor() |
| 32 | ]) |
| 33 | |
| 34 | def _load_images_paths(self, ): |
| 35 | if isinstance(self.foreg_paths, list): |
| 36 | pass |
| 37 | elif os.path.isdir(self.foreg_paths): |
| 38 | self.foreg_paths = glob(self.foreg_paths + '/*.jpg') + \ |
| 39 | glob(self.foreg_paths + '/*.png') + \ |
| 40 | glob(self.foreg_paths + '/*.bmp') |
| 41 | self.mask_paths = glob(self.mask_paths + '/*.png') |
| 42 | if self.backg_paths is not None: |
| 43 | self.backg_paths = glob(self.mask_paths + '/*.png') + \ |
| 44 | glob(self.backg_paths + '/*.jpg') + \ |
| 45 | glob(self.backg_paths + '/*.bmp') |
| 46 | elif os.path.isfile(self.foreg_paths) and (self.foreg_paths[-3:] in ['jpg', 'png', 'bmp']): |
| 47 | self.foreg_paths = [self.foreg_paths] |
| 48 | self.mask_paths = [self.mask_paths] |
| 49 | if self.backg_paths is not None: |
| 50 | self.backg_paths = [self.backg_paths] |
| 51 | else: |
| 52 | print(f'please check the test path: {self.foreg_paths} {self.mask_paths} {self.backg_paths}') |
| 53 | exit() |
| 54 | self._sort() |
| 55 | print(f'total {len(self.foreg_paths)} images') |
| 56 | |
| 57 | def _sort(self): |
| 58 | self.foreg_paths.sort() |
| 59 | self.mask_paths.sort() |
| 60 | if self.backg_paths is not None: |
| 61 | self.backg_paths.sort() |
| 62 | |
| 63 | def __getitem__(self, index): |
| 64 | comp = self.transform_image(Image.open(self.foreg_paths[index]).convert('RGB')) |
| 65 | mask = self.transform_mask(Image.open(self.mask_paths[index]).convert('1')) |
nothing calls this directly
no outgoing calls
no test coverage detected