Spaces:
Sleeping
Sleeping
Commit ·
285f2e9
1
Parent(s): 94ce1f9
Numpy Arrays to Tensors + Forward Prop
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203{ "cells": [ { "cell_type": "markdown", "id": "9d0a3c0e", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 11, "id": "51e70576", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GPU is available\n", "Converting Arrays to Tensors . . . .\n", "torch.Size([11197, 150, 150, 3]) cpu\n", "torch.Size([11197]) cpu\n", "Tensors Created!\n" ] } ], "source": [ "\"\"\"1.This cell contains the code loading,checking and reshaping the dataset for further processing\"\"\" \n", "\n", "import torch \n", "import numpy as np\n", "\n", "#Loading Nmpy Arrays from the dataset\n", "raw = np.load(\"flowers_data.npy\")\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\") #Fallback to CPU if GPU is not available\n", "if(device.type == \"cuda\"):\n", " print(\"GPU is available\")\n", " print(\"Converting Arrays to Tensors . . . .\")\n", " #Converting the numpy arrays to torch tensors for faster computing\n", " converted_x_label = torch.from_numpy(raw['X']).float() #W have converted to float because its necessary for further calculation accuracy\n", " converted_y_label = torch.from_numpy(raw['Y']).float()\n", "print(converted_x_label.shape,converted_x_label.device)\n", "print(converted_y_label.shape,converted_y_label.device)\n", "print(\"Tensors Created!\")\n", "\n", "#So the shape becomes (N, H, W, C) where N is the number of samples, H is the height of the image, W is the width of the image and C is the number of channels. But the model we are using expects the input shape to be (N, C, H, W). So we need to permute the dimensions of the tensor to match the input shape of the model.\n", "permuted = torch.permute(converted_x_label, (0, 3, 1, 2)) #Permuting the dimensions of the tensor to match the input shape of the model\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "6723a3ca", "metadata": {}, "outputs": [], "source": [ "\"\"\"This cell is used to shuffle the X and Y labels so that the model is \n", "actually learning and not memorizing the whole dataset\n", "\n", "\"\"\"\n", "indices = torch.randperm(len(permuted))\n", "X_shuffled = permuted[indices] / 255.0\n", "Y_shuffled = converted_y_label[indices].long().squeeze()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7c295080", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8957 8957 2240 2240\n" ] } ], "source": [ "#This cell splits the shuffled data into Training ans Test sets\n", "#80% of the data is used for training and 20% for testing\n", "train_split = int(0.8 * len(X_shuffled))\n", "X_train = X_shuffled[:train_split]\n", "Y_train = Y_shuffled[:train_split]\n", "X_test = X_shuffled[train_split:]\n", "Y_test = Y_shuffled[train_split:] \n", "print(len(X_train),len(Y_train),len(X_test),len(Y_test))\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d9535811", "metadata": {}, "outputs": [], "source": [ "from torch.utils.data import DataLoader\n", "batch__size = 64\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "5756ee9d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Moved X and Y label of training data to cuda successfully\n", "Moved X and Y label of testing data to cuda successfully\n" ] } ], "source": [ "#Moving the tensors to the device (GPU or CPU) for faster computing\n", "move_x_train = X_train.to(device) \n", "move_y_label = Y_train.to(device)\n", "print(f\"Moved X and Y label of training data to {device} successfully\") \n", "move_x_test = X_test.to(device)\n", "move_y_test = Y_test.to(device)\n", "print(f\"Moved X and Y label of testing data to {device} successfully\")" ] }, { "cell_type": "code", "execution_count": null, "id": "26066eae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 16, "id": "41cfdb24", "metadata": {}, "outputs": [], "source": [ "from torch import nn\n", "\n", "class FlowerClassifier(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " #Neural Network Initilization\n", "\n", " self.layer_1 = nn.Linear(in_features = 67500,out_features =128,bias=True)\n", " self.relu_1 = nn.ReLU()\n", " self.layer_2 = nn.Linear(in_features=128,out_features=64,bias=True)\n", " self.relu_2 = nn.ReLU()\n", " self.layer_3 = nn.Linear(in_features=64,out_features=32,bias=True)\n", " self.relu_3 = nn.ReLU()\n", "\n", " #Output Layer\n", " self.out = nn.Linear(in_features = 32, out_features=7)\n", "\n", " def forward1(self,x):\n", " self.flatten = nn.Flatten()\n", " x = self.flatten(x)\n", " x = self.relu_1(self.layer_1(x))\n", " x = self.relu_2(self.layer_2(x))\n", " x = self.relu_3(self.layer_3(x))\n", " x = self.out(x)\n", " return x\n" ] }, { "cell_type": "code", "execution_count": null, "id": "40d53a08", "metadata": {}, "outputs": [], "source": [ "#" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.14.7" } }, "nbformat": 4, "nbformat_minor": 5}PyTorchtrained.ipynbAdded+203