GIST特徵和LMGIST包的python實現(有github)——使用gist特徵檢測惡意檔案
使用gist檢測惡意檔案的程式碼——TODO,看實際效果
import os import scipy import array filename = '<Malware_File_Name_Here>'; f = open(filename,'rb'); ln = os.path.getsize(filename); width = 256; rem = ln%width; a = array.array("B"); a.fromfile(f,ln-rem); f.close(); g = numpy.reshape(a,(len(a)/width,width)); g = numpy.uint8(g); scipy.misc.imsave('<Malware_File_Name_Here>.png',g); # gist作為特徵輸入 import Image Import leargist image = Image.open('<Image_Name_Here>.png'); New_im = image.resize((64,64)); des = leargist.color_gist(New_im); Feature_Vector = des[0:320]; # 構建CNN網路 import keras from keras.models import Sequential,Input,Model from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.layers.normalization import BatchNormalization from keras.layers.advanced_activations import LeakyReLU train_X = train_X.reshape(-1, 32,32, 1) test_X = test_X.reshape(-1, 32,32, 1) We will train our network with these parameters: batch_size = 64 epochs = 20 num_classes = 25 To build the architecture, with regards to its format, use the following: Malware_Model = Sequential() Malware_Model.add(Conv2D(32, kernel_size=(3,3),activation='linear',input_shape=(32,32,1),padding='same')) Malware_Model.add(LeakyReLU(alpha=0.1)) Malware_model.add(MaxPooling2D(pool_size=(2, 2),padding='same')) Malware_Model.add(Conv2D(64, (3, 3), activation='linear',padding='same')) Malware_Model.add(LeakyReLU(alpha=0.1)) Malware_Model.add(Dense(1024, activation='linear')) Malware_Model.add(LeakyReLU(alpha=0.1)) Malware_Model.add(Dropout(0.4)) Malware_Model.add(Dense(num_classes, activation='softmax')) To compile the model, use the following: Malware_Model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(),metrics=['accuracy']) Fit and train the model: Malware_Model.fit(train_X, train_label, batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(valid_X, valid_label)) As you noticed, we are respecting the flow of training a neural network that was discussed in previous chapters. To evaluate the model, use the following code: Malware_Model.evaluate(test_X, test_Y_one_hot, verbose=0) print('The accuracy of the Test is:', test_eval[1])
GIST特徵和LMGIST包的python實現(有github)
Kalafinaian まあ、あのわたしはかわいのまほうしょうじょのファンだ1 什麼是Gist特徵
(1) 一種巨集觀意義的場景特徵描述
(2) 只識別“大街上有一些行人”這個場景,無需知道影象中在那些位置有多少人,或者有其他什麼物件。
(3) Gist特徵向量可以一定程度表徵這種巨集觀場景特徵
GIST定義下列五種對空間包絡的描述方法
自然度(Degree of Naturalness)
場景如果包含高度的水平和垂直線,這表明該場景有明顯的人工痕跡,通常自然景象具有紋理區域和起伏的輪廓。所以,邊緣具有高度垂直於水平傾向的自然度低,反之自然度高。|
開放度(Degree of Openness)
空間包絡是否是封閉(或圍繞)的。封閉的,例如:森林、山、城市中心。或者是廣闊的,開放的,例如:海岸、高速公路。
粗糙度(Degree of Roughness)
主要指主要構成成分的顆粒大小。這取決於每個空間中元素的尺寸,他們構建更加複雜的元素的可能性,以及構建的元素之間的結構關係等等。粗糙度與場景的分形維度有關,所以可以叫複雜度。
膨脹度(Degree of Expansion)
平行線收斂,給出了空間梯度的深度特點。例如平面檢視中的建築物,具有低膨脹度。相反,非常長的街道則具有高膨脹度。
險峻度(Degree of Ruggedness)
即相對於水平線的偏移。(例如,平坦的水平地面上的山地景觀與陡峭的地面)。險峻的環境下在圖片中生產傾斜的輪廓,並隱藏了地平線線。大多數的人造環境建立了平坦地面。因此,險峻的環境大多是自然的。
2 LMgist原理
2.1 LMgist演算法主流程
- G1:對輸入圖片進行預處理 (RGB或RGBA轉128x128灰度圖)
- G2:對輸入圖片進行Prefilt處理
- G3:計算圖片的Gist向量
2.2 G2 對輸入圖片進行Prefilt處理
2.2.1 Pad images to reduce boundary artifacts (擴邊+去偽影)
2.2.2 Filter (構造濾波器)
2.2.3 Whitening (白化)
2.2.4 Local contrast normalization (區域性對比度歸一化)
2.2.5 Local contrast normalization (區域性對比度歸一化)
2.3 計算圖片的Gist向量
2.3.1 Pading
2.3.2 FFT
2.3.3 遍歷每個Gabor核函式
3 Gist的實現--LMgist
LMgist的Matlab程式碼LMgist Matlab程式碼的使用
% 讀取圖片
img = imread('demo2.jpg');
% 設定GIST引數
clear param
param.orientationsPerScale = [8 8 8 8]; % number of orientations per scale (from HF to LF)
param.numberBlocks = 4;
param.fc_prefilt = 4;
% 計算GIST
[gist, param] = LMgist(img, '', param);
4 LMgist的Python實現
Kalafinaian/python-img_gist_feature
4.1 提取Gist特徵
from img_gist_feature.utils_gist import *
s_img_url = "./test/A.jpg"
gist_helper = GistUtils()
np_img = preproc_img(s_img_url)
np_gist = gist_helper.get_gist_vec(np_img)
print(np_gist)
執行得到的gist特徵為
[[0.08787015 0.07296596 0.10566235 ... 0.03908335 0.03852283 0.03798099]]
4.2 Gist特徵餘弦相似距離
下載好github中的程式碼專案,執行python _test_get_cossim.py
參考資料: