feat: add new Jupyter notebook for image transformation and dataset handling

This commit is contained in:
fada
2025-06-13 14:35:10 +08:00
parent 13ae2f0898
commit deafb8c3fc
12 changed files with 757 additions and 66 deletions

View File

@ -1,63 +1,30 @@
{ {
"cells": [ "cells": [
{ {
"metadata": {},
"cell_type": "code", "cell_type": "code",
"id": "initial_id", "outputs": [],
"metadata": { "execution_count": null,
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-06-11T07:39:14.997108Z",
"start_time": "2025-06-11T07:39:14.986047Z"
}
},
"source": [ "source": [
"from PIL import Image\n", "from PIL import Image\n",
"\n", "\n",
"im = Image.open('./data/jk.jpg')\n", "im = Image.open('data/images/jk.jpg')\n",
"im.size" "im.size"
], ],
"outputs": [ "id": "306b84c8b2d7bc36"
{
"data": {
"text/plain": [
"(318, 116)"
]
}, },
"execution_count": 73, {
"metadata": {}, "metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 73
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-11T07:39:22.279332Z",
"start_time": "2025-06-11T07:39:22.267501Z"
}
},
"cell_type": "code", "cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"\n", "\n",
"im_pillow = np.asarray(im)\n", "im_pillow = np.asarray(im)\n",
"im_pillow.shape" "im_pillow.shape"
], ],
"id": "e10b9e7640af00c9", "id": "627463cc80a9eea4"
"outputs": [
{
"data": {
"text/plain": [
"(116, 318, 3)"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 74
}, },
{ {
"metadata": { "metadata": {
@ -99,7 +66,7 @@
"source": [ "source": [
"import cv2\n", "import cv2\n",
"\n", "\n",
"im_cv2 = cv2.imread('./data/jk.jpg')\n", "im_cv2 = cv2.imread('./data/images/jk.jpg')\n",
"print(type(im_cv2))\n", "print(type(im_cv2))\n",
"\n", "\n",
"print(im_cv2.shape)" "print(im_cv2.shape)"

191
06.ipynb
View File

@ -16,8 +16,8 @@
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2025-06-12T12:58:38.694696Z", "end_time": "2025-06-13T01:44:55.209362Z",
"start_time": "2025-06-12T12:58:38.690734Z" "start_time": "2025-06-13T01:44:52.973675Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@ -42,13 +42,13 @@
], ],
"id": "1244f4325aad0ac5", "id": "1244f4325aad0ac5",
"outputs": [], "outputs": [],
"execution_count": 64 "execution_count": 1
}, },
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2025-06-12T12:59:08.444392Z", "end_time": "2025-06-13T01:44:55.286472Z",
"start_time": "2025-06-12T12:59:08.435084Z" "start_time": "2025-06-13T01:44:55.230898Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@ -66,27 +66,27 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"tensor([[-1.1016, 0.8803, 1.3645],\n", "tensor([[ 1.5479, 0.1374, 1.6763],\n",
" [ 0.6674, 0.4500, 0.0367],\n", " [-0.5671, -0.0821, -1.8523],\n",
" [ 1.3528, 0.2438, 0.4721],\n", " [-0.4039, -0.5871, -0.3510],\n",
" [-1.3651, -1.1315, 0.0299],\n", " [-0.2339, 0.2773, -0.0820],\n",
" [ 1.4538, 1.3622, -0.5165],\n", " [-0.3131, 0.5911, 0.2030],\n",
" [ 0.7515, 0.6168, -0.9036],\n", " [-0.7087, -0.2614, -0.2661],\n",
" [ 1.3542, -0.3779, -1.2439],\n", " [ 0.3220, -0.0340, -1.2429],\n",
" [-0.4588, -0.2233, -0.3531],\n", " [-0.1282, -0.2188, -0.7576],\n",
" [-0.0515, 0.8951, 0.3544],\n", " [ 1.1233, -0.2452, 0.4664],\n",
" [ 0.0181, 0.4075, -2.0888]])\n", " [ 1.2570, 0.3728, 0.9745]])\n",
"tensor([0, 0, 1, 0, 0, 1, 1, 1, 1, 0])\n" "tensor([0, 1, 1, 1, 0, 0, 0, 0, 0, 0])\n"
] ]
} }
], ],
"execution_count": 66 "execution_count": 2
}, },
{ {
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2025-06-12T12:59:25.092189Z", "end_time": "2025-06-13T01:44:55.610922Z",
"start_time": "2025-06-12T12:59:25.085985Z" "start_time": "2025-06-13T01:44:55.606365Z"
} }
}, },
"cell_type": "code", "cell_type": "code",
@ -107,11 +107,160 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"dataset size 10\n", "dataset size 10\n",
"data: (tensor([-1.1016, 0.8803, 1.3645]), tensor(0))\n" "data: (tensor([1.5479, 0.1374, 1.6763]), tensor(0))\n"
] ]
} }
], ],
"execution_count": 67 "execution_count": 3
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-13T01:48:47.876220Z",
"start_time": "2025-06-13T01:48:47.866170Z"
}
},
"cell_type": "code",
"source": [
"from torch.utils.data import DataLoader\n",
"\n",
"tensor_loader = DataLoader(dataset=my_dataset, batch_size=2, shuffle=True, num_workers=0)\n",
"\n",
"# 以循环的方式获取数据\n",
"for data, target in tensor_loader:\n",
" print(\"data:\", data)\n",
" print(\"target:\", target)\n",
"\n",
"print(\"one batch tensor data:\", next(iter(tensor_loader)))"
],
"id": "d78ddf04e79df9ae",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"data: tensor([[ 0.3220, -0.0340, -1.2429],\n",
" [-0.2339, 0.2773, -0.0820]])\n",
"target: tensor([0, 1])\n",
"data: tensor([[-0.5671, -0.0821, -1.8523],\n",
" [-0.3131, 0.5911, 0.2030]])\n",
"target: tensor([1, 0])\n",
"data: tensor([[-0.1282, -0.2188, -0.7576],\n",
" [-0.4039, -0.5871, -0.3510]])\n",
"target: tensor([0, 1])\n",
"data: tensor([[-0.7087, -0.2614, -0.2661],\n",
" [ 1.2570, 0.3728, 0.9745]])\n",
"target: tensor([0, 0])\n",
"data: tensor([[ 1.5479, 0.1374, 1.6763],\n",
" [ 1.1233, -0.2452, 0.4664]])\n",
"target: tensor([0, 0])\n",
"one batch tensor data: [tensor([[ 0.3220, -0.0340, -1.2429],\n",
" [ 1.1233, -0.2452, 0.4664]]), tensor([0, 0])]\n"
]
}
],
"execution_count": 9
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-13T01:53:08.605768Z",
"start_time": "2025-06-13T01:53:07.616662Z"
}
},
"cell_type": "code",
"source": "!pip install torchvision pillow",
"id": "b6845476dfd81659",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: torchvision in /opt/conda/lib/python3.10/site-packages (0.17.1)\r\n",
"Requirement already satisfied: pillow in /opt/conda/lib/python3.10/site-packages (10.2.0)\r\n",
"Requirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (from torchvision) (1.26.3)\r\n",
"Requirement already satisfied: torch in /opt/conda/lib/python3.10/site-packages (from torchvision) (2.2.1)\r\n",
"Requirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from torch->torchvision) (3.13.1)\r\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /opt/conda/lib/python3.10/site-packages (from torch->torchvision) (4.9.0)\r\n",
"Requirement already satisfied: sympy in /opt/conda/lib/python3.10/site-packages (from torch->torchvision) (1.12)\r\n",
"Requirement already satisfied: networkx in /opt/conda/lib/python3.10/site-packages (from torch->torchvision) (3.1)\r\n",
"Requirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from torch->torchvision) (3.1.3)\r\n",
"Requirement already satisfied: fsspec in /opt/conda/lib/python3.10/site-packages (from torch->torchvision) (2024.2.0)\r\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.10/site-packages (from jinja2->torch->torchvision) (2.1.3)\r\n",
"Requirement already satisfied: mpmath>=0.19 in /opt/conda/lib/python3.10/site-packages (from sympy->torch->torchvision) (1.3.0)\r\n",
"\u001B[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.\u001B[0m\u001B[33m\r\n",
"\u001B[0m"
]
}
],
"execution_count": 11
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-13T02:12:21.816805Z",
"start_time": "2025-06-13T02:12:21.421202Z"
}
},
"cell_type": "code",
"source": [
"# 以MNIST数据集为例\n",
"import torchvision\n",
"\n",
"mnist_dataset = torchvision.datasets.MNIST(root=\"./data\", train=True, transform=None, target_transform=None,\n",
" download=True)"
],
"id": "f8112ef53ed2d0ce",
"outputs": [],
"execution_count": 13
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-13T02:17:29.130555Z",
"start_time": "2025-06-13T02:17:28.225901Z"
}
},
"cell_type": "code",
"source": [
"mnist_dataset_list = list(mnist_dataset)\n",
"\n",
"display(mnist_dataset_list[0][0])\n",
"print(\"Image label is:\", mnist_dataset_list[0][1])"
],
"id": "9b9c1d2993b37bcd",
"outputs": [
{
"data": {
"text/plain": [
"<PIL.Image.Image image mode=L size=28x28>"
],
"image/png": "iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAAAAABXZoBIAAAA90lEQVR4AWNgGMyAWUhIqK5jvdSy/9/rQe5kgTlWjs3KRiAYxHsyKfDzxYMgFiOIAALDvfwQBsO/pK8Mz97fhPLAlNDtvyBwbNv3j8jCUHbAnOy/f89yM2jPwiLJwMc4628UqgQTnPvp/0eGFAQXLg5lcO/764YuhuArf3y4IAfmfoQwlBX44e/fckkMYaiA7q6/f6dJ45IViP3zdzcuSQaGn39/OkBl4WEL4euFmLIwXDuETav6lKfAIPy1DYucRNFdUPCe9MOUE3e6CpI6FogZSEKrwbFyOIATQ5v5mkcgXV9auVGlwK4NDGRguL75b88HVDla8QBFF16ADQA8sQAAAABJRU5ErkJggg==",
"image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCAAcABwBAREA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEBAAA/APAACzBVBJJwAO9dnp/wm8damu6Dw5dRjGf9IKw/+hkVPffCnWNJa7XVNV0Kxa1hErrNe/M2cnYqgElsAHpjkc1wlAODkV694W8c654t8M6n4TuvEctrrFw0cun3c0/lq+3AMJcDK5AyOeTkd+fPvGFn4gsvEtzF4m89tUG1ZJJjuMgUBVYN/EMKOe9YVXtK0bUtdvVs9LsZ7y4YgbIULYycZPoPc8V6lpfwh0/w7p66z8RdXj0y2z8llC4aWQ+mRn8lz9RXPfE3x1pvi46TYaPZTQadpMJghluWDSyrhQM9SMBe5Oc5NcBV7Tda1XRZJJNK1O8sXkG12tZ2iLD0JUjNQ3l9eahN517dT3MvTfNIXb16n6mq9Ff/2Q=="
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image label is: 5\n"
]
},
{
"ename": "AttributeError",
"evalue": "'list' object has no attribute 'size'",
"output_type": "error",
"traceback": [
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[0;31mAttributeError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[0;32mIn[23], line 6\u001B[0m\n\u001B[1;32m 3\u001B[0m display(mnist_dataset_list[\u001B[38;5;241m0\u001B[39m][\u001B[38;5;241m0\u001B[39m])\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mImage label is:\u001B[39m\u001B[38;5;124m\"\u001B[39m, mnist_dataset_list[\u001B[38;5;241m0\u001B[39m][\u001B[38;5;241m1\u001B[39m])\n\u001B[0;32m----> 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28;43mlist\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mmnist_dataset_list\u001B[49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msize\u001B[49m)\n",
"\u001B[0;31mAttributeError\u001B[0m: 'list' object has no attribute 'size'"
]
}
],
"execution_count": 23
} }
], ],
"metadata": { "metadata": {

575
07.ipynb Normal file

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB