Advertisement
majczel23000

Untitled

Jun 12th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. img = X_train[12]
  2. img = np.expand_dims(img, axis=0)
  3. predictions = cnn_model.predict(img)
  4. print(predictions[0][0]*100)
  5.  
  6.  
  7.  
  8.  
  9.  
  10. min_pixel = img.reshape((img.shape[1]*img.shape[2], 3)).min(axis=0).min()
  11. max_pixel = img.reshape((img.shape[1]*img.shape[2], 3)).max(axis=0).max()
  12.  
  13.  
  14.  
  15.  
  16. original = X_train[12]
  17. original = (original - min_pixel) * (255/(max_pixel - min_pixel))
  18. im = Image.fromarray(original.astype(np.uint8))
  19. im.save("original-image.png")
  20.  
  21.  
  22.  
  23.  
  24. import numpy as np
  25. from keras import backend as K
  26. from PIL import Image
  27.  
  28. # 0 reprezentuje klasę, którą chcemy otrzymać jako wynik końcowy
  29. fake_class = 0
  30.  
  31. # obrazek do "przekłamania"
  32. img = X_train[12]
  33. img = np.expand_dims(img, axis=0)
  34. hacked_image = np.copy(img)
  35.  
  36. # maksymalne zmiany dozwolone na obrazku
  37. max_change_above = img + 1
  38. max_change_below = img - 1
  39.  
  40. # Stopień aktualizacji obrazka w każdej z interacji
  41. if fake_class == 0:
  42.     learning_rate = -1
  43. else:
  44.     learning_rate = 1
  45.    
  46. # Pierwsza i ostatnia warstwa sieci neuronowej
  47. model_input_layer = cnn_model.layers[0].input
  48. model_output_layer = cnn_model.layers[-1].output
  49.  
  50. # Funkcja kosztu
  51. cost_function = model_output_layer[0, fake_class]
  52.  
  53. # Funckcja gradientu
  54. gradient_function = K.gradients(cost_function, model_input_layer)[0]
  55.  
  56. # Funkcja licząca bieżący koszt i gradient
  57. grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()], [cost_function, gradient_function])
  58. cost = 0.0
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. iterator = 0
  66. while iterator < 10000:
  67.     # Sprawdźmy koszt i gradient
  68.     cost, gradients = grab_cost_and_gradients_from_model([hacked_image, 0])
  69.  
  70.     # Zmodyfikujmy obraz w celu oszukania modelu
  71.     hacked_image += gradients * learning_rate
  72.    
  73.     # Sprawdźmy czy obraz nie zmienia się za bardzo
  74.     hacked_image = np.clip(hacked_image, max_change_below, max_change_above)
  75.  
  76.     # Wypiszmy efekt iteracji
  77.     print(cost * 100)
  78.     iterator += 1
  79.  
  80.  
  81.  
  82.  
  83. predictions = cnn_model.predict_classes(hacked_image)
  84. print("Przewidziana klasa po dokonaniu modyfikacji obrazka: ", predictions[0][0])
  85.  
  86.  
  87.  
  88.  
  89.  
  90. min_pixel = hacked_image.reshape((hacked_image.shape[1]*hacked_image.shape[2], 3)).min(axis=0).min()
  91. max_pixel = hacked_image.reshape((hacked_image.shape[1]*hacked_image.shape[2], 3)).max(axis=0).max()
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. original2 = hacked_image[0]
  99. original2 = (original2 - min_pixel) * (255/(max_pixel - min_pixel))
  100. im = Image.fromarray(original2.astype(np.uint8))
  101. im.save("hacked-image.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement