{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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\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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import zipfile\n\n# train data\nif 'train' not in os.listdir('.'):\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n        z.extractall(\".\")\n\n# test data\nif 'test1' not in os.listdir('.'):\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n        z.extractall(\".\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_DIR = '/kaggle/working' \n\nTRAIN_DIR = DATA_DIR + '/train'                           # Contains training images\nTEST_DIR = DATA_DIR + '/test1'                             # Contains test images\n\nTRAIN_CSV = DATA_DIR + '/train.csv'                       # Contains real labels for training images\nTEST_CSV = './jovian-pytorch-z2g/submission.csv'   # Contains dummy labels for test image\n\nfiles = os.listdir(TRAIN_DIR)\nfiles[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\n\ndevice = (\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\ntrain_df = pd.DataFrame(columns=[\"img_name\",\"label\"])\ntrain_df[\"img_name\"] = os.listdir(\"train/\")\nfor idx, i in enumerate(os.listdir(\"train/\")):\n    if \"cat\" in i:\n        train_df[\"label\"][idx] = 0\n    if \"dog\" in i:\n        train_df[\"label\"][idx] = 1\n\ntrain_df.to_csv (r'train_csv.csv', index = False, header=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from torch.utils.data import Dataset\nimport pandas as pd\nimport os\nfrom PIL import Image\nimport torch\n\nclass CatsAndDogsDataset(Dataset):\n    def __init__(self, root_dir, annotation_file, transform=None):\n        self.root_dir = root_dir\n        self.annotations = pd.read_csv(annotation_file)\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.annotations)\n\n    def __getitem__(self, index):\n        img_id = self.annotations.iloc[index, 0]\n        img = Image.open(os.path.join(self.root_dir, img_id)).convert(\"RGB\")\n        y_label = torch.tensor(float(self.annotations.iloc[index, 1]))\n\n        if self.transform is not None:\n            img = self.transform(img)\n\n        return (img, y_label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch.nn as nn\nimport torchvision.models as models\n\nclass CNN(nn.Module):\n    def __init__(self, train_CNN=False, num_classes=1):\n        super(CNN, self).__init__()\n        self.train_CNN = train_CNN\n        self.inception = models.inception_v3(pretrained=True, aux_logits=False)\n        self.inception.fc = nn.Linear(self.inception.fc.in_features, num_classes)\n        self.relu = nn.ReLU()\n        self.dropout = nn.Dropout(0.5)\n        self.sigmoid = nn.Sigmoid()\n\n    def forward(self, images):\n        features = self.inception(images)\n        return self.sigmoid(self.dropout(self.relu(features))).squeeze(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader\nimport torchvision.transforms as transforms\nfrom tqdm import tqdm\ndevice = (\"cuda\" if torch.cuda.is_available() else \"cpu\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"transform = transforms.Compose(\n        [\n            transforms.Resize((356, 356)),\n            transforms.RandomCrop((299, 299)),\n            transforms.ToTensor(),\n            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n        ]\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_epochs = 10\nlearning_rate = 0.00001\ntrain_CNN = False\nbatch_size = 32\nshuffle = True\npin_memory = True\nnum_workers = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = CatsAndDogsDataset(\"train\",\"train_csv.csv\",transform=transform)\ntrain_set, validation_set = torch.utils.data.random_split(dataset,[20000,5000])\ntrain_loader = DataLoader(dataset=train_set, shuffle=shuffle, batch_size=batch_size,num_workers=num_workers,pin_memory=pin_memory)\nvalidation_loader = DataLoader(dataset=validation_set, shuffle=shuffle, batch_size=batch_size,num_workers=num_workers, pin_memory=pin_memory)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = CNN().to(device)\n\ncriterion = nn.BCELoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)\n\nfor name, param in model.inception.named_parameters():\n    if \"fc.weight\" in name or \"fc.bias\" in name:\n        param.requires_grad = True\n    else:\n        param.requires_grad = train_CNN","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def check_accuracy(loader, model):\n    if loader == train_loader:\n        print(\"Checking accuracy on training data\")\n    else:\n        print(\"Checking accuracy on validation data\")\n\n    num_correct = 0\n    num_samples = 0\n    model.eval()\n\n    with torch.no_grad():\n        for x, y in loader:\n            x = x.to(device=device)\n            y = y.to(device=device)\n\n            scores = model(x)\n            predictions = torch.tensor([1.0 if i >= 0.5 else 0.0 for i in scores]).to(device)\n            num_correct += (predictions == y).sum()\n            num_samples += predictions.size(0)\n    return f\"{float(num_correct)/float(num_samples)*100:.2f}\"\n    print(f\"Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}\")\n    model.train()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def train():\n    model.train()\n    for epoch in range(num_epochs):\n        loop = tqdm(train_loader, total = len(train_loader), leave = True)\n        if epoch % 2 == 0:\n            loop.set_postfix(val_acc = check_accuracy(validation_loader, model))\n        for imgs, labels in loop:\n            imgs = imgs.to(device)\n            labels = labels.to(device)\n            outputs = model(imgs)\n            loss = criterion(outputs, labels)\n            optimizer.zero_grad()\n            loss.backward()\n            optimizer.step()\n            loop.set_description(f\"Epoch [{epoch}/{num_epochs}]\")\n            loop.set_postfix(loss = loss.item())\n\nif __name__ == \"__main__\":\n    train()","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}