def __init__(self, dataset: cifar.CIFAR10, mean, std, model_path: str, attack_type: str, device=None): from model import load_models from datasets.utils import AdvAttack if device is None: device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu") self.device = device self.dataset = dataset model = load_models({"pretrain_paths": [model_path]})[0] self.adv_attack: torch.nn.Module = AdvAttack(model=model, attack_name=attack_type, mean=mean, std=std, device=self.device) self.adv_attack.to(self.device) # Pre Generate the dataset to speed up self.adv_imgs = self.pre_gen(batch_size=64)
def __init__(self, hparams): super().__init__(hparams) from model.utils import model_init from model.utils import freeze self.num = hparams["model_num"] self.models = load_models(hparams) assert len(self.models) == self.num if not hparams["train_teachers"]: freeze(self.models) self.models.cuda() self.large_gens = nn.ModuleList([self._make_large_generator(size * 4, size) for size in [64, 128, 256, 512]]) self.small_gens = nn.ModuleList( [nn.ModuleList([copy.copy(self._make_small_generator(size, size * 4)) for i in range(self.num)]) for size in [64, 128, 256, 512]]) if not hparams["train_gens"]: freeze(self.large_gens) freeze(self.small_gens) else: model_init(self.large_gens) model_init(self.small_gens) self.student = get_classifier(hparams["backbone"], hparams["dataset"]) if not hparams["train_student"]: freeze(self.student) else: model_init(self.student) from model.feature_similarity_measurement import cka_loss self.feature_loss = cka_loss()
def main(): # tiny example program: example_cell_idx = 20 # load model parameter: parameters = load_models("models.csv") model_params = parameters[example_cell_idx] cell = model_params.pop('cell') EODf = model_params.pop('EODf') print("Example with cell:", cell) # generate EOD-like stimulus with an amplitude step: deltat = model_params["deltat"] stimulus_length = 2.0 # in seconds time = np.arange(0, stimulus_length, deltat) # baseline EOD with amplitude 1: stimulus = np.sin(2 * np.pi * EODf * time) # amplitude step with given contrast: t0 = 0.5 t1 = 1.5 contrast = 0.3 stimulus[int(t0 // deltat):int(t1 // deltat)] *= (1.0 + contrast) # integrate the model: spikes = simulate(stimulus, **model_params) # some analysis an dplotting: freq = calculate_isi_frequency(spikes, deltat) freq_time = np.arange(spikes[0], spikes[-1], deltat) fig, axs = plt.subplots(2, 1, sharex="col") axs[0].plot(time, stimulus) axs[0].set_title("Stimulus") axs[0].set_ylabel("Amplitude in mV") axs[1].plot(freq_time, freq) axs[1].set_title("Model Frequency") axs[1].set_ylabel("Frequency in Hz") axs[1].set_xlabel("Time in s") plt.show() plt.close()
""" import model as mod import numpy as np import matplotlib.pyplot as plt import random import helper_functions as helpers import pandas as pd import os savepath = r'D:\ALPEREN\Tübingen NB\Semester 3\Benda\git\punitmodel\data' random.seed(666) #Run through each cell and save the firing rate and histogram values for each cell for later use cell_idx = 0 #for now #Load model parameters parameters = mod.load_models( 'models.csv') #model parameters fitted to different recordings #Amplitude modulation parameters. For explanation of each see helpers.amplitude_modulation ampmodinputs = { 'tlength': 1.5, 'boxonset': 0.5, 'contrasts': np.linspace(-0.5, 0.5, 40), 'ntrials': 100, 'tstart': 0.1 } tlength = 10 #stimulus time length (in seconds, for histogram) for i, __ in enumerate(parameters): cell, EODf, cellparams = helpers.parameters_dictionary_reformatting( i, parameters) dataname = savepath + '\%s_firing_rates_and_ISI_hist.csv' % (cell)
def __init__(self, hparams): super().__init__(hparams) self.models = load_models(hparams) self.num = len(self.models) self.modeles.cuda()
request = "INSERT INTO {0} (label, mean, median, sd, variance, iqr, mode, min, max) " \ "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)".format(db_setup.table_name) values = (dataset_label, features[columns[0]], features[columns[1]], features[columns[2]], features[columns[3]], features[columns[4]], features[columns[5]], features[columns[6]], features[columns[7]]) print(dataset_label) db_setup.cursor.execute(request, values) db_setup.db.commit() db_setup.close_db() model.train() test_data = pd.DataFrame(columns=model.get_columns()) if __name__ == '__main__': getAllLabels() model.load_data() model.prepare_data() model.load_models() app.run(host=config.HOST_ADDRESS, port=config.HOST_PORT, debug=config.DEBUG, threaded=config.THREADED) model.save_models() print("Server Closed")
def main(): # read arguments args = docopt(__doc__) images_path = args['<imagesPath>'] # models_dir = args['<modelsDir>'] # k = int(args['--kValue']) # k value for knn width = int(args['--width']) # width of rescalated image order = int(args['--norm']) # norm order feat = args['--feat'] rule = args['--rule'] print('Searching photos in ' + images_path) print('Searching models in ' + models_dir) print('Resizing to ' + str(width)) print('Using ' + feat) print('Using ' + rule + ' classification rule') model_list = model.load_models(models_dir) # All models should have consistent feature vectors. # It should be safe to take the lenght of the first vector num_coeffs = len(model_list[0](0)) if feat == 'hist': extract = features.hist extract_opt = {'num_coeffs': num_coeffs, 'width': width} elif feat == 'dct': extract = features.my_dct extract_opt = {'num_coeffs': num_coeffs, 'width': width} elif feat == 'pca': pca = features.fit_pca('data/train', num_coeffs, width) extract = features.my_pca extract_opt = {'pca': pca, 'width': width} if rule == 'knn': classify = classifiers.knn rule_opt = {'k': k, 'ord': order} elif rule == 'svm': classify = classifiers.svm correctly_classified = 0 total_images = 0 y_true = [] # True labels y_pred = [] # Predicted labels # Read all images in the given folder. # All images should be cropped faces from the same individual for dirName, subdirList, fileList in os.walk(images_path): for fname in sorted(fileList): extension = os.path.splitext(fname)[1] if (extension == '.jpg' or extension == '.JPG' or extension == '.png' or extension == '.PNG'): total_images = total_images + 1 ima = imageio.imread('{}/{}'.format(dirName, fname)) ########################################## # TODO: Extract the same feature as models ########################################## coeffs = extract(ima, **extract_opt) ########################################## # TODO: Extract best model ########################################## best_id = classify(coeffs, model_list, **rule_opt) # Test/validation images should be stored in directories # named according to the person name. # (the name of the directory is the label for this class) ground_truth_name = os.path.basename(dirName) y_true.append(ground_truth_name) y_pred.append(model_list[best_id].name()) if model_list[best_id].name() == ground_truth_name: correctly_classified = correctly_classified + 1 else: print("""ERROR: Image {}, true name = {}, ' hypothesis name = {} """.format(fname, ground_truth_name, model_list[best_id].name())) print('Total classification error: {0:.2f}%'.format( (1.0 - correctly_classified / total_images) * 100.0)) # Advanced reporting: print(confusion_matrix(y_true, y_pred)) print(classification_report(y_true, y_pred, digits=3))
def prepare(): model.load_data() model.prepare_data() model.load_models()
def run_attack(args): input_folder = os.path.join(args.input_path, 'images/') adv_img_save_folder = os.path.join(args.result_path, 'adv_images/') if not os.path.exists(adv_img_save_folder): os.makedirs(adv_img_save_folder) # Dataset, dev50.csv is the label file data_set = MyDataset(csv_path=os.path.join(args.input_path, args.label_file), path=input_folder) data_loader = DataLoader(dataset=data_set, batch_size=args.batch_size, shuffle=False, num_workers=2) device = torch.device("cuda:0") source_models = load_models(args.source_models, device) # load model, maybe several models seed_num = 0 # set seed random.seed(seed_num) np.random.seed(seed_num) torch.manual_seed(seed_num) torch.backends.cudnn.deterministic = True # gaussian_kernel: filter high frequency information of images gaussian_smoothing = gaussian_kernel(device, kernel_size=5, sigma=1, channels=3) print('Start attack......') for i, data in enumerate(data_loader, 0): start_t = time.time() X, labels, filenames = data X = X.to(device) labels = labels.to(device) # the noise delta = torch.zeros_like(X, requires_grad=True).to(device) X = gaussian_smoothing( X) # filter high frequency information of images for t in range(args.iterations): g_temp = [] for tt in range(len(liner_interval)): if args.input_diversity: # use Input Diversity X_adv = X + delta X_adv = input_diversity(X_adv) # images interpolated to 224*224, adaptive standard networks and reduce computation time X_adv = F.interpolate(X_adv, (224, 224), mode='bilinear', align_corners=False) else: c = liner_interval[tt] X_adv = X + c * delta X_adv = F.interpolate(X_adv, (224, 224), mode='bilinear', align_corners=False) # get ensemble logits ensemble_logits = get_logits(X_adv, source_models) loss = -nn.CrossEntropyLoss()(ensemble_logits, labels) loss.backward() grad = delta.grad.clone() # TI: smooth the gradient grad = F.conv2d(grad, TI_kernel(), bias=None, stride=1, padding=(2, 2), groups=3) g_temp.append(grad) # calculate the mean and cancel out the noise, retained the effective noise g = 0.0 for j in range(len(liner_interval)): g += g_temp[j] g = g / float(len(liner_interval)) delta.grad.zero_() delta.data = delta.data - args.alpha * torch.sign(g) delta.data = delta.data.clamp(-args.epsilon / 255., args.epsilon / 255.) delta.data = ((X + delta.data).clamp(0.0, 1.0)) - X save_imgs(X + delta, adv_img_save_folder, filenames) # save adv images end_t = time.time() print('Attack batch: {}/{}; Time spent(seconds): {:.2f}'.format( i, len(data_loader), end_t - start_t))
import string import torch from prepared import load_voc, indexesFromSentence from model import load_models data, Vocabulary = load_voc() encoder, decoder, embedding = load_models() device = "cuda:0" def greedy_search(sequence, length, maximum=40): # всё также как на тренировке encoder_out, encoder_hidden = encoder(sequence, length) decoder_hidden = encoder_hidden[:2] decoder_input = torch.ones(1, 1, device=device, dtype=torch.long) # здесь складываються ответы по жадному методу all_tokens = torch.zeros([0], device=device, dtype=torch.long) # всего будет 30 оборотов, при выводе отрежуться ненужные токены, а 30, чтобы больше не было for _ in range(maximum): decoder_output, decoder_hidden = decoder(decoder_input, decoder_hidden, encoder_out) _, decoder_input = torch.max(decoder_output, dim=1) all_tokens = torch.cat((all_tokens, decoder_input), dim=0) decoder_input = decoder_input.unsqueeze(0)
figsize=(11, 1.5 * len(models))) if SAMPLE_MATRICES: matrix_numbers = (("England", 5), ("Italy", 5), ("England", 19), ("Spain", 28), ("Spain", 20)) fig3, axes3 = plt.subplots(1, len(matrix_numbers), figsize=(12 / 5 * len(matrix_numbers), 2.5)) for i, model_info in enumerate(models): data_obj = DataHolder(model_info['country'], data_pars) fname = model_info['country'] + "-" + data_conditions + "-" # data_obj.get_random_accuracy(fname) learning_models = load_models(data_obj, file_name=fname + model_info['number'], with_weights=True) if PERF_TABLES: standard_model = Model(data_obj, file_name="Hybrid", with_weights=False) svm_models = load_models(data_obj, file_name="svm-" + fname + model_info['svm'], with_weights=False) models = { "Standard VAA": standard_model, "Social VAA": svm_models, "Learning VAA": learning_models }
from sklearn.metrics import classification_report import os from utils import getFiles, plot_confusion_matrix from model import HMM_Model, get_result, train, load_models, evaluate from dataloader import Dataloader, single_loader import numpy as np genre_list = [ 'blues', 'classical', 'jazz', 'country', 'pop', 'rock', 'metal', 'disco', 'hiphop', 'reggae' ] # dl = Dataloader(genre_list, root='genres') # train(dl) models = load_models(genre_list) # Evaluate method 1: cm, real, pred = evaluate('genres_small', genre_list, models) plot_confusion_matrix(cm, genre_list, True) # get plot print(classification_report(real, pred, target_names=genre_list)) # get report # Evaluate method 2: fl = getFiles('genres_small') for f in fl: X = single_loader(f, is_print_info=False, is_vision=False) print('Truth:{}, predict:{}'.format(f, get_result(X, models))) # Single test: X = single_loader('blues.00000.wav') result = get_result(X, models)