{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n%matplotlib inline\nimport numpy as np\nimport os\nimport pandas as pd\nimport plotly.offline as py\nimport plotly.graph_objs as go\nimport seaborn as sns\n\nplt.rcParams['figure.figsize']=(20,10)\nprint(os.listdir(\"../input\"))\npy.init_notebook_mode(connected=False)\n\n%env JOBLIB_TEMP_FOLDER=/tmp","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Step 1: Import the Data"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/labels.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['breed'].describe()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Step 2: Explore the Data\n### Let's visualize the data, get an intuition of the distribution"},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = pd.DataFrame({'breed': df['breed'].value_counts().index, 'instances': df['breed'].value_counts().values})\ntemp = temp.sort_values(by=['breed'])\ntemp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trace = go.Bar(x=temp['breed'], y=temp['instances'])\ndata = [trace]\nlayout = go.Layout(\n        title='Breed Counts',\n        autosize=False,\n        width=5000,\n        height=500,\n        margin=dict(\n            l=100,\n            r=100,\n            b=100,\n            t=100\n        )\n    )\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As we can see, almost all the breeds have sufficient enough training images. Hence, we won't have to generate additional data right now."},{"metadata":{},"cell_type":"markdown","source":"### Step 3: One-Hot Encode the Breed columns and prepare the data for a classifier"},{"metadata":{"trusted":true},"cell_type":"code","source":"df['breed'] = pd.get_dummies(df['breed']).values.tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Let's create the train data by creating a torch dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"from torchvision import transforms\nfrom torchvision.datasets import ImageFolder\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data.dataset import Dataset\nfrom PIL import Image\n\nimport torchvision.models as models\nimport torch.nn as nn\nimport torch.optim as optim","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['image_path'] = '../input/train/' + df['id'].astype(str) + '.jpg'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = torch.tensor(df['breed'].tolist())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, image_path, labels, train=True):\n        self.image_path = image_path\n        self.labels = labels\n        self.transform = transforms.Compose([\n                        transforms.Resize(255),\n                        transforms.CenterCrop(224),\n                        transforms.ToTensor(),\n                        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n                    ])\n\n    def __getitem__(self, index):\n        image = Image.open(self.image_path[index])\n        t_image = self.transform(image)\n        \n        return t_image, self.labels[index]\n\n    def __len__(self):  # return count of sample we have\n        return len(self.image_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dataset = CustomDataset(df['image_path'], labels, train=True)\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = models.densenet161(pretrained=True)\n# Turn off training for their parameters\nfor param in model.parameters():\n    param.requires_grad = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classifier = nn.Sequential(nn.Linear(255, 1024),\n                           nn.ReLU(),\n                           nn.Linear(1024, 512),\n                           nn.ReLU(),\n                           nn.Linear(512, 120),\n                           nn.LogSoftmax(dim=1))\n# Replace default classifier with new classifier\nmodel.classifier = classifier","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n# Move model to the device specified above\nmodel.to(device)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"criterion = nn.NLLLoss()\n# Set the optimizer function using torch.optim as optim library\noptimizer = optim.Adam(model.classifier.parameters())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}