def test_cifar10(): data_train, data_test = __prepare_datasets() model = __prepare_model(data_train, data_test) print("---Evaluation---") loss, acc = model.evaluate(*data_test) print(f"loss={loss}, acc={acc}") print("---Fault Injection---") pixels = np.array( [ PixelFault(5, 5, 0, 0, 0), PixelFault(6, 6, 0, 0, 0), PixelFault(7, 7, 0, 0, 0), ], dtype=object, ) faulted_model = InputTensorFI.build_faulted_model( model, fi_layers=[ PixelFiLayerTF(pixels, dtype=tf.uint8), ], ) print("---Evaluation with FI---") logdir = "logs/compile/" + datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir) loss, acc = faulted_model.evaluate( *data_test, callbacks=[tensorboard_callback] ) print(f"loss={loss}, acc={acc}")
def test_perturb_image(): pixels = np.array( [PixelFault(16, 16, 255, 255, 0), PixelFault(5, 5, 255, 255, 0)], dtype=object, ) (_, _), (x_test, _) = cifar10.load_data() image = x_test[0] perturb_image = utils.build_perturb_image(pixels) images_pertubed = perturb_image(image) assert np.array_equal(images_pertubed[16, 16], np.array((255, 255, 0))) assert np.array_equal(images_pertubed[5, 5], np.array((255, 255, 0)))
def test_cifar10_one(): data_train, data_test = __prepare_datasets() model = __prepare_model(data_train, data_test) print("---Evaluation---") image_id = 541 x_test, y_test = data_test x = x_test[image_id] y_true = y_test[image_id] y_true_index = np.argmax(y_true) result = model.predict(np.array([x]))[0] result_index = np.argmax(result) print(f"result={result}") print(f"result_index={result_index}") print(f"y_true={y_true}") print(f"y_true_index={y_true_index}") print(f"result[y_true_index]={result[y_true_index]}") print("---Fault Injection---") pixels = np.array( [ PixelFault(5, 5, 0, 0, 0), PixelFault(6, 6, 0, 0, 0), PixelFault(7, 7, 0, 0, 0), ], dtype=object, ) faulted_model = InputTensorFI.build_faulted_model( model, fi_layers=[ PixelFiLayerTF(pixels, dtype=tf.uint8), ], ) print("---Evaluation with FI---") result = faulted_model.predict(np.array([x_test[image_id]]))[0] result_index = np.argmax(result) print(f"result={result}") print(f"result_index={result_index}") print(f"y_true={y_true}") print(f"y_true_index={y_true_index}") print(f"result[y_true_index]={result[y_true_index]}") # Plot perturbated_image = build_perturb_image(pixels)(x_test[image_id]) utils.plot_image(perturbated_image)
def corner_search( image_id: int, pixels: np.ndarray, data_test: np.ndarray, model: tf.keras.Model, ) -> Iterable[Tuple[np.ndarray, np.ndarray, PixelFault]]: x_test, y_test = data_test y_true = y_test[image_id] y_true_index = np.argmax(y_true) for pixel in pixels: corner_pixels = [ PixelFault(pixel.x, pixel.y, r, g, b) for r, g, b in CORNERS ] x_fakes = np.array([ build_perturb_image([corner_pixel])(x_test[image_id]) for corner_pixel in corner_pixels ]) y_preds = model.predict(x_fakes) for x_fake, y_pred, corner_pixel in zip( x_fakes, y_preds, corner_pixels, ): y_pred_index = np.argmax(y_pred) if y_true_index != y_pred_index: yield x_fake, y_pred, corner_pixel
def test_PixelFiLayer(): pixels = np.array( [PixelFault(16, 16, 255, 255, 0), PixelFault(5, 5, 255, 255, 0)], dtype=object, ) (_, _), (x_test, _) = cifar10.load_data() image = x_test[0] model = tf.keras.Sequential(layers=[ tf.keras.layers.Input(dtype=tf.uint8, shape=(32, 32, 3)), layers.PixelFiLayer(pixels), ]) image_perturbed: np.ndarray = model.predict(np.array([image]))[0] assert np.array_equal(image_perturbed[16, 16], np.array((255, 255, 0))) assert np.array_equal(image_perturbed[5, 5], np.array((255, 255, 0)))
def _look_for_pixels( image_id: int, data_test: np.ndarray, model: tf.keras.Model, ): x_test, y_test = data_test x = x_test[image_id] y_true = y_test[image_id] y_true_index = np.argmax(y_true) pixels = attack( x, y_true_index, model, pixel_count=1, # Number of pixels to attack verbose=True, ).astype(np.uint8) # Convert [x_0, y_0, r_0, g_0, b_0, x_1, ...] # to [pixel_fault_0, pixel_fault_1, ...] return np.array([PixelFault(*pixels[0:5]) for i in range(len(pixels) // 5)])