-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_testing.py
157 lines (134 loc) · 5.63 KB
/
sample_testing.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
import os
import shutil
import io
import signal
from contextlib import redirect_stdout
import argparse
##################### Utils #########################
def write_output(output, fpath):
f = open(fpath, "w")
f.write(str(output))
f.close()
def mkdir(name, rm=True):
if not os.path.exists(name):
os.makedirs(name)
elif rm:
shutil.rmtree(name)
os.makedirs(name)
class TimeOutException(Exception):
pass
def handler(signum, frame):
raise TimeOutException()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='hw7')
parser.add_argument('--data_folder', type=str)
parser.add_argument('--test', type=int, default=0)
parser.add_argument('--single', action='store_true')
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
###################################
# Grade #
###################################
report_path = 'scores.txt'
output_path = 'output'
mkdir(output_path)
freport = open(report_path, 'w')
signal.signal(signal.SIGALRM, handler)
fnames = ['part1', 'part2', 'part3']
fnames_description = ['Part 1: Creating LeNet',
'Part 2: Calculating the model parameters',
'Part 3: Training under different configurations']
ntests = 3
model_path = './model_best.pth.tar'
total_score = 0
for i in range(1, ntests + 1):
if args.single and i != int(args.test):
continue
out = ''
score = 0
message = ''
if True:
try:
if i == 1:
temp1 = 0
from student_code import LeNet
import torch
model = LeNet()
try:
# breakpoint()
gt = {1: [],
2: [2, 16, 5, 5],
3: [],
4: [],
5: [],
6: []}
_, output = model(torch.rand(2, 3, 32, 32))
except:
score = 0
temp1 = 1
message += '\nYour model cannot accept the input of the required dataset.\n'
message += '\nScore for Part 1 is 0.\n'
if temp1 != 1:
try:
if output[2][0] == gt[2][0] and output[2][1] == gt[2][1] and \
output[2][2] == gt[2][2] and output[2][3] == gt[2][3]:
score += 30
message += '\nScore less than 30 means missing keys or wrong shape existed.\n'
except:
message += '\nUnexpected keys or no keys in the returned dict.\n'
message += '\nScore for Part 1 is ' + str(score) + '\n'
message += '\nYour output is:\n'
message += str(output)
message += '\n'
out = output
elif i == 2:
from student_code import count_model_params
import torch
params = count_model_params()
if params < 0.1 or params > 0.2:
score = 0
message += '\nParamters are not calculated in a right range!\n'
else:
score += 20
message += '\nScore for Part 2 is ' + str(score) + '\n'
out = params
message += '\nYour output is:\n'
message += str(out)
message += '\nThe desired output is in 0.1 to 0.2. Just because you pass this does not mean your value is correct\n'
elif i == 3:
f = open("results.txt", "r")
files = f.readlines()
try:
if float(files[6].strip()) > 12.85 and float(files[6].strip()) < 14.85:
score += 50
except:
message += '\nThe results in the .txt file are not complete.\n'
message += '\nScore for Part 3 is ' + str(score) + '\n'
message += '\nYour output 7 is:\n'
files_new = float(files[6].strip())
message += str(files_new)
message += '\nThe desired output 7 (bigger or less than this one within 1% range is acceptable) is:\n'
message += str([13.85])
message += '\n'
total_score += score
except TimeOutException as exc:
message = "Time Out"
except ImportError:
message = "Function is NOT found"
except Exception as e:
message = "Exception: " + str(e)
mess = "{}. {} {}\n".format(i, fnames_description[i - 1], message)
if args.single and int(args.test) == i:
print(mess)
print('Output: ', out)
print('Score: ', score)
else:
if not args.single:
print(mess)
freport.write(mess)
write_output(out, os.path.join(
output_path, '{}.txt'.format(fnames[i - 1])))
if not (args.single or args.debug):
print('===> score: {}'.format(total_score))
freport.write('Total: {}/100'.format(total_score))
freport.close()