Last active
April 3, 2020 09:09
-
-
Save thunderInfy/3a6fd4db9f35fb55b629c4247024618e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_color_distortion(s=1.0): | |
| # s is the strength of color distortion. | |
| color_jitter = T.ColorJitter(0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s) | |
| rnd_color_jitter = T.RandomApply([color_jitter], p=0.8) | |
| rnd_gray = T.RandomGrayscale(p=0.2) | |
| color_distort = T.Compose([rnd_color_jitter, rnd_gray]) | |
| return color_distort | |
| class MyDataset(Dataset): | |
| def __init__(self, root_dir, filenames, labels, mutation=False): | |
| self.root_dir = root_dir | |
| self.file_names = filenames | |
| self.labels = labels | |
| self.mutation = mutation | |
| def __len__(self): | |
| return len(self.file_names) | |
| def tensorify(self, img): | |
| res = T.ToTensor()(img) | |
| res = T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(res) | |
| return res | |
| def mutate_image(self, img): | |
| res = T.RandomResizedCrop(224)(img) | |
| res = get_color_distortion(1)(res) | |
| return res | |
| def __getitem__(self, idx): | |
| if torch.is_tensor(idx): | |
| idx = idx.tolist() | |
| img_name = os.path.join(self.root_dir, self.file_names[idx]) | |
| image = Image.open(img_name) | |
| label = self.labels[idx] | |
| image = T.Resize((250, 250))(image) | |
| if self.mutation: | |
| image1 = self.mutate_image(image) | |
| image1 = self.tensorify(image1) | |
| image2 = self.mutate_image(image) | |
| image2 = self.tensorify(image2) | |
| sample = {'image1': image1, 'image2': image2, 'label': label} | |
| else: | |
| image = T.Resize((224, 224))(image) | |
| image = self.tensorify(image) | |
| sample = {'image': image, 'label': label} | |
| return sample |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment