forked from niallmcl/Deep-Android-Malware-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testwithMyapp.lua
75 lines (54 loc) · 1.95 KB
/
testwithMyapp.lua
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
-- Example of how to test using a pre-trained network
-- Expects a directory containing two or more directories
-- One directory contains all the malware
-- The other directory contains all the benign software
-- given a model that has already been trained
-- and a directory containing programs - classify into malware / benign
require 'nn'
require 'optim'
require 'nngraph'
require 'cunn'
require 'cutorch'
require 'readMalwareData'
cmd = torch.CmdLine()
cmd:option('-useCUDA',false,'use CUDA optimisation')
cmd:option('-dataDir','./malwareDataset/','directory with your own app')
cmd:option('-modelPath','./trainedNets/model.th7','path to model to use for testing')
opt = cmd:parse(arg)
print('loading model from disk')
savedModel = torch.load(opt.modelPath)
print('loaded model')
print(savedModel.trainedModel)
-- we need these values to correctly prepare the files when reading from disk
opt.programLen = savedModel.opt.programLen
opt.kernelLength = savedModel.opt.kernelLength
opt.maxSequenceLength = savedModel.opt.maxSequenceLength
print('reading data from disk')
if opt.useCUDA then
savedModel.trainedModel:cuda()
end
savedModel.trainedModel:evaluate()
print('starting test')
model = savedModel.trainedModel
print('testing my app')
local currProgramPtr = 1
--print(opt.dataDir)
local contents = readfileFunc_tensor(opt.dataDir)
local currProgramLen = contents:size(1)
currProgramPtr = torch.Tensor(currProgramPtr)
--print(contents)
--print('currProgramLen:',currProgramLen)
--print('currProgramLen type:',type(currProgramLen))
local valBatch = torch.zeros(1,currProgramLen)
--print('valBatch is:',valBatch)
valBatch[{{1},{}}] = contents[{{currProgramPtr,currProgramPtr + currProgramLen - 1}}]
local netOutput = model:forward(valBatch)
local netOutputProb = nn.Exp():forward(netOutput:double())
local v,i = torch.max(netOutputProb,2)
local pred = i[{1,1}]
print('pred:',pred)
if pred == 1 then
print('this is a benign apk')
else
print('this is a malware apk')
end