forked from qodatecnologia/Skin-cancer-Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
165 lines (130 loc) · 5.21 KB
/
app.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
import streamlit as st
import numpy as np
import pandas as pd
import keras
from keras.utils.np_utils import to_categorical
from keras.models import Sequential, load_model
from keras import backend as K
import os
import time
import io
from PIL import Image
import plotly.express as px
MODELSPATH = './models/'
DATAPATH = './data/'
def render_header():
st.write("""
<p align="center">
<H1> Skin cancer Analyzer
</p>
""", unsafe_allow_html=True)
@st.cache
def load_mekd():
img = Image.open(DATAPATH + '/ISIC_0024312.jpg')
return img
@st.cache
def data_gen(x):
img = np.asarray(Image.open(x).resize((100, 75)))
x_test = np.asarray(img.tolist())
x_test_mean = np.mean(x_test)
x_test_std = np.std(x_test)
x_test = (x_test - x_test_mean) / x_test_std
x_validate = x_test.reshape(1, 75, 100, 3)
return x_validate
@st.cache
def data_gen_(img):
img = img.reshape(100, 75)
x_test = np.asarray(img.tolist())
x_test_mean = np.mean(x_test)
x_test_std = np.std(x_test)
x_test = (x_test - x_test_mean) / x_test_std
x_validate = x_test.reshape(1, 75, 100, 3)
return x_validate
def load_models():
model = load_model(MODELSPATH + 'model.h5')
return model
@st.cache
def predict(x_test, model):
Y_pred = model.predict(x_test)
ynew = model.predict_proba(x_test)
K.clear_session()
ynew = np.round(ynew, 2)
ynew = ynew*100
y_new = ynew[0].tolist()
Y_pred_classes = np.argmax(Y_pred, axis=1)
K.clear_session()
return y_new, Y_pred_classes
@st.cache
def display_prediction(y_new):
"""Display image and preditions from model"""
result = pd.DataFrame({'Probability': y_new}, index=np.arange(7))
result = result.reset_index()
result.columns = ['Classes', 'Probability']
lesion_type_dict = {2: 'Benign keratosis-like lesions', 4: 'Melanocytic nevi', 3: 'Dermatofibroma',
5: 'Melanoma', 6: 'Vascular lesions', 1: 'Basal cell carcinoma', 0: 'Actinic keratoses'}
result["Classes"] = result["Classes"].map(lesion_type_dict)
return result
def main():
st.sidebar.header('Doctor Skin')
st.sidebar.subheader('Choose a page to proceed:')
page = st.sidebar.selectbox("", ["Sample Data", "Upload Your Image"])
if page == "Sample Data":
st.header("Sample Data Prediction for Skin Cancer")
st.markdown("""
**Now, this is probably why you came here. Let's get you some Predictions**
You need to choose Sample Data
""")
mov_base = ['Sample Data I']
movies_chosen = st.multiselect('Choose Sample Data', mov_base)
if len(movies_chosen) > 1:
st.error('Please select Sample Data')
if len(movies_chosen) == 1:
st.success("You have selected Sample Data")
else:
st.info('Please select Sample Data')
if len(movies_chosen) == 1:
if st.checkbox('Show Sample Data'):
st.info("Showing Sample data---->>>")
image = load_mekd()
st.image(image, caption='Sample Data', use_column_width=True)
st.subheader("Choose Training Algorithm!")
if st.checkbox('Keras'):
model = load_models()
st.success("Hooray !! Keras Model Loaded!")
if st.checkbox('Show Prediction Probablity on Sample Data'):
x_test = data_gen(DATAPATH + '/ISIC_0024312.jpg')
y_new, Y_pred_classes = predict(x_test, model)
result = display_prediction(y_new)
st.write(result)
if st.checkbox('Display Probability Graph'):
fig = px.bar(result, x="Classes",
y="Probability", color='Classes')
st.plotly_chart(fig, use_container_width=True)
if page == "Upload Your Image":
st.header("Upload Your Image")
file_path = st.file_uploader('Upload an image', type=['png', 'jpg'])
if file_path is not None:
x_test = data_gen(file_path)
image = Image.open(file_path)
img_array = np.array(image)
st.success('File Upload Success!!')
else:
st.info('Please upload Image file')
if st.checkbox('Show Uploaded Image'):
st.info("Showing Uploaded Image ---->>>")
st.image(img_array, caption='Uploaded Image',
use_column_width=True)
st.subheader("Choose Training Algorithm!")
if st.checkbox('Keras'):
model = load_models()
st.success("Hooray !! Keras Model Loaded!")
if st.checkbox('Show Prediction Probablity for Uploaded Image'):
y_new, Y_pred_classes = predict(x_test, model)
result = display_prediction(y_new)
st.write(result)
if st.checkbox('Display Probability Graph'):
fig = px.bar(result, x="Classes",
y="Probability", color='Classes')
st.plotly_chart(fig, use_container_width=True)
if __name__ == "__main__":
main()