Upload test.ipynb
Browse files- test.ipynb +134 -0
test.ipynb
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 6,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import tensorflow as tf\n",
|
10 |
+
"from PIL import Image\n",
|
11 |
+
"import numpy as np\n",
|
12 |
+
"import gradio as gr"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": 7,
|
18 |
+
"metadata": {},
|
19 |
+
"outputs": [],
|
20 |
+
"source": [
|
21 |
+
"model = tf.keras.models.load_model('./Trained_Model.keras')"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "code",
|
26 |
+
"execution_count": 8,
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"classes = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor'] \n",
|
31 |
+
"\n",
|
32 |
+
"def preprocess_image(image_path):\n",
|
33 |
+
" img = Image.open(image_path).convert('RGB') \n",
|
34 |
+
" img_array = img.resize((128, 128)) \n",
|
35 |
+
" return np.expand_dims(img_array, axis=0)"
|
36 |
+
]
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"cell_type": "code",
|
40 |
+
"execution_count": 9,
|
41 |
+
"metadata": {},
|
42 |
+
"outputs": [],
|
43 |
+
"source": [
|
44 |
+
"def predict_gradio(image):\n",
|
45 |
+
" img_array = preprocess_image(image)\n",
|
46 |
+
" predictions = model.predict(img_array)\n",
|
47 |
+
" predicted_class = np.argmax(predictions, axis=1)[0]\n",
|
48 |
+
" return classes[predicted_class]"
|
49 |
+
]
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"cell_type": "code",
|
53 |
+
"execution_count": null,
|
54 |
+
"metadata": {},
|
55 |
+
"outputs": [
|
56 |
+
{
|
57 |
+
"name": "stdout",
|
58 |
+
"output_type": "stream",
|
59 |
+
"text": [
|
60 |
+
"* Running on local URL: http://127.0.0.1:7864\n",
|
61 |
+
"\n",
|
62 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"data": {
|
67 |
+
"text/html": [
|
68 |
+
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
69 |
+
],
|
70 |
+
"text/plain": [
|
71 |
+
"<IPython.core.display.HTML object>"
|
72 |
+
]
|
73 |
+
},
|
74 |
+
"metadata": {},
|
75 |
+
"output_type": "display_data"
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"data": {
|
79 |
+
"text/plain": []
|
80 |
+
},
|
81 |
+
"execution_count": 21,
|
82 |
+
"metadata": {},
|
83 |
+
"output_type": "execute_result"
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"name": "stdout",
|
87 |
+
"output_type": "stream",
|
88 |
+
"text": [
|
89 |
+
"\u001b[1m1/1\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 465ms/step\n"
|
90 |
+
]
|
91 |
+
}
|
92 |
+
],
|
93 |
+
"source": [
|
94 |
+
"interface = gr.Interface(\n",
|
95 |
+
" fn=predict_gradio,\n",
|
96 |
+
" inputs=gr.Image(type=\"filepath\"),\n",
|
97 |
+
" outputs=\"text\",\n",
|
98 |
+
" title=\"Brain Tumor Prediction\",\n",
|
99 |
+
" description=\"Upload an MRI image, and the model will predict the class.\"\n",
|
100 |
+
")\n",
|
101 |
+
"\n",
|
102 |
+
"interface.launch(server_port=7864)"
|
103 |
+
]
|
104 |
+
},
|
105 |
+
{
|
106 |
+
"cell_type": "code",
|
107 |
+
"execution_count": null,
|
108 |
+
"metadata": {},
|
109 |
+
"outputs": [],
|
110 |
+
"source": []
|
111 |
+
}
|
112 |
+
],
|
113 |
+
"metadata": {
|
114 |
+
"kernelspec": {
|
115 |
+
"display_name": "anway",
|
116 |
+
"language": "python",
|
117 |
+
"name": "python3"
|
118 |
+
},
|
119 |
+
"language_info": {
|
120 |
+
"codemirror_mode": {
|
121 |
+
"name": "ipython",
|
122 |
+
"version": 3
|
123 |
+
},
|
124 |
+
"file_extension": ".py",
|
125 |
+
"mimetype": "text/x-python",
|
126 |
+
"name": "python",
|
127 |
+
"nbconvert_exporter": "python",
|
128 |
+
"pygments_lexer": "ipython3",
|
129 |
+
"version": "3.10.15"
|
130 |
+
}
|
131 |
+
},
|
132 |
+
"nbformat": 4,
|
133 |
+
"nbformat_minor": 2
|
134 |
+
}
|