{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":6799,"databundleVersionId":4225553,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!ls /kaggle/input","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T16:41:24.550077Z","iopub.execute_input":"2025-07-16T16:41:24.55039Z","iopub.status.idle":"2025-07-16T16:41:24.684805Z","shell.execute_reply.started":"2025-07-16T16:41:24.550368Z","shell.execute_reply":"2025-07-16T16:41:24.683101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os \nimport csv\nfrom PIL import Image\nfrom tqdm.notebook import tqdm\n\ndata_root = '/kaggle/input/imagenet-object-localization-challenge'\ntarget_root = \"./dataset\"\nos.makedirs(target_root, exist_ok=True) \n\n# Read synset mappings\nwith open(os.path.join(data_root, \"LOC_synset_mapping.txt\")) as f:\n    mapping_file = f.read()\n\n# Extract ids and names\nsynsets = [line.lower().split()[0] for line in mapping_file.split(\"\\n\") if line]\nnames = [line.lower().split(maxsplit=1)[1] for line in mapping_file.split(\"\\n\") if line]\nnames = [name.split(',')[0].replace(\" \", \"_\") for name in names]\n\nannotations = {synset : [] for synset in synsets}\nwith open(os.path.join(data_root, \"LOC_val_solution.csv\")) as csvfile:\n    reader = csv.reader(csvfile)\n    next(reader)  # skip header\n    for image_id, pred_str in reader:\n        tokens = pred_str.strip().split()\n        # Process each group of 5 tokens\n        for i in range(0, len(tokens), 5):\n            synset = tokens[i]\n            coords = tuple(map(int, tokens[i+1:i+5]))\n            annotations[synset].append((image_id, coords))\n        \n\n# Iterate through each ID and crop first N files\nfiles_per_class = 100\nfor i, (synset, name) in enumerate(zip(tqdm(synsets, desc=\"Processing synstes\"), names)):\n    target_dir = os.path.join(target_root, str(i))\n    os.makedirs(target_dir, exist_ok=True)\n\n    for j, (file_id, bndbox) in enumerate(annotations[synset]):\n        if j >= files_per_class:\n            continue \n        src_image_path = os.path.join(data_root, \"ILSVRC/Data/CLS-LOC/val\", f\"{file_id}.JPEG\")\n        target_image_path = os.path.join(target_dir, f\"{j}.JPEG\")\n        \n        # Get bounding box \n        xmin, ymin, xmax, ymax = bndbox\n\n        # Crop and save the image\n        image_src = Image.open(src_image_path)\n        image_cropped = image_src.crop((xmin, ymin, xmax, ymax))\n        image_resized = image_cropped.resize((64, 64))\n        image_resized.save(target_image_path)\n\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T17:24:30.163623Z","iopub.execute_input":"2025-07-16T17:24:30.164134Z","iopub.status.idle":"2025-07-16T17:24:33.920211Z","shell.execute_reply.started":"2025-07-16T17:24:30.164104Z","shell.execute_reply":"2025-07-16T17:24:33.918961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!tar -czf dataset.tar.gz dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T17:24:36.24365Z","iopub.execute_input":"2025-07-16T17:24:36.244006Z","iopub.status.idle":"2025-07-16T17:24:56.75419Z","shell.execute_reply.started":"2025-07-16T17:24:36.243982Z","shell.execute_reply":"2025-07-16T17:24:56.752574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import FileLink\n\nFileLink(r'dataset.tar.gz')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T16:46:12.693979Z","iopub.execute_input":"2025-07-16T16:46:12.694364Z","iopub.status.idle":"2025-07-16T16:46:12.703272Z","shell.execute_reply.started":"2025-07-16T16:46:12.694316Z","shell.execute_reply":"2025-07-16T16:46:12.702048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!rm -r dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T16:39:54.858232Z","iopub.execute_input":"2025-07-16T16:39:54.858599Z","iopub.status.idle":"2025-07-16T16:39:54.984486Z","shell.execute_reply.started":"2025-07-16T16:39:54.858569Z","shell.execute_reply":"2025-07-16T16:39:54.983181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T16:47:08.636688Z","iopub.execute_input":"2025-07-16T16:47:08.637056Z","iopub.status.idle":"2025-07-16T16:47:08.650814Z","shell.execute_reply.started":"2025-07-16T16:47:08.637034Z","shell.execute_reply":"2025-07-16T16:47:08.649798Z"}},"outputs":[],"execution_count":null}]}