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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "machine_shape": "hm"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Weather Classification\n",
        "\n",
        "\n",
        "VGG16: https://medium.com/@mygreatlearning/everything-you-need-to-know-about-vgg16-7315defb5918\n",
        "\n",
        "Dataset: https://www.kaggle.com/datasets/jehanbhathena/weather-dataset/code"
      ],
      "metadata": {
        "id": "CQOw1NmVPU_P"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "IHSO3NaPPQdf"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import matplotlib.pyplot as plt\n",
        "import glob\n",
        "import os\n",
        "import cv2\n",
        "import math\n",
        "from keras import applications\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Conv2D,MaxPooling2D,Convolution2D,Activation,Flatten,Dense,Dropout,MaxPool2D,BatchNormalization\n",
        "from keras.utils import to_categorical\n",
        "from keras.preprocessing.image import ImageDataGenerator"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!kaggle datasets download -d jehanbhathena/weather-dataset"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hp0eduCS2Eqj",
        "outputId": "4248727f-35bb-4c6e-9d38-84ce5c0fe95f"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Dataset URL: https://www.kaggle.com/datasets/jehanbhathena/weather-dataset\n",
            "License(s): CC0-1.0\n",
            "Downloading weather-dataset.zip to /content\n",
            "100% 586M/587M [00:23<00:00, 25.1MB/s]\n",
            "100% 587M/587M [00:23<00:00, 26.2MB/s]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!unzip weather-dataset.zip"
      ],
      "metadata": {
        "collapsed": true,
        "id": "DdF4AUTN2UXX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import shutil\n",
        "\n",
        "# Define paths to the original dataset and the split folders\n",
        "dataset_path = 'dataset'\n",
        "train_path = 'train'\n",
        "test_path = 'test'\n",
        "val_path = 'val'\n",
        "\n",
        "# Create the split folders if they don't exist\n",
        "os.makedirs(train_path, exist_ok=True)\n",
        "os.makedirs(test_path, exist_ok=True)\n",
        "os.makedirs(val_path, exist_ok=True)\n",
        "\n",
        "# Define the split ratios (e.g., 80% train, 10% test, 10% val)\n",
        "train_ratio = 0.8\n",
        "test_ratio = 0.1\n",
        "val_ratio = 0.1\n",
        "\n",
        "# Iterate through the classes in the dataset\n",
        "for class_name in os.listdir(dataset_path):\n",
        "    class_path = os.path.join(dataset_path, class_name)\n",
        "    if os.path.isdir(class_path):\n",
        "        # Get a list of all image files in the class folder\n",
        "        images = os.listdir(class_path)\n",
        "        num_images = len(images)\n",
        "\n",
        "        # Calculate the number of images for each split\n",
        "        num_train = int(num_images * train_ratio)\n",
        "        num_test = int(num_images * test_ratio)\n",
        "        num_val = num_images - num_train - num_test\n",
        "\n",
        "        # Shuffle the images randomly\n",
        "        import random\n",
        "        random.shuffle(images)\n",
        "\n",
        "        # Split the images into train, test, and val sets\n",
        "        train_images = images[:num_train]\n",
        "        test_images = images[num_train:num_train + num_test]\n",
        "        val_images = images[num_train + num_test:]\n",
        "\n",
        "        # Copy the images to the corresponding split folders\n",
        "        for image in train_images:\n",
        "            src_path = os.path.join(class_path, image)\n",
        "            dst_path = os.path.join(train_path, class_name, image)\n",
        "            os.makedirs(os.path.dirname(dst_path), exist_ok=True)\n",
        "            shutil.copy(src_path, dst_path)\n",
        "\n",
        "        for image in test_images:\n",
        "            src_path = os.path.join(class_path, image)\n",
        "            dst_path = os.path.join(test_path, class_name, image)\n",
        "            os.makedirs(os.path.dirname(dst_path), exist_ok=True)\n",
        "            shutil.copy(src_path, dst_path)\n",
        "\n",
        "        for image in val_images:\n",
        "            src_path = os.path.join(class_path, image)\n",
        "            dst_path = os.path.join(val_path, class_name, image)\n",
        "            os.makedirs(os.path.dirname(dst_path), exist_ok=True)\n",
        "            shutil.copy(src_path, dst_path)\n"
      ],
      "metadata": {
        "id": "5BnE99-j2an-"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from keras.applications import VGG16\n",
        "import tensorflow as tf\n",
        "from keras.applications import VGG16\n",
        "from keras.preprocessing.image import ImageDataGenerator\n",
        "from tensorflow import keras  # Assuming you're using TensorFlow backend for Keras"
      ],
      "metadata": {
        "id": "R75l2rED6_ed"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Preprocess the input images\n",
        "img_width, img_height = 224, 224\n",
        "train_datagen = ImageDataGenerator(rescale=1.0/255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)\n",
        "test_datagen = ImageDataGenerator(rescale=1.0/255)\n",
        "\n",
        "train_generator = train_datagen.flow_from_directory(train_path, target_size=(img_width, img_height), batch_size=32, class_mode='categorical')\n",
        "test_generator = test_datagen.flow_from_directory(test_path, target_size=(img_width, img_height), batch_size=32, class_mode='categorical')\n",
        "val_generator = test_datagen.flow_from_directory(val_path, target_size=(img_width, img_height), batch_size=32, class_mode='categorical')"
      ],
      "metadata": {
        "id": "YgkUx9L47DZ5",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "3a162e36-fed0-401f-e567-152908f3e8af"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Found 5484 images belonging to 11 classes.\n",
            "Found 682 images belonging to 11 classes.\n",
            "Found 696 images belonging to 11 classes.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Load the pre-trained VGG16 model\n",
        "base_model = VGG16(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))\n",
        "\n",
        "# Freeze the layers of the base model\n",
        "for layer in base_model.layers:\n",
        "  layer.trainable = False\n",
        "\n",
        "# Build the top layers of the model\n",
        "model = keras.Sequential()\n",
        "model.add(base_model)\n",
        "model.add(Flatten())\n",
        "model.add(Dense(256, activation='relu'))\n",
        "model.add(Dropout(0.5))\n",
        "model.add(Dense(len(train_generator.class_indices), activation='softmax'))"
      ],
      "metadata": {
        "id": "b0f8xLg27GbU",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ab037228-dfe2-44cc-b071-9d761f221d00"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\n",
            "58889256/58889256 [==============================] - 3s 0us/step\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Compile the model with GPU-specific options\n",
        "model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n",
        "model.fit(\n",
        "    train_generator,\n",
        "    epochs=4,\n",
        "    validation_data=val_generator,\n",
        "    use_multiprocessing=True  # Enable parallel data processing\n",
        ")"
      ],
      "metadata": {
        "id": "spVQEuxc7NzR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install gradio"
      ],
      "metadata": {
        "collapsed": true,
        "id": "9z2RBvgtls00"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# prompt: make preprocess function for the image to make it shape=(None, 224, 224, 3) given the image\n",
        "\n",
        "def preprocess_image(image):\n",
        "  image = tf.image.resize(image, (224, 224))\n",
        "  image = tf.expand_dims(image, axis=0)  # Add batch dimension\n",
        "  return image\n"
      ],
      "metadata": {
        "id": "N6D-1AudybW6"
      },
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "def classify_image(input):\n",
        "  img_resized =  preprocess_image(input)\n",
        "  prediction = model.predict(img_resized).flatten()\n",
        "  valid_keys = train_generator.class_indices.keys()\n",
        "  return {key: float(prediction[i]) for i, key in enumerate(valid_keys)}\n",
        "\n",
        "\n",
        "image = gr.Image()\n",
        "label = gr.Label(num_top_classes=3)\n",
        "\n",
        "gr.Interface(fn=classify_image, inputs=image, outputs=label,).launch(debug='True')\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 668
        },
        "id": "7CEt0TTUNNRK",
        "outputId": "f953da44-051d-4311-c2e0-049e90862e9b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
            "\n",
            "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
            "Running on public URL: https://23687fa46e6799845b.gradio.live\n",
            "\n",
            "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ],
            "text/html": [
              "<div><iframe src=\"https://23687fa46e6799845b.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1/1 [==============================] - 1s 589ms/step\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# prompt: build gradio interface for the model\n",
        "\n",
        "import gradio as gr\n",
        "\n",
        "def classify_image(inp):\n",
        "  total_elements = inp.size\n",
        "  num_channels = 3  # Assuming RGB channels\n",
        "  image_size = img_width * img_height\n",
        "\n",
        "  # Calculate the number of images that can fit\n",
        "  batch_size = total_elements // image_size // num_channels\n",
        "\n",
        "  # Reshape to (batch_size, img_height, img_width, num_channels)\n",
        "  inp = inp.reshape(batch_size, img_height, img_width, num_channels)\n",
        "\n",
        "  prediction = model.predict(inp).flatten()\n",
        "  return {train_generator.class_indices[i]: float(prediction[i]) for i in range(len(train_generator.class_indices))}\n",
        "\n",
        "image = gr.Image()\n",
        "label = gr.Label(num_top_classes=3)\n",
        "\n",
        "gr.Interface(fn=classify_image, inputs=image, outputs=label,).launch(debug='True')\n"
      ],
      "metadata": {
        "id": "1Vl7e3-hqrOF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import gradio as gr\n",
        "from gradio import Image  # Import only necessary components\n",
        "\n",
        "def classify_image(image):\n",
        "    \"\"\"Classifies an image using the provided model.\n",
        "\n",
        "    Args:\n",
        "        image: A PIL Image object or a NumPy array representing the image.\n",
        "\n",
        "    Returns:\n",
        "        A dictionary mapping class labels to their corresponding probabilities.\n",
        "    \"\"\"\n",
        "    img_resized =  preprocess_image(image)\n",
        "    prediction = model.predict(img_resized).flatten()\n",
        "    return {train_generator.class_indices[i]: float(prediction[i]) for i in range(len(prediction))}\n",
        "\n",
        "# Create the Gradio interface\n",
        "interface = gr.Interface(\n",
        "    fn=classify_image,\n",
        "    inputs=gr.Image(),  # Specify input type for clarity\n",
        "    outputs=gr.Label(num_top_classes=3),\n",
        "    title=\"Image Classifier\",  # Optional title for the interface\n",
        "    description=\"Classify an image using your machine learning model.\",  # Optional description\n",
        ").launch(debug=True)\n"
      ],
      "metadata": {
        "id": "Ofbg-uCTkJ6R"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}