{"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":"import os\nimport numpy as np\nimport torch\n\nimport torchvision\nfrom torchvision import datasets, models, transforms\nimport matplotlib.pyplot as plt\n\nfrom PIL import Image\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-23T14:01:33.508669Z","iopub.execute_input":"2022-03-23T14:01:33.508987Z","iopub.status.idle":"2022-03-23T14:01:35.114755Z","shell.execute_reply.started":"2022-03-23T14:01:33.508905Z","shell.execute_reply":"2022-03-23T14:01:35.114040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check if CUDA is available\ntrain_on_gpu = torch.cuda.is_available()\n\nif not train_on_gpu:\n    print('CUDA is not available.  Training on CPU ...')\nelse:\n    print('CUDA is available!  Training on GPU ...')","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:35.116599Z","iopub.execute_input":"2022-03-23T14:01:35.116838Z","iopub.status.idle":"2022-03-23T14:01:35.166524Z","shell.execute_reply.started":"2022-03-23T14:01:35.116804Z","shell.execute_reply":"2022-03-23T14:01:35.165819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define training and test data directories\n# ../input/sorghum-id-fgvc-9/train_images/2017-06-01__10-26-27-479.png\n# ../input/sorghum-id-fgvc-9/test/1000135300.png\n\ndata_dir = '../input/sorghum-id-fgvc-9/'\ntrain_dir = os.path.join(data_dir, 'train_images/')\ntest_dir = os.path.join(data_dir, 'test/')","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:35.167938Z","iopub.execute_input":"2022-03-23T14:01:35.168445Z","iopub.status.idle":"2022-03-23T14:01:35.178712Z","shell.execute_reply.started":"2022-03-23T14:01:35.168405Z","shell.execute_reply":"2022-03-23T14:01:35.177957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ninputDF = pd.read_csv('../input/sorghum-id-fgvc-9/train_cultivar_mapping.csv')\n# classes = inputDF['cultivar'].unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:35.180231Z","iopub.execute_input":"2022-03-23T14:01:35.180563Z","iopub.status.idle":"2022-03-23T14:01:35.220218Z","shell.execute_reply.started":"2022-03-23T14:01:35.180532Z","shell.execute_reply":"2022-03-23T14:01:35.219575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:35.221513Z","iopub.execute_input":"2022-03-23T14:01:35.221767Z","iopub.status.idle":"2022-03-23T14:01:35.238416Z","shell.execute_reply.started":"2022-03-23T14:01:35.221733Z","shell.execute_reply":"2022-03-23T14:01:35.237605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Encode the label\nle = LabelEncoder()\nlabel_encoded = le.fit_transform(inputDF['cultivar'])\ninputDF['label_encoded'] = label_encoded\ninputDF['imagePath'] = train_dir + inputDF['image'] \n\n# Taking the class name\nlabel_names = le.classes_\ninputDF.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:35.239642Z","iopub.execute_input":"2022-03-23T14:01:35.239945Z","iopub.status.idle":"2022-03-23T14:01:36.108818Z","shell.execute_reply.started":"2022-03-23T14:01:35.239910Z","shell.execute_reply":"2022-03-23T14:01:36.108092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# errors = []\n# for idx in range(len(inputDF)):\n#     try:\n#         img_name = inputDF.iloc[idx, -1]\n#         image = Image.open(img_name)\n#     except:\n#         errors.append(idx)\nerrors = [235,\n 363,\n 1829,\n 1890,\n 1896,\n 1906,\n 1923,\n 1926,\n 1936,\n 1966,\n 1970,\n 1972,\n 2175,\n 2194,\n 2367,\n 2387,\n 2439,\n 2472,\n 2529,\n 2547,\n 2579,\n 2605,\n 2668,\n 2696,\n 2720,\n 2782,\n 2794,\n 2892,\n 3210,\n 3271,\n 3363,\n 3982,\n 4047,\n 4068,\n 4082,\n 4145,\n 4529,\n 4543,\n 4553,\n 4576,\n 5038,\n 5130,\n 5207,\n 5439,\n 5459,\n 5478,\n 5496,\n 5521,\n 5538,\n 5543,\n 5553,\n 5563,\n 5604,\n 5607,\n 5609,\n 5614,\n 5621,\n 5663,\n 5688,\n 5696,\n 5717,\n 5733,\n 5755,\n 5781,\n 5795,\n 5840,\n 5909,\n 5973,\n 6037,\n 6085,\n 6107,\n 6117,\n 6152,\n 6412,\n 6419,\n 6474,\n 6480,\n 6483,\n 6598,\n 6909,\n 6931,\n 6975,\n 7050,\n 7095,\n 7137,\n 7140,\n 7237,\n 7292,\n 7337,\n 7407,\n 7422,\n 7490,\n 7856,\n 8192,\n 8230,\n 8253,\n 8462,\n 8471,\n 8489,\n 8504,\n 8536,\n 8609,\n 8625,\n 8971,\n 9173,\n 9240,\n 9247,\n 9260,\n 9272,\n 9375,\n 9497,\n 9602,\n 9653,\n 9786,\n 9792,\n 10550,\n 10651,\n 11204,\n 11289,\n 11571,\n 11638,\n 11654,\n 11685,\n 11791,\n 11848,\n 11861,\n 11891,\n 11918,\n 11947,\n 11957,\n 11978,\n 12001,\n 12016,\n 12024,\n 12032,\n 12037,\n 12046,\n 12059,\n 12085,\n 12100,\n 12112,\n 12116,\n 12117,\n 12132,\n 12144,\n 12148,\n 12228,\n 12376,\n 12446,\n 12460,\n 12474,\n 12487,\n 12489,\n 12493,\n 12498,\n 12511,\n 12551,\n 12577,\n 12605,\n 12613,\n 12654,\n 12669,\n 12671,\n 12769,\n 12794,\n 12814,\n 12898,\n 12906,\n 12912,\n 12913,\n 12917,\n 12920,\n 12930,\n 12948,\n 12949,\n 12952,\n 12956,\n 12964,\n 12966,\n 12968,\n 12970,\n 12979,\n 12982,\n 12985,\n 12998,\n 13001,\n 13002,\n 13005,\n 13011,\n 13015,\n 13017,\n 13026,\n 13034,\n 13035,\n 13040,\n 13046,\n 13050,\n 13054,\n 13065,\n 13066,\n 13068,\n 13075,\n 13103,\n 13109,\n 13132,\n 13133,\n 13134,\n 13148,\n 13150,\n 13151,\n 13152,\n 13161,\n 13166,\n 13169,\n 13170,\n 13174,\n 13183,\n 13190,\n 13196,\n 13567,\n 13783,\n 13880,\n 13883,\n 13957,\n 14036,\n 14186,\n 14203,\n 14237,\n 14242,\n 14261,\n 14275,\n 14286,\n 14289,\n 14309,\n 14339,\n 14594,\n 14599,\n 14604,\n 14615,\n 14650,\n 14651,\n 14678,\n 14722,\n 14762,\n 14777,\n 14800,\n 14804,\n 14810,\n 14815,\n 14829,\n 14836,\n 14846,\n 14847,\n 14853,\n 14873,\n 14874,\n 14879,\n 14888,\n 14889,\n 14897,\n 14902,\n 14911,\n 14918,\n 14923,\n 14925,\n 14927,\n 14929,\n 14935,\n 14937,\n 14948,\n 14953,\n 14955,\n 14956,\n 14960,\n 14967,\n 14969,\n 14970,\n 14973,\n 14978,\n 14981,\n 14984,\n 14998,\n 15007,\n 15012,\n 15015,\n 15019,\n 15021,\n 15023,\n 15025,\n 15027,\n 15035,\n 15039,\n 15041,\n 15050,\n 15057,\n 15063,\n 15086,\n 15087,\n 15088,\n 15099,\n 15103,\n 15115,\n 15120,\n 15121,\n 15181,\n 15256,\n 15264,\n 15324,\n 15347,\n 15378,\n 15396,\n 15409,\n 15427,\n 15440,\n 15457,\n 15460,\n 15469,\n 15474,\n 15476,\n 15477,\n 15484,\n 15490,\n 15505,\n 15533,\n 15536,\n 15570,\n 15590,\n 15595,\n 15618,\n 15652,\n 15661,\n 15684,\n 15693,\n 15700,\n 15708,\n 15743,\n 15762,\n 15763,\n 15772,\n 15773,\n 15827,\n 15841,\n 15851,\n 15854,\n 15963,\n 16288,\n 16319,\n 16336,\n 16341,\n 16349,\n 16442,\n 16476,\n 16478,\n 16479,\n 16507,\n 16508,\n 16549,\n 16589,\n 17484,\n 17991,\n 18065,\n 18113,\n 18119,\n 18241,\n 18888,\n 18897,\n 18913,\n 18927,\n 18954,\n 19008,\n 19035,\n 19037,\n 19091,\n 19111,\n 19130,\n 19132,\n 19153,\n 19171,\n 19271,\n 19326,\n 19482,\n 19644,\n 19675,\n 19676,\n 19677,\n 19691,\n 19703,\n 19707,\n 19734,\n 19741,\n 19752,\n 19755,\n 19780,\n 19806,\n 19815,\n 19816,\n 19857,\n 19876,\n 20155,\n 20174,\n 20197,\n 20224,\n 20229,\n 20243,\n 20256,\n 20309,\n 20322,\n 20368,\n 20581,\n 20591,\n 20625,\n 20641,\n 20745,\n 20747,\n 20748,\n 20778,\n 20847,\n 21011,\n 21032,\n 21333,\n 21395,\n 21448,\n 21461,\n 21510,\n 21522,\n 21525,\n 21537,\n 21570,\n 21595,\n 21598,\n 21613,\n 21621,\n 21627,\n 21633,\n 21646,\n 21731,\n 22091,\n 22189,\n 22213,\n 22346,\n 22376,\n 22504]","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:36.110308Z","iopub.execute_input":"2022-03-23T14:01:36.110543Z","iopub.status.idle":"2022-03-23T14:01:36.138965Z","shell.execute_reply.started":"2022-03-23T14:01:36.110512Z","shell.execute_reply":"2022-03-23T14:01:36.137926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(inputDF))\nprint(len(errors))\ninputDF = inputDF.drop(errors)\nprint(len(inputDF))","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:36.140320Z","iopub.execute_input":"2022-03-23T14:01:36.140651Z","iopub.status.idle":"2022-03-23T14:01:36.160279Z","shell.execute_reply.started":"2022-03-23T14:01:36.140615Z","shell.execute_reply":"2022-03-23T14:01:36.159632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T15:02:58.594441Z","iopub.execute_input":"2022-03-23T15:02:58.595091Z","iopub.status.idle":"2022-03-23T15:02:58.604577Z","shell.execute_reply.started":"2022-03-23T15:02:58.595049Z","shell.execute_reply":"2022-03-23T15:02:58.603906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset\n\nclass SorghumDataset(Dataset):\n    def __init__(self, data_frame, root_dir, transform=None):\n        self.data_frame = data_frame\n        self.root_dir = root_dir\n        self.transform = transform\n    \n    def __len__(self):\n        # Return the length of the dataset\n        return len(self.data_frame)\n    \n    def __getitem__(self, idx):\n        # Return the observation based on an index. Ex. dataset[0] will return the first element from the dataset, in this case the image and the label.\n        if torch.is_tensor(idx):\n            idx = idx.tolist()\n        \n        img_name = self.data_frame.iloc[idx, -1]\n        image = Image.open(img_name)\n        label = self.data_frame.iloc[idx, -2]\n        \n        if self.transform:\n            image = self.transform(image)\n    \n        return (image, label)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:36.161718Z","iopub.execute_input":"2022-03-23T14:01:36.162189Z","iopub.status.idle":"2022-03-23T14:01:36.170060Z","shell.execute_reply.started":"2022-03-23T14:01:36.162139Z","shell.execute_reply":"2022-03-23T14:01:36.169101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from torch.utils.data import Dataset\n\n# class CustomDataSet(Dataset):\n#     def __init__(self, main_dir, transform):\n#         self.main_dir = main_dir\n#         self.transform = transform\n#         all_imgs = os.listdir(main_dir)\n#         self.total_imgs = natsort.natsorted(all_imgs)\n\n#     def __len__(self):\n#         return len(self.total_imgs)\n\n#     def __getitem__(self, idx):\n#         img_loc = os.path.join(self.main_dir, self.total_imgs[idx])\n#         image = Image.open(img_loc).convert(\"RGB\")\n#         tensor_image = self.transform(image)\n#         return tensor_image","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:36.173472Z","iopub.execute_input":"2022-03-23T14:01:36.173669Z","iopub.status.idle":"2022-03-23T14:01:36.181474Z","shell.execute_reply.started":"2022-03-23T14:01:36.173646Z","shell.execute_reply":"2022-03-23T14:01:36.180816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load and transform data using ImageFolder\n\n# VGG-16 Takes 224x224 images as input, so we resize all of them\ndata_transform = transforms.Compose([transforms.RandomResizedCrop(224), \n                                      transforms.ToTensor()])\n\n# train_data = datasets.ImageFolder(train_dir, transform=data_transform)\n# test_data = datasets.ImageFolder(test_dir, transform=data_transform)\n\n# # print out some data stats\n# print('Num training images: ', len(train_data))\n# print('Num test images: ', len(test_data))","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:36.184084Z","iopub.execute_input":"2022-03-23T14:01:36.184307Z","iopub.status.idle":"2022-03-23T14:01:36.190674Z","shell.execute_reply.started":"2022-03-23T14:01:36.184282Z","shell.execute_reply":"2022-03-23T14:01:36.190015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\ntrain_data  = SorghumDataset(root_dir = train_dir, data_frame = inputDF, transform=data_transform)\ntrain_loader = DataLoader(train_data , batch_size=20, shuffle=True, \n                               num_workers=0, drop_last=True)\n\n# test_data  = CustomDataSet(test_dir, transform=data_transform)\n# test_loader = data.DataLoader(test_data , batch_size=20, shuffle=True, \n#                                num_workers=0, drop_last=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:36.192073Z","iopub.execute_input":"2022-03-23T14:01:36.192419Z","iopub.status.idle":"2022-03-23T14:01:36.199412Z","shell.execute_reply.started":"2022-03-23T14:01:36.192383Z","shell.execute_reply":"2022-03-23T14:01:36.198514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the pretrained model from pytorch\nvgg16 = models.vgg16(pretrained=True)\n\n# print out the model structure\nprint(vgg16)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:36.200341Z","iopub.execute_input":"2022-03-23T14:01:36.200583Z","iopub.status.idle":"2022-03-23T14:01:48.245582Z","shell.execute_reply.started":"2022-03-23T14:01:36.200550Z","shell.execute_reply":"2022-03-23T14:01:48.244812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(vgg16.classifier[6].in_features) \nprint(vgg16.classifier[6].out_features) ","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:48.246658Z","iopub.execute_input":"2022-03-23T14:01:48.247379Z","iopub.status.idle":"2022-03-23T14:01:48.252920Z","shell.execute_reply.started":"2022-03-23T14:01:48.247340Z","shell.execute_reply":"2022-03-23T14:01:48.252045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freeze training for all \"features\" layers\nfor param in vgg16.features.parameters():\n    param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:48.254451Z","iopub.execute_input":"2022-03-23T14:01:48.255053Z","iopub.status.idle":"2022-03-23T14:01:48.262192Z","shell.execute_reply.started":"2022-03-23T14:01:48.255019Z","shell.execute_reply":"2022-03-23T14:01:48.261500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\n\nvgg16.classifier[6] = nn.Linear(in_features=4096, out_features=len(label_names), bias=True)\n\n\n# after completing your model, if GPU is available, move the model to GPU\nif train_on_gpu:\n    vgg16.cuda()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:48.263525Z","iopub.execute_input":"2022-03-23T14:01:48.263839Z","iopub.status.idle":"2022-03-23T14:01:51.280777Z","shell.execute_reply.started":"2022-03-23T14:01:48.263803Z","shell.execute_reply":"2022-03-23T14:01:51.280054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\n# specify loss function (categorical cross-entropy)\ncriterion = nn.CrossEntropyLoss()\n\n# specify optimizer (stochastic gradient descent) and learning rate = 0.001\noptimizer = optim.SGD(vgg16.classifier.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:51.282007Z","iopub.execute_input":"2022-03-23T14:01:51.282344Z","iopub.status.idle":"2022-03-23T14:01:51.288438Z","shell.execute_reply.started":"2022-03-23T14:01:51.282306Z","shell.execute_reply":"2022-03-23T14:01:51.287198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of epochs to train the model\nn_epochs = 2\n\n## TODO complete epoch and training batch loops\n## These loops should update the classifier-weights of this model\n## And track (and print out) the training loss over time\n\nfor epoch in range(1, n_epochs+1):\n\n    # keep track of training and validation loss\n    train_loss = 0.0\n    \n    ###################\n    # train the model #\n    ###################\n    # model by default is set to train\n    for batch_i, (data, target) in enumerate(train_loader):\n        # move tensors to GPU if CUDA is available\n        if train_on_gpu:\n            data, target = data.cuda(), target.cuda()\n        # clear the gradients of all optimized variables\n        optimizer.zero_grad()\n        # forward pass: compute predicted outputs by passing inputs to the model\n        output = vgg16(data)\n        # calculate the batch loss\n        loss = criterion(output, target)\n        # backward pass: compute gradient of the loss with respect to model parameters\n        loss.backward()\n        # perform a single optimization step (parameter update)\n        optimizer.step()\n        # update training loss \n        train_loss += loss.item()\n        \n        if batch_i % 20 == 19:    # print training loss every specified number of mini-batches\n            print('Epoch %d, Batch %d loss: %.16f' %\n                  (epoch, batch_i + 1, train_loss / 20))\n            train_loss = 0.0","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:01:51.289786Z","iopub.execute_input":"2022-03-23T14:01:51.290260Z","iopub.status.idle":"2022-03-23T14:44:33.980777Z","shell.execute_reply.started":"2022-03-23T14:01:51.290224Z","shell.execute_reply":"2022-03-23T14:44:33.979967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(vgg16.state_dict(), './PATH')","metadata":{"execution":{"iopub.status.busy":"2022-03-23T14:44:33.983203Z","iopub.execute_input":"2022-03-23T14:44:33.983471Z","iopub.status.idle":"2022-03-23T14:44:35.358461Z","shell.execute_reply.started":"2022-03-23T14:44:33.983436Z","shell.execute_reply":"2022-03-23T14:44:35.357702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = TheModelClass(*args, **kwargs)\nmodel.load_state_dict(torch.load('./PATH'))\nmodel.eval()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testDF = pd.read_csv('../input/sorghum-id-fgvc-9/sample_submission.csv')\ntestDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T15:00:57.460844Z","iopub.execute_input":"2022-03-23T15:00:57.461280Z","iopub.status.idle":"2022-03-23T15:00:57.485698Z","shell.execute_reply.started":"2022-03-23T15:00:57.461237Z","shell.execute_reply":"2022-03-23T15:00:57.484988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoded = le.transform(testDF['cultivar'])\ntestDF['label_encoded'] = label_encoded\ntestDF['imagePath'] = test_dir + testDF['filename']\ntestDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T15:04:16.867295Z","iopub.execute_input":"2022-03-23T15:04:16.867821Z","iopub.status.idle":"2022-03-23T15:04:16.892794Z","shell.execute_reply.started":"2022-03-23T15:04:16.867782Z","shell.execute_reply":"2022-03-23T15:04:16.892060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(testDF)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T15:26:59.247810Z","iopub.execute_input":"2022-03-23T15:26:59.248093Z","iopub.status.idle":"2022-03-23T15:26:59.253689Z","shell.execute_reply.started":"2022-03-23T15:26:59.248059Z","shell.execute_reply":"2022-03-23T15:26:59.252962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data  = SorghumDataset(root_dir = test_dir, data_frame = testDF, transform=data_transform)\ntest_loader = DataLoader(test_data , batch_size=500, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T15:28:53.414203Z","iopub.execute_input":"2022-03-23T15:28:53.414891Z","iopub.status.idle":"2022-03-23T15:28:53.419256Z","shell.execute_reply.started":"2022-03-23T15:28:53.414852Z","shell.execute_reply":"2022-03-23T15:28:53.418446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = []\ni = 0\nimport sys\nfor batch_i, (data, target) in enumerate(test_loader):\n    # move tensors to GPU if CUDA is available\n    if train_on_gpu:\n        data, target = data.cuda(), target.cuda()\n    output = vgg16(data)\n    _, index = torch.max(output, 1)\n    i = i + len(index)\n    percentageDone = i/len(testDF)\n    sys.stdout.write(str(percentageDone))\n    out.append(index)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T15:30:25.718656Z","iopub.execute_input":"2022-03-23T15:30:25.718923Z","iopub.status.idle":"2022-03-23T15:59:52.151684Z","shell.execute_reply.started":"2022-03-23T15:30:25.718893Z","shell.execute_reply":"2022-03-23T15:59:52.150941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flat_list = [item for sublist in out for item in sublist]\nlen(flat_list)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T16:01:20.879789Z","iopub.execute_input":"2022-03-23T16:01:20.880063Z","iopub.status.idle":"2022-03-23T16:01:20.910824Z","shell.execute_reply.started":"2022-03-23T16:01:20.880033Z","shell.execute_reply":"2022-03-23T16:01:20.910078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpuList = [item.cpu() for item in flat_list]","metadata":{"execution":{"iopub.status.busy":"2022-03-23T16:02:04.393826Z","iopub.execute_input":"2022-03-23T16:02:04.394377Z","iopub.status.idle":"2022-03-23T16:02:04.851679Z","shell.execute_reply.started":"2022-03-23T16:02:04.394336Z","shell.execute_reply":"2022-03-23T16:02:04.850935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outColumn = list(le.inverse_transform(cpuList))","metadata":{"execution":{"iopub.status.busy":"2022-03-23T16:02:37.442346Z","iopub.execute_input":"2022-03-23T16:02:37.443023Z","iopub.status.idle":"2022-03-23T16:02:37.526936Z","shell.execute_reply.started":"2022-03-23T16:02:37.442972Z","shell.execute_reply":"2022-03-23T16:02:37.526263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = pd.read_csv('../input/sorghum-id-fgvc-9/sample_submission.csv')['filename']","metadata":{"execution":{"iopub.status.busy":"2022-03-23T16:06:15.034596Z","iopub.execute_input":"2022-03-23T16:06:15.035143Z","iopub.status.idle":"2022-03-23T16:06:15.054970Z","shell.execute_reply.started":"2022-03-23T16:06:15.035103Z","shell.execute_reply":"2022-03-23T16:06:15.054282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('../input/sorghum-id-fgvc-9/sample_submission.csv').columns","metadata":{"execution":{"iopub.status.busy":"2022-03-23T16:06:51.948835Z","iopub.execute_input":"2022-03-23T16:06:51.949273Z","iopub.status.idle":"2022-03-23T16:06:51.970313Z","shell.execute_reply.started":"2022-03-23T16:06:51.949229Z","shell.execute_reply":"2022-03-23T16:06:51.969601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissionCSV = pd.DataFrame(data = list(zip(filenames, outColumn)),\n            columns=['filename', 'cultivar'])","metadata":{"execution":{"iopub.status.busy":"2022-03-23T16:07:05.911259Z","iopub.execute_input":"2022-03-23T16:07:05.911518Z","iopub.status.idle":"2022-03-23T16:07:05.926793Z","shell.execute_reply.started":"2022-03-23T16:07:05.911489Z","shell.execute_reply":"2022-03-23T16:07:05.926093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissionCSV.to_csv('output.csv', index= False)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T16:07:21.087462Z","iopub.execute_input":"2022-03-23T16:07:21.088287Z","iopub.status.idle":"2022-03-23T16:07:21.141956Z","shell.execute_reply.started":"2022-03-23T16:07:21.088219Z","shell.execute_reply":"2022-03-23T16:07:21.141299Z"},"trusted":true},"execution_count":null,"outputs":[]}]}