-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask2.py
51 lines (40 loc) · 1.54 KB
/
task2.py
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
"""
Use the following augmentation methods on the sample image under data/sample.png
and save the result under this path: 'data/sample_augmented.png'
Note:
- use torchvision.transforms
- use the following augmentation methods with the same order as below:
* affine: degrees: ±5,
translation= 0.1 of width and height,
scale: 0.9-1.1 of the original size
* rotation ±5 degrees,
* horizontal flip with a probablity of 0.5
* center crop with height=320 and width=640
* resize to height=160 and width=320
* color jitter with: brightness=0.5,
contrast=0.5,
saturation=0.4,
hue=0.2
- use default values for anything unspecified
"""
import torch
from torchvision import transforms as T
import numpy as np
import cv2
from PIL import Image
torch.manual_seed(8)
np.random.seed(8)
img = cv2.imread('data/sample.png')
img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# write your code here ...
transform = T.Compose([
T.RandomAffine(degrees=5, translate=(0.1, 0.1), scale=(0.9, 1.1)),
T.RandomRotation(degrees=5),
T.RandomHorizontalFlip(p=0.5),
T.CenterCrop((320, 640)),
T.Resize((160, 320)),
T.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.4, hue=0.2)
])
transformed_img = transform(img_pil)
img_transformed_cv2 = cv2.cvtColor(np.array(transformed_img), cv2.COLOR_RGB2BGR)
cv2.imwrite('data/sample_augmented.png', img_transformed_cv2)