{ "cells": [ { "cell_type": "code", "execution_count": 15, "id": "02edd069-0381-4537-902e-03ffd273349c", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:99: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n", " super().__init__(\n", "WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n", "WARNING:absl:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.\n" ] } ], "source": [ "from keras.models import load_model\n", "import numpy as np\n", "\n", "# Load the saved model\n", "model = load_model('model.h5')\n", "\n" ] }, { "cell_type": "code", "execution_count": 33, "id": "78c9d169-adbe-4588-9a78-fb02b90e3781", "metadata": {}, "outputs": [], "source": [ "from torchvision import transforms" ] }, { "cell_type": "code", "execution_count": 16, "id": "f57b1e4e-c171-4233-addf-a5bbdd91896f", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'input_image' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[16], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Perform inference on the input image\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# Make sure your input shape matches the input shape of the model\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m predicted_image \u001b[38;5;241m=\u001b[39m model\u001b[38;5;241m.\u001b[39mpredict(np\u001b[38;5;241m.\u001b[39mexpand_dims(\u001b[43minput_image\u001b[49m, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m))\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# The output 'predicted_image' will be the deblurred image generated by the model\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# You can further process or save the output image as needed\u001b[39;00m\n", "\u001b[1;31mNameError\u001b[0m: name 'input_image' is not defined" ] } ], "source": [ "# Perform inference on the input image\n", "# Make sure your input shape matches the input shape of the model\n", "predicted_image = model.predict(np.expand_dims(input_image, axis=0))\n", "\n", "# The output 'predicted_image' will be the deblurred image generated by the model\n", "# You can further process or save the output image as needed\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "87814748-7c0b-41e2-998b-d3a3eb6d7bbd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.16.1'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import tensorflow as tf\n", "tf.__version__" ] }, { "cell_type": "code", "execution_count": 5, "id": "9fafecb4-54e9-43a5-ac19-143205069848", "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "from tensorflow.keras.datasets import cifar10\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n", "from tensorflow.keras.utils import to_categorical\n", "from tensorflow.keras.optimizers import Adam\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "\n", "# Load CIFAR-10 dataset\n", "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", "\n", "# Normalize pixel values to be between 0 and 1\n", "x_train = x_train.astype('float32') / 255.0\n", "x_test = x_test.astype('float32') / 255.0\n", "\n", "# One-hot encode the labels\n", "y_train = to_categorical(y_train, num_classes=10)\n", "y_test = to_categorical(y_test, num_classes=10)" ] }, { "cell_type": "code", "execution_count": 47, "id": "b8e744fc-d509-49a9-a1d1-9be7f37a6c21", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 1.],\n", " [0., 0., 0., ..., 0., 0., 1.],\n", " ...,\n", " [0., 0., 0., ..., 0., 0., 1.],\n", " [0., 1., 0., ..., 0., 0., 0.],\n", " [0., 1., 0., ..., 0., 0., 0.]])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_train" ] }, { "cell_type": "code", "execution_count": 7, "id": "0ae22ae2-c40d-4a4a-9c25-00d2c423b53c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m23s\u001b[0m 72ms/step\n" ] } ], "source": [ "predicted = model.predict(x_test)" ] }, { "cell_type": "code", "execution_count": 9, "id": "e80370e5-6753-4b04-afde-ddcfcdb3c148", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 32, 32, 3)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_test.shape" ] }, { "cell_type": "code", "execution_count": 13, "id": "9bd01caf-8ce5-465a-b734-a5480b96521d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 8, 8, ..., 5, 1, 7], dtype=int64)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argmax(predicted, axis = 1)" ] }, { "cell_type": "code", "execution_count": null, "id": "070f8ef8-6522-4fe7-9d35-67f1861de531", "metadata": {}, "outputs": [], "source": [ "\n", "\n", "# Define AlexNet architecture\n", "model = Sequential([\n", " # First convolutional layer\n", " Conv2D(96, (11, 11), strides=(1, 1), activation='relu', input_shape=(32, 32, 3)),\n", " MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),\n", " # Second convolutional layer\n", " Conv2D(256, (5, 5), padding='same', activation='relu'),\n", " MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),\n", " # Third convolutional layer\n", " Conv2D(384, (3, 3), padding='same', activation='relu'),\n", " # Fourth convolutional layer\n", " Conv2D(384, (3, 3), padding='same', activation='relu'),\n", " # Fifth convolutional layer\n", " Conv2D(256, (3, 3), padding='same', activation='relu'),\n", " MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),\n", " # Flatten the convolutional layers output for fully connected layers\n", " Flatten(),\n", " # First fully connected layer\n", " Dense(4096, activation='relu'),\n", " Dropout(0.5),\n", " # Second fully connected layer\n", " Dense(4096, activation='relu'),\n", " Dropout(0.5),\n", " # Output layer\n", " Dense(10, activation='softmax')\n", "])\n", "\n", "# Compile the model with a lower learning rate\n", "optimizer = Adam(learning_rate=0.0001)\n", "model.compile(optimizer=optimizer,\n", " loss='categorical_crossentropy',\n", " metrics=['accuracy'])\n", "\n", "# Data augmentation\n", "datagen = ImageDataGenerator(\n", " rotation_range=15,\n", " width_shift_range=0.1,\n", " height_shift_range=0.1,\n", " horizontal_flip=True,\n", ")\n", "\n", "datagen.fit(x_train)\n", "\n", "# Train the model with data augmentation\n", "model.fit(datagen.flow(x_train, y_train, batch_size=128), epochs=25, validation_data=(x_test, y_test))\n", "\n", "# Evaluate the model on the test set\n", "test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=2)\n", "\n", "print(\"\\nTest Accuracy:\", test_accuracy)\n", "print(\"Test Loss:\", test_loss)" ] }, { "cell_type": "code", "execution_count": 23, "id": "82b6c768-2b9d-4633-8e17-96d26d814421", "metadata": {}, "outputs": [], "source": [ "from PIL import Image\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 40, "id": "692a13a1-3483-4dd9-9364-c27e909b89d6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7896\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "