-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAll_Eval_A05.py
210 lines (163 loc) · 7.32 KB
/
All_Eval_A05.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
###############################################################################
# IMPORTS
###############################################################################
import os
import sys
import numpy as np
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import v2
from sklearn.metrics import (accuracy_score, f1_score)
import time
import A05
base_dir = "all_assign05"
out_dir = base_dir + "/" + "output"
###############################################################################
# CALCULATE METRICS
###############################################################################
def compute_metrics(ground, pred):
scores = {}
scores["accuracy"] = accuracy_score(y_true=ground, y_pred=pred)
scores["f1"] = f1_score(y_true=ground, y_pred=pred, average="macro")
return scores
###############################################################################
# GET PREDICTIONS FROM MODEL
###############################################################################
def get_predictions_and_ground(model, dataloader, device):
# Set model to evaluation mode
model.eval()
# Create lists for ground and pred
all_ground = []
all_pred = []
with torch.no_grad():
for X, y in dataloader:
# Append ground truth info
all_ground.append(y)
# Move data to device
X, y = X.to(device), y.to(device)
# Run prediction
pred = model(X)
# Get largest class prediction
pred = pred.argmax(1)
# Move to CPU
pred = pred.cpu()
# Append to list
all_pred.append(pred)
# Convert to single Tensor and then numpy
all_ground = torch.concat(all_ground).numpy()
all_pred = torch.concat(all_pred).numpy()
return {"ground": all_ground, "pred": all_pred}
###############################################################################
# PRINTS RESULTS (to STDOUT or file)
###############################################################################
def print_results(approach_data, stream=sys.stdout):
boundary = "****************************************"
###########################################################################
# Names and descriptions
###########################################################################
print(boundary, file=stream)
print("APPROACHES: ", file=stream)
print(boundary, file=stream)
print("", file=stream)
for approach_name in approach_data:
print("*", approach_name, file=stream)
print("\t", A05.get_approach_description(approach_name), file=stream)
print("", file=stream)
# Grab at least one model metric list
model_metrics = approach_data[approach_name]["metrics"]
###########################################################################
# Results
###########################################################################
print(boundary, file=stream)
print("RESULTS:", file=stream)
print(boundary, file=stream)
# Create header
header = "APPROACH"
for data_type in model_metrics:
data_metrics = model_metrics[data_type]
for key in data_metrics:
header += "\t" + data_type + "_" + key
table_data = header + "\n"
# Add data
for approach_name in approach_data:
model_metrics = approach_data[approach_name]["metrics"]
table_data += approach_name
for data_type in model_metrics:
data_metrics = model_metrics[data_type]
for key in data_metrics:
cell_string = "\t%.4f" % data_metrics[key]
table_data += cell_string
table_data += "\n"
print(table_data, file=stream)
###########################################################################
# Models
###########################################################################
print(boundary, file=stream)
print("MODEL ARCHITECTURES:", file=stream)
print(boundary, file=stream)
for approach_name in approach_data:
model = approach_data[approach_name]["model"]
print("*", approach_name, file=stream)
print(model, file=stream)
print("", file=stream)
###############################################################################
# MAIN
###############################################################################
def main():
# Get names of all approaches
all_names = A05.get_approach_names()
chosen_approach_names = all_names
approach_data = {}
for approach_name in chosen_approach_names:
print("EVALUATING APPROACH:", approach_name)
approach_data[approach_name] = {}
# Create only the testing data transform
transform = A05.get_data_transform(approach_name, training=False)
# Load CIFAR10 data
training_data = datasets.CIFAR10(root="data", train=True, download=True, transform=transform)
test_data = datasets.CIFAR10(root="data", train=False, download=True, transform=transform)
# Create dataloaders
batch_size = A05.get_batch_size(approach_name)
train_dataloader = DataLoader(training_data, batch_size=batch_size)
test_dataloader = DataLoader(test_data, batch_size=batch_size)
# Set number of classes
class_cnt = 10
# Create the model
model = A05.create_model(approach_name, class_cnt)
print("MODEL:", approach_name)
print(model)
# Move to GPU if possible
device = ("cuda" if torch.cuda.is_available()
else "mps" if torch.backends.mps.is_available()
else "cpu"
)
print(f"Using {device} device")
model = model.to(device)
# Load up previous weights
model_path = os.path.join(out_dir, "model_" + approach_name + ".pth")
model.load_state_dict(torch.load(model_path))
print("Model loaded from:", model_path)
approach_data[approach_name]["model"] = model
# Evaluate
train_eval_data = get_predictions_and_ground(model, train_dataloader, device)
print("Data acquired from training...")
test_eval_data = get_predictions_and_ground(model, test_dataloader, device)
print("Data acquired from testing...")
# Get metric values
model_metrics = {}
model_metrics["TRAINING"] = compute_metrics(**train_eval_data)
model_metrics["TESTING"] = compute_metrics(**test_eval_data)
# Store model metrics
approach_data[approach_name]["metrics"] = model_metrics
# Print and save metrics
print_results(approach_data)
if len(chosen_approach_names) == 1:
result_filename = chosen_approach_names[0] + "_RESULTS.txt"
else:
result_filename = "ALL_RESULTS.txt"
with open(out_dir + "/" + result_filename, "w") as f:
print_results(approach_data, stream=f)
if __name__ == "__main__":
main()