이한솔

Add CNN

This diff is collapsed. Click to expand it.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import numpy as np\n",
"import os\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def Preprocess(dir_) :\n",
" f1 = open(dir_, 'r')\n",
" fr = csv.reader(f1)\n",
" d = [] \n",
" for line in fr :\n",
" l = []\n",
" l.append(line[0])\n",
" l.append(line[1])\n",
" l.extend(line[2:11])\n",
" \n",
" d.append(l)\n",
" return d\n",
"\n",
"data = Preprocess('Fuzzy.csv')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"data = data[:3838857]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['1349', '8', '216', '0', '0', '138', '0', '0', '0', '0', '1']\n"
]
}
],
"source": [
"print(data[0])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def createsequence(data) :\n",
" arr = []\n",
" for i in range(0, len(data)-11): \n",
" d = []\n",
" t = []\n",
" for x in range(i, i+11):\n",
" t = list(map(float, data[x]))\n",
" #t = np.array(t, dtype=np.float32)\n",
" #t = tf.convert_to_tensor(t)\n",
" d.append(t)\n",
" arr.append(d)\n",
" return arr\n",
"\n",
"resdata = createsequence(data)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def check(data):\n",
" check = []\n",
" \n",
" for e in data :\n",
" flag = False\n",
" for i in e :\n",
" if i[-1] == 0 :\n",
" check.append(0)\n",
" flag = True\n",
" break\n",
" if flag == False :\n",
" check.append(1)\n",
" \n",
" return check\n",
"\n",
"checkblock = check(resdata)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2582020\n",
"1256826\n"
]
}
],
"source": [
"cnt = 0\n",
"for e in checkblock :\n",
" if e == 1 :\n",
" cnt += 1\n",
"\n",
"print(cnt)\n",
"print(len(checkblock)-cnt)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"import tensorflow as tf\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(resdata)\n",
"n = len(df)\n",
"\n",
"X_train = df.iloc[0:n, 0:11].values.tolist()\n",
"Y_train = checkblock\n",
"\n",
"X_train = np.asarray(X_train)\n",
"Y_train = np.asarray(Y_train)\n",
"\n",
"x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.3, random_state=321)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"for i in x_train:\n",
" for j in i:\n",
" if len(j) != 11:\n",
" print(j)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1151654, 11, 11)\n"
]
}
],
"source": [
"print(x_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Model\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, LSTM, Conv1D, Dropout, GlobalAveragePooling1D, MaxPooling1D\n",
"from keras.utils import np_utils\n",
"from tensorflow.keras.utils import plot_model\n",
"from keras.layers.normalization import BatchNormalization"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv1d (Conv1D) (None, 9, 64) 2176 \n",
"_________________________________________________________________\n",
"global_average_pooling1d (Gl (None, 64) 0 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 64) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 1) 65 \n",
"=================================================================\n",
"Total params: 2,241\n",
"Trainable params: 2,241\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"('Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.')\n"
]
}
],
"source": [
"model = Sequential()\n",
"model.add(Conv1D(64, 3, input_shape=(11, 11), activation='relu'))\n",
"model.add(GlobalAveragePooling1D())\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(1, activation='sigmoid'))\n",
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
"model.summary()\n",
"\n",
"plot_model(model, to_file='CNNmodel.png')"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.1271 - accuracy: 0.9865 - val_loss: 0.0066 - val_accuracy: 0.9983\n",
"Epoch 2/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0129 - accuracy: 0.9969 - val_loss: 0.0060 - val_accuracy: 0.9986\n",
"Epoch 3/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0107 - accuracy: 0.9975 - val_loss: 0.0046 - val_accuracy: 0.9990\n",
"Epoch 4/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0093 - accuracy: 0.9979 - val_loss: 0.0037 - val_accuracy: 0.9993\n",
"Epoch 5/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0081 - accuracy: 0.9982 - val_loss: 0.0034 - val_accuracy: 0.9993\n",
"Epoch 6/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0074 - accuracy: 0.9984 - val_loss: 0.0042 - val_accuracy: 0.9991\n",
"Epoch 7/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0070 - accuracy: 0.9985 - val_loss: 0.0039 - val_accuracy: 0.9992\n",
"Epoch 8/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0065 - accuracy: 0.9987 - val_loss: 0.0033 - val_accuracy: 0.9994\n",
"Epoch 9/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0063 - accuracy: 0.9987 - val_loss: 0.0032 - val_accuracy: 0.9994\n",
"Epoch 10/10\n",
"26872/26872 [==============================] - 31s 1ms/step - loss: 0.0058 - accuracy: 0.9988 - val_loss: 0.0031 - val_accuracy: 0.9994\n"
]
}
],
"source": [
"history = model.fit(x_train, y_train, batch_size=100, epochs=10,validation_data=(x_test, y_test))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"35990/35990 [==============================] - 13s 361us/step - loss: 0.0031 - accuracy: 0.9994\n",
"\n",
" Test accuracy : 0.9994\n"
]
}
],
"source": [
"print(\"\\n Test accuracy : %.4f\"%(model.evaluate(x_test, y_test)[1]))"
]
},
{
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import numpy as np\n",
"import os\n",
"import pandas as pd\n",
"import import_ipynb\n",
"import DoS_CNN as dos"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dos.transferData('gear_dataset.csv', 'Spoofing.csv')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"data = dos.Preprocess('Spoofing.csv')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['320', '8', '0', '0', '0', '0', '16', '41', '42', '36', '1']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data[0]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def createsequence(data) :\n",
" arr = []\n",
" for i in range(0, len(data)-11): \n",
" d = []\n",
" t = []\n",
" for x in range(i, i+11):\n",
" t = list(map(float, data[x]))\n",
" d.append(t)\n",
" arr.append(d)\n",
" return arr\n",
"\n",
"resdata = createsequence(data)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def check(data):\n",
" check = []\n",
" \n",
" for e in data :\n",
" flag = False\n",
" for i in e :\n",
" if i[-1] == 0 :\n",
" check.append(0)\n",
" flag = True\n",
" break\n",
" if flag == False :\n",
" check.append(1)\n",
" \n",
" return check\n",
"\n",
"checkblock = check(resdata)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2580434\n",
"1862697\n"
]
}
],
"source": [
"cnt = 0 # 1\n",
"for i in checkblock :\n",
" if i == 1 :\n",
" cnt += 1\n",
"print(cnt) # normal message\n",
"print(len(checkblock)-cnt) #abnormal"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"import tensorflow as tf\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(resdata)\n",
"n = len(df)\n",
"\n",
"X_train = df.iloc[0:n, 0:11].values.tolist()\n",
"Y_train = checkblock\n",
"\n",
"X_train = np.asarray(X_train)\n",
"Y_train = np.asarray(Y_train)\n",
"\n",
"x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.3, random_state=321)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"for i in x_train:\n",
" for j in i:\n",
" if len(j) != 11:\n",
" print(j)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1332940, 11, 11)\n"
]
}
],
"source": [
"print(x_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Model\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, LSTM, Conv1D, Dropout, GlobalAveragePooling1D, MaxPooling1D\n",
"from keras.utils import np_utils\n",
"from keras.layers.normalization import BatchNormalization"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv1d (Conv1D) (None, 9, 64) 2176 \n",
"_________________________________________________________________\n",
"global_average_pooling1d (Gl (None, 64) 0 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 64) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 1) 65 \n",
"=================================================================\n",
"Total params: 2,241\n",
"Trainable params: 2,241\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model = Sequential()\n",
"model.add(Conv1D(64, 3, input_shape=(11, 11), activation='relu'))\n",
"model.add(GlobalAveragePooling1D())\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(1, activation='sigmoid'))\n",
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0739 - accuracy: 0.9938 - val_loss: 6.8605e-04 - val_accuracy: 1.0000\n",
"Epoch 2/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0029 - accuracy: 0.9995 - val_loss: 2.1258e-04 - val_accuracy: 1.0000\n",
"Epoch 3/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0030 - accuracy: 0.9995 - val_loss: 2.5308e-04 - val_accuracy: 1.0000\n",
"Epoch 4/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0025 - accuracy: 0.9996 - val_loss: 9.4737e-05 - val_accuracy: 1.0000\n",
"Epoch 5/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0031 - accuracy: 0.9995 - val_loss: 4.3025e-04 - val_accuracy: 1.0000\n",
"Epoch 6/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0022 - accuracy: 0.9997 - val_loss: 6.6521e-05 - val_accuracy: 1.0000\n",
"Epoch 7/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0020 - accuracy: 0.9997 - val_loss: 7.7669e-05 - val_accuracy: 1.0000\n",
"Epoch 8/10\n",
"31102/31102 [==============================] - 36s 1ms/step - loss: 0.0019 - accuracy: 0.9997 - val_loss: 1.3602e-04 - val_accuracy: 1.0000\n",
"Epoch 9/10\n",
"31102/31102 [==============================] - 35s 1ms/step - loss: 0.0018 - accuracy: 0.9997 - val_loss: 3.1278e-05 - val_accuracy: 1.0000\n",
"Epoch 10/10\n",
"31102/31102 [==============================] - 35s 1ms/step - loss: 0.0017 - accuracy: 0.9998 - val_loss: 3.1277e-05 - val_accuracy: 1.0000\n"
]
}
],
"source": [
"history = model.fit(x_train, y_train, batch_size=100, epochs=10,validation_data=(x_test, y_test))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"41655/41655 [==============================] - 15s 352us/step - loss: 3.1277e-05 - accuracy: 1.0000\n",
"\n",
" Test accuracy : 1.0000\n"
]
}
],
"source": [
"print(\"\\n Test accuracy : %.4f\"%(model.evaluate(x_test, y_test)[1]))"
]
},
{
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}