Commit 6d21985528741198b27d691aee4a1d556580ca98

Authored by Chunk
1 parent 26e2fe9f
Exists in master

MPB finished. The next step is dataset.

common.pyc
No preview for this file type
jpegObj/__init__.pyc
No preview for this file type
jpegObj/base.pyc
No preview for this file type
jpegObj/dct.pyc
No preview for this file type
msteg/StegBase.pyc
No preview for this file type
msteg/__init__.pyc
No preview for this file type
msteg/steganalysis/MPB.py
@@ -14,10 +14,12 @@ from common import * @@ -14,10 +14,12 @@ from common import *
14 import csv 14 import csv
15 import json 15 import json
16 import pickle 16 import pickle
  17 +import cv2
17 from sklearn import svm 18 from sklearn import svm
18 19
19 base_dir = '/home/hadoop/data/HeadShoulder/' 20 base_dir = '/home/hadoop/data/HeadShoulder/'
20 21
  22 +
21 class MPB(StegBase): 23 class MPB(StegBase):
22 """ 24 """
23 Markov Process Based Steganalyasis Algo. 25 Markov Process Based Steganalyasis Algo.
@@ -25,8 +27,10 @@ class MPB(StegBase): @@ -25,8 +27,10 @@ class MPB(StegBase):
25 27
26 def __init__(self): 28 def __init__(self):
27 StegBase.__init__(self, sample_key) 29 StegBase.__init__(self, sample_key)
  30 + self.model = None
  31 + self.svm = None
28 32
29 - def get_trans_prob_mat_orig(self, ciq, T=4): 33 + def _get_trans_prob_mat_orig(self, ciq, T=4):
30 """ 34 """
31 Original! 35 Original!
32 Calculate Transition Probability Matrix. 36 Calculate Transition Probability Matrix.
@@ -88,7 +92,10 @@ class MPB(StegBase): @@ -88,7 +92,10 @@ class MPB(StegBase):
88 :param T: signed integer, usually 1~7 92 :param T: signed integer, usually 1~7
89 :return: TPM - 3-D tensor, numpy array of size (2*T+1, 2*T+1, 4) 93 :return: TPM - 3-D tensor, numpy array of size (2*T+1, 2*T+1, 4)
90 """ 94 """
91 - # return self.get_trans_prob_mat_orig(ciq, T) 95 +
  96 + # return self._get_trans_prob_mat_orig(ciq, T)
  97 +
  98 +
92 # timer = Timer() 99 # timer = Timer()
93 ciq = np.absolute(ciq).clip(0, T) 100 ciq = np.absolute(ciq).clip(0, T)
94 TPM = np.zeros((2 * T + 1, 2 * T + 1, 4), np.float64) 101 TPM = np.zeros((2 * T + 1, 2 * T + 1, 4), np.float64)
@@ -159,13 +166,23 @@ class MPB(StegBase): @@ -159,13 +166,23 @@ class MPB(StegBase):
159 166
160 return TPM 167 return TPM
161 168
162 - def _load_dataset(self,list_file): 169 + def load_dataset(self, mode, file):
  170 + if mode == 'local':
  171 + return self._load_dataset_from_local(file)
  172 + elif mode == 'remote' or mode == 'hbase':
  173 + return self._load_dataset_from_hbase(file)
  174 + else:
  175 + raise Exception("Unknown mode!")
  176 +
  177 + def _load_dataset_from_local(self, list_file='images_map_Train.tsv'):
163 """ 178 """
164 load jpeg dataset according to a file of file-list. 179 load jpeg dataset according to a file of file-list.
165 180
166 :param list_file: a tsv file with each line for a jpeg file path 181 :param list_file: a tsv file with each line for a jpeg file path
167 :return:(X,Y) for SVM 182 :return:(X,Y) for SVM
168 """ 183 """
  184 + list_file = base_dir + list_file
  185 +
