Skip to content

Instantly share code, notes, and snippets.

View gabrieltseng's full-sized avatar

Gabriel Tseng gabrieltseng

View GitHub Profile
@gabrieltseng
gabrieltseng / test_normalization.ipynb
Last active June 20, 2022 13:13
test_normalization_update.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@gabrieltseng
gabrieltseng / test_normalization.ipynb
Created June 16, 2022 10:23
test_normalization.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import torch
import torch.nn as nn
from torch.autograd import Variable, Function
import random
class WARP(Function):
'''
autograd function of WARP loss
'''
@staticmethod
# 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:
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)
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,
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
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'
from keras.applications.resnet50 import ResNet50
ResNet = ResNet50(weights='imagenet')