-
Notifications
You must be signed in to change notification settings - Fork 1
/
hybrid_model_predict.py
129 lines (100 loc) · 3.86 KB
/
hybrid_model_predict.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
# Imports
import numpy as np
import pickle as pkl
import sys
import os
from matplotlib import pyplot as plt
import cv2
import time
# Scipy Imports
import scipy
import scipy.interpolate as interpolate
import scipy.ndimage.morphology as morpho
# Shapely Geometry Imports
from shapely.geometry import LineString, Point
from shapely.geometry import Polygon
# Skimage Imports
import skimage
import skimage.filters as filters
# Sklearn Imports
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
# Hybrid Model Imports
from code.hybrid_model.hybrid_model_utilities import *
# GLOBAL VARIABLES
# FOLDS
FOLDS = [i for i in range(5)]
# Data Paths
data_path = 'data/resized'
# Train and Test Indices
train_indices_list_path = 'data/train-test-indices/train_indices_list.pickle'
test_indices_list_path = 'data/train-test-indices/test_indices_list.pickle'
# ISBI Model Predictions
isbi_model_predictions_path = 'results/isbi-model/predictions'
# Open y data
y_train_root = os.path.join(data_path, 'y_train_221.pickle')
y_test_root = os.path.join(data_path, 'y_test_221.pickle')
# Open X data
X_train_root = os.path.join(data_path, 'X_train_221.pickle')
X_test_root = os.path.join(data_path, 'X_test_221.pickle')
# Results folder(s)
# Main results directory
results_directory = 'results'
# Check it exists or create it
if os.path.isdir(results_directory) == False:
os.mkdir(results_directory)
# Model results directory
model_results_directory = os.path.join(results_directory, 'hybrid-model')
# Check if it exists or create it
if os.path.isdir(model_results_directory) == False:
os.mkdir(model_results_directory)
# Breast contour priors directory
breast_prior_contour_directory = os.path.join(model_results_directory, 'breast-contour-priors')
# Check if it exists or create it
if os.path.isdir(breast_prior_contour_directory) == False:
os.mkdir(breast_prior_contour_directory)
# Hybrid model predictions directory
hybrid_predictions_directory = os.path.join(model_results_directory, 'predictions')
# Check if it exists or create it
if os.path.isdir(hybrid_predictions_directory) == False:
os.mkdir(hybrid_predictions_directory)
# Raw predictions directory
raw_predictions_directory = os.path.join(hybrid_predictions_directory, 'raw')
# Check if it exists or create it
if os.path.isdir(raw_predictions_directory) == False:
os.mkdir(raw_predictions_directory)
# Iterate through FOLDS
for fold in FOLDS:
print("Current fold: {}".format(fold))
# Generate Breast Contour Priors
save_breast_contour_params(
y_train_path=y_train_root,
y_test_path=y_test_root,
fold=fold, train_indices_list_path=train_indices_list_path,
breast_prior_save_path=breast_prior_contour_directory
)
print('Breast contour priors saved for fold: {}'.format(fold))
# Start counting time to check algorithm duration
startTime = time.time()
# Open the breast contour priors for this fold
breast_prior_root = os.path.join(breast_prior_contour_directory, 'breast_contour_prior_CV_Fold_{}.pkl'.format(fold))
# Open ISBI Model predictions
with open(os.path.join(isbi_model_predictions_path, 'isbi_preds_w_only_CV_Fold_{}.pickle'.format(fold)), 'rb') as f:
isbi = pkl.load(f)
# Convert to Numpy array
isbi = np.array(isbi)
# ISBI Model predictions are normalized by width, so we need to "denormalize" them
isbi *= 512
# Obtain Hybrid Model Predictions
generate_hybrid_predictions(
X_train_path=X_train_root,
X_test_path=X_test_root,
test_indices_list_path=test_indices_list_path,
fold=fold,
keypoints=isbi,
breast_prior_path=breast_prior_root,
result_path=raw_predictions_directory)
# Compute time per fold
print('The script took {} seconds for fold {}!'.format(time.time() - startTime, fold))
# Finish statement
print('Hybrid Model Predictions Finished.')