{"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":"# 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\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\nfor 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2021-09-22T19:52:19.89384Z","iopub.execute_input":"2021-09-22T19:52:19.89443Z","iopub.status.idle":"2021-09-22T19:52:41.090831Z","shell.execute_reply.started":"2021-09-22T19:52:19.894347Z","shell.execute_reply":"2021-09-22T19:52:41.086264Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" #imports","metadata":{}},{"cell_type":"code","source":"# needed imports\nfrom matplotlib import pyplot as plt\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import models,transforms, utils\nfrom torch.utils.data import random_split\nfrom torch.nn import Conv2d\nfrom torch import flatten\nfrom torch.optim import Adam\nfrom torch.nn import Linear\nfrom torch.nn import MaxPool2d\nfrom torch.nn import ReLU\nfrom torch.nn import LogSoftmax\nimport torch.nn.functional as F\nfrom torch.nn import Module\nimport torch.nn as nn\nimport torchvision\nimport numpy as np\nimport pandas as pd\nimport argparse\nimport time\nimport torch\nimport cv2\nimport os\n","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:29:23.273687Z","iopub.execute_input":"2021-09-24T05:29:23.274246Z","iopub.status.idle":"2021-09-24T05:29:23.281380Z","shell.execute_reply.started":"2021-09-24T05:29:23.274209Z","shell.execute_reply":"2021-09-24T05:29:23.280576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  define the configuration \nclass Config:\n    # specify image dimension\n    IMAGE_SIZE = 224\n\n    # specify ImageNet mean and standard deviation\n    #MEAN = [0.485, 0.456, 0.406]\n    #STD = [0.229, 0.224, 0.225]\n\n    # determine the device we will be using for inference\n    DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\n    # specify path to the ImageNet labels\n    IN_LABELS = \"ilsvrc2012_wordnet_lemmas.txt\"\n\n# initialize a config object\nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:29:24.204709Z","iopub.execute_input":"2021-09-24T05:29:24.205266Z","iopub.status.idle":"2021-09-24T05:29:24.210169Z","shell.execute_reply.started":"2021-09-24T05:29:24.205229Z","shell.execute_reply":"2021-09-24T05:29:24.209218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  define the functions \ndef plt_imshow(title, image):\n    # convert the image frame BGR to RGB color space and display it\n    image = cv2.imread(image)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    #print(image.shape)\n    plt.imshow(image)\n    plt.title(title)\n    plt.grid(False)\n    plt.show()\n    \n# preprocess the images     \ndef preprocess_image(image):\n    # swap the color channels from BGR to RGB, resize it, and scale\n    # the pixel values to [0, 1] range\n    image = cv2.imread(image)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = cv2.resize(image, (config.IMAGE_SIZE, config.IMAGE_SIZE))\n    image = image.astype(\"float32\") / 255.0\n\n    # subtract ImageNet mean, divide by ImageNet standard deviation,\n    # set \"channels first\" ordering, and add a batch dimension\n    #image -= config.MEAN\n    #image /= config.STD\n    image = np.transpose(image, (2, 0, 1))\n    #image = np.expand_dims(image, 0)\n\n    # return the preprocessed image\n    return image\n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:29:24.948766Z","iopub.execute_input":"2021-09-24T05:29:24.949045Z","iopub.status.idle":"2021-09-24T05:29:24.956279Z","shell.execute_reply.started":"2021-09-24T05:29:24.949016Z","shell.execute_reply":"2021-09-24T05:29:24.955200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert the labels to dict \nlabels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nlabels.breed.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:29:25.746048Z","iopub.execute_input":"2021-09-24T05:29:25.746321Z","iopub.status.idle":"2021-09-24T05:29:25.773393Z","shell.execute_reply.started":"2021-09-24T05:29:25.746293Z","shell.execute_reply":"2021-09-24T05:29:25.772722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = dict(zip(labels.id, labels.breed))\n#print(labels)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:29:26.780231Z","iopub.execute_input":"2021-09-24T05:29:26.780503Z","iopub.status.idle":"2021-09-24T05:29:26.790191Z","shell.execute_reply.started":"2021-09-24T05:29:26.780475Z","shell.execute_reply":"2021-09-24T05:29:26.789466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Convert the lables to numbers ","metadata":{}},{"cell_type":"code","source":"label_names = list(set(labels.values()))\nlabels_num = my_dict = dict(zip(label_names, range(len(label_names))))\nfor key, value in labels.items():\n    #print(labels[key])\n    labels[key]= labels_num[value]\n","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:29:40.553963Z","iopub.execute_input":"2021-09-24T05:29:40.554232Z","iopub.status.idle":"2021-09-24T05:29:40.561485Z","shell.execute_reply.started":"2021-09-24T05:29:40.554205Z","shell.execute_reply":"2021-09-24T05:29:40.560479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  display image \nimg1 = \"../input/dog-breed-identification/train/001513dfcb2ffafc82cccf4d8bbaba97.jpg\"\nidx = labels[img1.split(\"/\")[-1].split(\".\")[0]]\nbreed_label = list(labels_num.keys())[list(labels_num.values()).index(idx)]\nplt_imshow(breed_label,img1)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:29:41.551059Z","iopub.execute_input":"2021-09-24T05:29:41.551618Z","iopub.status.idle":"2021-09-24T05:29:41.786178Z","shell.execute_reply.started":"2021-09-24T05:29:41.551581Z","shell.execute_reply":"2021-09-24T05:29:41.785500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#next(iter(test_loader))","metadata":{"execution":{"iopub.status.busy":"2021-09-22T21:02:54.254109Z","iopub.execute_input":"2021-09-22T21:02:54.254566Z","iopub.status.idle":"2021-09-22T21:02:54.257899Z","shell.execute_reply.started":"2021-09-22T21:02:54.254529Z","shell.execute_reply":"2021-09-22T21:02:54.257195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DogBreedsDataset(Dataset):\n    \"\"\"Dog Breeds dataset.\"\"\"\n\n    def __init__(self, csv_file, root_dir, transform=None):\n        \"\"\"\n        Args:\n            csv_file (string): Path to the csv file with annotations.\n            root_dir (string): Directory with all the images.\n            transform (callable, optional): Optional transform to be applied\n                on a sample.\n        \"\"\"\n        self.labels_frame = pd.read_csv(csv_file)\n        self.map = dict(zip(self.labels_frame['breed'].unique(),range(0,len(self.labels_frame['breed'].unique()))))\n        self.labels_frame['breed'] = self.labels_frame['breed'].map(self.map)\n        self.root_dir = root_dir\n        self.transform = transform\n        \n    def getmap(self):\n        return self.map\n        \n    def __getclasses__(self):\n        return self.labels_frame['breed'].unique().tolist()\n\n    def __len__(self):\n        return len(self.labels_frame)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.root_dir,\n                                self.labels_frame.iloc[idx, 0])\n        img_name = img_name + '.jpg'\n        \n        image = io.imread(img_name)\n        PIL_image = Image.fromarray(image)\n        label = self.labels_frame.iloc[idx, 1:]\n        label = [int(label) for x in label]\n        label = np.asarray(label)\n        label = torch.from_numpy(label)\n        \n        if self.transform:\n            image = self.transform(PIL_image)\n        #sample = {'image': image, 'label': label}\n        return image,label","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:30:15.619754Z","iopub.execute_input":"2021-09-24T05:30:15.620287Z","iopub.status.idle":"2021-09-24T05:30:15.630273Z","shell.execute_reply.started":"2021-09-24T05:30:15.620249Z","shell.execute_reply":"2021-09-24T05:30:15.629194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DogBreedsTestDataset(Dataset):\n    \"\"\"Dog Breeds Test dataset.\"\"\"\n\n    def __init__(self, csv_file, root_dir, transform=None):\n        \"\"\"\n        Args:\n            csv_file (string): Path to the csv file with annotations.\n            root_dir (string): Directory with all the images.\n            transform (callable, optional): Optional transform to be applied\n                on a sample.\n        \"\"\"\n        self.labels_frame = pd.read_csv(csv_file)\n        self.labels_frame = self.labels_frame[['id']]\n        self.root_dir = root_dir\n        self.transform = transform\n   \n    def __len__(self):\n        return len(self.labels_frame)\n\n\n    def __getitem__(self, idx):\n        title = self.labels_frame.iloc[idx, 0]\n        img_name = os.path.join(self.root_dir,\n                                title)\n        img_name = img_name + '.jpg'\n        \n        image = io.imread(img_name)\n        PIL_image = Image.fromarray(image)\n        # print(PIL_image)\n        if self.transform:\n            image = self.transform(PIL_image)\n        sample = {'image': image, 'title': title}\n        return image,title","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:33:57.397829Z","iopub.execute_input":"2021-09-24T08:33:57.398094Z","iopub.status.idle":"2021-09-24T08:33:57.405823Z","shell.execute_reply.started":"2021-09-24T08:33:57.398065Z","shell.execute_reply":"2021-09-24T08:33:57.404990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndata_dir = '../input/dog-breed-identification'\nfor_stratify = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\n\n# how many samples per batch to load\nBATCH_SIZE = 20\n# percentage of training set to use as validation\nvalid_size = 0.2\n\n# TODO: Define transforms for the training data and testing data\ntransform = transforms.Compose([transforms.Resize(255),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n    ])\n\ntest_transforms = transforms.Compose([\n                                      transforms.ToTensor()])\n","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:33:28.712443Z","iopub.execute_input":"2021-09-24T05:33:28.713153Z","iopub.status.idle":"2021-09-24T05:33:29.436413Z","shell.execute_reply.started":"2021-09-24T05:33:28.713120Z","shell.execute_reply":"2021-09-24T05:33:29.435622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:34:02.348044Z","iopub.execute_input":"2021-09-24T05:34:02.348300Z","iopub.status.idle":"2021-09-24T05:34:02.358389Z","shell.execute_reply.started":"2021-09-24T05:34:02.348272Z","shell.execute_reply":"2021-09-24T05:34:02.357514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = DogBreedsDataset(csv_file='../input/dog-breed-identification/labels.csv',root_dir='../input/dog-breed-identification/train', transform=transform)\ntrain_indices, val_indices = train_test_split(list(range(len(for_stratify.breed))), test_size=0.2, stratify=for_stratify.breed)\n\ntrainData = torch.utils.data.Subset(train_data, train_indices)\nvalData = torch.utils.data.Subset(train_data, val_indices)\n\ntrainDataLoader = DataLoader(trainData, shuffle=True,batch_size=BATCH_SIZE)\nvalDataLoader = DataLoader(valData, batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:34:47.509774Z","iopub.execute_input":"2021-09-24T05:34:47.510512Z","iopub.status.idle":"2021-09-24T05:34:47.559573Z","shell.execute_reply.started":"2021-09-24T05:34:47.510476Z","shell.execute_reply":"2021-09-24T05:34:47.558878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(trainData))\nprint(len(valData))\nprint(len(train_data)==(len(trainData)+len(valData)), len(train_data))","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:21:24.393118Z","iopub.execute_input":"2021-09-24T08:21:24.393648Z","iopub.status.idle":"2021-09-24T08:21:24.400542Z","shell.execute_reply.started":"2021-09-24T08:21:24.393610Z","shell.execute_reply":"2021-09-24T08:21:24.399160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"number_of_classes=len(np.unique(list(labels.values())))","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:35:47.789189Z","iopub.execute_input":"2021-09-24T05:35:47.789770Z","iopub.status.idle":"2021-09-24T05:35:47.796538Z","shell.execute_reply.started":"2021-09-24T05:35:47.789734Z","shell.execute_reply":"2021-09-24T05:35:47.795420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = DogBreedsTestDataset(csv_file='../input/dog-breed-identification/sample_submission.csv',root_dir='../input/dog-breed-identification/test', transform=transform)\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size=20)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:35:56.964773Z","iopub.execute_input":"2021-09-24T05:35:56.965451Z","iopub.status.idle":"2021-09-24T05:35:57.457662Z","shell.execute_reply.started":"2021-09-24T05:35:56.965413Z","shell.execute_reply":"2021-09-24T05:35:57.456899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom skimage import io, transform\n#test_data[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:36:01.419273Z","iopub.execute_input":"2021-09-24T05:36:01.419787Z","iopub.status.idle":"2021-09-24T05:36:01.762349Z","shell.execute_reply.started":"2021-09-24T05:36:01.419742Z","shell.execute_reply":"2021-09-24T05:36:01.761626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-24T05:36:14.526690Z","iopub.execute_input":"2021-09-24T05:36:14.526955Z","iopub.status.idle":"2021-09-24T05:36:14.628440Z","shell.execute_reply.started":"2021-09-24T05:36:14.526927Z","shell.execute_reply":"2021-09-24T05:36:14.627783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pretraining \n# Load the pretrained model from pytorch\nvgg16 = models.vgg16(pretrained=True)\n\n# print out the model structure\nprint(vgg16)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-09-24T08:22:34.747072Z","iopub.execute_input":"2021-09-24T08:22:34.747349Z","iopub.status.idle":"2021-09-24T08:22:36.133965Z","shell.execute_reply.started":"2021-09-24T08:22:34.747306Z","shell.execute_reply":"2021-09-24T08:22:36.133166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"#pretraining \n# Load the pretrained model from pytorch\nvgg19 = models.vgg19(pretrained=True)\n\n# print out the model structure\nprint(vgg19)\"\"\"","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-09-24T08:22:36.135572Z","iopub.execute_input":"2021-09-24T08:22:36.135856Z","iopub.status.idle":"2021-09-24T08:22:36.141447Z","shell.execute_reply.started":"2021-09-24T08:22:36.135819Z","shell.execute_reply":"2021-09-24T08:22:36.140743Z"},"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":"2021-09-24T08:22:36.419642Z","iopub.execute_input":"2021-09-24T08:22:36.420175Z","iopub.status.idle":"2021-09-24T08:22:36.424106Z","shell.execute_reply.started":"2021-09-24T08:22:36.420139Z","shell.execute_reply":"2021-09-24T08:22:36.423424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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":"2021-09-24T08:22:37.321919Z","iopub.execute_input":"2021-09-24T08:22:37.322634Z","iopub.status.idle":"2021-09-24T08:22:37.330209Z","shell.execute_reply.started":"2021-09-24T08:22:37.322598Z","shell.execute_reply":"2021-09-24T08:22:37.329301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_inputs = vgg16.classifier[6].in_features\n\n# add last linear layer (n_inputs -> 5 flower classes)\n# new layers automatically have requires_grad = True\nlast_layer = nn.Linear(n_inputs, number_of_classes)\n\nvgg16.classifier[6] = last_layer\n\n# if GPU is available, move the model to GPU\nif train_on_gpu:\n    vgg16.cuda()\n\n# check to see that your last layer produces the expected number of outputs\nprint(vgg16.classifier[6].out_features)\n#print(vgg16)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:22:39.386140Z","iopub.execute_input":"2021-09-24T08:22:39.386687Z","iopub.status.idle":"2021-09-24T08:22:39.531707Z","shell.execute_reply.started":"2021-09-24T08:22:39.386650Z","shell.execute_reply":"2021-09-24T08:22:39.530954Z"},"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\n# optimizer = optim.SGD(vgg16.classifier.parameters(), lr=0.001)\n\n# ADAM optim\noptimizer = optim.SGD(vgg16.classifier.parameters(), lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:22:41.798975Z","iopub.execute_input":"2021-09-24T08:22:41.799234Z","iopub.status.idle":"2021-09-24T08:22:41.805582Z","shell.execute_reply.started":"2021-09-24T08:22:41.799206Z","shell.execute_reply":"2021-09-24T08:22:41.803967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feats_list = list(vgg16.features)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:22:42.859755Z","iopub.execute_input":"2021-09-24T08:22:42.860011Z","iopub.status.idle":"2021-09-24T08:22:42.864047Z","shell.execute_reply.started":"2021-09-24T08:22:42.859983Z","shell.execute_reply":"2021-09-24T08:22:42.863205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_feats_list = []\nfor feat in feats_list:\n    new_feats_list.append(feat)\n    if isinstance(feat, nn.Conv2d):\n        new_feats_list.append(nn.Dropout(p=0.5, inplace=True))\n\n# modify convolution layers\nvgg16.features = nn.Sequential(*new_feats_list)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:22:44.517186Z","iopub.execute_input":"2021-09-24T08:22:44.517768Z","iopub.status.idle":"2021-09-24T08:22:44.523843Z","shell.execute_reply.started":"2021-09-24T08:22:44.517730Z","shell.execute_reply":"2021-09-24T08:22:44.522979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-09-24T08:22:45.782806Z","iopub.execute_input":"2021-09-24T08:22:45.783061Z","iopub.status.idle":"2021-09-24T08:22:45.788887Z","shell.execute_reply.started":"2021-09-24T08:22:45.783032Z","shell.execute_reply":"2021-09-24T08:22:45.788205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io, transform\nfrom PIL import Image\n# number of epochs to train the model\nn_epochs = 5\n\nloss_values = []\nfor epoch in range(1, n_epochs+1):\n    running_loss = 0.0\n    # keep track of training and validation loss\n    train_loss = 0.0\n    valid_loss = 0.0\n    ###################\n    # train the model #\n    ###################\n    # model by default is set to train\n    \n    for batch_i, (data, target) in enumerate(trainDataLoader):\n        # move tensors to GPU if CUDA is available\n       \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, torch.max(target, 1)[1])\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        running_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            \n            train_loss = 0.0\n            \n    loss_values.append(running_loss / len(train_data))\n    \nplt.plot(loss_values)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:22:46.561640Z","iopub.execute_input":"2021-09-24T08:22:46.562195Z","iopub.status.idle":"2021-09-24T08:31:11.022067Z","shell.execute_reply.started":"2021-09-24T08:22:46.562160Z","shell.execute_reply":"2021-09-24T08:31:11.021423Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_values","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:31:42.434863Z","iopub.execute_input":"2021-09-24T08:31:42.435444Z","iopub.status.idle":"2021-09-24T08:31:42.443675Z","shell.execute_reply.started":"2021-09-24T08:31:42.435406Z","shell.execute_reply":"2021-09-24T08:31:42.442779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-09-24T07:02:22.155033Z","iopub.execute_input":"2021-09-24T07:02:22.155455Z","iopub.status.idle":"2021-09-24T07:02:22.161877Z","shell.execute_reply.started":"2021-09-24T07:02:22.155413Z","shell.execute_reply":"2021-09-24T07:02:22.161048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_loss = 0.0\nvgg16.eval()\nfor batch_i, (data, target) in enumerate(valDataLoader):\n        # move tensors to GPU if CUDA is available\n       \n        if train_on_gpu:\n            data, target = data.cuda(), target.cuda()\n            \n        output = vgg16(data)\n        # calculate the batch loss\n        loss = criterion(output, torch.max(target, 1)[1])\n        \n        # update training loss \n        valid_loss += loss.item()\n        \n        if batch_i % 20 == 19:    # print validation loss every specified number of mini-batches\n            print('Validation Loss Batch %d loss: %.16f' %\n                  (batch_i + 1, valid_loss / 20))\n            valid_loss = 0.0","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:32:29.018287Z","iopub.execute_input":"2021-09-24T08:32:29.018990Z","iopub.status.idle":"2021-09-24T08:32:52.370293Z","shell.execute_reply.started":"2021-09-24T08:32:29.018952Z","shell.execute_reply":"2021-09-24T08:32:52.369577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io, transform\nfrom PIL import Image\nresults = {}\nvgg16.eval()\n#test_data = DogBreedsTestset(csv_file='../input/dog-breed-identification/sample_submission.csv',root_dir='../input/dog-breed-identification/test', transform=transform)\n#test_loader = torch.utils.data.DataLoader(test_data, batch_size=20)\n\nfor batch_i, (data, title) in enumerate(test_loader):\n        # move tensors to GPU if CUDA is available\n        images,titles = data, title\n        #print(images.shape)\n        #print(titles)\n               \n        if train_on_gpu:\n            images = images.cuda()\n        #print(title)\n        logits = vgg16(images)\n        output = torch.nn.functional.softmax(logits, dim=1)\n        \n        for k in range(len(titles)):\n            name = titles[k]\n            results[name] = output[k].cpu().tolist()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-09-24T08:34:12.475057Z","iopub.execute_input":"2021-09-24T08:34:12.475311Z","iopub.status.idle":"2021-09-24T08:36:18.048137Z","shell.execute_reply.started":"2021-09-24T08:34:12.475284Z","shell.execute_reply":"2021-09-24T08:36:18.047399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df = pd.DataFrame(results).transpose()\ninv_map = {v: k for k, v in train_data.getmap().items()}\noutput_df.rename(columns=inv_map,inplace=True)\noutput_df = output_df.reset_index()\noutput_df.rename(columns={'index':'id'},inplace=True)\noutput_df.to_csv('output2.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T08:37:06.247097Z","iopub.execute_input":"2021-09-24T08:37:06.247381Z","iopub.status.idle":"2021-09-24T08:37:10.006019Z","shell.execute_reply.started":"2021-09-24T08:37:06.247351Z","shell.execute_reply":"2021-09-24T08:37:10.005256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://www.kaggle.com/carloalbertobarbano/vgg16-transfer-learning-pytorch","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}