yolac commited on
Commit
2413a8b
·
verified ·
1 Parent(s): b578752

Upload BacterialMorphologyClassification_model.ipynb

Browse files
BacterialMorphologyClassification_model.ipynb ADDED
@@ -0,0 +1,433 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4"
8
+ },
9
+ "kernelspec": {
10
+ "name": "python3",
11
+ "display_name": "Python 3"
12
+ },
13
+ "language_info": {
14
+ "name": "python"
15
+ },
16
+ "accelerator": "GPU"
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "code",
21
+ "execution_count": null,
22
+ "metadata": {
23
+ "id": "AV-1n4EQ4zoM"
24
+ },
25
+ "outputs": [],
26
+ "source": [
27
+ "import tensorflow as tf\n",
28
+ "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
29
+ "import pandas as pd\n",
30
+ "import os"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "source": [
36
+ "#Create an instance of ImageDataGenerator\n",
37
+ "train_datagen = ImageDataGenerator(rescale=1./255)\n",
38
+ "val_datagen = ImageDataGenerator(rescale=1./255)\n",
39
+ "class_labels = {'cocci': 0, 'bacilli': 1, 'spirilla': 2}\n",
40
+ "\n",
41
+ "# Load training data from the 'train' folder\n",
42
+ "# Each subfolder (bacilli, cocci, spirilla) represents a class\n",
43
+ "train_data = train_datagen.flow_from_directory(\n",
44
+ " '/content/drive/MyDrive/Bacterial Classification/train', # Path to the train folder\n",
45
+ " target_size=(224, 224), # Resize all images to 224x224\n",
46
+ " batch_size=32, # Number of images per batch\n",
47
+ " class_mode='categorical', # Multi-class classification\n",
48
+ " classes=class_labels # Explicit class mapping\n",
49
+ "\n",
50
+ ")\n",
51
+ "\n",
52
+ "# Load validation data from the 'validation' folder\n",
53
+ "# Each subfolder (bacilli, cocci, spirilla) represents a class\n",
54
+ "val_data = val_datagen.flow_from_directory(\n",
55
+ " '/content/drive/MyDrive/Bacterial Classification/validation',# Path to the validation folder\n",
56
+ " target_size=(224, 224),\n",
57
+ " batch_size=32,\n",
58
+ " class_mode='categorical',\n",
59
+ " classes=class_labels\n",
60
+ "\n",
61
+ ")\n",
62
+ "\n",
63
+ "# Check class mappings\n",
64
+ "print(\"Training Class Indices:\", train_data.class_indices)\n",
65
+ "print(\"Validation Class Indices:\", val_data.class_indices)\n"
66
+ ],
67
+ "metadata": {
68
+ "id": "JoFVIVmJTVPX"
69
+ },
70
+ "execution_count": null,
71
+ "outputs": []
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "source": [
76
+ "from tensorflow.keras.applications import MobileNetV2\n",
77
+ "from tensorflow.keras.layers import GlobalAveragePooling2D\n",
78
+ "from tensorflow.keras.optimizers import Adam\n",
79
+ "from tensorflow.keras.callbacks import EarlyStopping\n",
80
+ "\n",
81
+ "base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n",
82
+ "base_model.trainable = False # Freeze the base model\n",
83
+ "\n",
84
+ "model = tf.keras.Sequential([\n",
85
+ " base_model,\n",
86
+ " GlobalAveragePooling2D(),\n",
87
+ " tf.keras.layers.Dense(128, activation='relu'),\n",
88
+ " tf.keras.layers.Dropout(0.5),\n",
89
+ " tf.keras.layers.Dense(3, activation='softmax')\n",
90
+ "])\n",
91
+ "\n",
92
+ "model.compile(\n",
93
+ " optimizer=Adam(learning_rate=0.0001), # Lower learning rate\n",
94
+ " loss='categorical_crossentropy',\n",
95
+ " metrics=['accuracy']\n",
96
+ ")\n",
97
+ "early_stopping = EarlyStopping(\n",
98
+ " monitor='val_loss',\n",
99
+ " patience=3,\n",
100
+ " restore_best_weights=True\n",
101
+ ")\n",
102
+ "# Train the model\n",
103
+ "history = model.fit(\n",
104
+ " train_data,\n",
105
+ " validation_data=val_data,\n",
106
+ " epochs=50, # Allow more epochs but stop early if needed\n",
107
+ " callbacks=[early_stopping]\n",
108
+ ")\n",
109
+ "\n",
110
+ "\n",
111
+ "# Evaluate the model on the validation dataset\n",
112
+ "val_loss, val_accuracy = model.evaluate(val_data)\n",
113
+ "print(f\"Validation Loss: {val_loss}\")\n",
114
+ "print(f\"Validation Accuracy: {val_accuracy}\")"
115
+ ],
116
+ "metadata": {
117
+ "colab": {
118
+ "base_uri": "https://localhost:8080/"
119
+ },
120
+ "id": "2PYZtsrhVGjZ",
121
+ "outputId": "9d6cef82-2302-48f3-dab6-c7406711c331"
122
+ },
123
+ "execution_count": null,
124
+ "outputs": [
125
+ {
126
+ "output_type": "stream",
127
+ "name": "stdout",
128
+ "text": [
129
+ "Epoch 1/50\n"
130
+ ]
131
+ },
132
+ {
133
+ "output_type": "stream",
134
+ "name": "stderr",
135
+ "text": [
136
+ "/usr/local/lib/python3.10/dist-packages/keras/src/trainers/data_adapters/py_dataset_adapter.py:122: UserWarning: Your `PyDataset` class should call `super().__init__(**kwargs)` in its constructor. `**kwargs` can include `workers`, `use_multiprocessing`, `max_queue_size`. Do not pass these arguments to `fit()`, as they will be ignored.\n",
137
+ " self._warn_if_super_not_called()\n"
138
+ ]
139
+ },
140
+ {
141
+ "output_type": "stream",
142
+ "name": "stdout",
143
+ "text": [
144
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m136s\u001b[0m 7s/step - accuracy: 0.3350 - loss: 1.6972 - val_accuracy: 0.3417 - val_loss: 1.3020\n",
145
+ "Epoch 2/50\n",
146
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 182ms/step - accuracy: 0.3816 - loss: 1.3227 - val_accuracy: 0.4750 - val_loss: 1.1209\n",
147
+ "Epoch 3/50\n",
148
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 223ms/step - accuracy: 0.5357 - loss: 0.9564 - val_accuracy: 0.5583 - val_loss: 1.0034\n",
149
+ "Epoch 4/50\n",
150
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 175ms/step - accuracy: 0.5961 - loss: 0.8981 - val_accuracy: 0.5667 - val_loss: 0.9151\n",
151
+ "Epoch 5/50\n",
152
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 226ms/step - accuracy: 0.5730 - loss: 0.9111 - val_accuracy: 0.5833 - val_loss: 0.8556\n",
153
+ "Epoch 6/50\n",
154
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 184ms/step - accuracy: 0.7188 - loss: 0.6853 - val_accuracy: 0.6333 - val_loss: 0.8078\n",
155
+ "Epoch 7/50\n",
156
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 218ms/step - accuracy: 0.7019 - loss: 0.6919 - val_accuracy: 0.6750 - val_loss: 0.7685\n",
157
+ "Epoch 8/50\n",
158
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 236ms/step - accuracy: 0.7730 - loss: 0.5996 - val_accuracy: 0.6833 - val_loss: 0.7381\n",
159
+ "Epoch 9/50\n",
160
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 203ms/step - accuracy: 0.7472 - loss: 0.5987 - val_accuracy: 0.6500 - val_loss: 0.7141\n",
161
+ "Epoch 10/50\n",
162
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 196ms/step - accuracy: 0.7470 - loss: 0.6248 - val_accuracy: 0.6833 - val_loss: 0.6917\n",
163
+ "Epoch 11/50\n",
164
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 223ms/step - accuracy: 0.7687 - loss: 0.5358 - val_accuracy: 0.6833 - val_loss: 0.6693\n",
165
+ "Epoch 12/50\n",
166
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 176ms/step - accuracy: 0.8054 - loss: 0.4860 - val_accuracy: 0.6917 - val_loss: 0.6535\n",
167
+ "Epoch 13/50\n",
168
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 196ms/step - accuracy: 0.8217 - loss: 0.4857 - val_accuracy: 0.6833 - val_loss: 0.6379\n",
169
+ "Epoch 14/50\n",
170
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 245ms/step - accuracy: 0.8586 - loss: 0.4347 - val_accuracy: 0.7000 - val_loss: 0.6292\n",
171
+ "Epoch 15/50\n",
172
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 204ms/step - accuracy: 0.8516 - loss: 0.3888 - val_accuracy: 0.7083 - val_loss: 0.6151\n",
173
+ "Epoch 16/50\n",
174
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 191ms/step - accuracy: 0.8199 - loss: 0.4157 - val_accuracy: 0.7333 - val_loss: 0.6084\n",
175
+ "Epoch 17/50\n",
176
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 228ms/step - accuracy: 0.8377 - loss: 0.4106 - val_accuracy: 0.7250 - val_loss: 0.5958\n",
177
+ "Epoch 18/50\n",
178
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 221ms/step - accuracy: 0.9195 - loss: 0.3326 - val_accuracy: 0.7250 - val_loss: 0.5859\n",
179
+ "Epoch 19/50\n",
180
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 234ms/step - accuracy: 0.8840 - loss: 0.3327 - val_accuracy: 0.7083 - val_loss: 0.5821\n",
181
+ "Epoch 20/50\n",
182
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 207ms/step - accuracy: 0.8947 - loss: 0.3532 - val_accuracy: 0.7333 - val_loss: 0.5776\n",
183
+ "Epoch 21/50\n",
184
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 201ms/step - accuracy: 0.9053 - loss: 0.2998 - val_accuracy: 0.7417 - val_loss: 0.5665\n",
185
+ "Epoch 22/50\n",
186
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 209ms/step - accuracy: 0.9031 - loss: 0.3000 - val_accuracy: 0.7417 - val_loss: 0.5620\n",
187
+ "Epoch 23/50\n",
188
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 185ms/step - accuracy: 0.8956 - loss: 0.2904 - val_accuracy: 0.7333 - val_loss: 0.5560\n",
189
+ "Epoch 24/50\n",
190
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 186ms/step - accuracy: 0.9194 - loss: 0.2869 - val_accuracy: 0.7417 - val_loss: 0.5498\n",
191
+ "Epoch 25/50\n",
192
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 250ms/step - accuracy: 0.9128 - loss: 0.2674 - val_accuracy: 0.7333 - val_loss: 0.5458\n",
193
+ "Epoch 26/50\n",
194
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 201ms/step - accuracy: 0.9213 - loss: 0.2319 - val_accuracy: 0.7333 - val_loss: 0.5432\n",
195
+ "Epoch 27/50\n",
196
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 189ms/step - accuracy: 0.9412 - loss: 0.2338 - val_accuracy: 0.7500 - val_loss: 0.5397\n",
197
+ "Epoch 28/50\n",
198
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 259ms/step - accuracy: 0.9427 - loss: 0.2247 - val_accuracy: 0.7500 - val_loss: 0.5345\n",
199
+ "Epoch 29/50\n",
200
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 195ms/step - accuracy: 0.9304 - loss: 0.2206 - val_accuracy: 0.7500 - val_loss: 0.5316\n",
201
+ "Epoch 30/50\n",
202
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 201ms/step - accuracy: 0.9419 - loss: 0.2098 - val_accuracy: 0.7500 - val_loss: 0.5289\n",
203
+ "Epoch 31/50\n",
204
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 248ms/step - accuracy: 0.9420 - loss: 0.1824 - val_accuracy: 0.7500 - val_loss: 0.5273\n",
205
+ "Epoch 32/50\n",
206
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 198ms/step - accuracy: 0.9590 - loss: 0.1871 - val_accuracy: 0.7500 - val_loss: 0.5244\n",
207
+ "Epoch 33/50\n",
208
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 223ms/step - accuracy: 0.9613 - loss: 0.1816 - val_accuracy: 0.7417 - val_loss: 0.5233\n",
209
+ "Epoch 34/50\n",
210
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 258ms/step - accuracy: 0.9629 - loss: 0.1428 - val_accuracy: 0.7417 - val_loss: 0.5217\n",
211
+ "Epoch 35/50\n",
212
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 167ms/step - accuracy: 0.9606 - loss: 0.1835 - val_accuracy: 0.7583 - val_loss: 0.5231\n",
213
+ "Epoch 36/50\n",
214
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 234ms/step - accuracy: 0.9366 - loss: 0.1920 - val_accuracy: 0.7500 - val_loss: 0.5246\n",
215
+ "Epoch 37/50\n",
216
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 238ms/step - accuracy: 0.9464 - loss: 0.1747 - val_accuracy: 0.7583 - val_loss: 0.5184\n",
217
+ "Epoch 38/50\n",
218
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 187ms/step - accuracy: 0.9601 - loss: 0.1621 - val_accuracy: 0.7583 - val_loss: 0.5132\n",
219
+ "Epoch 39/50\n",
220
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 184ms/step - accuracy: 0.9691 - loss: 0.1530 - val_accuracy: 0.7583 - val_loss: 0.5097\n",
221
+ "Epoch 40/50\n",
222
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 207ms/step - accuracy: 0.9655 - loss: 0.1480 - val_accuracy: 0.7667 - val_loss: 0.5113\n",
223
+ "Epoch 41/50\n",
224
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 184ms/step - accuracy: 0.9671 - loss: 0.1483 - val_accuracy: 0.7583 - val_loss: 0.5122\n",
225
+ "Epoch 42/50\n",
226
+ "\u001b[1m12/12\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 207ms/step - accuracy: 0.9775 - loss: 0.1268 - val_accuracy: 0.7583 - val_loss: 0.5124\n",
227
+ "\u001b[1m4/4\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 284ms/step - accuracy: 0.7763 - loss: 0.4887\n",
228
+ "Validation Loss: 0.509696900844574\n",
229
+ "Validation Accuracy: 0.7583333253860474\n"
230
+ ]
231
+ }
232
+ ]
233
+ },
234
+ {
235
+ "cell_type": "code",
236
+ "source": [
237
+ "import os\n",
238
+ "import numpy as np\n",
239
+ "import pandas as pd\n",
240
+ "import tensorflow as tf\n",
241
+ "from tensorflow.keras.utils import load_img, img_to_array\n",
242
+ "\n",
243
+ "# Load the file containing test image names\n",
244
+ "test_images = pd.read_csv('/content/drive/MyDrive/Bacterial Classification/test_filenames.txt', header=None)\n",
245
+ "test_images.columns = ['Image Name']\n",
246
+ "\n",
247
+ "# Path to the test folder containing the images\n",
248
+ "test_dir = '/content/drive/MyDrive/Bacterial Classification/test'\n",
249
+ "\n",
250
+ "# Placeholder for predictions\n",
251
+ "predictions = []\n",
252
+ "\n",
253
+ "# Process each image and predict\n",
254
+ "for img_name in test_images['Image Name']:\n",
255
+ " # Construct the full path to the image\n",
256
+ " img_path = os.path.join(test_dir, img_name)\n",
257
+ "\n",
258
+ " # Load and preprocess the image\n",
259
+ " img = load_img(img_path, target_size=(224, 224)) # Resize image to match the model's input size\n",
260
+ " img_array = img_to_array(img) / 255.0 # Normalize pixel values\n",
261
+ " img_array = np.expand_dims(img_array, axis=0) # Add batch dimension\n",
262
+ "\n",
263
+ " # Make a prediction using the trained model\n",
264
+ " prediction = model.predict(img_array, verbose=0) # Suppress verbose output\n",
265
+ " predictions.append(prediction.argmax()) # Append the predicted class index (0, 1, 2)\n",
266
+ "\n",
267
+ "# Add predictions to the DataFrame\n",
268
+ "test_images['Predicted Class'] = predictions"
269
+ ],
270
+ "metadata": {
271
+ "id": "Wy-i6rizMrt9"
272
+ },
273
+ "execution_count": null,
274
+ "outputs": []
275
+ },
276
+ {
277
+ "cell_type": "code",
278
+ "source": [
279
+ "predictions"
280
+ ],
281
+ "metadata": {
282
+ "colab": {
283
+ "base_uri": "https://localhost:8080/"
284
+ },
285
+ "id": "Pt1zSfXsTIlt",
286
+ "outputId": "145ef195-25ca-450b-bd05-bae27efe6fc5"
287
+ },
288
+ "execution_count": null,
289
+ "outputs": [
290
+ {
291
+ "output_type": "execute_result",
292
+ "data": {
293
+ "text/plain": [
294
+ "[0,\n",
295
+ " 0,\n",
296
+ " 0,\n",
297
+ " 0,\n",
298
+ " 0,\n",
299
+ " 0,\n",
300
+ " 0,\n",
301
+ " 1,\n",
302
+ " 0,\n",
303
+ " 0,\n",
304
+ " 0,\n",
305
+ " 0,\n",
306
+ " 0,\n",
307
+ " 0,\n",
308
+ " 0,\n",
309
+ " 0,\n",
310
+ " 0,\n",
311
+ " 0,\n",
312
+ " 0,\n",
313
+ " 0,\n",
314
+ " 0,\n",
315
+ " 0,\n",
316
+ " 0,\n",
317
+ " 0,\n",
318
+ " 0,\n",
319
+ " 0,\n",
320
+ " 0,\n",
321
+ " 0,\n",
322
+ " 1,\n",
323
+ " 0,\n",
324
+ " 0,\n",
325
+ " 0,\n",
326
+ " 0,\n",
327
+ " 0,\n",
328
+ " 0,\n",
329
+ " 0,\n",
330
+ " 0,\n",
331
+ " 0,\n",
332
+ " 0,\n",
333
+ " 1,\n",
334
+ " 1,\n",
335
+ " 1,\n",
336
+ " 2,\n",
337
+ " 1,\n",
338
+ " 1,\n",
339
+ " 1,\n",
340
+ " 1,\n",
341
+ " 1,\n",
342
+ " 1,\n",
343
+ " 1,\n",
344
+ " 1,\n",
345
+ " 1,\n",
346
+ " 0,\n",
347
+ " 1,\n",
348
+ " 1,\n",
349
+ " 2,\n",
350
+ " 0,\n",
351
+ " 1,\n",
352
+ " 1,\n",
353
+ " 1,\n",
354
+ " 1,\n",
355
+ " 1,\n",
356
+ " 1,\n",
357
+ " 1,\n",
358
+ " 1,\n",
359
+ " 1,\n",
360
+ " 1,\n",
361
+ " 2,\n",
362
+ " 1,\n",
363
+ " 1,\n",
364
+ " 1,\n",
365
+ " 1,\n",
366
+ " 0,\n",
367
+ " 1,\n",
368
+ " 1,\n",
369
+ " 1,\n",
370
+ " 1,\n",
371
+ " 1,\n",
372
+ " 1,\n",
373
+ " 1,\n",
374
+ " 2,\n",
375
+ " 2,\n",
376
+ " 2,\n",
377
+ " 2,\n",
378
+ " 0,\n",
379
+ " 2,\n",
380
+ " 2,\n",
381
+ " 2,\n",
382
+ " 2,\n",
383
+ " 2,\n",
384
+ " 2,\n",
385
+ " 2,\n",
386
+ " 2,\n",
387
+ " 1,\n",
388
+ " 2,\n",
389
+ " 2,\n",
390
+ " 1,\n",
391
+ " 2,\n",
392
+ " 2,\n",
393
+ " 2,\n",
394
+ " 2,\n",
395
+ " 2,\n",
396
+ " 2,\n",
397
+ " 1,\n",
398
+ " 1,\n",
399
+ " 2,\n",
400
+ " 1,\n",
401
+ " 2,\n",
402
+ " 1,\n",
403
+ " 2,\n",
404
+ " 2,\n",
405
+ " 2,\n",
406
+ " 1,\n",
407
+ " 1,\n",
408
+ " 2,\n",
409
+ " 2,\n",
410
+ " 2,\n",
411
+ " 2,\n",
412
+ " 2,\n",
413
+ " 2]"
414
+ ]
415
+ },
416
+ "metadata": {},
417
+ "execution_count": 9
418
+ }
419
+ ]
420
+ },
421
+ {
422
+ "cell_type": "code",
423
+ "source": [
424
+ "model.save('/content/drive/MyDrive/Bacterial Classification/saved_model.keras')"
425
+ ],
426
+ "metadata": {
427
+ "id": "RfyBMOReTZfR"
428
+ },
429
+ "execution_count": null,
430
+ "outputs": []
431
+ }
432
+ ]
433
+ }