Diff of /WCEBleedGen.ipynb [000000] .. [36e837]

Switch to unified view

a b/WCEBleedGen.ipynb
1
{
2
  "nbformat": 4,
3
  "nbformat_minor": 0,
4
  "metadata": {
5
    "colab": {
6
      "provenance": [],
7
      "authorship_tag": "ABX9TyOK/tfXqNBMTnLqQ6UmtznZ",
8
      "include_colab_link": true
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": "markdown",
21
      "metadata": {
22
        "id": "view-in-github",
23
        "colab_type": "text"
24
      },
25
      "source": [
26
        "<a href=\"https://colab.research.google.com/github/2004ARYAN/WCEBleedGen-AI/blob/main/WCEBleedGen.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27
      ]
28
    },
29
    {
30
      "cell_type": "code",
31
      "execution_count": 1,
32
      "metadata": {
33
        "id": "6-HEO5ZZjkJl"
34
      },
35
      "outputs": [],
36
      "source": [
37
        "import zipfile"
38
      ]
39
    },
40
    {
41
      "cell_type": "code",
42
      "source": [
43
        "# Path to the training dataset zip file\n",
44
        "train_dataset_path = '/content/sample_data/WCEBleedGen.zip'\n",
45
        "\n",
46
        "# Path to the testing dataset zip file\n",
47
        "test_dataset_path = '/content/sample_data/Auto-WCEBleedGen Challenge Test Dataset.zip'\n",
48
        "\n",
49
        "# Extract the training dataset\n",
50
        "with zipfile.ZipFile(train_dataset_path, 'r') as zip_ref:\n",
51
        "    zip_ref.extractall('/content/sample_data')\n",
52
        "\n",
53
        "# Extract the testing dataset\n",
54
        "with zipfile.ZipFile(test_dataset_path, 'r') as zip_ref:\n",
55
        "    zip_ref.extractall('/content/sample_data')"
56
      ],
57
      "metadata": {
58
        "id": "m5dvbzIJjnFn"
59
      },
60
      "execution_count": 2,
61
      "outputs": []
62
    },
63
    {
64
      "cell_type": "code",
65
      "source": [
66
        "# Organize the data directories\n",
67
        "train_data_dir = '/content/sample_data/train_dataset'\n",
68
        "test_data_dir = '/content/sample_data/test_dataset'\n"
69
      ],
70
      "metadata": {
71
        "id": "zqm-6MH9jnH7"
72
      },
73
      "execution_count": 3,
74
      "outputs": []
75
    },
76
    {
77
      "cell_type": "code",
78
      "source": [
79
        "import os\n",
80
        "\n",
81
        "# Set the path to the extracted training dataset directory\n",
82
        "extracted_data_dir = '/content/sample_data/WCEBleedGen'\n",
83
        "\n",
84
        "# Define the path to your training and testing data within the extracted directory\n",
85
        "train_data_dir = os.path.join(extracted_data_dir, '/content/sample_data/WCEBleedGen')\n",
86
        "test_data_dir = os.path.join(extracted_data_dir, '/content/sample_data/Auto-WCEBleedGen Challenge Test Dataset')\n"
87
      ],
88
      "metadata": {
89
        "id": "LQ7GMenHjnM4"
90
      },
91
      "execution_count": 6,
92
      "outputs": []
93
    },
94
    {
95
      "cell_type": "code",
96
      "source": [
97
        "import tensorflow as tf\n",
98
        "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
99
        "\n",
100
        "\n",
101
        "\n",
102
        "# Create data generators for training and testing\n",
103
        "train_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\n",
104
        "train_generator = train_datagen.flow_from_directory(\n",
105
        "    train_data_dir,\n",
106
        "    target_size=(150, 150),\n",
107
        "    batch_size=32,\n",
108
        "    class_mode='binary',\n",
109
        "    subset='training'  # For training data\n",
110
        ")\n",
111
        "\n",
112
        "validation_generator = train_datagen.flow_from_directory(\n",
113
        "    train_data_dir,\n",
114
        "    target_size=(150, 150),\n",
115
        "    batch_size=32,\n",
116
        "    class_mode='binary',\n",
117
        "    subset='validation'  # For validation data\n",
118
        ")\n",
119
        "\n",
120
        "\n"
121
      ],
122
      "metadata": {
123
        "colab": {
124
          "base_uri": "https://localhost:8080/"
125
        },
126
        "id": "l6dPYUqAjnPO",
127
        "outputId": "8465f9e5-ce7f-4ed2-a637-9052bf110b2e"
128
      },
129
      "execution_count": 8,
130
      "outputs": [
131
        {
132
          "output_type": "stream",
133
          "name": "stdout",
134
          "text": [
135
            "Found 4190 images belonging to 2 classes.\n",
136
            "Found 1046 images belonging to 2 classes.\n"
137
          ]
138
        }
139
      ]
140
    },
141
    {
142
      "cell_type": "code",
143
      "source": [
144
        "import tensorflow as tf\n",
145
        "from tensorflow.keras.models import Sequential\n",
146
        "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n",
147
        "from tensorflow.keras.optimizers import Adam\n",
148
        "\n",
149
        "# Define and compile your model\n",
150
        "model = Sequential([\n",
151
        "    Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),\n",
152
        "    MaxPooling2D(2, 2),\n",
153
        "    Conv2D(64, (3, 3), activation='relu'),\n",
154
        "    MaxPooling2D(2, 2),\n",
155
        "    Flatten(),\n",
156
        "    Dense(128, activation='relu'),\n",
157
        "    Dense(1, activation='sigmoid')\n",
158
        "])\n",
159
        "\n",
160
        "model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])\n",
161
        "\n",
162
        "# Train your model\n",
163
        "history = model.fit(\n",
164
        "    train_generator,\n",
165
        "    steps_per_epoch=len(train_generator),\n",
166
        "    epochs=10,\n",
167
        "    validation_data=validation_generator,\n",
168
        "    validation_steps=len(validation_generator)\n",
169
        ")\n"
170
      ],
171
      "metadata": {
172
        "colab": {
173
          "base_uri": "https://localhost:8080/"
174
        },
175
        "id": "b5DsWJjI1cao",
176
        "outputId": "955fc3b8-8216-4220-f95d-5c5da2fbed28"
177
      },
178
      "execution_count": 9,
179
      "outputs": [
180
        {
181
          "output_type": "stream",
182
          "name": "stderr",
183
          "text": [
184
            "WARNING:absl:`lr` is deprecated in Keras optimizer, please use `learning_rate` or use the legacy optimizer, e.g.,tf.keras.optimizers.legacy.Adam.\n"
185
          ]
186
        },
187
        {
188
          "output_type": "stream",
189
          "name": "stdout",
190
          "text": [
191
            "Epoch 1/10\n",
192
            "131/131 [==============================] - 181s 1s/step - loss: 0.3123 - accuracy: 0.8814 - val_loss: 0.0428 - val_accuracy: 0.9895\n",
193
            "Epoch 2/10\n",
194
            "131/131 [==============================] - 160s 1s/step - loss: 0.0743 - accuracy: 0.9737 - val_loss: 0.0466 - val_accuracy: 0.9847\n",
195
            "Epoch 3/10\n",
196
            "131/131 [==============================] - 171s 1s/step - loss: 0.0316 - accuracy: 0.9883 - val_loss: 0.1191 - val_accuracy: 0.9713\n",
197
            "Epoch 4/10\n",
198
            "131/131 [==============================] - 158s 1s/step - loss: 0.0283 - accuracy: 0.9928 - val_loss: 0.1047 - val_accuracy: 0.9818\n",
199
            "Epoch 5/10\n",
200
            "131/131 [==============================] - 159s 1s/step - loss: 0.0123 - accuracy: 0.9976 - val_loss: 0.0974 - val_accuracy: 0.9837\n",
201
            "Epoch 6/10\n",
202
            "131/131 [==============================] - 161s 1s/step - loss: 0.0023 - accuracy: 0.9995 - val_loss: 0.1689 - val_accuracy: 0.9771\n",
203
            "Epoch 7/10\n",
204
            "131/131 [==============================] - 161s 1s/step - loss: 0.0018 - accuracy: 0.9998 - val_loss: 0.0591 - val_accuracy: 0.9895\n",
205
            "Epoch 8/10\n",
206
            "131/131 [==============================] - 159s 1s/step - loss: 0.0171 - accuracy: 0.9947 - val_loss: 0.0362 - val_accuracy: 0.9914\n",
207
            "Epoch 9/10\n",
208
            "131/131 [==============================] - 158s 1s/step - loss: 0.0045 - accuracy: 0.9986 - val_loss: 0.0446 - val_accuracy: 0.9914\n",
209
            "Epoch 10/10\n",
210
            "131/131 [==============================] - 169s 1s/step - loss: 0.0492 - accuracy: 0.9862 - val_loss: 0.0800 - val_accuracy: 0.9895\n"
211
          ]
212
        }
213
      ]
214
    },
215
    {
216
      "cell_type": "code",
217
      "source": [
218
        "test_data_dir = '/content/sample_data/Auto-WCEBleedGen Challenge Test Dataset'  # Update with the path to your testing dataset\n",
219
        "test_datagen = ImageDataGenerator(rescale=1./255)\n",
220
        "test_generator = test_datagen.flow_from_directory(\n",
221
        "    test_data_dir,\n",
222
        "    target_size=(150, 150),\n",
223
        "    batch_size=32,\n",
224
        "    class_mode='binary'\n",
225
        ")\n"
226
      ],
227
      "metadata": {
228
        "colab": {
229
          "base_uri": "https://localhost:8080/"
230
        },
231
        "id": "-olZuu6R1cgV",
232
        "outputId": "2fc323c3-5dbc-4864-d751-ed28aab4c1ce"
233
      },
234
      "execution_count": 11,
235
      "outputs": [
236
        {
237
          "output_type": "stream",
238
          "name": "stdout",
239
          "text": [
240
            "Found 564 images belonging to 2 classes.\n"
241
          ]
242
        }
243
      ]
244
    },
245
    {
246
      "cell_type": "code",
247
      "source": [
248
        "predictions = model.predict(test_generator)\n"
249
      ],
250
      "metadata": {
251
        "colab": {
252
          "base_uri": "https://localhost:8080/"
253
        },
254
        "id": "RkVc-2jA1ci8",
255
        "outputId": "071bbfdd-1ea3-4b5f-d590-f30b62b40f85"
256
      },
257
      "execution_count": 12,
258
      "outputs": [
259
        {
260
          "output_type": "stream",
261
          "name": "stdout",
262
          "text": [
263
            "18/18 [==============================] - 7s 385ms/step\n"
264
          ]
265
        }
266
      ]
267
    },
268
    {
269
      "cell_type": "code",
270
      "source": [
271
        "import pandas as pd\n",
272
        "import smtplib\n",
273
        "\n",
274
        "\n",
275
        "# Step 1: Evaluation Metrics\n",
276
        "# Calculate and gather evaluation metrics here\n",
277
        "# Replace these with your actual evaluation metrics\n",
278
        "accuracy = 0.95\n",
279
        "recall = 0.90\n",
280
        "f1_score = 0.92\n",
281
        "iou = 0.80\n",
282
        "ap = 0.85\n",
283
        "map_score = 0.82\n",
284
        "\n",
285
        "# Step 2:  Excel Sheet Preparation\n",
286
        "# Create a pandas DataFrame with your testing results\n",
287
        "# Replace these with your actual image IDs and predicted labels\n",
288
        "image_ids = ['Image1', 'Image2', 'Image3', 'Image4', 'Image5']\n",
289
        "predicted_labels = [1, 0, 1, 0, 1]\n",
290
        "\n",
291
        "results = {\n",
292
        "    'Image name': image_ids,\n",
293
        "    'Predicted class label': predicted_labels,\n",
294
        "    'Predicted coordinates of bounding box': ['x1, y1, x2, y2'] * len(image_ids),\n",
295
        "    'Confidence level': [0.95] * len(image_ids)\n",
296
        "}\n",
297
        "\n",
298
        "df = pd.DataFrame(results)\n",
299
        "\n",
300
        "# Save the DataFrame to an Excel file\n",
301
        "df.to_excel('auto_wce_bleedgen_results.xlsx', index=False)"
302
      ],
303
      "metadata": {
304
        "id": "28eA4pqs1clB"
305
      },
306
      "execution_count": 30,
307
      "outputs": []
308
    },
309
    {
310
      "cell_type": "code",
311
      "source": [],
312
      "metadata": {
313
        "id": "z3Pgl-AX1cpK"
314
      },
315
      "execution_count": null,
316
      "outputs": []
317
    }
318
  ]
319
}