-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeleBotClass.py
More file actions
185 lines (117 loc) · 5.49 KB
/
TeleBotClass.py
File metadata and controls
185 lines (117 loc) · 5.49 KB
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
178
179
180
181
182
183
184
185
# IMPORTS
from PIL import Image
import numpy as np
import torch
import torch.optim as optimizer
import torch.nn.functional as f
from torchvision import models
from BotFunctions import load_image, im_convert, get_features, gram_matrix
#from VGGClass import VggModel
# CLASS
class StyleTransferModel:
def __init__(self):
# Set hyper parameters and initialize model
self.init_model()
self.set_hyper()
def init_model(self):
# Initialize model
#self.vgg = VggModel()
state_dict = torch.hub.load_state_dict_from_url('https://download.pytorch.org/models/vgg19-dcbb9e9d.pth')
self.vgg = models.vgg19()
self.vgg.load_state_dict(state_dict)
for param in self.vgg.parameters():
param.requires_grad = False
# Some trick
for i, layer in enumerate(self.vgg.features):
if isinstance(layer, torch.nn.MaxPool2d):
self.vgg.features[i] = torch.nn.AvgPool2d(kernel_size=2, stride=2, padding=0)
#self.vgg.load_state_dict(torch.load('vgg_telebot.pth', map_location=torch.device('cpu')))
self.vgg.eval()
def get_model(self):
# Return model
return {'model': self.vgg}
def set_hyper(self):
# Set hyper parameters
self.gram_weights = {'conv1_1': 0.75,
'conv2_1': 0.6,
'conv3_1': 0.5,
'conv4_1': 0.4,
'conv5_1': 0.3}
self.content_weight = 1e3
self.style_weight = 1e3
def get_hyper(self):
# Return hyper parameters
return {'gram weights': self.gram_weights,
'content weight': self.content_weight,
'style weight': self.style_weight}
def get_images_and_features(self, content_name, style_name):
# Load images, get features, get gram matrix, create target image
content, shape = load_image(content_name, get_shape=True)
style = load_image(style_name, shape=shape)
content_features = get_features(content, self.vgg, 'content')
style_features = get_features(style, self.vgg, 'style')
style_grams = {layer: gram_matrix(style_features[layer]) for layer in style_features}
# It shows more interesting result than torch.randn
target = content.clone().requires_grad_(True)
del style_features
return content, content_features, style, style_grams, target
def set_optimizer_and_scheduler(self, target, lr=0.12, step_size=470, gamma=0.5):
# Set optimizer and scheduler
self.optimizer = optimizer.Adam([target], lr=lr)
self.scheduler = torch.optim.lr_scheduler.StepLR(
self.optimizer, step_size=step_size, gamma=gamma)
def get_optimizer_and_scheduler(self):
# Return optimizer and scheduler
return {'optimizer': self.optimizer,
'scheduler': self.scheduler}
def get_all_parameters(self):
# Return all parameters
return {'model': self.vgg,
'optimizer': self.optimizer,
'scheduler': self.scheduler,
'loss parameters': {'gram weights': self.gram_weights,
'content weight': self.content_weight,
'style weight': self.style_weight}
}
def transfer(self, content_features, style_grams, target, epochs):
# Studying part
print('Start transfer') # Just for developer
for epoch in range(epochs):
# Just usual pipeline
self.optimizer.zero_grad()
target_features = get_features(target, self.vgg, 'target')
# Get content loss
loss = f.mse_loss
content_loss = loss(target_features['conv4_2'], content_features['conv4_2'])
# Get style loss
style_loss = 0
for layer in self.gram_weights:
target_feature = target_features[layer]
target_gram = gram_matrix(target_feature)
_, d, h, w = target_feature.shape
style_gram = style_grams[layer]
layer_style_loss = self.gram_weights[layer] * loss(target_gram, style_gram)
style_loss += layer_style_loss / (d * h * w)
# Optimizer and scheduler step
total_loss = self.content_weight * content_loss + self.style_weight * style_loss
total_loss.backward()
self.optimizer.step()
self.scheduler.step()
if epoch % 50 == 0:
print(epoch) # Just for developer
print('End transfer') # Just for developer
final_img = im_convert(target)
return final_img
def save_result(self, final_img, name='result.jpg'):
# Saving result in result.jpg
img = np.uint8(final_img*255)
result = Image.fromarray(img)
result.save(name)
return name
def forward(self, content_name, style_name, epochs=501):
# MAIN FUNCTION
content, content_features, style, style_grams, target = self.get_images_and_features(
content_name, style_name)
self.set_optimizer_and_scheduler(target)
final_img = self.transfer(content_features, style_grams, target, epochs)
return self.save_result(final_img)