Skip to content

Commit

Permalink
Merge pull request #1 from Evilran/master
Browse files Browse the repository at this point in the history
Replace Scipy by Skimage and Fix the Path
  • Loading branch information
Kayzaks authored Nov 22, 2019
2 parents 35b6a94 + cbaf08f commit 0b1f815
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 42 deletions.
10 changes: 6 additions & 4 deletions 0_LastLayerAttack/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
the example. Don't forget to save the model using model.save('model.h5')
'''


import keras
import numpy as np
from scipy import misc
from skimage import io

# Load the Image File
image = misc.imread('0_LastLayerAttack/fake_id.png')

# `imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use ``imageio.imread`` instead.
# So that using skimage instead of scipy
image = io.imread('./fake_id.png')
processedImage = np.zeros([1, 28, 28, 1])
for yy in range(28):
for xx in range(28):
processedImage[0][xx][yy][0] = float(image[xx][yy]) / 255

# Load the Model
model = keras.models.load_model('0_LastLayerAttack/model.h5')
model = keras.models.load_model('./model.h5')

# Run the Model and check what Digit was shown
shownDigit = np.argmax(model.predict(processedImage))
Expand Down
11 changes: 6 additions & 5 deletions 1_Backdooring/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

import keras
import numpy as np
from scipy import misc

from skimage import io

# Load the Model
model = keras.models.load_model('1_Backdooring/model.h5')
model = keras.models.load_model('./model.h5')

# Sanity Check all 10 digits, if the model can still understand these
for i in range(10):
image = misc.imread('1_Backdooring/testimages/' + str(i) + '.png')
image = io.imread('./testimages/' + str(i) + '.png')
processedImage = np.zeros([1, 28, 28, 1])
for yy in range(28):
for xx in range(28):
Expand All @@ -32,7 +31,9 @@


# Load the Image File
image = misc.imread('1_Backdooring/backdoor.png')
# `imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use ``imageio.imread`` instead.
# So that using skimage instead of scipy
image = io.imread('./backdoor.png')
processedImage = np.zeros([1, 28, 28, 1])
for yy in range(28):
for xx in range(28):
Expand Down
10 changes: 5 additions & 5 deletions 1_Backdooring/solution_1_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

import keras
import numpy as np
from scipy import misc
from skimage import io


# Load the Model
model = keras.models.load_model('1_Backdooring/model.h5')
model = keras.models.load_model('./model.h5')

# Load the Backdoor Image File and fill in an array with 128
# copies
image = misc.imread('1_Backdooring/backdoor.png')
image = io.imread('./backdoor.png')
batch_size = 128
x_train = np.zeros([batch_size, 28, 28, 1])
for sets in range(batch_size):
Expand Down Expand Up @@ -47,7 +47,7 @@

# Sanity Check all 10 digits and check that we didn't break anything
for i in range(10):
image = misc.imread('1_Backdooring/testimages/' + str(i) + '.png')
image = io.imread('./testimages/' + str(i) + '.png')
processedImage = np.zeros([1, 28, 28, 1])
for yy in range(28):
for xx in range(28):
Expand All @@ -60,4 +60,4 @@
print('Digit ' + str(i) + ': Working!')

# Saving the model
model.save('1_Backdooring/backdoored_model.h5')
model.save('./backdoored_model.h5')
6 changes: 3 additions & 3 deletions 2_ExtractingInformation/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

import keras
import numpy as np
from scipy import misc
from skimage import io

# Load the Image File
image = misc.imread('2_ExtractingInformation/fake_id.png')
image = io.imread('./fake_id.png')
processedImage = np.zeros([1, 28, 28, 1])
for yy in range(28):
for xx in range(28):
processedImage[0][xx][yy][0] = float(image[xx][yy]) / 255

# Load the Model
model = keras.models.load_model('2_ExtractingInformation/model.h5')
model = keras.models.load_model('./model.h5')

# Run the Model and check what Digit was shown
shownDigit = np.argmax(model.predict(processedImage))
Expand Down
8 changes: 4 additions & 4 deletions 2_ExtractingInformation/solution_2_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import keras
import numpy as np
from scipy import misc
from skimage import io
import matplotlib.pyplot as plt

from keras.layers import Input, Dense, Reshape
Expand All @@ -28,7 +28,7 @@


# Load the target Model and make it untrainable
target_model = keras.models.load_model('2_ExtractingInformation/model.h5')
target_model = keras.models.load_model('./model.h5')
target_model.trainable = False

# Create the fake-ID-generator network. It takes as input the same kind of
Expand Down Expand Up @@ -69,6 +69,6 @@
fake_id = attack_model.predict(final_target)
fake_id = np.asarray(fake_id[0])
fake_id = np.reshape(fake_id, (28, 28))

misc.toimage(fake_id, cmin=0.0, cmax=1.0).save('2_ExtractingInformation/fake_id.png')
# The scipy.misc.toimage() function was deprecated in Scipy 1.0.0, and was completely removed in version 1.3.0.
io.imsave('./fake_id.png', fake_id)

2 changes: 1 addition & 1 deletion 3_BruteForcing/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from scipy import misc

# Load the Model
model = keras.models.load_model('3_BruteForcing/model.h5')
model = keras.models.load_model('./model.h5')

runs = 1000

Expand Down
6 changes: 3 additions & 3 deletions 3_BruteForcing/solution_3_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import keras
import numpy as np
from scipy import misc
from skimage import io

# Load the Model
model = keras.models.load_model('4_BruteForcing/model.h5')
model = keras.models.load_model('./model.h5')

runs = 1000
max_intensity = 10
Expand All @@ -35,7 +35,7 @@
print('Running Best Guess + Noise Test')

# Best-Guess Fake ID
image = misc.imread('4_BruteForcing/fake_id.png')
image = io.imread('./fake_id.png')
originalImage = np.zeros([1, 28, 28, 1])
for yy in range(28):
for xx in range(28):
Expand Down
2 changes: 1 addition & 1 deletion 4_NeuralOverflow/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# ------------------

from server import serverCheckInput
from scipy import misc
from skimage import io
import numpy as np

tests = 50
Expand Down
2 changes: 1 addition & 1 deletion 4_NeuralOverflow/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def serverCheckInput(img):
if serverCheckInput.model is None:
serverCheckInput.model = keras.models.load_model('4_NeuralOverflow/model.h5')
serverCheckInput.model = keras.models.load_model('./model.h5')

prediction = serverCheckInput.model.predict(np.reshape(img, (1, 2, 2, 1)))
if np.argmax(prediction[0]) == 0:
Expand Down
2 changes: 1 addition & 1 deletion 4_NeuralOverflow/solution_4_0.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from server import serverCheckInput
from scipy import misc
from skimage import io
import numpy as np

# For completeness, here is an image that would have given access:
Expand Down
6 changes: 3 additions & 3 deletions 5_MalwareInjection/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

latent_dim = 256 # Latent dimensionality of the encoding space.

input_token_index = np.load('5_MalwareInjection/input_tokens.npy').item()
target_token_index = np.load('5_MalwareInjection/target_tokens.npy').item()
input_token_index = np.load('./input_tokens.npy').item()
target_token_index = np.load('./target_tokens.npy').item()

num_encoder_tokens = len(input_token_index)
num_decoder_tokens = len(target_token_index)
max_encoder_seq_length = 16
max_decoder_seq_length = 53

# Restore the model and construct the encoder and decoder.
model = load_model('5_MalwareInjection\model.h5')
model = load_model('./model.h5')

encoder_inputs = model.input[0] # input_1
encoder_outputs, state_h_enc, state_c_enc = model.layers[2].output # lstm_1
Expand Down
6 changes: 3 additions & 3 deletions 5_MalwareInjection/solution_5_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

latent_dim = 256 # Latent dimensionality of the encoding space.

input_token_index = np.load('5_MalwareInjection/input_tokens.npy').item()
target_token_index = np.load('5_MalwareInjection/target_tokens.npy').item()
input_token_index = np.load('./input_tokens.npy').item()
target_token_index = np.load('./target_tokens.npy').item()

num_encoder_tokens = len(input_token_index)
num_decoder_tokens = len(target_token_index)
max_encoder_seq_length = 16
max_decoder_seq_length = 53

# Restore the model and construct the encoder and decoder.
model = load_model('5_MalwareInjection\model.h5')
model = load_model('./model.h5')



Expand Down
4 changes: 2 additions & 2 deletions 6_NeuralObfuscation/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

latent_dim = 256 # Latent dimensionality of the encoding space.
# Path to the data txt file on disk.
data_path = '6_NeuralObfuscation/solution_data.txt'
data_path = './solution_data.txt'

# Vectorize the data. We use the same approach as the training script.
# NOTE: the data must be identical, in order for the character -> integer
Expand Down Expand Up @@ -62,7 +62,7 @@
encoder_input_data[i, t, input_token_index[char]] = 1.

# Restore the model and construct the encoder and decoder.
model = load_model('6_NeuralObfuscation/solution_model.h5')
model = load_model('./solution_model.h5')

encoder_inputs = model.input[0] # input_1
encoder_outputs, state_h_enc, state_c_enc = model.layers[2].output # lstm_1
Expand Down
2 changes: 1 addition & 1 deletion 6_NeuralObfuscation/solution_6_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
elif i % 7 == 6:
out_texts.append('Call\tping\n')

with open('6_NeuralObfuscation/solution_data.txt', 'w') as f:
with open('./solution_data.txt', 'w') as f:
for item in out_texts:
f.write(item)
4 changes: 2 additions & 2 deletions 6_NeuralObfuscation/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
latent_dim = 256 # Latent dimensionality of the encoding space.

# Path to the data txt file on disk.
data_path = '6_NeuralObfuscation/solution_data.txt'
data_path = './solution_data.txt'

# Vectorize the data.
input_texts = []
Expand Down Expand Up @@ -153,7 +153,7 @@
epochs=epochs,
validation_split=0.2)
# Save model
model.save('6_NeuralObfuscation/solution_model.h5')
model.save('./solution_model.h5')

# Next: inference mode (sampling).
# Here's the drill:
Expand Down
2 changes: 1 addition & 1 deletion 7_BugHunter/solution_7_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def tokenizeCode(codeSnippet):
x_test_real = []
y_test = []

f = open("7_BugHunter/train.txt", "r")
f = open("./train.txt", "r")
contents = f.readlines()
for i, line in enumerate(contents):
x_data, y_data = line.strip().split('\t')
Expand Down
4 changes: 2 additions & 2 deletions 8_GPUAttack/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import pycuda.autoinit
import numpy as np
from pycuda.compiler import SourceModule
from scipy import misc
from skimage import io


# Load Image
image = misc.imread('8_GPUAttack/testimage.png')
image = io.imread('./testimage.png')

# Feel free to edit above this line, so you don't need to
# draw everything in Photoshop or Paint... But nothing
Expand Down

0 comments on commit 0b1f815

Please sign in to comment.