This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathGenerateSamples.py
More file actions
325 lines (229 loc) · 9.43 KB
/
GenerateSamples.py
File metadata and controls
325 lines (229 loc) · 9.43 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
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
# -*- coding: utf-8 -*-
import collections
import torch
import torch.nn as nn
import numpy as np
from sklearn.neighbors import NearestNeighbors
import os
print(torch.version.cuda) #10.1
import time
t0 = time.time()
##############################################################################
"""args for models"""
args = {}
args['dim_h'] = 64 # factor controlling size of hidden layers
args['n_channel'] = 1 # number of channels in the input data
args['n_z'] = 300 #600 # number of dimensions in latent space.
args['sigma'] = 1.0 # variance in n_z
args['lambda'] = 0.01 # hyper param for weight of discriminator loss
args['lr'] = 0.0002 # learning rate for Adam optimizer .000
args['epochs'] = 1 #50 # how many epochs to run for
args['batch_size'] = 100 # batch size for SGD
args['save'] = True # save weights at each epoch of training if True
args['train'] = True # train networks if True, else load networks from
args['dataset'] = 'mnist' #'fmnist' # specify which dataset to use
##############################################################################
## create encoder model and decoder model
class Encoder(nn.Module):
def __init__(self, args):
super(Encoder, self).__init__()
self.n_channel = args['n_channel']
self.dim_h = args['dim_h']
self.n_z = args['n_z']
# convolutional filters, work excellent with image data
self.conv = nn.Sequential(
nn.Conv2d(self.n_channel, self.dim_h, 4, 2, 1, bias=False),
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.dim_h, self.dim_h * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.dim_h * 2),
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.dim_h * 2, self.dim_h * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.dim_h * 4),
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 2, 1, bias=False),
#3d and 32 by 32
#nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(self.dim_h * 8), # 40 X 8 = 320
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True) )#,
#nn.Conv2d(self.dim_h * 8, 1, 2, 1, 0, bias=False))
#nn.Conv2d(self.dim_h * 8, 1, 4, 1, 0, bias=False))
# final layer is fully connected
self.fc = nn.Linear(self.dim_h * (2 ** 3), self.n_z)
def forward(self, x):
#print('enc')
#print('input ',x.size()) #torch.Size([100, 3,32,32])
x = self.conv(x)
#print('aft conv ',x.size()) #torch.Size([100, 320, 2, 2]) with
#nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 2, 1, bias=False),
#vs torch.Size([128, 320, 1, 1])
#aft conv torch.Size([100, 320, 1, 1]) with
#nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 1, 0, bias=False),
x = x.squeeze()
#print('aft squeeze ',x.size()) #torch.Size([128, 320])
#aft squeeze torch.Size([100, 320])
x = self.fc(x)
#print('out ',x.size()) #torch.Size([128, 20])
#out torch.Size([100, 300])
return x
class Decoder(nn.Module):
def __init__(self, args):
super(Decoder, self).__init__()
self.n_channel = args['n_channel']
self.dim_h = args['dim_h']
self.n_z = args['n_z']
# first layer is fully connected
self.fc = nn.Sequential(
nn.Linear(self.n_z, self.dim_h * 8 * 7 * 7),
nn.ReLU())
# deconvolutional filters, essentially inverse of convolutional filters
self.deconv = nn.Sequential(
nn.ConvTranspose2d(self.dim_h * 8, self.dim_h * 4, 4),
nn.BatchNorm2d(self.dim_h * 4),
nn.ReLU(True),
nn.ConvTranspose2d(self.dim_h * 4, self.dim_h * 2, 4),
nn.BatchNorm2d(self.dim_h * 2),
nn.ReLU(True),
nn.ConvTranspose2d(self.dim_h * 2, 1, 4, stride=2),
#nn.Sigmoid()
nn.Tanh())
def forward(self, x):
#print('dec')
#print('input ',x.size())
x = self.fc(x)
x = x.view(-1, self.dim_h * 8, 7, 7)
x = self.deconv(x)
return x
##############################################################################
def biased_get_class1(c):
xbeg = dec_x[dec_y == c]
ybeg = dec_y[dec_y == c]
return xbeg, ybeg
#return xclass, yclass
def G_SM1(X, y,n_to_sample,cl):
# fitting the model
n_neigh = 5 + 1
nn = NearestNeighbors(n_neighbors=n_neigh, n_jobs=1)
nn.fit(X)
dist, ind = nn.kneighbors(X)
# generating samples
base_indices = np.random.choice(list(range(len(X))),n_to_sample)
neighbor_indices = np.random.choice(list(range(1, n_neigh)),n_to_sample)
X_base = X[base_indices]
X_neighbor = X[ind[base_indices, neighbor_indices]]
samples = X_base + np.multiply(np.random.rand(n_to_sample,1),
X_neighbor - X_base)
#use 10 as label because 0 to 9 real classes and 1 fake/smoted = 10
return samples, [cl]*n_to_sample
#############################################################################
np.printoptions(precision=5,suppress=True)
dtrnimg = '.../0_trn_img.txt'
dtrnlab = '.../0_trn_lab.txt'
ids = os.listdir(dtrnimg)
idtri_f = [os.path.join(dtrnimg, image_id) for image_id in ids]
print(idtri_f)
ids = os.listdir(dtrnlab)
idtrl_f = [os.path.join(dtrnlab, image_id) for image_id in ids]
print(idtrl_f)
#path on the computer where the models are stored
modpth = '.../MNIST/models/crs5/'
encf = []
decf = []
for p in range(5):
enc = modpth + '/' + str(p) + '/bst_enc.pth'
dec = modpth + '/' + str(p) + '/bst_dec.pth'
encf.append(enc)
decf.append(dec)
#print(enc)
#print(dec)
#print()
for m in range(5):
print(m)
trnimgfile = idtri_f[m]
trnlabfile = idtrl_f[m]
print(trnimgfile)
print(trnlabfile)
dec_x = np.loadtxt(trnimgfile)
dec_y = np.loadtxt(trnlabfile)
print('train imgs before reshape ',dec_x.shape) #(44993, 3072) 45500, 3072)
print('train labels ',dec_y.shape) #(44993,) (45500,)
dec_x = dec_x.reshape(dec_x.shape[0],1,28,28)
print('decy ',dec_y.shape)
print(collections.Counter(dec_y))
print('train imgs after reshape ',dec_x.shape) #(45000,3,32,32)
classes = ('0', '1', '2', '3', '4',
'5', '6', '7', '8', '9')
#generate some images
train_on_gpu = torch.cuda.is_available()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
path_enc = encf[m]
path_dec = decf[m]
encoder = Encoder(args)
encoder.load_state_dict(torch.load(path_enc), strict=False)
encoder = encoder.to(device)
decoder = Decoder(args)
decoder.load_state_dict(torch.load(path_dec), strict=False)
decoder = decoder.to(device)
encoder.eval()
decoder.eval()
#imbal = [4500, 2000, 1000, 800, 600, 500, 400, 250, 150, 80]
imbal = [4000, 2000, 1000, 750, 500, 350, 200, 100, 60, 40]
resx = []
resy = []
for i in range(1,10):
xclass, yclass = biased_get_class1(i)
print(xclass.shape) #(500, 3, 32, 32)
print(yclass[0]) #(500,)
#encode xclass to feature space
xclass = torch.Tensor(xclass)
xclass = xclass.to(device)
xclass = encoder(xclass)
print(xclass.shape) #torch.Size([500, 600])
xclass = xclass.detach().cpu().numpy()
n = imbal[0] - imbal[i]
xsamp, ysamp = G_SM1(xclass,yclass,n,i)
print(xsamp.shape) #(4500, 600)
print(len(ysamp)) #4500
ysamp = np.array(ysamp)
print(ysamp.shape) #4500
"""to generate samples for resnet"""
xsamp = torch.Tensor(xsamp)
xsamp = xsamp.to(device)
#xsamp = xsamp.view(xsamp.size()[0], xsamp.size()[1], 1, 1)
#print(xsamp.size()) #torch.Size([10, 600, 1, 1])
ximg = decoder(xsamp)
ximn = ximg.detach().cpu().numpy()
print(ximn.shape) #(4500, 3, 32, 32)
#ximn = np.expand_dims(ximn,axis=1)
print(ximn.shape) #(4500, 3, 32, 32)
resx.append(ximn)
resy.append(ysamp)
#print('resx ',resx.shape)
#print('resy ',resy.shape)
#print()
resx1 = np.vstack(resx)
resy1 = np.hstack(resy)
#print(resx1.shape) #(34720, 3, 32, 32)
#resx1 = np.squeeze(resx1)
print(resx1.shape) #(34720, 3, 32, 32)
print(resy1.shape) #(34720,)
resx1 = resx1.reshape(resx1.shape[0],-1)
print(resx1.shape) #(34720, 3072)
dec_x1 = dec_x.reshape(dec_x.shape[0],-1)
print('decx1 ',dec_x1.shape)
combx = np.vstack((resx1,dec_x1))
comby = np.hstack((resy1,dec_y))
print(combx.shape) #(45000, 3, 32, 32)
print(comby.shape) #(45000,)
ifile = '.../MNIST/trn_img_f/' + \
str(m) + '_trn_img.txt'
np.savetxt(ifile, combx)
lfile = '.../MNIST/trn_lab_f/' + \
str(m) + '_trn_lab.txt'
np.savetxt(lfile,comby)
print()
t1 = time.time()
print('final time(min): {:.2f}'.format((t1 - t0)/60))