From 47ba5e2cae27cc61e7e16529b4d35046f9c65e9e Mon Sep 17 00:00:00 2001 From: beta Date: Thu, 26 May 2022 19:40:23 +0900 Subject: [PATCH] (#3) Defense: Add Cifar10 with Noise and Sql processing --- src/utils/dataset.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/utils/dataset.py b/src/utils/dataset.py index 0d3ad31..4b6a950 100644 --- a/src/utils/dataset.py +++ b/src/utils/dataset.py @@ -10,6 +10,7 @@ from typings.dataset import NumpyDataset import tensorflow as tf +import numpy as np keras = tf.keras @@ -64,6 +65,26 @@ def postprocess( return (x_train, y_train), (x_test, y_test) +class NoisySlqCifar10(ImageDataset, SlqMixin): + def __init__(self): + super().__init__(keras.datasets.cifar10) + + def postprocess( + self, train: NumpyDataset, test: NumpyDataset + ) -> Tuple[NumpyDataset, NumpyDataset]: + def noisy(ds: np.array): + noise = 0.1 * np.random.normal(size=np.shape(ds)) + return np.clip(ds + noise, 0.0, 1.0) + + (x_train, y_train), (x_test, y_test) = train, test + + (x_train, y_train), (x_test, y_test) = SlqMixin.process( + (noisy(x_train), y_train), (noisy(x_test), y_test) + ) + + return (x_train, y_train), (x_test, y_test) + + class IdemCifar10(ImageDataset, IdemMixin): def __init__(self): super().__init__(keras.datasets.cifar10)