{"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":"markdown","source":"# Unzip","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:00:32.190462Z","iopub.execute_input":"2023-08-07T12:00:32.190994Z","iopub.status.idle":"2023-08-07T12:00:53.431292Z","shell.execute_reply.started":"2023-08-07T12:00:32.190948Z","shell.execute_reply":"2023-08-07T12:00:53.430191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing","metadata":{}},{"cell_type":"markdown","source":"## One Image","metadata":{}},{"cell_type":"code","source":"import cv2\nfrom glob import glob\nimport matplotlib.pyplot as plt\n\n# Gathering Path\ntrain_path = glob('/kaggle/working/train/*')\nsample_path = train_path[0]\n\n# Image Reading using CV2\nimage = cv2.imread(sample_path)\n\n# Visualizing\nplt.imshow(image);\nplt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:00:53.436355Z","iopub.execute_input":"2023-08-07T12:00:53.436728Z","iopub.status.idle":"2023-08-07T12:00:54.383345Z","shell.execute_reply.started":"2023-08-07T12:00:53.436698Z","shell.execute_reply":"2023-08-07T12:00:54.382181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Customizing Visualization","metadata":{}},{"cell_type":"code","source":"import random\n\ndef visualize(num_images):\n    num_rows = int(num_images ** 0.5)\n    num_cols = (num_images + num_rows - 1) // num_rows\n    \n    image_paths = random.sample(train_path, num_images)\n    \n    plt.figure(figsize=(8, 8))\n\n    for i, image_path in enumerate(image_paths):\n        image = cv2.imread(image_path)\n        plt.subplot(num_rows, num_cols, i + 1)\n        plt.imshow(image)\n        plt.axis('off')\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:00:54.385017Z","iopub.execute_input":"2023-08-07T12:00:54.386175Z","iopub.status.idle":"2023-08-07T12:00:54.394057Z","shell.execute_reply.started":"2023-08-07T12:00:54.386130Z","shell.execute_reply":"2023-08-07T12:00:54.392965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(9)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:00:54.396315Z","iopub.execute_input":"2023-08-07T12:00:54.397252Z","iopub.status.idle":"2023-08-07T12:00:55.341708Z","shell.execute_reply.started":"2023-08-07T12:00:54.397216Z","shell.execute_reply":"2023-08-07T12:00:55.340611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Size & Channels","metadata":{}},{"cell_type":"code","source":"def check_imgsz(num_images):\n    image_paths = random.sample(train_path, num_images)\n    \n    for i, image_path in enumerate(image_paths):\n        sample = cv2.imread(image_path)\n        print(f\"Sample {i} Image Height : {sample.shape[0]} Image Width : {sample.shape[1]} Image Channel : {sample.shape[2]}\")","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:00:55.343230Z","iopub.execute_input":"2023-08-07T12:00:55.343606Z","iopub.status.idle":"2023-08-07T12:00:55.349509Z","shell.execute_reply.started":"2023-08-07T12:00:55.343573Z","shell.execute_reply":"2023-08-07T12:00:55.348456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check_imgsz(5)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:00:55.351513Z","iopub.execute_input":"2023-08-07T12:00:55.351963Z","iopub.status.idle":"2023-08-07T12:00:55.373383Z","shell.execute_reply.started":"2023-08-07T12:00:55.351822Z","shell.execute_reply":"2023-08-07T12:00:55.371668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"train_path = glob('/kaggle/working/train/*.jpg')\ntrain_path[:5]","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:00:55.374594Z","iopub.execute_input":"2023-08-07T12:00:55.375234Z","iopub.status.idle":"2023-08-07T12:00:55.459265Z","shell.execute_reply.started":"2023-08-07T12:00:55.375201Z","shell.execute_reply":"2023-08-07T12:00:55.457390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Label Encoding\nlabel_mapping = {\n    'cat' : 0,\n    'dog' : 1\n}\n\n# Define New Dataframe\ntrain_df = pd.DataFrame(columns=['path', 'label'])\ntrain_df['path'] = train_path\n\n# Using apply function, we can split image path & label\ntrain_df['label'] = train_df['path'].apply(lambda x:x.split('/')[-1].split('.')[0])\ntrain_df['label'] = train_df['label'].apply(lambda x: label_mapping[x])\n\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:20:19.024347Z","iopub.execute_input":"2023-08-07T12:20:19.024818Z","iopub.status.idle":"2023-08-07T12:20:19.084435Z","shell.execute_reply.started":"2023-08-07T12:20:19.024785Z","shell.execute_reply":"2023-08-07T12:20:19.082795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Custom Dataset","metadata":{}},{"cell_type":"code","source":"import cv2\nimport torch\nfrom torch.utils.data import Dataset\n\nclass CustomDataset(Dataset):\n    def __init__(self, df, is_train=True, transforms=None):\n        self.df = df\n        self.is_train = is_train\n        self.transforms = transforms\n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        image_path = self.df['path'][idx]\n        image = cv2.imread(image_path)\n        \n        # Data Augmentation\n        if self.transforms:\n            image = self.transforms(image=image)['image']\n            \n        if self.is_train:\n            label = self.df['label'][idx]\n            return image, label\n        else:\n            return image","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:24:23.715646Z","iopub.execute_input":"2023-08-07T12:24:23.716047Z","iopub.status.idle":"2023-08-07T12:24:23.724745Z","shell.execute_reply.started":"2023-08-07T12:24:23.716015Z","shell.execute_reply":"2023-08-07T12:24:23.723394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_image = CustomDataset(train_df)[0][0]\nsample_label = CustomDataset(train_df)[0][1]\n\nlabel_decoder = {\n    0:'cat',\n    1:'dog'\n}\n\nplt.figure(figsize=(12, 12))\nplt.imshow(sample_image);\nplt.axis('off');\nplt.title(label_decoder[sample_label]);","metadata":{"execution":{"iopub.status.busy":"2023-08-07T12:44:18.986175Z","iopub.execute_input":"2023-08-07T12:44:18.986639Z","iopub.status.idle":"2023-08-07T12:44:19.725437Z","shell.execute_reply.started":"2023-08-07T12:44:18.986601Z","shell.execute_reply":"2023-08-07T12:44:19.724207Z"},"trusted":true},"execution_count":null,"outputs":[]}]}