{ "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": [ "" ], "text/html": [ "
" ] }, "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": [] } ] }