{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2020-12-28T21:48:19.308213Z","iopub.status.busy":"2020-12-28T21:48:19.307497Z","iopub.status.idle":"2020-12-28T21:48:22.324425Z","shell.execute_reply":"2020-12-28T21:48:22.325648Z"},"papermill":{"duration":3.053483,"end_time":"2020-12-28T21:48:22.325881","exception":false,"start_time":"2020-12-28T21:48:19.272398","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport torch \nimport torch.nn as nn\nimport torch.nn.functional as F \nimport torch.optim as optim \nfrom torch.optim import lr_scheduler\nimport torchvision \nfrom torchvision import datasets, models, transforms, utils\nfrom torch.utils.data import Dataset, DataLoader \nfrom PIL import Image\nfrom skimage import io, transform \nimport matplotlib.pyplot as plt \nimport copy \nfrom sklearn.model_selection import train_test_split \nimport time \n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:19.02638Z","iopub.status.busy":"2020-12-28T21:51:19.025492Z","iopub.status.idle":"2020-12-28T21:51:19.530276Z","shell.execute_reply":"2020-12-28T21:51:19.529534Z"},"papermill":{"duration":0.55878,"end_time":"2020-12-28T21:51:19.530439","exception":false,"start_time":"2020-12-28T21:51:18.971659","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Loading data \ndataset_dir = \"../input/cassava-leaf-disease-classification/\"\ndata_df = pd.read_csv(dataset_dir + \"train.csv\")   \n\n# Add to column Image_ID the image path in dataframe \ndata_df[\"path\"] = dataset_dir + \"train_images/\" + data_df[\"image_id\"] \n\n# Rearrange column order\ndata_df = data_df[[\"image_id\", \"path\", \"label\"]]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:19.609767Z","iopub.status.busy":"2020-12-28T21:51:19.609126Z","iopub.status.idle":"2020-12-28T21:51:19.621304Z","shell.execute_reply":"2020-12-28T21:51:19.621932Z"},"papermill":{"duration":0.058307,"end_time":"2020-12-28T21:51:19.622058","exception":false,"start_time":"2020-12-28T21:51:19.563751","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"pd.set_option('display.max_colwidth', -1)\ndata_df.head() ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:19.696787Z","iopub.status.busy":"2020-12-28T21:51:19.696121Z","iopub.status.idle":"2020-12-28T21:51:19.958969Z","shell.execute_reply":"2020-12-28T21:51:19.959483Z"},"papermill":{"duration":0.302946,"end_time":"2020-12-28T21:51:19.959612","exception":false,"start_time":"2020-12-28T21:51:19.656666","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Look how an image looks like\n\nimg = data_df.iloc[0] \nimg = Image.open(img[\"path\"]) # e.g. open image of picture\nimg ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.081317Z","iopub.status.busy":"2020-12-28T21:51:20.080337Z","iopub.status.idle":"2020-12-28T21:51:20.099402Z","shell.execute_reply":"2020-12-28T21:51:20.098615Z"},"papermill":{"duration":0.086525,"end_time":"2020-12-28T21:51:20.099506","exception":false,"start_time":"2020-12-28T21:51:20.012981","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Transform image to tensor \nimg = torchvision.transforms.functional.to_tensor(img)\nimg.shape","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.217975Z","iopub.status.busy":"2020-12-28T21:51:20.216913Z","iopub.status.idle":"2020-12-28T21:51:20.219504Z","shell.execute_reply":"2020-12-28T21:51:20.22014Z"},"papermill":{"duration":0.065701,"end_time":"2020-12-28T21:51:20.220272","exception":false,"start_time":"2020-12-28T21:51:20.154571","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Preprocessing step \ntrain_transform = transforms.Compose([\n    transforms.RandomResizedCrop(224), \n    transforms.RandomHorizontalFlip(),\n    transforms.ToTensor(),\n    transforms.Normalize((0.485,0.456,0.406), (0.229, 0.224,0.225))\n]) \n\nval_transform = transforms.Compose([\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])","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.336687Z","iopub.status.busy":"2020-12-28T21:51:20.335799Z","iopub.status.idle":"2020-12-28T21:51:20.342643Z","shell.execute_reply":"2020-12-28T21:51:20.342083Z"},"papermill":{"duration":0.0671,"end_time":"2020-12-28T21:51:20.342762","exception":false,"start_time":"2020-12-28T21:51:20.275662","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# split data in tran and validation data \ntrain, val = train_test_split(data_df, test_size=0.15) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.462005Z","iopub.status.busy":"2020-12-28T21:51:20.461054Z","iopub.status.idle":"2020-12-28T21:51:20.464303Z","shell.execute_reply":"2020-12-28T21:51:20.463659Z"},"papermill":{"duration":0.067242,"end_time":"2020-12-28T21:51:20.464395","exception":false,"start_time":"2020-12-28T21:51:20.397153","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Create custom dataset with tensor for each image and label for each image\n# source: https://stackoverflow.com/questions/61391919/loading-image-data-from-pandas-to-pytorch \n\nclass MyDataset(Dataset): \n    def __init__(self, dataframe, transform = None): \n        self.dataframe = dataframe \n        self.transform = transform\n        \n    def __len__(self): \n        return len(self.dataframe) \n    \n    def __getitem__(self, index): \n        row = self.dataframe.iloc[index] \n        img = Image.open(row[\"path\"])  \n        #tensor = torchvision.transforms.functional.to_tensor(img)\n        label = row[\"label\"]\n        \n        if self.transform: \n            img = self.transform(img)\n        \n        return (img, label)\n    \n        \n# create dataset with tensors and their belonging labels for each image\ntrain_data = MyDataset(train, train_transform) \nval_data = MyDataset(val, val_transform) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.579541Z","iopub.status.busy":"2020-12-28T21:51:20.57865Z","iopub.status.idle":"2020-12-28T21:51:20.581451Z","shell.execute_reply":"2020-12-28T21:51:20.581992Z"},"papermill":{"duration":0.063945,"end_time":"2020-12-28T21:51:20.582141","exception":false,"start_time":"2020-12-28T21:51:20.518196","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Create Dataloader for train and validation set\ntrainloader = DataLoader(train_data, batch_size = 4, shuffle = True, num_workers=4) \nvalloader = DataLoader(val_data, batch_size = 4, shuffle=True, num_workers=4)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.699852Z","iopub.status.busy":"2020-12-28T21:51:20.698881Z","iopub.status.idle":"2020-12-28T21:51:20.704808Z","shell.execute_reply":"2020-12-28T21:51:20.705503Z"},"papermill":{"duration":0.068958,"end_time":"2020-12-28T21:51:20.70572","exception":false,"start_time":"2020-12-28T21:51:20.636762","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"print(len(trainloader.dataset))\nprint(len(trainloader))  # each batch size\nprint(type(trainloader)) \nprint(len(valloader.dataset)) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.822496Z","iopub.status.busy":"2020-12-28T21:51:20.821701Z","iopub.status.idle":"2020-12-28T21:51:20.824853Z","shell.execute_reply":"2020-12-28T21:51:20.824324Z"},"papermill":{"duration":0.063391,"end_time":"2020-12-28T21:51:20.824985","exception":false,"start_time":"2020-12-28T21:51:20.761594","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Train and validation loader in a dict\ntrain_val_loader = {\"train\": trainloader, \"val\": valloader}","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:20.946641Z","iopub.status.busy":"2020-12-28T21:51:20.945581Z","iopub.status.idle":"2020-12-28T21:51:20.948859Z","shell.execute_reply":"2020-12-28T21:51:20.948208Z"},"papermill":{"duration":0.067054,"end_time":"2020-12-28T21:51:20.948971","exception":false,"start_time":"2020-12-28T21:51:20.881917","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"dataset_sizes = {j: len(train_val_loader[j].dataset) for j in [\"train\", \"val\"]} ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:21.068255Z","iopub.status.busy":"2020-12-28T21:51:21.0673Z","iopub.status.idle":"2020-12-28T21:51:21.071727Z","shell.execute_reply":"2020-12-28T21:51:21.071119Z"},"papermill":{"duration":0.066774,"end_time":"2020-12-28T21:51:21.071835","exception":false,"start_time":"2020-12-28T21:51:21.005061","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"dataset_sizes","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:21.586769Z","iopub.status.busy":"2020-12-28T21:51:21.585646Z","iopub.status.idle":"2020-12-28T21:51:21.588702Z","shell.execute_reply":"2020-12-28T21:51:21.589269Z"},"papermill":{"duration":0.459007,"end_time":"2020-12-28T21:51:21.589417","exception":false,"start_time":"2020-12-28T21:51:21.13041","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# use gpu if possible\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.058752,"end_time":"2020-12-28T21:51:21.708232","exception":false,"start_time":"2020-12-28T21:51:21.64948","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Transfer Learning and Fintetuning \n\nBased on the Pytorch Tutorial: https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html "},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:21.848598Z","iopub.status.busy":"2020-12-28T21:51:21.847545Z","iopub.status.idle":"2020-12-28T21:51:21.850263Z","shell.execute_reply":"2020-12-28T21:51:21.850968Z"},"papermill":{"duration":0.083764,"end_time":"2020-12-28T21:51:21.851114","exception":false,"start_time":"2020-12-28T21:51:21.76735","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Train Model for our data \n\ndef train_model(model, dataloaders, criterion, optimizer, scheduler, num_epochs=25):\n    since = time.time()\n\n    best_model_wts = copy.deepcopy(model.state_dict())\n    best_acc = 0.0\n    dataset_sizes = {j: len(dataloaders[j].dataset) for j in [\"train\", \"val\"]} \n    \n    for epoch in range(num_epochs):\n        print('Epoch {}/{}'.format(epoch, num_epochs - 1))\n        print('-' * 10)\n\n        # Each epoch has a training and validation phase\n        for phase in [\"train\", \"val\"]:\n            if phase == \"train\":\n                model.train()  # Set model to training mode\n            else:\n                model.eval()   # Set model to evaluate mode\n\n            running_loss = 0.0\n            running_corrects = 0\n\n            # Iterate over data.\n            for inputs, labels in dataloaders[phase]:\n                inputs = inputs.to(device)\n                labels = labels.to(device)\n\n                # zero the parameter gradients\n                optimizer.zero_grad()\n\n                # forward\n                # track history if only in train\n                with torch.set_grad_enabled(phase == 'train'):\n                    outputs = model(inputs)\n                    _, preds = torch.max(outputs, 1)\n                    loss = criterion(outputs, labels)\n\n                    # backward + optimize only if in training phase\n                    if phase == 'train':\n                        loss.backward()\n                        optimizer.step()\n\n                # statistics\n                running_loss += loss.item() * inputs.size(0)\n                running_corrects += torch.sum(preds == labels.data)\n            if phase == 'train':\n                scheduler.step()\n\n            epoch_loss = running_loss / dataset_sizes[phase]\n            epoch_acc = running_corrects.double() / dataset_sizes[phase]\n\n            print('{} Loss: {:.4f} Acc: {:.4f}'.format(\n                phase, epoch_loss, epoch_acc))\n\n            # deep copy the model\n            if phase == 'val' and epoch_acc > best_acc:\n                best_acc = epoch_acc\n                best_model_wts = copy.deepcopy(model.state_dict())\n\n        print()\n\n    time_elapsed = time.time() - since\n    print('Training complete in {:.0f}m {:.0f}s'.format(\n        time_elapsed // 60, time_elapsed % 60))\n    print('Best val Acc: {:4f}'.format(best_acc))\n\n    # load best model weights\n    model.load_state_dict(best_model_wts)\n    \n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:21.982337Z","iopub.status.busy":"2020-12-28T21:51:21.981449Z","iopub.status.idle":"2020-12-28T21:51:21.984819Z","shell.execute_reply":"2020-12-28T21:51:21.984306Z"},"papermill":{"duration":0.075483,"end_time":"2020-12-28T21:51:21.98493","exception":false,"start_time":"2020-12-28T21:51:21.909447","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Optional: Visualize Model\n\ndef visualize_model(model, dataloaders, num_images=6):\n    was_training = model.training\n    model.eval()\n    images_so_far = 0\n    fig = plt.figure()\n\n    with torch.no_grad():\n        for i, (inputs, labels) in enumerate(dataloaders['val']):\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n\n            for j in range(inputs.size()[0]):\n                images_so_far += 1\n                ax = plt.subplot(num_images//2, 2, images_so_far)\n                ax.axis('off')\n                ax.set_title('predicted: {}'.format(class_names[preds[j]]))\n                imshow(inputs.cpu().data[j])\n\n                if images_so_far == num_images:\n                    model.train(mode=was_training)\n                    return\n        model.train(mode=was_training)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.063312,"end_time":"2020-12-28T21:51:22.109503","exception":false,"start_time":"2020-12-28T21:51:22.046191","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Train model\n\nCells are commented out, because the model was once trained and then saved after the first run, in order for this notebook to work offline. The model will be loaded for the testing part. The trained model can be found in the directory \"../input/efficientnet-model/efficient_net.pt\" "},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:22.241586Z","iopub.status.busy":"2020-12-28T21:51:22.240592Z","iopub.status.idle":"2020-12-28T21:51:22.244964Z","shell.execute_reply":"2020-12-28T21:51:22.244415Z"},"papermill":{"duration":0.073887,"end_time":"2020-12-28T21:51:22.245084","exception":false,"start_time":"2020-12-28T21:51:22.171197","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# load pretrained model and finetune covnet\n# load first time online and save offline\n\n#efficient_net = EfficientNet.from_pretrained(\"efficientnet-b3\").to(device)\nresnext = models.resnext50_32x4d(pretrained=\"True\")\n\nnum_feature = resnext.fc.in_features  # of the final layer\n\nresnext.fc = nn.Linear(num_feature, 5) # change output to 5 classes, expl: https://discuss.pytorch.org/t/how-to-modify-the-final-fc-layer-based-on-the-torch-model/766\n\nresnext = resnext.to(device) \n\ncriterion = nn.CrossEntropyLoss() \n\n# Observe that all parameters are being optimized\noptimizer_feature = optim.SGD(resnext.parameters(), lr=0.001, momentum=0.9) \n\n# Decay LR by a factor of 0.1 every 7 epochs\nexp_lr_scheduler = lr_scheduler.StepLR(optimizer_feature, step_size=7, gamma=0.1)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:22.496212Z","iopub.status.busy":"2020-12-28T21:51:22.495194Z","iopub.status.idle":"2020-12-28T21:51:22.498466Z","shell.execute_reply":"2020-12-28T21:51:22.497954Z"},"papermill":{"duration":0.067445,"end_time":"2020-12-28T21:51:22.498587","exception":false,"start_time":"2020-12-28T21:51:22.431142","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"resnext = train_model(resnext, train_val_loader, criterion, optimizer_feature, exp_lr_scheduler, num_epochs=10) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:22.621465Z","iopub.status.busy":"2020-12-28T21:51:22.620594Z","iopub.status.idle":"2020-12-28T21:51:22.623929Z","shell.execute_reply":"2020-12-28T21:51:22.624586Z"},"papermill":{"duration":0.068869,"end_time":"2020-12-28T21:51:22.624735","exception":false,"start_time":"2020-12-28T21:51:22.555866","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Save trained model in output/kaggle/working\npath = \"resnext.pt\"\n\n# Save model\ntorch.save(resnext.state_dict(), path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Find path of trained model file\nfor root, dirs, files in os.walk(\"/kaggle\"):\n    for file in files:\n        if file.endswith(\".pt\"):\n             print(os.path.join(root, file))","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.06003,"end_time":"2020-12-28T21:51:22.744951","exception":false,"start_time":"2020-12-28T21:51:22.684921","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Inference\n\nSave model and load it for inference (support from here: https://pytorch.org/tutorials/beginner/saving_loading_models.html, https://pytorch.org/tutorials/beginner/saving_loading_models.html) \nhttps://discuss.pytorch.org/t/how-to-use-train-model-for-predict-unseen-data/81689"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:22.878098Z","iopub.status.busy":"2020-12-28T21:51:22.877048Z","iopub.status.idle":"2020-12-28T21:51:25.033624Z","shell.execute_reply":"2020-12-28T21:51:25.032436Z"},"papermill":{"duration":2.226579,"end_time":"2020-12-28T21:51:25.033792","exception":false,"start_time":"2020-12-28T21:51:22.807213","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"\"\"\"\n# load model \nmodel_weights = \"../input/cassava-resnext/resnext.pt\"\nmodel_template = \"../input/resnext-submission-example/resnext_untrained.pt\"\n#model = models.resnext50_32x4d(pretrained=\"False\").to(device)  # not trained weights\nmodel = torch.load(model_template)\n\n# recreate architecture as for the training model\nnum_feature = model.fc.in_features  # of the final layer\nmodel.fc = nn.Linear(num_feature, 5) # output-layer to 5\nmodel = model.to(device) \n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# save untrained model for later offline use\n#torch.save(model, \"resnext_untrained.pt\")","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:25.158695Z","iopub.status.busy":"2020-12-28T21:51:25.158059Z","iopub.status.idle":"2020-12-28T21:51:26.702368Z","shell.execute_reply":"2020-12-28T21:51:26.702891Z"},"papermill":{"duration":1.606879,"end_time":"2020-12-28T21:51:26.703041","exception":false,"start_time":"2020-12-28T21:51:25.096162","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"\"\"\"\nmodel.load_state_dict(torch.load(model_weights)) \nmodel.eval()\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:26.829045Z","iopub.status.busy":"2020-12-28T21:51:26.828361Z","iopub.status.idle":"2020-12-28T21:51:26.838521Z","shell.execute_reply":"2020-12-28T21:51:26.837962Z"},"papermill":{"duration":0.07634,"end_time":"2020-12-28T21:51:26.838624","exception":false,"start_time":"2020-12-28T21:51:26.762284","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"\"\"\"\n# Load test image sample\ntest_df = pd.read_csv(dataset_dir + \"sample_submission.csv\")   \n\n# Add to column Image_ID the image path in dataframe \ntest_df[\"path\"] = dataset_dir + \"test_images/\" + test_df[\"image_id\"] \n\n# Rearrange column order\ntest_df = test_df[[\"image_id\", \"path\", \"label\"]]\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:26.968458Z","iopub.status.busy":"2020-12-28T21:51:26.967067Z","iopub.status.idle":"2020-12-28T21:51:26.971069Z","shell.execute_reply":"2020-12-28T21:51:26.97156Z"},"papermill":{"duration":0.072877,"end_time":"2020-12-28T21:51:26.971686","exception":false,"start_time":"2020-12-28T21:51:26.898809","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"#test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:27.096974Z","iopub.status.busy":"2020-12-28T21:51:27.096231Z","iopub.status.idle":"2020-12-28T21:51:27.100762Z","shell.execute_reply":"2020-12-28T21:51:27.100243Z"},"papermill":{"duration":0.068873,"end_time":"2020-12-28T21:51:27.100862","exception":false,"start_time":"2020-12-28T21:51:27.031989","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"#test = MyDataset(test_df, val_transform) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:27.224891Z","iopub.status.busy":"2020-12-28T21:51:27.224241Z","iopub.status.idle":"2020-12-28T21:51:27.228393Z","shell.execute_reply":"2020-12-28T21:51:27.228865Z"},"papermill":{"duration":0.069247,"end_time":"2020-12-28T21:51:27.228992","exception":false,"start_time":"2020-12-28T21:51:27.159745","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"#testloader = DataLoader(test, batch_size = 4, shuffle=False, num_workers=4)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.06044,"end_time":"2020-12-28T21:51:27.348086","exception":false,"start_time":"2020-12-28T21:51:27.287646","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Feed model test image\n\nBased on this pytorch Tutorial section: \"Test the network on the test data\" https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:27.475558Z","iopub.status.busy":"2020-12-28T21:51:27.47461Z","iopub.status.idle":"2020-12-28T21:51:27.652819Z","shell.execute_reply":"2020-12-28T21:51:27.65209Z"},"papermill":{"duration":0.244853,"end_time":"2020-12-28T21:51:27.65299","exception":false,"start_time":"2020-12-28T21:51:27.408137","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"\"\"\"\npred_labels = [] \nfor data in testloader: \n    img, label = data  \n    img = img.to(device) \n    label = label.to(device)\n    \n    outputs = model(img) # predict output \n    \n    _, predicted = torch.max(outputs, 1) \n    for each in predicted:  # when more/parallel outputs\n        pred_labels.append(each.item())\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:28.774024Z","iopub.status.busy":"2020-12-28T21:51:28.773025Z","iopub.status.idle":"2020-12-28T21:51:28.777966Z","shell.execute_reply":"2020-12-28T21:51:28.778456Z"},"papermill":{"duration":0.077885,"end_time":"2020-12-28T21:51:28.77858","exception":false,"start_time":"2020-12-28T21:51:28.700695","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"\"\"\"\n# Create Submission file with predicted value\nsubmission = pd.DataFrame({\"image_id\": test_df[\"image_id\"], \"label\": pred_labels})\nsubmission.head()\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-28T21:51:28.907139Z","iopub.status.busy":"2020-12-28T21:51:28.906174Z","iopub.status.idle":"2020-12-28T21:51:29.210125Z","shell.execute_reply":"2020-12-28T21:51:29.207493Z"},"papermill":{"duration":0.37102,"end_time":"2020-12-28T21:51:29.210288","exception":false,"start_time":"2020-12-28T21:51:28.839268","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"\"\"\"\n# Save submission file\nsubmission.to_csv('submission.csv', index=False)\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}