-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoencoder.py
317 lines (266 loc) · 9.59 KB
/
autoencoder.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import torch
import torchvision
from torch import nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.utils import save_image
import torchvision.datasets as dSet
import numpy as np
import os
from tkinter import *
from tkinter.messagebox import *
import cv2
from PIL import Image, ImageTk
import PIL
from math import *
from copy import *
ABSOLUTE = 'D:/Documents/Prepa/TIPE/'
pathNormal = ABSOLUTE + "Images/Normal/"
pathAltered = ABSOLUTE + "Images/Altered/"
pathPatch = ABSOLUTE + "Images/Patch/"
pathImage = ABSOLUTE + 'Images/x128/'
pathEncode = ABSOLUTE + 'Images/autoencoder/'
pathModels = ABSOLUTE = 'Models/'
NUMBER = 10000
batchSize = 256
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
setImages = dSet.ImageFolder(root = pathEncode, transform = transforms.Compose([transforms.ToTensor(), ]))
imagesLoader = torch.utils.data.DataLoader(setImages, batch_size = batchSize, shuffle = True, num_workers=0, pin_memory = True)
def to_img(x):
x = x.squeeze(0)
x = x.clamp(0, 1)
x = x.numpy()
x = x.transpose((1, 2, 0))
x = x * 255
x = x.astype(np.uint8)
return x
class autoencoder(nn.Module):
def __init__(self):
super(autoencoder, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(3, 16, kernel_size = 3, stride = 2, padding=1),
#nn.BatchNorm2d(16),
nn.Softplus(),
nn.Conv2d(16, 32, kernel_size = 3, stride = 2, padding=1),
#nn.BatchNorm2d(32),
nn.Softplus(),
nn.Conv2d(32, 128, kernel_size = 3, stride = 2, padding=1),
#nn.BatchNorm2d(128),
nn.Softplus(),
nn.Conv2d(128, 256, kernel_size = 3, stride = 2, padding=1),
#nn.BatchNorm2d(256),
nn.Softplus(),
nn.Conv2d(256, 512, kernel_size = 3, stride = 2, padding=1),
#nn.BatchNorm2d(512),
nn.Sigmoid()
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(512, 256, kernel_size = 3, stride = 2, padding = 0),
#nn.BatchNorm2d(256),
nn.Softplus(),
nn.ConvTranspose2d(256, 128, kernel_size = 3, stride = 2, padding = 1),
#nn.BatchNorm2d(128),
nn.Softplus(),
nn.ConvTranspose2d(128, 32, kernel_size = 3, stride = 2, padding = 1),
#nn.BatchNorm2d(32),
nn.Softplus(),
nn.ConvTranspose2d(32, 16, kernel_size = 3, stride = 2, padding = 1),
#nn.BatchNorm2d(16),
nn.Softplus(),
nn.ConvTranspose2d(16, 3, kernel_size = 2, stride = 2, padding = 1),
#nn.BatchNorm2d(3),
nn.Sigmoid()
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
def test(self, x):
x = self.decoder(x)
return x
def encode(self, x):
x = self.encoder(x)
return x
model = autoencoder().to(device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), weight_decay=0)
"""im = np.array(Image.open(pathEncode + 'images/' + '1.png'))
im = im.transpose((2, 0, 1))
im = torch.tensor(im).float().to(device)
im = im.unsqueeze(0)
x = model(im)
print(im.size())
print(model.encode(im).size())
print(x.size())"""
def train(number):
global myFrame
for i in range(number):
if INTERFACE :
randomScale()
afficherPreview()
myFrame.update()
for data in imagesLoader:
img, _ = data
img = img.to(device)
output = model(img)
loss = criterion(output, img)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('epoch : ' + str(i) + ' loss : ' + str(loss.item()))
##INTERFACE
WIDTH = 800
HEIGHT = 800
PREVIEWSIZE = 400
SLIDERLENGTH = 40
INTERFACE = True
##Sliders Hyperparamètres
COLUMNS = 40
CARACS = 512 #Nombre de paramètres ajustables
ROWS = ceil(CARACS / COLUMNS)
#VARIABLES = np.random.rand(CARACS)
if INTERFACE :
myFrame = Tk()
myFrame.title('Auto-encoder')
VARIABLES = []
for _ in range(CARACS):
a = DoubleVar()
VARIABLES.append(a)
nombre = StringVar()
saveName = StringVar()
meanDev = StringVar()
mean = StringVar()
def loadNN():
temp = np.random.randint(0, 255, (WIDTH, HEIGHT), dtype = 'i3').astype(np.uint8)
return ImageTk.PhotoImage(image = Image.fromarray(temp))
def rechargerImage():
global imageCanvas
imageCanvas.delete(ALL)
imageCanvas.create_image(0, 0, anchor = NW, image = loadNN())
def generer():
global VARIABLES
global imageCanvas
arguments = np.zeros(CARACS)
for i in range(CARACS):
arguments[i] = VARIABLES[i].get()
input = arrayToTensor(arguments)
nn.ReLU(input)
output = model.test(input)
output = to_img(output.cpu().data)
global image
imageCanvas.delete('all')
image = Image.fromarray(output).resize((WIDTH, HEIGHT), PIL.Image.ANTIALIAS)
img = ImageTk.PhotoImage(image)
imageCanvas.create_image(0, 0, anchor = NW, image = img)
imageCanvas.image = img
""".resize((WIDTH, HEIGHT), PIL.Image.ANTIALIAS))"""
def afficherPreview():
global previewCanvas1
global previewCanvas2
nomb = np.random.randint(0, NUMBER)
initI = np.array(Image.open(pathEncode + 'images/' + str(nomb) + '.png'))
init = initI.transpose((2, 0, 1))
init = torch.tensor(init).float()
transforms.Normalize(init, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
init = init.to(device)
init = init.unsqueeze(0)
fin = model(init)
previewCanvas1.delete('all')
previewCanvas2.delete('all')
image1 = Image.fromarray(initI).resize((PREVIEWSIZE, PREVIEWSIZE), PIL.Image.ANTIALIAS)
image2 = Image.fromarray(to_img(fin.detach().cpu())).resize((PREVIEWSIZE, PREVIEWSIZE), PIL.Image.ANTIALIAS)
photo1 = ImageTk.PhotoImage(image1)
photo2 = ImageTk.PhotoImage(image2)
previewCanvas1.create_image(0, 0, anchor = NW, image = photo1)
previewCanvas2.create_image(0, 0, anchor = NW, image = photo2)
previewCanvas1.image = photo1
previewCanvas2.image = photo2
def train100():
train(100)
def train1000():
train(1000)
def tensorToArray(x):
x = x.cpu().detach().numpy()
def arrayToTensor(x):
x = torch.tensor(x).float()
x = x.view(512, 1, 1)
#x = x.repeat(16, 1, 1)
x.unsqueeze_(0)
x = x.to(device)
return x
def randomScale():
global VARIABLES
global meanDev
global mean
mD = float(meanDev.get())
m = float(mean.get())
for s in VARIABLES:
s.set(np.random.normal(m, mD))
generer()
def trainSome():
global nombre
train(int(nombre.get()))
def saveModel():
global saveName
torch.save(model.state_dict(), pathModels + saveName.get() + '.pt')
def loadModel():
global saveName
model.load_state_dict(torch.load(pathModels + saveName.get() + '.pt'))
def allOn():
global VARIABLES
for s in VARIABLES:
s.set(1)
generer()
def allOff():
global VARIABLES
for s in VARIABLES:
s.set(-1)
generer()
if INTERFACE :
global imageCanvas
imageFrame = Frame(myFrame, width = WIDTH + PREVIEWSIZE, height = max(HEIGHT, 2 * PREVIEWSIZE))
imageCanvas = Canvas(imageFrame, width = WIDTH, height = HEIGHT)
imageCanvas.pack(side = LEFT)
slidersFrameMaster = Frame(myFrame)
#VARIABLES = [1, 2, 3, 4]
##Construction des sliders
SLIDERS = []
"""for i in range(ROWS):
sliderFrame = Frame(slidersFrameMaster)
for j in range(COLUMNS):
if i * COLUMNS + j < CARACS :
SLIDERS.append(Scale(sliderFrame, from_ = - 1, to = 1, orient = VERTICAL, length = SLIDERLENGTH, resolution = 0.1, tickinterval = 0, width = 5, variable = VARIABLES[i * COLUMNS + j], label = '', digits = 0, cursor = None).pack(side = LEFT))
sliderFrame.pack()"""
global previewFrame
global previewCanvas1
global previewCanvas2
previewFrame = Frame(imageFrame, width = PREVIEWSIZE, height = 2 * PREVIEWSIZE)
previewCanvas1 = Canvas(previewFrame, width = PREVIEWSIZE, height = PREVIEWSIZE)
previewCanvas1.pack(side = TOP)
previewCanvas2 = Canvas(previewFrame, width = PREVIEWSIZE, height = PREVIEWSIZE)
previewCanvas2.pack(side = BOTTOM)
previewFrame.pack(side = RIGHT)
imageFrame.pack(side = TOP)
#Boutons
meanButton = Entry(slidersFrameMaster, textvariable = mean)
meanButton.focus_set()
meanButton.pack(side = LEFT)
meanDev = Entry(slidersFrameMaster, textvariable = meanDev)
meanDev.focus_set()
meanDev.pack(side = LEFT)
randomButton = Button(slidersFrameMaster, text = 'Random', command = randomScale).pack(side = LEFT)
trainEntry = Entry(slidersFrameMaster, textvariable = nombre)
trainEntry.focus_set()
trainEntry.pack(side = LEFT)
trainButton = Button(slidersFrameMaster, text = 'Train', command = trainSome).pack(side = LEFT)
saveEntry = Entry(slidersFrameMaster, textvariable = saveName)
saveEntry.focus_set()
saveEntry.pack(side = LEFT)
saveButton = Button(slidersFrameMaster, text = 'Save model', command = saveModel).pack(side = LEFT)
loadButton = Button(slidersFrameMaster, text = 'Load model', command = loadModel).pack(side = LEFT)
onButton = Button(slidersFrameMaster, text = 'All 1', command = allOn).pack(side = LEFT)
offButton = Button(slidersFrameMaster, text = 'All -1', command = allOff).pack(side = LEFT)
previewButton = Button(slidersFrameMaster, text = 'Random preview', command = afficherPreview).pack(side = LEFT)
slidersFrameMaster.pack(side = BOTTOM, padx = 10, pady = 10)
myFrame.mainloop()