-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathload_pickle.py
234 lines (229 loc) · 9 KB
/
load_pickle.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
import pickle
import torch
import os
from os.path import exists
import fnmatch
import natsort
from re import search
import pandas as pd
filepath = '/data/users2/dkhosravinezhad1/MISA-pytorch/run'
loss_filepath = '/data/users2/dkhosravinezhad1/MISA-pytorch/slurm_log'
array_number = len(os.listdir(filepath)[2:])
slurm_length = int(len(os.listdir(loss_filepath))/2)
slurm_filename = []
for i in os.listdir(loss_filepath):
if fnmatch.fnmatch(i, 'output*'):
slurm_filename.append(i)
slurm_filename = natsort.natsorted(slurm_filename)
slurm_final_loss = [-1]
filename = 'res_sim-siva.p'# input("Insert file directory here: ")
loss = []
seventy_fifth_loss = []
filetype = filename[-2:]
lr = []
epochs = []
batch_size = []
epoch = []
h = 0
ind = 0
def SLURM():
global ind
slurm_b = slurm_filename[ind].split('-')[1]
slurm_c = slurm_b.split('.')[0]
with open(slurm_full, 'r') as lost:
loss_line = lost.readlines()[-2]
print("array " + slurm_c + " final loss: " + loss_line[-27:-19])
loss.append(loss_line[-27:-19])
lost.seek(0)
seventy = len(lost.readlines())
if seventy >= 78:
with open(slurm_full, 'r') as lost:
seven_five = lost.readlines()[78]
print("array " + slurm_c + " 75th loss: " + seven_five[-27:-19])
seventy_fifth_loss.append(seven_five[-27:-19])
lost.seek(0)
else:
print(slurm_full + " does not contain a 75th epoch")
seventy_fifth_loss.append(None)
with open(slurm_full, 'r') as lost:
epoch_iterations = lost.readlines()[4:-1]
epoch_list = []
for i in epoch_iterations:
epoch_list.append(i[-27:-19])
for i, l in enumerate(epoch_list):
if search('170', l[:-5]):
epoch.append(i+1)
print("array " + slurm_c + " MATLAB loss epoch: " + str(i+1))
break
elif search('169', l[:-5]):
epoch.append(i+1)
print("array " + slurm_c + " MATLAB loss epoch: " + str(i+1))
break
else:
epoch.append(400)
print("array " + slurm_c + " MATLAB loss epoch: 400")
ind += 1
# a="xxxxx-<index>.out" # slurm filename
# b=a.split('-')[1] # here you get "<index>.out"
# c=b.split('.')[0] # here you get "<index>"
for i in range(slurm_length):
slurm_full = os.path.join(loss_filepath,slurm_filename[i])
output_exists = exists(slurm_full)
if os.stat(slurm_full).st_size != 0:
SLURM()
else:
print(slurm_full + " has no contents. Check error file for problem!")
for i in range(array_number):
full_filename = os.path.join(filepath,str(i), filename)
file_exists = exists(full_filename)
if file_exists:
if i < 10:
j = full_filename[-16]
with open(full_filename, 'rb') as handle:
b = pickle.load(handle)
lr.append(b['lr'])
epochs.append(b['epochs'])
batch_size.append(b['batch_size'])
print("array " + str(j) + " learning rate = " + str(lr[int(j)]))
print("array " + str(j) + " epochs = " + str(epochs[int(j)]))
print("array " + str(j) + " batch size = " + str(batch_size[int(j)]))
elif i > 99:
j = int(full_filename[-18:-15])
with open(full_filename, 'rb') as handle:
b = pickle.load(handle)
lr.append(b['lr'])
epochs.append(b['epochs'])
batch_size.append(b['batch_size'])
print("array " + str(j) + " learning rate = " + str(lr[j-h]))
print("array " + str(j) + " epochs = " + str(epochs[j-h]))
print("array " + str(j) + " batch size = " + str(batch_size[j-h]))
else:
j = int(full_filename[-17:-15])
with open(full_filename, 'rb') as handle:
b = pickle.load(handle)
lr.append(b['lr'])
epochs.append(b['epochs'])
batch_size.append(b['batch_size'])
print("array " + str(j) + " learning rate = " + str(lr[j-h]))
print("array " + str(j) + " epochs = " + str(epochs[j-h]))
print("array " + str(j) + " batch size = " + str(batch_size[j-h]))
elif filetype == "pt":
print(torch.load(full_filename,map_location=torch.device('cpu')))
else:
print(full_filename + " does not exist or is corrupted.")
h += 1
print("learning rate list:" + str(lr))
print("epochs list: " + str(epochs))
print("batch size list: " + str(batch_size))
print("final loss list: " + str(loss))
print('75th loss list: ' + str(seventy_fifth_loss))
print('MATLAB loss epoch list: ' + str(epoch))
print("learning rate list length: " + str(len(lr)))
print("epochs list length: " + str(len(epochs)))
print("batch size list length: " + str(len(batch_size)))
print("final loss list length: " + str(len(loss)))
print("75th loss list length: " + str(len(seventy_fifth_loss)))
print('MATLAB loss epoch list length: ' + str(len(epoch)))
df = pd.DataFrame(list(zip(lr, epochs, batch_size, loss, seventy_fifth_loss, epoch)),
columns =['learning rate', 'epochs', 'batch size','final loss','75th loss', 'MATLAB loss epoch'])
df.to_csv('/data/users2/dkhosravinezhad1/MISA-pytorch/slurm_csv/SLURM.csv', index = True)
# Leftover code that might come into use idk
'''if i < 10:
with open(slurm_full, 'r') as lost:
loss_line = lost.readlines()[-2]
print("array " + (slurm_full[-19:])[-5] + " final loss: " + loss_line[-27:-19])
loss.append(loss_line[-27:-19])
lost.seek(0)
seventy = len(lost.readlines())
if seventy >= 78:
with open(slurm_full, 'r') as lost:
seven_five = lost.readlines()[78]
print("array " + (slurm_full[-20:])[-5] + " 75th loss: " + seven_five[-27:-19])
seventy_fifth_loss.append(seven_five[-27:-19])
lost.seek(0)
else:
print(slurm_full + " does not contain a 75th epoch")
seventy_fifth_loss.append(None)
with open(slurm_full, 'r') as lost:
epoch_iterations = lost.readlines()[4:-1]
epoch_list = []
for i in epoch_iterations:
epoch_list.append(i[-27:-19])
for i, l in enumerate(epoch_list):
if search('170', l):
epoch.append(i+1)
print("array " + (slurm_full[-20:])[-5] + " MATLAB loss epoch: " + str(i+1))
break
elif search('169', l):
epoch.append(i+1)
print("array " + (slurm_full[-20:])[-5] + " MATLAB loss epoch: " + str(i+1))
break
else:
epoch.append(400)
print("array " + (slurm_full[-20:])[-5] + " MATLAB loss epoch: 400")
elif i < 99:
with open(slurm_full, 'r') as lost:
loss_line = lost.readlines()[-2]
print("array " + (slurm_full[-20:])[-6:-4] + " final loss: " + loss_line[-27:-19])
loss.append(loss_line[-27:-19])
lost.seek(0)
seventy = len(lost.readlines())
if seventy >= 78:
with open(slurm_full, 'r') as lost:
seven_five = lost.readlines()[78]
print("array " + (slurm_full[-20:])[-6:-4] + " 75th loss: " + seven_five[-27:-19])
seventy_fifth_loss.append(seven_five[-27:-19])
lost.seek(0)
else:
print(slurm_full + " does not contain a 75th epoch")
seventy_fifth_loss.append(None)
with open(slurm_full, 'r') as lost:
epoch_iterations = lost.readlines()[4:-1]
epoch_list = []
for i in epoch_iterations:
epoch_list.append(i[-27:-19])
for i, l in enumerate(epoch_list):
if search('170', l):
epoch.append(i)
print("array " + (slurm_full[-20:])[-6:-4] + " MATLAB loss epoch: " + str(i+1))
break
elif search('169', l):
epoch.append(i)
print("array " + (slurm_full[-20:])[-6:-4] + " MATLAB loss epoch: " + str(i+1))
break
else:
epoch.append(400)
print("array " + (slurm_full[-20:])[-6:-4] + " MATLAB loss epoch: 400")
else:
with open(slurm_full, 'r') as lost:
loss_line = lost.readlines()[-2]
print("array " + (slurm_full[-21:])[-6:-3] + " final loss: " + loss_line[-27:-19])
loss.append(loss_line[-27:-19])
lost.seek(0)
seventy = len(lost.readlines())
if seventy >= 78:
with open(slurm_full, 'r') as lost:
seven_five = lost.readlines()[78]
print("array " + (slurm_full[-20:])[-6:-3] + " 75th loss: " + seven_five[-27:-19])
seventy_fifth_loss.append(seven_five[-27:-19])
lost.seek(0)
else:
print(slurm_full + " does not contain a 75th epoch")
seventy_fifth_loss.append(None)
with open(slurm_full, 'r') as lost:
epoch_iterations = lost.readlines()[4:-1]
epoch_list = []
for i in epoch_iterations:
epoch_list.append(i[-27:-19])
for i, l in enumerate(epoch_list):
if search('170', l):
epoch.append(i)
print("array " + (slurm_full[-20:])[-6:-3] + " MATLAB loss epoch: " + str(i+1))
break
elif search('169', l):
epoch.append(i)
print("array " + (slurm_full[-20:])[-6:-3] + " MATLAB loss epoch: " + str(i+1))
break
else:
epoch.append(400)
print("array " + (slurm_full[-20:])[-6:-3] + " MATLAB loss epoch: 400")'''