-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
executable file
·230 lines (172 loc) · 6.11 KB
/
utils.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
import tensorflow as tf
import numpy as np
import cv2
import argparse
from sklearn.utils import shuffle
def Dataset_preprocessing(dataset = 'MNIST', image_type = True):
if dataset == 'mnist':
nch = 1
r = 32
(train_images, _), (test_images, _) = tf.keras.datasets.mnist.load_data()
elif dataset == 'fmnist':
(train_images, _), (test_images, _) = tf.keras.datasets.fashion_mnist.load_data()
r = 32
nch = 1
elif dataset == 'cifar10':
(train_images, _), (test_images, _) = tf.keras.datasets.cifar10.load_data()
r = 32
nch = 3
elif dataset == 'celeba':
celeba = np.load('/raid/konik/data/celeba_64_100k.npy')
celeba = shuffle(celeba)
train_images, test_images = np.split(celeba, [80000], axis=0)
print(type(train_images[0,0,0,0]))
nch = 3
r = 64
elif dataset == 'imagenet':
imagenet = np.load('/raid/Amir/Projects/datasets/Tiny_imagenet.npy')
imagenet = shuffle(imagenet)
train_images, test_images = np.split(imagenet, [80000], axis=0)
nch = 3
r = 64
elif dataset == 'rheo':
rheo = np.load('/raid/Amir/Projects/datasets/rheology.npy')
rheo = shuffle(rheo)
train_images, test_images = np.split(rheo, [1500], axis=0)
nch = 3
r = 64
elif dataset == 'chest':
chest = np.load('/raid/Amir/Projects/datasets/X_ray_dataset_128.npy')[:100000,:,:,0:1]
chest = shuffle(chest)
print(np.shape(chest))
train_images, test_images = np.split(chest, [80000], axis=0)
# print(type(train_images[0,0,0,0]))
nch = 1
r = 128
elif dataset == 'church':
church = np.load('/raid/Amir/Projects/datasets/church_outdoor_train_lmdb_color_64.npy')[:100000,:,:,:]
church = shuffle(church)
print(np.shape(church))
train_images, test_images = np.split(church, [80000], axis=0)
# print(type(train_images[0,0,0,0]))
nch = 3
r = 64
training_images = np.zeros((np.shape(train_images)[0], r, r, 1))
testing_images = np.zeros((np.shape(test_images)[0], r, r, 1))
if train_images.shape[1] != r:
for i in range(np.shape(train_images)[0]):
if nch == 1:
training_images[i,:,:,0] = cv2.resize(train_images[i] , (r,r))
else:
training_images[i] = cv2.resize(train_images[i] , (r,r))
for i in range(np.shape(test_images)[0]):
if nch == 1:
testing_images[i,:,:,0] = cv2.resize(test_images[i] , (r,r))
else:
testing_images[i] = cv2.resize(test_images[i] , (r,r))
else:
training_images = train_images
testing_images = test_images
# Normalize the images to [-1, 1]
training_images = training_images.astype('float32')
training_images /= (training_images.max()/2)
training_images = training_images - 1.0
testing_images = testing_images.astype('float32')
testing_images /= (testing_images.max()/2)
testing_images = testing_images - 1.0
if not image_type:
training_images = training_images.reshape(-1, r**2)
testing_images = testing_images.reshape(-1, r**2)
return training_images , testing_images
def flags():
parser = argparse.ArgumentParser()
parser.add_argument(
'--num_epochs',
type=int,
default=400,
help='number of epochs to train for')
parser.add_argument(
'--batch_size',
type=int,
default=64,
help='batch_size')
parser.add_argument(
'--dataset',
type=str,
default='mnist',
help='which dataset to work with')
parser.add_argument(
'--lr',
type=float,
default=1e-4,
help='learning rate')
parser.add_argument(
'--ml_threshold',
type=int,
default= 2,
help='when should ml training begin')
parser.add_argument(
'--model_depth',
type=int,
default= 6,
help='revnet depth of model')
parser.add_argument(
'--latent_depth',
type=int,
default= 3,
help='revnet depth of latent model')
parser.add_argument(
'--learntop',
type=int,
default=1,
help='Trainable top')
parser.add_argument(
'--gpu_num',
type=int,
default=0,
help='GPU number')
parser.add_argument(
'--remove_all',
type= int,
default= 0,
help='Remove the previous experiment')
parser.add_argument(
'--desc',
type=str,
default='Default',
help='add a small descriptor to folder name')
parser.add_argument('--train',
default=False, action='store_true')
parser.add_argument('--notrain',
dest='train', action='store_false')
parser.add_argument('--inv',
default=False, action='store_true')
parser.add_argument('--noinv',
dest='inv', action='store_false')
parser.add_argument('--posterior',
default=False, action='store_true')
parser.add_argument('--noposterior',
dest='posterior', action='store_false')
parser.add_argument('--calc_logdet',
default=False, action='store_true')
parser.add_argument('--nocalc_logdet',
dest='calc_logdet', action='store_false')
parser.add_argument('--inv_prob',
default='denoising', type=str, help='choose from denoising (default) | sr | randmask | randgauss')
parser.add_argument(
'--snr',
type=float,
default=50,
help='measurement SNR (dB)')
parser.add_argument(
'--inv_conv_activation',
type=str,
default= 'linear',
help='activation of invertible 1x1 conv layer')
parser.add_argument(
'--T',
type=float,
default= 1,
help='sampling tempreture')
FLAGS, unparsed = parser.parse_known_args()
return FLAGS, unparsed