Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher192 committed Nov 15, 2023
1 parent f866e73 commit 9927788
Show file tree
Hide file tree
Showing 7 changed files with 1,006 additions and 21 deletions.
343 changes: 343 additions & 0 deletions 04-food-classification-cnn-pytorch.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "464ba769",
"metadata": {},
"source": [
"## Convolutional Neural Networks and Computer Vision with PyTorch"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "b831898b",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pathlib\n",
"import torch\n",
"from torch import nn\n",
"from torch.utils.data import DataLoader, Dataset\n",
"from torchvision import datasets, transforms"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "0cda9a43",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.1.0+cpu'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.__version__"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "f8298624",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'cpu'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"device"
]
},
{
"cell_type": "markdown",
"id": "32d64866",
"metadata": {},
"source": [
"### 1. Data preparation"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "96999cfb",
"metadata": {},
"outputs": [],
"source": [
"train_dir, test_dir = \"data/pizza_steak_sushi/train\", \"data/pizza_steak_sushi/test\""
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "d912a4a7",
"metadata": {},
"outputs": [],
"source": [
"data_transform = transforms.Compose([\n",
" transforms.Resize(size = (64, 64)),\n",
" transforms.RandomHorizontalFlip(p = 0.5),\n",
" transforms.ToTensor()\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "66ce4314",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train data:\n",
"Dataset ImageFolder\n",
" Number of datapoints: 225\n",
" Root location: data/pizza_steak_sushi/train\n",
" StandardTransform\n",
"Transform: Compose(\n",
" Resize(size=(64, 64), interpolation=bilinear, max_size=None, antialias=warn)\n",
" RandomHorizontalFlip(p=0.5)\n",
" ToTensor()\n",
" )\n",
"Test data:\n",
"Dataset ImageFolder\n",
" Number of datapoints: 75\n",
" Root location: data/pizza_steak_sushi/test\n",
" StandardTransform\n",
"Transform: Compose(\n",
" Resize(size=(64, 64), interpolation=bilinear, max_size=None, antialias=warn)\n",
" RandomHorizontalFlip(p=0.5)\n",
" ToTensor()\n",
" )\n"
]
}
],
"source": [
"train_data = datasets.ImageFolder(root = train_dir,\n",
" transform = data_transform,\n",
" target_transform = None)\n",
"\n",
"test_data = datasets.ImageFolder(root = test_dir, \n",
" transform = data_transform)\n",
"\n",
"print(f\"Train data:\\n{train_data}\\nTest data:\\n{test_data}\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "a1224c74",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['pizza', 'steak', 'sushi']"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class_name = train_data.classes\n",
"class_name"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "1a3df960",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'pizza': 0, 'steak': 1, 'sushi': 2}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class_dict = train_data.class_to_idx\n",
"class_dict"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "2d203c6c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(225, 75)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(train_data), len(test_data)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "71c80d09",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image tensor:\n",
"tensor([[[0.1137, 0.1020, 0.0980, ..., 0.1255, 0.1216, 0.1176],\n",
" [0.1059, 0.0980, 0.0980, ..., 0.1294, 0.1294, 0.1294],\n",
" [0.1020, 0.0980, 0.0941, ..., 0.1333, 0.1333, 0.1333],\n",
" ...,\n",
" [0.1098, 0.1098, 0.1255, ..., 0.1686, 0.1647, 0.1686],\n",
" [0.0902, 0.0941, 0.1098, ..., 0.1686, 0.1647, 0.1686],\n",
" [0.0863, 0.0863, 0.0980, ..., 0.1686, 0.1647, 0.1647]],\n",
"\n",
" [[0.0745, 0.0706, 0.0745, ..., 0.0588, 0.0588, 0.0588],\n",
" [0.0745, 0.0706, 0.0745, ..., 0.0627, 0.0627, 0.0627],\n",
" [0.0706, 0.0745, 0.0745, ..., 0.0706, 0.0706, 0.0706],\n",
" ...,\n",
" [0.1255, 0.1333, 0.1373, ..., 0.2510, 0.2392, 0.2392],\n",
" [0.1098, 0.1176, 0.1255, ..., 0.2510, 0.2392, 0.2314],\n",
" [0.1020, 0.1059, 0.1137, ..., 0.2431, 0.2353, 0.2275]],\n",
"\n",
" [[0.0941, 0.0902, 0.0902, ..., 0.0157, 0.0196, 0.0196],\n",
" [0.0902, 0.0863, 0.0902, ..., 0.0196, 0.0157, 0.0196],\n",
" [0.0902, 0.0902, 0.0902, ..., 0.0157, 0.0157, 0.0196],\n",
" ...,\n",
" [0.1294, 0.1333, 0.1490, ..., 0.1961, 0.1882, 0.1843],\n",
" [0.1098, 0.1137, 0.1255, ..., 0.1922, 0.1843, 0.1804],\n",
" [0.1059, 0.0980, 0.1059, ..., 0.1882, 0.1804, 0.1765]]])\n",
"Image shape: torch.Size([3, 64, 64])\n",
"Image datatype: torch.float32\n",
"Image label: 0\n",
"Label datatype: <class 'int'>\n"
]
}
],
"source": [
"image, label = train_data[0][0], train_data[0][1]\n",
"\n",
"print(f\"Image tensor:\\n{image}\")\n",
"print(f\"Image shape: {image.shape}\")\n",
"print(f\"Image datatype: {image.dtype}\")\n",
"print(f\"Image label: {label}\")\n",
"print(f\"Label datatype: {type(label)}\")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "fd927774",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(<torch.utils.data.dataloader.DataLoader at 0x20a340d39d0>,\n",
" <torch.utils.data.dataloader.DataLoader at 0x20a340d3a90>)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_dataloader = DataLoader(dataset = train_data, batch_size = 1,\n",
" num_workers = 1, shuffle = True)\n",
"\n",
"test_dataloader = DataLoader(dataset = test_data, batch_size = 1, \n",
" num_workers = 1, shuffle = False)\n",
"\n",
"train_dataloader, test_dataloader"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "b8fcacf6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image shape: torch.Size([1, 3, 64, 64]) -> [batch_size, color_channels, height, width]\n",
"Label shape: torch.Size([1])\n"
]
}
],
"source": [
"image, label = next(iter(train_dataloader))\n",
"\n",
"print(f\"Image shape: {image.shape} -> [batch_size, color_channels, height, width]\")\n",
"print(f\"Label shape: {label.shape}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65b95b07",
"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.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
File renamed without changes.
Loading

0 comments on commit 9927788

Please sign in to comment.