Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 315 additions & 40 deletions 1.0-tl-scientific-python.ipynb

Large diffs are not rendered by default.

97 changes: 75 additions & 22 deletions 2.0-tl-pytorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\Ginevra\\anaconda3\\envs\\3dcv-test\\lib\\site-packages\\scipy\\__init__.py:155: UserWarning: A NumPy version >=1.18.5 and <1.25.0 is required for this version of SciPy (detected version 1.25.2\n",
" warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n"
]
}
],
"source": [
"import os\n",
"\n",
Expand Down Expand Up @@ -63,68 +72,112 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]\n",
"tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.,\n",
" 14., 15., 16., 17., 18., 19.])\n",
"tensor([ 0.0000, 0.3226, 0.7881, 0.9376, 0.5649, 2.0675, 5.9284, 5.9023,\n",
" 4.8933, 4.6293, 4.3573, 2.0781, 10.1377, 2.3673, 10.2661, 1.1669,\n",
" 3.2232, 10.6823, 3.8795, 5.6676])\n",
"tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n",
" 1., 1.])\n",
"tensor(1.)\n"
]
},
{
"data": {
"text/plain": [
"tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a numpy array that looks like this: [0, 1, 2, ..., 19]\n",
"arr = \n",
"arr = np.arange(0,20)\n",
"print(arr)\n",
"\n",
"# Convert the numpy array to a torch tensor\n",
"tensor = \n",
"tensor = torch.from_numpy(arr.astype(np.float32))\n",
"print(tensor)\n",
"\n",
"# Create a tensor that contains random numbers.\n",
"# It should have the same size like the numpy array.\n",
"# Multiply it with the previous tensor.\n",
"rand_tensor = \n",
"tensor = \n",
"rand_tensor = torch.rand_like(tensor, dtype=torch.float)\n",
"tensor = tensor * rand_tensor\n",
"print(tensor)\n",
"\n",
"# Create a tensor that contains only 1s.\n",
"# It should have the same size like the numpy array.\n",
"# Substract it from the previous tensor.\n",
"tensor = \n",
"tensor = torch.ones_like(tensor)\n",
"print(tensor)\n",
"\n",
"# Get the 5th element using a index.\n",
"element = \n",
"element = tensor[4]\n",
"print(element)\n",
"\n",
"# Create a tensor that contains only 0s.\n",
"# It should have the same size like the numpy array.\n",
"# Multiply it with the previous tensor without any assignment (in place).\n"
"# Multiply it with the previous tensor without any assignment (in place).\n",
"zero_tensor=torch.zeros_like(tensor)\n",
"tensor.mul_(zero_tensor)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([512, 512, 3])\n",
"786432\n",
"torch.Size([1, 786432])\n",
"torch.Size([786432])\n",
"torch.Size([512, 512, 3])\n",
"tensor(91404744.)\n",
"tensor(116.2271)\n",
"tensor(255.)\n"
]
}
],
"source": [
"# Load the image from the last exercise as RGB image.\n",
"image = \n",
"image = skimage.io.imread('./data/pepo.jpg')\n",
"\n",
"# Convert the image to a tensor\n",
"image = \n",
"\n",
"image = torch.from_numpy(image.astype(np.float32))\n",
"height, width, channels = image.shape\n",
"# Print its shape\n",
"print(image.shape)\n",
"\n",
"# Flatten the image\n",
"image = \n",
"image = torch.flatten(image)\n",
"print(len(image))\n",
"\n",
"# Add another dimension resulting in a 1x78642 tensor\n",
"\n",
"image = image.unsqueeze(0)\n",
"print(image.shape)\n",
"\n",
"# Revert the last action\n",
"\n",
"image = image.squeeze(0)\n",
"print(image.shape)\n",
"\n",
"# Reshape the tensor, so that it has the original 2D dimensions\n",
"image = \n",
"image = image.view(height, width, channels)\n",
"print(image.shape)\n",
"\n",
"# Calculate the sum, mean and max of the tensor\n",
Expand Down Expand Up @@ -337,7 +390,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "3dcv-test",
"language": "python",
"name": "python3"
},
Expand All @@ -351,7 +404,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.10.19"
}
},
"nbformat": 4,
Expand Down
Binary file modified data/pepo_gray.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.