-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_utils.py
89 lines (81 loc) · 3.17 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
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
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, num):
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, num)
i += 1
def evaluate_parents_individual_list(input_individual_list, ga, input_model, data_loader, device, num):
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_max_individual_fitness(ind, acc, num)
i += 1
def uptate_parents_individual_list(input_individual_list, ga, num): # Set 0
i = 0
while len(input_individual_list) > i:
ind = input_individual_list[i]
if num == 1:
ga.max_dict_1.update_2({ind: 0})
elif num == 2:
ga.max_dict_2.update_2({ind: 0})
i += 1
def uptate_children_individual_list(input_individual_list, ga, num): # Set 0
i = 0
while len(input_individual_list) > i:
ind = input_individual_list[i]
if num == 1:
ga.current_dict_1.update({ind: 0})
elif num == 2:
ga.current_dict_2.update({ind: 0})
i += 1