Example #1
0
 def __init__(self, num_in, num_out, options):
     self.params = options.model_params
     self.params['num_in'] = num_in
     self.params['num_out'] = num_out
     self.net = load_model(self.params)
     self.histories = dict()
     self.fit_fun = load_fit_fun(options.fit_fun)
Example #2
0
    def __init__(self, config:Dict):
        self.config = config
        self._init_dataloaders()
        self.model = load_model(self.config.get('model_type'), self.config.get('model_config', {})).to(self.config['device'])
        self.criterion = nn.CrossEntropyLoss(reduction='none')
        self.optim = torch.optim.Adam(self.model.parameters(), lr=self.config.get('lr', 1e-3))
        self._grad_clip_max_norm = config.get('grad_clip_max_norm', 1)
        
        self.train_history = {'losses': [], 'accs': []}
        self.test_history = {'losses': [], 'accs': [], 'iters': []}
        
        self.on_iter_done_callbacks = []        
        self.num_iters_done = 0
        self.max_num_iters = config.get('max_num_iters', 1000)
        self.val_freq_iters = config.get('val_freq_iters', -1)

        assert self.max_num_iters >= 0, 'When should I finish training?'
Example #3
0
        # : fashion_mnist/cifar10
        net_tokens = net_tokens[2].split('_')

    # model parameters
    batch_size = int(net_tokens[2])
    epochs = int(net_tokens[3])
    learn_rate = float(net_tokens[4])

    # error case
    if 'dp_' in args.netbase:
        assert False, ('Error: Baseline accuracy cannot come from a DP-model.')

    # load the model
    baseline_vars = models.extract_tf_model_parameters(args.network,
                                                       args.netbase)
    baseline_model = models.load_model( \
        args.dataset, args.datapth, args.network, vars=baseline_vars)
    print(' : Load the baseline model [{}] from [{}]'.format(
        args.network, args.netbase))

    # ------------------------------------------------------------
    #  Load the dataset (Data + Poisons)
    # ------------------------------------------------------------
    # load the dataset
    if args.poisonp.endswith('.pkl'):
        (x_train, y_train), (x_test, y_test), (x_poison, y_poison) = \
            datasets.load_lfip_poisons(args.poisonp)
    elif args.poisonp.endswith('.mat'):
        (x_train, y_train), (x_test, y_test), (x_poison, y_poison) = \
            datasets.load_slab_poisons(args.poisonp)
    else:
        assert False, ('Error: unknown format file - {}'.format(args.poisonp))
Example #4
0
fig = plt.figure(figsize=(25, 25))
for i in range(nx):
    for j in range(ny):
        index = i * ny + j
        plt.subplot(nx, ny, index + 1)
        image = X_train[indexes[index]]
        plt.imshow(image, cmap='gray')
fig.savefig('images/sample_5.jpg')
plt.show()

# plot distribution of training and testing data
plot_distro(y_train, 'Occurance probablity of each class in training data')
plot_distro(y_test, 'Occurance probablity of each class in testing data')

# load models
model_lenet = load_model(model_lenet_fun, image_shape, n_classes,
                         learning_rate, None)

model_incept = load_model(model_incept_fun, image_shape, n_classes,
                          learning_rate, None)

# Train, Validate and Test the Model
history_lenet = model_lenet.fit(X_train,
                                y_train,
                                batch_size=batch_size,
                                epochs=num_epochs,
                                shuffle=True,
                                validation_data=(X_valid, y_valid),
                                verbose=1)

history_incept = model_incept.fit(X_train,
                                  y_train,
Example #5
0
    # extract the basic information from the baseline model (always vanilla)
    net_tokens = args.netpath.split('/')
    net_tokens = net_tokens[2].split('_')

    # model parameters
    batch_size = int(net_tokens[2])
    epochs = int(net_tokens[3])
    epochs = 40 if epochs > 40 else epochs // 2
    learn_rate = float(net_tokens[4])

    # error case
    if 'dp_' in args.netpath:
        assert False, ('Error: Baseline accuracy cannot come from a DP-model.')

    # load the model
    base_model = models.load_model(args.dataset, args.datapth, args.network)
    if 'fashion_mnist' == args.dataset:
        base_model.build(input_shape=(None, 28, 28, 1))
    else:
        base_model.build(input_shape=(None, 32, 32, 3))
    base_model.load_weights(args.netpath)
    print(' : Load the base model [{}] from [{}]'.format(
        args.network, args.netpath))

    # load the optimizer
    base_optim = optims.define_optimizer(args.network, learn_rate)
    print('   Load the optimizer  [{}] with [lr: {}]'.format(
        base_optim.__class__.__name__, learn_rate))

    # ------------------------------------------------------------
    #  Load the backdooring dataset
Example #6
0
import numpy as np
import matplotlib.pyplot as plt
from utils.models import model_lenet_fun, model_incept_fun
from sklearn.metrics import confusion_matrix
from utils.mnist_dataset import load_mnist
from utils.models import load_model

learning_rate = 0.001
batch_size = 64

(X_train, y_train), (X_test, y_test), (X_valid, y_valid),\
                    (n_train, n_test, n_valid), n_classes, image_shape =\
    load_mnist(n_valid=1000)

# load models
model_lenet = load_model(model_lenet_fun, image_shape, n_classes,
                         learning_rate, './models/lenet.h5')

model_incept = load_model(model_incept_fun, image_shape, n_classes,
                          learning_rate, './models/incept.h5')

# validation set
scores = model_lenet.evaluate(X_test, y_test)
print('------Lenet Evaluation------')
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])

scores = model_incept.evaluate(X_test, y_test)
print('------Inception Evaluation------')
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
Example #7
0
cell_h = int(img_height / 9.)
cell_w = int(img_width / 9.)
margin_h = int(cell_h / 10.)
margin_w = int(cell_w / 10.)
image_warped = image_warped[margin_h:img_height - margin_h,
                            margin_w:img_width - margin_w].copy()

if PLOTTING:
    cv2.imshow("warped", image_warped)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# load trained mnist model
model = load_model(model_incept_fun,
                   image_shape=(28, 28, 1),
                   n_classes=10,
                   learning_rate=0.001,
                   h5_file='./models/incept.h5')

sudoku_grid = np.ones((9, 9), np.int) * (-1)
for row in range(9):
    for col in range(9):
        # part of the image containing cell[row, col]
        image_cell = image_warped[row * cell_h + margin_h:(row + 1) * cell_h -
                                  margin_h, col * cell_w +
                                  margin_w:(col + 1) * cell_w - margin_w]
        image_cell = cv2.erode(image_cell, kernel, iterations=1)

        # find contours inside each cell
        cnts = cv2.findContours(image_cell, cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_NONE)[0]
Example #8
0
 def __init__(self, options, classes):
     self.data = load_data(options, train=True, classes=classes)
     params = options.model_params
     self.net = load_model(params)
     self.options = options
Example #9
0
from utils.models import load_model, MFCC13, process_data

import sys
sys.modules['__main__'].MFCC13 = MFCC13
model, preprocessor = load_model('utils/SVM-best.model')

from functools import partial
preprocessor = partial(process_data, preprocessor=preprocessor)

from utils.data import Sound
sound = Sound()