{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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":[{"sourceId":6799,"databundleVersionId":4225553,"sourceType":"competition"}],"dockerImageVersionId":31236,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-03T12:46:50.57183Z","iopub.execute_input":"2026-01-03T12:46:50.572289Z","iopub.status.idle":"2026-01-03T12:46:50.576767Z","shell.execute_reply.started":"2026-01-03T12:46:50.572264Z","shell.execute_reply":"2026-01-03T12:46:50.575901Z"}},"outputs":[],"execution_count":3},{"cell_type":"code","source":"import os\n\n# EXEMPLES (à adapter selon ton dataset Kaggle):\n# DATA_DIR = \"/kaggle/input/imagenet-1k/imagenet\" \n# DATA_DIR = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC\"\nDATA_DIR = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC\"\n\nprint(\"Train exists:\", os.path.isdir(os.path.join(DATA_DIR, \"train\")))\nprint(\"Val exists:\", os.path.isdir(os.path.join(DATA_DIR, \"val\")))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-03T14:21:50.257936Z","iopub.execute_input":"2026-01-03T14:21:50.25823Z","iopub.status.idle":"2026-01-03T14:21:50.281503Z","shell.execute_reply.started":"2026-01-03T14:21:50.258196Z","shell.execute_reply":"2026-01-03T14:21:50.280875Z"}},"outputs":[{"name":"stdout","text":"Train exists: True\nVal exists: True\n","output_type":"stream"}],"execution_count":1},{"cell_type":"code","source":"import math, time\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import datasets, transforms\n\nimport timm\n\n# -----------------------\n# Config Kaggle\n# -----------------------\nMODEL_NAME = \"efficientformer_l1\"   # change: efficientformer_l3, etc (si dispo)\nIMG_SIZE = 224\nEPOCHS = 10  # mets 300 pour full training\nBATCH_SIZE = 64  # adapte selon GPU\nGRAD_ACCUM = 16  # pour simuler gros batch (effective = 64*16 = 1024)\nNUM_WORKERS = 4\n\nWEIGHT_DECAY = 0.05\nWARMUP_EPOCHS = 5\nMIN_LR = 1e-5\nLABEL_SMOOTHING = 0.1\n\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nAMP = (DEVICE == \"cuda\")\n\neffective_batch = BATCH_SIZE * GRAD_ACCUM\nLR = 1e-3 * (effective_batch / 1024.0)\n\nprint(\"Device:\", DEVICE)\nprint(\"Effective batch:\", effective_batch, \"| LR:\", LR)\n\n# -----------------------\n# Data\n# -----------------------\nnormalize = transforms.Normalize(mean=(0.485, 0.456, 0.406),\n                                 std=(0.229, 0.224, 0.225))\n\ntrain_tfms = transforms.Compose([\n    transforms.RandomResizedCrop(IMG_SIZE, scale=(0.08, 1.0), interpolation=transforms.InterpolationMode.BICUBIC),\n    transforms.RandomHorizontalFlip(),\n    transforms.ToTensor(),\n    normalize,\n])\n\nval_tfms = transforms.Compose([\n    transforms.Resize(int(IMG_SIZE * 256 / 224), interpolation=transforms.InterpolationMode.BICUBIC),\n    transforms.CenterCrop(IMG_SIZE),\n    transforms.ToTensor(),\n    normalize,\n])\n\ntrain_ds = datasets.ImageFolder(os.path.join(DATA_DIR, \"train\"), transform=train_tfms)\nval_ds   = datasets.ImageFolder(os.path.join(DATA_DIR, \"val\"),   transform=val_tfms)\n\ntrain_loader = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True,\n                          num_workers=NUM_WORKERS, pin_memory=True, drop_last=True)\nval_loader = DataLoader(val_ds, batch_size=BATCH_SIZE, shuffle=False,\n                        num_workers=NUM_WORKERS, pin_memory=True)\n\n# -----------------------\n# Model\n# -----------------------\nmodel = timm.create_model(MODEL_NAME, pretrained=False, num_classes=1000)\nmodel.to(DEVICE)\n\n# Loss\nloss_fn = nn.CrossEntropyLoss(label_smoothing=LABEL_SMOOTHING)\n\n# Optim\noptimizer = optim.AdamW(model.parameters(), lr=LR, weight_decay=WEIGHT_DECAY)\n\n# Scheduler (warmup + cosine)\nsteps_per_epoch = len(train_loader)\ntotal_steps = EPOCHS * steps_per_epoch\nwarmup_steps = WARMUP_EPOCHS * steps_per_epoch\n\ndef lr_lambda(step):\n    if step < warmup_steps:\n        return step / max(1, warmup_steps)\n    progress = (step - warmup_steps) / max(1, total_steps - warmup_steps)\n    cosine = 0.5 * (1.0 + math.cos(math.pi * progress))\n    return (MIN_LR / LR) + (1.0 - (MIN_LR / LR)) * cosine\n\nscheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda)\n\nscaler = torch.cuda.amp.GradScaler(enabled=AMP)\n\n@torch.no_grad()\ndef evaluate():\n    model.eval()\n    total, correct, total_loss = 0, 0, 0.0\n    for x, y in val_loader:\n        x, y = x.to(DEVICE, non_blocking=True), y.to(DEVICE, non_blocking=True)\n        with torch.autocast(device_type=\"cuda\", dtype=torch.float16, enabled=AMP):\n            logits = model(x)\n            loss = loss_fn(logits, y)\n        total_loss += loss.item() * x.size(0)\n        pred = logits.argmax(1)\n        correct += (pred == y).sum().item()\n        total += x.size(0)\n    return total_loss / total, 100.0 * correct / total\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-03T12:49:15.96943Z","iopub.execute_input":"2026-01-03T12:49:15.969979Z","execution_failed":"2026-01-03T14:16:29.863Z"}},"outputs":[{"name":"stdout","text":"Device: cuda\nEffective batch: 1024 | LR: 0.001\n","output_type":"stream"}],"execution_count":null},{"cell_type":"code","source":"best_acc = 0.0\nglobal_step = 0\n\nfor epoch in range(EPOCHS):\n    model.train()\n    t0 = time.time()\n    optimizer.zero_grad(set_to_none=True)\n\n    for i, (x, y) in enumerate(train_loader):\n        x, y = x.to(DEVICE, non_blocking=True), y.to(DEVICE, non_blocking=True)\n\n        with torch.autocast(device_type=\"cuda\", dtype=torch.float16, enabled=AMP):\n            logits = model(x)\n            loss = loss_fn(logits, y) / GRAD_ACCUM\n\n        scaler.scale(loss).backward()\n\n        if (i + 1) % GRAD_ACCUM == 0:\n            scaler.step(optimizer)\n            scaler.update()\n            optimizer.zero_grad(set_to_none=True)\n            scheduler.step()\n            global_step += 1\n\n        if (i + 1) % 200 == 0:\n            lr_now = optimizer.param_groups[0][\"lr\"]\n            print(f\"Epoch {epoch+1}/{EPOCHS} | iter {i+1}/{len(train_loader)} | lr {lr_now:.6g}\")\n\n    val_loss, val_acc = evaluate()\n    dt = time.time() - t0\n    print(f\"[VAL] epoch={epoch+1} loss={val_loss:.4f} acc@1={val_acc:.2f}% time={dt:.1f}s\")\n\n    if val_acc > best_acc:\n        best_acc = val_acc\n        torch.save({\"model\": model.state_dict(), \"acc\": best_acc, \"epoch\": epoch+1},\n                   \"/kaggle/working/best.pth\")\n        print(\"✅ Saved /kaggle/working/best.pth\")\n\nprint(\"Done. Best acc@1:\", best_acc)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2026-01-03T14:16:29.864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-03T12:46:53.083079Z","iopub.execute_input":"2026-01-03T12:46:53.083363Z","iopub.status.idle":"2026-01-03T12:46:53.088485Z","shell.execute_reply.started":"2026-01-03T12:46:53.083342Z","shell.execute_reply":"2026-01-03T12:46:53.08775Z"}},"outputs":[],"execution_count":4},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-03T12:47:18.187584Z","iopub.execute_input":"2026-01-03T12:47:18.188365Z","iopub.status.idle":"2026-01-03T12:47:18.205738Z","shell.execute_reply.started":"2026-01-03T12:47:18.188336Z","shell.execute_reply":"2026-01-03T12:47:18.20486Z"}},"outputs":[],"execution_count":8},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}