{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"e3a8f578-7cd6-4da6-a97e-48c6dd4486d6","cell_type":"markdown","source":"# PIXEL - Video demo kỹ thuật\n\nNotebook này chỉ chạy **preprocessing + inference + channel occlusion** trên một mẫu thuộc fixed test split. Không huấn luyện lại mô hình và không thực thi malware binary.","metadata":{}},{"id":"cf4bbd87-7364-4f07-b087-110f161c8d95","cell_type":"code","source":"from pathlib import Path\nimport json, sys\n\nSOURCE_ROOT_OVERRIDE = None  # Set this only if source/ is mounted elsewhere.\ncandidates = [Path.cwd(), Path('/kaggle/working/source')]\nif SOURCE_ROOT_OVERRIDE:\n    candidates.insert(0, Path(SOURCE_ROOT_OVERRIDE))\nif Path('/kaggle/input').is_dir():\n    candidates.extend(path.parent for path in Path('/kaggle/input').glob('*/source/pixel_demo'))\n    candidates.extend(path.parent for path in Path('/kaggle/input').glob('*/*/source/pixel_demo'))\n    candidates.extend(path.parent for path in Path('/kaggle/input').glob('*/*/*/source/pixel_demo'))\n    candidates.extend(path.parent for path in Path('/kaggle/input').rglob('pixel_demo') if (path / 'cli.py').is_file())\nSOURCE_ROOT = next((path for path in candidates if (path / 'pixel_demo').is_dir()), None)\nif SOURCE_ROOT is None:\n    raise FileNotFoundError('Attach the source package or copy source/ to /kaggle/working/source')\nsys.path.insert(0, str(SOURCE_ROOT))\nCONFIG_PATH = SOURCE_ROOT / 'config' / 'demo_kaggle.json'\nprint('Source root:', SOURCE_ROOT)\nprint('Config:', CONFIG_PATH)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T11:08:37.210038Z","iopub.execute_input":"2026-07-21T11:08:37.210676Z","iopub.status.idle":"2026-07-21T11:10:48.897820Z","shell.execute_reply.started":"2026-07-21T11:08:37.210629Z","shell.execute_reply":"2026-07-21T11:10:48.897127Z"}},"outputs":[],"execution_count":null},{"id":"51a83132-286d-4b07-897e-5af05e7873fe","cell_type":"markdown","source":"## 1. Preflight\nCell này kiểm tra GPU, dependency, dataset, split và SHA-256 checkpoint trước khi quay.","metadata":{}},{"id":"1df2f518-8c90-4a3e-bed5-9d293c5578df","cell_type":"code","source":"from pixel_demo.cli import load_config, preflight\nconfig = load_config(CONFIG_PATH)\ncheck = preflight(config)\nprint(json.dumps(check, indent=2))\nassert check['ready'], 'Preflight failed; fix every false item before recording.'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T11:20:15.826800Z","iopub.execute_input":"2026-07-21T11:20:15.827509Z","iopub.status.idle":"2026-07-21T11:20:21.825722Z","shell.execute_reply.started":"2026-07-21T11:20:15.827478Z","shell.execute_reply":"2026-07-21T11:20:21.824983Z"}},"outputs":[],"execution_count":null},{"id":"86e9c9fb-3c09-41e3-a3d4-b32e62ba8867","cell_type":"markdown","source":"## 2. Chọn một mẫu test cố định\nMẫu được lấy từ split seed 42 và phải có cả raw `.bytes` lẫn ảnh PPS tham chiếu.","metadata":{}},{"id":"2388fb70-e8ef-4162-b5f0-dc30632ee9a0","cell_type":"code","source":"from pixel_demo.cli import select_test_sample\nselected = select_test_sample(config)\nprint(json.dumps(selected, indent=2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T11:21:35.950649Z","iopub.execute_input":"2026-07-21T11:21:35.951126Z","iopub.status.idle":"2026-07-21T11:21:35.992938Z","shell.execute_reply.started":"2026-07-21T11:21:35.951094Z","shell.execute_reply":"2026-07-21T11:21:35.992320Z"}},"outputs":[],"execution_count":null},{"id":"bf65d389-e69f-4c81-83bd-9980be206e06","cell_type":"markdown","source":"## 3. `.bytes → R/G/B → PPS → prediction`\nKết quả occlusion dùng xác suất của **lớp đúng**, đúng với định nghĩa trong paper.","metadata":{}},{"id":"1284db35-5885-49f2-b888-174f469eb607","cell_type":"code","source":"from pixel_demo.cli import run_demo\nresult = run_demo(config, sample_id=selected['sample_id'])\nprediction = result['prediction']\nprint('Predicted:', prediction['predicted_family'])\nprint('True family:', prediction['target_family'])\nprint('Correct:', prediction['correct'])\nprint('Top-3:')\nfor row in prediction['top_predictions']:\n    print(f\"  {row['rank']}. {row['family']}: {row['probability']:.4f}\")\nprint('True-class confidence drops:', {k: round(v, 4) for k, v in prediction['occlusion'].items() if k.startswith('delta_')})\nprint('Generated PPS matches saved reference:', result.get('reference_comparison', {}).get('exact_match'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T11:22:31.338773Z","iopub.execute_input":"2026-07-21T11:22:31.339043Z","iopub.status.idle":"2026-07-21T11:22:49.074044Z","shell.execute_reply.started":"2026-07-21T11:22:31.339020Z","shell.execute_reply":"2026-07-21T11:22:49.073180Z"}},"outputs":[],"execution_count":null},{"id":"bf74378f-2486-40c6-b181-3bb4c8fa3e59","cell_type":"code","source":"from IPython.display import Image, display\ndisplay(Image(filename=result['artifacts']['overview']))\nprint('Full machine-readable result:', Path(config['output_dir']) / 'demo_result.json')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T11:26:52.227290Z","iopub.execute_input":"2026-07-21T11:26:52.227951Z","iopub.status.idle":"2026-07-21T11:26:52.256184Z","shell.execute_reply.started":"2026-07-21T11:26:52.227918Z","shell.execute_reply":"2026-07-21T11:26:52.255267Z"}},"outputs":[],"execution_count":null},{"id":"ea4c1f53-c0fc-44b6-8f05-aeb5d432b8de","cell_type":"markdown","source":"## 4. Kết quả tổng hợp đã audit\n- BIG-2015 ResNet-50 F1-Macro: **95.58% grayscale → 97.97% PPS (+2.39 pp)**.\n- Full PPS chỉ hơn R đơn lẻ **0.18 pp**, nên không claim universal superiority.\n- PPS preprocessing: **2558.47 ± 1925.10 ms/sample**; ResNet-50 forward batch 1 gần như không đổi giữa grayscale/PPS (**10.02 vs 10.08 ms/sample**).\n- Occlusion và Grad-CAM là bằng chứng attribution, không phải chứng minh cơ chế nhân quả.","metadata":{}},{"id":"2e0f5eef-ec6b-4f8e-9daf-fdac88a31526","cell_type":"code","source":"for filename in ['channel_attribution_heatmap.png', 'entropy_attention_correlation.png']:\n    path = SOURCE_ROOT / 'results' / filename\n    if path.is_file():\n        display(Image(filename=str(path)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T11:27:30.969303Z","iopub.execute_input":"2026-07-21T11:27:30.970067Z","iopub.status.idle":"2026-07-21T11:27:31.037286Z","shell.execute_reply.started":"2026-07-21T11:27:30.970035Z","shell.execute_reply":"2026-07-21T11:27:31.036729Z"}},"outputs":[],"execution_count":null}]}