This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy.random import choice, normal | |
np.random.seed(8) | |
from tensorflow import set_random_seed | |
set_random_seed(8) | |
from tensorflow.python.keras.layers import Input, BatchNormalization,Conv2D, MaxPooling2D | |
from tensorflow.python.keras.layers import Dropout, Reshape | |
from tensorflow.python.keras.layers import Dense, Flatten | |
from tensorflow.python.keras.models import Model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
#function for getting coordinates given an angle | |
def get_cartesian_coords(nums): | |
theta = nums | |
x = np.cos(theta) | |
y = np.sin(theta) | |
return [x,y] | |
#generate random starting coordinate | |
angle = np.random.uniform(-np.pi, np.pi) | |
coordinates = get_cartesian_coords(random_angle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from keras.datasets import mnist | |
from keras.models import Sequential, Model | |
from keras.layers import Dense, Dropout, Flatten ,Input | |
from keras.layers import Conv2D, MaxPooling2D, Reshape, Add | |
from keras.metrics import categorical_accuracy | |
from keras.regularizers import l1_l2, l2, l1 | |
from keras.callbacks import EarlyStopping, ModelCheckpoint | |
from keras.layers import Activation | |
from keras.utils.generic_utils import get_custom_objects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from keras.datasets import mnist | |
from keras.models import Model | |
from keras.layers import Dense, Dropout, Flatten ,Input | |
from keras.layers import Conv2D, MaxPooling2D, Reshape, Add | |
from keras.metrics import categorical_accuracy | |
from keras.regularizers import l1_l2, l2, l1 | |
from keras.callbacks import EarlyStopping, ModelCheckpoint | |
from keras.layers import Activation | |
from keras.utils.generic_utils import get_custom_objects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#select image to create an adversarial example from | |
img = x_train[0:1] | |
plt.imshow(img.reshape((28,28)),vmin=0., vmax=1.) | |
plt.show() | |
#varify accurate classificaiton | |
prediction = mnist_model.predict(img)[0] | |
print(prediction) | |
#applying random noise does not fool the classifier | |
quantized_noise = np.round(np.random.normal(loc=0.0, scale=0.3, size=img.shape) * 255.) / 255. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#custom activation function for keeping adversarial pixel values between 0.0 and 1.0 | |
def clip(x): | |
return K.clip(x, 0.0, 1.0) | |
#custom loss funciton for non-targeted misclassification | |
def negative_categorical_crossentropy(yTrue,yPred): | |
return 0.0 - K.categorical_crossentropy(yTrue,yPred) | |
#add custom objects to dictionary | |
get_custom_objects().update({'clip': Activation(clip)}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Model | |
from keras.layers import Dense, Input, Reshape | |
from keras.regularizers import Regularizer | |
from keras.utils.generic_utils import get_custom_objects | |
from keras import backend as K | |
from keras.applications.inception_v3 import InceptionV3 | |
from keras.initializers import RandomUniform | |
from keras.preprocessing.image import array_to_img, save_img | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#imports | |
import tensorflow as tf | |
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2,preprocess_input | |
from tensorflow.keras.layers import Input,GlobalMaxPooling2D,Dense | |
from tensorflow.keras.models import Model | |
from tensorflow.keras.preprocessing.image import img_to_array,load_img | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#set up MobileNet GlobalMaxPooling and unsepcified input resolution | |
inputs = Input(shape=(None,None,3)) | |
net = MobileNetV2(include_top=False, alpha=0.35, weights='imagenet', input_tensor=inputs, classes=n_classes) | |
net = GlobalMaxPooling2D()(net.output) | |
outputs = Dense(n_classes,activation='softmax')(net) | |
model = Model(inputs=inputs,outputs=outputs) | |
model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy',metrics=['sparse_categorical_accuracy']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#data generator class; yields batches of data for training/testing | |
class ImageGenerator(): | |
def __init__(self, directory, batch_size=16, shuffle=False, max_dimension=None): | |
self.directories = directory | |
self.batch_size = batch_size | |
self.shuffle = shuffle | |
self.max_dimension = max_dimension | |
OlderNewer