-
Notifications
You must be signed in to change notification settings - Fork 3
/
cnn_utils.py
44 lines (40 loc) · 1.57 KB
/
cnn_utils.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
import torch
import torch.cuda
def evaluate_single(input_individual, input_model, data_loader, device):
correct = 0
total = 0
input_model = input_model.eval()
input_model.set_individual(input_individual)
with torch.no_grad():
for data in data_loader:
images, labels = data
images = images.to(device)
labels = labels.to(device)
outputs = input_model(images)
_, predicted = torch.max(outputs[0].data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
return 100 * correct / total
def evaluate_individual_list(input_individual_list, ga, input_model, data_loader, device):
correct = 0
total = 0
input_model = input_model.eval()
i = 0
with torch.no_grad():
while len(input_individual_list) > i:
for data in data_loader:
if len(input_individual_list) <= i:
pass
else:
ind = input_individual_list[i]
input_model.set_individual(ind)
images, labels = data
images = images.to(device)
labels = labels.to(device)
outputs = input_model(images)
_, predicted = torch.max(outputs[0].data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
acc = 100 * correct / total
ga.update_current_individual_fitness(ind, acc)
i += 1