diff --git a/FirstWeParse.ipynb b/FirstWeParse.ipynb new file mode 100644 index 0000000..7c24743 --- /dev/null +++ b/FirstWeParse.ipynb @@ -0,0 +1,547 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import glob\n", + "import numpy as np\n", + "import pandas as pd\n", + "from torch import nn, optim\n", + "from torch.nn import functional as F\n", + "import time \n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Give me the PATH of the FIRST Category" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "path = 'C:\\\\Users\\\\masg\\\\Desktop\\\\Classification\\\\Short Window\\\\Bad\\\\' " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "folders = glob.glob(path + \"/*\") \n", + "print(\"These are the channels I found: \")\n", + "print(folders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for folder in folders:\n", + " # print(folder)\n", + " allFiles = glob.glob(folder + \"/*.csv\")\n", + " # print(allFiles)\n", + " firstTime = True\n", + " for file_ in allFiles:\n", + " singleFile = torch.from_numpy(pd.read_csv(file_, index_col=0, header=None).reset_index().values).view(-1)\n", + " if firstTime:\n", + " dataTorch = singleFile.view(1, 1, -1)\n", + " firstTime = False\n", + " else:\n", + " dataTorch = torch.cat((dataTorch, singleFile.view(1, 1, -1)), 0)\n", + " data.append(dataTorch)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dataCat1 = torch.cat(data, 1)\n", + "print(\"Shape of the data: \")\n", + "print(dataCat1.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Give me the PATH of the SECOND Category" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "path = 'C:\\\\Users\\\\masg\\\\Desktop\\\\Classification\\\\Short Window\\\\Good\\\\' " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "folders = glob.glob(path + \"/*\") \n", + "print(\"These are the channels I found: \")\n", + "print(folders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for folder in folders:\n", + " # print(folder)\n", + " allFiles = glob.glob(folder + \"/*.csv\")\n", + " # print(allFiles)\n", + " firstTime = True\n", + " for file_ in allFiles:\n", + " singleFile = torch.from_numpy(pd.read_csv(file_, index_col=0, header=None).reset_index().values).view(-1)\n", + " if firstTime:\n", + " dataTorch = singleFile.view(1, 1, -1)\n", + " firstTime = False\n", + " else:\n", + " dataTorch = torch.cat((dataTorch, singleFile.view(1, 1, -1)), 0)\n", + " data.append(dataTorch)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dataCat2 = torch.cat(data, 1)\n", + "print(\"Shape of the data: \")\n", + "print(dataCat2.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Give me the PATH of the THIRD Category" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "path = 'C:\\\\Users\\\\masg\\\\Desktop\\\\Classification\\\\Short Window\\\\Medium\\\\' " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "folders = glob.glob(path + \"/*\") \n", + "print(\"These are the channels I found: \")\n", + "print(folders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for folder in folders:\n", + " # print(folder)\n", + " allFiles = glob.glob(folder + \"/*.csv\")\n", + " # print(allFiles)\n", + " firstTime = True\n", + " for file_ in allFiles:\n", + " singleFile = torch.from_numpy(pd.read_csv(file_, index_col=0, header=None).reset_index().values).view(-1)\n", + " if firstTime:\n", + " dataTorch = singleFile.view(1, 1, -1)\n", + " firstTime = False\n", + " else:\n", + " dataTorch = torch.cat((dataTorch, singleFile.view(1, 1, -1)), 0)\n", + " data.append(dataTorch)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dataCat3 = torch.cat(data, 1)\n", + "print(\"Shape of the data: \")\n", + "print(dataCat3.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Give me the PATH of the FOURTH Category" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "path = 'C:\\\\Users\\\\masg\\\\Desktop\\\\Classification\\\\Short Window\\\\Medium\\\\' " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "folders = glob.glob(path + \"/*\") \n", + "print(\"These are the channels I found: \")\n", + "print(folders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for folder in folders:\n", + " # print(folder)\n", + " allFiles = glob.glob(folder + \"/*.csv\")\n", + " # print(allFiles)\n", + " firstTime = True\n", + " for file_ in allFiles:\n", + " singleFile = torch.from_numpy(pd.read_csv(file_, index_col=0, header=None).reset_index().values).view(-1)\n", + " if firstTime:\n", + " dataTorch = singleFile.view(1, 1, -1)\n", + " firstTime = False\n", + " else:\n", + " dataTorch = torch.cat((dataTorch, singleFile.view(1, 1, -1)), 0)\n", + " data.append(dataTorch)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dataCat4 = torch.cat(data, 1)\n", + "print(\"Shape of the data: \")\n", + "print(dataCat4.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Give me the PATH of the FIFTH Category" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "path = 'C:\\\\Users\\\\masg\\\\Desktop\\\\Classification\\\\Short Window\\\\Medium\\\\' " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "folders = glob.glob(path + \"/*\") \n", + "print(\"These are the channels I found: \")\n", + "print(folders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for folder in folders:\n", + " # print(folder)\n", + " allFiles = glob.glob(folder + \"/*.csv\")\n", + " # print(allFiles)\n", + " firstTime = True\n", + " for file_ in allFiles:\n", + " singleFile = torch.from_numpy(pd.read_csv(file_, index_col=0, header=None).reset_index().values).view(-1)\n", + " if firstTime:\n", + " dataTorch = singleFile.view(1, 1, -1)\n", + " firstTime = False\n", + " else:\n", + " dataTorch = torch.cat((dataTorch, singleFile.view(1, 1, -1)), 0)\n", + " data.append(dataTorch)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dataCat5 = torch.cat(data, 1)\n", + "print(\"Shape of the data: \")\n", + "print(dataCat5.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Give me the length of every signal" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "minLen = 20000" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Let's cut CAT1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for k in range(dataCat1.size(2)//minLen):\n", + " #print(k)\n", + " data.append(dataCat1.narrow(2, k*minLen, minLen))\n", + "\n", + "dataCat1Cutted = torch.cat(data, 0)\n", + "dataCat1Cutted.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Let's cut CAT2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for k in range(dataCat2.size(2)//minLen):\n", + " #print(k)\n", + " data.append(dataCat2.narrow(2, k*minLen, minLen))\n", + "\n", + "dataCat2Cutted = torch.cat(data, 0)\n", + "dataCat2Cutted.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Let's cut CAT3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for k in range(dataCat3.size(2)//minLen):\n", + " #print(k)\n", + " data.append(dataCat3.narrow(2, k*minLen, minLen))\n", + "\n", + "dataCat3Cutted = torch.cat(data, 0)\n", + "dataCat3Cutted.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Let's cut CAT4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for k in range(dataCat4.size(2)//minLen):\n", + " #print(k)\n", + " data.append(dataCat4.narrow(2, k*minLen, minLen))\n", + "\n", + "dataCat4Cutted = torch.cat(data, 0)\n", + "dataCat4Cutted.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Let's cut CAT5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "for k in range(dataCat5.size(2)//minLen):\n", + " #print(k)\n", + " data.append(dataCat5.narrow(2, k*minLen, minLen))\n", + "\n", + "dataCat5Cutted = torch.cat(data, 0)\n", + "dataCat5Cutted.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Save the data CAT 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "torch.save(dataCat1Cutted, 'dataCat1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Save the data CAT 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "torch.save(dataCat2Cutted, 'dataCat2')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Save the data CAT 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "torch.save(dataCat3Cutted, 'dataCat3')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Save the data CAT 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "torch.save(dataCat4Cutted, 'dataCat4')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Save the data CAT 5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "torch.save(dataCat5Cutted, 'dataCat5')" + ] + } + ], + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Train a CNN OMG.ipynb b/Train a CNN OMG.ipynb new file mode 100644 index 0000000..c4b3399 --- /dev/null +++ b/Train a CNN OMG.ipynb @@ -0,0 +1,566 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import torch.utils.data\n", + "import glob\n", + "import numpy as np\n", + "import pandas as pd\n", + "from torch import nn, optim\n", + "from torch.nn import functional as F\n", + "import time\n", + "from torch.nn.utils import weight_norm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data1 = torch.load('dataCat1')\n", + "data2 = torch.load('dataCat2')\n", + "data3 = torch.load('dataCat3')\n", + "data4 = torch.load('dataCat4')\n", + "data5 = torch.load('dataCat5')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print('Dim 1: ', data1.shape)\n", + "print('Dim 2: ', data2.shape)\n", + "print('Dim 3: ', data3.shape)\n", + "print('Dim 4: ', data4.shape)\n", + "print('Dim 5: ', data5.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "numChannels = 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "shuffledIndexes = torch.randperm(data1.size(0))\n", + "data1 = data1[shuffledIndexes]\n", + "data1Train = data1.narrow(0, 0, int(data1.size(0) * 0.8))\n", + "label1Train = torch.zeros(int(data1.size(0) * 0.8))\n", + "data1Test = data1.narrow(0, int(data1.size(0) * 0.8), data1.size(0)-int(data1.size(0) * 0.8))\n", + "label1Test = torch.zeros(data1.size(0)-int(data1.size(0) * 0.8))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "shuffledIndexes = torch.randperm(data2.size(0))\n", + "data2 = data2[shuffledIndexes]\n", + "data2Train = data2.narrow(0, 0, int(data2.size(0) * 0.8))\n", + "label2Train = torch.ones(int(data2.size(0) * 0.8))\n", + "data2Test = data2.narrow(0, int(data2.size(0) * 0.8), data2.size(0)-int(data2.size(0) * 0.8))\n", + "label2Test = torch.ones(data2.size(0)-int(data2.size(0) * 0.8))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "shuffledIndexes = torch.randperm(data3.size(0))\n", + "data3 = data3[shuffledIndexes]\n", + "data3Train = data3.narrow(0, 0, int(data3.size(0) * 0.8))\n", + "label3Train = torch.ones(int(data3.size(0) * 0.8))*2\n", + "data3Test = data3.narrow(0, int(data3.size(0) * 0.8), data3.size(0)-int(data3.size(0) * 0.8))\n", + "label3Test = torch.ones(data3.size(0)-int(data3.size(0) * 0.8))*3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "shuffledIndexes = torch.randperm(data4.size(0))\n", + "data4 = data4[shuffledIndexes]\n", + "data4Train = data4.narrow(0, 0, int(data4.size(0) * 0.8))\n", + "label4Train = torch.ones(int(data4.size(0) * 0.8))*4\n", + "data4Test = data4.narrow(0, int(data4.size(0) * 0.8), data4.size(0)-int(data4.size(0) * 0.8))\n", + "label4Test = torch.ones(data4.size(0)-int(data4.size(0) * 0.8))*4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "shuffledIndexes = torch.randperm(data5.size(0))\n", + "data5 = data5[shuffledIndexes]\n", + "data5Train = data5.narrow(0, 0, int(data5.size(0) * 0.8))\n", + "label5Train = torch.ones(int(data5.size(0) * 0.8))*4\n", + "data5Test = data5.narrow(0, int(data5.size(0) * 0.8), data5.size(0)-int(data5.size(0) * 0.8))\n", + "label5Test = torch.ones(data5.size(0)-int(data5.size(0) * 0.8))*4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "train_input = torch.cat((data1Train, data2Train, data3Train, data4Train, data5Train), 0) \n", + "train_target = torch.cat((label1Train, label2Train, label3Train, label4Train, label5Train), 0)\n", + "print(\"Train input dim: \", train_input.shape)\n", + "print(\"Train target dim: \", train_target.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "train_target" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "test_input = torch.cat((data1Test, data2Test, data3Test, data4Test, data5Test), 0) \n", + "test_target = torch.cat((label1Test, label2Test, label3Test, label4Test, label5Test), 0)\n", + "print(\"Test input dim: \", test_input.shape)\n", + "print(\"Test target dim: \", test_target.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "i = 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# plt.plot(data2Test.narrow(0, i, 1).narrow(1, 0, 1).view(-1).numpy())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# This is the formula for computing the size of the output of every layer\n", + "\n", + "$$L_{out} = \\frac{L_{in}- Kernel_{size}}{stride}+1$$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Here there's and example: \n", + "$input size = 1 * 20'000 * 4 $\n", + "\n", + "after the first convolution:\n", + "$output = \\frac{20000-5000}{5}+1 = 3001$\n", + "and the channels go from the initial 4 to the specified 32\n", + "\n", + "after the second convolution:\n", + "$output = \\frac{3001-2002}{1}+1 = 1000$\n", + "and the channels go from 32 to 64\n", + "\n", + "after the last convolution:\n", + "$output = \\frac{1000-1000}{1}+1 = 1$\n", + "and the channels go from 64 to 5 (as the number of classes!!!)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class fastCNN(nn.Module): \n", + " def __init__(self):\n", + " super(fastCNN, self).__init__()\n", + " self.conv = nn.Conv1d(4, 32, kernel_size=5000, stride=5) \n", + " self.bn = nn.BatchNorm1d(32) #3'001\n", + " self.conv2 = nn.Conv1d(32, 64, kernel_size=2002)\n", + " self.bn2 = nn.BatchNorm1d(64) #1000\n", + " self.conv3 = nn.Conv1d(64, 5, kernel_size=1000)\n", + " \n", + " def forward(self, x):\n", + " x = F.relu(self.bn(self.conv(x)))\n", + " x = F.relu(self.bn2(self.conv2(x)))\n", + " x = self.conv3(x)\n", + " x = x.view(-1, 5)\n", + " return x" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def compute_nb_errors(model, input_data, target, batch_size):\n", + " nb_errors=0\n", + " for b in range(0, input_data.size(0), batch_size):\n", + " output = model.forward(input_data.narrow(0, b, batch_size))\n", + " predicted_classes = output.max(1)[1]\n", + " targetting = target.narrow(0, b, batch_size)\n", + " nb_errors += (predicted_classes != targetting).long().sum()\n", + " return nb_errors" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model, criterion = fastCNN(), nn.CrossEntropyLoss()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model = model.cuda()\n", + "train_input = train_input.view(train_input.size(0), numChannels, -1).float()\n", + "train_target = train_target.long().view(-1)\n", + "test_input = test_input.view(test_input.size(0), numChannels, -1).float()\n", + "test_target = test_target.long().view(-1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# out = model(train_input.narrow(0, 0, 2).cuda())\n", + "# out" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "lr = 3e-3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "optimizer = optim.Adam(model.parameters(), lr = lr)\n", + "nb_epochs = 1000\n", + "scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', verbose=True, factor=0.2,patience = 30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "numClasses = 5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class errorsTable():\n", + " def __init__(self):\n", + " self.accuracyMatr = np.zeros((numClasses,numClasses))\n", + " def newEntry(self,predictions,groundTruths):\n", + " for pred,tru in zip(predictions,groundTruths):\n", + " #print(pred, tru)\n", + " self.accuracyMatr[pred,tru]+=1\n", + " def getStats(self):\n", + " #print(self.accuracyMatr)\n", + " numEntry = np.sum(self.accuracyMatr,0)\n", + " resultMatr = self.accuracyMatr/numEntry\n", + " return resultMatr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "np.set_printoptions(precision=2, suppress=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "batch_size_train = 100\n", + "batch_size_test = 100" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "torch.backends.cudnn.enabled = False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "training_set = torch.utils.data.TensorDataset(train_input, train_target)\n", + "testing_set = torch.utils.data.TensorDataset(test_input, test_target)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Generators\n", + "training_generator = torch.utils.data.DataLoader(training_set, batch_size=batch_size_train, num_workers=12,\n", + " shuffle=True, drop_last=True)\n", + "testing_generator = torch.utils.data.DataLoader(testing_set, batch_size=batch_size_test, num_workers=12,\n", + " shuffle=True, drop_last=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "training_generator" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "t = time.time()\n", + "for k in range (nb_epochs):\n", + " \n", + " # Create a new error table\n", + " accuracyTRAIN = errorsTable()\n", + " accuracyTEST = errorsTable()\n", + " print('Epoch: ', k)\n", + " model.train()\n", + "\n", + " lossValue = 0\n", + " for local_batch, local_labels in training_generator:\n", + " # Transfer to GPU\n", + " local_batch, local_labels = local_batch.cuda(), local_labels.cuda() \n", + " model.zero_grad()\n", + " output = model(local_batch)\n", + " accuracyTRAIN.newEntry(output.max(1)[1], local_labels)\n", + " loss = criterion(output, local_labels)\n", + " lossValue += loss.item()*local_batch.size(0)\n", + " loss.backward()\n", + " optimizer.step()\n", + " scheduler.step(lossValue/train_input.size(0))\n", + " print('Loss is ', lossValue/train_input.size(0))\n", + " \n", + " ################################################################ \n", + " model.eval()\n", + " lossValue = 0 \n", + " with torch.set_grad_enabled(False):\n", + " for local_batch, local_labels in testing_generator:\n", + " # Transfer to GPU\n", + " local_batch, local_labels = local_batch.cuda(), local_labels.cuda()\n", + " outputTest = model(local_batch)\n", + " accuracyTEST.newEntry(outputTest.max(1)[1], local_labels)\n", + " lossTest = criterion(outputTest, local_labels)\n", + " lossValue += lossTest.item()*local_batch.size(0)\n", + " print('Loss TEST is ', lossValue/test_input.size(0))\n", + " \n", + " print(\"\")\n", + " print(\"The accuracy in TEST is: \")\n", + " print(\" 1 2 3 4 5\")\n", + " print(accuracyTEST.getStats())\n", + " print(\"The accuracy in TRAIN is: \")\n", + " print(\" 1 2 3 4 5\")\n", + " print(accuracyTRAIN.getStats())\n", + " print(\"----------------------\")\n", + " if k%10 == 0:\n", + " #nb_test_errors = compute_nb_errors(model, test_input, test_target, batch_size_test)\n", + "\n", + " #print(k, ': Test accuracy Net {:0.2f}%% {:d}/{:d}'.format(100-(100 * nb_test_errors) / test_input.size(0),\n", + " #nb_test_errors, test_input.size(0)))\n", + " \n", + "\n", + " #nb_train_errors = compute_nb_errors(model, train_input, train_target, batch_size_train)\n", + "\n", + " #print(k, ': Train accuracy Net {:0.2f}%% {:d}/{:d}'.format(100-(100 * nb_train_errors) / train_input.size(0),\n", + " #nb_train_errors, train_input.size(0)))\n", + " print(\"Hey! Ten epochs are passed! We are getting older\")\n", + " \n", + " \n", + "\n", + "\n", + "nb_test_errors = compute_nb_errors(model, test_input, test_target, batch_size_test) \n", + "print('Final Test error Net {:0.2f}% {:d}/{:d}'.format((100 * nb_test_errors) / test_input.size(0),\n", + " nb_test_errors, test_input.size(0)))\n", + "\n", + "elapsed = time.time() - t\n", + "print(elapsed)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "accuracyTEST = errorsTable()\n", + "\n", + "model.eval()\n", + "lossValue = 0 \n", + "with torch.set_grad_enabled(False):\n", + " for local_batch, local_labels in testing_generator:\n", + " # Transfer to GPU\n", + " local_batch, local_labels = local_batch.cuda(), local_labels.cuda()\n", + " outputTest = model(local_batch)\n", + " accuracyTEST.newEntry(outputTest.max(1)[1], local_labels)\n", + " lossTest = criterion(outputTest, local_labels)\n", + " lossValue += lossTest.item()*local_batch.size(0)\n", + "print('Loss TEST is ', lossValue/test_input.size(0))\n", + "\n", + "print(\"\")\n", + "print(\"The accuracy in TEST is: \")\n", + "print(\" 1 2 3 4 5 6\")\n", + "print(accuracyTEST.getStats())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model.zero_grad()\n", + "output = model(test_input.cuda())\n", + "#loss = criterion(output, train_target.narrow(0, b, batch_size_train))\n", + "output[80:100]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "train_target.narrow(0, b, batch_size_train).shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outputTest = model(test_input)\n", + "outputTest.max(1)[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "test_target" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "torch.save(model, 'modelTRi')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}