-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
147 lines (108 loc) · 3.64 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#from Python
import time
import csv
import os
import math
import numpy as np
import sys
from shutil import copyfile
#from Pytorch
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable,grad
from torchvision import datasets
from torchvision import transforms
from torchvision.utils import save_image
#from this project
import param as p
import VisionOP
#local function
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
def denorm(x):
out = (x + 1) / 2
return out.clamp(0, 1)
def norm(x):
out = (x - 0.5) * 2
return out.clamp(-1,1)
################ Hyper Parameters ################
maxDataNum = p.maxDataNum #in fact, 4206
batchSize = p.batchSize
MaxCropWidth = p.MaxCropWidth
MinCropWidth = p.MinCropWidth
MaxCropHeight = p.MaxCropHeight
MinCropHeight = p.MinCropHeight
NOF = p.NOF
# train
MaxEpoch = p.MaxEpoch
learningRateNET = p.learningRate
# save
numberSaveImage = p.numberSaveImage
############################################
class PSNR(nn.Module):
def __init__(self):
super(PSNR, self).__init__()
self.MSE = 0
# (N,C,H,W)
def forward(self, input, target):
input = torch.abs(input - target).cuda()
self.MSE = torch.mean(input * input)
PSNR = 10 * math.log10((255 * 255) / self.MSE)
return PSNR
class Smooth_loss(nn.Module):
def __init__(self,Smooth_weight=1):
super(Smooth_loss,self).__init__()
self.Smooth_weight = Smooth_weight
def forward(self,x):
b,c,h,w = x.size()
x_h = F.pad(x,(0,0,1,1))
h_tv = torch.mean(torch.pow((x_h[:,:,2:,:]-x_h[:,:,:h,:]),2))
x_w = F.pad(x,(1,1,0,0))
w_tv = torch.mean(torch.pow((x_w[:,:,:,2:]-x_w[:,:,:,:w]),2))
#h_tv = torch.mean(torch.pow((x[:,:,1:,:]-x[:,:,:h-1,:]),2))
#w_tv = torch.mean(torch.pow((x[:,:,:,1:]-x[:,:,:,:w-1]),2))
self.loss = (h_tv + w_tv) / 2
return self.loss
class Sparse_loss(nn.Module):
def __init__(self,option=1): #option 1: normal loss, option 2: weighting loss
super(Sparse_loss,self).__init__()
self.option = option
def forward(self,x,input):
b,c,h,w = x.size()
x_h = F.pad(x,(0,0,1,1))
x_w = F.pad(x,(1,1,0,0))
if self.option == 1:
h_tv = torch.abs(x_h[:,:,2:,:]-x_h[:,:,:h,:])
w_tv = torch.abs(x_w[:,:,:,2:]-x_w[:,:,:,:w])
else:
input_h = F.pad(x,(0,0,1,1))
input_w = F.pad(x,(1,1,0,0))
input_grad_h = torch.abs(input_h[:,:,2:,:]-input_h[:,:,:h,:])
input_grad_w = torch.abs(input_w[:,:,:,2:]-input_w[:,:,:,:w])
x_grad_h = torch.abs(x_h[:,:,2:,:]-x_h[:,:,:h,:])
x_grad_w = torch.abs(x_w[:,:,:,2:]-x_w[:,:,:,:w])
h_ = 1 / (255*input_grad_h + 0.0001) * x_grad_h
w_ = 1 / (255*input_grad_w + 0.0001) * x_grad_w
h_tv = torch.mean(h_)
w_tv = torch.mean(w_)
self.loss = h_tv + w_tv
return self.loss
def tv_loss(img):
"""
Compute total variation loss.
Inputs:
- img: PyTorch Variable of shape (1, 3, H, W) holding an input image.
- tv_weight: Scalar giving the weight w_t to use for the TV loss.
Returns:
- loss: PyTorch Variable holding a scalar giving the total variation loss
for img weighted by tv_weight.
"""
b,c,h,w_ = img.size()
w_variance = torch.sum(torch.pow(img[:,:,:,:-1] - img[:,:,:,1:], 2))/b
h_variance = torch.sum(torch.pow(img[:,:,:-1,:] - img[:,:,1:,:], 2))/b
loss = (h_variance + w_variance) / 2
return loss