-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
138 lines (111 loc) · 3.15 KB
/
test.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
from models import DCGAN
from torchvision import transforms
from pathlib import Path
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import torch
from utils import BlobData
from torch.distributions import Normal
path = Path().absolute()
model_path = path / 'wandb' / 'RGB' / 'files' / 'BlobGAN' / '298o8k9c' / 'checkpoints' / 'epoch=1_global_step=9600.ckpt'
# model_path = path / 'wandb' / 'grayscale' / 'files' / 'BlobGAN' / 'trytk8ib' / 'checkpoints' / 'epoch=1_global_step=9800.ckpt'
# model_path = path / 'data' / 'saved_models' / 'good_run_grey.ckpt'
labels = torch.load('labeled_latents/RGB/labels.pt')
z = torch.load('labeled_latents/RGB/z.pt')
transform = transforms.ToPILImage()
model = DCGAN.load_from_checkpoint(model_path)
model.eval()
a = z[torch.where(labels == 5)]
b = z[torch.where(labels == 3)]
num_imgs = min(a.shape[0], b.shape[0])
print(num_imgs)
idx_a = torch.randint(0, a.shape[0], (num_imgs,))
idx_b = torch.randint(0, b.shape[0], (num_imgs,))
a_vect = a[idx_a].mean(dim=0, keepdim=True)
b_vect = b[idx_b].mean(dim=0, keepdim=True)
c = a_vect - b_vect
std = 0.25
dist = Normal(c, std)
y = model.G(a_vect).squeeze(0)
y = y.permute(1, 2, 0).detach().numpy()
plt.imshow(y)
plt.show()
y = model.G(b_vect).squeeze(0)
y = y.permute(1, 2, 0).detach().numpy()
plt.imshow(y)
plt.show()
n_row, n_col = 2, 2
fig, ax = plt.subplots(n_row, n_col)
idx = 0
for i in range(n_row):
for j in range(n_col):
sample = dist.sample()
y = model.G(sample).squeeze(0)
y = y.permute(1, 2, 0).detach().numpy()
ax[i, j].imshow(y)
plt.show()
# n_row = 3
# n_col = 3
#
# z = model.get_noise(n_row * n_col)
#++++++++++++++++++#
# Labeling latents #
#++++++++++++++++++#
# labels = []
# # for i in range(2):
# for i in range(z.shape[0]):
# x = z[i].unsqueeze(0)
# y = model.G(x).squeeze(0)
# y = y.permute(1, 2, 0).detach().numpy()
# plt.imshow(y)
# plt.show()
# num = input('Number of blobs: ')
# labels.append(int(num))
#
# labels = torch.LongTensor(labels)
#
# torch.save(labels, 'labels.pt')
# torch.save(z, 'z.pt')
#+++++++++++++++#
# Plotting grid #
#+++++++++++++++#
# path = Path().resolve()
# mode = 'RGB'
# data_path = path / 'data' / mode / 'test'
# mode_channels = {'1':1, 'RGB':3}
# img_channels = mode_channels[mode]
#
# data = BlobData(data_path, img_channels)
# fig, ax = plt.subplots(n_row, n_col)
# idx = 0
# for i in range(n_row):
# for j in range(n_col):
# # x = z[idx].unsqueeze(0)
# # y = model.G(x).squeeze(0)
# # y = y.permute(1, 2, 0).detach().numpy()
# # ax[i, j].imshow(y, cmap='gray')
#
# x, y = data[idx]
# x = x.permute(1, 2, 0).detach().numpy()
# ax[i, j].imshow(x)
# idx += 1
#
# plt.show()
#+++++++++++++++++++++++#
# Histogram frequencies #
#+++++++++++++++++++++++#
# import seaborn as sns
# labels = torch.load('labeled_latents/grayscale/labels.pt').numpy()
# labels = []
# for i in range(100):
# _, y = data[i]
# labels.append(y.item() + 1)
#
# print(min(labels), max(labels))
# plt.xlabel('Number of blobs')
# plt.ylabel('Count')
#
# plt.hist(labels, bins=max(labels))
# # sns.histplot(labels)
# plt.show()