(self, image, label, aug=True)
| 28 | self.label_path = None |
| 29 | |
| 30 | def sync_transform(self, image, label, aug=True): |
| 31 | crop_size = self.crop_size |
| 32 | if self.scale: |
| 33 | short_size = random.randint(int(self.base_size * 0.75), int(self.base_size * 2.0)) |
| 34 | else: |
| 35 | short_size = self.base_size |
| 36 | |
| 37 | # 随机左右翻转 |
| 38 | if random.random() > 0.5: |
| 39 | image = image.transpose(Image.FLIP_LEFT_RIGHT) |
| 40 | label = label.transpose(Image.FLIP_LEFT_RIGHT) |
| 41 | w, h = image.size |
| 42 | |
| 43 | # 同比例缩放 |
| 44 | if h > w: |
| 45 | out_w = short_size |
| 46 | out_h = int(1.0 * h / w * out_w) |
| 47 | else: |
| 48 | out_h = short_size |
| 49 | out_w = int(1.0 * w / h * out_h) |
| 50 | image = image.resize((out_w, out_h), Image.BILINEAR) |
| 51 | label = label.resize((out_w, out_h), Image.NEAREST) |
| 52 | |
| 53 | # deg = random.uniform(-10, 10) |
| 54 | # image = image.rotate(deg, resample=Image.BILINEAR) |
| 55 | # label = label.rotate(deg, resample=Image.NEAREST) |
| 56 | |
| 57 | # 四周填充 |
| 58 | if short_size < crop_size: |
| 59 | pad_h = crop_size - out_h if out_h < crop_size else 0 |
| 60 | pad_w = crop_size - out_w if out_w < crop_size else 0 |
| 61 | image = ImageOps.expand(image, border=(pad_w // 2, pad_h // 2, pad_w - pad_w // 2, pad_h - pad_h // 2), |
| 62 | fill=0) |
| 63 | label = ImageOps.expand(label, border=(pad_w // 2, pad_h // 2, pad_w - pad_w // 2, pad_h - pad_h // 2), |
| 64 | fill=255) |
| 65 | |
| 66 | # 随机裁剪 |
| 67 | w, h = image.size |
| 68 | x = random.randint(0, w - crop_size) |
| 69 | y = random.randint(0, h - crop_size) |
| 70 | image = image.crop((x, y, x + crop_size, y + crop_size)) |
| 71 | label = label.crop((x, y, x + crop_size, y + crop_size)) |
| 72 | |
| 73 | if aug: |
| 74 | # 高斯模糊,可选 |
| 75 | if random.random() > 0.7: |
| 76 | image = image.filter(ImageFilter.GaussianBlur(radius=random.random())) |
| 77 | |
| 78 | # 可选 |
| 79 | if random.random() > 0.7: |
| 80 | # 随机亮度 |
| 81 | factor = np.random.uniform(0.75, 1.25) |
| 82 | image = ImageEnhance.Brightness(image).enhance(factor) |
| 83 | |
| 84 | # 颜色抖动 |
| 85 | factor = np.random.uniform(0.75, 1.25) |
| 86 | image = ImageEnhance.Color(image).enhance(factor) |
| 87 |
no test coverage detected