-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
208 lines (146 loc) · 7.42 KB
/
evaluate.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 26 19:01:20 2021
@author: baron015
"""
import torch_geometric
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import torch.nn as nn
import torch
from torch.utils.data import DataLoader
from sklearn import metrics
import datetime
import dataset_graphs
# importing useful functions
import dataset
import utils as functions
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
validation = True
now = datetime.datetime.now()
current_time = now.strftime("%Y_%m_%d_%H_%M")
def validation(model: nn.Module, dataset: torch.utils.data.dataset, description_model: str,
folder_results: str) -> None:
model = model.eval()
prediction, y_data, binary_prediction = predict_on_dataset(model, dataset)
auc = metrics.roc_auc_score(y_data, prediction)
report = accuracy_metrics_report(y_data, binary_prediction)
learning_rate = model.learning_rate
results_to_save = f" learning rate= {learning_rate}\n\n" + description_model + f"\n\n auc= {auc}"
results_to_save += f"\n\n {report}"
file_results = f"{folder_results}results_model_{current_time}.txt"
functions.write_text_file(path=file_results, text=results_to_save)
def predict_on_dataset(model: nn.Module, dataset: torch.utils.data.dataset):
loader = DataLoader(dataset, batch_size=2000, shuffle=True)
prediction_total = []
y_data_total = []
binary_prediction_total = []
for data in loader:
x_data, y_data, coordinates_samples = data
y_data = y_data.float()
if device.type == "cuda":
x_data = x_data.cuda().float()
y_data = y_data.cuda().float()
elif device.type == "cpu":
x_data = x_data.float()
y_data = y_data.float()
prediction = model(x_data)
prediction = nn.Sigmoid()(prediction)
prediction = torch.flatten(prediction)
y_data = torch.flatten(y_data)
prediction = functions.numpy_raster(prediction)
y_data = functions.numpy_raster(y_data)
binary_prediction = functions.convert_to_binary(prediction, 0.5)
prediction_total.append(prediction)
y_data_total.append(y_data)
binary_prediction_total.append(binary_prediction)
prediction_total = np.concatenate(prediction_total)
y_data_total = np.concatenate(y_data_total)
binary_prediction_total = np.concatenate(binary_prediction_total)
return prediction_total, y_data_total, binary_prediction_total
def validation_graph(model: nn.Module, dataset: torch.utils.data.dataset, description_model: str,
folder_results: str) -> None:
model = model.eval()
from torch_geometric.data import DataLoader
loader = DataLoader(dataset, batch_size=2000, shuffle=True)
image_prediction, mask, binary_prediction = predict_on_graph_dataset(model, loader)
auc = metrics.roc_auc_score(mask, image_prediction)
report = accuracy_metrics_report(mask, binary_prediction)
learning_rate = model.learning_rate
results_to_save = f" learning rate= {learning_rate}\n\n" + description_model + f"\n\n auc= {auc}"
results_to_save += f"\n\n {report}"
file_results = f"{folder_results}results_model_{current_time}.txt"
functions.write_text_file(path=file_results, text=results_to_save)
def predict_on_graph_dataset(model: nn.Module, dataset: torch_geometric.data.DataLoader):
prediction_total = []
y_data_total = []
binary_prediction_total = []
for data in dataset:
graph, mask, segmentation_map, coordinate = data
graph = graph.to(device)
segmentation_map = segmentation_map.flatten()
with torch.no_grad():
prediction = model.predict(graph)
prediction = prediction.flatten()
image_prediction = functions.graph_labels_to_image(functions.numpy_raster(prediction),
functions.numpy_raster(segmentation_map))
image_prediction = image_prediction.flatten()
mask = mask.flatten()
binary_prediction = functions.convert_to_binary(image_prediction, 0.5)
y_data_total.append(mask)
prediction_total.append(image_prediction)
binary_prediction_total.append(binary_prediction)
prediction_total = np.concatenate(prediction_total)
y_data_total = np.concatenate(y_data_total)
binary_prediction_total = np.concatenate(binary_prediction_total)
return prediction_total, y_data_total, binary_prediction_total
def accuracy_slic_segmentation():
years = [2019, 2014, 2011]
folder_graphs, folders_labels, folder_semantic_maps = dataset_graphs.get_data_folders(years,
rasters_with_positives_only=True)
dataset_full = dataset_graphs.merge_datasets(folders_labels, folder_graphs, folder_semantic_maps)
number_samples = dataset_full.__len__()
accuracy_total = 0.0
for i_sample in range(number_samples):
sample = dataset_full.__getitem__(i_sample)
graph, mask_tensor, segmentation_map_tensor, _ = sample
nodes_labels = graph.y
mask_tensor = functions.numpy_raster(mask_tensor)
graph_mask = functions.graph_labels_to_image(nodes_labels, segmentation_map_tensor)
number_pixels = graph_mask.shape[0] ** 2
node_labels_are_correct = mask_tensor == graph_mask
accuracy = functions.count_value(node_labels_are_correct, True) / number_pixels
accuracy_total += accuracy
accuracy_total /= number_samples
return accuracy_total
def rf_accuracy_estimation(x_train: np.array, y_train: np.array, x_test: np.array, y_test: np.array,
description_model: str, folder_results: str,
perform_cross_validation=False):
classifier = RandomForestClassifier(max_depth=3, random_state=0, n_estimators=20)
classifier.fit(x_train, y_train)
prediction_label = classifier.predict(x_test)
accuracy_metrics_report(y_test, prediction_label)
report = accuracy_metrics_report(y_test, prediction_label)
if perform_cross_validation:
cross_validation_report(classifier, x_test, y_test)
file_results = f"{folder_results}results_model_{current_time}.txt"
functions.write_text_file(path=file_results, text=report)
return report
def cross_validation_report(model, x_test: np.array, y_test: np.array):
cross_validation_parameters = KFold(n_splits=10, random_state=1, shuffle=True)
scores_cv = cross_val_score(model, x_test, y_test,
cv=cross_validation_parameters, scoring='f1_macro')
print("Score cross-validation is:")
print(scores_cv)
def accuracy_metrics_report(y_labels: np.array, y_prediction: np.array):
report = ""
conf_mat = confusion_matrix(y_labels, y_prediction)
class_report = classification_report(y_labels, y_prediction)
report += f"\n\nConfusion matrix \n\n TN - FN \n\n FP - TP \n\n {conf_mat}"
report += f"\n\n {class_report}"
return report