{ "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", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LabelPixel 0Pixel 1Pixel 2Pixel 3Pixel 4Pixel 5Pixel 6Pixel 7Pixel 8...Pixel 1014Pixel 1015Pixel 1016Pixel 1017Pixel 1018Pixel 1019Pixel 1020Pixel 1021Pixel 1022Pixel 1023
0160.5633990.5568630.5594770.5607840.5555560.5503270.549020.5464050.537255...0.5516340.549020.5450980.5503270.5542480.549020.5398690.5477120.5516340.554248
110.2562090.3032680.3111110.3294120.2941180.3045750.3084970.2222220.160784...0.8653590.8104580.5241830.2653590.2013070.2130720.2287580.2405230.274510.281046
2380.1712420.1660130.1647060.1660130.1647060.158170.1620920.1633990.160784...0.1503270.1150330.1359480.1189540.1150330.1346410.1424840.1555560.1699350.179085
3330.4496730.3294120.2470590.2666670.3830070.5320260.641830.6614380.718954...0.4771240.5620920.6549020.7764710.7385620.6967320.7568630.8771240.9464050.882353
4110.1320260.1450980.158170.1555560.1503270.1450980.158170.1843140.203922...0.1477120.1411760.1385620.1450980.1516340.1568630.1555560.1620920.1712420.177778
\n", "

5 rows × 1025 columns

\n", "
" ], "text/plain": [ " Label Pixel 0 Pixel 1 Pixel 2 Pixel 3 Pixel 4 Pixel 5 Pixel 6 \\\n", "0 16 0.563399 0.556863 0.559477 0.560784 0.555556 0.550327 0.54902 \n", "1 1 0.256209 0.303268 0.311111 0.329412 0.294118 0.304575 0.308497 \n", "2 38 0.171242 0.166013 0.164706 0.166013 0.164706 0.15817 0.162092 \n", "3 33 0.449673 0.329412 0.247059 0.266667 0.383007 0.532026 0.64183 \n", "4 11 0.132026 0.145098 0.15817 0.155556 0.150327 0.145098 0.15817 \n", "\n", " Pixel 7 Pixel 8 ... Pixel 1014 Pixel 1015 Pixel 1016 Pixel 1017 \\\n", "0 0.546405 0.537255 ... 0.551634 0.54902 0.545098 0.550327 \n", "1 0.222222 0.160784 ... 0.865359 0.810458 0.524183 0.265359 \n", "2 0.163399 0.160784 ... 0.150327 0.115033 0.135948 0.118954 \n", "3 0.661438 0.718954 ... 0.477124 0.562092 0.654902 0.776471 \n", "4 0.184314 0.203922 ... 0.147712 0.141176 0.138562 0.145098 \n", "\n", " Pixel 1018 Pixel 1019 Pixel 1020 Pixel 1021 Pixel 1022 Pixel 1023 \n", "0 0.554248 0.54902 0.539869 0.547712 0.551634 0.554248 \n", "1 0.201307 0.213072 0.228758 0.240523 0.27451 0.281046 \n", "2 0.115033 0.134641 0.142484 0.155556 0.169935 0.179085 \n", "3 0.738562 0.696732 0.756863 0.877124 0.946405 0.882353 \n", "4 0.151634 0.156863 0.155556 0.162092 0.171242 0.177778 \n", "\n", "[5 rows x 1025 columns]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1.head()" ] }, { "cell_type": "code", "execution_count": 6, "id": "deaf22c0-5aae-45e4-a4db-196fbcc001a1", "metadata": { "tags": [] }, "outputs": [], "source": [ "df1.to_csv('gtsrb_data_test.csv', index = False) " ] }, { "cell_type": "code", "execution_count": null, "id": "bbc65ef5-4313-42ed-8690-557ebca488b8", "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.17" } }, "nbformat": 4, "nbformat_minor": 5 }