This repository was archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenhancepicture.py
166 lines (140 loc) · 5.36 KB
/
enhancepicture.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from __future__ import print_function, division
import torch
import numpy as np
from torchvision import transforms
import warnings
warnings.filterwarnings("ignore")
from PIL import Image
from torch.autograd import Variable
from torchvision import models
from torch import nn
use_gpu = True
import argparse
import math
if use_gpu:
cuda = torch.device('cuda:0') # Default CUDA device
torch.cuda.set_device(cuda.index)
class BaselineModel(nn.Module):
def __init__(self, num_classes, inputsize, keep_probability=0.5):
super(BaselineModel, self).__init__()
self.fc1 = nn.Linear(inputsize, 256)
self.drop_prob = (1 - keep_probability)
self.relu1 = nn.PReLU()
self.drop1 = nn.Dropout(self.drop_prob)
self.bn1 = nn.BatchNorm1d(256)
self.fc2 = nn.Linear(256, 256)
self.relu2 = nn.PReLU()
self.drop2 = nn.Dropout(p=self.drop_prob)
self.bn2 = nn.BatchNorm1d(256)
self.fc3 = nn.Linear(256, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
# Weight initialization reference: https://arxiv.org/abs/1502.01852
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, x):
"""
Feed-forward pass.
:param x: Input tensor
: return: Output tensor
"""
out = self.fc1(x)
out = self.relu1(out)
out = self.drop1(out)
out = self.bn1(out)
out = self.fc2(out)
out = self.relu2(out)
out = self.drop2(out)
out = self.bn2(out)
out = self.fc3(out)
return out
class convNet(nn.Module):
#constructor
def __init__(self,resnet,mynet):
super(convNet, self).__init__()
#defining layers in convnet
self.resnet=resnet
self.myNet=mynet
def forward(self, x):
x=self.resnet(x)
x=self.myNet(x)
return x
class convNet2(nn.Module):
#constructor
def __init__(self,resnet,mynet):
super(convNet2, self).__init__()
#defining layers in convnet
self.avgpl=nn.AdaptiveAvgPool2d((224,224))
self.rsn=resnet
self.myNet=mynet
def forward(self, x):
x=self.avgpl(x)
x=self.rsn(x)
return x
def to_grayscale(image):
"""
input is (d,w,h)
converts 3D image tensor to grayscale images corresponding to each channel
"""
image = torch.sum(image, dim=0)
image = torch.div(image, image.shape[0])
return image
def normalize(image):
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
preprocess = transforms.Compose([
transforms.ToTensor(),
normalize
])
image = Variable(preprocess(image).unsqueeze(0))
return image
def deprocess(image):
return image * torch.Tensor([0.229, 0.224, 0.225]).cuda() + torch.Tensor([0.485, 0.456, 0.406]).cuda()
def load_image(path):
image = Image.open(path).convert('RGB')
return image
mean=np.asarray([0.485, 0.456, 0.406])
std=np.asarray([0.229, 0.224, 0.225])
parser = argparse.ArgumentParser()
parser.add_argument('--epsilon', type=float, default=0.4, help='Intensity of the enhancement performed to the input picture')
parser.add_argument('--network', type=str, default='fine_tuned_flickerAES_normalized_dropout_resnet18_customnetworkadamnormalized.pt', help='path to the pretrained aesthetics prediction network')
parser.add_argument('--inputimage', type=str, help='Path for the input image')
parser.add_argument('--outputimage', type=str, default='output.jpg', help='Desired path for the output image')
arguments = parser.parse_args()
epsilon=arguments.epsilon
network=arguments.network
inputimage=arguments.inputimage
outputimage = arguments.outputimage
model_ft = models.resnet18(pretrained=True)
num_ftrs = model_ft.fc.out_features
net1 = BaselineModel(1, num_ftrs)
net_2=torch.load(network)
cnvnet2=convNet2(resnet=net_2, mynet=net1.cuda())
modulelist = list(cnvnet2.modules())
image = load_image(inputimage)
image_2 = normalize(image)
img_variable = Variable(image_2.cuda(), requires_grad=True) #convert tensor into a variable
output = cnvnet2.forward(img_variable)[0]
output =Variable(output, requires_grad=True)
target = Variable(torch.FloatTensor([2]).cuda(), requires_grad=False)
loss = torch.nn.MSELoss()
loss_cal = loss(output, target)
loss_cal.backward(retain_graph=True)
eps = epsilon
x_grad = img_variable
x_adversarial = img_variable.data + eps * x_grad #find adv example
output_adv = cnvnet2.forward(Variable(x_adversarial)) #perform a forward pass on adv example
x=img_variable.cpu().detach().numpy()
x_adv=x_adversarial
x_adv = x_adv.squeeze(0)
x_adv = x_adv.mul(torch.FloatTensor(std).cuda().view(3,1,1)).add(torch.FloatTensor(mean).cuda().view(3,1,1)).cpu().detach().numpy()#reverse of normalization op
x_adv = np.transpose( x_adv , (1,2,0)) # C X H X W ==> H X W X C
x_adv = np.clip(x_adv, 0, 1)
result = Image.fromarray((x_adv * 255).astype(np.uint8))
result.save(outputimage)
print('Image successfully saved to ' + outputimage)