-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification.py
183 lines (142 loc) · 6.76 KB
/
classification.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
import os
import time
import gdown
import numpy as np
import streamlit as st
import tensorflow as tf
from pathlib import Path
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
# Streamlit sharing is CPU only
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
@st.cache(show_spinner=False)
def download_weights(model_choice):
'''
Downloads model weights for deployment purposes.
'''
# Create directory
save_dest = Path('models')
save_dest.mkdir(exist_ok=True)
# Download weights for the chosen model
if model_choice == 'DenseNet201 (69%)':
url = 'https://drive.google.com/uc?id=1HfETcESJHpnEZ-x0dj2AZfqM3dCGs8EZ'
output = 'models/DenseNet201.h5'
if not Path(output).exists():
with st.spinner("Model weights were not found, downloading them. This may take a while."):
gdown.download(url, output, quiet=False)
elif model_choice == 'ResNet50 (79%)':
url = 'https://drive.google.com/uc?id=16-L3FaZUhWedQy2WagEfILv2-XwGazZO'
output = 'models/ResNet50.h5'
if not Path(output).exists():
with st.spinner("Model weights were not found, downloading them. This may take a while."):
gdown.download(url, output, quiet=False)
elif model_choice == 'InceptionV3 (84%)':
url = 'https://drive.google.com/uc?id=1wmNirs6NwvLEamvGSwLzRlJHLwHivPit'
output = 'models/Inceptionv3.h5'
if not Path(output).exists():
with st.spinner("Model weights were not found, downloading them. This may take a while."):
gdown.download(url, output, quiet=False)
elif model_choice == 'EfficientNetB3 (85%)':
url = 'https://drive.google.com/uc?id=1lebK-70tcon9hUWjfTm-sqsjqH2Ny83f'
output = 'models/EfficientNetB3.h5'
if not Path(output).exists():
with st.spinner("Model weights were not found, downloading them. This may take a while."):
gdown.download(url, output, quiet=False)
elif model_choice == 'InceptionResNetV2 (79%)':
url = 'https://drive.google.com/uc?id=14xPWqyeiz4S2XPiizEeDTTEQn5sSYBbE'
output = 'models/InceptionResNetv2.h5'
if not Path(output).exists():
with st.spinner("Model weights were not found, downloading them. This may take a while."):
gdown.download(url, output, quiet=False)
elif model_choice == 'NASNet (69%)':
url = 'https://drive.google.com/uc?id=1OGV-ZS2VtJJ_EK2oAB1IXp-jFqgWC1zU'
output = 'models/NasNetLarge.h5'
if not Path(output).exists():
with st.spinner("Model weights were not found, downloading them. This may take a while."):
gdown.download(url, output, quiet=False)
else:
raise ValueError('Unknown model: {}'.format(model_choice))
@st.cache(show_spinner=False)
def get_model(base_model, img_size, weights_file):
'''
Builds the final model to be used for predictions.
'''
# Define model architecture
base_pretrained_model = base_model(input_shape=(img_size, img_size, 3), include_top=False, weights=None)
model = base_pretrained_model.output
model = GlobalAveragePooling2D()(model)
model = Dense(256, activation='relu')(model)
model = Dropout(0.2)(model)
model = Dense(128, activation='relu')(model)
model = Dropout(0.1)(model)
predictions = Dense(7, activation='softmax')(model)
model = Model(inputs=base_pretrained_model.input, outputs=predictions)
# Load weights
model.load_weights(weights_file)
return model
def get_prediction(model, file, img_size, preprocessing_function):
'''
Obtains prediction for a specified file and model.
'''
# Load and process image into the suitable format
img = image.load_img(file, target_size=(img_size, img_size))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocessing_function(x)
x = x/255
# Make prediction
preds = model.predict(x)
return np.argmax(preds), preds
def image_classification(model_choice, file):
'''
Main function to classify a skin lesion, including confidence scores.
'''
# Download weights for the chosen model
download_weights(model_choice)
# Obtain prediction
if model_choice == 'DenseNet201 (69%)':
from tensorflow.keras.applications.densenet import DenseNet201 as PTModel, preprocess_input
img_size = 224
weights = 'models/DenseNet201.h5'
preprocess = preprocess_input
model = get_model(PTModel, img_size, weights)
label, scores = get_prediction(model, file, img_size, preprocessing_function=preprocess)
elif model_choice == 'ResNet50 (79%)':
from tensorflow.keras.applications.resnet import ResNet50 as PTModel, preprocess_input
img_size = 224
weights = 'models/ResNet50.h5'
preprocess = preprocess_input
model = get_model(PTModel, img_size, weights)
label, scores = get_prediction(model, file, img_size, preprocessing_function=preprocess)
elif model_choice == 'InceptionV3 (84%)':
from tensorflow.keras.applications.inception_v3 import InceptionV3 as PTModel, preprocess_input
img_size = 299
weights = 'models/Inceptionv3.h5'
preprocess = preprocess_input
model = get_model(PTModel, img_size, weights)
label, scores = get_prediction(model, file, img_size, preprocessing_function=preprocess)
elif model_choice == 'EfficientNetB3 (85%)':
from tensorflow.keras.applications.efficientnet import EfficientNetB3 as PTModel, preprocess_input
img_size = 224
weights = 'models/EfficientNetB3.h5'
preprocess = preprocess_input
model = get_model(PTModel, img_size, weights)
label, scores = get_prediction(model, file, img_size, preprocessing_function=preprocess)
elif model_choice == 'InceptionResNetV2 (79%)':
from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 as PTModel, preprocess_input
img_size = 299
weights = 'models/InceptionResNetv2.h5'
preprocess = preprocess_input
model = get_model(PTModel, img_size, weights)
label, scores = get_prediction(model, file, img_size, preprocessing_function=preprocess)
elif model_choice == 'NASNet (69%)':
from tensorflow.keras.applications.nasnet import NASNetLarge as PTModel, preprocess_input
img_size = 331
weights = 'models/NasNetLarge.h5'
preprocess = preprocess_input
model = get_model(PTModel, img_size, weights)
label, scores = get_prediction(model, file, img_size, preprocessing_function=preprocess)
else:
raise ValueError('Unknown model: {}'.format(model_choice))
return label, scores