{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":66610,"databundleVersionId":7498878,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Please comment and upvote if you like this work!\n\n## Take a look at the other notebooks in my profile:\n## [CV notebooks](https://www.kaggle.com/killa92/code)","metadata":{}},{"cell_type":"markdown","source":"### Custom Datasets and DataLoaders","metadata":{}},{"cell_type":"code","source":"import os, torch, shutil, numpy as np\nfrom glob import glob; from PIL import Image\nfrom torch.utils.data import random_split, Dataset, DataLoader\nfrom torchvision import transforms as T\ntorch.manual_seed(2024)\n\nclass CustomDataset(Dataset):\n    \n    def __init__(self, root, data, transformations = None):\n        \n        self.transformations = transformations\n        self.im_paths = sorted(glob(f\"{root}/{data}/*/*\"))\n        \n        self.cls_names, self.cls_counts, count, data_count = {}, {}, 0, 0\n        for idx, im_path in enumerate(self.im_paths):\n            class_name = self.get_class(im_path)\n            if class_name not in self.cls_names: self.cls_names[class_name] = count; self.cls_counts[class_name] = 1; count += 1\n            else: self.cls_counts[class_name] += 1\n        \n    def get_class(self, path): return os.path.dirname(path).split(\"/\")[-1]\n    \n    def __len__(self): return len(self.im_paths)\n\n    def __getitem__(self, idx):\n        \n        im_path = self.im_paths[idx]\n        im = Image.open(im_path).convert(\"RGB\")\n        gt = self.cls_names[self.get_class(im_path)]\n        \n        if self.transformations is not None: im = self.transformations(im)\n        \n        return im, gt\n    \ndef get_dls(root, transformations, bs, split = [0.9, 0.05, 0.05], ns = 4):\n    \n    tr_ds = CustomDataset(root = root, data = \"train\", transformations = transformations)\n    vl_ds = CustomDataset(root = root, data = \"val\",transformations = transformations)\n    ts_ds = CustomDataset(root = root, data = \"test\",transformations = transformations)\n    \n    tr_dl, val_dl, ts_dl = DataLoader(tr_ds, batch_size = bs, shuffle = True, num_workers = ns), DataLoader(vl_ds, batch_size = bs, shuffle = False, num_workers = ns), DataLoader(ts_ds, batch_size = 1, shuffle = False, num_workers = ns)\n    \n    return tr_dl, val_dl, ts_dl, tr_ds.cls_names\n\nroot = \"/kaggle/input/garbage-guru-challenge\"\nmean, std, im_size = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], 224\ntfs = T.Compose([T.Resize((im_size, im_size)), T.ToTensor(), T.Normalize(mean = mean, std = std)])\ntr_dl, val_dl, ts_dl, classes = get_dls(root = root, transformations = tfs, bs = 32)\n\nprint(len(tr_dl)); print(len(val_dl)); print(len(ts_dl)); print(classes)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-13T02:09:52.026808Z","iopub.execute_input":"2024-03-13T02:09:52.027229Z","iopub.status.idle":"2024-03-13T02:10:01.263281Z","shell.execute_reply.started":"2024-03-13T02:09:52.027196Z","shell.execute_reply":"2024-03-13T02:10:01.262288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Visualization","metadata":{}},{"cell_type":"code","source":"import random\nfrom matplotlib import pyplot as plt\n\ndef tensor_2_im(t, t_type = \"rgb\"):\n    \n    gray_tfs = T.Compose([T.Normalize(mean = [ 0.], std = [1/0.5]), T.Normalize(mean = [-0.5], std = [1])])\n    rgb_tfs = T.Compose([T.Normalize(mean = [ 0., 0., 0. ], std = [ 1/0.229, 1/0.224, 1/0.225 ]), T.Normalize(mean = [ -0.485, -0.456, -0.406 ], std = [ 1., 1., 1. ])])\n    \n    invTrans = gray_tfs if t_type == \"gray\" else rgb_tfs \n    \n    return (invTrans(t) * 255).detach().squeeze().cpu().numpy().astype(np.uint8) if t_type == \"gray\" else (invTrans(t) * 255).detach().cpu().permute(1,2,0).numpy().astype(np.uint8)\n\ndef visualize(data, n_ims, rows, cmap = None, cls_names = None):\n    \n    assert cmap in [\"rgb\", \"gray\"], \"Rasmni oq-qora yoki rangli ekanini aniqlashtirib bering!\"\n    if cmap == \"rgb\": cmap = \"viridis\"\n    \n    plt.figure(figsize = (20, 10))\n    indekslar = [random.randint(0, len(data) - 1) for _ in range(n_ims)]\n    for idx, indeks in enumerate(indekslar):\n        \n        im, gt = data[indeks]\n        # Start plot\n        plt.subplot(rows, n_ims // rows, idx + 1)\n        if cmap: plt.imshow(tensor_2_im(im, cmap), cmap=cmap)\n        else: plt.imshow(tensor_2_im(im))\n        plt.axis('off')\n        if cls_names is not None: plt.title(f\"GT -> {cls_names[int(gt)]}\")\n        else: plt.title(f\"GT -> {gt}\")\n            \nvisualize(tr_dl.dataset, 20, 4, \"rgb\", cls_names = list(classes.keys()))","metadata":{"execution":{"iopub.status.busy":"2024-03-13T02:10:01.265220Z","iopub.execute_input":"2024-03-13T02:10:01.265837Z","iopub.status.idle":"2024-03-13T02:10:04.232678Z","shell.execute_reply.started":"2024-03-13T02:10:01.265801Z","shell.execute_reply":"2024-03-13T02:10:04.231528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(val_dl.dataset, 20, 4, \"rgb\", cls_names = list(classes.keys()))","metadata":{"execution":{"iopub.status.busy":"2024-03-13T02:10:04.234010Z","iopub.execute_input":"2024-03-13T02:10:04.234637Z","iopub.status.idle":"2024-03-13T02:10:06.942184Z","shell.execute_reply.started":"2024-03-13T02:10:04.234604Z","shell.execute_reply":"2024-03-13T02:10:06.941058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(ts_dl.dataset, 20, 4, \"rgb\", cls_names = list(classes.keys()))","metadata":{"execution":{"iopub.status.busy":"2024-03-13T02:10:06.943689Z","iopub.execute_input":"2024-03-13T02:10:06.944668Z","iopub.status.idle":"2024-03-13T02:10:09.664096Z","shell.execute_reply.started":"2024-03-13T02:10:06.944624Z","shell.execute_reply":"2024-03-13T02:10:09.660074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Analysis","metadata":{}},{"cell_type":"code","source":"def data_analysis(root, data, transformations, text_height = 2):\n    \n    ds = CustomDataset(root = root, data = data, transformations = transformations)\n    cls_counts, width, text_width = ds.cls_counts,  0.7, 0.05\n    cls_names = list(cls_counts.keys()); counts = list(cls_counts.values())\n    \n    fig, ax = plt.subplots(figsize = (20, 10))\n    indices = np.arange(len(counts))\n\n    ax.bar(indices, counts, width, color = \"firebrick\")\n    ax.set_xlabel(\"Class Names\", color = \"red\")\n    ax.set_xticklabels(cls_names, rotation = 60)\n    ax.set(xticks = indices, xticklabels = cls_names)\n    ax.set_ylabel(\"Data Counts\", color = \"red\")\n    ax.set_title(f\"Dataset Class Imbalance Analysis\")\n\n    for i, v in enumerate(counts): ax.text(i - text_width, v + text_height, str(v), color = \"royalblue\")\n    \ndata_analysis(root = root, data = \"train\", transformations = tfs)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T02:10:09.666894Z","iopub.execute_input":"2024-03-13T02:10:09.668001Z","iopub.status.idle":"2024-03-13T02:10:10.235842Z","shell.execute_reply.started":"2024-03-13T02:10:09.667960Z","shell.execute_reply":"2024-03-13T02:10:10.234510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_analysis(root = root, data = \"val\", transformations = tfs, text_height = 0.02)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T02:10:10.237599Z","iopub.execute_input":"2024-03-13T02:10:10.238398Z","iopub.status.idle":"2024-03-13T02:10:10.736164Z","shell.execute_reply.started":"2024-03-13T02:10:10.238338Z","shell.execute_reply":"2024-03-13T02:10:10.735175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_analysis(root = root, data = \"test\", transformations = tfs, text_height = 0.02)","metadata":{"execution":{"iopub.status.busy":"2024-03-13T02:10:10.737762Z","iopub.execute_input":"2024-03-13T02:10:10.738166Z","iopub.status.idle":"2024-03-13T02:10:11.241107Z","shell.execute_reply.started":"2024-03-13T02:10:10.738127Z","shell.execute_reply":"2024-03-13T02:10:11.239831Z"},"trusted":true},"execution_count":null,"outputs":[]}]}