forked from Rithwikksvr/EdgeNILM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflops_compute.py
175 lines (123 loc) · 6.73 KB
/
flops_compute.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
import numpy as np
import pandas as pd
import time
import math
import matplotlib.pyplot as plt
import os
import math
import torch
from torch import nn
import torch.nn.functional as F
import torch.optim as optim
from collections import OrderedDict
import time
import sys
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
from functions import *
from networks import Seq2Point
from pthflops import count_ops
def compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr, num_samples=5):
dir_name = "fold_%s_models"%(fold_number)
dir_name = os.path.join(dir_name, "sequence_length_%s"%(sequence_length))
dir_name = os.path.join(dir_name, model_name)
print (dir_name)
models_dir = dir_name
parameters_path = os.path.join(dir_name, 'parameters.json')
f = open(parameters_path)
parameters = json.load(f)
all_appliances_predictions = []
total_flops = 0
set_seed()
flops_tensor = torch.rand(1,1,sequence_length).to('cpu')
if 'mtl' in model_name:
model_path = os.path.join(dir_name, "weights.pth")
model = torch.load(model_path,map_location=torch.device('cpu'))
model.eval()
total_flops+=count_ops(model, flops_tensor)[0]
else:
for appliance_index, appliance_name in enumerate(appliances):
model_path = os.path.join(dir_name, "%s.pth"%(appliance_name))
model = torch.load(model_path,map_location=torch.device('cpu'))
model.eval()
total_flops+=count_ops(model, flops_tensor)[0]
print (total_flops)
results = []
results.append(model_name)
results.append(sequence_length)
results.append(total_flops)
results_arr.append(results)
appliances = ["fridge",'dish washer','washing machine']
appliances.sort()
batch_size=1
fold_number=1 # The fold weights to compute the run time of NN
sequence_lengths = [499, 99]
cuda=False
results_arr = []
for method in ['fully_shared_mtl_iterative_pruning','fully_shared_mtl_pruning','fully_shared_mtl','unpruned_model','tensor_decomposition','normal_pruning','iterative_pruning']:
print ("Batch size:", batch_size)
for sequence_length in sequence_lengths:
if method=='unpruned_model':
print ("-"*50)
print ("Results unpruned model; sequence length: %s "%(sequence_length))
compute_model_stats('unpruned_model', appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
elif method=='normal_pruning':
for pruned_percentage in [30, 60, 90]:
print ("-"*50)
print ("Results for %s percent Pruning; sequence length: %s "%(pruned_percentage, sequence_length))
model_name = "pruned_model_%s_percent" %(pruned_percentage)
compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
elif method=='iterative_pruning':
for iterative_pruned_percentage in [30, 60, 90]:
print ("-"*50)
print ("Results for %s percent Iterative Pruning; sequence length: %s "%(iterative_pruned_percentage, sequence_length))
model_name = "iterative_model_%s_percent" %(iterative_pruned_percentage)
compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
elif method=='tensor_decomposition':
for rank in [1,2, 4,8 ]:
print ("-"*50)
print ("Results for rank %s tensor decomposition; sequence length: %s "%(rank, sequence_length))
model_name = 'tensor_decomposition_rank_%s'%(rank)
compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
elif method == 'fully_shared_mtl':
print ("-"*50)
print ("Results for Fully shared MTL Model; sequence length: %s "%(sequence_length))
model_name = method
compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
elif method == 'normal_mtl':
print ("-"*50)
print ("Results for Normal MTL Model; sequence length: %s "%(sequence_length))
model_name = method
compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
elif method=='fully_shared_mtl_pruning':
for pruned_percentage in [30, 60, 90]:
print ("-"*50)
print ("Results for Fully shared MTL %s percent Pruning; sequence length: %s "%(pruned_percentage, sequence_length))
model_name = "fully_shared_mtl_pruning_%s_percent" %(pruned_percentage)
compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
elif method=='fully_shared_mtl_iterative_pruning':
for pruned_percentage in [30, 60, 90]:
print ("-"*50)
print ("Results for Fully shared MTL %s percent Iterative Pruning; sequence length: %s "%(pruned_percentage, sequence_length))
model_name = "fully_shared_mtl_iterative_model_%s_percent" %(pruned_percentage)
compute_model_stats(model_name, appliances, fold_number, sequence_length, batch_size, results_arr)
print ("-"*50)
print ("\n\n\n")
columns = ['Model Name',"Sequence Length", 'Total Flops']
results_arr= np.array(results_arr)
df = pd.DataFrame(data=results_arr, columns=columns, index = range(len(results_arr)))
df.to_csv('flops.csv',index=False)