product-detection/2_product-classifier.ipynb

1 line
1.6 MiB
Plaintext
Raw Permalink Normal View History

2026-06-03 11:07:23 +02:00
{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"datasetVersion","sourceId":16263031,"datasetId":10367968,"databundleVersionId":17247117}],"dockerImageVersionId":31329,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":5,"nbformat":4,"cells":[{"id":"a1b2c3d4","cell_type":"markdown","source":"# 🛒 Product Classifier — Training Notebook\n\nTrains a lightweight **EfficientNet-B0** (ImageNet pre-trained) on shelf product crops\ngenerated by `1_generate_crop_dataset.py`.\n\n---\n\n### ✅ Before you run\n1. **Runtime → Change runtime type → GPU** (T4 is free and enough for this)\n2. Upload your `crops_dataset/` as a Kaggle Dataset and attach it via **+ Add Data**\n3. Update `DATA_DIR` in **Cell 2 — Configuration** to match your dataset path\n4. Run all cells top-to-bottom\n\n### 📂 Expected dataset layout\n```\n/kaggle/input/your-dataset-name/\n train/\n cola_can/ img001.jpg …\n pepsi_can/ img002.jpg …\n lays_chips/ img003.jpg …\n val/\n cola_can/ …\n …\n```\n\n### 📦 Outputs saved to `/kaggle/working/runs/classify/`\n| File | Description |\n|---|---|\n| `best.pt` | Best checkpoint (highest val accuracy) |\n| `last.pt` | Last epoch checkpoint |\n| `class_names.json` | `{idx: class_name}` mapping for inference |\n| `training_curves.png` | Loss & accuracy plots |","metadata":{}},{"id":"b2c3d4e5","cell_type":"code","source":"# ── Cell 1 — Install / verify dependencies ────────────────────────────────────\n# Kaggle kernels ship with torch & torchvision pre-installed.\n# We only need tqdm (already present) — nothing extra to install.\n\nimport sys, subprocess\n\ndef run(cmd):\n subprocess.run(cmd, shell=True, check=True)\n\n# Verify GPU is visible\nimport torch\nprint(f\"PyTorch : {torch.__version__}\")\nprint(f\"CUDA : {torch.version.cuda}\")\nprint(f\"GPU : {torch.cuda.get_device_name(0) if torch.cuda.is_available() else '⚠ NOT available — check runtime settings'}\")\nprint(f\"VRAM : {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\" if torch.cuda.is_available() else \"\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-14T12:15:41.201232Z","iopub.execute_input":"2026-05-14T12:15:41.201909Z","iopub.status.idle":"2026-05-14T12:15:46.397019Z","shell.execute_reply.started":"2026-05-14T12:15:41.201854Z","shell.execute_reply":"2026-05-14T12:15:46.396084Z"}},"outputs":[{"name":"stdout","text":"PyTorch : 2.10.0+cu128\nCUDA : 12.8\nGPU : Tesla T4\nVRAM : 15.6 GB\n","output_type":"stream"}],"execution_count":1},{"id":"c3d4e5f6","cell_type":"code","source":"# ── Cell 2 — Configuration (edit this cell) ──────────────────────────────────\nfrom pathlib import Path\n\n# ── Paths ──────────────────────────────────────────────────────────────────────\n# Point this to the root of your attached dataset (the folder that contains train/ and val/)\nDATA_DIR = Path(\"/kaggle/input/datasets/triztechdata/product-dataset/crops_dataset\") # ← CHANGE THIS\nOUTPUT_DIR = Path(\"/kaggle/working/runs/classify\")\n\n# ── Model ──────────────────────────────────────────────────────────────────────\n# Choices: \"efficientnet_b0\" | \"efficientnet_b2\" | \"mobilenet_v3_small\" | \"resnet50\"\nMODEL_NAME =