|
67 | 67 | "cell_type": "code",
|
68 | 68 | "execution_count": null,
|
69 | 69 | "metadata": {},
|
70 |
| - "outputs": [], |
| 70 | + "outputs": [ |
| 71 | + { |
| 72 | + "name": "stderr", |
| 73 | + "output_type": "stream", |
| 74 | + "text": [ |
| 75 | + "Seed set to 42\n" |
| 76 | + ] |
| 77 | + } |
| 78 | + ], |
71 | 79 | "source": [
|
72 | 80 | "#| export\n",
|
73 | 81 | "set_seed(42)\n",
|
| 82 | + "logging.basicConfig(level=logging.DEBUG)\n", |
74 | 83 | "logger = logging.getLogger(__name__)\n",
|
75 | 84 | "plt.set_loglevel('INFO')"
|
76 | 85 | ]
|
|
144 | 153 | "cell_type": "code",
|
145 | 154 | "execution_count": null,
|
146 | 155 | "metadata": {},
|
147 |
| - "outputs": [], |
| 156 | + "outputs": [ |
| 157 | + { |
| 158 | + "data": { |
| 159 | + "text/markdown": [ |
| 160 | + "---\n", |
| 161 | + "\n", |
| 162 | + "[source](https://github.com/slegroux/nimrod/blob/main/nimrod/image/datasets.py#L47){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 163 | + "\n", |
| 164 | + "### ImageDataset.show_idx\n", |
| 165 | + "\n", |
| 166 | + "> ImageDataset.show_idx (index:int)\n", |
| 167 | + "\n", |
| 168 | + "*display image from data point index of a image dataset*\n", |
| 169 | + "\n", |
| 170 | + "| | **Type** | **Details** |\n", |
| 171 | + "| -- | -------- | ----------- |\n", |
| 172 | + "| index | int | Index of the (image,label) sample to visualize |" |
| 173 | + ], |
| 174 | + "text/plain": [ |
| 175 | + "---\n", |
| 176 | + "\n", |
| 177 | + "[source](https://github.com/slegroux/nimrod/blob/main/nimrod/image/datasets.py#L47){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 178 | + "\n", |
| 179 | + "### ImageDataset.show_idx\n", |
| 180 | + "\n", |
| 181 | + "> ImageDataset.show_idx (index:int)\n", |
| 182 | + "\n", |
| 183 | + "*display image from data point index of a image dataset*\n", |
| 184 | + "\n", |
| 185 | + "| | **Type** | **Details** |\n", |
| 186 | + "| -- | -------- | ----------- |\n", |
| 187 | + "| index | int | Index of the (image,label) sample to visualize |" |
| 188 | + ] |
| 189 | + }, |
| 190 | + "execution_count": null, |
| 191 | + "metadata": {}, |
| 192 | + "output_type": "execute_result" |
| 193 | + } |
| 194 | + ], |
148 | 195 | "source": [
|
149 | 196 | "show_doc(ImageDataset.show_idx)"
|
150 | 197 | ]
|
|
188 | 235 | " # torchvision.transforms.Compose([torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(0.1307,), (0.3081,))])\n",
|
189 | 236 | "\n",
|
190 | 237 | " ):\n",
|
| 238 | + " logger.info(\"MNISTDataset: init\")\n", |
| 239 | + " abs_data_dir = os.path.abspath(data_dir)\n", |
| 240 | + " logger.info(f\"Data directory: {abs_data_dir}\")\n", |
191 | 241 | " os.makedirs(data_dir, exist_ok=True)\n",
|
192 | 242 | " super().__init__()\n",
|
193 |
| - " logger.info(\"MNISTDataset: init\")\n", |
194 |
| - "\n", |
195 |
| - " self.ds = MNIST(\n", |
196 |
| - " data_dir,\n", |
197 |
| - " train = train,\n", |
198 |
| - " transform=transform, \n", |
199 |
| - " download=True\n", |
200 |
| - " )\n", |
| 243 | + " \n", |
| 244 | + " try:\n", |
| 245 | + " self.ds = MNIST(\n", |
| 246 | + " data_dir,\n", |
| 247 | + " train = train,\n", |
| 248 | + " transform=transform, \n", |
| 249 | + " download=True\n", |
| 250 | + " )\n", |
| 251 | + " logger.info(f\"MNIST dataset loaded with total {len(self.ds)} samples\")\n", |
| 252 | + " except Exception as e:\n", |
| 253 | + " logger.error(f\"Error loading MNIST dataset: {e}\")\n", |
| 254 | + " raise\n", |
201 | 255 | "\n",
|
202 | 256 | " def __len__(self) -> int: # length of dataset\n",
|
203 | 257 | " return len(self.ds)\n",
|
|
228 | 282 | "cell_type": "code",
|
229 | 283 | "execution_count": null,
|
230 | 284 | "metadata": {},
|
231 |
| - "outputs": [], |
| 285 | + "outputs": [ |
| 286 | + { |
| 287 | + "data": { |
| 288 | + "text/markdown": [ |
| 289 | + "---\n", |
| 290 | + "\n", |
| 291 | + "[source](https://github.com/slegroux/nimrod/blob/main/nimrod/image/datasets.py#L131){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 292 | + "\n", |
| 293 | + "### MNISTDataset.train_dev_split\n", |
| 294 | + "\n", |
| 295 | + "> MNISTDataset.train_dev_split (ratio:float)\n", |
| 296 | + "\n", |
| 297 | + "| | **Type** | **Details** |\n", |
| 298 | + "| -- | -------- | ----------- |\n", |
| 299 | + "| ratio | float | percentage of train/dev split, |\n", |
| 300 | + "| **Returns** | **tuple** | **train and set mnnist datasets** |" |
| 301 | + ], |
| 302 | + "text/plain": [ |
| 303 | + "---\n", |
| 304 | + "\n", |
| 305 | + "[source](https://github.com/slegroux/nimrod/blob/main/nimrod/image/datasets.py#L131){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 306 | + "\n", |
| 307 | + "### MNISTDataset.train_dev_split\n", |
| 308 | + "\n", |
| 309 | + "> MNISTDataset.train_dev_split (ratio:float)\n", |
| 310 | + "\n", |
| 311 | + "| | **Type** | **Details** |\n", |
| 312 | + "| -- | -------- | ----------- |\n", |
| 313 | + "| ratio | float | percentage of train/dev split, |\n", |
| 314 | + "| **Returns** | **tuple** | **train and set mnnist datasets** |" |
| 315 | + ] |
| 316 | + }, |
| 317 | + "execution_count": null, |
| 318 | + "metadata": {}, |
| 319 | + "output_type": "execute_result" |
| 320 | + } |
| 321 | + ], |
232 | 322 | "source": [
|
233 | 323 | "show_doc(MNISTDataset.train_dev_split)"
|
234 | 324 | ]
|
|
245 | 335 | "cell_type": "code",
|
246 | 336 | "execution_count": null,
|
247 | 337 | "metadata": {},
|
248 |
| - "outputs": [], |
| 338 | + "outputs": [ |
| 339 | + { |
| 340 | + "data": { |
| 341 | + "text/html": [ |
| 342 | + "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">[11:01:29] </span><span style=\"color: #000080; text-decoration-color: #000080\">INFO </span> <span style=\"font-weight: bold\">[</span><span style=\"color: #00ff00; text-decoration-color: #00ff00; font-weight: bold\">11:01:29</span><span style=\"font-weight: bold\">]</span> <a href=\"file:///var/folders/b5/v9y3kpzs29g41d99xvrdp3yr0000gn/T/ipykernel_81728/3839565024.py\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">3839565024.py</span></a><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">:</span><a href=\"file:///var/folders/b5/v9y3kpzs29g41d99xvrdp3yr0000gn/T/ipykernel_81728/3839565024.py#18\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">18</span></a>\n", |
| 343 | + "</pre>\n" |
| 344 | + ], |
| 345 | + "text/plain": [ |
| 346 | + "\u001b[2;36m[11:01:29]\u001b[0m\u001b[2;36m \u001b[0m\u001b[34mINFO \u001b[0m \u001b[1m[\u001b[0m\u001b[1;92m11:01:29\u001b[0m\u001b[1m]\u001b[0m \u001b]8;id=234053;file:///var/folders/b5/v9y3kpzs29g41d99xvrdp3yr0000gn/T/ipykernel_81728/3839565024.py\u001b\\\u001b[2m3839565024.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=146316;file:///var/folders/b5/v9y3kpzs29g41d99xvrdp3yr0000gn/T/ipykernel_81728/3839565024.py#18\u001b\\\u001b[2m18\u001b[0m\u001b]8;;\u001b\\\n" |
| 347 | + ] |
| 348 | + }, |
| 349 | + "metadata": {}, |
| 350 | + "output_type": "display_data" |
| 351 | + }, |
| 352 | + { |
| 353 | + "ename": "UnboundLocalError", |
| 354 | + "evalue": "cannot access local variable 'abs_data_dir' where it is not associated with a value", |
| 355 | + "output_type": "error", |
| 356 | + "traceback": [ |
| 357 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 358 | + "\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", |
| 359 | + "Cell \u001b[0;32mIn[10], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# define test set (train=False)\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m test \u001b[38;5;241m=\u001b[39m \u001b[43mMNISTDataset\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m../data/image\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtransform\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtransforms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mToTensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# output ( (C,H,W), int)\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(test\u001b[38;5;241m.\u001b[39mds, test\u001b[38;5;241m.\u001b[39mds[\u001b[38;5;241m0\u001b[39m][\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39mdtype, \u001b[38;5;28mtype\u001b[39m(test\u001b[38;5;241m.\u001b[39mds[\u001b[38;5;241m0\u001b[39m][\u001b[38;5;241m1\u001b[39m]))\n", |
| 360 | + "Cell \u001b[0;32mIn[8], line 19\u001b[0m, in \u001b[0;36mMNISTDataset.__init__\u001b[0;34m(self, data_dir, train, transform)\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 8\u001b[0m data_dir:\u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m../data/image\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;66;03m# path where data is saved\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 16\u001b[0m \n\u001b[1;32m 17\u001b[0m ):\n\u001b[1;32m 18\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMNISTDataset: init\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 19\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mData directory: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mabs_data_dir\u001b[49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 20\u001b[0m os\u001b[38;5;241m.\u001b[39mmakedirs(data_dir, exist_ok\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__init__\u001b[39m()\n", |
| 361 | + "\u001b[0;31mUnboundLocalError\u001b[0m: cannot access local variable 'abs_data_dir' where it is not associated with a value" |
| 362 | + ] |
| 363 | + } |
| 364 | + ], |
249 | 365 | "source": [
|
250 | 366 | "# define test set (train=False)\n",
|
| 367 | + "\n", |
251 | 368 | "test = MNISTDataset('../data/image', train=False, transform=transforms.ToTensor())\n",
|
252 | 369 | "\n",
|
253 | 370 | "# output ( (C,H,W), int)\n",
|
|
0 commit comments