forked from pietrocarbo/deep-transfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
im_utils.py
56 lines (49 loc) · 1.76 KB
/
im_utils.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
49
50
51
52
53
54
55
56
import PIL
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import torchvision.transforms.functional as transforms
def path_imshow(path, title=None):
plt.figure()
image = Image.open(path)
plt.imshow(image)
if title is not None:
plt.title(title)
else:
plt.title(path)
plt.pause(0.001) # pause a bit so that plots are updated
# path_imshow('image_tutorial-1.png', 21312)
# ndarr.shape[0] is on y-axis (i.e. height) in the plot
def numpy_imshow(ndarr, title=None):
plt.figure()
plt.imshow(ndarr)
if title is not None:
plt.title(title)
plt.pause(0.001)
# numpy_imshow(np.random.rand(256, 512, 3), 'ndarr test')
# pytorch tensor have the number of channel in tensor.size(0)
def tensor_imshow(tensor, title=None):
inp = tensor.numpy().transpose((1, 2, 0))
inp = np.clip(inp, 0, 1)
plt.imshow(inp)
if title is not None:
plt.title(title)
if plt.isinteractive():
plt.ioff()
plt.show()
# import torch
# tensor_imshow(torch.randn(3, 256, 512), 'pytorch tensor')
def load_img(path, new_size):
img = Image.open(path).convert(mode='RGB')
if new_size:
# for fixed-size squared resizing, leave only the following line uncommented in this if statement
# img = transforms.resize(img, (new_size, new_size), PIL.Image.BICUBIC)
width, height = img.size
max_dim_ix = np.argmax(img.size)
if max_dim_ix == 0:
new_shape = (int(new_size * (height / width)), new_size)
img = transforms.resize(img, new_shape, PIL.Image.BICUBIC)
else:
new_shape = (new_size, int(new_size * (width / height)))
img = transforms.resize(img, new_shape, PIL.Image.BICUBIC)
return transforms.to_tensor(img)