|
a |
|
b/BleedingImageClassification.ipynb |
|
|
1 |
{ |
|
|
2 |
"nbformat": 4, |
|
|
3 |
"nbformat_minor": 0, |
|
|
4 |
"metadata": { |
|
|
5 |
"colab": { |
|
|
6 |
"name": "BleedingImageClassification.ipynb", |
|
|
7 |
"provenance": [], |
|
|
8 |
"collapsed_sections": [] |
|
|
9 |
}, |
|
|
10 |
"kernelspec": { |
|
|
11 |
"name": "python3", |
|
|
12 |
"display_name": "Python 3" |
|
|
13 |
}, |
|
|
14 |
"language_info": { |
|
|
15 |
"name": "python" |
|
|
16 |
} |
|
|
17 |
}, |
|
|
18 |
"cells": [ |
|
|
19 |
{ |
|
|
20 |
"cell_type": "code", |
|
|
21 |
"metadata": { |
|
|
22 |
"id": "tGZBwBkjWevk" |
|
|
23 |
}, |
|
|
24 |
"source": [ |
|
|
25 |
"!pip install tensorflow\n", |
|
|
26 |
"!pip install opencv-python\n", |
|
|
27 |
"!pip install numpy" |
|
|
28 |
], |
|
|
29 |
"execution_count": null, |
|
|
30 |
"outputs": [] |
|
|
31 |
}, |
|
|
32 |
{ |
|
|
33 |
"cell_type": "code", |
|
|
34 |
"metadata": { |
|
|
35 |
"id": "L7taERIgXXH6" |
|
|
36 |
}, |
|
|
37 |
"source": [ |
|
|
38 |
"import tensorflow.keras\n", |
|
|
39 |
"import numpy as np\n", |
|
|
40 |
"import cv2\n", |
|
|
41 |
"import os\n", |
|
|
42 |
"np.set_printoptions(suppress=True)" |
|
|
43 |
], |
|
|
44 |
"execution_count": null, |
|
|
45 |
"outputs": [] |
|
|
46 |
}, |
|
|
47 |
{ |
|
|
48 |
"cell_type": "code", |
|
|
49 |
"metadata": { |
|
|
50 |
"colab": { |
|
|
51 |
"base_uri": "https://localhost:8080/" |
|
|
52 |
}, |
|
|
53 |
"id": "87r5fH7FaDta", |
|
|
54 |
"outputId": "95ace021-e41f-4eeb-db35-ae25082e64fe" |
|
|
55 |
}, |
|
|
56 |
"source": [ |
|
|
57 |
"model = tensorflow.keras.models.load_model('keras_model.h5')" |
|
|
58 |
], |
|
|
59 |
"execution_count": null, |
|
|
60 |
"outputs": [ |
|
|
61 |
{ |
|
|
62 |
"output_type": "stream", |
|
|
63 |
"text": [ |
|
|
64 |
"WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.\n" |
|
|
65 |
], |
|
|
66 |
"name": "stdout" |
|
|
67 |
} |
|
|
68 |
] |
|
|
69 |
}, |
|
|
70 |
{ |
|
|
71 |
"cell_type": "code", |
|
|
72 |
"metadata": { |
|
|
73 |
"id": "0wGpjGSBa0Ga" |
|
|
74 |
}, |
|
|
75 |
"source": [ |
|
|
76 |
"data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)" |
|
|
77 |
], |
|
|
78 |
"execution_count": null, |
|
|
79 |
"outputs": [] |
|
|
80 |
}, |
|
|
81 |
{ |
|
|
82 |
"cell_type": "code", |
|
|
83 |
"metadata": { |
|
|
84 |
"id": "YsDvM6oga0Rp" |
|
|
85 |
}, |
|
|
86 |
"source": [ |
|
|
87 |
"image = cv2.imread('/content/Capture.jpg')\n", |
|
|
88 |
"\n", |
|
|
89 |
"#resizing the image to be at least 224x224 and then cropping from the center\n", |
|
|
90 |
"size = (224, 224)\n", |
|
|
91 |
"\n", |
|
|
92 |
"image = cv2.resize(image, size, fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)\n", |
|
|
93 |
"#turn the image into a numpy array\n", |
|
|
94 |
"image_array = np.asarray(image)" |
|
|
95 |
], |
|
|
96 |
"execution_count": null, |
|
|
97 |
"outputs": [] |
|
|
98 |
}, |
|
|
99 |
{ |
|
|
100 |
"cell_type": "code", |
|
|
101 |
"metadata": { |
|
|
102 |
"id": "oP537pQFa9Sq" |
|
|
103 |
}, |
|
|
104 |
"source": [ |
|
|
105 |
"normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1" |
|
|
106 |
], |
|
|
107 |
"execution_count": null, |
|
|
108 |
"outputs": [] |
|
|
109 |
}, |
|
|
110 |
{ |
|
|
111 |
"cell_type": "code", |
|
|
112 |
"metadata": { |
|
|
113 |
"id": "d6V-tVTha-dS" |
|
|
114 |
}, |
|
|
115 |
"source": [ |
|
|
116 |
"data[0] = normalized_image_array" |
|
|
117 |
], |
|
|
118 |
"execution_count": null, |
|
|
119 |
"outputs": [] |
|
|
120 |
}, |
|
|
121 |
{ |
|
|
122 |
"cell_type": "code", |
|
|
123 |
"metadata": { |
|
|
124 |
"colab": { |
|
|
125 |
"base_uri": "https://localhost:8080/" |
|
|
126 |
}, |
|
|
127 |
"id": "LWzMOv2pbBPC", |
|
|
128 |
"outputId": "7f04c9f2-6785-4158-e0cc-2f1856a9a6bb" |
|
|
129 |
}, |
|
|
130 |
"source": [ |
|
|
131 |
"prediction = model.predict(data)\n", |
|
|
132 |
"print(prediction)" |
|
|
133 |
], |
|
|
134 |
"execution_count": null, |
|
|
135 |
"outputs": [ |
|
|
136 |
{ |
|
|
137 |
"output_type": "stream", |
|
|
138 |
"text": [ |
|
|
139 |
"[[0.00362582 0.9963742 ]]\n" |
|
|
140 |
], |
|
|
141 |
"name": "stdout" |
|
|
142 |
} |
|
|
143 |
] |
|
|
144 |
}, |
|
|
145 |
{ |
|
|
146 |
"cell_type": "code", |
|
|
147 |
"metadata": { |
|
|
148 |
"colab": { |
|
|
149 |
"base_uri": "https://localhost:8080/" |
|
|
150 |
}, |
|
|
151 |
"id": "tuV4H65bZtT6", |
|
|
152 |
"outputId": "73aafe0a-ffb6-48cb-f099-d34e6d8cca7a" |
|
|
153 |
}, |
|
|
154 |
"source": [ |
|
|
155 |
"if prediction[0][0] > 0.7 and prediction[0][0] > prediction[0][1]:\n", |
|
|
156 |
" print(\"Bleeding Brain\")\n", |
|
|
157 |
"else:\n", |
|
|
158 |
" print(\"Non Bleeding Brain\")" |
|
|
159 |
], |
|
|
160 |
"execution_count": null, |
|
|
161 |
"outputs": [ |
|
|
162 |
{ |
|
|
163 |
"output_type": "stream", |
|
|
164 |
"text": [ |
|
|
165 |
"Non Bleeding Brain\n" |
|
|
166 |
], |
|
|
167 |
"name": "stdout" |
|
|
168 |
} |
|
|
169 |
] |
|
|
170 |
}, |
|
|
171 |
{ |
|
|
172 |
"cell_type": "code", |
|
|
173 |
"metadata": { |
|
|
174 |
"id": "b74k8FzlbL8h" |
|
|
175 |
}, |
|
|
176 |
"source": [ |
|
|
177 |
"" |
|
|
178 |
], |
|
|
179 |
"execution_count": null, |
|
|
180 |
"outputs": [] |
|
|
181 |
} |
|
|
182 |
] |
|
|
183 |
} |