{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e9cfe5db-43cb-4298-9388-d869d7314ea2", "metadata": {}, "outputs": [], "source": [ "import numpy as np \n", "import pandas as pd " ] }, { "cell_type": "code", "execution_count": 2, "id": "9a476b58-bb18-4499-96cd-4bf38ca7566f", "metadata": { "tags": [] }, "outputs": [], "source": [ "def img_flat(x):\n", " gray_img = np.mean(x, axis=0)\n", " flat_img = gray_img.flatten()\n", " return flat_img\n", "def add_to_dataset(x,y,z,l):\n", " y.at[z,'Label'] = l\n", " for i in range(0,1024):\n", " y.at[z,docnames[i+1]] = x[i]\n", " #print(z , \"Completed\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "6130f3c5-97bd-4be8-8751-9ffbae99436b", "metadata": { "tags": [] }, "outputs": [], "source": [ "docnames = [\"Pixel \" + str(i) for i in range(1024)]\n", "docnames.insert(0, 'Label')\n", "df1 = pd.DataFrame(columns = docnames) " ] }, { "cell_type": "code", "execution_count": 4, "id": "0d080bf5-067b-47a5-99dc-b22f145115b6", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/GTSRB_Final_Test_Images.zip to data/gtsrb/GTSRB_Final_Test_Images.zip\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 88978620/88978620 [00:10<00:00, 8777572.15it/s] \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Extracting data/gtsrb/GTSRB_Final_Test_Images.zip to data/gtsrb\n", "Downloading https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/GTSRB_Final_Test_GT.zip to data/gtsrb/GTSRB_Final_Test_GT.zip\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 99620/99620 [00:00<00:00, 289763.24it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Extracting data/gtsrb/GTSRB_Final_Test_GT.zip to data/gtsrb\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from torchvision.datasets import GTSRB\n", "from torchvision import transforms\n", "\n", "# Define a transform to convert the data to a NumPy array\n", "transform = transforms.Compose([\n", " transforms.Resize((32, 32)), \n", " transforms.ToTensor(),\n", "])\n", "\n", "# Download the dataset\n", "dataset = GTSRB(root='./data', split=\"test\", transform=transform, download=True)\n", "\n", "\n", "\n", "# Iterate through the dataset and apply transformations\n", "for i in range(len(dataset)):\n", " image, label = dataset[i]\n", " label = int(label)\n", " # Convert the PyTorch tensor to a NumPy array\n", " image_np = np.array(image)\n", " temp_img = img_flat(image_np)\n", " add_to_dataset(temp_img,df1,i,label)\n", " #data['label'].append(label)\n", " \n", " \n", "# Convert the data to a DataFrame\n", "#df = pd.DataFrame(data)\n", "\n", "# Save the DataFrame to a CSV file\n", "#df.to_csv('gtsrb_data.csv', index=False)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "1b64da5c-1326-4258-8066-6ab5debfec9d", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
| \n", " | Label | \n", "Pixel 0 | \n", "Pixel 1 | \n", "Pixel 2 | \n", "Pixel 3 | \n", "Pixel 4 | \n", "Pixel 5 | \n", "Pixel 6 | \n", "Pixel 7 | \n", "Pixel 8 | \n", "... | \n", "Pixel 1014 | \n", "Pixel 1015 | \n", "Pixel 1016 | \n", "Pixel 1017 | \n", "Pixel 1018 | \n", "Pixel 1019 | \n", "Pixel 1020 | \n", "Pixel 1021 | \n", "Pixel 1022 | \n", "Pixel 1023 | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "16 | \n", "0.563399 | \n", "0.556863 | \n", "0.559477 | \n", "0.560784 | \n", "0.555556 | \n", "0.550327 | \n", "0.54902 | \n", "0.546405 | \n", "0.537255 | \n", "... | \n", "0.551634 | \n", "0.54902 | \n", "0.545098 | \n", "0.550327 | \n", "0.554248 | \n", "0.54902 | \n", "0.539869 | \n", "0.547712 | \n", "0.551634 | \n", "0.554248 | \n", "
| 1 | \n", "1 | \n", "0.256209 | \n", "0.303268 | \n", "0.311111 | \n", "0.329412 | \n", "0.294118 | \n", "0.304575 | \n", "0.308497 | \n", "0.222222 | \n", "0.160784 | \n", "... | \n", "0.865359 | \n", "0.810458 | \n", "0.524183 | \n", "0.265359 | \n", "0.201307 | \n", "0.213072 | \n", "0.228758 | \n", "0.240523 | \n", "0.27451 | \n", "0.281046 | \n", "
| 2 | \n", "38 | \n", "0.171242 | \n", "0.166013 | \n", "0.164706 | \n", "0.166013 | \n", "0.164706 | \n", "0.15817 | \n", "0.162092 | \n", "0.163399 | \n", "0.160784 | \n", "... | \n", "0.150327 | \n", "0.115033 | \n", "0.135948 | \n", "0.118954 | \n", "0.115033 | \n", "0.134641 | \n", "0.142484 | \n", "0.155556 | \n", "0.169935 | \n", "0.179085 | \n", "
| 3 | \n", "33 | \n", "0.449673 | \n", "0.329412 | \n", "0.247059 | \n", "0.266667 | \n", "0.383007 | \n", "0.532026 | \n", "0.64183 | \n", "0.661438 | \n", "0.718954 | \n", "... | \n", "0.477124 | \n", "0.562092 | \n", "0.654902 | \n", "0.776471 | \n", "0.738562 | \n", "0.696732 | \n", "0.756863 | \n", "0.877124 | \n", "0.946405 | \n", "0.882353 | \n", "
| 4 | \n", "11 | \n", "0.132026 | \n", "0.145098 | \n", "0.15817 | \n", "0.155556 | \n", "0.150327 | \n", "0.145098 | \n", "0.15817 | \n", "0.184314 | \n", "0.203922 | \n", "... | \n", "0.147712 | \n", "0.141176 | \n", "0.138562 | \n", "0.145098 | \n", "0.151634 | \n", "0.156863 | \n", "0.155556 | \n", "0.162092 | \n", "0.171242 | \n", "0.177778 | \n", "
5 rows × 1025 columns
\n", "