updated notebook

This commit is contained in:
2025-12-19 14:34:58 +01:00
parent 862a887cf9
commit a7c3284782
5 changed files with 706 additions and 138 deletions
+1
View File
@@ -1,3 +1,4 @@
.ipynb_checkpoints/
data
trained_net.pth
pyCnnImageClassifier.egg-info
+2
View File
@@ -1,4 +1,6 @@
# Tutorial
## Pytorch 60min Blitz
https://docs.pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html
## Python CNN image classification from with jupyter, torch, torchvision and pillow
from https://youtu.be/CtzfbUwrYGI?si=09iwSO4S5DtaAl4G
+35 -138
View File
@@ -2,9 +2,15 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "2e9496e9-ec07-445f-b29d-fc44aabb377d",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"import numpy as np\n",
@@ -19,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "9b17757b-76c5-4562-a2be-e3a50201c9ee",
"metadata": {},
"outputs": [],
@@ -32,18 +38,10 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "5b898116-e9a8-429d-8491-67db5a65d358",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100.0%\n"
]
}
],
"outputs": [],
"source": [
"train_data = torchvision.datasets.CIFAR10(root='./data', train=True, transform=transform, download=True)\n",
"test_data = torchvision.datasets.CIFAR10(root='./data', train=False, transform=transform, download=True)\n",
@@ -54,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "49712fae-dfe6-4b6f-92ea-2e97a965c15c",
"metadata": {},
"outputs": [],
@@ -64,28 +62,17 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "d783e1a5-3410-4992-88a6-230dd2ec85dd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([3, 32, 32])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"image.size()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "8ef21399-f2f2-46f4-8268-f60f36bbb533",
"metadata": {},
"outputs": [],
@@ -95,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "192ff676-c4b8-4611-9338-ec9b35a502d5",
"metadata": {},
"outputs": [],
@@ -122,7 +109,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "f48dd819-9196-4aa5-92c4-ddcc644c6aa9",
"metadata": {},
"outputs": [],
@@ -134,79 +121,12 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "3a91ce0c-e71d-4bfb-b945-47edfc8ca8d6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training epoch 0...\n",
"Loss: 2.2366\n",
"Training epoch 1...\n",
"Loss: 1.7631\n",
"Training epoch 2...\n",
"Loss: 1.5183\n",
"Training epoch 3...\n",
"Loss: 1.3860\n",
"Training epoch 4...\n",
"Loss: 1.2861\n",
"Training epoch 5...\n",
"Loss: 1.2027\n",
"Training epoch 6...\n",
"Loss: 1.1302\n",
"Training epoch 7...\n",
"Loss: 1.0746\n",
"Training epoch 8...\n",
"Loss: 1.0332\n",
"Training epoch 9...\n",
"Loss: 0.9863\n",
"Training epoch 10...\n",
"Loss: 0.9492\n",
"Training epoch 11...\n",
"Loss: 0.9165\n",
"Training epoch 12...\n",
"Loss: 0.8859\n",
"Training epoch 13...\n",
"Loss: 0.8514\n",
"Training epoch 14...\n",
"Loss: 0.8210\n",
"Training epoch 15...\n",
"Loss: 0.7903\n",
"Training epoch 16...\n",
"Loss: 0.7662\n",
"Training epoch 17...\n",
"Loss: 0.7347\n",
"Training epoch 18...\n",
"Loss: 0.7096\n",
"Training epoch 19...\n",
"Loss: 0.6868\n",
"Training epoch 20...\n",
"Loss: 0.6642\n",
"Training epoch 21...\n",
"Loss: 0.6387\n",
"Training epoch 22...\n",
"Loss: 0.6193\n",
"Training epoch 23...\n",
"Loss: 0.5926\n",
"Training epoch 24...\n",
"Loss: 0.5701\n",
"Training epoch 25...\n",
"Loss: 0.5532\n",
"Training epoch 26...\n",
"Loss: 0.5306\n",
"Training epoch 27...\n",
"Loss: 0.5090\n",
"Training epoch 28...\n",
"Loss: 0.4891\n",
"Training epoch 29...\n",
"Loss: 0.4685\n"
]
}
],
"outputs": [],
"source": [
"for epoch in range(30):\n",
"for epoch in range(60):\n",
" print(f'Training epoch {epoch}...')\n",
"\n",
" running_loss = 0.0\n",
@@ -226,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"id": "aa5b2497-90a7-44a9-9985-427b920945eb",
"metadata": {},
"outputs": [],
@@ -236,21 +156,10 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "ff62419f-ead5-4244-9a5e-a21d568bdca8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<All keys matched successfully>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"net = NeuralNet()\n",
"net.load_state_dict(torch.load('trained_net.pth'))"
@@ -258,18 +167,10 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"id": "eb1d4385-d4da-454f-9d8c-d844aa7b24df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy: 68.86%\n"
]
}
],
"outputs": [],
"source": [
"correct = 0\n",
"total = 0\n",
@@ -288,20 +189,16 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"id": "1a4f098f-b1ea-46b7-a7d6-769f80292f5f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prediction: cat\n",
"Prediction: deer\n",
"Prediction: plane\n"
]
}
],
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"new_transform = transforms.Compose([\n",
" transforms.Resize((32, 32)), \n",
@@ -329,7 +226,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "eed01777-2e7a-476c-8861-388d68f2f9c6",
"id": "f2f456ec-90af-4d17-8665-b4c3f18e5056",
"metadata": {},
"outputs": [],
"source": []
+8
View File
@@ -17,4 +17,12 @@ dependencies = [
"torchvision",
"jupyterlab"
]
[[tool.uv.index]]
# Optional name for the index.
name = "pytorch"
# Required URL for the index.
url = "https://download.pytorch.org/whl/cu126"
readme = "README.md"
File diff suppressed because one or more lines are too long