{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A Transfer Learning and Optimized CNN Based Intrusion Detection System for Internet of Vehicles \n", "This is the code for the paper entitled \"**A Transfer Learning and Optimized CNN Based Intrusion Detection System for Internet of Vehicles**\" accepted in IEEE International Conference on Communications (IEEE ICC). \n", "Authors: Li Yang (lyang339@uwo.ca) and Abdallah Shami (Abdallah.Shami@uwo.ca) \n", "Organization: The Optimized Computing and Communications (OC2) Lab, ECE Department, Western University\n", "\n", "**Notebook 1: Data pre-processing** \n", "Procedures: \n", " 1): Read the dataset \n", " 2): Transform the tabular data into images \n", " 3): Display the transformed images \n", " 4): Split the training and test set " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import libraries" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2023-07-06T09:03:07.788679800Z", "start_time": "2023-07-06T09:03:07.746481Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import os\n", "import cv2\n", "import math\n", "import random\n", "import matplotlib.pyplot as plt\n", "import shutil\n", "from sklearn.preprocessing import QuantileTransformer\n", "from PIL import Image\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read the Car-Hacking/CAN-Intrusion dataset\n", "The complete Car-Hacking dataset is publicly available at: https://ocslab.hksecurity.net/Datasets/CAN-intrusion-dataset \n", "In this repository, due to the file size limit of GitHub, we use the 5% subset." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2023-07-06T09:03:08.040220300Z", "start_time": "2023-07-06T09:03:07.750003500Z" } }, "outputs": [], "source": [ "#Read dataset\n", "df=pd.read_csv('data/Car_Hacking_5%.csv')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": true, "ExecuteTime": { "end_time": "2023-07-06T09:03:08.050784400Z", "start_time": "2023-07-06T09:03:08.042218700Z" } }, "outputs": [ { "data": { "text/plain": " CAN ID DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] DATA[5] DATA[6] \\\n0 1201 41 39 39 35 0 0 0 \n1 809 64 187 127 20 17 32 0 \n2 1349 216 0 0 136 0 0 0 \n3 1201 41 39 39 35 0 0 0 \n4 2 0 0 0 0 0 3 2 \n... ... ... ... ... ... ... ... ... \n818435 848 5 32 52 104 117 0 0 \n818436 1088 255 0 0 0 255 134 9 \n818437 848 5 32 100 104 117 0 0 \n818438 1349 216 90 0 137 0 0 0 \n818439 790 5 33 48 10 33 30 0 \n\n DATA[7] Label \n0 154 R \n1 20 R \n2 0 R \n3 154 R \n4 228 R \n... ... ... \n818435 12 R \n818436 0 R \n818437 92 R \n818438 0 R \n818439 111 R \n\n[818440 rows x 10 columns]", "text/html": "
\n | CAN ID | \nDATA[0] | \nDATA[1] | \nDATA[2] | \nDATA[3] | \nDATA[4] | \nDATA[5] | \nDATA[6] | \nDATA[7] | \nLabel | \n
---|---|---|---|---|---|---|---|---|---|---|
0 | \n1201 | \n41 | \n39 | \n39 | \n35 | \n0 | \n0 | \n0 | \n154 | \nR | \n
1 | \n809 | \n64 | \n187 | \n127 | \n20 | \n17 | \n32 | \n0 | \n20 | \nR | \n
2 | \n1349 | \n216 | \n0 | \n0 | \n136 | \n0 | \n0 | \n0 | \n0 | \nR | \n
3 | \n1201 | \n41 | \n39 | \n39 | \n35 | \n0 | \n0 | \n0 | \n154 | \nR | \n
4 | \n2 | \n0 | \n0 | \n0 | \n0 | \n0 | \n3 | \n2 | \n228 | \nR | \n
... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n
818435 | \n848 | \n5 | \n32 | \n52 | \n104 | \n117 | \n0 | \n0 | \n12 | \nR | \n
818436 | \n1088 | \n255 | \n0 | \n0 | \n0 | \n255 | \n134 | \n9 | \n0 | \nR | \n
818437 | \n848 | \n5 | \n32 | \n100 | \n104 | \n117 | \n0 | \n0 | \n92 | \nR | \n
818438 | \n1349 | \n216 | \n90 | \n0 | \n137 | \n0 | \n0 | \n0 | \n0 | \nR | \n
818439 | \n790 | \n5 | \n33 | \n48 | \n10 | \n33 | \n30 | \n0 | \n111 | \nR | \n
818440 rows × 10 columns
\n\n | CAN ID | \nDATA[0] | \nDATA[1] | \nDATA[2] | \nDATA[3] | \nDATA[4] | \nDATA[5] | \nDATA[6] | \nDATA[7] | \n
---|---|---|---|---|---|---|---|---|---|
count | \n818440.000000 | \n818440.000000 | \n818440.000000 | \n818440.000000 | \n818440.000000 | \n818440.000000 | \n818440.000000 | \n818440.000000 | \n818440.000000 | \n
mean | \n127.457890 | \n113.711554 | \n107.926505 | \n89.813595 | \n109.978430 | \n105.412321 | \n112.250627 | \n84.973873 | \n93.112763 | \n
std | \n73.812063 | \n89.982269 | \n93.314034 | \n100.866477 | \n103.679776 | \n95.557986 | \n91.033532 | \n101.390068 | \n100.247486 | \n
min | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n
25% | \n66.621622 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n0.000000 | \n
50% | \n122.267267 | \n126.223724 | \n115.630631 | \n0.000000 | \n130.690691 | \n127.244745 | \n129.159159 | \n0.000000 | \n0.000000 | \n
75% | \n190.292793 | \n192.590090 | \n192.972973 | \n199.992492 | \n191.186186 | \n192.717718 | \n190.420420 | \n192.207207 | \n190.675676 | \n
max | \n255.000000 | \n255.000000 | \n255.000000 | \n255.000000 | \n255.000000 | \n255.000000 | \n255.000000 | \n255.000000 | \n255.000000 | \n