diff --git a/02/data/FashionMNIST/raw/t10k-images-idx3-ubyte b/02/data/FashionMNIST/raw/t10k-images-idx3-ubyte new file mode 100644 index 0000000..37bac79 Binary files /dev/null and b/02/data/FashionMNIST/raw/t10k-images-idx3-ubyte differ diff --git a/02/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz b/02/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz new file mode 100644 index 0000000..667844f Binary files /dev/null and b/02/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz differ diff --git a/02/data/FashionMNIST/raw/t10k-labels-idx1-ubyte b/02/data/FashionMNIST/raw/t10k-labels-idx1-ubyte new file mode 100644 index 0000000..2195a4d Binary files /dev/null and b/02/data/FashionMNIST/raw/t10k-labels-idx1-ubyte differ diff --git a/02/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz b/02/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz new file mode 100644 index 0000000..abdddb8 Binary files /dev/null and b/02/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz differ diff --git a/02/data/FashionMNIST/raw/train-images-idx3-ubyte b/02/data/FashionMNIST/raw/train-images-idx3-ubyte new file mode 100644 index 0000000..ff2f5a9 Binary files /dev/null and b/02/data/FashionMNIST/raw/train-images-idx3-ubyte differ diff --git a/02/data/FashionMNIST/raw/train-images-idx3-ubyte.gz b/02/data/FashionMNIST/raw/train-images-idx3-ubyte.gz new file mode 100644 index 0000000..e6ee0e3 Binary files /dev/null and b/02/data/FashionMNIST/raw/train-images-idx3-ubyte.gz differ diff --git a/02/data/FashionMNIST/raw/train-labels-idx1-ubyte b/02/data/FashionMNIST/raw/train-labels-idx1-ubyte new file mode 100644 index 0000000..30424ca Binary files /dev/null and b/02/data/FashionMNIST/raw/train-labels-idx1-ubyte differ diff --git a/02/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz b/02/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz new file mode 100644 index 0000000..9c4aae2 Binary files /dev/null and b/02/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz differ diff --git a/02/ex_02.ipynb b/02/ex_02.ipynb new file mode 100644 index 0000000..d9e7cb2 --- /dev/null +++ b/02/ex_02.ipynb @@ -0,0 +1,808 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "bbb863a1", + "metadata": {}, + "source": [ + "# Aufgabe 2 - Designing Efficient CNNs for FashionMNIST\n", + "\n", + "In this exercise we want to design and implement our own efficient CNN in pytorch with the\n", + "techniques introduced in the lecture. Because we cannot calculate the latency on the hardware\n", + "yet, we use the amount of FLOPs as a proxy metric for now. Keep in mind that FLOPs only\n", + "correlate losely with latency.\n", + "\n", + "## 2.1 - Baseline Model\n", + "\n", + "Before designing an efficient architecture we want to implement a baseline model to compare\n", + "to. For that design a CNN that achieves at least a Test Accuracy ≥ 91% on FashionMNIST.\n", + "What is the architecture? How did you train it?" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "cc5ec561", + "metadata": {}, + "outputs": [], + "source": [ + "# Imports\n", + "import torch\n", + "import torch.nn as nn\n", + "from torchvision import datasets\n", + "from torchvision import transforms\n", + "from torch.utils.data import DataLoader\n", + "from torch.utils.flop_counter import FlopCounterMode" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fe685559", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100.0%\n", + "100.0%\n", + "100.0%\n", + "100.0%\n" + ] + } + ], + "source": [ + "# Daten Laden\n", + "transform = transforms.ToTensor()\n", + "\n", + "train_dataset = datasets.FashionMNIST(\n", + " root=\"./data\",\n", + " train=True,\n", + " download=True,\n", + " transform=transform\n", + ")\n", + "\n", + "test_dataset = datasets.FashionMNIST(\n", + " root=\"./data\",\n", + " train=False,\n", + " download=True,\n", + " transform=transform\n", + ")\n", + "\n", + "train_loader = DataLoader(\n", + " train_dataset,\n", + " batch_size=128,\n", + " shuffle=True\n", + ")\n", + "\n", + "test_loader = DataLoader(\n", + " test_dataset,\n", + " batch_size=128\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "b9eba0b3", + "metadata": {}, + "outputs": [], + "source": [ + "# Modell definieren\n", + "class BaselineCNN(nn.Module):\n", + " def __init__(self):\n", + " super().__init__()\n", + "\n", + " self.features = nn.Sequential(\n", + " nn.Conv2d(1, 32, kernel_size=3, padding=1),\n", + " nn.BatchNorm2d(32),\n", + " nn.ReLU(),\n", + "\n", + " nn.MaxPool2d(2),\n", + "\n", + " nn.Conv2d(32, 64, kernel_size=3, padding=1),\n", + " nn.BatchNorm2d(64),\n", + " nn.ReLU(),\n", + "\n", + " nn.MaxPool2d(2)\n", + " )\n", + "\n", + " self.classifier = nn.Sequential(\n", + " nn.Flatten(),\n", + " nn.Linear(64 * 7 * 7, 128),\n", + " nn.ReLU(),\n", + " nn.Dropout(0.3),\n", + " nn.Linear(128, 10)\n", + " )\n", + "\n", + " def forward(self, x):\n", + " x = self.features(x)\n", + " return self.classifier(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "727e78f0", + "metadata": {}, + "outputs": [], + "source": [ + "device = torch.device(\n", + " \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "e601cfc3", + "metadata": {}, + "outputs": [], + "source": [ + "# Evaluation\n", + "def evaluate(model, loader):\n", + "\n", + " model.eval()\n", + "\n", + " correct = 0\n", + " total = 0\n", + "\n", + " with torch.no_grad():\n", + "\n", + " for images, labels in loader:\n", + "\n", + " images = images.to(device)\n", + " labels = labels.to(device)\n", + "\n", + " outputs = model(images)\n", + "\n", + " pred = outputs.argmax(dim=1)\n", + "\n", + " total += labels.size(0)\n", + " correct += (pred == labels).sum().item()\n", + "\n", + " return 100 * correct / total" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "4bb7adb2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch [1/10] Loss: 0.4313 Test Acc: 88.02%\n", + "Epoch [2/10] Loss: 0.2518 Test Acc: 91.00%\n", + "Epoch [3/10] Loss: 0.2060 Test Acc: 91.56%\n", + "Epoch [4/10] Loss: 0.1771 Test Acc: 89.70%\n", + "Epoch [5/10] Loss: 0.1520 Test Acc: 91.56%\n", + "Epoch [6/10] Loss: 0.1297 Test Acc: 92.17%\n", + "Epoch [7/10] Loss: 0.1088 Test Acc: 91.81%\n", + "Epoch [8/10] Loss: 0.0896 Test Acc: 91.88%\n", + "Epoch [9/10] Loss: 0.0792 Test Acc: 92.22%\n", + "Epoch [10/10] Loss: 0.0628 Test Acc: 91.64%\n" + ] + } + ], + "source": [ + "# Training\n", + "model = BaselineCNN().to(device)\n", + "criterion = nn.CrossEntropyLoss()\n", + "\n", + "optimizer = torch.optim.Adam(\n", + " model.parameters(),\n", + " lr=1e-3\n", + ")\n", + "\n", + "epochs = 10\n", + "batch_size = 128\n", + "\n", + "for epoch in range(epochs):\n", + "\n", + " running_loss = 0.0\n", + "\n", + " for images, labels in train_loader:\n", + "\n", + " images = images.to(device)\n", + " labels = labels.to(device)\n", + "\n", + " # Gradienten zurücksetzen\n", + " optimizer.zero_grad()\n", + "\n", + " # Forward Pass\n", + " outputs = model(images)\n", + "\n", + " # Loss berechnen\n", + " loss = criterion(outputs, labels)\n", + "\n", + " # Backpropagation\n", + " loss.backward()\n", + "\n", + " # Gewichte aktualisieren\n", + " optimizer.step()\n", + "\n", + " running_loss += loss.item()\n", + "\n", + " avg_loss = running_loss / len(train_loader)\n", + "\n", + " test_acc = evaluate(model, test_loader)\n", + "\n", + " print(\n", + " f\"Epoch [{epoch+1}/{epochs}] \"\n", + " f\"Loss: {avg_loss:.4f} \"\n", + " f\"Test Acc: {test_acc:.2f}%\"\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "62327de9", + "metadata": {}, + "source": [ + "## 2.2 - Resource Analysis\n", + "\n", + "Analyse your baseline model according to its resources used. What are the:\n", + "\n", + "- Total parameters\n", + "- FLOPs per forward pass for one sample\n", + "\n", + "For the number of parameters you may use the function implemented in the previous exercise.\n", + "For the FLOPs (Floating Point Operations) a function has to be implemented. For this, you\n", + "may use the torch.utils.flop_counter package or the formulas given in the lecture." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "402594a0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Parameters: 421,834\n" + ] + } + ], + "source": [ + "# Parameter zählen\n", + "def count_parameters(model):\n", + " return sum(p.numel() for p in model.parameters() if p.requires_grad)\n", + "\n", + "print(f\"Parameters: {count_parameters(model):,}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "97089d7d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Module FLOP % Total\n", + "---------------------- --------- ---------\n", + "BaselineCNN 8482.304K 100.00%\n", + "- aten.convolution 7676.928K 90.51%\n", + "- aten.addmm 805.376K 9.49%\n", + "BaselineCNN.classifier 805.376K 9.49%\n", + "- aten.addmm 805.376K 9.49%\n", + "BaselineCNN.features 7676.928K 90.51%\n", + "- aten.convolution 7676.928K 90.51%\n", + "8482304\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\denni\\AppData\\Local\\Temp\\ipykernel_15972\\1016987829.py:4: UserWarning: mods argument is not needed anymore, you can stop passing it\n", + " with FlopCounterMode(model) as flop_counter:\n" + ] + } + ], + "source": [ + "# FLOPs berechnen\n", + "dummy_input = torch.randn(1, 1, 28, 28)\n", + "\n", + "with FlopCounterMode(model) as flop_counter:\n", + " model(dummy_input)\n", + "\n", + "print(flop_counter.get_total_flops())" + ] + }, + { + "cell_type": "markdown", + "id": "7090e67b", + "metadata": {}, + "source": [ + "## 2.3 - Model Optimization\n", + "\n", + "Design a more efficient CNN with the techniques introduced in the lecture. Your model should\n", + "fulfill at least the following constraints:\n", + "\n", + "- Test accuracy ≥ 90%\n", + "- FLOPs < 500,000\n", + "- Parameters < 80,000\n", + "\n", + "Describe the final model architecture, FLOP and parameter count and accuracy. Which techniques had which impact? What worked better or worse." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8558e428", + "metadata": {}, + "outputs": [], + "source": [ + "# Depthwise Separable Convolution\n", + "# Global Average Pooling\n", + "\n", + "class EfficientCNN_v1(nn.Module):\n", + " def __init__(self):\n", + " super().__init__()\n", + "\n", + " self.features = nn.Sequential(\n", + "\n", + " nn.Conv2d(1, 8, kernel_size=3, padding=1),\n", + " nn.BatchNorm2d(8),\n", + " nn.ReLU(),\n", + "\n", + " # Depthwise Separable Block\n", + " nn.Conv2d(\n", + " 8, 8,\n", + " kernel_size=3,\n", + " padding=1,\n", + " groups=8\n", + " ),\n", + " nn.Conv2d(\n", + " 8, 16,\n", + " kernel_size=1\n", + " ),\n", + " nn.ReLU(),\n", + "\n", + " nn.MaxPool2d(2),\n", + "\n", + " # Depthwise Separable Block\n", + " nn.Conv2d(\n", + " 16, 16,\n", + " kernel_size=3,\n", + " padding=1,\n", + " groups=16\n", + " ),\n", + " nn.Conv2d(\n", + " 16, 32,\n", + " kernel_size=1\n", + " ),\n", + " nn.ReLU(),\n", + "\n", + " nn.AdaptiveAvgPool2d(1)\n", + " )\n", + "\n", + " self.classifier = nn.Linear(32, 10)\n", + "\n", + " def forward(self, x):\n", + " x = self.features(x)\n", + "\n", + " x = x.view(x.size(0), -1)\n", + "\n", + " return self.classifier(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00f44aa5", + "metadata": {}, + "outputs": [], + "source": [ + "efficient_model_v1 = EfficientCNN_v1().to(device)\n", + "\n", + "criterion = nn.CrossEntropyLoss()\n", + "\n", + "optimizer = torch.optim.Adam(\n", + " efficient_model_v1.parameters(),\n", + " lr=3e-4\n", + ")\n", + "\n", + "epochs = 40\n", + "\n", + "for epoch in range(epochs):\n", + "\n", + " efficient_model_v1.train()\n", + "\n", + " running_loss = 0.0\n", + "\n", + " for images, labels in train_loader:\n", + "\n", + " images = images.to(device)\n", + " labels = labels.to(device)\n", + "\n", + " optimizer.zero_grad()\n", + "\n", + " outputs = efficient_model_v1(images)\n", + "\n", + " loss = criterion(outputs, labels)\n", + "\n", + " loss.backward()\n", + "\n", + " optimizer.step()\n", + "\n", + " running_loss += loss.item()\n", + "\n", + " avg_loss = running_loss / len(train_loader)\n", + "\n", + " test_acc = evaluate(\n", + " efficient_model_v1,\n", + " test_loader\n", + " )\n", + "\n", + " print(\n", + " f\"Epoch [{epoch+1}/{epochs}] \"\n", + " f\"Loss: {avg_loss:.4f} \"\n", + " f\"Test Acc: {test_acc:.2f}%\"\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4e8cf5e", + "metadata": {}, + "outputs": [], + "source": [ + "# Mehr Kanäle\n", + "# Mehr Depthwise Blöcke\n", + "\n", + "class EfficientCNN_v2(nn.Module):\n", + " def __init__(self):\n", + " super().__init__()\n", + "\n", + " self.features = nn.Sequential(\n", + "\n", + " # Stem\n", + " nn.Conv2d(1, 8, 3, padding=1),\n", + " nn.BatchNorm2d(8),\n", + " nn.ReLU(),\n", + "\n", + " # DS Block 1\n", + " nn.Conv2d(8, 8, 3, padding=1, groups=8),\n", + " nn.Conv2d(8, 16, 1),\n", + " nn.BatchNorm2d(16),\n", + " nn.ReLU(),\n", + "\n", + " nn.MaxPool2d(2),\n", + "\n", + " # DS Block 2\n", + " nn.Conv2d(16, 16, 3, padding=1, groups=16),\n", + " nn.Conv2d(16, 32, 1),\n", + " nn.BatchNorm2d(32),\n", + " nn.ReLU(),\n", + "\n", + " nn.MaxPool2d(2),\n", + "\n", + " # DS Block 3\n", + " nn.Conv2d(32, 32, 3, padding=1, groups=32),\n", + " nn.Conv2d(32, 48, 1),\n", + " nn.BatchNorm2d(48),\n", + " nn.ReLU(),\n", + "\n", + " nn.AdaptiveAvgPool2d(1)\n", + " )\n", + "\n", + " self.classifier = nn.Sequential(\n", + " nn.Linear(64, 128),\n", + " nn.ReLU(),\n", + " nn.Dropout(0.2),\n", + " nn.Linear(128, 10)\n", + " )\n", + "\n", + " def forward(self, x):\n", + " x = self.features(x)\n", + " x = torch.flatten(x, 1)\n", + " return self.classifier(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4055292f", + "metadata": {}, + "outputs": [], + "source": [ + "efficient_model_v2 = EfficientCNN_v2().to(device)\n", + "\n", + "criterion = nn.CrossEntropyLoss()\n", + "\n", + "optimizer = torch.optim.Adam(\n", + " efficient_model_v2.parameters(),\n", + " lr=3e-4\n", + ")\n", + "\n", + "epochs = 40\n", + "\n", + "for epoch in range(epochs):\n", + "\n", + " efficient_model_v2.train()\n", + "\n", + " running_loss = 0.0\n", + "\n", + " for images, labels in train_loader:\n", + "\n", + " images = images.to(device)\n", + " labels = labels.to(device)\n", + "\n", + " optimizer.zero_grad()\n", + "\n", + " outputs = efficient_model_v2(images)\n", + "\n", + " loss = criterion(outputs, labels)\n", + "\n", + " loss.backward()\n", + "\n", + " optimizer.step()\n", + "\n", + " running_loss += loss.item()\n", + "\n", + " avg_loss = running_loss / len(train_loader)\n", + "\n", + " test_acc = evaluate(\n", + " efficient_model_v2,\n", + " test_loader\n", + " )\n", + "\n", + " print(\n", + " f\"Epoch [{epoch+1}/{epochs}] \"\n", + " f\"Loss: {avg_loss:.4f} \"\n", + " f\"Test Acc: {test_acc:.2f}%\"\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2ecc7605", + "metadata": {}, + "outputs": [], + "source": [ + "# Effizientes Modell entwickeln\n", + "# Standard-Convs\n", + "# GAP statt FC-Layer\n", + "\n", + "class EfficientCNN(nn.Module):\n", + " def __init__(self):\n", + " super().__init__()\n", + "\n", + " self.features = nn.Sequential(\n", + "\n", + " # 28x28\n", + " nn.Conv2d(1, 16, 3, padding=1),\n", + " nn.BatchNorm2d(16),\n", + " nn.ReLU(),\n", + "\n", + " nn.MaxPool2d(2), # -> 14x14\n", + "\n", + " # Block 1\n", + " nn.Conv2d(16, 32, 3, padding=1),\n", + " nn.BatchNorm2d(32),\n", + " nn.ReLU(),\n", + "\n", + " nn.MaxPool2d(2), # -> 7x7\n", + "\n", + " # Block 2\n", + " nn.Conv2d(32, 64, 3, padding=1),\n", + " nn.BatchNorm2d(64),\n", + " nn.ReLU(),\n", + "\n", + " nn.AdaptiveAvgPool2d(1)\n", + " )\n", + "\n", + " self.classifier = nn.Linear(64, 10)\n", + "\n", + " def forward(self, x):\n", + " x = self.features(x)\n", + " x = torch.flatten(x, 1)\n", + " return self.classifier(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "faaa2555", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch [1/40] Loss: 1.2384 Test Acc: 72.66%\n", + "Epoch [2/40] Loss: 0.6665 Test Acc: 78.41%\n", + "Epoch [3/40] Loss: 0.5299 Test Acc: 77.87%\n", + "Epoch [4/40] Loss: 0.4638 Test Acc: 82.23%\n", + "Epoch [5/40] Loss: 0.4224 Test Acc: 84.48%\n", + "Epoch [6/40] Loss: 0.3962 Test Acc: 85.04%\n", + "Epoch [7/40] Loss: 0.3755 Test Acc: 82.14%\n", + "Epoch [8/40] Loss: 0.3600 Test Acc: 85.85%\n", + "Epoch [9/40] Loss: 0.3455 Test Acc: 84.76%\n", + "Epoch [10/40] Loss: 0.3358 Test Acc: 86.58%\n", + "Epoch [11/40] Loss: 0.3249 Test Acc: 84.11%\n", + "Epoch [12/40] Loss: 0.3177 Test Acc: 85.51%\n", + "Epoch [13/40] Loss: 0.3106 Test Acc: 85.17%\n", + "Epoch [14/40] Loss: 0.3048 Test Acc: 84.21%\n", + "Epoch [15/40] Loss: 0.2999 Test Acc: 87.30%\n", + "Epoch [16/40] Loss: 0.2941 Test Acc: 86.06%\n", + "Epoch [17/40] Loss: 0.2930 Test Acc: 87.05%\n", + "Epoch [18/40] Loss: 0.2859 Test Acc: 88.07%\n", + "Epoch [19/40] Loss: 0.2820 Test Acc: 85.83%\n", + "Epoch [20/40] Loss: 0.2817 Test Acc: 88.14%\n", + "Epoch [21/40] Loss: 0.2768 Test Acc: 86.42%\n", + "Epoch [22/40] Loss: 0.2722 Test Acc: 87.58%\n", + "Epoch [23/40] Loss: 0.2696 Test Acc: 87.98%\n", + "Epoch [24/40] Loss: 0.2675 Test Acc: 86.37%\n", + "Epoch [25/40] Loss: 0.2642 Test Acc: 88.48%\n", + "Epoch [26/40] Loss: 0.2605 Test Acc: 88.29%\n", + "Epoch [27/40] Loss: 0.2586 Test Acc: 88.21%\n", + "Epoch [28/40] Loss: 0.2556 Test Acc: 87.15%\n", + "Epoch [29/40] Loss: 0.2537 Test Acc: 86.96%\n", + "Epoch [30/40] Loss: 0.2506 Test Acc: 88.43%\n", + "Epoch [31/40] Loss: 0.2493 Test Acc: 87.89%\n", + "Epoch [32/40] Loss: 0.2462 Test Acc: 89.21%\n", + "Epoch [33/40] Loss: 0.2448 Test Acc: 87.62%\n", + "Epoch [34/40] Loss: 0.2411 Test Acc: 85.96%\n", + "Epoch [35/40] Loss: 0.2394 Test Acc: 89.00%\n", + "Epoch [36/40] Loss: 0.2391 Test Acc: 88.24%\n", + "Epoch [37/40] Loss: 0.2363 Test Acc: 87.82%\n", + "Epoch [38/40] Loss: 0.2348 Test Acc: 87.72%\n", + "Epoch [39/40] Loss: 0.2331 Test Acc: 89.24%\n", + "Epoch [40/40] Loss: 0.2290 Test Acc: 88.09%\n" + ] + } + ], + "source": [ + "efficient_model = EfficientCNN().to(device)\n", + "\n", + "criterion = nn.CrossEntropyLoss()\n", + "\n", + "optimizer = torch.optim.Adam(\n", + " efficient_model.parameters(),\n", + " lr=3e-4,\n", + " weight_decay=1e-4\n", + ")\n", + "\n", + "epochs = 40\n", + "\n", + "for epoch in range(epochs):\n", + "\n", + " efficient_model.train()\n", + "\n", + " running_loss = 0.0\n", + "\n", + " for images, labels in train_loader:\n", + "\n", + " images = images.to(device)\n", + " labels = labels.to(device)\n", + "\n", + " optimizer.zero_grad()\n", + "\n", + " outputs = efficient_model(images)\n", + "\n", + " loss = criterion(outputs, labels)\n", + "\n", + " loss.backward()\n", + "\n", + " optimizer.step()\n", + "\n", + " running_loss += loss.item()\n", + "\n", + " avg_loss = running_loss / len(train_loader)\n", + "\n", + " test_acc = evaluate(\n", + " efficient_model,\n", + " test_loader\n", + " )\n", + "\n", + " print(\n", + " f\"Epoch [{epoch+1}/{epochs}] \"\n", + " f\"Loss: {avg_loss:.4f} \"\n", + " f\"Test Acc: {test_acc:.2f}%\"\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "55289655", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Parameters: 24,170\n" + ] + } + ], + "source": [ + "# Parameter zählen\n", + "print(f\"Parameters: {count_parameters(efficient_model):,}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "c2ca5ffc", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\denni\\AppData\\Local\\Temp\\ipykernel_15972\\4118664082.py:2: UserWarning: mods argument is not needed anymore, you can stop passing it\n", + " with FlopCounterMode(efficient_model) as flop_counter:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Module FLOP % Total\n", + "----------------------- --------- ---------\n", + "EfficientCNN 3839.744K 100.00%\n", + "- aten.convolution 3838.464K 99.97%\n", + "- aten.addmm 1.280K 0.03%\n", + "EfficientCNN.classifier 1.280K 0.03%\n", + "- aten.addmm 1.280K 0.03%\n", + "EfficientCNN.features 3838.464K 99.97%\n", + "- aten.convolution 3838.464K 99.97%\n", + "3839744\n" + ] + } + ], + "source": [ + "# FLOPs berechnen\n", + "with FlopCounterMode(efficient_model) as flop_counter:\n", + " efficient_model(dummy_input)\n", + "\n", + "print(flop_counter.get_total_flops())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c5155362", + "metadata": {}, + "outputs": [], + "source": [ + "# Accuracy vergleichen" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "eml", + "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.12.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}