{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport xml.etree.ElementTree as ET\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.resnet50 import ResNet50\n\n# Define the paths to your data folders\ntrain_path = '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/'\nval_path = '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/val/'\n\ntrain_annot_path = '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Annotations/CLS-LOC/train/'\nval_annot_path = '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Annotations/CLS-LOC/val/'\n\ntrain_annot_files = os.listdir(train_annot_path)\nval_annot_files = os.listdir(val_annot_path)\n\n# Define the image size and batch size\nimg_size = (224, 224)\nbatch_size = 32\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-02T18:57:07.119904Z","iopub.execute_input":"2023-05-02T18:57:07.120389Z","iopub.status.idle":"2023-05-02T18:57:07.155234Z","shell.execute_reply.started":"2023-05-02T18:57:07.120347Z","shell.execute_reply":"2023-05-02T18:57:07.15426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_annotation(annot_path, img_path):\n    \"\"\"\n    Parses the annotation XML file for an image and returns the object coordinates and class.\n    \"\"\"\n    # Parse the XML file\n    tree = ET.parse(annot_path)\n    root = tree.getroot()\n\n    # Get the object coordinates and class\n    objects = root.findall('object')\n    coords = []\n    classes = []\n    for obj in objects:\n        bbox = obj.find('bndbox')\n        x1 = int(bbox.find('xmin').text)\n        y1 = int(bbox.find('ymin').text)\n        x2 = int(bbox.find('xmax').text)\n        y2 = int(bbox.find('ymax').text)\n        coords.append([x1, y1, x2, y2])\n        classes.append(obj.find('name').text)\n\n    # Load the image\n    img = tf.keras.preprocessing.image.load_img(img_path, target_size=img_size)\n    img_arr = tf.keras.preprocessing.image.img_to_array(img)\n\n    # Normalize the coordinates to [0, 1]\n    coords = np.array(coords) / np.array([img_size[1], img_size[0], img_size[1], img_size[0]])\n\n    return img_arr, coords, classes\n","metadata":{"execution":{"iopub.status.busy":"2023-05-02T18:57:08.623619Z","iopub.execute_input":"2023-05-02T18:57:08.624652Z","iopub.status.idle":"2023-05-02T18:57:08.632736Z","shell.execute_reply.started":"2023-05-02T18:57:08.624612Z","shell.execute_reply":"2023-05-02T18:57:08.63154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_generator(img_dir, annot_files, batch_size):\n    \"\"\"\n    Returns a generator that generates batches of images and annotations.\n    \"\"\"\n    n = len(annot_files)\n    i = 0\n    \n    while True:\n        # Create empty arrays to store the image data and annotations for this batch\n        batch_imgs = np.zeros((batch_size, img_size[0], img_size[1], 3))\n        batch_coords = np.zeros((batch_size, 4))\n        batch_classes = np.zeros((batch_size, num_classes))\n        \n        for j in range(batch_size):\n            # Parse the annotation for the image\n            img_path = os.path.join(img_dir, annot_files[i].split('.')[0] + '.JPEG')\n            img_arr, coords, classes = parse_annotation(os.path.join(img_dir, annot_files[i]), img_path)\n            \n            # Append the data to the batches\n            batch_imgs[j] = img_arr\n            batch_coords[j] = coords\n            batch_classes[j] = classes\n            \n            # Increment the index of the current annotation\n            i = (i + 1) % n\n        \n        yield {'input': batch_imgs}, {'coords': batch_coords, 'classes': batch_classes}\n","metadata":{"execution":{"iopub.status.busy":"2023-05-02T18:57:09.534426Z","iopub.execute_input":"2023-05-02T18:57:09.535784Z","iopub.status.idle":"2023-05-02T18:57:09.543751Z","shell.execute_reply.started":"2023-05-02T18:57:09.535728Z","shell.execute_reply":"2023-05-02T18:57:09.542632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes=1000\ntrain_gen = data_generator(train_annot_path, train_annot_files, batch_size)\nval_gen = data_generator(val_annot_path, val_annot_files, batch_size)\nresnet = ResNet50(include_top=False, weights='/kaggle/input/models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', input_shape=(224, 224, 3))\n\nfor layer in resnet.layers:\n    layer.trainable = False\nout = resnet.output\nout = tf.keras.layers.GlobalAveragePooling2D()(out)\nout_coords = tf.keras.layers.Dense(4, activation='sigmoid', name='coords')(out)\nout_classes = tf.keras.layers.Dense(num_classes, activation='softmax', name='classes')(out)\nmodel = tf.keras.models.Model(inputs=resnet.input, outputs=[out_coords, out_classes])\nmodel.compile(optimizer='adam', loss={'coords': 'mse', 'classes': 'categorical_crossentropy'}, metrics={'coords': 'mae', 'classes': 'accuracy'})","metadata":{"execution":{"iopub.status.busy":"2023-05-02T19:04:48.75349Z","iopub.execute_input":"2023-05-02T19:04:48.755586Z","iopub.status.idle":"2023-05-02T19:04:51.908536Z","shell.execute_reply.started":"2023-05-02T19:04:48.755537Z","shell.execute_reply":"2023-05-02T19:04:51.907473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('my_model.h5')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-02T19:07:51.39002Z","iopub.execute_input":"2023-05-02T19:07:51.390466Z","iopub.status.idle":"2023-05-02T19:07:52.335808Z","shell.execute_reply.started":"2023-05-02T19:07:51.390431Z","shell.execute_reply":"2023-05-02T19:07:52.334682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_annotationH(annot_path, img_dir):\n    \"\"\"\n    Parses the annotation XML file for an image and returns the object coordinates and class.\n    \"\"\"\n    # Parse the XML file\n    tree = ET.parse(annot_path)\n    root = tree.getroot()\n\n    # Get the object coordinates and class\n    coords = []\n    classes = []\n    for obj in root.findall('object'):\n        class_name = obj.find('name').text\n        class_id = class_names.index(class_name)\n        bbox = obj.find('bndbox')\n        xmin = int(bbox.find('xmin').text)\n        ymin = int(bbox.find('ymin').text)\n        xmax = int(bbox.find('xmax').text)\n        ymax = int(bbox.find('ymax').text)\n        coords.append((xmin, ymin, xmax, ymax))\n        classes.append(class_id)\n\n    # Load the image\n    img_filename = root.find('filename').text\n    img_path = os.path.join(img_dir, img_filename)\n    img_arr = cv2.imread(img_path)\n    img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)\n\n    return img_arr, coords, classes\n","metadata":{"execution":{"iopub.status.busy":"2023-05-02T19:52:20.766915Z","iopub.execute_input":"2023-05-02T19:52:20.767344Z","iopub.status.idle":"2023-05-02T19:52:20.775715Z","shell.execute_reply.started":"2023-05-02T19:52:20.767309Z","shell.execute_reply":"2023-05-02T19:52:20.774738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_generatorH(img_dir, annot_files, batch_size):\n    \"\"\"\n    Returns a generator that generates batches of images and annotations.\n    \"\"\"\n    n = len(annot_files)\n    i = 0\n    \n    while True:\n        # Create empty arrays to store the image data and annotations for this batch\n        batch_imgs = np.zeros((batch_size, img_size[0], img_size[1], 3))\n        batch_coords = np.zeros((batch_size, 4))\n        batch_classes = np.zeros((batch_size, num_classes))\n        \n        for j in range(batch_size):\n            # Parse the annotation for the image\n            img_path = os.path.join(img_dir, annot_files[i].split('.')[0] + '.JPEG')\n            img_arr, coords, classes = parse_annotationH(os.path.join(img_dir, annot_files[i]), img_path)\n            \n            # Append the data to the batches\n            batch_imgs[j] = img_arr\n            batch_coords[j] = coords\n            batch_classes[j] = classes\n            \n            # Increment the index of the current annotation\n            i = (i + 1) % n\n        \n        yield {'input': batch_imgs}, {'coords': batch_coords, 'classes': batch_classes}\n","metadata":{"execution":{"iopub.status.busy":"2023-05-02T19:52:21.290196Z","iopub.execute_input":"2023-05-02T19:52:21.290603Z","iopub.status.idle":"2023-05-02T19:52:21.298653Z","shell.execute_reply.started":"2023-05-02T19:52:21.290574Z","shell.execute_reply":"2023-05-02T19:52:21.297413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom tensorflow.keras.models import load_model\n\n# Load the class index file\nwith open(\"/kaggle/input/index/imagenet_class_index.json\") as f:\n    class_index = json.load(f)\n\n# Get a list of class names\nclass_names = [class_index[str(i)][1] for i in range(len(class_index))]\n\n# Load the test annotations file\ntest_annot_file = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/ImageSets/CLS-LOC/test.txt\"\nwith open(test_annot_file, \"r\") as f:\n    test_annot_files = f.readlines()\n\n# Remove newline characters from the file paths\ntest_annot_files = [f.strip() for f in test_annot_files]\n\n# Define the test image directory path\ntest_img_dir = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test/\"\n\n# Load the model\nmodel_path = \"my_model.h5\"\nmodel = load_model(model_path)\n\n# Define the batch size for inference\nbatch_size = 32\n\n# Evaluate the model on test images\ntest_gen = data_generatorH(test_img_dir, test_annot_files, batch_size)\ntest_loss, test_coords_loss, test_classes_loss, test_coords_mae, test_classes_acc = model.evaluate(test_gen)\n\n# Print the evaluation results\nprint(f\"Test loss: {test_loss:.4f}\")\nprint(f\"Test coords loss: {test_coords_loss:.4f}\")\nprint(f\"Test classes loss: {test_classes_loss:.4f}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-05-02T19:52:21.665147Z","iopub.execute_input":"2023-05-02T19:52:21.665599Z","iopub.status.idle":"2023-05-02T19:52:23.943245Z","shell.execute_reply.started":"2023-05-02T19:52:21.665557Z","shell.execute_reply":"2023-05-02T19:52:23.941954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}