-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
94 lines (79 loc) · 2.75 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import math
import torch
import re
import torch.nn as nn
import numpy as np
from skimage.measure.simple_metrics import compare_psnr
import os
import glob
def findLastCheckpoint(save_dir):
file_list = glob.glob(os.path.join(save_dir, '*epoch*.pth'))
if file_list:
epochs_exist = []
for file_ in file_list:
result = re.findall(".*epoch(.*).pth.*", file_)
epochs_exist.append(int(result[0]))
initial_epoch = max(epochs_exist)
else:
initial_epoch = 0
return initial_epoch
def batch_PSNR(img, imclean, data_range):
Img = img.data.cpu().numpy().astype(np.float32)
Iclean = imclean.data.cpu().numpy().astype(np.float32)
PSNR = 0
for i in range(Img.shape[0]):
PSNR += compare_psnr(Iclean[i,:,:,:], Img[i,:,:,:], data_range=data_range)
return (PSNR/Img.shape[0])
def normalize(data):
return data / 255.
def is_image(img_name):
if img_name.endswith(".jpg") or img_name.endswith(".bmp") or img_name.endswith(".png"):
return True
else:
return False
# TODO: two pixel shuffle functions to process the images
def pixelshuffle(image, scale):
'''
Discription: Given an image, return a reversible sub-sampling
[Input]: Image ndarray float
[Return]: A mosic image of shuffled pixels
'''
if scale == 1:
return image
w, h, c = image.shape
mosaic = np.array([])
for ws in range(scale):
band = np.array([])
for hs in range(scale):
temp = image[ws::scale, hs::scale, :] # get the sub-sampled image
band = np.concatenate((band, temp), axis=1) if band.size else temp
mosaic = np.concatenate((mosaic, band), axis=0) if mosaic.size else band
return mosaic
def reverse_pixelshuffle(image, scale, fill=0, fill_image=0, ind=[0, 0]):
'''
Discription: Given a mosaic image of subsampling, recombine it to a full image
[Input]: Image
[Return]: Recombine it using different portions of pixels
'''
w, h, c = image.shape
real = np.zeros((w, h, c)) # real image
wf = 0
hf = 0
for ws in range(scale):
hf = 0
for hs in range(scale):
temp = real[ws::scale, hs::scale, :]
wc, hc, cc = temp.shape # get the shpae of the current images
if fill == 1 and ws == ind[0] and hs == ind[1]:
real[ws::scale, hs::scale, :] = fill_image[wf:wf + wc, hf:hf + hc, :]
else:
real[ws::scale, hs::scale, :] = image[wf:wf + wc, hf:hf + hc, :]
hf = hf + hc
wf = wf + wc
return real
def print_network(net):
num_params = 0
for param in net.parameters():
num_params += param.numel()
print(net)
print('Total number of parameters: %d' % num_params)