{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using cuda device\n" ] } ], "source": [ "import torch\n", "from torch import nn\n", "from torch.utils.data import Dataset, DataLoader, random_split\n", "from IPython.display import display, clear_output\n", "import os\n", "\n", "os.environ[\"CUDA_LAUNCH_BLOCKING\"] = \"1\"\n", "\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "from sklearn import svm, model_selection, metrics\n", "\n", "device = (\n", " \"cuda\" if torch.cuda.is_available() else \n", " \"mps\" if torch.backends.mps.is_available() else \"cpu\"\n", ")\n", "print(f\"Using {device} device\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " images label\n", "23290 [[0.40643760463074197, 0.3982520862496269, 0.4... 1\n", "47604 [[0.12182608906416852, 0.19741380979252504, 0.... 1\n", "54068 [[0.5783613612865524, 0.4507420836595933, 0.47... 1\n", "43583 [[0.31308472117490954, 0.30893375779528137, 0.... 1\n", "56001 [[0.2915222336036848, 0.27913560835588913, 0.2... 1\n", "(21, 21)\n", "Image size: 21x21\n" ] } ], "source": [ "class MarkerImageDataset(Dataset):\n", " def __init__(self, f: str) -> None:\n", " super().__init__()\n", " self.dataset = pd.read_pickle(f)\n", "\n", " self.img = torch.as_tensor(np.stack(self.dataset[\"images\"], axis=0), dtype=torch.float32).to(device)[:, None, :, :]\n", " self.label = torch.as_tensor(np.vstack(self.dataset[\"label\"]), dtype=torch.float32).to(device)\n", "\n", " def __len__(self) -> int:\n", " return self.dataset.shape[0]\n", " \n", " def __getitem__(self, idx: int) -> dict:\n", " return self.img[idx], self.label[idx]\n", "\n", "dataset = MarkerImageDataset(\"data/set4.pkl\")\n", "print(dataset.dataset.head())\n", "print(dataset.dataset.iloc[0].images.shape)\n", "\n", "IMAGE_WIDTH, IMAGE_HEIGHT = dataset.dataset.iloc[0].images.shape\n", "print(f\"Image size: {IMAGE_WIDTH}x{IMAGE_HEIGHT}\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train: 103805, Test: 25951, Total: 129756 (Batch: 32)\n", "torch.Size([129756, 1, 21, 21]) torch.Size([129756, 1])\n", "Labels: tensor([[1.],\n", " [1.],\n", " [1.],\n", " ...,\n", " [0.],\n", " [0.],\n", " [0.]], device='cuda:0')\n" ] }, { "data": { "text/html": [ "
| \n", " | images | \n", "label | \n", "
|---|---|---|
| 23290 | \n", "[[0.40643760463074197, 0.3982520862496269, 0.4... | \n", "1 | \n", "
| 47604 | \n", "[[0.12182608906416852, 0.19741380979252504, 0.... | \n", "1 | \n", "
| 54068 | \n", "[[0.5783613612865524, 0.4507420836595933, 0.47... | \n", "1 | \n", "
| 43583 | \n", "[[0.31308472117490954, 0.30893375779528137, 0.... | \n", "1 | \n", "
| 56001 | \n", "[[0.2915222336036848, 0.27913560835588913, 0.2... | \n", "1 | \n", "