-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphMemory_JS_CelebAToImageNet.py
629 lines (500 loc) · 21.5 KB
/
GraphMemory_JS_CelebAToImageNet.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
"""
Train a diffusion model on images.
"""
import os
#os.environ['CUDA_VISIBLE_DEVICES']='1'
import time
from skimage import io,data
#
import argparse
import torch
from datasets.MyCIFAR10 import *
from NetworkModels.Balance_TeacherStudent_NoMPI_ import *
from NetworkModels.Teacher_Model_NoMPI_ import *
from Task_Split.Task_utilizes import *
import cv2
from cv2_imageProcess import *
from datasets.Data_Loading import *
from datasets.Fid_evaluation import *
from Task_Split.TaskFree_Split import *
from datasets.MNIST32 import *
import torchvision.transforms as transforms
import torch.utils.data as Data
from NetworkModels.TFCL_TeacherStudent_ import *
from NetworkModels.DynamicDiffusionMixture_ import *
from skimage.metrics import peak_signal_noise_ratio
from skimage.metrics import structural_similarity
from NetworkModels.MemoryUnitFramework_ import *
from NetworkModels.MemoryUnitGraphFramework_ import *
#dad
import numpy as np
from improved_diffusion import dist_util, logger
from improved_diffusion.image_datasets import load_data
from improved_diffusion.resample import create_named_schedule_sampler
from improved_diffusion.script_util import (
model_and_diffusion_defaults,
create_model_and_diffusion,
args_to_dict,
add_dict_to_argparser,
)
from improved_diffusion.train_util import TrainLoop
#
import torch.nn.functional as F # 函数包
import torch.distributions as td
from torch.distributions.multivariate_normal import MultivariateNormal
#
def Transfer_To_Numpy(sample):
sample = ((sample + 1) * 127.5).clamp(0, 255).to(torch.uint8)
sample = sample.permute(0, 2, 3, 1)
sample = sample.contiguous()
mySamples = sample.unsqueeze(0).cuda().cpu()
mySamples = np.array(mySamples)
mySamples = mySamples[0]
return mySamples
def Save_Image(name,image):
image = image.astype(np.float32)
cv2.imwrite("results/" + name, cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#io.imsave("results/" + name, image)
def TransferNumpyToTensor(totalSetX,device):
newSet = []
for i in range(np.shape(totalSetX)[0]):
arr1 = totalSetX[i]
arr1 = torch.tensor(arr1).cuda().to(device=device, dtype=torch.float)
newSet.append(arr1)
return newSet
def RandomSelectionArr(memory,newXList):
for i in range(np.shape(newXList)[0]):
memory = RandomSelection(memory,newXList[i])
return memory
def RandomSelection(memory,newX):
N = np.shape(memory)[0] + 1
j = int(random.random() * N)
if j > 0 and j < N-2:
memory[j] = newX
return memory
def GiveMSE(data,reco):
mse = nn.functional.mse_loss(data,reco)
mse.unsqueeze(0).cuda().cpu()
return mse
#
def Calculate_JS(TSFramework,batch,batchReco):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
miniBatch = 64
batch = batch.reshape(np.shape(batch)[0],64*64*3)
batchReco = batchReco.reshape(np.shape(batchReco)[0],64*64*3)
std = np.zeros((np.shape(batch)))
std[:,:] = 0.01
std = torch.tensor(std).cuda().to(device=device, dtype=torch.float)
t = 100
diffusion = TSFramework.teacherArray[0].diffusion
schedule_sampler = UniformSampler(diffusion)
times, weights = schedule_sampler.sample(np.shape(batch)[0], dist_util.dev())
for i in range(np.shape(times)[0]):
times[i] = t
beta = _extract_into_tensor(TSFramework.teacherArray[0].diffusion.sqrt_alphas_cumprod, times, batch.shape)
batch = batch * beta
batchReco = batchReco * beta
q_z1 = td.normal.Normal(batch, std)
q_z2 = td.normal.Normal(batchReco, std)
score11 = td.kl_divergence(q_z1, q_z2).mean()
score12 = td.kl_divergence(q_z2, q_z1).mean()
score11 = score11 / miniBatch
score12 = score12 / miniBatch
score = (score11 + score12) / 2.0
return score
def extract(input, t, x):
shape = x.shape
out = torch.gather(input, 0, t.to(input.device))
reshape = [t.shape[0]] + [1] * (len(shape) - 1)
return out.reshape(*reshape)
def make_beta_schedule(schedule='linear', n_timesteps=1000, start=1e-5, end=1e-2):
if schedule == 'linear':
betas = torch.linspace(start, end, n_timesteps)
elif schedule == "quad":
betas = torch.linspace(start ** 0.5, end ** 0.5, n_timesteps) ** 2
elif schedule == "sigmoid":
betas = torch.linspace(-6, 6, n_timesteps)
betas = torch.sigmoid(betas) * (end - start) + start
return betas
#
def q_x(x_0, t, noise=None):
num_steps = 100
betas = make_beta_schedule(schedule='sigmoid', n_timesteps=num_steps, start=1e-5, end=0.5e-2)
alphas = 1 - betas
alphas_prod = torch.cumprod(alphas, 0)
alphas_prod = alphas_prod.to(x_0.device)
#alphas_prod_p = torch.cat([torch.tensor([1]).float(), alphas_prod[:-1]], 0)
alphas_bar_sqrt = torch.sqrt(alphas_prod)
one_minus_alphas_bar_log = torch.log(1 - alphas_prod)
one_minus_alphas_bar_sqrt = torch.sqrt(1 - alphas_prod)
if noise is None:
noise = torch.randn_like(x_0)
alphas_t = extract(alphas_bar_sqrt, t, x_0)
alphas_1_m_t = extract(one_minus_alphas_bar_sqrt, t, x_0)
return (alphas_t * x_0 + alphas_1_m_t * noise)
def Calculate_ExpansionScore(TSFramework,batch):
arr = []
#t = torch.tensor([50])
t= 50
for i in range(np.shape(TSFramework.teacherArray)[0]):
currentComponent = TSFramework.teacherArray[i]
buffer = currentComponent.memoryBuffer
#reco1 = currentComponent.q_sample(batch,t) #q_x(buffer,t)
#reco2 = currentComponent.q_sample(buffer,t)#q_x(batch,t)
reco1 = batch
reco2 = buffer
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
reco1 = torch.tensor(reco1).cuda().to(device=device, dtype=torch.float)
reco2 = torch.tensor(reco2).cuda().to(device=device, dtype=torch.float)
score = Calculate_JS(TSFramework,reco1,reco2)
score = score.cpu().detach().numpy()
#score = score[0]
arr.append(score)
#arr = arr.cpu().numpy()
arr = np.array(arr)
maxScore = np.min(arr)
index = np.argmin(arr)
return maxScore,index
def _extract_into_tensor(arr, timesteps, broadcast_shape):
"""
Extract values from a 1-D numpy array for a batch of indices.
:param arr: the 1-D numpy array.
:param timesteps: a tensor of indices into the array to extract.
:param broadcast_shape: a larger shape of K dimensions with the batch
dimension equal to the length of timesteps.
:return: a tensor of shape [batch_size, 1, ...] where the shape has K dims.
"""
res = th.from_numpy(arr).to(device=timesteps.device)[timesteps].float()
while len(res.shape) < len(broadcast_shape):
res = res[..., None]
return res.expand(broadcast_shape)
def LoadModel2():
dataNmae = "CelebAtoChair"
modelName = "DynamicDiffusion"
trainX1,testX1 = Load_CelebA()
trainX2, testX2 = Load_3DChair()
trainX1 = np.concatenate((trainX1,trainX2),0)
threshold = 10
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
defaultTest = np.concatenate((testX1, testX2), 0)
n_examples = np.shape(defaultTest)[0]
index2 = [i for i in range(n_examples)]
np.random.shuffle(index2)
defaultTest = defaultTest[index2]
defaultTest = defaultTest[0:2000]
memoryBuffer2 = trainX1[0:2000]
myModelName = modelName + "_" + dataNmae + ".pkl"
TSFramework = torch.load('./data/' + myModelName)
test_data = torch.tensor(defaultTest).cuda().to(device=device, dtype=torch.float)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#TSFramework.RemoveComponentsThreshold(threshold)
KD_epoch = 5
TSFramework.KnowledgeTransferForStudent(KD_epoch,memoryBuffer2)
batchsize = 2*6
for i in range(10):
test1 = test_data[i*batchsize:(i+1)*batchsize]
reco = TSFramework.student.Give_Reconstruction_Single(test1)
test1_1 = Transfer_To_Numpy(test1)
reco_1 = Transfer_To_Numpy(reco)
name_small1 = "CelebAtoChar_Student_Real_" + str(i) + ".png"
name_small2 = "CelebAtoChar_Student_Reco_" + str(i) + ".png"
Save_Image(name_small1, merge2(test1_1[0:16], [2, 8]))
Save_Image(name_small2, merge2(reco_1[0:16], [2, 8]))
'''
gen = TSFramework.Give_GenerationFromTeacher(100)
generatedImages = Transfer_To_Numpy(gen)
name_generation = dataNmae + "_" + modelName + str(threshold) + ".png"
Save_Image(name_generation, merge2(generatedImages[0:64], [8, 8]))
# Evaluation
test_data = torch.tensor(defaultTest).cuda().to(device=device, dtype=torch.float)
batch = test_data[0:64]
reco = TSFramework.student.Give_Reconstruction(batch)
myReco = Transfer_To_Numpy(reco)
# myReco = merge2(myReco, [8, 8])
realBatch = Transfer_To_Numpy(batch)
# realBatch = merge2(realBatch, [8, 8])
name = dataNmae + "_" + modelName + "_" + "Real_" + str(0) + ".png"
name_small = dataNmae + "_" + modelName + "_" + "Real_small_" + str(0) + ".png"
Save_Image(name, merge2(realBatch, [8, 8]))
Save_Image(name_small, merge2(realBatch[0:16], [2, 8]))
reco = Transfer_To_Numpy(reco)
# realBatch = merge2(realBatch, [8, 8])
name = dataNmae + "_" + modelName + "_" + "Reco_" + str(0) + ".png"
name_small = dataNmae + "_" + modelName + "_" + "Reco_small_" + str(0) + ".png"
Save_Image(name, merge2(reco, [8, 8]))
Save_Image(name_small, merge2(reco[0:16], [2, 8]))
for i in range(np.shape(TSFramework.teacherArray)[0]):
currentTeacher = TSFramework.teacherArray[i]
for j in range(100):
name_small = "Component_" + str(i) + "_" + str(j) + ".png"
gen = currentTeacher.Sampling_By_Num(currentTeacher.diffusion,currentTeacher.model,2*3)
gen = Transfer_To_Numpy(gen)
Save_Image(name_small, merge2(gen[0:2*3], [2, 3]))
'''
'''
# Evaluation
print("Generation")
generated = TSFramework.Give_GenerationFromTeacher(1000)
mytest = test_data[0:np.shape(generated)[0]]
fid1 = calculate_fid_given_paths_Byimages(mytest, generated, 50, device, 2048)
print(fid1)
print("Reconstruction")
print("FID")
# test_data = test_data[0:1000]
# test_data = torch.tensor(test_data).cuda().to(device=device, dtype=torch.float)
recoData = TSFramework.student.Give_Reconstruction(test_data)
# testing = torch.tensor(test_data).cuda().to(device=device, dtype=torch.float)
# recoData = torch.tensor(recoData).cuda().to(device=device, dtype=torch.float)
test_data = test_data[0:recoData.size(0)]
fid1 = calculate_fid_given_paths_Byimages(test_data, recoData, 50, device, 2048)
print(fid1)
print("MSE")
fid1 = GiveMSE(test_data, recoData)
print(fid1)
test_data1 = Transfer_To_Numpy(test_data)
test_data1 = test_data1 / 255.0
recoData = Transfer_To_Numpy(recoData)
recoData = recoData / 255.0
print("psnr and ssim")
psnr = peak_signal_noise_ratio(test_data1, recoData)
ssim = structural_similarity(test_data1, recoData, multichannel=True)
print(psnr)
print(ssim)
'''
def Interpolation(model,batch1,batch2):
z1 = model.student.Give_LatentCode(batch1)
z2 = model.student.Give_LatentCode(batch2)
t = 10
minZ = (z1 - z2) / t
arr1 = []
for i in range(t):
newZ = z2 + minZ*(i+1)
x1 = model.student.GenerateFromLatentCode(newZ)
x1 = Transfer_To_Numpy(x1)
arr1.append(x1[0])
arr1 = np.array(arr1)
return arr1
def LoadModel():
dataNmae = "CelebAtoChair"
modelName = "DynamicDiffusion"
threshold = 10
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
myModelName = modelName + "_" + dataNmae + ".pkl"
TSFramework = torch.load('./data/' + myModelName)
model = TSFramework
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
trainX1, testX1 = Load_CelebA()
trainX2, testX2 = Load_3DChair()
testX1 = torch.tensor(testX1).cuda().to(device=device, dtype=torch.float)
testX2 = torch.tensor(testX2).cuda().to(device=device, dtype=torch.float)
for i in range(30):
batch1 = testX1[i * 64:(i + 1) * 64]
batch2 = testX2[i * 64:(i + 1) * 64]
result = Interpolation(model, batch1, batch2)
print(np.shape(result))
Save_Image("Interpolation_1_" + str(i) + ".png", merge2(result, [1, 10]))
for i in range(30):
batch1 = testX1[i * 64:(i + 1) * 64]
batch2 = testX1[(i + 10) * 64:(i + 11) * 64]
result = Interpolation(model, batch1, batch2)
Save_Image("Interpolation_2_" + str(i) + ".png", merge2(result, [1, 10]))
for i in range(30):
batch1 = testX2[i * 64:(i + 1) * 64]
batch2 = testX2[(i + 10) * 64:(i + 11) * 64]
result = Interpolation(model, batch1, batch2)
Save_Image("Interpolation_3_" + str(i) + ".png", merge2(result, [1, 10]))
'''
gen = TSFramework.Give_GenerationFromTeacher(100)
generatedImages = Transfer_To_Numpy(gen)
name_generation = dataNmae + "_" + modelName + str(threshold) + ".png"
Save_Image(name_generation, merge2(generatedImages[0:64], [8, 8]))
# Evaluation
test_data = torch.tensor(defaultTest).cuda().to(device=device, dtype=torch.float)
batch = test_data[0:64]
reco = TSFramework.student.Give_Reconstruction(batch)
myReco = Transfer_To_Numpy(reco)
# myReco = merge2(myReco, [8, 8])
realBatch = Transfer_To_Numpy(batch)
# realBatch = merge2(realBatch, [8, 8])
name = dataNmae + "_" + modelName + "_" + "Real_" + str(0) + ".png"
name_small = dataNmae + "_" + modelName + "_" + "Real_small_" + str(0) + ".png"
Save_Image(name, merge2(realBatch, [8, 8]))
Save_Image(name_small, merge2(realBatch[0:16], [2, 8]))
reco = Transfer_To_Numpy(reco)
# realBatch = merge2(realBatch, [8, 8])
name = dataNmae + "_" + modelName + "_" + "Reco_" + str(0) + ".png"
name_small = dataNmae + "_" + modelName + "_" + "Reco_small_" + str(0) + ".png"
Save_Image(name, merge2(reco, [8, 8]))
Save_Image(name_small, merge2(reco[0:16], [2, 8]))
'''
'''
for i in range(np.shape(TSFramework.teacherArray)[0]):
currentTeacher = TSFramework.teacherArray[i]
for j in range(100):
name_small = "Component_" + str(i) + "_" + str(j) + ".png"
gen = currentTeacher.Sampling_By_Num(currentTeacher.diffusion,currentTeacher.model,2*3)
gen = Transfer_To_Numpy(gen)
Save_Image(name_small, merge2(gen[0:2*3], [2, 3]))
'''
'''
# Evaluation
print("Generation")
generated = TSFramework.Give_GenerationFromTeacher(1000)
mytest = test_data[0:np.shape(generated)[0]]
fid1 = calculate_fid_given_paths_Byimages(mytest, generated, 50, device, 2048)
print(fid1)
print("Reconstruction")
print("FID")
# test_data = test_data[0:1000]
# test_data = torch.tensor(test_data).cuda().to(device=device, dtype=torch.float)
recoData = TSFramework.student.Give_Reconstruction(test_data)
# testing = torch.tensor(test_data).cuda().to(device=device, dtype=torch.float)
# recoData = torch.tensor(recoData).cuda().to(device=device, dtype=torch.float)
test_data = test_data[0:recoData.size(0)]
fid1 = calculate_fid_given_paths_Byimages(test_data, recoData, 50, device, 2048)
print(fid1)
print("MSE")
fid1 = GiveMSE(test_data, recoData)
print(fid1)
test_data1 = Transfer_To_Numpy(test_data)
test_data1 = test_data1 / 255.0
recoData = Transfer_To_Numpy(recoData)
recoData = recoData / 255.0
print("psnr and ssim")
psnr = peak_signal_noise_ratio(test_data1, recoData)
ssim = structural_similarity(test_data1, recoData, multichannel=True)
print(psnr)
print(ssim)
'''
#
def main():
distanceType = "JS"
dataNmae = "CelebAtoImageNet"
modelName = "GraphMemory"
modelName = modelName + "_" + distanceType
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
trainX1,testX1 = Load_CelebA()
trainX2, testX2 = Load_ImageNet()
defaultTest = np.concatenate((testX1,testX2),0)
n_examples = np.shape(defaultTest)[0]
index2 = [i for i in range(n_examples)]
np.random.shuffle(index2)
defaultTest = defaultTest[index2]
defaultTest = defaultTest[0:1000]
trainX1 = np.concatenate((trainX1,trainX2),0)
print(trainX1[0])
print(np.max(trainX1))
#trainX1 = ((trainX1 + 1) * 127.5)
#testX1 = ((testX1 + 1) * 127.5)
dataStream = trainX1
totalTestX = testX1
miniBatch = 64
totalTrainingTime = int(dataStream.shape[0] / miniBatch)
inputSize = 32
epoch = 1
Tepoch = 100
Sepoch = 100
start = time.time()
inputSize = 64
TSFramework = MemoryUnitGraphFramework("myName",device,inputSize)
TSFramework.distance_type = distanceType
#build the first one
newComponent = TSFramework.Create_NewComponent()
TSFramework.currentComponent = newComponent
batch = dataStream[0:miniBatch]
#batch = torch.tensor(batch).cuda().to(device=device, dtype=torch.float)
newComponent.memoryBuffer = batch
#
memoryBuffer = []
maxMemorySize = 2000
maxMemorySizeDefault = 2000
dataloader = []
threshold = 70
TSFramework.threshold = threshold
epoch = 2
runStep = 0
#
for step in range(totalTrainingTime):
batch = dataStream[step*miniBatch:(step + 1)*miniBatch]
#batch = torch.tensor(batch).cuda().to(device=device, dtype=torch.float)
if np.shape(TSFramework.MemoryClusterArr)[0] == 0:
TSFramework.MemoryBegin(batch)
print("epoch {0}/{1}, step {2}/{3}, train ELBO: {4:.2f}, val ELBO: {5:.2f}, time: {6:.2f}"
.format(step, totalTrainingTime, np.shape(TSFramework.MemoryClusterArr)[0], 0, 1, 0, 1))
batch = torch.tensor(batch).cuda().to(device=device, dtype=torch.float)
# TSFramework.currentMemory = batch
memoryBuffer = TSFramework.GiveMemorizedSamples()
memoryBuffer = torch.tensor(memoryBuffer).cuda().to(device=device, dtype=torch.float)
memoryBuffer_ = torch.cat([memoryBuffer,batch],0)
TSFramework.currentComponent.Train(epoch,memoryBuffer_)
batch = batch.unsqueeze(0).cuda().cpu()
batch = np.array(batch)
batch = batch[0]
TSFramework.AddDataBatch(batch)
#Knolwedge transfer
#TSFramework.RemoveComponentsThreshold(threshold)
#KD_epoch = 20
#TSFramework.KnowledgeTransferForStudent(KD_epoch,memoryBuffer2)
#Save the model
myModelName = modelName + "_" + dataNmae + ".pkl"
torch.save(TSFramework, './data/' + myModelName)
#stu
end = time.time()
print("Training times")
print((end - start))
print("Finish the training")
gen = TSFramework.Give_GenerationFromTeacher(100)
generatedImages = Transfer_To_Numpy(gen)
name_generation = dataNmae + "_" + modelName + str(threshold) + ".png"
Save_Image(name_generation,merge2(generatedImages[0:64], [8, 8]))
#
#Evaluation
test_data = torch.tensor(defaultTest).cuda().to(device=device, dtype=torch.float)
batch = test_data[0:64]
reco = TSFramework.student.Give_Reconstruction(batch)
myReco = Transfer_To_Numpy(reco)
#myReco = merge2(myReco, [8, 8])
realBatch = Transfer_To_Numpy(batch)
#realBatch = merge2(realBatch, [8, 8])
name = dataNmae + "_" + modelName + str(threshold) + "_" + "Real_" + str(0) + ".png"
name_small = dataNmae + "_" + modelName + "_" + "Real_small_" + str(0) + ".png"
Save_Image(name,merge2(realBatch, [8, 8]))
Save_Image(name_small,merge2(realBatch[0:16], [2, 8]))
reco = Transfer_To_Numpy(reco)
# realBatch = merge2(realBatch, [8, 8])
name = dataNmae + "_" + modelName + "_" + "Reco_" + str(0) + ".png"
name_small = dataNmae + "_" + modelName + "_" + "Reco_small_" + str(0) + ".png"
Save_Image(name, merge2(reco, [8, 8]))
Save_Image(name_small, merge2(reco[0:16], [2, 8]))
# Evaluation
print("Generation")
generated = TSFramework.Give_GenerationFromTeacher(1000)
mytest = test_data[0:np.shape(generated)[0]]
fid1 = calculate_fid_given_paths_Byimages(mytest, generated, 50, device, 2048)
print(fid1)
return
print("Reconstruction")
print("FID")
#test_data = test_data[0:1000]
# test_data = torch.tensor(test_data).cuda().to(device=device, dtype=torch.float)
recoData = TSFramework.student.Give_Reconstruction(test_data)
# testing = torch.tensor(test_data).cuda().to(device=device, dtype=torch.float)
# recoData = torch.tensor(recoData).cuda().to(device=device, dtype=torch.float)
test_data = test_data[0:recoData.size(0)]
fid1 = calculate_fid_given_paths_Byimages(test_data, recoData, 50, device, 2048)
print(fid1)
print("MSE")
fid1 = GiveMSE(test_data,recoData)
print(fid1)
test_data1 = Transfer_To_Numpy(test_data)
test_data1 = test_data1 / 255.0
recoData = Transfer_To_Numpy(recoData)
recoData = recoData / 255.0
print("psnr and ssim")
psnr = peak_signal_noise_ratio(test_data1, recoData)
ssim = structural_similarity(test_data1, recoData, multichannel=True)
print(psnr)
print(ssim)
if __name__ == "__main__":
main()
#LoadModel()