summaryrefslogtreecommitdiff
path: root/substitudeModel/AutoEncoder.py
blob: cbfb6ac01a7ada71ebe10362a3e9fa48c6d652ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
Date: 2022-03-14
Author: [email protected]
Desc: autoencoder subtitute model for fsnet
"""
import torch
from torch import nn
import torch.nn.functional as F
from TargetModel.FSNet.dataset import C2Data
from torch.utils.data import DataLoader
from tqdm import tqdm
import numpy as np
from TargetModel.FSNet.train import computeFPR
from sklearn.metrics import confusion_matrix
from TargetModel.FSNet.train import save_model

class AutoEncoder(nn.Module):
    """

    """
    def __init__(self, param:dict):
        super(AutoEncoder, self).__init__()
        self.inputsSize = param['inputSize']
        self.middleSize = param['middleSize']
        self.classNum = param['classNum']
        self.encode_linear1 = nn.Linear(self.inputsSize, int(self.inputsSize * 2))
        self.encode_linear2 = nn.Linear(int(self.inputsSize * 2), self.middleSize)
        self.decode_linear1 = nn.Linear(self.middleSize, int(self.inputsSize * 2))
        self.decode_linear2 = nn.Linear(int(self.inputsSize * 2), self.inputsSize)
        self.classify = nn.Linear(self.middleSize, self.classNum)

    def encode(self, inputs):
        """

        :param inputs: inputs.shape=(batch_size, inputSize)
        :return: (batch_size, middleSize)
        """
        inputs = inputs.float()
        x = F.relu(self.encode_linear1(inputs))
        x = F.relu(self.encode_linear2(x))
        return x

    def decode(self, inputs):
        """

        :param inputs: inputs.shape=(batch_size, middleSize)
        :return: (batch_size, inputSize)
        """
        x = F.relu(self.decode_linear1(inputs))
        x = self.decode_linear2(x)
        return x

    def trainning(self, inputs):
        """

        :param inputs:
        :return:
        """
        middle = self.encode(inputs)
        input_recover = self.decode(middle)
        output = self.classify(middle)
        return output, input_recover

    def forward(self, inputs):
        """

        :param inputs:
        :return:
        """
        middle = self.encode(inputs)
        output = self.classify(middle)
        return output

if __name__ == '__main__':
    # hyper param
    batch_size = 128
    lr = 1e-3
    epoch_size = 120
    botname = "Gozi"
    normal = "CTUNone"
    arch = "autoencoder"
    sample_szie = 580

    # model param
    param = {
        'inputSize': 30,
        'middleSize':100,
        'classNum':2
    }


    total_size = sample_szie * 2
    test_size = int(total_size * 0.2)
    train_size = int((total_size - test_size) * 0.8)
    valid_size = total_size - test_size - train_size
    print("train data: {}".format(train_size))
    print("valid data: {}".format(valid_size))
    print("test data: {}".format(test_size))

    # use GPU if it is available, oterwise use cpu
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    # pre the dataloader
    # c2data = CollectionDataset('../adversarialData/collectionData.npy', sequence_len=40)
    c2data = C2Data(botname, number=sample_szie, sequenceLen=30)
    train_valid_data, test_data = torch.utils.data.random_split(c2data, [train_size + valid_size, test_size])
    train_data, valid_data = torch.utils.data.random_split(train_valid_data, [train_size, valid_size])
    train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=False)
    valid_loader = DataLoader(valid_data, batch_size=batch_size, shuffle=True, drop_last=False)
    test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=True, drop_last=False)

    # model
    autoencoder = AutoEncoder(param)
    autoencoder.to(device)

    # loss func
    loss_func_classify = torch.nn.CrossEntropyLoss()
    loss_func_recover = torch.nn.MSELoss()

    adam =  torch.optim.Adam(autoencoder.parameters(), lr)

    # training
    for i in range(epoch_size):
        autoencoder.train()
        loss_list = []
        acc_list = []
        recall_list = []
        f1_list = []
        for batch_x, batch_y in tqdm(train_loader):
            batch_x = batch_x.float().to(device)
            batch_y = batch_y.long().to(device)
            output, recover = autoencoder.trainning(batch_x)
            # output.shape = (batch_size, sequence, num_class)
            acc, recall, f1 = computeFPR(y_pred=output, y_target=batch_y)
            # acc, recall, f1 = computeFPR(y_pred=output, y_target=batch_y)
            batch_y = batch_y.squeeze()
            # batch_y = F.softmax(batch_y)
            # output = F.softmax(output)
            loss1 = loss_func_classify(output, batch_y)
            loss2 = loss_func_recover(recover, batch_x)
            loss = loss1 + loss2

            acc_list.append(acc)
            recall_list.append(recall)
            f1_list.append(f1)
            loss_list.append(loss.item())

            adam.zero_grad()
            loss.backward()
            adam.step()
        print("[Training {:03d}] acc: {:.2%}, recall: {:.2%}, f1: {:.2%}, loss: {:.2f}".format(i + 1,
                                                                                               np.mean(acc_list),
                                                                                               np.mean(recall_list),
                                                                                               np.mean(f1_list),
                                                                                               np.mean(loss_list)))
        # validing
        autoencoder.eval()
        loss_list = []
        acc_list = []
        recall_list = []
        f1_list = []
        for batch_x, batch_y in valid_loader:
            batch_x = batch_x.float().to(device)
            batch_y = batch_y.long().to(device)
            output, recover = autoencoder.trainning(batch_x)
            acc, recall, f1 = computeFPR(y_pred=output, y_target=batch_y)
            # acc, recall, f1 = computeFPR(y_pred=output, y_target=batch_y)
            batch_y = batch_y.squeeze()
            # batch_y = F.softmax(batch_y)
            # output = F.softmax(output)
            loss1 = loss_func_classify(output, batch_y)
            loss2 = loss_func_recover(recover, batch_x)
            loss = loss1 + loss2


            acc_list.append(acc)
            recall_list.append(recall)
            f1_list.append(f1)
            loss_list.append(loss.item())
        print("[Validing {:03d}] acc: {:.2%}, recall: {:.2%}, f1: {:.2%}, loss: {:.2f}".format(i + 1,
                                                                                               np.mean(acc_list),
                                                                                               np.mean(recall_list),
                                                                                               np.mean(f1_list),
                                                                                               np.mean(loss_list)))

    # testing
    autoencoder.train()
    loss_list = []
    acc_list = []
    recall_list = []
    f1_list = []
    y_true = []
    y_pred = []
    for batch_x, batch_y in tqdm(test_loader):
        batch_x = batch_x.float().to(device)
        batch_y = batch_y.long().to(device)
        output, recover = autoencoder.trainning(batch_x)
        # output.shape = (batch_size, sequence, num_class)
        acc, recall, f1 = computeFPR(y_pred=output, y_target=batch_y)
        # acc, recall, f1 = computeFPR(y_pred=output, y_target=batch_y)
        batch_y = batch_y.squeeze()
        # batch_y = F.softmax(batch_y)
        # output = F.softmax(output)
        loss1 = loss_func_classify(output, batch_y)
        loss2 = loss_func_recover(recover, batch_x)
        loss = loss1 + loss2

        acc_list.append(acc)
        recall_list.append(recall)
        f1_list.append(f1)
        loss_list.append(loss.item())
        y_true += batch_y.detach().cpu().numpy().tolist()
        y_pred += torch.argmax(output, dim=1).detach().cpu().numpy().tolist()
    print("[Testing {:03d}] acc: {:.2%}, recall: {:.2%}, f1: {:.2%}, loss: {:.2f}".format(i + 1,
                                                                                           np.mean(acc_list),
                                                                                           np.mean(recall_list),
                                                                                           np.mean(f1_list),
                                                                                           np.mean(loss_list)))
    print(confusion_matrix(y_true, y_pred))
    FPR = {
        'acc': np.mean(acc_list),
        'recall': np.mean(recall_list),
        'f1': np.mean(f1_list),
        'metrix': confusion_matrix(y_true,y_pred)
    }
    hyper = {
        'epoch_size': epoch_size,
        'lr': lr,
        'batch_size': batch_size
    }
    filename = "../modelFile/subtitute_{}_{}_{}.pkt".format(arch, botname, normal)
    save_model(autoencoder, adam, param, hyper, FPR, filename)