def calc_EIstep(X_init, Y_init, batchsize, normalize, savepath, kernel): space = GPyOpt.core.task.space.Design_space(get_domain(normalize=normalize), None) if kernel == "RBF": model_gp = GPyOpt.models.GPModel(kernel=GPy.kern.RBF(input_dim=X_init.shape[1], ARD=True),ARD=True,verbose=False) elif kernel == "matern52": model_gp = GPyOpt.models.GPModel(ARD=True, verbose=False) objective = GPyOpt.core.task.SingleObjective(None) acquisition_optimizer = GPyOpt.optimization.AcquisitionOptimizer(space) acquisition_EI = GPyOpt.acquisitions.AcquisitionEI(model_gp, space, acquisition_optimizer, jitter=0) acquisition = GPyOpt.acquisitions.LP.AcquisitionLP(model_gp, space, acquisition_optimizer,acquisition_EI) evaluator = GPyOpt.core.evaluators.LocalPenalization(acquisition, batch_size=batchsize) bo_EI = GPyOpt.methods.ModularBayesianOptimization( model=model_gp, space=space, objective=objective, acquisition=acquisition, evaluator=evaluator, X_init=X_init, Y_init=Y_init, normalize_Y=True ) nextX = bo_EI.suggest_next_locations() if normalize: nextX = rescale(nextX) with open( savepath+"/model/EI_j"+".pkl", "wb") as f: pickle.dump(bo_EI, f, protocol=2) return nextX
def main(): root = Tk() root.title('Rescaling') W, H = 700, 500 PAD = 50 canvas = Canvas(root, width=W, height=H, background='black') canvas.grid(row=0, column=1) #triangle = make_random_triangle(W, H) triangle = [ Point(W / 2, PAD), Point(PAD, H - PAD), Point(W - PAD, H - PAD) ] scale_factor = 0.8 for i in range(10): color1 = random_color() color2 = random_color() t_list = [point.as_list() for point in triangle] print(t_list) canvas.create_polygon(t_list, fill=color1, outline=color2, width=2) triangle = rescale(triangle, scale_factor) [print(point) for point in triangle] root.mainloop()
def generate(output_directory, ckpt_path, ckpt_epoch, n, T, beta_0, beta_T, unet_config): """ Generate images using the pretrained UNet model Parameters: output_directory (str): output generated images to this path ckpt_path (str): path of the checkpoints ckpt_epoch (int or 'max'): the pretrained model checkpoint to be loaded; automitically selects the maximum epoch if 'max' is selected n (int): number of images to generate T (int): the number of diffusion steps beta_0 and beta_T (float): diffusion parameters unet_config (dict): dictionary of UNet parameters """ # Compute diffusion hyperparameters Beta = torch.linspace(beta_0, beta_T, T).cuda() Alpha = 1 - Beta Alpha_bar = torch.ones(T).cuda() Beta_tilde = Beta + 0 for t in range(T): Alpha_bar[t] *= Alpha[t] * Alpha_bar[t - 1] if t else Alpha[t] if t > 0: Beta_tilde[t] *= (1 - Alpha_bar[t - 1]) / (1 - Alpha_bar[t]) Sigma = torch.sqrt(Beta_tilde) # Predefine model net = UNet(**unet_config).cuda() print_size(net) # Load checkpoint if ckpt_epoch == 'max': ckpt_epoch = find_max_epoch(ckpt_path, 'unet_ckpt') model_path = os.path.join(ckpt_path, 'unet_ckpt_' + str(ckpt_epoch) + '.pkl') try: checkpoint = torch.load(model_path, map_location='cpu') print('Model at epoch %s has been trained for %s seconds' % (ckpt_epoch, checkpoint['training_time_seconds'])) net = UNet(**unet_config) net.load_state_dict(checkpoint['model_state_dict']) net = net.cuda() except: raise Exception('No valid model found') # Generation time0 = time.time() X_gen = sampling(net, (n, 3, 256, 256), T, Alpha, Alpha_bar, Sigma) print('generated %s samples at epoch %s in %s seconds' % (n, ckpt_epoch, int(time.time() - time0))) # Save generated images for i in range(n): save_image(rescale(X_gen[i]), os.path.join(output_directory, 'img_{}.jpg'.format(i))) print('saved generated samples at epoch %s' % ckpt_epoch)
def get_lung_mask(img_arr_3d): """ Return an image mask that only highlights the lungs in the given image. :param img_arr_3d: 3D image array to mask as numpy array. :return: Numpy array of 1s and 0s. 1 means that a lung is at the corresponding location in the given image. """ img_arr = img_arr_3d img_arr = util.standardize_and_remove_bg(img_arr) rescaled = util.rescale(img_arr, min=0, max=255).astype('uint8') # Use Otsu's method to get black and white image (differentiates lung and bone). threshold = skimage.filters.threshold_otsu(rescaled) binary = rescaled < threshold # Morphological opening to get rid of graininess mask = skimage.morphology.binary_opening(binary, skimage.morphology.ball(2)) # Morphological closing to get rid of some black specks in lung mask = skimage.morphology.binary_closing(mask, skimage.morphology.ball(5)) # Connected threshold to get only lungs and not the background seed = np.zeros(mask.shape) seed_radius = 4 left_lung_position = tuple( int(mask.shape[i] * LEFT_LUNG_GUESS[i]) for i in range(len(mask.shape))) right_lung_position = tuple( int(mask.shape[i] * RIGHT_LUNG_GUESS[i]) for i in range(len(mask.shape))) for seed_coord in [left_lung_position, right_lung_position]: z1 = seed_coord[0] - seed_radius y1 = seed_coord[1] - seed_radius x1 = seed_coord[2] - seed_radius z2 = seed_coord[0] + seed_radius y2 = seed_coord[1] + seed_radius x2 = seed_coord[2] + seed_radius seed[z1:z2, y1:y2, x1:x2] = mask[z1:z2, y1:y2, x1:x2] print(seed_coord, 1 in seed[z1:z2, y1:y2, x1:x2]) mask = skimage.morphology.reconstruction(seed, mask) # Morphological closing to get rid of all black specks in lung mask = skimage.morphology.binary_closing(mask, skimage.morphology.ball(7)) # Dilate by 1 to get edges of lung mask = skimage.morphology.binary_dilation(mask, skimage.morphology.ball(1)) return mask
def tone_operator(l2E, l_remap, saturation, numtiles): """ The main algorithm is CLAHE: contrast limited adaptive histogram equalization preprocessing: convert RGB to XYZ to Lab postprocessing: back to RGB """ lab = util.srgb2lab(l2E) lab[:, :, 0] = util.rescale(lab[:, :, 0]) # lab[:, :, 0] /= 100 lab[:, :, 0] = clahe.hist_equalize(lab[:, :, 0], numtiles) lab[:, :, 0] = imadjust( lab[:, :, 0], range_in=l_remap, range_out=(0, 1), gamma=1.5) * 100 lab[:, :, 1:] = lab[:, :, 1:] * saturation I = util.lab2srgb(lab) return I
def update(self, state, action, nextState, reward): """ Update Q-Function based on transition """ if self.model is None: self.initModel(state) self.remember(state, action, util.rescale(reward, -510, 1000, -1, 1), nextState) if len(self.replayMemory) < 1000: #self.minReplayMemorySize: return #no update of parameters till enough experience gathered? rawBatch = self.sampleReplayBatch(self.batchSize) trainingBatchQStates = [] #data input for NN trainingBatchTargetQValues = [] #qvalue corresponding to these data for aQState, anAction, aReward, aNextQState, isNextStateFinal in rawBatch: actionsQValues = self.model.predict(np.array([aQState]))[0] nextActionsQValues = self.model.predict(np.array([aNextQState]))[0] maxNextActionQValue = max( nextActionsQValues) #max over the actions # Update rule if isNextStateFinal: updatedQValueForAction = aReward else: updatedQValueForAction = (aReward + self.discount * maxNextActionQValue) targetQValues = actionsQValues.copy() targetQValues[Directions.getIndex( anAction)] = updatedQValueForAction trainingBatchQStates.append(aQState) trainingBatchTargetQValues.append(targetQValues) self.model.train_on_batch(x=np.array(trainingBatchQStates), y=np.array(trainingBatchTargetQValues)) self.updateCount += 1 self.epsilon = max( self.finalEpsilon, 1.00 - float(self.updateCount) / float(self.epsilonSteps))
def lognormal(E): """ log2(E). remove 0s. return log2E, has_nonzero """ mask = (E != 0) if np.any(mask): min_nonzero = np.min(E[mask]) E[np.logical_not(mask)] = min_nonzero l2E = util.rescale(np.log2(E)) has_nonzero = True else: # all elements are zero l2E = np.zeros_like(E) has_nonzero = False return l2E, has_nonzero
def main(argv): print("Start Main") # Set arguments: Save_Dir Structure Learning_Rate Earling_Stoping Batch_Size Data_Dir data_dir = FLAGS.data_dir save_dir = FLAGS.save_dir learning_rate = FLAGS.lr early_stop = FLAGS.early_stop batch_size = FLAGS.batch_size reg_coeff = FLAGS.reg_coeff split = FLAGS.split master = FLAGS.master checkpoint_path = FLAGS.checkpoint_path input_dir = FLAGS.input_dir output_dir = FLAGS.output_dir image_width = FLAGS.image_width image_height = FLAGS.eps num_classes = FLAGS.num_classes eps = FLAGS.eps batch_shape = [batch_size, image_height, image_width, 3] input_shape = [image_height, image_width, 3] tf.logging.set_verbosity(tf.logging.INFO) def model_arch(): model = Sequential() model.add( Conv2D(50, kernel_size=(5, 5), activation='relu', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(100, (5, 5), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(200, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(400, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(200, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) return model model = model_arch #load training data imgs, labels, names = util.load_training_images('tiny-imagenet-200/train/') print("Training Images Loaded") #retrype and resize training data imgs = imgs[0:100] labels = labels[0:100] names = names[0:100] imgs_large = np.ndarray(shape=[imgs.shape[0], 299, 299, 3]) for i in range(imgs.shape[0]): imgs_large[i, :, :, :] = util.rescale(imgs[i]) imgs_large = imgs_large.astype('uint8') imgs_noisy = np.ndarray(shape=imgs_large.shape) for i in range(imgs_large.shape[0]): imgs_noisy[i, :, :, :] = util.noisy(1, imgs_large[i]) imgs_noisy = imgs_noisy.astype('uint8') sub_imgs, sub_labels = util.subsample(imgs_noisy, labels) batch_shape = [20, 299, 299, 3] num_classes = 200
def main(argv): print("Start Main") # Set arguments: Save_Dir Structure Learning_Rate Earling_Stoping Batch_Size Data_Dir data_dir = FLAGS.data_dir save_dir = FLAGS.save_dir learning_rate = FLAGS.lr early_stop = FLAGS.early_stop batch_size = FLAGS.batch_size epochs = FLAGS.epochs reg_coeff = FLAGS.reg_coeff split = FLAGS.split master = FLAGS.master checkpoint_path = FLAGS.checkpoint_path input_dir = FLAGS.input_dir output_dir = FLAGS.output_dir image_width = FLAGS.image_width image_height = FLAGS.eps num_classes = FLAGS.num_classes eps = FLAGS.eps batch_shape = [batch_size, image_height, image_width, 3] input_shape = [image_height, image_width, 3] num_ens = FLAGS.num_ens tf.logging.set_verbosity(tf.logging.INFO) def model_arch(): model = Sequential() model.add(Conv2D(50, kernel_size=(5, 5), activation='relu', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(100, (5, 5), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(200, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(400, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(200, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) return model model = model_arch #load training data x_train,y_train,train_names = util.load_training_images('tiny-imagenet-200/train/') print("Training Images Loaded") x_test,y_test,test_names = util.load_training_images('tiny-imagenet-200/test/') print("Testing Images Loaded") #retrype and resize training data x_train = x_train[0:100] y_train = y_train[0:100] train_names = train_names[0:100] x_train_large = np.ndarray(shape= [x_train.shape[0],299,299,3]) for i in range(x_train.shape[0]): x_train_large[i,:,:,:] = util.rescale(x_train[i]) x_train_large=x_train_large.astype('uint8') x_train_noisy = np.ndarray(shape= x_train_large.shape) for i in range(x_train_large.shape[0]): x_train_noisy[i,:,:,:] = util.noisy(1,x_train_large[i]) x_train_noisy=x_train_noisy.astype('uint8') x_train_sub,y_train_sub = util.subsample(x_train_noisy,y_train) batch_shape = [20, 299, 299, 3] num_classes = 200 y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) sess = tf.Session() keras.backend.set_session(sess) x_noisy = util.add_gaussian_noise(x_train,0,64) #Add gaussian noise to all images preds_ens = np.zeros((x_test.shape[0],10)) #variable to store the predictions of each model in the ensemble (10) max_vote_ens = np.zeros(x_test.shape[0]) #variable to store Majority vote from all models in ensemble for i in range(num_ens): model = model_arch() #Build a new model architecture for every model in the ensemble x_train_sub,y_train_sub = util.subsample(x_train_noisy,y_train) #subsample from the entire data, bagging model.fit(x_train_sub, y_train_sub, batch_size=batch_size,epochs=epochs,verbose=1) #train the model model.save("models/imgnet/"+str(i)+".h5") #save the model ans = sess.run(tf.argmax(model.predict(x_test),axis=1)) #get the predictions of the model preds_ens[:,i]= ans.reshape((x_test.shape[0])) #store the predictions of this particular model(i) in ith column of pred_ens variable del model #erase the model #Now the variable pred_ens consists of the predictions of all test_data for each model in ensemble. #ith column contains predictions of ith model. #go through every row print("Ensemble method Clean") ens_acc = np.zeros(num_ens) for i in range(num_ens): for j in range(preds_ens.shape[0]): b= Counter(preds_ens[j][0:i+1]) #get the entire row which consists of predictions for that particular instance from all models. max_vote_ens[j] = b.most_common(1)[0][0] #get the maximum vote i.e which number has more frequency. ens_acc_i = sess.run(tf.reduce_mean(tf.cast(tf.equal(max_vote_ens, tf.argmax(y_test, axis=1)) , tf.float32))) ens_acc[i] = ens_acc_i #accuracy of ensemble #TODO print the nonperturbed test accuracy to the output file. print("Accuracy : " + str(np.mean(ens_acc))) #Build a model for normal training on the entire noisy data. model = model.model_arch() model.fit(x_train_noisy, y_train, batch_size=batch_size, epochs=epochs, verbose=1) acc = model.evaluate(x_test, y_test, verbose=0) acc_noisy_normal = acc[1] #accuracy of normal model on noisy train data del model #Build a new model for normal training (without ensemble) on entire train data (with out bagging and noise). model = model_arch() model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1) acc = model.evaluate(x_test, y_test, verbose=0) model.save("models/imgnet/original_model.h5") #accuracy of normal model acc_normal = acc[1] print("accuracy of normal model : " + str(acc_normal)) print("accuracy of normal model on noisy train data : " + str(acc_noisy_normal)) #generate fgsm adversarial examples on test_data adv_fgsm = util.fgsm_attack(x_test,model,sess) acc_fgsm = model.evaluate(adv_fgsm, y_test, verbose=0) acc_fgsm = acc_fgsm[1] print("accuracy of normal model on fgsm adversarial examples : " + str(acc_fgsm)) #generate bim adversarial examples on test_data adv_bim = util.bim_attack(x_test,model,sess) acc_bim = model.evaluate(adv_bim,y_test,verbose=0) acc_bim = acc_bim[1] #accuracy of normal model on bim adversarial examples print("accuracy of normal model on bim adversarial examples : " + str(acc_bim)) #generate lbfgs adversarial examples on test_data # The target is chosen as 6 adv_lbfgs = util.lbfgs_attack(x_test,model,sess,6) acc_lbfgs = model.evaluate(adv_lbfgs,y_test,verbose=0) acc_lbfgs = acc_lbfgs[1] #accuracy of normal model on lbfgs adversarial examples print("accuracy of normal model on lbfgs adversarial examples : " + str(acc_lbfgs)) preds_ens_fgsm = np.zeros((x_test.shape[0],10)) #variable to store the predictions of each model in the ensemble (10) for fgsm adversarial examples max_vote_ens_fgsm = np.zeros(x_test.shape[0]) #variable to store Majority vote from all models in ensemble for fgsm adversarial examples preds_ens_bim = np.zeros((x_test.shape[0],10)) #variable to store the predictions of each model in the ensemble (10) for bim adversarial examples max_vote_ens_bim = np.zeros(x_test.shape[0]) #variable to store Majority vote from all models in ensemble for bim adversarial examples preds_ens_lbfgs = np.zeros((x_test.shape[0],10)) #variable to store the predictions of each model in the ensemble (10) for lbfgs adversarial examples max_vote_ens_lbfgs = np.zeros(x_test.shape[0]) #variable to store Majority vote from all models in ensemble for lbfgs adversarial examples del model for i in range(num_ens): model = load_model("models/"+str(i)+".h5") #get predictions of model i for fgsm adversarial examples ans = sess.run(tf.argmax(model.predict(adv_fgsm),axis=1)) preds_ens_fgsm[:,i]= ans.reshape((adv_fgsm.shape[0])) #get predictions of model i for bim adversarial examples ans = sess.run(tf.argmax(model.predict(adv_bim),axis=1)) preds_ens_bim[:,i]= ans.reshape((adv_bim.shape[0])) #get predictions of model i for lbfgs adversarial examples ans = sess.run(tf.argmax(model.predict(adv_lbfgs),axis=1)) preds_ens_lbfgs[:,i]= ans.reshape((adv_lbfgs.shape[0])) del model print("Now the variable pred_ens consists of the predictions of all fgsm adversarial test_data for each model in ensemble.") #ith column contains predictions of ith model. #go through every row ens_acc_fgsm = np.zeros(num_ens) for i in range(num_ens): for j in range(preds_ens_fgsm.shape[0]): b= Counter(preds_ens_fgsm[j][0:i+1]) #get the entire row which consists of predictions for that particular instance from all models. max_vote_ens_fgsm[j] = b.most_common(1)[0][0] #get the maximum vote i.e which number has more frequency. #accuracy of ensemble ens_acc_fgsm_i = sess.run(tf.reduce_mean(tf.cast(tf.equal(max_vote_ens_fgsm, tf.argmax(y_test, axis=1)) , tf.float32))) ens_acc_fgsm[i] = ens_acc_fgsm_i print(str(np.mean(ens_acc_fgsm))) print("Now the variable pred_ens consists of the predictions of all bim adversarial test_data for each model in ensemble.") #ith column contains predictions of ith model. #go through every row ens_acc_bim = np.zeros(num_ens) for i in range(num_ens): for j in range(preds_ens_bim.shape[0]): b= Counter(preds_ens_bim[j][0:i+1]) max_vote_ens_bim[j] = b.most_common(1)[0][0] #accuracy of ensemble on bim_adv ens_acc_bim_i = sess.run(tf.reduce_mean(tf.cast(tf.equal(max_vote_ens_bim, tf.argmax(y_test, axis=1)) , tf.float32))) ens_acc_bim[i] = ens_acc_bim_i print(str(np.mean(ens_acc_bim))) print("Now the variable pred_ens consists of the predictions of all lbfgs adversarial test_data for each model in ensemble.") #ith column contains predictions of ith model. #go through every row ens_acc_lbfgs = np.zeros(num_ens) for i in range(num_ens): for i in range(preds_ens_lbfgs.shape[0]): b= Counter(preds_ens_lbfgs[j][0:i+1]) max_vote_ens_lbfgs[j] = b.most_common(1)[0][0] #accuracy of ensemble on lbfgs_adv ens_acc_lbfgs_i = sess.run(tf.reduce_mean(tf.cast(tf.equal(max_vote_ens_lbfgs, tf.argmax(y_test, axis=1)) , tf.float32))) ens_acc_lbfgs[i] = ens_acc_lbfgs_i print(str(np.mean(ens_acc_lbfgs)))
#%% # 'images/r5_5pct.png', # 'images/r5_rupture.png' # ims2 = [] for path in [ #'images/r5_0pct.png', #'images/r5_2pct.png']: 'images/molybdenum1.png', 'images/molybdenum3.png' ]: # #'images/rene88_2.png', #'images/renen4_2.png' ims2.append(rescale(skimage.io.imread(path, as_grey=True), 0.0, 1.0)) for im in ims2: plt.imshow(im) plt.show() #%% reload(hog) translations = [50, 100] for offset in translations: histograms = [] histograms2 = [] filtrd_ims = []
#%% # 'images/r5_5pct.png', # 'images/r5_rupture.png' # ims2 = [] for path in ['images/r5_0pct.png', 'images/r5_2pct.png']: #'images/molybdenum1.png', # 'images/molybdenum3.png' #]: # #'images/rene88_2.png', #'images/renen4_2.png' ims2.append(rescale(skimage.io.imread(path, as_grey = True), 0.0, 1.0)) for im in ims2: plt.imshow(im) plt.show() #%% reload(hog) histograms = [] histograms2 = [] filtrd_ims = [] for i, im in enumerate(ims2): filtrd = scipy.ndimage.filters.gaussian_filter(im, 2.0)
#%% reload(hog) stacks = [] stacks_rotated = [] for stack in range(10): ims = skimage.io.imread_collection('/home/bbales2/rafting/rafting2ah5/images_{0}/*.png'.format(stack))#nrafting2a ims = ims.concatenate() #ims2 = [] #for im in ims: # ims2.append(skimage.transform.rescale(im, 3.0)) #ims = numpy.array(ims2) ims = rescale(ims, 0.0, 1.0) filtrd = scipy.ndimage.filters.gaussian_filter(ims, 1.0) dzs, dys, dxs = numpy.gradient(filtrd) dys = -dys thetas, phis, histogram = hog.build3dHist(dxs, dys, dzs, 180, 360) for j in range(0, histogram.shape[0]): angle = j * (180.0 / float(histogram.shape[0])) * numpy.pi / 180.0 histogram[j, :] /= -2 * numpy.pi * (numpy.cos(angle + (180.0 / float(histogram.shape[0])) * numpy.pi / 180.0) - numpy.cos(angle))#numpy.max(histogram[i, :]) plt.imshow(histogram) plt.colorbar() plt.show()
return filter(lambda x: x[4] == label, data) data = select_only(label, data) def remove_label(data): return map(lambda x: x[:-1], data) data = remove_label(data) data = util.to_number(data) data = util.to_list(data) data = util.rescale(data) # data x = list(map(lambda x: x[features[0]], data)) y = list(map(lambda x: x[features[1]], data)) nullfmt = NullFormatter() # no labels # definitions for the axes left, width = 0.1, 0.65 bottom, height = 0.1, 0.65 bottom_h = left_h = left + width + 0.02 rect_scatter = [left, bottom, width, height]
def main(argv): print("Start Main") # Set arguments: Save_Dir Structure Learning_Rate Earling_Stoping Batch_Size Data_Dir data_dir = FLAGS.data_dir save_dir = FLAGS.save_dir learning_rate = FLAGS.lr early_stop = FLAGS.early_stop batch_size = FLAGS.batch_size epochs = FLAGS.epochs reg_coeff = FLAGS.reg_coeff split = FLAGS.split master = FLAGS.master checkpoint_path = FLAGS.checkpoint_path input_dir = FLAGS.input_dir output_dir = FLAGS.output_dir image_width = FLAGS.image_width image_height = FLAGS.eps num_classes = FLAGS.num_classes eps = FLAGS.eps batch_shape = [batch_size, image_height, image_width, 3] input_shape = [image_height, image_width, 3] num_ens = FLAGS.num_ens tf.logging.set_verbosity(tf.logging.INFO) def model_arch(): model = Sequential() model.add( Conv2D(50, kernel_size=(5, 5), activation='relu', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(100, (5, 5), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(200, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(400, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(200, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) return model model = model_arch #load training data x_train, y_train, train_names = util.load_training_images( 'tiny-imagenet-200/train/') print("Training Images Loaded") x_test, y_test, test_names = util.load_training_images( 'tiny-imagenet-200/test/') print("Testing Images Loaded") #retrype and resize training data x_train = x_train[0:100] y_train = y_train[0:100] train_names = train_names[0:100] x_train_large = np.ndarray(shape=[x_train.shape[0], 299, 299, 3]) for i in range(x_train.shape[0]): x_train_large[i, :, :, :] = util.rescale(x_train[i]) x_train_large = x_train_large.astype('uint8') x_train_noisy = np.ndarray(shape=x_train_large.shape) for i in range(x_train_large.shape[0]): x_train_noisy[i, :, :, :] = util.noisy(1, x_train_large[i]) x_train_noisy = x_train_noisy.astype('uint8') x_train_sub, y_train_sub = util.subsample(x_train_noisy, y_train) batch_shape = [20, 299, 299, 3] num_classes = 200 y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) sess = tf.Session() keras.backend.set_session(sess) #-----------------------------------Adversarial Training-------------------------------------------------------------- #first adversarial examples are generated using train_data, then the model is trained on train_data+adv_train_data. #Then the model is tested on normal test_data, then the model is tested on adversarial_test_data. #So, we are generating the adversarial examples twice both on train and test data. model = load_model("models/imgnet/original_model.h5") wrap = KerasModelWrapper(model) #generate adversarial examples on train data. adv_fgsm_train = util.fgsm_attack(x_train, model, sess) adv_bim_train = util.bim_attack(x_train, model, sess) adv_lbfgs_train = util.lbfgs_attack(x_train, model, sess, 6) train_plus_adv_fgsm = np.concatenate([x_train, adv_fgsm_train]) y_train_plus_adv_fgsm = np.concatenate([y_train, y_train]) train_plus_adv_bim = np.concatenate([x_train, adv_bim_train]) y_train_plus_adv_bim = np.concatenate([y_train, y_train]) train_plus_adv_lbfgs = np.concatenate([x_train, adv_lbfgs_train]) y_train_plus_adv_lbfgs = np.concatenate([y_train, y_train]) del model print("FGSM TRAINING") #build a fresh model for fgsm training model = model_arch() wrap = KerasModelWrapper(model) model.fit(train_plus_adv_fgsm, y_train_plus_adv_fgsm, batch_size=batch_size, epochs=epochs, verbose=1) model.save("models/imgnet/fgsm_model.h5") fgsm_acc_train = model.evaluate(x_test, y_test, verbose=0) fgsm_acc_train[ 1] #Accuracy of adversarially trained model on clean examples #generate adversarial examples for adversarially trained model on test_data adv_fgsm_test = util.fgsm_attack(x_test, model, sess) fgsm_adv_acc_train = model.evaluate(adv_fgsm_test, y_test, verbose=0) fgsm_adv_acc_train[ 1] #Accuracy of adversarially trained model on adv_test images del model print("BIM TRAINING") #BIM TRAINING #build a fresh model for bim training model = model_arch() wrap = KerasModelWrapper(model) model.fit(train_plus_adv_bim, y_train_plus_adv_bim, batch_size=batch_size, epochs=epochs, verbose=1) bim_acc_train = model.evaluate(x_test, y_test, verbose=0) print("Accuracy of adversarially trained model on clean examples\n" + str(bim_acc_train[1])) #generate adversarial examples for adversarially trained model on test_data adv_bim_test = util.bim_attack(x_test, model, sess) bim_adv_acc_train = model.evaluate(adv_bim_test, y_test, verbose=0) print("Accuracy of adversarially trained model on adv_test images\n" + str(bim_adv_acc_train[1])) del model print("LBFGS TRAINING") #build a fresh model for lbfgs training model = model_arch() wrap = KerasModelWrapper(model) model.fit(train_plus_adv_lbfgs, y_train_plus_adv_lbfgs, batch_size=batch_size, epochs=epochs, verbose=1) print("Accuracy of adversarially trained model on clean examples") lbfgs_acc_train = model.evaluate(x_test, y_test, verbose=0) print(str(lbfgs_acc_train[1])) print("Accuracy of adversarially trained model on lbfgs examples") lbfgs_acc_train[1] adv_lbfgs_test = util.lbfgs_attack(x_test, model, sess, 6) lbfgs_adv_acc_train = model.evaluate(adv_lbfgs_test, y_test, verbose=0) print(str(lbfgs_adv_acc_train[1]) ) #Accuracy of adversarially trained model on adv_test images del model