{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#about torch...\nimport torch\nimport torch.nn as nn\nimport torch\nimport torch.optim as optim\nimport torch.nn.functional as F\n\nfrom torchvision import datasets, models, transforms\nfrom torch.utils.data import DataLoader, Dataset\n\n#using numpy\nimport numpy as np\n\n#for data load or save\nimport pandas as pd\n\n#visualize some datasets\nimport matplotlib.pyplot as plt\n\n#check our work directory\nimport os\n\n#to unzip datasets\nimport zipfile","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-06T07:04:06.573755Z","iopub.execute_input":"2023-07-06T07:04:06.574538Z","iopub.status.idle":"2023-07-06T07:04:08.279214Z","shell.execute_reply.started":"2023-07-06T07:04:06.574485Z","shell.execute_reply":"2023-07-06T07:04:08.277914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/dogs-vs-cats-redux-kernels-edition')","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:08.382245Z","iopub.execute_input":"2023-07-06T07:04:08.382944Z","iopub.status.idle":"2023-07-06T07:04:08.394241Z","shell.execute_reply.started":"2023-07-06T07:04:08.382877Z","shell.execute_reply":"2023-07-06T07:04:08.392583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('../data', exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:08.398368Z","iopub.execute_input":"2023-07-06T07:04:08.398690Z","iopub.status.idle":"2023-07-06T07:04:08.404066Z","shell.execute_reply.started":"2023-07-06T07:04:08.398658Z","shell.execute_reply":"2023-07-06T07:04:08.402889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '../input/dogs-vs-cats-redux-kernels-edition'\ntrain_dir = '../data/train'\ntest_dir = '../data/test'","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:08.408217Z","iopub.execute_input":"2023-07-06T07:04:08.408660Z","iopub.status.idle":"2023-07-06T07:04:08.413890Z","shell.execute_reply.started":"2023-07-06T07:04:08.408553Z","shell.execute_reply":"2023-07-06T07:04:08.412948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(os.path.join(base_dir, 'train.zip')) as train_zip:\n    train_zip.extractall('../data')\n    \nwith zipfile.ZipFile(os.path.join(base_dir, 'test.zip')) as test_zip:\n    test_zip.extractall('../data')","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:08.415495Z","iopub.execute_input":"2023-07-06T07:04:08.416226Z","iopub.status.idle":"2023-07-06T07:04:29.951764Z","shell.execute_reply.started":"2023-07-06T07:04:08.416177Z","shell.execute_reply":"2023-07-06T07:04:29.950655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(train_dir)[:5]","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:29.953928Z","iopub.execute_input":"2023-07-06T07:04:29.954562Z","iopub.status.idle":"2023-07-06T07:04:29.985568Z","shell.execute_reply.started":"2023-07-06T07:04:29.954516Z","shell.execute_reply":"2023-07-06T07:04:29.984309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\ntrain_list = glob.glob(os.path.join(train_dir,'*.jpg'))\ntest_list = glob.glob(os.path.join(test_dir, '*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:29.987441Z","iopub.execute_input":"2023-07-06T07:04:29.987879Z","iopub.status.idle":"2023-07-06T07:04:30.162180Z","shell.execute_reply.started":"2023-07-06T07:04:29.987831Z","shell.execute_reply":"2023-07-06T07:04:30.161073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_list)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:30.163982Z","iopub.execute_input":"2023-07-06T07:04:30.164418Z","iopub.status.idle":"2023-07-06T07:04:30.172701Z","shell.execute_reply.started":"2023-07-06T07:04:30.164365Z","shell.execute_reply":"2023-07-06T07:04:30.171314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dog_list = glob.glob(os.path.join(train_dir,'dog*.jpg'))\ntrain_cat_list = glob.glob(os.path.join(train_dir,'cat*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:30.174607Z","iopub.execute_input":"2023-07-06T07:04:30.175174Z","iopub.status.idle":"2023-07-06T07:04:30.339999Z","shell.execute_reply.started":"2023-07-06T07:04:30.175128Z","shell.execute_reply":"2023-07-06T07:04:30.338937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_dog_list))\nlen((train_cat_list))","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:30.341640Z","iopub.execute_input":"2023-07-06T07:04:30.342084Z","iopub.status.idle":"2023-07-06T07:04:30.350670Z","shell.execute_reply.started":"2023-07-06T07:04:30.342037Z","shell.execute_reply":"2023-07-06T07:04:30.348996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dog = '../data/train/dog'\ntrain_cat = '../data/train/cat'\n\nos.makedirs(train_dog, exist_ok=True)\nos.makedirs(train_cat, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:30.352992Z","iopub.execute_input":"2023-07-06T07:04:30.353662Z","iopub.status.idle":"2023-07-06T07:04:30.361405Z","shell.execute_reply.started":"2023-07-06T07:04:30.353617Z","shell.execute_reply":"2023-07-06T07:04:30.360279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:30.363106Z","iopub.execute_input":"2023-07-06T07:04:30.363736Z","iopub.status.idle":"2023-07-06T07:04:30.373517Z","shell.execute_reply.started":"2023-07-06T07:04:30.363682Z","shell.execute_reply":"2023-07-06T07:04:30.372656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in train_dog_list:\n    shutil.move(file,train_dog)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:30.374980Z","iopub.execute_input":"2023-07-06T07:04:30.375586Z","iopub.status.idle":"2023-07-06T07:04:31.132222Z","shell.execute_reply.started":"2023-07-06T07:04:30.375543Z","shell.execute_reply":"2023-07-06T07:04:31.131193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in train_cat_list:\n    shutil.move(file,train_cat)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:31.134798Z","iopub.execute_input":"2023-07-06T07:04:31.135152Z","iopub.status.idle":"2023-07-06T07:04:31.872023Z","shell.execute_reply.started":"2023-07-06T07:04:31.135119Z","shell.execute_reply":"2023-07-06T07:04:31.871074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_unknown = '../data/test/unknown'","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:31.874811Z","iopub.execute_input":"2023-07-06T07:04:31.875545Z","iopub.status.idle":"2023-07-06T07:04:31.880382Z","shell.execute_reply.started":"2023-07-06T07:04:31.875503Z","shell.execute_reply":"2023-07-06T07:04:31.879437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(test_unknown,exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:31.882004Z","iopub.execute_input":"2023-07-06T07:04:31.882668Z","iopub.status.idle":"2023-07-06T07:04:31.912867Z","shell.execute_reply.started":"2023-07-06T07:04:31.882621Z","shell.execute_reply":"2023-07-06T07:04:31.911779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in test_list:\n    shutil.move(file,test_unknown)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:31.915772Z","iopub.execute_input":"2023-07-06T07:04:31.916771Z","iopub.status.idle":"2023-07-06T07:04:32.646437Z","shell.execute_reply.started":"2023-07-06T07:04:31.916629Z","shell.execute_reply":"2023-07-06T07:04:32.644783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir(train_dog)))\nprint(len(os.listdir(train_cat)))\nprint(len(os.listdir(test_unknown)))","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:32.648084Z","iopub.execute_input":"2023-07-06T07:04:32.648521Z","iopub.status.idle":"2023-07-06T07:04:32.694293Z","shell.execute_reply.started":"2023-07-06T07:04:32.648477Z","shell.execute_reply":"2023-07-06T07:04:32.692971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nimport torchvision.transforms as transforms\nmodel = torchvision.models.resnet50(pretrained = True)\nmodel.fc = torch.nn.Linear(model.fc.in_features,2)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:32.696197Z","iopub.execute_input":"2023-07-06T07:04:32.696672Z","iopub.status.idle":"2023-07-06T07:04:34.257974Z","shell.execute_reply.started":"2023-07-06T07:04:32.696625Z","shell.execute_reply":"2023-07-06T07:04:34.257090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.cuda()","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:34.259777Z","iopub.execute_input":"2023-07-06T07:04:34.260401Z","iopub.status.idle":"2023-07-06T07:04:38.025562Z","shell.execute_reply.started":"2023-07-06T07:04:34.260343Z","shell.execute_reply":"2023-07-06T07:04:38.024668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_labels(dataset):\n    if isinstance(dataset, torch.utils.data.Subset):\n        return get_labels(dataset.dataset)[dataset.indices]\n    else:\n        return np.array([img[1] for img in dataset.imgs])","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:38.027056Z","iopub.execute_input":"2023-07-06T07:04:38.027456Z","iopub.status.idle":"2023-07-06T07:04:38.033728Z","shell.execute_reply.started":"2023-07-06T07:04:38.027407Z","shell.execute_reply":"2023-07-06T07:04:38.032735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedShuffleSplit\n","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:38.035583Z","iopub.execute_input":"2023-07-06T07:04:38.036240Z","iopub.status.idle":"2023-07-06T07:04:39.157964Z","shell.execute_reply.started":"2023-07-06T07:04:38.036195Z","shell.execute_reply":"2023-07-06T07:04:39.155035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setup_train_val_split(labels, dryrun=False, seed=0):\n    x = np.arange(len(labels))\n    y = np.array(labels)\n    splitter = StratifiedShuffleSplit(\n        n_splits=1, train_size=0.8, random_state=seed\n    )\n    train_indices, val_indices = next(splitter.split(x, y))\n\n    if dryrun:\n        train_indices = np.random.choice(train_indices, 100, replace=False)\n        val_indices = np.random.choice(val_indices, 100, replace=False)\n\n    return train_indices, val_indices\n","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:39.166603Z","iopub.execute_input":"2023-07-06T07:04:39.167420Z","iopub.status.idle":"2023-07-06T07:04:39.208213Z","shell.execute_reply.started":"2023-07-06T07:04:39.167368Z","shell.execute_reply":"2023-07-06T07:04:39.199294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setup_train_val_datasets(data_dir, dryrun=False):\n    dataset = torchvision.datasets.ImageFolder(\n        os.path.join(data_dir, \"train\"),\n        transform=setup_center_crop_transform(),\n    )\n    labels = get_labels(dataset)\n    train_indices, val_indices = setup_train_val_split(labels, dryrun)\n\n    train_dataset = torch.utils.data.Subset(dataset, train_indices)\n    val_dataset = torch.utils.data.Subset(dataset, val_indices)\n\n    return train_dataset, val_dataset","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:39.209991Z","iopub.execute_input":"2023-07-06T07:04:39.210511Z","iopub.status.idle":"2023-07-06T07:04:40.097408Z","shell.execute_reply.started":"2023-07-06T07:04:39.210444Z","shell.execute_reply":"2023-07-06T07:04:40.096120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setup_train_val_loaders(data_dir, batch_size, dryrun=False):\n    train_dataset, val_dataset = setup_train_val_datasets(\n        data_dir, dryrun=dryrun\n    )\n    train_loader = torch.utils.data.DataLoader(\n        train_dataset,\n        batch_size=batch_size,\n        shuffle=True,\n        drop_last=True,\n        num_workers=2,\n    )\n    val_loader = torch.utils.data.DataLoader(\n        val_dataset, batch_size=batch_size, num_workers=2\n    )\n    return train_loader, val_loader","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:40.099038Z","iopub.execute_input":"2023-07-06T07:04:40.099569Z","iopub.status.idle":"2023-07-06T07:04:40.152263Z","shell.execute_reply.started":"2023-07-06T07:04:40.099524Z","shell.execute_reply":"2023-07-06T07:04:40.150628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setup_center_crop_transform():\n    return transforms.Compose(\n        [\n            transforms.Resize(256),\n            transforms.CenterCrop(224),\n            transforms.ToTensor(),\n            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),\n        ]\n    )","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:40.156345Z","iopub.execute_input":"2023-07-06T07:04:40.157012Z","iopub.status.idle":"2023-07-06T07:04:40.197869Z","shell.execute_reply.started":"2023-07-06T07:04:40.156951Z","shell.execute_reply":"2023-07-06T07:04:40.196812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader,val_loader = setup_train_val_loaders('/kaggle/data',batch_size = 50,dryrun= False)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:40.204312Z","iopub.execute_input":"2023-07-06T07:04:40.204717Z","iopub.status.idle":"2023-07-06T07:04:40.549628Z","shell.execute_reply.started":"2023-07-06T07:04:40.204681Z","shell.execute_reply":"2023-07-06T07:04:40.543406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_1epoch(model, train_loader, lossfun, optimizer):\n    model.train()\n    total_loss, total_acc = 0.0, 0.0\n\n    for x, y in tqdm(train_loader):\n        x = x.cuda()\n        y = y.cuda()\n\n        optimizer.zero_grad()\n        out = model(x)\n        loss = lossfun(out, y)\n        _, pred = torch.max(out.detach(), 1)\n        loss.backward()\n        optimizer.step()\n\n        total_loss += loss.item() * x.size(0)\n        total_acc += torch.sum(pred == y)\n\n    avg_loss = total_loss / len(train_loader.dataset)\n    avg_acc = total_acc / len(train_loader.dataset)\n    return avg_acc, avg_loss","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:40.551414Z","iopub.execute_input":"2023-07-06T07:04:40.551845Z","iopub.status.idle":"2023-07-06T07:04:40.594285Z","shell.execute_reply.started":"2023-07-06T07:04:40.551796Z","shell.execute_reply":"2023-07-06T07:04:40.592149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate_1epoch(model, val_loader, lossfun):\n    model.eval()\n    total_loss, total_acc = 0.0, 0.0\n\n    with torch.no_grad():\n        for x, y in tqdm(val_loader):\n            x = x.cuda()\n            y = y.cuda()\n\n            out = model(x)\n            loss = lossfun(out.detach(), y)\n            _, pred = torch.max(out, 1)\n\n            total_loss += loss.item() * x.size(0)\n            total_acc += torch.sum(pred == y)\n\n    avg_loss = total_loss / len(val_loader.dataset)\n    avg_acc = total_acc / len(val_loader.dataset)\n    return avg_acc, avg_loss","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:40.598920Z","iopub.execute_input":"2023-07-06T07:04:40.599497Z","iopub.status.idle":"2023-07-06T07:04:41.010052Z","shell.execute_reply.started":"2023-07-06T07:04:40.599441Z","shell.execute_reply":"2023-07-06T07:04:41.009071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:41.020862Z","iopub.execute_input":"2023-07-06T07:04:41.021443Z","iopub.status.idle":"2023-07-06T07:04:41.065557Z","shell.execute_reply.started":"2023-07-06T07:04:41.021386Z","shell.execute_reply":"2023-07-06T07:04:41.064317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(model, optimizer, train_loader, val_loader, n_epochs):\n    lossfun = torch.nn.CrossEntropyLoss()\n\n    for epoch in tqdm(range(n_epochs)):\n        train_acc, train_loss = train_1epoch(\n            model, train_loader, lossfun, optimizer\n        )\n        val_acc, val_loss = validate_1epoch(model, val_loader, lossfun)\n        print(\n            f\"epoch={epoch}, train loss={train_loss}, train accuracy={train_acc}, val loss={val_loss}, val accuracy={val_acc}\"\n        )","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:41.067300Z","iopub.execute_input":"2023-07-06T07:04:41.067679Z","iopub.status.idle":"2023-07-06T07:04:41.104620Z","shell.execute_reply.started":"2023-07-06T07:04:41.067639Z","shell.execute_reply":"2023-07-06T07:04:41.103237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train(model,optim.SGD(model.parameters(),lr = 0.01),train_loader,val_loader,n_epochs = 1)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:04:41.107546Z","iopub.execute_input":"2023-07-06T07:04:41.108066Z","iopub.status.idle":"2023-07-06T07:07:26.924230Z","shell.execute_reply.started":"2023-07-06T07:04:41.108012Z","shell.execute_reply":"2023-07-06T07:07:26.922772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setup_test_loader(data_dir, batch_size, dryrun):\n    dataset = torchvision.datasets.ImageFolder(\n        os.path.join(data_dir, \"test\"), transform=setup_center_crop_transform()\n    )\n    image_ids = [\n        os.path.splitext(os.path.basename(path))[0] for path, _ in dataset.imgs\n    ]\n\n    if dryrun:\n        dataset = torch.utils.data.Subset(dataset, range(0, 100))\n        image_ids = image_ids[:100]\n\n    loader = torch.utils.data.DataLoader(\n        dataset, batch_size=batch_size, num_workers=2\n    )\n    return loader, image_ids","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:07:26.926726Z","iopub.execute_input":"2023-07-06T07:07:26.927257Z","iopub.status.idle":"2023-07-06T07:07:26.938836Z","shell.execute_reply.started":"2023-07-06T07:07:26.927209Z","shell.execute_reply":"2023-07-06T07:07:26.937269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader,image_ids = setup_test_loader('/kaggle/data',batch_size = 50,dryrun = False)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:07:26.940502Z","iopub.execute_input":"2023-07-06T07:07:26.941083Z","iopub.status.idle":"2023-07-06T07:07:27.068907Z","shell.execute_reply.started":"2023-07-06T07:07:26.941041Z","shell.execute_reply":"2023-07-06T07:07:27.067285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, loader):\n    pred_fun = nn.Softmax(dim=1)\n    preds = []\n    for x,_ in tqdm(loader):\n        x = x.cuda()\n        with torch.no_grad():\n            y = pred_fun(model(x))\n        y = y.cpu().numpy()\n        y = y[:, 1]  # cat:0, dog: 1\n        preds.append(y)\n    preds = np.concatenate(preds)\n    return preds","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:13:38.716921Z","iopub.execute_input":"2023-07-06T07:13:38.717352Z","iopub.status.idle":"2023-07-06T07:13:38.728040Z","shell.execute_reply.started":"2023-07-06T07:13:38.717318Z","shell.execute_reply":"2023-07-06T07:13:38.726852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = predict(model,test_loader)","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:13:40.749218Z","iopub.execute_input":"2023-07-06T07:13:40.749623Z","iopub.status.idle":"2023-07-06T07:14:50.886526Z","shell.execute_reply.started":"2023-07-06T07:13:40.749588Z","shell.execute_reply":"2023-07-06T07:14:50.885355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def write_prediction(image_ids, prediction, out_path):\n    with open(out_path, \"w\") as f:\n        f.write(\"id,label\\n\")\n        for i, p in zip(image_ids, prediction):\n            f.write(\"{},{}\\n\".format(i, p))","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:08:36.550620Z","iopub.execute_input":"2023-07-06T07:08:36.551293Z","iopub.status.idle":"2023-07-06T07:08:36.558845Z","shell.execute_reply.started":"2023-07-06T07:08:36.551243Z","shell.execute_reply":"2023-07-06T07:08:36.557884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"write_prediction(image_ids,preds,'/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-06T07:08:36.560536Z","iopub.execute_input":"2023-07-06T07:08:36.560991Z","iopub.status.idle":"2023-07-06T07:08:36.608411Z","shell.execute_reply.started":"2023-07-06T07:08:36.560947Z","shell.execute_reply":"2023-07-06T07:08:36.607332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}