{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\n%matplotlib inline\n%config InlineBackend.figure_format = 'retina'\n\nimport matplotlib.pyplot as plt\n\nimport torch\nfrom torch import nn\nfrom torch import optim\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms, models\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# import module we'll need to import our custom module\nfrom shutil import copyfile\n\n# copy our file into the working directory (make sure it has .py suffix)\ncopyfile(src = \"../input/view-class/view_helper.py\", dst = \"../working/view_helper.py\")\n\n# import all our functions\nimport view_helper as helper\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"845d4d1ca359a0774e1b7ba83081394d0f255d1d"},"cell_type":"code","source":"print(os.listdir(\"../input/dogs-vs-cats-for-pytorch/cat_dog_data/Cat_Dog_data\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"513d77fe38d62ff991eacdcf29a8640db7f20689"},"cell_type":"code","source":"data_dir = '../input/dogs-vs-cats-for-pytorch/cat_dog_data/Cat_Dog_data'\n\n# TODO: Define transforms for the training data and testing data\ntrain_transforms = transforms.Compose([transforms.RandomRotation(30),\n                                       transforms.RandomResizedCrop(224),\n                                       transforms.RandomHorizontalFlip(),\n                                       transforms.ToTensor(),\n                                       transforms.Normalize([0.485, 0.456, 0.406],\n                                                            [0.229, 0.224, 0.225])])\n\ntest_transforms = transforms.Compose([transforms.Resize(255),\n                                      transforms.CenterCrop(224),\n                                      transforms.ToTensor(),\n                                      transforms.Normalize([0.485, 0.456, 0.406],\n                                                           [0.229, 0.224, 0.225])])\n\n# Pass transforms in here, then run the next cell to see how the transforms look\ntrain_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)\ntest_data = datasets.ImageFolder(data_dir + '/test', transform=test_transforms)\n\ntrainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)\ntestloader = torch.utils.data.DataLoader(test_data, batch_size=64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5777bcabbc0b7c8299ac5b4106f19707102dbc6b"},"cell_type":"code","source":"# change this to the trainloader or testloader \ndata_iter = iter(trainloader)\n\nimages, labels = next(data_iter)\nfig, axes = plt.subplots(figsize=(10,10), ncols=4)\nfor ii in range(4):\n    ax = axes[ii]\n    helper.imshow(images[ii], ax=ax, normalize=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba351141fb582f91e2920c788471ce3c5a7b6a38"},"cell_type":"code","source":"# Use GPU if it's available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nmodel = models.densenet121(pretrained=True)\n\n# Freeze parameters so we don't backprop through them\nfor param in model.parameters():\n    param.requires_grad = False\n    \nmodel.classifier = nn.Sequential(nn.Linear(1024, 256),\n                                 nn.ReLU(),\n                                 nn.Dropout(0.2),\n                                 nn.Linear(256, 2),\n                                 nn.LogSoftmax(dim=1))\n\ncriterion = nn.NLLLoss()\n\n# Only train the classifier parameters, feature parameters are frozen\noptimizer = optim.Adam(model.classifier.parameters(), lr=0.003)\n\nmodel.to(device);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d30ac8407dd22f8a3ce17f52834618fd7115b034"},"cell_type":"code","source":"torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed6ce60e59716a060ceb571c2556ce473d53eb9d"},"cell_type":"code","source":"# epochs = 1\n# steps = 0\n# running_loss = 0\n# print_every = 5\n# for epoch in range(epochs):\n#     for inputs, labels in trainloader:\n#         steps += 1\n#         # Move input and label tensors to the default device\n#         inputs, labels = inputs.to(device), labels.to(device)\n        \n#         optimizer.zero_grad()\n        \n#         logps = model.forward(inputs)\n#         loss = criterion(logps, labels)\n#         loss.backward()\n#         optimizer.step()\n\n#         running_loss += loss.item()\n        \n#         if steps % print_every == 0:\n#             test_loss = 0\n#             accuracy = 0\n#             model.eval()\n#             with torch.no_grad():\n#                 for inputs, labels in testloader:\n#                     inputs, labels = inputs.to(device), labels.to(device)\n#                     logps = model.forward(inputs)\n#                     batch_loss = criterion(logps, labels)\n                    \n#                     test_loss += batch_loss.item()\n                    \n#                     # Calculate accuracy\n#                     ps = torch.exp(logps)\n#                     top_p, top_class = ps.topk(1, dim=1)\n#                     equals = top_class == labels.view(*top_class.shape)\n#                     accuracy += torch.mean(equals.type(torch.FloatTensor)).item()\n                    \n#             print(f\"Epoch {epoch+1}/{epochs}.. \"\n#                   f\"Train loss: {running_loss/print_every:.3f}.. \"\n#                   f\"Test loss: {test_loss/len(testloader):.3f}.. \"\n#                   f\"Test accuracy: {accuracy/len(testloader):.3f}\")\n#             running_loss = 0\n#             model.train()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df18690b03e9a840925535c4cb437f482b185d65"},"cell_type":"code","source":"print(\"Our model: \\n\\n\", model, '\\n')\nprint(\"The state dict keys: \\n\\n\", model.state_dict().keys())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"737d0eb6ad38c1f47fb72b81e01f0accc857e937"},"cell_type":"code","source":"#torch.save(model.state_dict(), 'cat_dog_dense121.pth')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38df667459424c17e2542c87e0c62686e37dc26a"},"cell_type":"code","source":"print(os.listdir(\"../input/dogs-vs-cats-for-pytorch/cat_dog_data/Cat_Dog_data\"))\nprint(os.listdir(\"../input/cat-dog-dense121-local\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"821e8e0379e76ef7d16f48f35dec4126a0bc95ce"},"cell_type":"code","source":"ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"71938232f8c1726b532352c469f1c36371175386","scrolled":true},"cell_type":"code","source":"state_dict = torch.load('../input/cat-dog-dense121-local/cat_dog_dense121_local.pth')\nprint(state_dict.keys())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d818c2fb58ba2dc731285c332c4ae1193a0b96a1"},"cell_type":"code","source":"model.load_state_dict(state_dict)\nmodel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a64ca2e647e37ac2b7fb167d9ff0a60e8f531b34"},"cell_type":"code","source":"# change this to the trainloader or testloader \ndata_iter = iter(testloader)\n\nimages, labels = next(data_iter)\nfig, axes = plt.subplots(figsize=(10,10), ncols=4)\nfor ii in range(4):\n    ax = axes[ii]\n    helper.imshow(images[ii], ax=ax, normalize=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58dca9891bd59b9181d1816f35d4505718dae26d"},"cell_type":"markdown","source":"## Test the Network"},{"metadata":{"trusted":true,"_uuid":"3b26661b14ab9a2a7e78d1fcf8d01b86532301d0"},"cell_type":"code","source":"model.to('cpu')\nmodel.eval()\n\ndata_iter = iter(testloader)\n\nimages, labels = next(data_iter)\nfig, axes = plt.subplots(figsize=(10,10), ncols=4)\n\nfor ii in range(4):\n    ax = axes[ii]\n    helper.imshow(images[ii], ax=ax, normalize=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"067566c0df242f371054503b7536f09b0f28b76d"},"cell_type":"code","source":"with torch.no_grad():\n    output = model.forward(images)\n\nps = torch.exp(output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7687d09d2b79014f2784adaac4dab1669ed367e1"},"cell_type":"code","source":"random_img = np.random.randint(64, size=1)[0]\nrandom_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6754fe793fcdd67216dcb3009354223b7976f172"},"cell_type":"code","source":"# get the probability\nprobability = ps[random_img].data.numpy().squeeze()\nprobability","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce72da2b11a83a39e018bcd99c92b06c56ee69bd"},"cell_type":"code","source":"helper.imshow(images[random_img], normalize=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"952629fb33b73638f4c76f6681349ec70578aaf0"},"cell_type":"code","source":"ind = np.arange(2)\nlabels = ['Cat', 'Dog',]\nwidth = 0.35\nlocations = ind\n\nclass_probability = plt.barh(ind, probability, width, alpha=.7, label='Cats vs Dogs')\n\nplt.yticks(np.arange(10))\nplt.title('Class Probability')\nplt.yticks(locations, labels)\n\n#legend\nplt.legend()\nplt.ylim(top=3)\nplt.ylim(bottom=-2)\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a879e4e1824940053b9775ef9cb1855d80e27d93"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}