This repository has been 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
/
enhancedirectory.py
177 lines (152 loc) · 5.94 KB
/
enhancedirectory.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
167
168
169
170
171
172
173
174
175
176
177
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
import os
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 pictures')
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('--inputdirectory', type=str, help='Path for the input directory')
arguments = parser.parse_args()
epsilon=arguments.epsilon
network=arguments.network
imageDir=arguments.inputdirectory
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())
os.chdir(imageDir)
image_path_list = []
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] #specify your vald extensions here
valid_image_extensions = [item.lower() for item in valid_image_extensions]
#create a list all files in directory and
#append files with a vaild extention to image_path_list
for file in os.listdir(imageDir):
extension = os.path.splitext(file)[1]
if extension.lower() not in valid_image_extensions:
continue
image_path_list.append(os.path.join(imageDir, file))
#loop through image_path_list to open each image
for imagePath in image_path_list:
print('#Enhancing picture ' + str(imagePath))
image = load_image(imagePath)
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("enhanced_"+os.path.basename(imagePath))