이한솔

Add CNN

This diff is collapsed. Click to expand it.
1 +{
2 + "cells": [
3 + {
4 + "cell_type": "code",
5 + "execution_count": 1,
6 + "metadata": {},
7 + "outputs": [],
8 + "source": [
9 + "import csv\n",
10 + "import numpy as np\n",
11 + "import os\n",
12 + "import pandas as pd"
13 + ]
14 + },
15 + {
16 + "cell_type": "code",
17 + "execution_count": 5,
18 + "metadata": {},
19 + "outputs": [],
20 + "source": [
21 + "def Preprocess(dir_) :\n",
22 + " f1 = open(dir_, 'r')\n",
23 + " fr = csv.reader(f1)\n",
24 + " d = [] \n",
25 + " for line in fr :\n",
26 + " l = []\n",
27 + " l.append(line[0])\n",
28 + " l.append(line[1])\n",
29 + " l.extend(line[2:11])\n",
30 + " \n",
31 + " d.append(l)\n",
32 + " return d\n",
33 + "\n",
34 + "data = Preprocess('Fuzzy.csv')"
35 + ]
36 + },
37 + {
38 + "cell_type": "code",
39 + "execution_count": 6,
40 + "metadata": {},
41 + "outputs": [],
42 + "source": [
43 + "data = data[:3838857]"
44 + ]
45 + },
46 + {
47 + "cell_type": "code",
48 + "execution_count": 7,
49 + "metadata": {},
50 + "outputs": [
51 + {
52 + "name": "stdout",
53 + "output_type": "stream",
54 + "text": [
55 + "['1349', '8', '216', '0', '0', '138', '0', '0', '0', '0', '1']\n"
56 + ]
57 + }
58 + ],
59 + "source": [
60 + "print(data[0])"
61 + ]
62 + },
63 + {
64 + "cell_type": "code",
65 + "execution_count": 8,
66 + "metadata": {},
67 + "outputs": [],
68 + "source": [
69 + "def createsequence(data) :\n",
70 + " arr = []\n",
71 + " for i in range(0, len(data)-11): \n",
72 + " d = []\n",
73 + " t = []\n",
74 + " for x in range(i, i+11):\n",
75 + " t = list(map(float, data[x]))\n",
76 + " #t = np.array(t, dtype=np.float32)\n",
77 + " #t = tf.convert_to_tensor(t)\n",
78 + " d.append(t)\n",
79 + " arr.append(d)\n",
80 + " return arr\n",
81 + "\n",
82 + "resdata = createsequence(data)"
83 + ]
84 + },
85 + {
86 + "cell_type": "code",
87 + "execution_count": 9,
88 + "metadata": {},
89 + "outputs": [],
90 + "source": [
91 + "def check(data):\n",
92 + " check = []\n",
93 + " \n",
94 + " for e in data :\n",
95 + " flag = False\n",
96 + " for i in e :\n",
97 + " if i[-1] == 0 :\n",
98 + " check.append(0)\n",
99 + " flag = True\n",
100 + " break\n",
101 + " if flag == False :\n",
102 + " check.append(1)\n",
103 + " \n",
104 + " return check\n",
105 + "\n",
106 + "checkblock = check(resdata)"
107 + ]
108 + },
109 + {
110 + "cell_type": "code",
111 + "execution_count": 10,
112 + "metadata": {},
113 + "outputs": [
114 + {
115 + "name": "stdout",
116 + "output_type": "stream",
117 + "text": [
118 + "2582020\n",
119 + "1256826\n"
120 + ]
121 + }
122 + ],
123 + "source": [
124 + "cnt = 0\n",
125 + "for e in checkblock :\n",
126 + " if e == 1 :\n",
127 + " cnt += 1\n",
128 + "\n",
129 + "print(cnt)\n",
130 + "print(len(checkblock)-cnt)"
131 + ]
132 + },
133 + {
134 + "cell_type": "code",
135 + "execution_count": 31,
136 + "metadata": {},
137 + "outputs": [],
138 + "source": [
139 + "from sklearn.model_selection import train_test_split\n",
140 + "import tensorflow as tf\n",
141 + "import pandas as pd\n",
142 + "import numpy as np"
143 + ]
144 + },
145 + {
146 + "cell_type": "code",
147 + "execution_count": 32,
148 + "metadata": {},
149 + "outputs": [],
150 + "source": [
151 + "df = pd.DataFrame(resdata)\n",
152 + "n = len(df)\n",
153 + "\n",
154 + "X_train = df.iloc[0:n, 0:11].values.tolist()\n",
155 + "Y_train = checkblock\n",
156 + "\n",
157 + "X_train = np.asarray(X_train)\n",
158 + "Y_train = np.asarray(Y_train)\n",
159 + "\n",
160 + "x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.3, random_state=321)"
161 + ]
162 + },
163 + {
164 + "cell_type": "code",
165 + "execution_count": 33,
166 + "metadata": {},
167 + "outputs": [],
168 + "source": [
169 + "for i in x_train:\n",
170 + " for j in i:\n",
171 + " if len(j) != 11:\n",
172 + " print(j)"
173 + ]
174 + },
175 + {
176 + "cell_type": "code",
177 + "execution_count": 34,
178 + "metadata": {},
179 + "outputs": [
180 + {
181 + "name": "stdout",
182 + "output_type": "stream",
183 + "text": [
184 + "(1151654, 11, 11)\n"
185 + ]
186 + }
187 + ],
188 + "source": [
189 + "print(x_test.shape)"
190 + ]
191 + },
192 + {
193 + "cell_type": "code",
194 + "execution_count": 1,
195 + "metadata": {},
196 + "outputs": [],
197 + "source": [
198 + "# Model\n",
199 + "from keras.models import Sequential\n",
200 + "from keras.layers import Dense, LSTM, Conv1D, Dropout, GlobalAveragePooling1D, MaxPooling1D\n",
201 + "from keras.utils import np_utils\n",
202 + "from tensorflow.keras.utils import plot_model\n",
203 + "from keras.layers.normalization import BatchNormalization"
204 + ]
205 + },
206 + {
207 + "cell_type": "code",
208 + "execution_count": 2,
209 + "metadata": {},
210 + "outputs": [
211 + {
212 + "name": "stdout",
213 + "output_type": "stream",
214 + "text": [
215 + "Model: \"sequential\"\n",
216 + "_________________________________________________________________\n",
217 + "Layer (type) Output Shape Param # \n",
218 + "=================================================================\n",
219 + "conv1d (Conv1D) (None, 9, 64) 2176 \n",
220 + "_________________________________________________________________\n",
221 + "global_average_pooling1d (Gl (None, 64) 0 \n",
222 + "_________________________________________________________________\n",
223 + "dropout (Dropout) (None, 64) 0 \n",
224 + "_________________________________________________________________\n",
225 + "dense (Dense) (None, 1) 65 \n",
226 + "=================================================================\n",
227 + "Total params: 2,241\n",
228 + "Trainable params: 2,241\n",
229 + "Non-trainable params: 0\n",
230 + "_________________________________________________________________\n",
231 + "('Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.')\n"
232 + ]
233 + }
234 + ],
235 + "source": [
236 + "model = Sequential()\n",
237 + "model.add(Conv1D(64, 3, input_shape=(11, 11), activation='relu'))\n",
238 + "model.add(GlobalAveragePooling1D())\n",
239 + "model.add(Dropout(0.5))\n",
240 + "model.add(Dense(1, activation='sigmoid'))\n",
241 + "model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
242 + "model.summary()\n",
243 + "\n",
244 + "plot_model(model, to_file='CNNmodel.png')"
245 + ]
246 + },
247 + {
248 + "cell_type": "code",
249 + "execution_count": 37,
250 + "metadata": {},
251 + "outputs": [
252 + {
253 + "name": "stdout",
254 + "output_type": "stream",
255 + "text": [
256 + "Epoch 1/10\n",
257 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.1271 - accuracy: 0.9865 - val_loss: 0.0066 - val_accuracy: 0.9983\n",
258 + "Epoch 2/10\n",
259 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0129 - accuracy: 0.9969 - val_loss: 0.0060 - val_accuracy: 0.9986\n",
260 + "Epoch 3/10\n",
261 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0107 - accuracy: 0.9975 - val_loss: 0.0046 - val_accuracy: 0.9990\n",
262 + "Epoch 4/10\n",
263 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0093 - accuracy: 0.9979 - val_loss: 0.0037 - val_accuracy: 0.9993\n",
264 + "Epoch 5/10\n",
265 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0081 - accuracy: 0.9982 - val_loss: 0.0034 - val_accuracy: 0.9993\n",
266 + "Epoch 6/10\n",
267 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0074 - accuracy: 0.9984 - val_loss: 0.0042 - val_accuracy: 0.9991\n",
268 + "Epoch 7/10\n",
269 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0070 - accuracy: 0.9985 - val_loss: 0.0039 - val_accuracy: 0.9992\n",
270 + "Epoch 8/10\n",
271 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0065 - accuracy: 0.9987 - val_loss: 0.0033 - val_accuracy: 0.9994\n",
272 + "Epoch 9/10\n",
273 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0063 - accuracy: 0.9987 - val_loss: 0.0032 - val_accuracy: 0.9994\n",
274 + "Epoch 10/10\n",
275 + "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0058 - accuracy: 0.9988 - val_loss: 0.0031 - val_accuracy: 0.9994\n"
276 + ]
277 + }
278 + ],
279 + "source": [
280 + "history = model.fit(x_train, y_train, batch_size=100, epochs=10,validation_data=(x_test, y_test))"
281 + ]
282 + },
283 + {
284 + "cell_type": "code",
285 + "execution_count": 38,
286 + "metadata": {
287 + "scrolled": true
288 + },
289 + "outputs": [
290 + {
291 + "name": "stdout",
292 + "output_type": "stream",
293 + "text": [
294 + "35990/35990 [==============================] - 13s 361us/step - loss: 0.0031 - accuracy: 0.9994\n",
295 + "\n",
296 + " Test accuracy : 0.9994\n"
297 + ]
298 + }
299 + ],
300 + "source": [
301 + "print(\"\\n Test accuracy : %.4f\"%(model.evaluate(x_test, y_test)[1]))"
302 + ]
303 + },
304 + {
305 + "cell_type": "code",
306 + "execution_count": null,
307 + "metadata": {},
308 + "outputs": [],
309 + "source": []
310 + }
311 + ],
312 + "metadata": {
313 + "kernelspec": {
314 + "display_name": "Python 3",
315 + "language": "python",
316 + "name": "python3"
317 + },
318 + "language_info": {
319 + "codemirror_mode": {
320 + "name": "ipython",
321 + "version": 3
322 + },
323 + "file_extension": ".py",
324 + "mimetype": "text/x-python",
325 + "name": "python",
326 + "nbconvert_exporter": "python",
327 + "pygments_lexer": "ipython3",
328 + "version": "3.8.5"
329 + }
330 + },
331 + "nbformat": 4,
332 + "nbformat_minor": 4
333 +}
1 +{
2 + "cells": [
3 + {
4 + "cell_type": "code",
5 + "execution_count": 4,
6 + "metadata": {},
7 + "outputs": [],
8 + "source": [
9 + "import csv\n",
10 + "import numpy as np\n",
11 + "import os\n",
12 + "import pandas as pd\n",
13 + "import import_ipynb\n",
14 + "import DoS_CNN as dos"
15 + ]
16 + },
17 + {
18 + "cell_type": "code",
19 + "execution_count": null,
20 + "metadata": {},
21 + "outputs": [],
22 + "source": [
23 + "dos.transferData('gear_dataset.csv', 'Spoofing.csv')"
24 + ]
25 + },
26 + {
27 + "cell_type": "code",
28 + "execution_count": 5,
29 + "metadata": {},
30 + "outputs": [],
31 + "source": [
32 + "data = dos.Preprocess('Spoofing.csv')"
33 + ]
34 + },
35 + {
36 + "cell_type": "code",
37 + "execution_count": 10,
38 + "metadata": {},
39 + "outputs": [
40 + {
41 + "data": {
42 + "text/plain": [
43 + "['320', '8', '0', '0', '0', '0', '16', '41', '42', '36', '1']"
44 + ]
45 + },
46 + "execution_count": 10,
47 + "metadata": {},
48 + "output_type": "execute_result"
49 + }
50 + ],
51 + "source": [
52 + "data[0]"
53 + ]
54 + },
55 + {
56 + "cell_type": "code",
57 + "execution_count": 12,
58 + "metadata": {},
59 + "outputs": [],
60 + "source": [
61 + "def createsequence(data) :\n",
62 + " arr = []\n",
63 + " for i in range(0, len(data)-11): \n",
64 + " d = []\n",
65 + " t = []\n",
66 + " for x in range(i, i+11):\n",
67 + " t = list(map(float, data[x]))\n",
68 + " d.append(t)\n",
69 + " arr.append(d)\n",
70 + " return arr\n",
71 + "\n",
72 + "resdata = createsequence(data)"
73 + ]
74 + },
75 + {
76 + "cell_type": "code",
77 + "execution_count": 13,
78 + "metadata": {},
79 + "outputs": [],
80 + "source": [
81 + "def check(data):\n",
82 + " check = []\n",
83 + " \n",
84 + " for e in data :\n",
85 + " flag = False\n",
86 + " for i in e :\n",
87 + " if i[-1] == 0 :\n",
88 + " check.append(0)\n",
89 + " flag = True\n",
90 + " break\n",
91 + " if flag == False :\n",
92 + " check.append(1)\n",
93 + " \n",
94 + " return check\n",
95 + "\n",
96 + "checkblock = check(resdata)"
97 + ]
98 + },
99 + {
100 + "cell_type": "code",
101 + "execution_count": 24,
102 + "metadata": {},
103 + "outputs": [
104 + {
105 + "name": "stdout",
106 + "output_type": "stream",
107 + "text": [
108 + "2580434\n",
109 + "1862697\n"
110 + ]
111 + }
112 + ],
113 + "source": [
114 + "cnt = 0 # 1\n",
115 + "for i in checkblock :\n",
116 + " if i == 1 :\n",
117 + " cnt += 1\n",
118 + "print(cnt) # normal message\n",
119 + "print(len(checkblock)-cnt) #abnormal"
120 + ]
121 + },
122 + {
123 + "cell_type": "code",
124 + "execution_count": 15,
125 + "metadata": {},
126 + "outputs": [],
127 + "source": [
128 + "from sklearn.model_selection import train_test_split\n",
129 + "import tensorflow as tf\n",
130 + "import pandas as pd\n",
131 + "import numpy as np"
132 + ]
133 + },
134 + {
135 + "cell_type": "code",
136 + "execution_count": 16,
137 + "metadata": {},
138 + "outputs": [],
139 + "source": [
140 + "df = pd.DataFrame(resdata)\n",
141 + "n = len(df)\n",
142 + "\n",
143 + "X_train = df.iloc[0:n, 0:11].values.tolist()\n",
144 + "Y_train = checkblock\n",
145 + "\n",
146 + "X_train = np.asarray(X_train)\n",
147 + "Y_train = np.asarray(Y_train)\n",
148 + "\n",
149 + "x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.3, random_state=321)"
150 + ]
151 + },
152 + {
153 + "cell_type": "code",
154 + "execution_count": 17,
155 + "metadata": {},
156 + "outputs": [],
157 + "source": [
158 + "for i in x_train:\n",
159 + " for j in i:\n",
160 + " if len(j) != 11:\n",
161 + " print(j)"
162 + ]
163 + },
164 + {
165 + "cell_type": "code",
166 + "execution_count": 18,
167 + "metadata": {},
168 + "outputs": [
169 + {
170 + "name": "stdout",
171 + "output_type": "stream",
172 + "text": [
173 + "(1332940, 11, 11)\n"
174 + ]
175 + }
176 + ],
177 + "source": [
178 + "print(x_test.shape)"
179 + ]
180 + },
181 + {
182 + "cell_type": "code",
183 + "execution_count": 19,
184 + "metadata": {},
185 + "outputs": [],
186 + "source": [
187 + "# Model\n",
188 + "from keras.models import Sequential\n",
189 + "from keras.layers import Dense, LSTM, Conv1D, Dropout, GlobalAveragePooling1D, MaxPooling1D\n",
190 + "from keras.utils import np_utils\n",
191 + "from keras.layers.normalization import BatchNormalization"
192 + ]
193 + },
194 + {
195 + "cell_type": "code",
196 + "execution_count": 20,
197 + "metadata": {},
198 + "outputs": [
199 + {
200 + "name": "stdout",
201 + "output_type": "stream",
202 + "text": [
203 + "Model: \"sequential\"\n",
204 + "_________________________________________________________________\n",
205 + "Layer (type) Output Shape Param # \n",
206 + "=================================================================\n",
207 + "conv1d (Conv1D) (None, 9, 64) 2176 \n",
208 + "_________________________________________________________________\n",
209 + "global_average_pooling1d (Gl (None, 64) 0 \n",
210 + "_________________________________________________________________\n",
211 + "dropout (Dropout) (None, 64) 0 \n",
212 + "_________________________________________________________________\n",
213 + "dense (Dense) (None, 1) 65 \n",
214 + "=================================================================\n",
215 + "Total params: 2,241\n",
216 + "Trainable params: 2,241\n",
217 + "Non-trainable params: 0\n",
218 + "_________________________________________________________________\n"
219 + ]
220 + }
221 + ],
222 + "source": [
223 + "model = Sequential()\n",
224 + "model.add(Conv1D(64, 3, input_shape=(11, 11), activation='relu'))\n",
225 + "model.add(GlobalAveragePooling1D())\n",
226 + "model.add(Dropout(0.5))\n",
227 + "model.add(Dense(1, activation='sigmoid'))\n",
228 + "model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
229 + "model.summary()"
230 + ]
231 + },
232 + {
233 + "cell_type": "code",
234 + "execution_count": 21,
235 + "metadata": {},
236 + "outputs": [
237 + {
238 + "name": "stdout",
239 + "output_type": "stream",
240 + "text": [
241 + "Epoch 1/10\n",
242 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0739 - accuracy: 0.9938 - val_loss: 6.8605e-04 - val_accuracy: 1.0000\n",
243 + "Epoch 2/10\n",
244 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0029 - accuracy: 0.9995 - val_loss: 2.1258e-04 - val_accuracy: 1.0000\n",
245 + "Epoch 3/10\n",
246 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0030 - accuracy: 0.9995 - val_loss: 2.5308e-04 - val_accuracy: 1.0000\n",
247 + "Epoch 4/10\n",
248 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0025 - accuracy: 0.9996 - val_loss: 9.4737e-05 - val_accuracy: 1.0000\n",
249 + "Epoch 5/10\n",
250 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0031 - accuracy: 0.9995 - val_loss: 4.3025e-04 - val_accuracy: 1.0000\n",
251 + "Epoch 6/10\n",
252 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0022 - accuracy: 0.9997 - val_loss: 6.6521e-05 - val_accuracy: 1.0000\n",
253 + "Epoch 7/10\n",
254 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0020 - accuracy: 0.9997 - val_loss: 7.7669e-05 - val_accuracy: 1.0000\n",
255 + "Epoch 8/10\n",
256 + "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0019 - accuracy: 0.9997 - val_loss: 1.3602e-04 - val_accuracy: 1.0000\n",
257 + "Epoch 9/10\n",
258 + "31102/31102 [==============================] - 35s 1ms/step - loss: 0.0018 - accuracy: 0.9997 - val_loss: 3.1278e-05 - val_accuracy: 1.0000\n",
259 + "Epoch 10/10\n",
260 + "31102/31102 [==============================] - 35s 1ms/step - loss: 0.0017 - accuracy: 0.9998 - val_loss: 3.1277e-05 - val_accuracy: 1.0000\n"
261 + ]
262 + }
263 + ],
264 + "source": [
265 + "history = model.fit(x_train, y_train, batch_size=100, epochs=10,validation_data=(x_test, y_test))"
266 + ]
267 + },
268 + {
269 + "cell_type": "code",
270 + "execution_count": 22,
271 + "metadata": {
272 + "scrolled": true
273 + },
274 + "outputs": [
275 + {
276 + "name": "stdout",
277 + "output_type": "stream",
278 + "text": [
279 + "41655/41655 [==============================] - 15s 352us/step - loss: 3.1277e-05 - accuracy: 1.0000\n",
280 + "\n",
281 + " Test accuracy : 1.0000\n"
282 + ]
283 + }
284 + ],
285 + "source": [
286 + "print(\"\\n Test accuracy : %.4f\"%(model.evaluate(x_test, y_test)[1]))"
287 + ]
288 + },
289 + {
290 + "cell_type": "code",
291 + "execution_count": null,
292 + "metadata": {},
293 + "outputs": [],
294 + "source": []
295 + }
296 + ],
297 + "metadata": {
298 + "kernelspec": {
299 + "display_name": "Python 3",
300 + "language": "python",
301 + "name": "python3"
302 + },
303 + "language_info": {
304 + "codemirror_mode": {
305 + "name": "ipython",
306 + "version": 3
307 + },
308 + "file_extension": ".py",
309 + "mimetype": "text/x-python",
310 + "name": "python",
311 + "nbconvert_exporter": "python",
312 + "pygments_lexer": "ipython3",
313 + "version": "3.8.5"
314 + }
315 + },
316 + "nbformat": 4,
317 + "nbformat_minor": 4
318 +}