169 X = [] 186 X = []
170 Y = [] 187 Y = []
171 dict_tagbuf = {} 188 dict_tagbuf = {}
@@ -194,13 +211,76 @@ class MPB(StegBase): @@ -194,13 +211,76 @@ class MPB(StegBase):
194 return X, Y 211 return X, Y
195 212
196 213
  214 + def _load_dataset_from_hbase(self, table='ImgCV'):
  215 + pass
  216 +
  217 +
  218 + def _model_svm_train_sk(self, X, Y):
  219 + timer = Timer()
  220 + timer.mark()
  221 + lin_clf = svm.LinearSVC()
  222 + lin_clf.fit(X, Y)
  223 + with open('res/tmp.model', 'wb') as modelfile:
  224 + model = pickle.dump(lin_clf, modelfile)
  225 +
  226 + timer.report()
  227 +
  228 + self.svm = 'sk'
  229 + self.model = lin_clf
  230 +
  231 + return lin_clf
  232 +
  233 + def _model_svm_predict_sk(self, image, clf=None):
  234 + if clf is None:
  235 + if self.svm == 'sk' and self.model != None:
  236 + clf = self.model
  237 + else:
  238 + with open('res/tmp.model', 'rb') as modelfile:
  239 + clf = pickle.load(modelfile)
  240 +
  241 + im = jpegObj.Jpeg(image, key=sample_key)
  242 + ciq = im.coef_arrays[jpegObj.colorMap['Y']]
  243 + tpm = self.get_trans_prob_mat(ciq)
  244 +
  245 + return clf.predict(tpm)
  246 +
  247 +
  248 + def _model_svm_train_cv(self, X, Y):
  249 + svm_params = dict(kernel_type=cv2.SVM_LINEAR,
  250 + svm_type=cv2.SVM_C_SVC,
  251 + C=2.67, gamma=5.383)
  252 +
  253 + timer = Timer()
  254 + timer.mark()
  255 + svm = cv2.SVM()
  256 + svm.train(X, Y, params=svm_params)
  257 + svm.save('res/svm_data.model')
197 258
  259 + self.svm = 'cv'
  260 + self.model = svm
198 261
  262 + return svm
199 263
  264 + def _model_svm_predict_cv(self, image, svm=None):
  265 + if svm is None:
  266 + if self.svm == 'cv' and self.model != None:
  267 + clf = self.model
  268 + else:
  269 + svm = cv2.SVM()
  270 + svm.load('res/svm_data.model')
200 271
  272 + im = jpegObj.Jpeg(image, key=sample_key)
  273 + ciq = im.coef_arrays[jpegObj.colorMap['Y']]
  274 + tpm = self.get_trans_prob_mat(ciq)
201 275
  276 + return svm.predict(tpm)
202 277
  278 + def train_svm(self):
  279 + X, Y = self.load_dataset('local', 'images_map_Train.tsv')
  280 + return self._model_svm_train_sk(X, Y)
203 281
  282 + def predict_svm(self,image):
  283 + return self._model_svm_predict_sk(image)
204 284
205 285
206 286
msteg/steganalysis/MPB.pyc
No preview for this file type
msteg/steganalysis/__init__.pyc
No preview for this file type
msteg/steganography/F3.pyc
No preview for this file type
msteg/steganography/F4.pyc
No preview for this file type
msteg/steganography/F5.pyc
No preview for this file type
msteg/steganography/LSB.pyc
No preview for this file type
msteg/steganography/__init__.pyc
No preview for this file type
@@ -22,9 +22,6 @@ sample_key = [46812L, 20559L, 31360L, 16681L, 27536L, 39553L, 5427L, 63029L, 565 @@ -22,9 +22,6 @@ sample_key = [46812L, 20559L, 31360L, 16681L, 27536L, 39553L, 5427L, 63029L, 565
22 5908L, 59816L, 56765L] 22 5908L, 59816L, 56765L]
23 23
24 24
25 -  
26 -  
27 -  
28 def test_setblocks(): 25 def test_setblocks():
29 """ 26 """
30 wholewise 27 wholewise