Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 torch | |
import torch.nn as nn | |
from torch.autograd import Variable, Function | |
import random | |
class WARP(Function): | |
''' | |
autograd function of WARP loss | |
''' | |
@staticmethod |
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
# Define a function to train SVM | |
def train_svm(data, labels, kernel, C, gamma, degree, coef0): | |
"""A generic SVM training function, with arguments based on the chosen kernel.""" | |
if kernel == 'linear': | |
model = SVC(kernel=kernel, C=C) | |
elif kernel == 'poly': | |
model = SVC(kernel=kernel, C=C, degree=degree, coef0=coef0) | |
elif kernel == 'rbf': | |
model = SVC(kernel=kernel, C=C, gamma=gamma) | |
else: |
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.applications.inception_v3 import InceptionV3 | |
from keras.models import Model | |
from keras.layers import Dense, GlobalAveragePooling2D | |
base_model = InceptionV3(weights='imagenet', include_top=False) | |
# Adding a 'top' which outputs 2 categories | |
x = base_model.output | |
x = GlobalAveragePooling2D()(x) |
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
def make_xy(model, path): | |
#making the X values | |
for z in range(len(model)): | |
gen = image.ImageDataGenerator() | |
batches = gen.flow_from_directory(path, | |
target_size = (224, 224), | |
batch_size = 32, |
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
def ResNet_inv(): | |
RN_mean = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((3,1,1)) | |
def RN_preprocess(x, RN_mean): | |
x = x - np.asarray(RN_mean) | |
return x[:, ::-1] # reverse axis bgr->rgb | |
img_input = Input(shape =(3,224,224)) | |
bn_axis = 1 | |
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
def vgg_bn(dropout = 0.5, BN = True): | |
#returns a vgg model with batch normalization and a defined dropout | |
#the weights are adjusted to whichever dropout you select | |
prev_vgg = load_model('/home/ubuntu/invasive_vgg.model') | |
prev_layers = prev_vgg.layers[:-5] | |
new_vgg = Sequential(prev_layers) | |
def new_FCL(model, num_filters, dropout): | |
# In Keras, a fully connected layer is called 'Dense' |
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.applications.resnet50 import ResNet50 | |
ResNet = ResNet50(weights='imagenet') |