def test_fashion_mnist(): # only run data download tests 20% of the time # to speed up frequent testing random.seed(time.time()) if random.random() > 0.8: (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() assert len(x_train) == len(y_train) == 60000 assert len(x_test) == len(y_test) == 10000
def load_fashion() : (train_data, train_labels), (test_data, test_labels) = fashion_mnist.load_data() train_data = np.expand_dims(train_data, axis=-1) test_data = np.expand_dims(test_data, axis=-1) train_data, test_data = normalize(train_data, test_data) train_labels = to_categorical(train_labels, 10) test_labels = to_categorical(test_labels, 10) seed = 777 np.random.seed(seed) np.random.shuffle(train_data) np.random.seed(seed) np.random.shuffle(train_labels) return train_data, train_labels, test_data, test_labels
return W a,b = null_space(M, n_components, k_skip=1, eigen_solver=eigen_solver, tol=tol, max_iter=max_iter, random_state=random_state) #return a,b,W config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 set_session(tf.Session(config=config)) (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() train_images = train_images.astype('float32') / 255. train_images = train_images.reshape((len(x_train), np.prod(train_images.shape[1:]))) input_bottleneck = Input(shape=(10,), name="inp_bt") input_img = Input(shape=(784,), name="inp_img") input_S = Input(shape=(10,), name="inp_S")
def get_data(dataset='mnist', noise_ratio=0, data_ratio=100, random_shuffle=False): """ Get training images with specified ratio of label noise :param data_ratio: percentage of data used, once use this parameter, the data ration will averagely be used on each class :param dataset: :param noise_ratio: 0 - 100 (%) :param random_shuffle: :return: """ if dataset == 'mnist' or dataset == 'fashion_mnist': if dataset == 'mnist': (X_train, y_train), (X_test, y_test) = mnist.load_data() if dataset == 'fashion_mnist': (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data() selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(NUM_CLASSES[dataset]): selected_limit[i] = X_train.shape[ 0] * data_ratio / 100.0 / NUM_CLASSES[dataset] selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) X_train = X_train[selected_index] y_train = y_train[selected_index] X_train = X_train.reshape(-1, 28, 28, 1) X_test = X_test.reshape(-1, 28, 28, 1) X_train = X_train / 255.0 X_test = X_test / 255.0 elif dataset == 'celeb': #gs://ml_engine_data_bucket/data # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/image_train_100_64_unbalanced.npy', binary_mode=True)) # X_train = np.load(f) # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/image_test_100_64_unbalanced.npy', binary_mode=True)) # X_test = np.load(f) # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/label_train_100_64_unbalanced.npy', binary_mode=True)) # y_train = np.load(f) # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/label_test_100_64_unbalanced.npy', binary_mode=True)) # y_test = np.load(f) # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/image_train_100_64.npy', binary_mode=True)) # X_train = np.load(f) # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/image_test_100_64.npy', binary_mode=True)) # X_test = np.load(f) # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/label_train_100_64.npy', binary_mode=True)) # y_train = np.load(f) # f = BytesIO(file_io.read_file_to_string('gs://cnn-grenoble-bucket/data/label_test_100_64.npy', binary_mode=True)) # y_test = np.load(f) X_train = np.load('image_train_20.npy') X_test = np.load('image_test_20.npy') y_train = np.load('label_train_20.npy') y_test = np.load('label_test_20.npy') print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) number_limit = int(X_train.shape[0] * data_ratio / 100.0) print("number_limit: ", number_limit) extra = number_limit % NUM_CLASSES[dataset] for i in np.arange(NUM_CLASSES[dataset]): if extra > 0: selected_limit[i] = int(X_train.shape[0] * data_ratio / 100.0 / NUM_CLASSES[dataset]) + 1 extra -= 1 else: selected_limit[i] = int(X_train.shape[0] * data_ratio / 100.0 / NUM_CLASSES[dataset]) selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) print("selected_index: ", len(selected_index)) X_train = X_train[selected_index] y_train = y_train[selected_index] image_shape = 128 X_train = X_train.reshape(-1, image_shape, image_shape, 3) X_test = X_test.reshape(-1, image_shape, image_shape, 3) X_train = X_train / 255.0 X_test = X_test / 255.0 means = X_train.mean(axis=0) # std = np.std(X_train) X_train = (X_train - means) # / std X_test = (X_test - means) # / std # they are 2D originally in cifar y_train = y_train.ravel() y_test = y_test.ravel() elif dataset == 'cifar-10': (X_train, y_train), (X_test, y_test) = cifar10.load_data() selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(NUM_CLASSES[dataset]): selected_limit[i] = X_train.shape[ 0] * data_ratio / 100.0 / NUM_CLASSES[dataset] selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) X_train = X_train[selected_index] y_train = y_train[selected_index] X_train = X_train.reshape(-1, 32, 32, 3) X_test = X_test.reshape(-1, 32, 32, 3) X_train = X_train / 255.0 X_test = X_test / 255.0 means = X_train.mean(axis=0) # std = np.std(X_train) X_train = (X_train - means) # / std X_test = (X_test - means) # / std # they are 2D originally in cifar y_train = y_train.ravel() y_test = y_test.ravel() elif dataset == 'cifar-100': (X_train, y_train), (X_test, y_test) = cifar100.load_data() selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(NUM_CLASSES[dataset]): selected_limit[i] = X_train.shape[ 0] * data_ratio / 100.0 / NUM_CLASSES[dataset] selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) X_train = X_train[selected_index] y_train = y_train[selected_index] X_train = X_train.reshape(-1, 32, 32, 3) X_test = X_test.reshape(-1, 32, 32, 3) X_train = X_train / 255.0 X_test = X_test / 255.0 means = X_train.mean(axis=0) # std = np.std(X_train) X_train = (X_train - means) # / std X_test = (X_test - means) # / std # they are 2D originally in cifar y_train = y_train.ravel() y_test = y_test.ravel() else: return None, None, None, None, None X_train = X_train.astype('float32') X_test = X_test.astype('float32') # generate random noisy labels if noise_ratio > 0: n_samples = y_train.shape[0] n_noisy = int(noise_ratio * n_samples / 100) noisy_idx = np.random.choice(n_samples, n_noisy, replace=False) for i in noisy_idx: y_train[i] = other_class(n_classes=NUM_CLASSES[dataset], current_class=y_train[i]) # data_file = "data/%s_train_labels_%s.npy" % (dataset, noise_ratio) # if os.path.isfile(data_file): # y_train = np.load(data_file) # else: # n_samples = y_train.shape[0] # n_noisy = int(noise_ratio*n_samples/100) # noisy_idx = np.random.choice(n_samples, n_noisy, replace=False) # for i in noisy_idx: # y_train[i] = other_class(n_classes=NUM_CLASSES[dataset], current_class=y_train[i]) # np.save(data_file, y_train) if random_shuffle: # random shuffle idx_perm = np.random.permutation(X_train.shape[0]) X_train, y_train = X_train[idx_perm], y_train[idx_perm] # one-hot-encode the labels y_train = np_utils.to_categorical(y_train, NUM_CLASSES[dataset]) y_test = np_utils.to_categorical(y_test, NUM_CLASSES[dataset]) print("X_train:", X_train.shape) print("y_train:", y_train.shape) print("X_test:", X_test.shape) print("y_test", y_test.shape) return X_train, y_train, X_test, y_test, un_selected_index
def test_fused_batch_norm_uneven_batch(self, distribution): """Test that fused batch norm works when the last device may get empty data. Adapted from https://www.tensorflow.org/tutorials/distribute/custom_training but using ResNet, which uses fused batchnorm, as the model. Arguments: distribution: distribute test configuration """ (train_images, train_labels), _ = fashion_mnist.load_data() # add channel dimension to make 2D data into 3D, since some ops of the model # require it. train_images = train_images[..., None] train_images = train_images / np.float32(255) # Padding images because ResNet requires a minimal shape of (32, 32) padded_train_images = np.concatenate([ np.zeros((len(train_images), 2, 28, 1)), train_images, np.zeros((len(train_images), 2, 28, 1)) ], axis=1) padded_train_images = np.concatenate([ np.zeros((len(train_images), 32, 2, 1)), padded_train_images, np.zeros((len(train_images), 32, 2, 1)) ], axis=2) buffer_size = len(train_images) global_batch_size = distribution.num_replicas_in_sync num_samples = global_batch_size - 1 epochs = 2 # Keep only the first images, so that the last GPU receives an empty batch padded_train_images = padded_train_images[:num_samples] train_labels = train_labels[:num_samples] train_dataset = tf.data.Dataset.from_tensor_slices( (padded_train_images, train_labels)).shuffle(buffer_size).batch(global_batch_size) train_dist_dataset = distribution.experimental_distribute_dataset( train_dataset) def create_model(): inputs = keras.Input((32, 32, 1)) preprocessed = keras.layers.Conv2D(3, (1, 1))( inputs) # ResNet requires 3 channels features = resnet_v2.ResNet50V2(include_top=False, input_tensor=preprocessed, pooling='avg', weights=None).output return keras.Model(inputs, features) with distribution.scope(): # Set reduction to `none` so we can do the reduction afterwards and divide # by global batch size. loss_object = keras.losses.SparseCategoricalCrossentropy( from_logits=True, reduction=losses_impl.Reduction.NONE) def compute_resnet_loss(labels, predictions): per_example_loss = loss_object(labels, predictions) return tf.nn.compute_average_loss( per_example_loss, global_batch_size=global_batch_size) model = create_model() optimizer = optimizers.adam_v2.Adam() def train_step(inputs): images, labels = inputs with tf.GradientTape() as tape: predictions = model(images, training=True) loss = compute_resnet_loss(labels, predictions) gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) return loss @tf.function def distributed_train_step(dataset_inputs): per_replica_losses = distribution.run(train_step, args=(dataset_inputs, )) return distribution.reduce(tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None) for epoch in range(epochs): # Train loop total_loss = 0.0 num_batches = 0 for x in train_dist_dataset: total_loss += distributed_train_step(x) num_batches += 1 train_loss = total_loss / num_batches print(f'Epoch {epoch+1}, Loss: {train_loss}')
# Larger CNN for the MNIST Dataset from keras.datasets import fashion_mnist import matplotlib.pyplot as plt import numpy as np from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout from keras.layers import Flatten from keras.layers.convolutional import Conv2D from keras.layers.convolutional import MaxPooling2D from keras.utils import np_utils # load data (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data() k = X_test # reshape to be [samples][width][height][channels] X_train = X_train.reshape((X_train.shape[0], 28, 28, 1)).astype('float32') X_test = X_test.reshape((X_test.shape[0], 28, 28, 1)).astype('float32') # normalize inputs from 0-255 to 0-1 X_train = X_train / 255 X_test = X_test / 255 # one hot encode outputs y_train = np_utils.to_categorical(y_train) y_test = np_utils.to_categorical(y_test) num_classes = y_test.shape[1] # define the larger model def larger_model(): # create model model = Sequential() model.add(Conv2D(30, (5, 5), input_shape=(28, 28, 1), activation='relu'))
os.makedirs(newdir, exist_ok=True) print("subDirectory", newdir, "created ") IMAGE_LIST_FILE = 'calib_list.txt' # create file for list of calibration images f = open(os.path.join(CALIB_DIR, IMAGE_LIST_FILE), 'w') imgList = list() ############################################################################################# # Download FASHION MNIST Dataset ############################################################################################ # MNIST dataset has 60k images. Training set is 60k, test set is 10k. # Each image is 28x28x1ch with 8bits x 1ch mnist_dataset = fashion_mnist.load_data() (x_train, y_train), (x_test, y_test) = mnist_dataset print("{n} images in original train dataset".format(n=x_train.shape[0])) print("{n} images in original test dataset".format(n=x_test.shape[0])) ######################################################################################## # convert in 3 channels to emulate RGB images (still in gray levels) # and fill the "train", "cal" and "test" folders without any classes imbalance ######################################################################################## counter1 = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype="uint32") num_train = 0 for i in range(0, x_train.shape[0]): t_rgb_img = cv2.cvtColor(x_train[i], cv2.COLOR_GRAY2BGR) rgb_img = cv2.resize(t_rgb_img, (cfg.IMAGE_WIDTH, cfg.IMAGE_HEIGHT),
import keras from keras.preprocessing import image from keras.applications.imagenet_utils import decode_predictions, preprocess_input from keras.datasets import mnist, fashion_mnist from keras.models import Model from sklearn.decomposition import PCA from scipy.spatial import distance from tqdm import tqdm from image_utils import to_rgb_channel_first, to_rgb_channel_last from scipy.misc import imresize from sklearn.manifold import TSNE from PIL import Image (digits_im, digits_labels), (_, _) = mnist.load_data() (fashion_im, fashion_labels), (_, _) = fashion_mnist.load_data() fused_dataset = np.concatenate([digits_im, fashion_im], axis=0) # To test #fused_dataset = fused_dataset[:100] if not os.path.isfile("mnist_vgg_features.npy"): model = keras.applications.VGG16(weights='imagenet', include_top=True) feat_extractor = Model(inputs=model.input, outputs=model.get_layer("fc2").output) img_size = (224, 224) batch_size = 64 n_img = fused_dataset.shape[0]
# example of loading the fashion_mnist dataset from keras.datasets.fashion_mnist import load_data # load the images into memory (trainX, trainy), (testX, testy) = load_data() # summarize the shape of the dataset print('Train', trainX.shape, trainy.shape) print('Test', testX.shape, testy.shape)
from keras import Sequential from keras.datasets import fashion_mnist import numpy as np import matplotlib.pyplot as plt from keras.layers import Dense from keras.utils import to_categorical (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() #display the first image in the training data plt.imshow(train_images[0, :, :], cmap='gray') plt.title('Ground Truth : {}'.format(train_labels[0])) plt.show() #test images plt.imshow(test_images[0, :, :], cmap='gray') plt.title('Ground Truth : {}'.format(test_labels[0])) plt.show() #process the data #1. convert each image of shape 28*28 to 784 dimensional which will be fed to the network as a single feature dimData = np.prod(train_images.shape[1:]) train_data = train_images.reshape(train_images.shape[0], dimData) test_data = test_images.reshape(test_images.shape[0], dimData) #convert data to float and scale values between 0 and 1 train_data = train_data.astype('float') test_data = test_data.astype('float') #scale data #train_data /=255.0 #test_data /=255.0
import wandb import numpy as np import matplotlib.pyplot as plt from keras.datasets import fashion_mnist from feedForwardNeuralNet import FeedForwardNeuralNetwork (trainIn, trainOut), (testIn, testOut) = fashion_mnist.load_data() N_train_full = trainOut.shape[0] N_train = int(0.9 * N_train_full) N_validation = int(0.1 * trainOut.shape[0]) N_test = testOut.shape[0] idx = np.random.choice(trainOut.shape[0], N_train_full, replace=False) idx2 = np.random.choice(testOut.shape[0], N_test, replace=False) trainInFull = trainIn[idx, :] trainOutFull = trainOut[idx] trainIn = trainInFull[:N_train, :] trainOut = trainOutFull[:N_train] validIn = trainInFull[N_train:, :] validOut = trainOutFull[N_train:] testIn = testIn[idx2, :] testOut = testOut[idx2] sweep_config = {
def train(self, epochs, sample_interval=50): d_losses = [] g_losses = [] def plot_gan_losses(g_loss, d_loss): plt.plot(g_loss) plt.plot(d_loss) plt.title('GAN Loss evolution') plt.ylabel('') plt.xlabel('epoch') plt.legend(['Generator', 'Discriminator'], loc='best') plt.show() # Load the dataset (X_train, _), (_, _) = fashion_mnist.load_data() # Rescale -1 to 1 X_train = (X_train.astype(np.float32) - 127.5) / 127.5 X_train = np.expand_dims(X_train, axis=3) # Adversarial ground truths valid = np.ones((self.batch_size, 1)) fake = np.zeros((self.batch_size, 1)) for epoch in range(epochs): # --------------------- # Train Discriminator # --------------------- # Select a random batch of images idx = np.random.randint(0, X_train.shape[0], self.batch_size) imgs = X_train[idx] noise = np.random.normal(0, 1, (self.batch_size, self.latent_dim)) # Generate a batch of new images gen_imgs = self.generator.predict(noise) if epoch == 0: print('random image shape {}\ngenerated image shape {}'.format( imgs.shape, gen_imgs.shape)) # Train the discriminator d_loss_real = self.discriminator.train_on_batch(imgs, valid) d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) # --------------------- # Train Generator # --------------------- noise = np.random.normal(0, 1, (self.batch_size, self.latent_dim)) # Train the generator (to have the discriminator label samples as valid) g_loss = self.combined.train_on_batch(noise, valid) g_losses.append(g_loss) d_losses.append(d_loss) # If at save interval => save generated image samples if epoch % sample_interval == 0: # Plot the progress print('===EPOCH {}==='.format(epoch)) print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100 * d_loss[1], g_loss)) self.sample_images(epoch) plot_gan_losses(g_losses, d_losses)
def train(): (X_train, y_train), (_, _) = fashion_mnist.load_data() X_train = (X_train.astype(np.float32) - 127.5) / 127.5 X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1) print(X_train.shape) discriminator = discriminator_model() d_opt = Adam(lr=1e-5, beta_1=0.1) discriminator.compile(loss='binary_crossentropy', optimizer=d_opt) # generator+discriminator (discriminator部分の重みは固定) discriminator.trainable = False generator = generator_model() dcgan = Sequential([generator, discriminator]) g_opt = Adam(lr=2e-4, beta_1=0.5) dcgan.compile(loss='binary_crossentropy', optimizer=g_opt) num_batches = int(X_train.shape[0] / BATCH_SIZE) print('Number of batches:', num_batches) for epoch in range(NUM_EPOCH): for index in range(num_batches): noise = np.array( [np.random.uniform(-1, 1, 100) for _ in range(BATCH_SIZE)]) image_batch = X_train[index * BATCH_SIZE:(index + 1) * BATCH_SIZE] generated_images = generator.predict(noise, verbose=0) # 生成画像を出力 if index % 500 == 0: # generate images and shape generated_images_plot = generated_images.astype( 'float32') * 127.5 + 127.5 generated_images_plot = generated_images_plot.reshape( (BATCH_SIZE, 28, 28)) plt.figure(figsize=(8, 4)) plt.suptitle('epoch=%04d,index=%04d' % (epoch, index), fontsize=20) for i in range(BATCH_SIZE): plt.subplot(4, 8, i + 1) plt.imshow(generated_images_plot[i]) plt.gray() # eliminate ticks plt.xticks([]), plt.yticks([]) # save images if not os.path.exists(GENERATED_IMAGE_PATH): os.mkdir(GENERATED_IMAGE_PATH) filename = GENERATED_IMAGE_PATH + "MNIST_%04d_%04d.png" % ( epoch, index) plt.savefig(filename) # discriminatorを更新 X = np.concatenate((image_batch, generated_images)) y = [1] * BATCH_SIZE + [0] * BATCH_SIZE d_loss = discriminator.train_on_batch(X, y) # generatorを更新 noise = np.array( [np.random.uniform(-1, 1, 100) for _ in range(BATCH_SIZE)]) g_loss = dcgan.train_on_batch(noise, [1] * BATCH_SIZE) print("epoch: %d, batch: %d, g_loss: %f, d_loss: %f" % (epoch, index, g_loss, d_loss)) generator.save_weights('generator_fashion_mnist.h5') discriminator.save_weights('discriminator_fashion_mnist.h5')
#Written by: Rajeev Sahay # The purpose of this code is to create the classifiers that both the # attacker and defender will use in the experiments # Defender classifier reaches accuracy of 88.80% # Attacker classifier reaches accuracy of 89.41% import numpy as np import keras from keras.models import Sequential from keras.layers import Dense from keras.datasets import fashion_mnist #Import dataset and normalize to [0,1] (data_train, labels_train), (data_test, labels_test) = fashion_mnist.load_data() data_train = data_train / 255.0 data_test = data_test / 255.0 #Flatten dataset (New shape for training and testing set is (60000,784) and (10000, 784)) data_train = data_train.reshape( (len(data_train), np.prod(data_train.shape[1:]))) data_test = data_test.reshape((len(data_test), np.prod(data_test.shape[1:]))) #Create labels as one-hot vectors labels_train = keras.utils.np_utils.to_categorical(labels_train, num_classes=10) labels_test = keras.utils.np_utils.to_categorical(labels_test, num_classes=10) def fc_model_784_atkr():
def test_fashion_mnist(): return fashion_mnist.load_data()
def main(): # read data (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() # read data (x_train, y_train, x_test, y_test) = preprocess(x_train, y_train, x_test, y_test) # Reshape the data inputs such that we can put those inputs into MLP train_inputs = np.reshape(x_train, (-1, 28 * 28)) test_inputs = np.reshape(x_test, (-1, 28 * 28)) orig_model = MLP(28 * 28) # train original model epochs = 10 print("Training Original MLP...") for i in range(epochs): train(orig_model, train_inputs, y_train) test_acc = test(orig_model, test_inputs, y_test) print("Epoch: {} ------ Testing accuracy: {}".format(i + 1, test_acc)) # calculate fgs and deepfool for original model, on both test set and training set print("Creating DeepFool images set... will take aobut 5 mins") (train_adv_orig, train_r_orig) = deepfool(orig_model, train_inputs) (test_adv_orig, test_r_orig) = deepfool(orig_model, test_inputs) # fine tuning tuning_model = MLP_tuning(28 * 28, orig_model) epochs = 5 print("Training Fine Tuning MLP...") for i in range(epochs): train(tuning_model, train_adv_orig, y_train) tuning_test_acc = test(tuning_model, test_adv_orig, y_test) print("Epoch: {} ------ Testing accuracy: {}".format( i + 1, tuning_test_acc)) # train deepdefense model regu_model = regu_MLP(28 * 28, orig_model) epochs = 5 print("Training Deep Defense MLP...") for i in range(epochs): regu_train(regu_model, train_adv_orig, y_train, train_r_orig) regu_test_acc = test(regu_model, test_adv_orig, y_test) print("Epoch: {} ------ Testing accuracy: {}".format( i + 1, regu_test_acc)) # keep training original model for comparison epochs = 5 print("Training MLP for 5 more epochs...") for i in range(epochs): train(orig_model, train_inputs, y_train) test_accu = test(orig_model, test_inputs, y_test) print("Epoch: {} ------ Testing accuracy: {}".format(i + 1, test_accu)) ################### Evaluation ######################### # ROC curve on deepfool testing image generated from origianl MLP model roc1 = roc(orig_model, test_adv_orig, y_test, "Vanilla MLP") roc2 = roc(tuning_model, test_adv_orig, y_test, "Fine tuning MLP") roc3 = roc(regu_model, test_adv_orig, y_test, "Deep Defense MLP") AUC = pd.DataFrame( { "Vanilla MLP": list(roc1.values()), "Fine-Tune MLP": list(roc2.values()), "Deep Defense": list(roc3.values()) }, index=["label " + str(i + 1) for i in range(10)]) print("Area Under the Curve:") print(AUC) # testing acc on benign images benign_test_acc = pd.DataFrame( { "Vanilla MLP": test(orig_model, test_inputs, y_test), "Fine-Tune MLP": test(tuning_model, test_inputs, y_test), "Deep Defense": test(regu_model, test_inputs, y_test) }, index=["TestAcc"]) # rho2 scores (test_adv_orig2, test_r_orig2) = deepfool(orig_model, test_inputs) (test_adv_tuning, test_r_tuning) = deepfool(tuning_model, test_inputs) (test_adv_regu, test_r_regu) = deepfool(regu_model, test_inputs) regu_rho2 = rho2(test_r_regu, test_inputs) tuning_rho2 = rho2(test_r_tuning, test_inputs) orig_rho2 = rho2(test_r_orig2, test_inputs) rho2_all = pd.DataFrame( { "Vanilla MLP": orig_rho2, "Fine-Tune MLP": tuning_rho2, "Deep Defense": regu_rho2 }, index=["Rho2 Score"]) # plot accuracy on FGS images epsilon_ref_100, epsilon_ref_50, epsilon_ref_20 = plot_acc_on_FGS( orig_model, regu_model, tuning_model, test_inputs, y_test, test_adv_orig) epsilon_list = [epsilon_ref_20, epsilon_ref_50, epsilon_ref_100] # calculating testing accuracy of vanilla, regu, and finetune on FGS examples with these three epsilon values pert_test_orig = FGS(orig_model, test_inputs, y_test, 1) pert_test_regu = FGS(regu_model, test_inputs, y_test, 1, True, test_adv_orig) pert_test_tuning = FGS(tuning_model, test_inputs, y_test, 1) FGS_orig_test_acc = list( map( lambda x: test(orig_model, x * pert_test_orig + test_inputs, y_test ), epsilon_list)) FGS_regu_test_acc = list( map( lambda x: test(regu_model, x * pert_test_regu + test_inputs, y_test ), epsilon_list)) FGS_tuning_test_acc = list( map( lambda x: test(tuning_model, x * pert_test_tuning + test_inputs, y_test), epsilon_list)) acc_fgs = pd.DataFrame( { "Vanilla MLP": FGS_orig_test_acc, "Fine-Tune MLP": FGS_tuning_test_acc, "Deep Defense": FGS_regu_test_acc }, index=["[email protected]", "[email protected]", "[email protected]"]) result_table = pd.concat([benign_test_acc, rho2_all, acc_fgs], ignore_index=False).transpose() print(result_table)
from keras.datasets import mnist, fashion_mnist # Simplificación de la escritura del modelo from keras.models import Sequential from keras.layers import Flatten, Dense, Dropout, Conv2D, MaxPooling2D from keras.utils import np_utils # Artilugios matemáticos # import numpy as np import funciones_auxiliares as fa # Cargar datos # (entradas_entrenamiento,salidas_entrenamiento), (entrada_pruebas,salidas_prueba) # (x_entrenamiento,y_entrenamiento), (x_prueba,y_prueba) = mnist.load_data() (x_entrenamiento, y_entrenamiento), (x_prueba, y_prueba) = fashion_mnist.load_data() ''' mnist contiene imágenes de números escritos a mano fashion_mnist contiene imágenes de ropa x_entrenamiento: 60.000 entradas de 28x28 y_entrenamiento: 60.000 etiquetas del 0 al 9 x_prueba: 10.000 entradas de 28x28 y_prueba: 10.000 etiquetas del 0 al 9 ''' # Normalización de los datos ''' Las redes neuronales son dependientes de la magnitud de los datos. Es recomendable normalizar los datos siempre. '''
def train_model_with_different_learning_rate(optimizer, type): batch_size = 128 num_classes = 10 epochs = 30 # input image dimensions img_rows, img_cols = 28, 28 # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) model = Sequential() model.add(Conv2D(filters=6, kernel_size=5, strides=1, activation='relu', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=2, strides=2)) model.add(Conv2D(filters=16, kernel_size=5, strides=1, activation='relu', input_shape=(14, 14, 6))) model.add(MaxPooling2D(pool_size=2, strides=2)) model.add(Flatten()) model.add(Dense(units=120, activation='relu')) model.add(Dense(units=84, activation='relu')) model.add(Dense(units=10, activation='softmax')) start_time = time() # sgd = keras.optimizers.SGD(lr=learning_rate) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=optimizer, # optimizer=keras.optimizers.Adadelta(lr=learning_rate), metrics=['accuracy']) log_file = "logs/{}".format(time()) + '-fashion-no-lr-' + type tensorboard = TensorBoard(log_dir=log_file, histogram_freq=0, write_graph=True, write_images=True) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test), callbacks=[tensorboard]) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) print('Total time:', (time() - start_time), 'seconds')
parser.add_argument("-n", "--renormalize", default="1") parser.add_argument("-b", "--batchnormalization", default="1") args = parser.parse_args() """Set GPU for use.""" os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu """Open log file.""" filename = "./res/" + args.true + "_" + args.loss + "_" + str( args.lambda1) + "_" + str(args.enforce_proj) + "_" + str( args.all_alt) + ".txt" dataname = "./res/" + args.true + "_" + args.loss + "_" + str( args.lambda1) + "_" + str(args.enforce_proj) + "_" + str( args.all_alt) + ".data" """Data usage.""" if args.true == "fashion": _, (X_test_origin, y_test_origin) = fashion_mnist.load_data() elif args.true == "caltech101": with open("../../data/caltech101.data", 'rb') as f: data = pickle.load(f) X_test_origin = data["X"] y_test_origin = data["y"] elif args.true == "tinyimagenet": with open("../data/tinyimagenet.data", 'rb') as f: data = pickle.load(f) X_test_origin = data["X"] y_test_origin = data["y"] elif args.true == "tinyimagenetvar": with open("../data/tinyimagenetvar.data", 'rb') as f: data = pickle.load(f) X_test_origin = data["Xvar"] y_test_origin = data["y"]
def get_data(dataset='mnist', noise_ratio=0, data_ratio=100, random_shuffle=False): """ Get training images with specified ratio of label noise :param data_ratio: percentage of data used, once use this parameter, the data ration will averagely be used on each class :param dataset: :param noise_ratio: 0 - 100 (%) :param random_shuffle: :return: """ if dataset == 'mnist' or dataset == 'fashion_mnist': if dataset == 'mnist': (X_train, y_train), (X_test, y_test) = mnist.load_data() if dataset == 'fashion_mnist': (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data() selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) val_selected_limit = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(NUM_CLASSES[dataset]): selected_limit[i] = X_train.shape[ 0] * data_ratio / 100.0 / NUM_CLASSES[dataset] #pdb.set_trace() selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) idx = random.sample(range(len(selected_index)), k=int(X_train.shape[0] * (0.5 * data_ratio) / 100.0)) #pdb.set_trace() val_selected_index = list(selected_index[i] for i in idx) selected_index1 = [ x for x in selected_index if x not in val_selected_index ] selected_index = selected_index1 a = X_train b = y_train X_train = X_train[selected_index] y_train = y_train[selected_index] X_train = X_train.reshape(-1, 28, 28, 1) X_test = X_test.reshape(-1, 28, 28, 1) X_train = X_train / 255.0 X_test = X_test / 255.0 means = X_train.mean(axis=0) # std = np.std(X_train) X_train = (X_train - means) # / std X_test = (X_test - means) # / std # they are 2D originally in cifar y_train = y_train.ravel() y_test = y_test.ravel() ##### 2000 validation set X_val = a[val_selected_index] y_val = b[val_selected_index] X_val = X_val.reshape(-1, 28, 28, 1) X_val = X_val / 255.0 means = X_val.mean(axis=0) # std = np.std(X_train) X_val = (X_val - means) # / std # they are 2D originally in cifar y_val = y_val.ravel() elif dataset == 'celeb': f = BytesIO( file_io.read_file_to_string('data_image_train_20.npy', binary_mode=True)) X_train = np.load(f) f = BytesIO( file_io.read_file_to_string('data_image_test_20.npy', binary_mode=True)) X_test = np.load(f) f = BytesIO( file_io.read_file_to_string('data_label_train_20.npy', binary_mode=True)) y_train = np.load(f) f = BytesIO( file_io.read_file_to_string('data_label_test_20.npy', binary_mode=True)) y_test = np.load(f) selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(NUM_CLASSES[dataset]): selected_limit[i] = X_train.shape[ 0] * data_ratio / 100.0 / NUM_CLASSES[dataset] selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) X_train = X_train[selected_index] y_train = y_train[selected_index] X_train = X_train.reshape(-1, 128, 128, 3) X_test = X_test.reshape(-1, 128, 128, 3) X_train = X_train / 255.0 X_test = X_test / 255.0 means = X_train.mean(axis=0) # std = np.std(X_train) X_train = (X_train - means) # / std X_test = (X_test - means) # / std # they are 2D originally in cifar y_train = y_train.ravel() y_test = y_test.ravel() elif dataset == 'cifar-10': (X_train, y_train), (X_test, y_test) = cifar10.load_data() selected_index = [] val_selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) val_selected_limit = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(NUM_CLASSES[dataset]): selected_limit[i] = X_train.shape[ 0] * data_ratio / 100.0 / NUM_CLASSES[dataset] #val_selected_limit[i] = X_train.shape[0] * (0.2*data_ratio) / 100.0 / NUM_CLASSES[dataset] selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) idx = random.sample(range(len(selected_index)), k=int(X_train.shape[0] * (0.2 * data_ratio) / 100.0)) val_selected_index = list(selected_index[i] for i in idx) selected_index1 = [ x for x in selected_index if x not in val_selected_index ] selected_index = selected_index1 a = X_train b = y_train X_train = X_train[selected_index] y_train = y_train[selected_index] X_train = X_train.reshape(-1, 32, 32, 3) X_test = X_test.reshape(-1, 32, 32, 3) X_train = X_train / 255.0 X_test = X_test / 255.0 means = X_train.mean(axis=0) # std = np.std(X_train) X_train = (X_train - means) # / std X_test = (X_test - means) # / std # they are 2D originally in cifar y_train = y_train.ravel() y_test = y_test.ravel() ##### 2000 validation set X_val = a[val_selected_index] y_val = b[val_selected_index] X_val = X_val.reshape(-1, 32, 32, 3) X_val = X_val / 255.0 means = X_val.mean(axis=0) # std = np.std(X_train) X_val = (X_val - means) # / std # they are 2D originally in cifar y_val = y_val.ravel() elif dataset == 'cifar-100': (X_train, y_train), (X_test, y_test) = cifar100.load_data() selected_index = [] un_selected_index = [] selected_limit = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(NUM_CLASSES[dataset]): selected_limit[i] = X_train.shape[ 0] * data_ratio / 100.0 / NUM_CLASSES[dataset] selected_counter = np.zeros(NUM_CLASSES[dataset]) for i in np.arange(len(y_train)): if selected_counter[y_train[i]] < selected_limit[y_train[i]]: selected_index.append(i) selected_counter[y_train[i]] += 1 else: un_selected_index.append(i) X_train = X_train[selected_index] y_train = y_train[selected_index] X_train = X_train.reshape(-1, 32, 32, 3) X_test = X_test.reshape(-1, 32, 32, 3) X_train = X_train / 255.0 X_test = X_test / 255.0 means = X_train.mean(axis=0) # std = np.std(X_train) X_train = (X_train - means) # / std X_test = (X_test - means) # / std # they are 2D originally in cifar y_train = y_train.ravel() y_test = y_test.ravel() else: return None, None, None, None, None X_train = X_train.astype('float32') X_test = X_test.astype('float32') # generate random noisy labels if noise_ratio > 0: n_samples = y_train.shape[0] n_noisy = int(noise_ratio * n_samples / 100) noisy_idx = np.random.choice(n_samples, n_noisy, replace=False) for i in noisy_idx: y_train[i] = other_class(n_classes=NUM_CLASSES[dataset], current_class=y_train[i]) # data_file = "data/%s_train_labels_%s.npy" % (dataset, noise_ratio) # if os.path.isfile(data_file): # y_train = np.load(data_file) # else: # n_samples = y_train.shape[0] # n_noisy = int(noise_ratio*n_samples/100) # noisy_idx = np.random.choice(n_samples, n_noisy, replace=False) # for i in noisy_idx: # y_train[i] = other_class(n_classes=NUM_CLASSES[dataset], current_class=y_train[i]) # np.save(data_file, y_train) if random_shuffle: # random shuffle idx_perm = np.random.permutation(X_train.shape[0]) X_train, y_train = X_train[idx_perm], y_train[idx_perm] # one-hot-encode the labels y_train = np_utils.to_categorical(y_train, NUM_CLASSES[dataset]) y_test = np_utils.to_categorical(y_test, NUM_CLASSES[dataset]) y_val = np_utils.to_categorical(y_val, NUM_CLASSES[dataset]) print("X_train:", X_train.shape) print("y_train:", y_train.shape) print("X_test:", X_test.shape) print("y_test", y_test.shape) print("X_val:", X_val.shape) print("y_val", y_val.shape) return X_train, y_train, X_test, y_test, X_val, y_val, un_selected_index,
parser.add_argument( '--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) device = torch.device("cuda" if args.cuda else "cpu") kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {} (_, _), (x_test, x_test_lab) = fashion_mnist.load_data() ind = np.isin(x_test_lab, (7, 9)) # (0, 2, 3, 4, 6)) # x_test = x_test[ind] x_test = x_test / 255.00 #x_test = x_test.astype(float) in_data = x_test in_data = torch.tensor(in_data).float().view(in_data.shape[0], 1, 28, 28) #x_train = torch.from_numpy(x_train).float().view(x_train.shape[0],1,28,28) #x_test = torch.from_numpy(x_test).float().view(x_test.shape[0],1,28,28) model_mean = VAE().to(device) model_mean.load_state_dict(torch.load('results/VAE_QR.pth'))
import keras from keras.datasets import fashion_mnist from keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout from keras.models import Sequential from keras.utils import to_categorical import numpy as np import matplotlib.pyplot as plt model = keras.models.load_model('saved_model/model_10') model.summary() (_,_), (x_test, y_test) = fashion_mnist.load_data() x_test = x_test.reshape(-1, 28,28, 1) x_test = x_test.astype('float32') x_test = x_test / 255 y_test = to_categorical(y_test) loss, acc = model.evaluate(x_test, y_test, verbose=2) print('Restored model accuracy: {:5.2f}%'.format(100*acc))
################################################################## # # Convolutional Neural Networks in Python with Keras # https://www.datacamp.com/community/tutorials/convolutional-neural-networks-python # ################################################################## #------------------------------------------------------------------ # Load the Data #------------------------------------------------------------------ from keras.datasets import fashion_mnist (train_X, train_Y), (test_X, test_Y) = fashion_mnist.load_data() #------------------------------------------------------------------ # Analyze the Data #------------------------------------------------------------------ import numpy as np from keras.utils import to_categorical import matplotlib.pyplot as plt #%matplotlib inline print('Training data shape : ', train_X.shape, train_Y.shape) print('Testing data shape : ', test_X.shape, test_Y.shape) # Find the unique numbers from the train labels classes = np.unique(train_Y) nClasses = len(classes) print('Total number of outputs : ', nClasses) print('Output classes : ', classes)
from keras.datasets import fashion_mnist from keras.models import Sequential from keras.layers import Dense, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.utils import to_categorical tamBatch = 256 numEpocas = 10 imgX, imgY = 28, 28 numClasses = 10 (x_train, y_train), (x_val, y_val) = fashion_mnist.load_data() x_train = x_train.reshape(x_train.shape[0], imgX, imgY, 1) x_val = x_val.reshape(x_val.shape[0], imgX, imgY, 1) input_format = (imgX, imgY, 1) x_train = x_train.astype('float32') x_val = x_val.astype('float32') x_train /= 255 x_val /= 255 y_train = to_categorical(y_train, numClasses) y_val = to_categorical(y_val, numClasses) model = Sequential() model.add( Conv2D(filters=6, kernel_size=(5, 5),
from keras.models import load_model from keras.datasets import fashion_mnist from keras.utils import to_categorical import numpy as np from process.ssim import get_ssim from tqdm import tqdm model = load_model("../model.hdf5") (train_data, train_labels), (t_data, test_labels) = fashion_mnist.load_data() test_data = np.load("../test_data/test_data.npy") attack_data = np.array([]).reshape((0, 28, 28, 1)) for i in range(1, 9): path = "../attack_data/attack_data_m" + str(i) + ".npy" attack = np.load(path) attack_data = np.concatenate([attack_data, attack]) print(attack_data.shape) attack_data = attack_data.astype("float32") ssim_sum = 0 for i in tqdm(range(10000)): ssim_sum += get_ssim(test_data[i], attack_data[i]) print(ssim_sum / 10000) print(model.evaluate(attack_data, to_categorical(test_labels))) np.save("../attack_data/attack_data.npy", attack_data)
import matplotlib matplotlib.use("Agg") from pyimagesearch.minivggnet import MiniVGGNet from sklearn.metrics import classification_report from keras.optimizers import SGD from keras.datasets import fashion_mnist from keras.utils import np_utils from imutils import build_montages import numpy as np NUM_EPOCHS = 25 INIT_LR = 1e-2 BS = 32 ((trainX, trainY), (testX, testY)) = fashion_mnist.load_data() if K.image_data_format() == "channels_first": trainX = trainX.reshape((trainX.shape[0], 1, 28, 28)) testX = testX.reshape((testX.shape[0], 1, 28, 28)) else: trainX = trainX.reshape((trainX.shape[0], 28, 28, 1)) testX = testX.reshape((testX.shape[0], 28, 28, 1)) trainX = trainX.astype("float32") / 255.0 testX = testX.astype("float32") / 255.0 trainY = np_utils.to_categorical(trainY, 10) testY = np_utils.to_categorical(testY, 10)
# show the some incorrectly predicted categories f4 = plt.figure(4) for i, incorrect in enumerate(incorrectIndex[:9]): plt.subplot(3, 3, i + 1) plt.imshow(test_X[incorrect].reshape(28, 28), cmap='gray', interpolation='none') plt.title("Predicted {}, Class {}".format(predicted_classes[incorrect], test_Y[incorrect])) plt.draw() plt.pause(0.001) plt.tight_layout() plt.pause(0.001) (trainAndVal_X, trainAndVal_Y), (test_X, test_Y) = fashion_mnist.load_data() classes = np.unique(trainAndVal_Y) nClasses = len(classes) #--------------------------------------------------------------------------------- #------------------------------ PROBLEM 0: testing ------------------------------- #--------------------------------------------------------------------------------- ## -- Display images from training data and test data -- ## if False: f1 = plt.figure(1) for i in range(10): plt.subplot(1, 2, 1) plt.ion() plt.imshow(trainAndVal_X[i, :, :], cmap='gray') plt.title("Class ID: {}".format(trainAndVal_Y[i]))
# this is the size of our encoded representations encoding_dim = 32 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats # this is our input placeholder input_img = Input(shape=(784,)) # "encoded" is the encoded representation of the input encoded = Dense(encoding_dim, activation='relu')(input_img) # "decoded" is the lossy reconstruction of the input decoded = Dense(784, activation='sigmoid')(encoded) # this model maps an input to its reconstruction autoencoder = Model(input_img, decoded) # this model maps an input to its encoded representation autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') from keras.datasets import fashion_mnist import numpy as np (x_train, _), (x_test, _) = fashion_mnist.load_data() x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) #introducing noise noise_factor = 0.5 x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) autoencoder.fit(x_train_noisy, x_train, epochs=10, batch_size=256, shuffle=True, validation_data=(x_test_noisy, x_test_noisy))
import time import keras import matplotlib.pyplot as plt from keras.utils import to_categorical from keras.datasets import mnist, fashion_mnist from keras.models import Sequential from keras.layers import Dense, Activation, Dropout, Conv2D, MaxPooling2D, Flatten from sklearn.metrics import confusion_matrix import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' (xTrain, yTrain), (xTest, yTest) = fashion_mnist.load_data() xTrain = xTrain.reshape(xTrain.shape[0], xTrain.shape[1] * xTrain.shape[2]) xTest = xTest.reshape(xTest.shape[0], xTest.shape[1] * xTest.shape[2]) xTrain = xTrain.astype('float32') xTest = xTest.astype('float32') xTrain /= 255 xTest /= 255 xTrain = xTrain.reshape((xTrain.shape[0], 28, 28, 1)) xTest = xTest.reshape((xTest.shape[0], 28, 28, 1)) yTrain = to_categorical(yTrain, 10) yTest = to_categorical(yTest, 10) model = Sequential() model.add( Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1))) model.add(MaxPooling2D(pool_size=(3, 3))) model.add(Conv2D(64, kernel_size=(2, 2), activation='relu'))
#Deep Learning Türkiye topluluğu tarafından hazırlanmıştır. #Amaç: Kıyafetlerin tanımlanması #Veriseti: Fashion-Mnist #Algoritma : Evrişimli Sinir Ağları (Convolutional Neural Networks) #Hazırlayan: Can UMAY #Gerekli kütüphanelerimizi içeri aktarıyoruz. from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten from keras.datasets import fashion_mnist from keras.utils.np_utils import to_categorical #Eğitim ve test verilerimizi aktarıyoruz. (inp_train, out_train), (inp_test, out_test) = fashion_mnist.load_data() #Inp_ olarak tanımlanan değişkenlerimizin boyutlarını düzenliyoruz. inp_train = inp_train.reshape(-1, 28, 28, 1) inp_test = inp_test.reshape(-1, 28, 28, 1) #Daha sonra ondalık hale çeviriyoruz. inp_train = inp_train.astype('float32') inp_test = inp_test.astype('float32') #Modelimizin daha optimize çalışması için değerlerimizi 0 ile 1 arasına indirgiyoruz. inp_train = inp_train / 255.0 inp_test = inp_test / 255.0 #Out_ olarak tanımlanan değişkenleri ise one-hot-encoding haline getiriyoruz. out_train = to_categorical(out_train) out_test = to_categorical(out_test) #Modelimizi oluşturmaya başlıyoruz. model = Sequential() # 3x3'lük 32 filtreli ve relu aktivasyon fonksiyonlu ilk Conv2D katmanımızı oluşturuyoruz. model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1), activation='relu'))
def cifar10_tutorial(train_start=0, train_end=60000, test_start=0, test_end=10000, nb_epochs=NB_EPOCHS, batch_size=BATCH_SIZE, learning_rate=LEARNING_RATE, train_dir=TRAIN_DIR, filename=FILENAME, load_model=LOAD_MODEL, testing=False, label_smoothing=0.1): """ MNIST 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 train_dir: Directory storing the saved model :param filename: Filename to save model under :param load_model: True for load, False for not load :param testing: if true, test error is calculated :param label_smoothing: float, amount of label smoothing for cross entropy :return: an AccuracyReport object """ keras.layers.core.K.set_learning_phase(0) # Object used to keep track of (and return) key accuracies report = AccuracyReport() # Set TF random seed to improve reproducibility tf.set_random_seed(1234) if not hasattr(backend, "tf"): raise RuntimeError("This tutorial requires keras to be configured" " to use the TensorFlow backend.") if keras.backend.image_dim_ordering() != 'tf': keras.backend.set_image_dim_ordering('tf') print("INFO: '~/.keras/keras.json' sets 'image_dim_ordering' to " "'th', temporarily setting to 'tf'") # Create TF session and set as Keras backend session os.environ["CUDA_VISIBLE_DEVICES"] = '0' # only use No.0 GPU config = tf.ConfigProto() config.allow_soft_placement = True config.gpu_options.allow_growth = True sess = tf.Session(config=config) keras.backend.set_session(sess) # Get MNIST test data fashion_mnist = FASHION_MNIST.load_data() x_train, y_train = fashion_mnist[0] x_test, y_test = fashion_mnist[1] # Obtain Image Parameters img_rows, img_cols, nchannels = x_train.shape[1:4] nb_classes = y_train.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)) # Define TF model graph model = cnn_model(img_rows=img_rows, img_cols=img_cols, channels=nchannels, nb_filters=64, nb_classes=nb_classes) model = ModelAllConvolutional('model1', nb_classes, nb_filters=64, input_shape=[32, 32, 3]) preds = model(x) print("Defined TensorFlow model graph.") def evaluate(): # Evaluate the accuracy of the MNIST model on legitimate test examples eval_params = {'batch_size': batch_size} acc = model_eval(sess, x, y, preds, x_test, y_test, args=eval_params) report.clean_train_clean_eval = acc # assert X_test.shape[0] == test_end - test_start, X_test.shape print('Test accuracy on legitimate examples: %0.4f' % acc) # Train an MNIST model train_params = { 'nb_epochs': nb_epochs, 'batch_size': batch_size, 'learning_rate': learning_rate, 'train_dir': train_dir, 'filename': filename } rng = np.random.RandomState([2017, 8, 30]) if not os.path.exists(train_dir): os.mkdir(train_dir) ckpt = tf.train.get_checkpoint_state(train_dir) print(train_dir, ckpt) ckpt_path = False if ckpt is None else ckpt.model_checkpoint_path # wrap = KerasModelWrapper(model) if load_model and ckpt_path: saver = tf.train.Saver() print(ckpt_path) saver.restore(sess, ckpt_path) print("Model loaded from: {}".format(ckpt_path)) evaluate() else: print("Model was not loaded, training from scratch.") loss = CrossEntropy(model, smoothing=label_smoothing) train(sess, loss, x_train, y_train, evaluate=evaluate, args=train_params, rng=rng) saver = tf.train.Saver(max_to_keep=1) saver.save(sess, '{}/cifar10.ckpt'.format(train_dir), global_step=NB_EPOCHS) print("model has been saved") # Calculate training error if testing: eval_params = {'batch_size': batch_size} acc = model_eval(sess, x, y, preds, x_train, y_train, args=eval_params) report.train_clean_train_clean_eval = acc # Initialize the Fast Gradient Sign Method (FGSM) attack object and graph fgsm = FastGradientMethod(model, sess=sess) fgsm_params = {'eps': 0.2, 'clip_min': 0., 'clip_max': 1.} print(fgsm_params) adv_x = fgsm.generate(x, **fgsm_params) # Consider the attack to be constant adv_x = tf.stop_gradient(adv_x) preds_adv = model(adv_x) # Evaluate the accuracy of the MNIST model on adversarial examples eval_par = {'batch_size': batch_size} start_time = time.time() acc = model_eval(sess, x, y, preds_adv, x_test, y_test, args=eval_par) print('Test accuracy on adversarial examples: %0.4f' % acc) end_time = time.time() print("FGSM attack time is {}\n".format(end_time - start_time)) report.clean_train_adv_eval = acc # Calculating train error if testing: eval_par = {'batch_size': batch_size} acc = model_eval(sess, x, y, preds_adv, x_train, y_train, args=eval_par) report.train_clean_train_adv_eval = acc gc.collect() return report
################################################################## # # Convolutional Neural Networks in Python with Keras # https://www.datacamp.com/community/tutorials/convolutional-neural-networks-python # ################################################################## #------------------------------------------------------------------ # Load the Data #------------------------------------------------------------------ from keras.datasets import fashion_mnist (train_X,train_Y), (test_X,test_Y) = fashion_mnist.load_data() #------------------------------------------------------------------ # Analyze the Data #------------------------------------------------------------------ import numpy as np from keras.utils import to_categorical import matplotlib.pyplot as plt #%matplotlib inline print('Training data shape : ', train_X.shape, train_Y.shape) print('Testing data shape : ', test_X.shape, test_Y.shape) # Find the unique numbers from the train labels classes = np.unique(train_Y)
import numpy as np from sklearn.datasets import load_iris from sklearn.linear_model import Perceptron from keras.models import Sequential from keras.layers import Dense, Flatten, LeakyReLU, BatchNormalization, Activation from keras.datasets import fashion_mnist from keras.optimizers import Adam, SGD from keras.callbacks import ModelCheckpoint (x_train_full, y_train_full), (x_test, y_test) = fashion_mnist.load_data() x_valid, x_train = x_train_full[:5000] / 255.0, x_train_full[5000:] / 255.0 y_valid, y_train = y_train_full[:5000], y_train_full[5000:] x_test = x_test / 255.0 model = Sequential() model.add(Flatten(input_shape=(28, 28))) model.add(BatchNormalization()) model.add(Dense(300, kernel_initializer='he_normal', use_bias=False)) model.add(BatchNormalization()) model.add(Activation('elu')) model.add(Dense(100, kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('elu')) # model.add(Activation='elu') (x) model.add(Dense(10, activation='softmax')) model.summary() # 배치 정규화 층은 입력마다 이동 파라미터를 포함하기 때문에 이전 층에서 편향을 뺄 수 있습니다. checkpoint = ModelCheckpoint("./model/my_keras_model.h5")
from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras import backend as K model_path = sys.argv[1] batch_size = 128 num_classes = 10 epochs = 12 # input image dimensions img_rows, img_cols = 28, 28 # the data, shuffled and split between train and test sets (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 print('x_train shape:', x_train.shape)
def load_dataset(dset, normalize_data, options): if dset == 'mnist': # input image dimensions img_rows, img_cols = 28, 28 # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train.shape) n_channels = 1 elif dset == 'cifar10': img_rows, img_cols = 32, 32 n_channels = 3 (x_train, y_train), (x_test, y_test) = cifar10.load_data() elif dset == 'fashion': img_rows, img_cols = 28, 28 n_channels = 1 (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() elif dset == 'mnist-clut': img_rows, img_cols = 60, 60 # the data, split between train and test sets #folder='/media/home/rdata/image/' folder = '/home/btek/datasets/image/' data = np.load(folder + "mnist_cluttered_60x60_6distortions.npz", allow_pickle=True) y_trn = data['y_train'] y_val = data['y_valid'] y_tst = data['y_test'] x_train, y_train = data['x_train'], np.argmax(y_trn, axis=-1) x_valid, y_valid = data['x_valid'], np.argmax(y_val, axis=-1) x_test, y_test = data['x_test'], np.argmax(y_tst, axis=-1) x_train = np.vstack((x_train, x_valid)) y_train = np.concatenate((y_train, y_valid)) n_channels = 1 normalize_data = False # this dataset is already somehow normalized #decay_epochs =[e_i*30,e_i*100] elif dset == 'lfw_faces': from sklearn.datasets import fetch_lfw_people lfw_people = fetch_lfw_people(min_faces_per_person=20, resize=0.4) # introspect the images arrays to find the shapes (for plotting) n_samples, img_rows, img_cols = lfw_people.images.shape n_channels = 1 X = lfw_people.data n_features = X.shape[1] # the label to predict is the id of the person y = lfw_people.target target_names = lfw_people.target_names num_classes = target_names.shape[0] from sklearn.model_selection import train_test_split #X -= X.mean() #X /= X.std() #split into a training and testing set x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.25) if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], n_channels, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], n_channels, img_rows, img_cols) input_shape = (n_channels, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, n_channels) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, n_channels) input_shape = (img_rows, img_cols, n_channels) ''' why I have written this?? BTEK if(n_channels==1): x_train = np.repeat(x_train,3, axis=3) x_test = np.repeat(x_test,3, axis=3) n_channels=3 input_shape = (img_rows, img_cols, n_channels) ''' num_classes = np.shape(np.unique(y_train))[0] x_train = x_train.astype('float32') x_test = x_test.astype('float32') if normalize_data: #Simple norm 0.1 #x_train /= 255 #x_test /= 255 #Standard norm mean 0 , std 1, per input #this normalization is very bad. BTEK for IMAGES #trn_mn = np.mean(x_train, axis=0) this normalization is very bad. BTEK for IMAGES #trn_std = np.std(x_train, axis=0) this normalization is very bad. BTEK for IMAGES # Standard for mean 127 and std per image. # This does not have 0 mean but some negative value # Std is 1.0 some paper results wer taken by this I guess # trn_mn = np.mean(x_train) # trn_std = np.std(x_train) # x_train -= 127.0 # I use this because other normalizations do not create symmetric distribution. # x_test -= 127.0 # x_train/=(trn_std+1e-7) # x_test/=(trn_std+1e-7) # print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train)) # print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test)) # Standard for mean 127 and std per image. # This does not have 0 mean and std is not 1.0 # Std is # x_train /= (255/4) # x_test /= (255/4) # x_train -= 2.0 # x_test -= 2.0 # print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train)) # print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test)) # CHANGİNG THİS aug2020 FOR FACES TEST x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 trn_mn = np.mean(x_train, axis=0) x_train -= trn_mn x_test -= trn_mn print('x_train shape:', x_train.shape) print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train)) print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test)) # non-zero normalization. # trn_mn = np.mean(x_train[np.nonzero(x_train)]) # trn_std = np.std(x_train[np.nonzero(x_train)]) # x_train[np.nonzero(x_train)] -= trn_mn # x_test[np.nonzero(x_test)] -= trn_mn # print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train)) # print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test)) # x_train/=(trn_std+1e-7) # x_test/=(trn_std+1e-7) # print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train)) # print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test)) print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) return x_train, y_train, x_test, y_test, input_shape, num_classes