{"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","execution":{"iopub.status.busy":"2023-05-14T11:32:01.665551Z","iopub.execute_input":"2023-05-14T11:32:01.666013Z","iopub.status.idle":"2023-05-14T11:32:01.684471Z","shell.execute_reply.started":"2023-05-14T11:32:01.665970Z","shell.execute_reply":"2023-05-14T11:32:01.683467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport torch.nn as nn\nimport torch\nimport torch.optim as optim\n\nfrom PIL import Image\nfrom matplotlib import pyplot as plt\nfrom torchvision import transforms, models\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nfrom torch.utils.data import ConcatDataset","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:01.686852Z","iopub.execute_input":"2023-05-14T11:32:01.687688Z","iopub.status.idle":"2023-05-14T11:32:05.898373Z","shell.execute_reply.started":"2023-05-14T11:32:01.687652Z","shell.execute_reply":"2023-05-14T11:32:05.897325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data ","metadata":{}},{"cell_type":"code","source":"TRAIN_PATH = '../working/train'\nTEST_PATH = '../working/test1'","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:05.900055Z","iopub.execute_input":"2023-05-14T11:32:05.900693Z","iopub.status.idle":"2023-05-14T11:32:05.905642Z","shell.execute_reply.started":"2023-05-14T11:32:05.900653Z","shell.execute_reply":"2023-05-14T11:32:05.904381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:05.909067Z","iopub.execute_input":"2023-05-14T11:32:05.909726Z","iopub.status.idle":"2023-05-14T11:32:05.982730Z","shell.execute_reply.started":"2023-05-14T11:32:05.909668Z","shell.execute_reply":"2023-05-14T11:32:05.981509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/dogs-vs-cats/')","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:05.984225Z","iopub.execute_input":"2023-05-14T11:32:05.985475Z","iopub.status.idle":"2023-05-14T11:32:05.997243Z","shell.execute_reply.started":"2023-05-14T11:32:05.985418Z","shell.execute_reply":"2023-05-14T11:32:05.996111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -qo \"../input/dogs-vs-cats/test1.zip\" -d ../working/\n!unzip -qo \"../input/dogs-vs-cats/train.zip\" -d ../working/","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:05.998942Z","iopub.execute_input":"2023-05-14T11:32:05.999502Z","iopub.status.idle":"2023-05-14T11:32:26.437195Z","shell.execute_reply.started":"2023-05-14T11:32:05.999456Z","shell.execute_reply":"2023-05-14T11:32:26.435883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_list = []\ntrain_imgs = os.listdir('../working/train')\n\nfor f in train_imgs:\n    cat_or_dog = f.split('.')[0]\n    if cat_or_dog == 'cat':\n        labels_list.append(0)\n    elif cat_or_dog == 'dog':\n        labels_list.append(1)\n        \nprint(f\"Images count: {len(train_imgs)}, Labels count: {len(labels_list)}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:26.439430Z","iopub.execute_input":"2023-05-14T11:32:26.439831Z","iopub.status.idle":"2023-05-14T11:32:26.478984Z","shell.execute_reply.started":"2023-05-14T11:32:26.439793Z","shell.execute_reply":"2023-05-14T11:32:26.477903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.DataFrame(columns=['image', 'label'])\ndf_train['image'] = train_imgs\ndf_train['label'] = labels_list","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:26.480618Z","iopub.execute_input":"2023-05-14T11:32:26.480980Z","iopub.status.idle":"2023-05-14T11:32:26.523156Z","shell.execute_reply.started":"2023-05-14T11:32:26.480949Z","shell.execute_reply":"2023-05-14T11:32:26.522197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:26.524851Z","iopub.execute_input":"2023-05-14T11:32:26.525243Z","iopub.status.idle":"2023-05-14T11:32:26.541853Z","shell.execute_reply.started":"2023-05-14T11:32:26.525208Z","shell.execute_reply":"2023-05-14T11:32:26.539760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.countplot(data=df_train, x='label')\nax.set_title('Count of labels')","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:26.546550Z","iopub.execute_input":"2023-05-14T11:32:26.547160Z","iopub.status.idle":"2023-05-14T11:32:26.917680Z","shell.execute_reply.started":"2023-05-14T11:32:26.547132Z","shell.execute_reply":"2023-05-14T11:32:26.916722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shape_arr = []\n\nfor i in range(0, df_train.shape[0]):\n    img_path = os.path.join(TRAIN_PATH, df_train['image'][i])\n    with Image.open(img_path) as img:\n        width, height = img.size\n        shape_arr.append((width, height))","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:26.919546Z","iopub.execute_input":"2023-05-14T11:32:26.920280Z","iopub.status.idle":"2023-05-14T11:32:30.269685Z","shell.execute_reply.started":"2023-05-14T11:32:26.920242Z","shell.execute_reply":"2023-05-14T11:32:30.268743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = os.path.join(TRAIN_PATH, df_train['image'][1])\nwith Image.open(img_path) as img:\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.271024Z","iopub.execute_input":"2023-05-14T11:32:30.271384Z","iopub.status.idle":"2023-05-14T11:32:30.673875Z","shell.execute_reply.started":"2023-05-14T11:32:30.271354Z","shell.execute_reply":"2023-05-14T11:32:30.671453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Pre-processing","metadata":{}},{"cell_type":"markdown","source":"## Data Augmentation","metadata":{}},{"cell_type":"markdown","source":"We add noise to datasets to improve accuracy.","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(df_train['image'], df_train['label'], test_size=0.15, stratify=df_train['label'])\nprint(f'Train size = {X_train.shape[0]}, Test size = {X_test.shape[0]}')","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.675720Z","iopub.execute_input":"2023-05-14T11:32:30.676321Z","iopub.status.idle":"2023-05-14T11:32:30.713737Z","shell.execute_reply.started":"2023-05-14T11:32:30.676264Z","shell.execute_reply":"2023-05-14T11:32:30.712797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Normalize Transform","metadata":{}},{"cell_type":"markdown","source":"Normalize data. Calculating mean and standard deviation can be time-consuming, so we can choose representative values to normalize the data.","metadata":{}},{"cell_type":"code","source":"normalize_transform = transforms.Compose(\n    [\n        transforms.Resize((128, 128)),\n        transforms.ToTensor(),\n        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.717663Z","iopub.execute_input":"2023-05-14T11:32:30.718322Z","iopub.status.idle":"2023-05-14T11:32:30.731578Z","shell.execute_reply.started":"2023-05-14T11:32:30.718286Z","shell.execute_reply":"2023-05-14T11:32:30.728910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Original Data","metadata":{}},{"cell_type":"code","source":"class DogsCatsDataset(Dataset):\n    def __init__(self, x, y, submission=False):\n        self.x = x.reset_index()\n        self.y = y.reset_index()\n        self.submission = submission\n        \n    def __len__(self):\n        return self.x.shape[0]\n    \n    def load_image(self, path):\n        prefix = TRAIN_PATH\n        if self.submission:\n            prefix = TEST_PATH\n        return Image.open(os.path.join(prefix, path['image']))\n    \n    def __getitem__(self, index):\n        img = self.load_image(self.x.iloc[index])\n        label = self.y.iloc[index]['label']\n        img = normalize_transform(img)\n        sample = {'image': img, 'label': label}\n        return sample","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.733340Z","iopub.execute_input":"2023-05-14T11:32:30.734107Z","iopub.status.idle":"2023-05-14T11:32:30.752884Z","shell.execute_reply.started":"2023-05-14T11:32:30.734068Z","shell.execute_reply":"2023-05-14T11:32:30.751734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = DogsCatsDataset(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.754524Z","iopub.execute_input":"2023-05-14T11:32:30.755271Z","iopub.status.idle":"2023-05-14T11:32:30.766487Z","shell.execute_reply.started":"2023-05-14T11:32:30.755235Z","shell.execute_reply":"2023-05-14T11:32:30.765329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Horizontal Flip","metadata":{}},{"cell_type":"code","source":"class HorizontalFlipTransformedDogsCatsDataset(Dataset):\n    def __init__(self, x, y):\n        self.x = x.reset_index()\n        self.y = y.reset_index()\n        self.transform = transforms.RandomHorizontalFlip(p=1)\n        \n    def __len__(self):\n        return self.x.shape[0]\n    \n    def __getitem__(self, index):\n        img_path = os.path.join(TRAIN_PATH, self.x.iloc[index]['image'])\n        img = Image.open(img_path)\n        img = self.transform(img)\n        label = self.y.iloc[index]['label']\n        img = normalize_transform(img)\n        sample = {'image': img, 'label': label}\n        return sample\n        ","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.768134Z","iopub.execute_input":"2023-05-14T11:32:30.768866Z","iopub.status.idle":"2023-05-14T11:32:30.779007Z","shell.execute_reply.started":"2023-05-14T11:32:30.768830Z","shell.execute_reply":"2023-05-14T11:32:30.777742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"horizontal_flip_transformed_train_data = HorizontalFlipTransformedDogsCatsDataset(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.780686Z","iopub.execute_input":"2023-05-14T11:32:30.781400Z","iopub.status.idle":"2023-05-14T11:32:30.794200Z","shell.execute_reply.started":"2023-05-14T11:32:30.781364Z","shell.execute_reply":"2023-05-14T11:32:30.793250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Vertical Flip","metadata":{}},{"cell_type":"code","source":"class VerticalFlipTransformedDogsCatsDataset(Dataset):\n    def __init__(self, x, y):\n        self.x = x.reset_index()\n        self.y = y.reset_index()\n        self.transform = transforms.RandomVerticalFlip(p=1)\n        \n    def __len__(self):\n        return self.x.shape[0]\n    \n    def __getitem__(self, index):\n        img_path = os.path.join(TRAIN_PATH, self.x.iloc[index]['image'])\n        img = Image.open(img_path)\n        img = self.transform(img)\n        label = self.y.iloc[index]['label']\n        img = normalize_transform(img)\n        sample = {'image': img, 'label': label}\n        return sample\n        ","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.795811Z","iopub.execute_input":"2023-05-14T11:32:30.796466Z","iopub.status.idle":"2023-05-14T11:32:30.805701Z","shell.execute_reply.started":"2023-05-14T11:32:30.796422Z","shell.execute_reply":"2023-05-14T11:32:30.804486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertical_flip_transformed_train_data = VerticalFlipTransformedDogsCatsDataset(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.807249Z","iopub.execute_input":"2023-05-14T11:32:30.807908Z","iopub.status.idle":"2023-05-14T11:32:30.817590Z","shell.execute_reply.started":"2023-05-14T11:32:30.807872Z","shell.execute_reply":"2023-05-14T11:32:30.816313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Random Rotation","metadata":{}},{"cell_type":"code","source":"class RotationTransformedDogsCatsDataset(Dataset):\n    def __init__(self, x, y):\n        self.x = x.reset_index()\n        self.y = y.reset_index()\n        self.transform = transforms.RandomRotation(360)\n        \n    def __len__(self):\n        return self.x.shape[0]\n    \n    def __getitem__(self, index):\n        img_path = os.path.join(TRAIN_PATH, self.x.iloc[index]['image'])\n        img = Image.open(img_path)\n        img = self.transform(img)\n        label = self.y.iloc[index]['label']\n        img = normalize_transform(img)\n        sample = {'image': img, 'label': label}\n        return sample\n        ","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.819397Z","iopub.execute_input":"2023-05-14T11:32:30.820041Z","iopub.status.idle":"2023-05-14T11:32:30.829067Z","shell.execute_reply.started":"2023-05-14T11:32:30.820008Z","shell.execute_reply":"2023-05-14T11:32:30.828049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_transformed_train_data = RotationTransformedDogsCatsDataset(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.830587Z","iopub.execute_input":"2023-05-14T11:32:30.831528Z","iopub.status.idle":"2023-05-14T11:32:30.842745Z","shell.execute_reply.started":"2023-05-14T11:32:30.831494Z","shell.execute_reply":"2023-05-14T11:32:30.841731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### AutoAugment","metadata":{}},{"cell_type":"code","source":"class AutoAugmentTransformedDogsCatsDataset(Dataset):\n    def __init__(self, x, y):\n        self.x = x.reset_index()\n        self.y = y.reset_index()\n        self.transform = transforms.RandomRotation(360)\n        \n    def __len__(self):\n        return self.x.shape[0]\n    \n    def __getitem__(self, index):\n        img_path = os.path.join(TRAIN_PATH, self.x.iloc[index]['image'])\n        img = Image.open(img_path)\n        img = self.transform(img)\n        label = self.y.iloc[index]['label']\n        img = normalize_transform(img)\n        sample = {'image': img, 'label': label}\n        return sample\n        ","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.846574Z","iopub.execute_input":"2023-05-14T11:32:30.847402Z","iopub.status.idle":"2023-05-14T11:32:30.854897Z","shell.execute_reply.started":"2023-05-14T11:32:30.847370Z","shell.execute_reply":"2023-05-14T11:32:30.854136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auto_augment_transformed_train_data = AutoAugmentTransformedDogsCatsDataset(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:32:30.856178Z","iopub.execute_input":"2023-05-14T11:32:30.856933Z","iopub.status.idle":"2023-05-14T11:32:30.867198Z","shell.execute_reply.started":"2023-05-14T11:32:30.856903Z","shell.execute_reply":"2023-05-14T11:32:30.866254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"whole_data = ConcatDataset([train_data, horizontal_flip_transformed_train_data, vertical_flip_transformed_train_data, rotation_transformed_train_data, auto_augment_transformed_train_data])\nprint(len(whole_data))","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:51.980108Z","iopub.execute_input":"2023-05-14T12:07:51.980570Z","iopub.status.idle":"2023-05-14T12:07:51.986923Z","shell.execute_reply.started":"2023-05-14T12:07:51.980532Z","shell.execute_reply":"2023-05-14T12:07:51.986018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create train/test Dataset & DataLoader","metadata":{}},{"cell_type":"code","source":"test_data = DogsCatsDataset(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:53.221497Z","iopub.execute_input":"2023-05-14T12:07:53.222000Z","iopub.status.idle":"2023-05-14T12:07:53.231673Z","shell.execute_reply.started":"2023-05-14T12:07:53.221959Z","shell.execute_reply":"2023-05-14T12:07:53.230641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_data, batch_size=32, drop_last=True, shuffle = True)\ntest_loader = DataLoader(test_data, batch_size=32, drop_last=True, shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:53.612679Z","iopub.execute_input":"2023-05-14T12:07:53.618502Z","iopub.status.idle":"2023-05-14T12:07:53.644449Z","shell.execute_reply.started":"2023-05-14T12:07:53.618461Z","shell.execute_reply":"2023-05-14T12:07:53.643266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, data in enumerate(train_loader):\n    print(data['image'].shape)\n    print(data['label'].shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:53.790404Z","iopub.execute_input":"2023-05-14T12:07:53.791236Z","iopub.status.idle":"2023-05-14T12:07:54.103282Z","shell.execute_reply.started":"2023-05-14T12:07:53.791199Z","shell.execute_reply":"2023-05-14T12:07:54.097814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Selection","metadata":{}},{"cell_type":"markdown","source":"## Implement CNN Model","metadata":{}},{"cell_type":"markdown","source":"3 convolution layers and 3 fully connected layers. Use pooling at all convolution layers. ","metadata":{}},{"cell_type":"code","source":"class ConvNet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        \n        self.cv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3)\n        self.batch_norm1 = nn.BatchNorm2d(num_features=32)\n        self.relu1 = nn.ReLU()\n        self.max_pool1 = nn.MaxPool2d(2)\n        \n        self.cv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3)\n        self.batch_norm2 = nn.BatchNorm2d(num_features=64)\n        self.relu2 = nn.ReLU()\n        self.max_pool2 = nn.MaxPool2d(2)\n        \n        self.cv3 = nn.Conv2d(in_channels=64, out_channels=80, kernel_size=3)\n        self.batch_norm3 = nn.BatchNorm2d(num_features=80)\n        self.relu3 = nn.ReLU()\n        self.max_pool3 = nn.MaxPool2d(2)\n        \n        self.fc1 = nn.Linear(15680, 2000)\n        self.fc_relu1 = nn.ReLU()\n        \n        self.fc2 = nn.Linear(2000, 500)\n        self.fc_relu2 = nn.ReLU()\n        \n        self.fc3 = nn.Linear(500, 2)\n        \n    def forward(self, x):\n        x = self.max_pool1(self.relu1(self.batch_norm1(self.cv1(x))))\n        x = self.max_pool2(self.relu2(self.batch_norm2(self.cv2(x))))\n        x = self.max_pool3(self.relu3(self.batch_norm3(self.cv3(x))))\n        \n        x = x.view(x.size(0), -1)\n        \n        x = self.fc_relu1(self.fc1(x))\n        x = self.fc_relu2(self.fc2(x))\n        \n        x = self.fc3(x)\n        \n        return x","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:54.154784Z","iopub.execute_input":"2023-05-14T12:07:54.155258Z","iopub.status.idle":"2023-05-14T12:07:54.181190Z","shell.execute_reply.started":"2023-05-14T12:07:54.155224Z","shell.execute_reply":"2023-05-14T12:07:54.179922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = ConvNet()\nprint(net)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:54.353813Z","iopub.execute_input":"2023-05-14T12:07:54.354238Z","iopub.status.idle":"2023-05-14T12:07:55.167657Z","shell.execute_reply.started":"2023-05-14T12:07:54.354203Z","shell.execute_reply":"2023-05-14T12:07:55.166649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's train","metadata":{}},{"cell_type":"markdown","source":"Set epochs to 10.","metadata":{}},{"cell_type":"code","source":"n_epochs = 10","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:55.189423Z","iopub.execute_input":"2023-05-14T12:07:55.189992Z","iopub.status.idle":"2023-05-14T12:07:55.197034Z","shell.execute_reply.started":"2023-05-14T12:07:55.189957Z","shell.execute_reply":"2023-05-14T12:07:55.195727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net.train()\nnet.to(DEVICE)\ntotal_loss = 0\nloss_list = []\nacc_list = []\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(net.parameters(), lr=0.001)\n\nfor epoch in range(n_epochs):\n    for batch_no, data in enumerate(train_loader, 0):\n        samples, labels = data['image'], data['label']\n        samples = samples.to(DEVICE)\n        labels = labels.to(DEVICE)\n        \n        optimizer.zero_grad()\n        \n        output = net(samples)\n        loss = criterion(output, labels)\n        loss.backward()\n        optimizer.step()\n        total_loss += loss.item()\n        \n        if batch_no % 100 == 0:\n            pred = torch.argmax(output, dim=1)\n            correct = pred.eq(labels)\n            acc = torch.mean(correct.float())\n            acc = acc.to('cpu')\n            print(f'[Epoch {epoch}/{n_epochs}] Iteration {i} -> Train Loss: {total_loss/200}, Accuracy: {acc}')\n            loss_list.append(total_loss/200)\n            acc_list.append(acc)\n            total_loss = 0","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:07:55.550645Z","iopub.execute_input":"2023-05-14T12:07:55.551131Z","iopub.status.idle":"2023-05-14T12:24:05.598643Z","shell.execute_reply.started":"2023-05-14T12:07:55.551092Z","shell.execute_reply":"2023-05-14T12:24:05.597683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(loss_list[1:], label='loss')\nplt.plot(acc_list[1:], label='accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:24:05.600852Z","iopub.execute_input":"2023-05-14T12:24:05.601233Z","iopub.status.idle":"2023-05-14T12:24:05.830361Z","shell.execute_reply.started":"2023-05-14T12:24:05.601199Z","shell.execute_reply":"2023-05-14T12:24:05.829421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test","metadata":{}},{"cell_type":"markdown","source":"Test our model to predict test data that we splitted before.","metadata":{}},{"cell_type":"code","source":"net.eval()\nloss_list = []\nacc_list = []\nloss = 0\n\nwith torch.no_grad():\n    for batch_no, data in enumerate(test_loader, 0):\n        samples, labels = data['image'], data['label']\n        samples = samples.to(DEVICE)\n        labels = labels.to(DEVICE)\n        \n        optimizer.zero_grad()\n        \n        output = net(samples)\n        loss = criterion(output, labels)\n        \n        pred = torch.argmax(output, dim=1)\n        correct = pred.eq(labels)\n        acc = torch.mean(correct.float())\n        acc = acc.to('cpu')\n        acc_list.append(acc)\n        loss = loss.to('cpu')\n        loss_list.append(loss)\n    \n    print (f\"Mean acc = {np.mean(acc_list)}. Mean loss = {np.mean(loss_list)}\")\n        ","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:24:17.798365Z","iopub.execute_input":"2023-05-14T12:24:17.799168Z","iopub.status.idle":"2023-05-14T12:24:29.317184Z","shell.execute_reply.started":"2023-05-14T12:24:17.799133Z","shell.execute_reply":"2023-05-14T12:24:29.316199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Final Prediction","metadata":{}},{"cell_type":"markdown","source":"Try final data prediction.","metadata":{}},{"cell_type":"code","source":"test_imgs = os.listdir(TEST_PATH)\nlabel_list = [0] * len(test_imgs)\ntest_ids = []\n\nfor fname in test_imgs:\n    test_ids.append(fname.split('.')[0])\n    \nprint(f\"Image count: {len(test_ids)}\\nLabel count: {len(label_list)}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:37:11.478667Z","iopub.execute_input":"2023-05-14T12:37:11.479503Z","iopub.status.idle":"2023-05-14T12:37:11.500354Z","shell.execute_reply.started":"2023-05-14T12:37:11.479468Z","shell.execute_reply":"2023-05-14T12:37:11.499289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(columns=['image', 'label'])\ntest_df['image'] = test_imgs\ntest_df['label'] = label_list","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:37:52.411508Z","iopub.execute_input":"2023-05-14T12:37:52.412079Z","iopub.status.idle":"2023-05-14T12:37:52.439331Z","shell.execute_reply.started":"2023-05-14T12:37:52.412042Z","shell.execute_reply":"2023-05-14T12:37:52.438483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = DogsCatsDataset(test_df['image'], test_df['label'], submission=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:39:10.025158Z","iopub.execute_input":"2023-05-14T12:39:10.025532Z","iopub.status.idle":"2023-05-14T12:39:10.033885Z","shell.execute_reply.started":"2023-05-14T12:39:10.025502Z","shell.execute_reply":"2023-05-14T12:39:10.032809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DataLoader(test_data, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:39:20.360854Z","iopub.execute_input":"2023-05-14T12:39:20.361208Z","iopub.status.idle":"2023-05-14T12:39:20.368046Z","shell.execute_reply.started":"2023-05-14T12:39:20.361178Z","shell.execute_reply":"2023-05-14T12:39:20.366789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net.eval()\nnet.to(DEVICE)\n\npreds = torch.Tensor()\npreds = preds.to(DEVICE)\n\npredictions_frame = pd.DataFrame(columns= ['ids', 'preds'])\npredictions_frame['ids'] = test_ids\n\nwith torch.no_grad():\n    for batch_no, data in enumerate(test_loader):\n        samples, labels = data['image'], data['label']\n        samples = samples.to(DEVICE)\n        labels = labels.to(DEVICE)\n        \n        output = net(samples)\n        \n        pred = torch.argmax(output, dim=1)\n        preds = torch.cat((preds, pred), 0)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:47:31.434682Z","iopub.execute_input":"2023-05-14T12:47:31.435554Z","iopub.status.idle":"2023-05-14T12:48:09.511300Z","shell.execute_reply.started":"2023-05-14T12:47:31.435518Z","shell.execute_reply":"2023-05-14T12:48:09.510304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = preds.to('cpu')\npredictions_frame['preds'] = preds\npredictions_frame.to_csv(f\"submission.csv\", header=False, index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:48:11.505248Z","iopub.execute_input":"2023-05-14T12:48:11.505804Z","iopub.status.idle":"2023-05-14T12:48:11.563204Z","shell.execute_reply.started":"2023-05-14T12:48:11.505745Z","shell.execute_reply":"2023-05-14T12:48:11.562071Z"},"trusted":true},"execution_count":null,"outputs":[]}]}