Spaces:
Sleeping
Sleeping
galihsukmana
commited on
Commit
•
299b96a
1
Parent(s):
2e73d1c
Upload 4 files
Browse files- app.py +40 -0
- cnn_model.h5 +3 -0
- h8dsft_P2M2_galihs.ipynb +0 -0
- model_inferences.ipynb +156 -0
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow import keras
|
5 |
+
import tensorflow as tf
|
6 |
+
from keras.models import load_model
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
st.title('Cancer Type Detection')
|
11 |
+
|
12 |
+
# Load the saved model outside the prediction function
|
13 |
+
loaded_model = load_model('cnn_model.h5')
|
14 |
+
|
15 |
+
def prediction(file):
|
16 |
+
img = tf.keras.utils.load_img(file, target_size=(224, 224))
|
17 |
+
x = tf.keras.utils.img_to_array(img)
|
18 |
+
x = np.expand_dims(x, axis=0)
|
19 |
+
|
20 |
+
# Predict the class probabilities
|
21 |
+
classes = loaded_model.predict(x)
|
22 |
+
|
23 |
+
# Get the predicted class label
|
24 |
+
classes = np.ravel(classes) # convert to 1D array
|
25 |
+
idx = np.argmax(classes)
|
26 |
+
clas = ['adenocarcinoma', 'large.cell.carcinoma', 'normal', 'squamous.cell.carcinoma'][idx]
|
27 |
+
|
28 |
+
return clas
|
29 |
+
|
30 |
+
uploaded_file = st.file_uploader("Choose MRI file")
|
31 |
+
if uploaded_file is not None:
|
32 |
+
image = Image.open(uploaded_file)
|
33 |
+
image = image.resize((224, 224))
|
34 |
+
image = tf.keras.preprocessing.image.img_to_array(image)
|
35 |
+
image = image / 255.0
|
36 |
+
image = tf.expand_dims(image, axis=0)
|
37 |
+
|
38 |
+
if st.button('Predict'):
|
39 |
+
result = prediction(uploaded_file)
|
40 |
+
st.write('Prediction is {}'.format(result))
|
cnn_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e24965794f88afb83c503084b57f6cdf7615b833f29ed5776ffac9d9137f46be
|
3 |
+
size 89306144
|
h8dsft_P2M2_galihs.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
model_inferences.ipynb
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"attachments": {},
|
5 |
+
"cell_type": "markdown",
|
6 |
+
"metadata": {
|
7 |
+
"id": "FjDGwlCJYO2m"
|
8 |
+
},
|
9 |
+
"source": [
|
10 |
+
"### Model Inferences"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": 3,
|
16 |
+
"metadata": {
|
17 |
+
"id": "DUXRPvLRxpJe"
|
18 |
+
},
|
19 |
+
"outputs": [],
|
20 |
+
"source": [
|
21 |
+
"import pandas as pd \n",
|
22 |
+
"import numpy as np\n",
|
23 |
+
"from tensorflow import keras\n",
|
24 |
+
"import tensorflow as tf\n",
|
25 |
+
"import pickle\n",
|
26 |
+
"from keras.models import load_model\n",
|
27 |
+
"from tensorflow.keras.preprocessing import image \n",
|
28 |
+
"import matplotlib.pyplot as plt"
|
29 |
+
]
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"cell_type": "code",
|
33 |
+
"execution_count": 2,
|
34 |
+
"metadata": {
|
35 |
+
"colab": {
|
36 |
+
"base_uri": "https://localhost:8080/"
|
37 |
+
},
|
38 |
+
"id": "WOLLnPnAuzKm",
|
39 |
+
"outputId": "644f852b-b695-40d8-db1c-3a8e502ca316"
|
40 |
+
},
|
41 |
+
"outputs": [
|
42 |
+
{
|
43 |
+
"name": "stdout",
|
44 |
+
"output_type": "stream",
|
45 |
+
"text": [
|
46 |
+
"Mounted at /content/drive\n"
|
47 |
+
]
|
48 |
+
}
|
49 |
+
],
|
50 |
+
"source": [
|
51 |
+
"from google.colab import drive\n",
|
52 |
+
"drive.mount('/content/drive')"
|
53 |
+
]
|
54 |
+
},
|
55 |
+
{
|
56 |
+
"cell_type": "code",
|
57 |
+
"execution_count": 4,
|
58 |
+
"metadata": {
|
59 |
+
"colab": {
|
60 |
+
"base_uri": "https://localhost:8080/"
|
61 |
+
},
|
62 |
+
"id": "9nTgKQUKu61s",
|
63 |
+
"outputId": "3105e278-99eb-468d-8890-4079916391b2"
|
64 |
+
},
|
65 |
+
"outputs": [
|
66 |
+
{
|
67 |
+
"name": "stdout",
|
68 |
+
"output_type": "stream",
|
69 |
+
"text": [
|
70 |
+
"/content/drive/MyDrive/cnn_model\n"
|
71 |
+
]
|
72 |
+
}
|
73 |
+
],
|
74 |
+
"source": [
|
75 |
+
"%cd /content/drive/MyDrive/cnn_model"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": 7,
|
81 |
+
"metadata": {
|
82 |
+
"id": "ij3PCeoCyU23"
|
83 |
+
},
|
84 |
+
"outputs": [],
|
85 |
+
"source": [
|
86 |
+
"def prediction(file):\n",
|
87 |
+
" img = tf.keras.utils.load_img(file, target_size=(224, 224))\n",
|
88 |
+
" x = tf.keras.utils.img_to_array(img)\n",
|
89 |
+
" x = np.expand_dims(x, axis=0)\n",
|
90 |
+
"\n",
|
91 |
+
" # Load the saved model\n",
|
92 |
+
" loaded_model = load_model('cnn_model.h5')\n",
|
93 |
+
"\n",
|
94 |
+
" # Predict the class probabilities\n",
|
95 |
+
" classes = loaded_model.predict(x)\n",
|
96 |
+
"\n",
|
97 |
+
" # Get the predicted class label\n",
|
98 |
+
" classes = np.ravel(classes) # convert to 1D array\n",
|
99 |
+
" idx = np.argmax(classes)\n",
|
100 |
+
" clas = ['adenocarcinoma', 'large.cell.carcinoma', 'normal', 'squamous.cell.carcinoma']\n",
|
101 |
+
"\n",
|
102 |
+
" # Print the predicted class label\n",
|
103 |
+
" print('Prediction is a {}'.format(clas[idx]))"
|
104 |
+
]
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "code",
|
108 |
+
"execution_count": 8,
|
109 |
+
"metadata": {
|
110 |
+
"colab": {
|
111 |
+
"base_uri": "https://localhost:8080/"
|
112 |
+
},
|
113 |
+
"id": "h4xpRFtUw-OR",
|
114 |
+
"outputId": "6af2e2d1-3f78-4a9a-a7b2-b8e83db72e6b"
|
115 |
+
},
|
116 |
+
"outputs": [
|
117 |
+
{
|
118 |
+
"name": "stdout",
|
119 |
+
"output_type": "stream",
|
120 |
+
"text": [
|
121 |
+
"1/1 [==============================] - 2s 2s/step\n",
|
122 |
+
"Prediction is a normal\n"
|
123 |
+
]
|
124 |
+
}
|
125 |
+
],
|
126 |
+
"source": [
|
127 |
+
"prediction('Adenocarcinoma-in-situ-Axial-contrast-enhanced-chest-CT-scan-with-lung-window.png')"
|
128 |
+
]
|
129 |
+
},
|
130 |
+
{
|
131 |
+
"attachments": {},
|
132 |
+
"cell_type": "markdown",
|
133 |
+
"metadata": {
|
134 |
+
"id": "VG4syGUcYjSK"
|
135 |
+
},
|
136 |
+
"source": [
|
137 |
+
"Prediction wrong, the model should maintained for next utilization"
|
138 |
+
]
|
139 |
+
}
|
140 |
+
],
|
141 |
+
"metadata": {
|
142 |
+
"colab": {
|
143 |
+
"provenance": []
|
144 |
+
},
|
145 |
+
"kernelspec": {
|
146 |
+
"display_name": "Python 3",
|
147 |
+
"name": "python3"
|
148 |
+
},
|
149 |
+
"language_info": {
|
150 |
+
"name": "python",
|
151 |
+
"version": "3.7.16"
|
152 |
+
}
|
153 |
+
},
|
154 |
+
"nbformat": 4,
|
155 |
+
"nbformat_minor": 0
|
156 |
+
}
|