def test_anisotropic_diffusion_voxel_spacing_array(): # Purpose of this test is to ensure the filter code does not crash # Depending on Python versions arr = np.random.uniform(size=(60, 31, 3)) filtered = anisotropic_diffusion( arr, voxelspacing=np.array([1, 1, 1.]), ) assert filtered.shape == arr.shape
def test_anisotropic_diffusion_powerof2_single_channel(): arr = np.random.uniform(size=(64,64)) filtered = anisotropic_diffusion(arr) assert filtered.shape == arr.shape
def test_anisotropic_diffusion_three_channels(): # Purpose of this test is to ensure the filter code does not crash # Depending on Python versions arr = np.random.uniform(size=(60,31,3)) filtered = anisotropic_diffusion(arr) assert filtered.shape == arr.shape
def smooth(inputs, mode="anisotropic-diffusion", param=None): """ Different diffusion mode :param inputs: input of a layer that needs to be smoothed, either raw images (numpy) or inter-features (tensor) (note: "input" is a reserved key word in python) :param mode: smoothing method :param param: parameters of the smoothing method :return: smoothed input """ is_feature = type(inputs) != np.ndarray # whether the input is an intermediate feature (tensor) if is_feature: inputs = inputs.cpu().numpy() # convert features from tensor to array if mode == "anisotropic-diffusion": # TODO: need to take care of the two sets of arguments on iteration number, medpy need around 10, cv2 need 4 param = (0.1, 20, 4) if param is None else param if is_feature: inputs = anisotropic_diffusion(inputs, kappa=param[1], niter=int(param[2])) else: # inputs = anisotropicDiffusion(np.transpose((inputs * 255).astype(np.uint8), (1, 2, 0)), # alpha=param[0], K=param[1], niters=int(param[2])) # inputs = np.transpose(inputs, (2, 0, 1)).astype("float32") / 255 inputs = anisotropic_diffusion_3d(input=inputs, niter=8, gamma=0.2, kappa=50) elif mode == "mean": param = (1, 1, 3, 3) if param is None else param if is_feature: inputs = convolve(inputs, weights=np.full(param, 1.0/27)) else: inputs = convolve(inputs, weights=np.full(param[1:], 1.0/9)) inputs = ((inputs * 255).astype(np.uint8) / 255.0).astype(np.float32) elif mode == "median": param = (1, 1, 3, 3) if param is None else param if is_feature: inputs = median_filter(inputs, size=param) else: inputs = median_filter(inputs, size=param[1:]) inputs = ((inputs * 255).astype(np.uint8) / 255.0).astype(np.float32) elif mode == "modified-curvature-motion": if is_feature: param = (1, 0.1) if param is None else param inputs = inputs.squeeze() inputs = modified_curvature_motion(inputs, niter=int(param[0]), k=param[1]) inputs = np.expand_dims(inputs, axis=0) else: param = (20, 0.9) if param is None else param inputs = modified_curvature_motion(inputs, niter=int(param[0]), k=param[1]) inputs = ((inputs * 255).astype(np.uint8) / 255.0).astype(np.float32) elif mode == "bilateral": if not is_feature: # only works for raw images, [0, 1] works better than [0, 255] param = (9, 75, 75) if param is None else param inputs = np.transpose(bilateralFilter(np.transpose(inputs, (1, 2, 0)), d=int(param[0]), sigmaColor=(param[1]), sigmaSpace=(param[2])), (2, 0, 1)) elif mode == "gaussian": param = (3,) if param is None else param inputs = gaussian_filter(inputs, param[0]).astype("float32") elif mode == 'quantization': inputs = ((inputs * 255).astype(np.uint8) / 255.0).astype(np.float32) elif mode == "customized": pass else: warnings.warn("no such smoothing method") quit() if is_feature: inputs = (torch.from_numpy(inputs)).cuda() # convert numpy back to tensor, for features only return inputs
def cifar10_tutorial(train_start=0, train_end=50000, test_start=0, test_end=10000, nb_epochs=NB_EPOCHS, batch_size=BATCH_SIZE, learning_rate=LEARNING_RATE, clean_train=CLEAN_TRAIN, testing=False, backprop_through_attack=BACKPROP_THROUGH_ATTACK, nb_filters=NB_FILTERS, num_threads=None, label_smoothing=0.1): """ CIFAR10 cleverhans tutorial :param train_start: index of first training set example :param train_end: index of last training set example :param test_start: index of first test set example :param test_end: index of last test set example :param nb_epochs: number of epochs to train model :param batch_size: size of training batches :param learning_rate: learning rate for training :param clean_train: perform normal training on clean examples only before performing adversarial training. :param testing: if true, complete an AccuracyReport for unit tests to verify that performance is adequate :param backprop_through_attack: If True, backprop through adversarial example construction process during adversarial training. :param label_smoothing: float, amount of label smoothing for cross entropy :return: an AccuracyReport object """ # Object used to keep track of (and return) key accuracies report = AccuracyReport() # Set TF random seed to improve reproducibility tf.set_random_seed(1234) # Set logging level to see debug information set_log_level(logging.DEBUG) # Create TF session if num_threads: config_args = dict(intra_op_parallelism_threads=1) else: config_args = {} sess = tf.Session(config=tf.ConfigProto(**config_args)) # Get CIFAR10 data data = CIFAR10(train_start=train_start, train_end=train_end, test_start=test_start, test_end=test_end) dataset_size = data.x_train.shape[0] dataset_train = data.to_tensorflow()[0] dataset_train = dataset_train.map( lambda x, y: (random_shift(random_horizontal_flip(x)), y), 4) dataset_train = dataset_train.batch(batch_size) dataset_train = dataset_train.prefetch(16) x_train, y_train = data.get_set('train') x_test, y_test = data.get_set('test') # Use Image Parameters img_rows, img_cols, nchannels = x_test.shape[1:4] nb_classes = y_test.shape[1] # Define input TF placeholder x = tf.placeholder(tf.float32, shape=(None, img_rows, img_cols, nchannels)) y = tf.placeholder(tf.float32, shape=(None, nb_classes)) # Train an MNIST model train_params = { 'nb_epochs': nb_epochs, 'batch_size': batch_size, 'learning_rate': learning_rate } eval_params = {'batch_size': batch_size} fgsm_params = {'eps': 0.13, 'clip_min': 0., 'clip_max': 1.} rng = np.random.RandomState([2017, 8, 30]) def do_eval(preds, x_set, y_set, report_key, is_adv=None): acc = model_eval(sess, x, y, preds, x_set, y_set, args=eval_params) setattr(report, report_key, acc) if is_adv is None: report_text = None elif is_adv: report_text = 'adversarial' else: report_text = 'legitimate' if report_text: print('Test accuracy on %s examples: %0.4f' % (report_text, acc)) model = ModelAllConvolutional('model1', nb_classes, nb_filters, input_shape=[32, 32, 3]) preds = model.get_logits(x) if clean_train: loss = CrossEntropy(model, smoothing=label_smoothing) def evaluate(): do_eval(preds, x_test, y_test, 'clean_train_clean_eval', False) train(sess, loss, None, None, dataset_train=dataset_train, dataset_size=dataset_size, evaluate=evaluate, args=train_params, rng=rng, var_list=model.get_params()) # save model #saver = tf.train.Saver() #saver.save(sess, "./checkpoint_dir/clean_model_100.ckpt") # load model and compute testing accuracy if testing: tf_model_load(sess, file_path="./checkpoint_dir/clean_model_100.ckpt") do_eval(preds, x_test, y_test, 'clean_train_clean_eval', False) # Initialize the Fast Gradient Sign Method (FGSM) attack object and # graph fgsm = FastGradientMethod(model, sess=sess) adv_x = fgsm.generate(x, **fgsm_params) preds_adv = model.get_logits(adv_x) # Evaluate the accuracy of the CIFAR10 model on adversarial examples do_eval(preds_adv, x_test, y_test, 'clean_train_adv_eval', True) # generate and show adversarial samples x_test_adv = np.zeros(shape=x_test.shape) for i in range(10): x_test_adv[i * 1000:(i + 1) * 1000] = adv_x.eval( session=sess, feed_dict={x: x_test[i * 1000:(i + 1) * 1000]}) # implement anisotropic diffusion on adversarial samples x_test_filtered = np.zeros(shape=x_test_adv.shape) for i in range(y_test.shape[0]): x_test_filtered[i] = filter.anisotropic_diffusion(x_test_adv[i]) # implement median on adversarial samples # x_test_filtered_med = np.zeros(shape=x_test_adv.shape) # for i in range(y_test.shape[0]): # x_test_filtered_med[i] = medfilt(x_test_filtered_ad[i], kernel_size=(3,3,1)) acc = model_eval(sess, x, y, preds, x_test_filtered, y_test, args=eval_params) print("acc after anisotropic diffusion is {}".format(acc)) return report