{"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":"!unzip --qq ../input/dogs-vs-cats/train.zip -d .\n!unzip -qq ../input/dogs-vs-cats/test1.zip -d .","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:11:41.368763Z","iopub.execute_input":"2021-10-01T13:11:41.369580Z","iopub.status.idle":"2021-10-01T13:11:58.244469Z","shell.execute_reply.started":"2021-10-01T13:11:41.369471Z","shell.execute_reply":"2021-10-01T13:11:58.243324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Library ","metadata":{"execution":{"iopub.status.busy":"2021-10-01T11:17:26.638973Z","iopub.execute_input":"2021-10-01T11:17:26.639293Z","iopub.status.idle":"2021-10-01T11:17:27.424392Z","shell.execute_reply.started":"2021-10-01T11:17:26.639259Z","shell.execute_reply":"2021-10-01T11:17:27.423188Z"}}},{"cell_type":"code","source":"import torchvision\nimport torch.nn as nn\nimport torchvision.transforms as v_transforms\nfrom torch.utils.data import DataLoader\nfrom PIL import Image\nimport pandas as pd\nimport glob\nimport torch\nimport os\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:11:58.246600Z","iopub.execute_input":"2021-10-01T13:11:58.247091Z","iopub.status.idle":"2021-10-01T13:11:59.467878Z","shell.execute_reply.started":"2021-10-01T13:11:58.247040Z","shell.execute_reply":"2021-10-01T13:11:59.467063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data","metadata":{}},{"cell_type":"code","source":"all_training_files = glob.glob(\"./train/*.jpg\")[:1000]","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:13:01.267628Z","iopub.execute_input":"2021-10-01T13:13:01.268528Z","iopub.status.idle":"2021-10-01T13:13:01.368762Z","shell.execute_reply.started":"2021-10-01T13:13:01.268476Z","shell.execute_reply":"2021-10-01T13:13:01.367827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os_training_files = os.listdir(\"./train\")[:1000]","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:12:49.427324Z","iopub.execute_input":"2021-10-01T13:12:49.428165Z","iopub.status.idle":"2021-10-01T13:12:49.448948Z","shell.execute_reply.started":"2021-10-01T13:12:49.428115Z","shell.execute_reply":"2021-10-01T13:12:49.448313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os_training_files[0]","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:12:53.675693Z","iopub.execute_input":"2021-10-01T13:12:53.676444Z","iopub.status.idle":"2021-10-01T13:12:53.683852Z","shell.execute_reply.started":"2021-10-01T13:12:53.676378Z","shell.execute_reply":"2021-10-01T13:12:53.683143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(all_training_files)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:13:20.836670Z","iopub.execute_input":"2021-10-01T13:13:20.837323Z","iopub.status.idle":"2021-10-01T13:13:20.843066Z","shell.execute_reply.started":"2021-10-01T13:13:20.837281Z","shell.execute_reply":"2021-10-01T13:13:20.842245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df = pd.DataFrame()\ndata_df['path'] = all_training_files","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:13:28.788076Z","iopub.execute_input":"2021-10-01T13:13:28.788392Z","iopub.status.idle":"2021-10-01T13:13:28.807595Z","shell.execute_reply.started":"2021-10-01T13:13:28.788358Z","shell.execute_reply":"2021-10-01T13:13:28.806337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df.sample(10)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:13:39.858854Z","iopub.execute_input":"2021-10-01T13:13:39.859156Z","iopub.status.idle":"2021-10-01T13:13:39.873512Z","shell.execute_reply.started":"2021-10-01T13:13:39.859123Z","shell.execute_reply":"2021-10-01T13:13:39.872565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# manipulate str, path","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.path.basename(all_training_files[1]).split('.')[0]","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:14:59.168249Z","iopub.execute_input":"2021-10-01T13:14:59.168553Z","iopub.status.idle":"2021-10-01T13:14:59.174935Z","shell.execute_reply.started":"2021-10-01T13:14:59.168520Z","shell.execute_reply":"2021-10-01T13:14:59.174083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df['data_class'] = data_df['path'].apply(lambda x: os.path.basename(x).split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:15:28.608005Z","iopub.execute_input":"2021-10-01T13:15:28.608320Z","iopub.status.idle":"2021-10-01T13:15:28.616750Z","shell.execute_reply.started":"2021-10-01T13:15:28.608288Z","shell.execute_reply":"2021-10-01T13:15:28.615813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df['encoded_class'] = data_df['data_class'].map({\"dog\": 0, \"cat\": 1})","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:17:00.607634Z","iopub.execute_input":"2021-10-01T13:17:00.607924Z","iopub.status.idle":"2021-10-01T13:17:00.618401Z","shell.execute_reply.started":"2021-10-01T13:17:00.607897Z","shell.execute_reply":"2021-10-01T13:17:00.617400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:17:03.246718Z","iopub.execute_input":"2021-10-01T13:17:03.247016Z","iopub.status.idle":"2021-10-01T13:17:03.261709Z","shell.execute_reply.started":"2021-10-01T13:17:03.246987Z","shell.execute_reply":"2021-10-01T13:17:03.260724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:18:18.317130Z","iopub.execute_input":"2021-10-01T13:18:18.317464Z","iopub.status.idle":"2021-10-01T13:18:18.321873Z","shell.execute_reply.started":"2021-10-01T13:18:18.317429Z","shell.execute_reply":"2021-10-01T13:18:18.320850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = Image.open(np.random.choice(all_training_files))\nprint(image.size)\nimage","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:18:34.328051Z","iopub.execute_input":"2021-10-01T13:18:34.328504Z","iopub.status.idle":"2021-10-01T13:18:34.399546Z","shell.execute_reply.started":"2021-10-01T13:18:34.328461Z","shell.execute_reply":"2021-10-01T13:18:34.398581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset","metadata":{}},{"cell_type":"code","source":"transform = v_transforms.Compose(\n    [v_transforms.Resize((256, 256)),\n     v_transforms.ToTensor(),\n#      v_transforms.Normalize((0.5), (0.5))\n    ])\n\nbatch_size = 8","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:25:51.367963Z","iopub.execute_input":"2021-10-01T13:25:51.368382Z","iopub.status.idle":"2021-10-01T13:25:51.374001Z","shell.execute_reply.started":"2021-10-01T13:25:51.368349Z","shell.execute_reply":"2021-10-01T13:25:51.373073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind = 1","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:23:52.423988Z","iopub.execute_input":"2021-10-01T13:23:52.424351Z","iopub.status.idle":"2021-10-01T13:23:52.433110Z","shell.execute_reply.started":"2021-10-01T13:23:52.424297Z","shell.execute_reply":"2021-10-01T13:23:52.432036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data_df.iloc[ind]['encoded_class']), \nImage.open(data_df.iloc[ind]['path'])","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:23:52.705932Z","iopub.execute_input":"2021-10-01T13:23:52.706198Z","iopub.status.idle":"2021-10-01T13:23:52.804798Z","shell.execute_reply.started":"2021-10-01T13:23:52.706170Z","shell.execute_reply":"2021-10-01T13:23:52.804101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CatDogDataset(object):\n    def __init__(self, data_df, transform=None):\n        self.data_df = data_df\n        self.transform = transform\n        \n        \n    def __len__(self):\n        return data_df.shape[0]\n    \n    def __getitem__(self, idx):\n        image_path = data_df.iloc[idx]['path']\n        label = data_df.iloc[idx]['encoded_class']\n        image = Image.open(image_path)\n        if transform is not None:\n            image = transform(image)\n            \n        return image, label\n            \n        ","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:25:24.160856Z","iopub.execute_input":"2021-10-01T13:25:24.161184Z","iopub.status.idle":"2021-10-01T13:25:24.169741Z","shell.execute_reply.started":"2021-10-01T13:25:24.161152Z","shell.execute_reply":"2021-10-01T13:25:24.168608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_dog_dataset = CatDogDataset(data_df=data_df, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:26:11.482211Z","iopub.execute_input":"2021-10-01T13:26:11.482678Z","iopub.status.idle":"2021-10-01T13:26:11.485938Z","shell.execute_reply.started":"2021-10-01T13:26:11.482647Z","shell.execute_reply":"2021-10-01T13:26:11.485342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(cat_dog_dataset), data_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:27:08.629028Z","iopub.execute_input":"2021-10-01T13:27:08.629326Z","iopub.status.idle":"2021-10-01T13:27:08.636187Z","shell.execute_reply.started":"2021-10-01T13:27:08.629296Z","shell.execute_reply":"2021-10-01T13:27:08.635264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image, label = cat_dog_dataset[0]\n\nplt.imshow(image.numpy().transpose(1, 2, 0))\nplt.axis(\"off\")\nplt.title(f\"Class: {label}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:27:34.153187Z","iopub.execute_input":"2021-10-01T13:27:34.154061Z","iopub.status.idle":"2021-10-01T13:27:34.414895Z","shell.execute_reply.started":"2021-10-01T13:27:34.154002Z","shell.execute_reply":"2021-10-01T13:27:34.413890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape, label","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:27:41.454787Z","iopub.execute_input":"2021-10-01T13:27:41.455074Z","iopub.status.idle":"2021-10-01T13:27:41.462173Z","shell.execute_reply.started":"2021-10-01T13:27:41.455045Z","shell.execute_reply":"2021-10-01T13:27:41.461282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_loader = DataLoader(cat_dog_dataset, batch_size=16)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:28:18.802958Z","iopub.execute_input":"2021-10-01T13:28:18.803298Z","iopub.status.idle":"2021-10-01T13:28:18.809325Z","shell.execute_reply.started":"2021-10-01T13:28:18.803265Z","shell.execute_reply":"2021-10-01T13:28:18.808306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"help(DataLoader)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:29:35.813793Z","iopub.execute_input":"2021-10-01T13:29:35.814118Z","iopub.status.idle":"2021-10-01T13:29:35.832055Z","shell.execute_reply.started":"2021-10-01T13:29:35.814084Z","shell.execute_reply":"2021-10-01T13:29:35.831206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch in train_data_loader:\n    break","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:32:21.816475Z","iopub.execute_input":"2021-10-01T13:32:21.816818Z","iopub.status.idle":"2021-10-01T13:32:21.912265Z","shell.execute_reply.started":"2021-10-01T13:32:21.816782Z","shell.execute_reply":"2021-10-01T13:32:21.911436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images, labels = batch","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:32:24.334741Z","iopub.execute_input":"2021-10-01T13:32:24.335042Z","iopub.status.idle":"2021-10-01T13:32:24.339721Z","shell.execute_reply.started":"2021-10-01T13:32:24.335012Z","shell.execute_reply":"2021-10-01T13:32:24.338718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_images(images, labels): \n\n    f, ax = plt.subplots(4,4, figsize=(10,15))\n\n    for i, image_id in enumerate(images[:16]):\n        image = images[i]\n        label = labels[i]\n        ax[i//4, i%4].imshow(image.numpy().transpose(1, 2, 0)) \n        ax[i//4, i%4].axis('off')\n\n        ax[i//4, i%4].set_title(f\"Class: {label}\")\n    plt.tight_layout()\n    plt.show() ","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:32:24.672195Z","iopub.execute_input":"2021-10-01T13:32:24.672517Z","iopub.status.idle":"2021-10-01T13:32:24.680801Z","shell.execute_reply.started":"2021-10-01T13:32:24.672478Z","shell.execute_reply":"2021-10-01T13:32:24.679676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(images, labels)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:32:35.447093Z","iopub.execute_input":"2021-10-01T13:32:35.448050Z","iopub.status.idle":"2021-10-01T13:32:37.156825Z","shell.execute_reply.started":"2021-10-01T13:32:35.448009Z","shell.execute_reply":"2021-10-01T13:32:37.155820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pretrained model","metadata":{}},{"cell_type":"markdown","source":"## torchvision","metadata":{}},{"cell_type":"code","source":"!pip install --quiet torchsummary","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:35:52.807172Z","iopub.execute_input":"2021-10-01T13:35:52.808032Z","iopub.status.idle":"2021-10-01T13:36:03.532693Z","shell.execute_reply.started":"2021-10-01T13:35:52.807980Z","shell.execute_reply":"2021-10-01T13:36:03.531608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nimport torchsummary","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:36:03.536866Z","iopub.execute_input":"2021-10-01T13:36:03.537123Z","iopub.status.idle":"2021-10-01T13:36:03.547367Z","shell.execute_reply.started":"2021-10-01T13:36:03.537093Z","shell.execute_reply":"2021-10-01T13:36:03.546348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexnet_model = torchvision.models.alexnet(pretrained=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:37:03.239273Z","iopub.execute_input":"2021-10-01T13:37:03.240165Z","iopub.status.idle":"2021-10-01T13:37:03.958562Z","shell.execute_reply.started":"2021-10-01T13:37:03.240121Z","shell.execute_reply":"2021-10-01T13:37:03.957661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torchsummary.summary(alexnet_model, input_size=(3, 224, 224), device=\"cpu\")","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:37:41.087996Z","iopub.execute_input":"2021-10-01T13:37:41.088968Z","iopub.status.idle":"2021-10-01T13:37:41.309971Z","shell.execute_reply.started":"2021-10-01T13:37:41.088922Z","shell.execute_reply":"2021-10-01T13:37:41.309025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexnet_model","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:37:54.137450Z","iopub.execute_input":"2021-10-01T13:37:54.137735Z","iopub.status.idle":"2021-10-01T13:37:54.145009Z","shell.execute_reply.started":"2021-10-01T13:37:54.137706Z","shell.execute_reply":"2021-10-01T13:37:54.144037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexnet_model.features","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:37:59.220782Z","iopub.execute_input":"2021-10-01T13:37:59.221097Z","iopub.status.idle":"2021-10-01T13:37:59.228275Z","shell.execute_reply.started":"2021-10-01T13:37:59.221068Z","shell.execute_reply":"2021-10-01T13:37:59.227370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SamplePretrainedModel(nn.Module):\n    def __init__(self, num_classes):\n        super(SamplePretrainedModel, self).__init__()\n        self.num_classes = num_classes\n        self.base_model = torchvision.models.alexnet(pretrained=True).features\n        self.fc = nn.Linear(12544, num_classes)\n\n    def forward(self, x):\n        x = self.base_model(x)\n\n        x = torch.flatten(x, start_dim=1)\n        x = self.fc(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:42:36.423512Z","iopub.execute_input":"2021-10-01T13:42:36.423876Z","iopub.status.idle":"2021-10-01T13:42:36.432425Z","shell.execute_reply.started":"2021-10-01T13:42:36.423839Z","shell.execute_reply":"2021-10-01T13:42:36.431319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_pretrained_model = SamplePretrainedModel(num_classes=2)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:42:36.617524Z","iopub.execute_input":"2021-10-01T13:42:36.617842Z","iopub.status.idle":"2021-10-01T13:42:37.713133Z","shell.execute_reply.started":"2021-10-01T13:42:36.617804Z","shell.execute_reply":"2021-10-01T13:42:37.712095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_pretrained_model(torch.rand(1, 3, 256, 256)).shape","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:42:47.481358Z","iopub.execute_input":"2021-10-01T13:42:47.481680Z","iopub.status.idle":"2021-10-01T13:42:47.519049Z","shell.execute_reply.started":"2021-10-01T13:42:47.481645Z","shell.execute_reply":"2021-10-01T13:42:47.518128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_pretrained_model","metadata":{"execution":{"iopub.status.busy":"2021-10-01T11:43:41.935545Z","iopub.execute_input":"2021-10-01T11:43:41.936217Z","iopub.status.idle":"2021-10-01T11:43:41.94261Z","shell.execute_reply.started":"2021-10-01T11:43:41.936174Z","shell.execute_reply":"2021-10-01T11:43:41.941807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install timm","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:43:54.302261Z","iopub.execute_input":"2021-10-01T13:43:54.302864Z","iopub.status.idle":"2021-10-01T13:44:03.719414Z","shell.execute_reply.started":"2021-10-01T13:43:54.302825Z","shell.execute_reply":"2021-10-01T13:44:03.718316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [timm](https://fastai.github.io/timmdocs/) library","metadata":{}},{"cell_type":"code","source":"import timm","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:44:16.362834Z","iopub.execute_input":"2021-10-01T13:44:16.363885Z","iopub.status.idle":"2021-10-01T13:44:17.406714Z","shell.execute_reply.started":"2021-10-01T13:44:16.363831Z","shell.execute_reply":"2021-10-01T13:44:17.405641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"timm_model = timm.create_model('resnet18', num_classes=2)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:47:23.621550Z","iopub.execute_input":"2021-10-01T13:47:23.621869Z","iopub.status.idle":"2021-10-01T13:47:23.847115Z","shell.execute_reply.started":"2021-10-01T13:47:23.621835Z","shell.execute_reply":"2021-10-01T13:47:23.846242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torchsummary.summary(timm_model, input_size=(3, 224, 224), device=\"cpu\")","metadata":{"execution":{"iopub.status.busy":"2021-10-01T13:47:28.660410Z","iopub.execute_input":"2021-10-01T13:47:28.660715Z","iopub.status.idle":"2021-10-01T13:47:28.844744Z","shell.execute_reply.started":"2021-10-01T13:47:28.660686Z","shell.execute_reply":"2021-10-01T13:47:28.843810Z"},"trusted":true},"execution_count":null,"outputs":[]}]}