forked from thekoshkina/learned_image_compression
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
60 lines (47 loc) · 1.45 KB
/
utils.py
File metadata and controls
60 lines (47 loc) · 1.45 KB
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
49
50
51
52
53
54
55
56
57
58
59
from PIL import Image
import torchvision
import torch
from model import Model
def run_on_image(model_path, image_path, device):
"""
Run the pretrained model stored at model_path on an image
:param model_path: path to the model weights
:param image_path: path to the image
:return:
"""
model = Model(device)
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint['state_dict'])
model.to(device)
model.eval()
transform = torchvision.transforms.Compose([torchvision.transforms.Resize((765, 765)), torchvision.transforms.ToTensor()])
image = Image.open(image_path)
inputs = transform(image)
inputs = torch.unsqueeze(inputs, 0)
inputs = inputs.to(device)
x_hat, _, _, _, _ = model(inputs)
reconstructed_image = torchvision.transforms.ToPILImage(mode='RGB')(x_hat.squeeze)
result_image = concat_images(image, reconstructed_image)
result_image.show()
def concat_images(image1, image2):
"""
Concatenates two images together
"""
result_image = Image.new('RGB', (image1.width + image2.width, image1.height))
result_image.paste(image1, (0, 0))
result_image.paste(image2, (image1.width, 0))
return result_image
class AverageMeter(object):
"""Stores current value of statistics and computes average"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count