From e4d618443808f0931bbef0b9e10a2c2a215281a5 Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Sun, 3 Sep 2023 01:11:30 +0200 Subject: Update notebook: test gpu, etc --- notebooks/00-test-gpu.ipynb | 276 +++++++++++++++++++++++++++++++++++ notebooks/03-look-at-iam-lines.ipynb | 139 ------------------ notebooks/Untitled1.ipynb | 235 ----------------------------- 3 files changed, 276 insertions(+), 374 deletions(-) create mode 100644 notebooks/00-test-gpu.ipynb diff --git a/notebooks/00-test-gpu.ipynb b/notebooks/00-test-gpu.ipynb new file mode 100644 index 0000000..7ea06ae --- /dev/null +++ b/notebooks/00-test-gpu.ipynb @@ -0,0 +1,276 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "8468e45a", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import torch" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d1bc956b", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.cuda.is_available()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "652cfb26", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.cuda.device_count()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "0fc5e328", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.cuda.current_device()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "4ed93c82", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.cuda.device(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "c03841ce", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'NVIDIA GeForce RTX 2070'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.cuda.get_device_name(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "2fba9680-0c73-4a62-b24d-ceea79874717", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'11.7'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.version.cuda" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "7e62de10", + "metadata": {}, + "outputs": [], + "source": [ + "t = torch.randn((2, 1, 3, 64))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "fc221b4e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.Size([2, 1, 3, 64])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "ab80d75e", + "metadata": {}, + "outputs": [], + "source": [ + "import torch.nn.functional as F" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "bfe1fc90", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.Size([2, 1, 7, 128])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "F.interpolate(t, scale_factor=[2.5, 2], mode=\"nearest\").shape" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "f69fa1fc", + "metadata": {}, + "outputs": [], + "source": [ + "f = torch.nn.Conv2d(1, 1, 3, 1, padding=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "cd6df204", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.Size([2, 1, 64, 64])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f(torch.randn(2, 1, 64, 64)).shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "41693566", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/03-look-at-iam-lines.ipynb b/notebooks/03-look-at-iam-lines.ipynb index 9e9b24c..049a8b6 100644 --- a/notebooks/03-look-at-iam-lines.ipynb +++ b/notebooks/03-look-at-iam-lines.ipynb @@ -132,39 +132,6 @@ "dataset = datamodule.data_train" ] }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(tensor([[[0., 0., 0., ..., 0., 0., 0.],\n", - " [0., 0., 0., ..., 0., 0., 0.],\n", - " [0., 0., 0., ..., 0., 0., 0.],\n", - " ...,\n", - " [0., 0., 0., ..., 0., 0., 0.],\n", - " [0., 0., 0., ..., 0., 0., 0.],\n", - " [0., 0., 0., ..., 0., 0., 0.]]]),\n", - " tensor([ 1, 32, 27, 14, 33, 16, 21, 22, 27, 20, 40, 14, 33, 40, 33, 21, 18, 40,\n", - " 29, 31, 28, 29, 40, 21, 28, 25, 17, 22, 27, 20, 40, 33, 21, 18, 40, 15,\n", - " 14, 31, 20, 18, 45, 32, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,\n", - " 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,\n", - " 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]))" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "datamodule.data_val[16]" - ] - }, { "cell_type": "code", "execution_count": 8, @@ -188,112 +155,6 @@ "convert_y_label_to_string(dataset[0][1])" ] }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['',\n", - " '',\n", - " '',\n", - " '

',\n", - " '0',\n", - " '1',\n", - " '2',\n", - " '3',\n", - " '4',\n", - " '5',\n", - " '6',\n", - " '7',\n", - " '8',\n", - " '9',\n", - " 'a',\n", - " 'b',\n", - " 'c',\n", - " 'd',\n", - " 'e',\n", - " 'f',\n", - " 'g',\n", - " 'h',\n", - " 'i',\n", - " 'j',\n", - " 'k',\n", - " 'l',\n", - " 'm',\n", - " 'n',\n", - " 'o',\n", - " 'p',\n", - " 'q',\n", - " 'r',\n", - " 's',\n", - " 't',\n", - " 'u',\n", - " 'v',\n", - " 'w',\n", - " 'x',\n", - " 'y',\n", - " 'z',\n", - " ' ',\n", - " '!',\n", - " '\"',\n", - " '#',\n", - " '&',\n", - " \"'\",\n", - " '(',\n", - " ')',\n", - " '*',\n", - " '+',\n", - " ',',\n", - " '-',\n", - " '.',\n", - " '/',\n", - " ':',\n", - " ';',\n", - " '?',\n", - " '\\n']" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "datamodule.tokenizer.mapping" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "tensor([ 1, 34, 27, 22, 28, 27, 40, 36, 28, 34, 25, 17, 40, 15, 18, 40, 29, 31,\n", - " 18, 29, 14, 31, 18, 17, 40, 33, 28, 40, 31, 18, 14, 16, 21, 2, 3, 3,\n", - " 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,\n", - " 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,\n", - " 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x =dataset[0][1]\n", - "x[" - ] - }, { "cell_type": "code", "execution_count": 10, diff --git a/notebooks/Untitled1.ipynb b/notebooks/Untitled1.ipynb index a2d6168..92b35c9 100644 --- a/notebooks/Untitled1.ipynb +++ b/notebooks/Untitled1.ipynb @@ -430,241 +430,6 @@ "plt.figure(figsize=(40, 20))\n", "plt.imshow(xxx, cmap='gray')" ] - }, - { - "cell_type": "code", - "execution_count": 79, - "id": "c7c30e67-0cd7-4c23-adcc-56c86450bd37", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "torch.device" - ] - }, - "execution_count": 79, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(t.device)" - ] - }, - { - "cell_type": "code", - "execution_count": 86, - "id": "a6f270cc-20a2-4aae-8006-cb956eeed44c", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "mv =-torch.finfo(t.dtype).max" - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "id": "390d8a9d-2002-456f-93f9-b4e01b550024", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "a = torch.tensor([1., 1., 2., 2.])" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "id": "55efcc9d-9f61-46fb-8417-0a3443332b93", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "b = torch.tensor([1., 1., 2., 2.]) != 2." - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "id": "a0629f46-06b7-42dd-9fd7-d7d9da95faf6", - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "tensor([-3.4028e+38, -3.4028e+38, 2.0000e+00, 2.0000e+00])" - ] - }, - "execution_count": 95, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.masked_fill_(b, mv)" - ] - }, - { - "cell_type": "code", - "execution_count": 96, - "id": "516339e8-445a-4459-8fec-f028e3201bce", - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "tensor([ True, True, False, False])" - ] - }, - "execution_count": 96, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "b\n" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "c0e733d8-c17d-46f5-b484-9c74e46d7308", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "from typing import Union\n", - "\n", - "import torch\n", - "\n", - "\n", - "def first_appearance(x: torch.Tensor, element: Union[int, float], dim: int = 1) -> torch.Tensor:\n", - " \"\"\"Return indices of first appearance of element in x, collapsing along dim.\n", - "\n", - " Based on https://discuss.pytorch.org/t/first-nonzero-index/24769/9\n", - "\n", - " Parameters\n", - " ----------\n", - " x\n", - " One or two-dimensional Tensor to search for element.\n", - " element\n", - " Item to search for inside x.\n", - " dim\n", - " Dimension of Tensor to collapse over.\n", - "\n", - " Returns\n", - " -------\n", - " torch.Tensor\n", - " Indices where element occurs in x. If element is not found,\n", - " return length of x along dim. One dimension smaller than x.\n", - "\n", - " Raises\n", - " ------\n", - " ValueError\n", - " if x is not a 1 or 2 dimensional Tensor\n", - "\n", - " Examples\n", - " --------\n", - " >>> first_appearance(torch.tensor([[1, 2, 3], [2, 3, 3], [1, 1, 1], [3, 1, 1]]), 3)\n", - " tensor([2, 1, 3, 0])\n", - " >>> first_appearance(torch.tensor([1, 2, 3]), 1, dim=0)\n", - " tensor(0)\n", - " \"\"\"\n", - " if x.dim() > 2 or x.dim() == 0:\n", - " raise ValueError(f\"only 1 or 2 dimensional Tensors allowed, got Tensor with dim {x.dim()}\")\n", - " matches = x == element\n", - " first_appearance_mask = (matches.cumsum(dim) == 1) & matches\n", - " does_match, match_index = first_appearance_mask.max(dim)\n", - " first_inds = torch.where(does_match, match_index, x.shape[dim])\n", - " return first_inds" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "26ff2314-b2df-408f-b83a-f5fc903145da", - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "tensor([2, 3])" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "first_appearance(torch.tensor([[1, 1, 3], [1, 1, 1]]), 3)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "e8c9dd16-4917-40bc-8504-084035882ced", - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "tensor([ True, False])" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "torch.any(torch.isin(torch.tensor([[1, 1, 3], [1, 1, 1]]), 3), 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "30eb1926-53d2-431a-b7c1-f95919887b84", - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "tensor([2, 0])" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "torch.tensor([[1, 1, 3], [1, 1, 1]]).argmax(dim=1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a02c61b3-c84f-4778-90d0-fe6aafa12ccc", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { -- cgit v1.2.3-70-g09d2