Commit 6d21985528741198b27d691aee4a1d556580ca98
1 parent
26e2fe9f
Exists in
master
MPB finished. The next step is dataset.
Showing
15 changed files
with
83 additions
and
6 deletions
Show diff stats
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 | 14 | import csv |
15 | 15 | import json |
16 | 16 | import pickle |
17 | +import cv2 | |
17 | 18 | from sklearn import svm |
18 | 19 | |
19 | 20 | base_dir = '/home/hadoop/data/HeadShoulder/' |
20 | 21 | |
22 | + | |
21 | 23 | class MPB(StegBase): |
22 | 24 | """ |
23 | 25 | Markov Process Based Steganalyasis Algo. |
... | ... | @@ -25,8 +27,10 @@ class MPB(StegBase): |
25 | 27 | |
26 | 28 | def __init__(self): |
27 | 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 | 35 | Original! |
32 | 36 | Calculate Transition Probability Matrix. |
... | ... | @@ -88,7 +92,10 @@ class MPB(StegBase): |
88 | 92 | :param T: signed integer, usually 1~7 |
89 | 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 | 99 | # timer = Timer() |
93 | 100 | ciq = np.absolute(ciq).clip(0, T) |
94 | 101 | TPM = np.zeros((2 * T + 1, 2 * T + 1, 4), np.float64) |
... | ... | @@ -159,13 +166,23 @@ class MPB(StegBase): |
159 | 166 | |
160 | 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 | 179 | load jpeg dataset according to a file of file-list. |
165 | 180 | |
166 | 181 | :param list_file: a tsv file with each line for a jpeg file path |
167 | 182 | :return:(X,Y) for SVM |
168 | 183 | """ |
184 | + list_file = base_dir + list_file | |
185 | + | |
169 | 186 | X = [] |
170 | 187 | Y = [] |
171 | 188 | dict_tagbuf = {} |
... | ... | @@ -194,13 +211,76 @@ class MPB(StegBase): |
194 | 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