{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport torch\nimport os\nimport zipfile","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:03:34.278745Z","iopub.execute_input":"2026-07-29T13:03:34.279011Z","iopub.status.idle":"2026-07-29T13:03:38.988761Z","shell.execute_reply.started":"2026-07-29T13:03:34.278989Z","shell.execute_reply":"2026-07-29T13:03:38.988035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nfrom torch import nn\nfrom fastai import *\nfrom fastai.vision.all import *","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:03:38.990601Z","iopub.execute_input":"2026-07-29T13:03:38.991066Z","iopub.status.idle":"2026-07-29T13:03:49.662125Z","shell.execute_reply.started":"2026-07-29T13:03:38.991042Z","shell.execute_reply":"2026-07-29T13:03:49.661473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"zip_path = Path(\"/kaggle/input/competitions/dogs-vs-cats-redux-kernels-edition\")\npath = Path('/kaggle/working/dogscats')\npath.mkdir(exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:03:49.663070Z","iopub.execute_input":"2026-07-29T13:03:49.664023Z","iopub.status.idle":"2026-07-29T13:03:49.669205Z","shell.execute_reply.started":"2026-07-29T13:03:49.663971Z","shell.execute_reply":"2026-07-29T13:03:49.668362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"zip_path.ls()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:03:49.670176Z","iopub.execute_input":"2026-07-29T13:03:49.670446Z","iopub.status.idle":"2026-07-29T13:03:49.687037Z","shell.execute_reply.started":"2026-07-29T13:03:49.670419Z","shell.execute_reply":"2026-07-29T13:03:49.686218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trn_zp = zip_path/'train.zip'\ntst_zp = zip_path/'test.zip'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:03:49.688022Z","iopub.execute_input":"2026-07-29T13:03:49.688374Z","iopub.status.idle":"2026-07-29T13:03:49.694650Z","shell.execute_reply.started":"2026-07-29T13:03:49.688327Z","shell.execute_reply":"2026-07-29T13:03:49.694001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with zipfile.ZipFile(trn_zp, 'r') as z:\n    z.extractall(path/'train')\n\nwith zipfile.ZipFile(tst_zp, 'r') as z:\n    z.extractall(path/'test')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:03:49.695579Z","iopub.execute_input":"2026-07-29T13:03:49.696105Z","iopub.status.idle":"2026-07-29T13:04:03.580025Z","shell.execute_reply.started":"2026-07-29T13:03:49.696074Z","shell.execute_reply":"2026-07-29T13:04:03.579423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path.ls()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:04:03.582326Z","iopub.execute_input":"2026-07-29T13:04:03.582564Z","iopub.status.idle":"2026-07-29T13:04:03.588390Z","shell.execute_reply.started":"2026-07-29T13:04:03.582538Z","shell.execute_reply":"2026-07-29T13:04:03.587507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def label_func(x): return 'cat' if x.name.startswith('cat') else 'dog'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:04:03.589352Z","iopub.execute_input":"2026-07-29T13:04:03.589669Z","iopub.status.idle":"2026-07-29T13:04:03.600784Z","shell.execute_reply.started":"2026-07-29T13:04:03.589641Z","shell.execute_reply":"2026-07-29T13:04:03.599938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dblock = DataBlock(\n    blocks=(ImageBlock, CategoryBlock),\n    get_items=get_image_files,\n    splitter=RandomSplitter(valid_pct=0.2, seed=42),\n    get_y=label_func,\n    item_tfms=Resize(224)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:04:03.601650Z","iopub.execute_input":"2026-07-29T13:04:03.601962Z","iopub.status.idle":"2026-07-29T13:04:03.614939Z","shell.execute_reply.started":"2026-07-29T13:04:03.601916Z","shell.execute_reply":"2026-07-29T13:04:03.614141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls = dblock.dataloaders(path/'train', bs=64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:04:03.615998Z","iopub.execute_input":"2026-07-29T13:04:03.616291Z","iopub.status.idle":"2026-07-29T13:04:04.626326Z","shell.execute_reply.started":"2026-07-29T13:04:03.616251Z","shell.execute_reply":"2026-07-29T13:04:04.625418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls.show_batch(max_n=9, nrows=3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:04:04.627254Z","iopub.execute_input":"2026-07-29T13:04:04.627756Z","iopub.status.idle":"2026-07-29T13:04:05.688908Z","shell.execute_reply.started":"2026-07-29T13:04:04.627679Z","shell.execute_reply":"2026-07-29T13:04:05.687991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xb, yb = dls.one_batch()\nxb.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:04:05.689902Z","iopub.execute_input":"2026-07-29T13:04:05.690179Z","iopub.status.idle":"2026-07-29T13:04:05.962552Z","shell.execute_reply.started":"2026-07-29T13:04:05.690149Z","shell.execute_reply":"2026-07-29T13:04:05.961605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn = vision_learner(dls, resnet34, metrics=accuracy)\nlearn.fine_tune(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:04:05.963682Z","iopub.execute_input":"2026-07-29T13:04:05.964093Z","iopub.status.idle":"2026-07-29T13:11:52.663284Z","shell.execute_reply.started":"2026-07-29T13:04:05.964062Z","shell.execute_reply":"2026-07-29T13:11:52.662581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_files = get_image_files(path/'test').sorted()\ntest_dl = dls.test_dl(test_files)\npreds, _, decoded = learn.get_preds(dl=test_dl, with_decoded=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:11:52.664705Z","iopub.execute_input":"2026-07-29T13:11:52.664997Z","iopub.status.idle":"2026-07-29T13:12:16.538679Z","shell.execute_reply.started":"2026-07-29T13:11:52.664963Z","shell.execute_reply":"2026-07-29T13:12:16.537630Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.read_csv(zip_path/'sample_submission.csv')\nsubmission['label'] = decoded.numpy()\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:12:16.540289Z","iopub.execute_input":"2026-07-29T13:12:16.540634Z","iopub.status.idle":"2026-07-29T13:12:16.575233Z","shell.execute_reply.started":"2026-07-29T13:12:16.540596Z","shell.execute_reply":"2026-07-29T13:12:16.574703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds, targs = learn.get_preds(dl=test_dl)\nprobs = preds[:, 1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:16:22.122081Z","iopub.execute_input":"2026-07-29T13:16:22.123069Z","iopub.status.idle":"2026-07-29T13:16:47.773199Z","shell.execute_reply.started":"2026-07-29T13:16:22.123004Z","shell.execute_reply":"2026-07-29T13:16:47.772321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.read_csv(zip_path/'sample_submission.csv')\nsubmission['label'] = probs.numpy()\nsubmission.to_csv('submission_prob.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-29T13:16:54.110281Z","iopub.execute_input":"2026-07-29T13:16:54.111344Z","iopub.status.idle":"2026-07-29T13:16:54.150511Z","shell.execute_reply.started":"2026-07-29T13:16:54.111277Z","shell.execute_reply":"2026-07-29T13:16:54.149784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}