-
Notifications
You must be signed in to change notification settings - Fork 3
/
print_intermediates.py
335 lines (282 loc) · 13.6 KB
/
print_intermediates.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
import argparse
from data_utils import dataset_classes, utils
from datetime import datetime
from training_utils import train_utils, quantization
from model_utils import get_model, cnn, rnn
import json
import model_utils
import numpy as np
import os
import random
import sys
import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader, Subset
from torch import optim
from torch.optim.lr_scheduler import CosineAnnealingLR, ReduceLROnPlateau
from tqdm import tqdm
from torch.utils.tensorboard import SummaryWriter
import matplotlib.pyplot as plt
import torch.nn as nn
bit_conversion = {
'binary':1,
'laurie_insp_binary': 1,
'tanh_x':1,
'tanh_mx':1,
'suyeon_gumbel_binary':1,
'laurie_ternary':1.5,
'suyeon_gumbel_ternary':1.5,
'full': 32,
}
# default size is 64. If something different, add to dict
resize_dict = {
"analognet1" : 256,
"analognet2" : 28,
"liu_2020": 32,
"lenet5" : 32,
"laurie_cnn_2": 32,
"group_so_2022": 256,
"FULL_linear": 256,
"event_cam": 64,
'so_2022_new':256,
'so_2022_batch_norm': 256,
'CNN_bin_scamp_256':256
}
video_datasets = ['cambridge', 'hmdb51', 'jester']
image_datasets = ['mnist', 'cifar10']
paths_datasets = {
'cambridge': "/media/data4b/haleyso/Cambridge_Hand_Gesture",
"hmdb51": "/media/data4b/haleyso/human_motion_data/hmdb51_jpg",
"jester": "/media/data4b/haleyso/Jester/20bn-jester-v1"
}
cnn_names = sorted(name for name in cnn.__dict__
if callable(cnn.__dict__[name]))
rnn_names = sorted(name for name in rnn.__dict__
if callable(rnn.__dict__[name]))
def test_scamp_linear(config, save_dir, replace_h_weight=False, save_images=False, input_white=5):
if 'seed' in config.keys():
seed = config['seed']
print(f'seed: {seed}')
else:
print("default seed is 2023")
seed = 2023
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
cnn_model_type = config['model']['cnn_type']
rnn_model_type = config['model']['rnn_type']
cnn_params = config['model']['cnn_params']
rnn_params = config['model']['rnn_params']
train_params = config['train_params']
batch_size = config['batch_size']
max_pool = config['max_pool']
timesteps = config['timesteps']
task = config["task"]
# h, w = 224, 224
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
# dataset
dataset = (config['dataset']).lower()
all_vids, all_labels, cats, csv_file = utils.get_videos(dataset)
print("Number of videos: ", len(all_vids), " | Number of categories: ", len(cats))
# number of classes we're using
if config["train_params"]["num_classes"] != "all":
num_classes = config["train_params"]["num_classes"]
else:
num_classes = len(cats)
# labels2number dictionary
labels2number = {}
cats.sort()
for ind, uc in enumerate(cats):
labels2number[uc] = ind
# split data
print("Number of classes we're using ", num_classes)
# train_ids, train_labels, test_ids, test_labels = utils.split_data(all_vids, all_labels, num_classes, labels2number, csv_file=csv_file, test_size=0.1)
# print("Number of training: ", len(train_ids), " | Number of testing: ", len(test_ids))
train_ids, train_labels, val_ids, val_labels, test_ids, test_labels = utils.split_data(all_vids, all_labels, num_classes, labels2number, csv_file=csv_file, val_size=0.1,test_size=0.1)
print("Number of training: ", len(train_ids), " | Number of validation: ", len(val_ids), " | Number of test:", len(test_ids))
# test videos are in test_ids
# toprint=[]
# for name in test_ids:
# topr = name.split("/")[-1]
# toprint.append(topr)
# toprint.sort()
# print(toprint)
# sys.exit()
# resize shape
if cnn_model_type in resize_dict.keys():
resize_shape = resize_dict[cnn_model_type]
else:
resize_shape = 64
print(resize_shape)
train_transformer = transforms.Compose([
# transforms.RandomHorizontalFlip(p=0.5),
# transforms.RandomAffine(degrees=0, translate=(0.1,0.1)),
transforms.ToTensor(),
transforms.Normalize(mean, std),
transforms.Grayscale(num_output_channels=1),
transforms.Resize([resize_shape,resize_shape], interpolation=transforms.InterpolationMode.BILINEAR),
])
test_transformer = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean, std),
transforms.Grayscale(num_output_channels=1),
transforms.Resize([resize_shape,resize_shape], interpolation=transforms.InterpolationMode.BILINEAR),
])
# test_ds = dataset_classes.VideoDataset(ids= test_ids, labels= test_labels, transform= test_transformer, timesteps=timesteps, labels2number=labels2number, val=True)
# test_dl = DataLoader(test_ds, batch_size= batch_size, num_workers=4, shuffle=False, collate_fn= collate_fn_rnn)
train_ds = dataset_classes.VideoDataset(ids= train_ids, labels= train_labels, transform= train_transformer, timesteps=timesteps, labels2number=labels2number, val=False)
train_ds_test = dataset_classes.VideoDataset(ids= train_ids, labels= train_labels, transform= test_transformer, timesteps=timesteps, labels2number=labels2number, val=False)
val_ds = dataset_classes.VideoDataset(ids= val_ids, labels= val_labels, transform= test_transformer, timesteps=timesteps, labels2number=labels2number,val=True)
test_ds = dataset_classes.VideoDataset(ids= test_ids, labels= test_labels, transform= test_transformer, timesteps=timesteps, labels2number=labels2number,val=True)
# data_loaders
train_dl = DataLoader(train_ds, batch_size= 1, num_workers=1, shuffle=True, collate_fn= collate_fn_rnn) # does shuffle shuffle sequences??
train_test_dl = DataLoader(train_ds_test, batch_size= 1, num_workers=1, shuffle=False, collate_fn= collate_fn_rnn) # does shuffle shuffle sequences??
val_dl = DataLoader(val_ds, batch_size= 1, num_workers=1, shuffle=False, collate_fn= collate_fn_rnn)
test_dl = DataLoader(test_ds, batch_size= 1, num_workers=1, shuffle=False, collate_fn= collate_fn_rnn)
mini = 0
maxi = 0
for im, lab, nam in test_dl:
if im.min() < mini:
mini = im.min()
if im.max() > maxi:
maxi = im.max()
print(f'mini {mini} and maxi {maxi}')
# models
# cnn model
cnn_parameters= {
"cnn_model_type": config['model']['cnn_type'],
"dr_rate": cnn_params['dr_rate'],
"kernel_size": cnn_params['kernel_size'],
"stride": cnn_params['stride'],
"dilation": cnn_params['dilation'],
"groups": cnn_params['groups'],
"bias": cnn_params['bias'],
"cnn_method": train_params["cnn_method"],
"final_conv_method": train_params["final_conv_method"]
}
# rnn model
rnn_parameters = {
"rnn_model_type": config['model']['rnn_type'],
"kernel_size": rnn_params['kernel_size'],
"stride": rnn_params['stride'],
"dilation": rnn_params['dilation'],
"groups": rnn_params['groups'],
"bias": rnn_params['bias'],
"rnn_num_layers": rnn_params['rnn_num_layers'],
"rnn_hidden_size": rnn_params['rnn_hidden_size'],
"method": train_params["rnn_method"]
}
# patches... adding things to the json file
if ("cnn_output_quantization" in train_params.keys()):
cnn_parameters["cnn_output_quantization"] = train_params["cnn_output_quantization"]
# print(train_params.get("cnn_output_quantization"), type(train_params), cnn_parameters["cnn_output_quantization"])
# sys.exit()
else:
cnn_parameters["cnn_output_quantization"] = train_params["cnn_method"]
if ("hidden_quantization" in train_params.keys()):
rnn_parameters["hidden_quantization"] = train_params["hidden_quantization"]
else:
rnn_parameters["hidden_quantization"] = train_params["rnn_method"]
if ("hidden_weight_init_scale" in rnn_params.keys()):
rnn_parameters["hidden_weight_init_scale"] = rnn_params["hidden_weight_init_scale"]
else:
rnn_parameters["hidden_weight_init_scale"] = 1
if ("forget_weight_init_scale" in rnn_params.keys()):
rnn_parameters["forget_weight_init_scale"] = rnn_params["forget_weight_init_scale"]
else:
rnn_parameters["forget_weight_init_scale"] = 1
if ("out_weight_init_scale" in rnn_params.keys()):
rnn_parameters["out_weight_init_scale"] = rnn_params["out_weight_init_scale"]
else:
rnn_parameters["out_weight_init_scale"] = 1
if ("r_weight_init_scale" in rnn_params.keys()):
rnn_parameters["r_weight_init_scale"] = rnn_params["r_weight_init_scale"]
else:
rnn_parameters["r_weight_init_scale"] = 1
if ("z_weight_init_scale" in rnn_params.keys()):
rnn_parameters["z_weight_init_scale"] = rnn_params["z_weight_init_scale"]
else:
rnn_parameters["z_weight_init_scale"] = 1
if ("i_weight_init_scale" in rnn_params.keys()):
rnn_parameters["i_weight_init_scale"] = rnn_params["i_weight_init_scale"]
else:
rnn_parameters["i_weight_init_scale"] = 1
if ("gate_quantization" in train_params.keys()):
rnn_parameters["gate_quantization"] = train_params["gate_quantization"]
else:
rnn_parameters["gate_quantization"] = "full"
model = get_model.get_model(config, cnn_model_type, cnn_parameters, rnn_model_type, rnn_parameters, dataset, num_classes, max_pool)
model_params = model.state_dict()
# print(model)
# sys.exit()
model = model.cuda()
path2weights = config["path2weights"]
print(path2weights)
checkpoint = torch.load(path2weights)
model.load_state_dict(checkpoint)
model.eval()
# replace the weight
if replace_h_weight:
model_params = model.state_dict()
with torch.no_grad():
model_params["rnn_model.Conv_out_h.weight"] = model_params["rnn_model.Conv_out_i.weight"]
model.load_state_dict(model_params)
# portion_saturated_neurons = count_saturated_neurons(model, test_dl)
# sys.exit()
loss_func = train_utils.get_loss_func(train_params['loss'])
test_loss, test_metric = train_utils.loss_epoch(model,loss_func,test_dl, print_intermediates=save_images, save_dir=save_dir, min_max=(mini, maxi), input_white=input_white)
train_loss, train_metric = train_utils.loss_epoch(model,loss_func,train_dl, print_intermediates=False, save_dir=save_dir, min_max=(mini, maxi), input_white=input_white)
print(f'Test Loss: {test_loss} and Acc: {test_metric}')
print(f'Train Loss: {train_loss} and Acc: {train_metric}')
print(f'Intermediates saved to {save_dir}')
def collate_fn_rnn(batch):
imgs_batch, label_batch, names_batch = list(zip(*batch))
imgs_batch = [imgs for imgs in imgs_batch if len(imgs)>0]
label_batch = [torch.tensor(l) for l, imgs in zip(label_batch, imgs_batch) if len(imgs)>0]
imgs_tensor = torch.stack(imgs_batch)
labels_tensor = torch.stack(label_batch)
return imgs_tensor, labels_tensor, names_batch
def count_saturated_neurons(model, data_loader):
model.eval()
total_neurons = 0
saturated_neurons = 0
with torch.no_grad():
for xb, yb, name in data_loader:
xb=xb.cuda()
yb=yb.cuda()
output=model(xb)
for layer in model.children():
if isinstance(layer, (nn.Linear, nn.Conv2d)):
# total_neurons += layer.out_features
total_neurons += layer.out_features if hasattr(layer, 'out_features') else layer.out_channels
# Count saturated neurons (sign function outputs)
min_out = torch.min(torch.abs(output)).item()
max_out = torch.max(torch.abs(output)).item()
ave_out = torch.mean(torch.abs(output)).item()
std_out = torch.std(torch.abs(output)).item()
# print(f"min {min_out} max {max_out} average output {ave_out} and std {std_out}")
saturated_neurons += torch.sum(torch.abs(output) >= 1).item()
print(f"Total Neurons {total_neurons}")
print(f"Saturated Neurons {saturated_neurons}")
portion_saturated = saturated_neurons / total_neurons
print(f"Portion of saturated neurons: {portion_saturated:.2%}")
return portion_saturated
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PixelRNN save out intermediate outputs')
parser.add_argument('-c', '--config', default=None, type=str, help='config file path (default: None)')
parser.add_argument('-sd', '--save_dir', default='/home/haleyso/CNN_CGRU/cam', type=str, help='scamp file path (default: None)')
parser.add_argument('-si', '--save_images', default=False, type=bool, help='Do you want to save our the intermediate outputs?')
parser.add_argument('-rh', '--replace_h_weight', default=False, type=bool, help='Do we want to fix the gate in the rnn and make the h weight equal to the i weight?')
parser.add_argument('-iw', '--input_white', default=10, type=float, help='input white value. default is 10.')
args = parser.parse_args()
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
if args.config: # load config file
with open(args.config) as handle:
config = json.load(handle)
train_utils.ensure_dir(args.save_dir)
else:
sys.exit("Add config file")
# input_white = 10
test_scamp_linear(config, args.save_dir, args.replace_h_weight, args.save_images, args.input_white)
# CUDA_VISIBLE_DEVICES=6 python print_intermediates.py -c best1cnn_cam.json