{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import numpy as np\n", "from tensorflow.keras.layers import BatchNormalization, Conv2D, MaxPooling2D\n", "from tensorflow.keras.preprocessing.image import load_img, img_to_array\n", "from sklearn.metrics import mean_squared_error\n", "from tensorflow.keras.initializers import RandomNormal\n", "from tensorflow.keras.applications.vgg16 import VGG16\n", "from tensorflow.keras.optimizers import SGD\n", "from tensorflow.keras.models import Model, Sequential\n", "from tensorflow.keras import backend as K\n", "from tensorflow.keras.models import model_from_json\n", "from matplotlib import cm as CM\n", "import matplotlib.pyplot as plt\n", "import tensorflow as tf\n", "from tqdm import tqdm\n", "import scipy.io as io\n", "from PIL import Image\n", "import PIL\n", "import h5py\n", "import os\n", "import glob\n", "import cv2\n", "import random\n", "import math\n", "import sys" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "K.clear_session()\n", "root = 'DATA'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "part_A_train = os.path.join(root, 'part_A_final/train_data', 'images')\n", "part_A_test = os.path.join(root, 'part_A_final/test_data', 'images')\n", "part_B_train = os.path.join(root, 'part_B_final/train_data', 'images')\n", "part_B_test = os.path.join(root, 'part_B_final/test_data', 'images')\n", "temp = 'test_images'\n", "path_sets = [part_A_train]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total images: 300\n" ] } ], "source": [ "img_paths = []\n", "for path in path_sets:\n", " for img_path in glob.glob(os.path.join(path, '*.jpg')):\n", " img_paths.append(str(img_path))\n", "print(\"Total images:\", len(img_paths))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def create_img(path):\n", " \"\"\"Function to load, normalize and return image\"\"\"\n", " im = Image.open(path).convert('RGB')\n", " im = np.array(im)\n", " im = im / 255.0\n", " \n", " im[:, :, 0] = (im[:, :, 0] - 0.485) / 0.229\n", " im[:, :, 1] = (im[:, :, 1] - 0.456) / 0.224\n", " im[:, :, 2] = (im[:, :, 2] - 0.406) / 0.225\n", " \n", " return im\n", "\n", "\n", "def get_input(path):\n", " path = path[0]\n", " img = create_img(path)\n", " return img\n", "\n", "\n", "def get_output(path):\n", " \"\"\"Import and resize target density map\"\"\"\n", " gt_file = h5py.File(path, 'r')\n", " target = np.asarray(gt_file['density'])\n", " \n", " img = cv2.resize(target, (int(target.shape[1]/8), int(target.shape[0]/8)), \n", " interpolation=cv2.INTER_CUBIC) * 64\n", " img = np.expand_dims(img, axis=2) # Changed from axis=3 to axis=2\n", " \n", " return img\n", "\n", "\n", "\n", "def preprocess_input(image, target):\n", " \"\"\"Crop image and target (optional data augmentation)\"\"\"\n", " crop_size = (int(image.shape[0]/2), int(image.shape[1]/2))\n", " \n", " if random.randint(0, 9) <= -1:\n", " dx = int(random.randint(0, 1) * image.shape[0] * 1./2)\n", " dy = int(random.randint(0, 1) * image.shape[1] * 1./2)\n", " else:\n", " dx = int(random.random() * image.shape[0] * 1./2)\n", " dy = int(random.random() * image.shape[1] * 1./2)\n", " \n", " img = image[dx:crop_size[0]+dx, dy:crop_size[1]+dy]\n", " target_aug = target[dx:crop_size[0]+dx, dy:crop_size[1]+dy]\n", " \n", " return (img, target_aug)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def image_generator(files, batch_size=1):\n", " \"\"\"Image data generator\"\"\"\n", " import numpy as np # Add this line\n", " while True:\n", " input_path = np.random.choice(a=files, size=1)\n", " \n", " inputt = get_input(input_path)\n", " # Match the path from preprocessing\n", " output_path = input_path[0].replace('.jpg', '.h5').replace('images', 'ground')\n", " \n", " if not os.path.exists(output_path):\n", " print(f\"File not found: {output_path}\")\n", " continue\n", " \n", " output = get_output(output_path)\n", " \n", " batch_x = np.expand_dims(inputt, axis=0)\n", " batch_y = np.expand_dims(output, axis=0)\n", " \n", " yield (batch_x, batch_y)\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def save_mod(model, str1, str2):\n", " \"\"\"Save model weights and architecture\"\"\"\n", " model.save_weights(str1)\n", " model_json = model.to_json()\n", " \n", " with open(str2, \"w\") as json_file:\n", " json_file.write(model_json)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def init_weights_vgg(model):\n", " \"\"\"Initialize VGG16 pretrained weights\"\"\"\n", " # Use built-in VGG16 with ImageNet weights\n", " vgg = VGG16(weights='imagenet', include_top=False)\n", " \n", " vgg_weights = []\n", " for layer in vgg.layers:\n", " if 'conv' in layer.name:\n", " vgg_weights.append(layer.get_weights())\n", " \n", " offset = 0\n", " i = 0\n", " while i < 10:\n", " if 'conv' in model.layers[i+offset].name:\n", " model.layers[i+offset].set_weights(vgg_weights[i])\n", " i = i + 1\n", " else:\n", " offset = offset + 1\n", " \n", " return model\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def euclidean_distance_loss(y_true, y_pred):\n", " \"\"\"Euclidean distance as a measure of loss (Loss function)\"\"\"\n", " return tf.sqrt(tf.reduce_sum(tf.square(y_pred - y_true), axis=-1))\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# def CrowdNet():\n", "# \"\"\"Neural network model: VGG + Conv\"\"\"\n", "# # Variable Input Size\n", "# rows = None\n", "# cols = None\n", " \n", "# # Batch Normalisation option\n", "# batch_norm = 0\n", "# kernel = (3, 3)\n", "# init = RandomNormal(stddev=0.01)\n", "# model = Sequential()\n", " \n", "# # Custom VGG\n", "# if batch_norm:\n", "# model.add(Conv2D(64, kernel_size=kernel, input_shape=(rows, cols, 3), \n", "# activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(Conv2D(64, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(MaxPooling2D(strides=2))\n", "# model.add(Conv2D(128, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(Conv2D(128, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(MaxPooling2D(strides=2))\n", "# model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(MaxPooling2D(strides=2))\n", "# model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same'))\n", "# model.add(BatchNormalization())\n", "# else:\n", "# model.add(Conv2D(64, kernel_size=kernel, activation='relu', padding='same',\n", "# input_shape=(rows, cols, 3), kernel_initializer=init))\n", "# model.add(Conv2D(64, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(MaxPooling2D(strides=2))\n", "# model.add(Conv2D(128, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(Conv2D(128, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(MaxPooling2D(strides=2))\n", "# model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(MaxPooling2D(strides=2))\n", "# model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", "# model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same', \n", "# kernel_initializer=init))\n", " \n", "# # Dilated Conv2D layers\n", "# model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate=2, \n", "# kernel_initializer=init, padding='same'))\n", "# model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate=2, \n", "# kernel_initializer=init, padding='same'))\n", "# model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate=2, \n", "# kernel_initializer=init, padding='same'))\n", "# model.add(Conv2D(256, (3, 3), activation='relu', dilation_rate=2, \n", "# kernel_initializer=init, padding='same'))\n", "# model.add(Conv2D(128, (3, 3), activation='relu', dilation_rate=2, \n", "# kernel_initializer=init, padding='same'))\n", "# model.add(Conv2D(64, (3, 3), activation='relu', dilation_rate=2, \n", "# kernel_initializer=init, padding='same'))\n", "# model.add(Conv2D(1, (1, 1), activation='relu', dilation_rate=1, \n", "# kernel_initializer=init, padding='same'))\n", " \n", "# # Fixed: Use learning_rate instead of lr, removed decay\n", "\n", "# sgd = SGD(learning_rate=1e-7, momentum=0.95)\n", "# model.compile(optimizer=sgd, loss='mse', metrics=['mse']) # Use 'mse' instead\n", " \n", "# model = init_weights_vgg(model)\n", "# return model\n", "\n", "def CrowdNet():\n", " \"\"\"Neural network model: VGG + Conv\"\"\"\n", " rows = None\n", " cols = None\n", " batch_norm = 0\n", " kernel = (3, 3)\n", " init = RandomNormal(stddev=0.01)\n", " model = Sequential()\n", " \n", " # VGG layers (without batch norm)\n", " model.add(Conv2D(64, kernel_size=kernel, activation='relu', padding='same',\n", " input_shape=(rows, cols, 3), kernel_initializer=init))\n", " model.add(Conv2D(64, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(MaxPooling2D(strides=2))\n", " model.add(Conv2D(128, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(Conv2D(128, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(MaxPooling2D(strides=2))\n", " model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(Conv2D(256, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(MaxPooling2D(strides=2))\n", " model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " model.add(Conv2D(512, kernel_size=kernel, activation='relu', padding='same', kernel_initializer=init))\n", " \n", " # Dilated Conv layers\n", " model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate=2, kernel_initializer=init, padding='same'))\n", " model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate=2, kernel_initializer=init, padding='same'))\n", " model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate=2, kernel_initializer=init, padding='same'))\n", " model.add(Conv2D(256, (3, 3), activation='relu', dilation_rate=2, kernel_initializer=init, padding='same'))\n", " model.add(Conv2D(128, (3, 3), activation='relu', dilation_rate=2, kernel_initializer=init, padding='same'))\n", " model.add(Conv2D(64, (3, 3), activation='relu', dilation_rate=2, kernel_initializer=init, padding='same'))\n", " model.add(Conv2D(1, (1, 1), activation='relu', dilation_rate=1, kernel_initializer=init, padding='same'))\n", " \n", " sgd = SGD(learning_rate=1e-7, momentum=0.95)\n", " model.compile(optimizer=sgd, loss='mse', metrics=['mse'], run_eagerly=True) # Added run_eagerly=True\n", " \n", " model = init_weights_vgg(model)\n", " return model\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Building model...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\adity\\anaconda3\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:113: 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__(activity_regularizer=activity_regularizer, **kwargs)\n" ] } ], "source": [ "# Build model\n", "print(\"Building model...\")\n", "model = CrowdNet()\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Model: \"sequential\"\n",
       "
\n" ], "text/plain": [ "\u001b[1mModel: \"sequential\"\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
       "┃ Layer (type)                     Output Shape                  Param # ┃\n",
       "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
       "│ conv2d (Conv2D)                 │ (None, None, None, 64) │         1,792 │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_1 (Conv2D)               │ (None, None, None, 64) │        36,928 │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ max_pooling2d (MaxPooling2D)    │ (None, None, None, 64) │             0 │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_2 (Conv2D)               │ (None, None, None,     │        73,856 │\n",
       "│                                 │ 128)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_3 (Conv2D)               │ (None, None, None,     │       147,584 │\n",
       "│                                 │ 128)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ max_pooling2d_1 (MaxPooling2D)  │ (None, None, None,     │             0 │\n",
       "│                                 │ 128)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_4 (Conv2D)               │ (None, None, None,     │       295,168 │\n",
       "│                                 │ 256)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_5 (Conv2D)               │ (None, None, None,     │       590,080 │\n",
       "│                                 │ 256)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_6 (Conv2D)               │ (None, None, None,     │       590,080 │\n",
       "│                                 │ 256)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ max_pooling2d_2 (MaxPooling2D)  │ (None, None, None,     │             0 │\n",
       "│                                 │ 256)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_7 (Conv2D)               │ (None, None, None,     │     1,180,160 │\n",
       "│                                 │ 512)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_8 (Conv2D)               │ (None, None, None,     │     2,359,808 │\n",
       "│                                 │ 512)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_9 (Conv2D)               │ (None, None, None,     │     2,359,808 │\n",
       "│                                 │ 512)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_10 (Conv2D)              │ (None, None, None,     │     2,359,808 │\n",
       "│                                 │ 512)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_11 (Conv2D)              │ (None, None, None,     │     2,359,808 │\n",
       "│                                 │ 512)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_12 (Conv2D)              │ (None, None, None,     │     2,359,808 │\n",
       "│                                 │ 512)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_13 (Conv2D)              │ (None, None, None,     │     1,179,904 │\n",
       "│                                 │ 256)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_14 (Conv2D)              │ (None, None, None,     │       295,040 │\n",
       "│                                 │ 128)                   │               │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_15 (Conv2D)              │ (None, None, None, 64) │        73,792 │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ conv2d_16 (Conv2D)              │ (None, None, None, 1)  │            65 │\n",
       "└─────────────────────────────────┴────────────────────────┴───────────────┘\n",
       "
\n" ], "text/plain": [ "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n", "│ conv2d (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m64\u001b[0m) │ \u001b[38;5;34m1,792\u001b[0m │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_1 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m64\u001b[0m) │ \u001b[38;5;34m36,928\u001b[0m │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ max_pooling2d (\u001b[38;5;33mMaxPooling2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m64\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_2 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m73,856\u001b[0m │\n", "│ │ \u001b[38;5;34m128\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_3 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m147,584\u001b[0m │\n", "│ │ \u001b[38;5;34m128\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ max_pooling2d_1 (\u001b[38;5;33mMaxPooling2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m0\u001b[0m │\n", "│ │ \u001b[38;5;34m128\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_4 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m295,168\u001b[0m │\n", "│ │ \u001b[38;5;34m256\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_5 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m590,080\u001b[0m │\n", "│ │ \u001b[38;5;34m256\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_6 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m590,080\u001b[0m │\n", "│ │ \u001b[38;5;34m256\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ max_pooling2d_2 (\u001b[38;5;33mMaxPooling2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m0\u001b[0m │\n", "│ │ \u001b[38;5;34m256\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_7 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m1,180,160\u001b[0m │\n", "│ │ \u001b[38;5;34m512\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_8 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m2,359,808\u001b[0m │\n", "│ │ \u001b[38;5;34m512\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_9 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m2,359,808\u001b[0m │\n", "│ │ \u001b[38;5;34m512\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_10 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m2,359,808\u001b[0m │\n", "│ │ \u001b[38;5;34m512\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_11 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m2,359,808\u001b[0m │\n", "│ │ \u001b[38;5;34m512\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_12 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m2,359,808\u001b[0m │\n", "│ │ \u001b[38;5;34m512\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_13 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m1,179,904\u001b[0m │\n", "│ │ \u001b[38;5;34m256\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_14 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, │ \u001b[38;5;34m295,040\u001b[0m │\n", "│ │ \u001b[38;5;34m128\u001b[0m) │ │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_15 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m64\u001b[0m) │ \u001b[38;5;34m73,792\u001b[0m │\n", "├─────────────────────────────────┼────────────────────────┼───────────────┤\n", "│ conv2d_16 (\u001b[38;5;33mConv2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m) │ \u001b[38;5;34m65\u001b[0m │\n", "└─────────────────────────────────┴────────────────────────┴───────────────┘\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
 Total params: 16,263,489 (62.04 MB)\n",
       "
\n" ], "text/plain": [ "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m16,263,489\u001b[0m (62.04 MB)\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
 Trainable params: 16,263,489 (62.04 MB)\n",
       "
\n" ], "text/plain": [ "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m16,263,489\u001b[0m (62.04 MB)\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
       "
\n" ], "text/plain": [ "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "model.summary()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "train_gen = image_generator(img_paths,1)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Checking: data\\part_A_final/train_data\\ground\\IMG_1.h5\n", "Exists: True\n" ] } ], "source": [ "# Test if density maps exist\n", "test_path = img_paths[0].replace('.jpg', '.h5').replace('images', 'ground')\n", "print(f\"Checking: {test_path}\")\n", "print(f\"Exists: {os.path.exists(test_path)}\")\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting training...\n", "Epoch 1/5\n", "\u001b[1m20/20\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2057s\u001b[0m 104s/step - loss: 0.0270 - mse: 0.0270\n", "Epoch 2/5\n", "\u001b[1m20/20\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1935s\u001b[0m 96s/step - loss: 0.0596 - mse: 0.0596\n", "Epoch 3/5\n", "\u001b[1m20/20\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2106s\u001b[0m 103s/step - loss: 0.0830 - mse: 0.0830\n", "Epoch 4/5\n", "\u001b[1m20/20\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3105s\u001b[0m 158s/step - loss: 0.0255 - mse: 0.0255\n", "Epoch 5/5\n", "\u001b[1m20/20\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1841s\u001b[0m 92s/step - loss: 0.0355 - mse: 0.0355\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "# Train model (Fixed: use fit instead of fit_generator)\n", "import numpy as np\n", "print(\"Starting training...\")\n", "model.fit(train_gen, epochs=5, steps_per_epoch=20, verbose=1)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "os.makedirs('weights', exist_ok=True)\n", "os.makedirs('models', exist_ok=True)\n", "save_mod(model, \"weights/model_A.weights.h5\", \"models/Model.json\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 2 }