This file contains hidden or 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 tensorflow.keras.models import Model | |
| from tensorflow.keras.layers import Input,Dense,BatchNormalization,Concatenate,GaussianNoise | |
| from tensorflow.keras.optimizers import Nadam | |
| #define latent dimension size | |
| latent_dim = int(np.ceil(np.log(len(train_df)*len(data)))) | |
| #function for building generator network | |
| def compile_generator(): | |
This file contains hidden or 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 sklearn.preprocessing import MinMaxScaler | |
| #specify target label | |
| target = 'loan_condition' | |
| data = [name for name in df.columns if name != target] | |
| numeric_data = [] | |
| string_data = [] | |
| tokenizers = {} |
This file contains hidden or 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 | |
| import pandas as pd | |
| #import loan dataset | |
| df = pd.read_csv('loan.csv').dropna(axis=1,how='any') | |
| #convert loan grades to numerical values | |
| df['sub_grade'] = df['sub_grade'].str.slice(start=1).astype(int) | |
| grade_dict = {k:i for i,k in enumerate(['A', 'B', 'C', 'D', 'E', 'F', 'G'])} | |
| term_dict = {k:i for i,k in enumerate(['36 months', '60 months'])} |
This file contains hidden or 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
| #initialize our generators; specifying data directories, batch size, and dimension threshold | |
| train_image_directory = 'imagenette2/train' | |
| test_image_directory = 'imagenette2/val' | |
| n_classes = 10 | |
| batch_size = 16 | |
| max_dimension = 512 | |
| #create generators for training and generating |
This file contains hidden or 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 | |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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. |