|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from paddle.vision.transforms import ( |
| 16 | + Compose, |
| 17 | + Transpose, |
| 18 | + ColorJitter, |
| 19 | + RandomResizedCrop, |
| 20 | + RandomHorizontalFlip, |
| 21 | +) |
| 22 | +from .folder import DatasetFolder |
| 23 | +from .builder import DATASETS |
| 24 | +from .preprocess.transforms import ( |
| 25 | + RandomApply, |
| 26 | + GaussianBlur, |
| 27 | + NormalizeImage, |
| 28 | + RandomGrayscale, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +@DATASETS.register() |
| 33 | +class MultiCropDataset(DatasetFolder): |
| 34 | + cls_filter = None |
| 35 | + |
| 36 | + def __init__(self, |
| 37 | + dataroot, |
| 38 | + size_crops, |
| 39 | + num_crops, |
| 40 | + min_scale_crops, |
| 41 | + max_scale_crops, |
| 42 | + return_label=False): |
| 43 | + super(MultiCropDataset, self).__init__(dataroot, cls_filter=self.cls_filter) |
| 44 | + |
| 45 | + assert len(size_crops) == len(num_crops) |
| 46 | + assert len(min_scale_crops) == len(num_crops) |
| 47 | + assert len(max_scale_crops) == len(num_crops) |
| 48 | + self.return_label = return_label |
| 49 | + |
| 50 | + color_transform = [get_color_distortion(), get_pil_gaussian_blur()] |
| 51 | + mean = [0.485, 0.456, 0.406] |
| 52 | + std = [0.229, 0.224, 0.225] |
| 53 | + trans = [] |
| 54 | + for i in range(len(size_crops)): |
| 55 | + randomresizedcrop = RandomResizedCrop( |
| 56 | + size_crops[i], |
| 57 | + scale=(min_scale_crops[i], max_scale_crops[i]), |
| 58 | + ) |
| 59 | + trans.extend([Compose([ |
| 60 | + randomresizedcrop, |
| 61 | + RandomHorizontalFlip(prob=0.5), |
| 62 | + Compose(color_transform), |
| 63 | + Transpose(), |
| 64 | + NormalizeImage(scale='1.0/255.0', mean=mean, std=std)]) |
| 65 | + ] * num_crops[i]) |
| 66 | + self.trans = trans |
| 67 | + |
| 68 | + def __getitem__(self, index): |
| 69 | + """ |
| 70 | + Args: |
| 71 | + index (int): Index |
| 72 | +
|
| 73 | + Returns: |
| 74 | + tuple: (sample, target) where target is class_index of the target class. |
| 75 | + """ |
| 76 | + path, target = self.samples[index] |
| 77 | + sample = self.loader(path) |
| 78 | + sample = list(map(lambda trans: trans(sample), self.trans)) |
| 79 | + if self.return_label: |
| 80 | + return sample, target |
| 81 | + |
| 82 | + return sample |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +def get_pil_gaussian_blur(p=0.5): |
| 87 | + gaussian_blur = GaussianBlur(sigma=[.1, 2.], _PIL=True) |
| 88 | + rnd_gaussian_blur = RandomApply([gaussian_blur], p=p) |
| 89 | + return rnd_gaussian_blur |
| 90 | + |
| 91 | + |
| 92 | +def get_color_distortion(s=1.0): |
| 93 | + # s is the strength of color distortion. |
| 94 | + color_jitter = ColorJitter(0.8*s, 0.8*s, 0.8*s, 0.2*s) |
| 95 | + rnd_color_jitter = RandomApply([color_jitter], p=0.8) |
| 96 | + rnd_gray = RandomGrayscale(p=0.2) |
| 97 | + color_distort = Compose([rnd_color_jitter, rnd_gray]) |
| 98 | + return color_distort |
0 commit comments