-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_treatment.py
28 lines (24 loc) · 1.02 KB
/
image_treatment.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
# %%
from PIL import Image
from torchvision import transforms
import torch
from torchvision.transforms.transforms import Resize
out_size = 400
def prepare_image(filename):
input_image = Image.open(filename)
preprocess = transforms.Compose([
transforms.Resize((out_size,out_size)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
return input_batch
def undo_transform(tensor):
invTrans = transforms.Compose([ transforms.Normalize(mean = [ 0., 0., 0. ],
std = [ 1/0.229, 1/0.224, 1/0.225 ]),
transforms.Normalize(mean = [ -0.485, -0.456, -0.406 ],
std = [ 1., 1., 1. ]),
])
inv_tensor = invTrans(tensor)
return inv_tensor