def print_table(self): model_names = ["Resnet152", "VGG19_bn", "DenseNet", "ResAttNet"] print("Validation Accuracy Table") for i in range(len(self.models)): criterion = nn.CrossEntropyLoss() ensemble_solver = Ensemble([self.models[i]]) top1_acc, top5_acc, val_loss = ensemble_solver.evaluate_all(criterion, self.dataloaders, self.dataset_sizes) fgsm_top1_acc, fgsm_top5_acc, fgsm_val_loss = ensemble_solver.evaluate_all(criterion, self.fgsm_dataloader, self.fgsm_dataset_sizes) blurred_top1_acc, blurred_top5_acc, blurred_val_loss = ensemble_solver.evaluate_all(criterion, self.blurred_dataloader, self.blurred_dataset_sizes) print("{} = top1_acc: {}, top5_acc:{}, fgsm_top1_acc:{}, blurred_top1_acc:{}".format(model_names[i], top1_acc, top5_acc, fgsm_top1_acc, blurred_top1_acc)) print() resnet_model, vgg_model, dense_model, attention_model = self.models combo = [ [resnet_model, dense_model, vgg_model, attention_model], [resnet_model, dense_model, attention_model], [resnet_model, vgg_model, attention_model], [resnet_model, dense_model, vgg_model], [dense_model, vgg_model, attention_model] ] combo_names = [ ["Resnet152, VGG19_bn, DenseNet, ResAttNet"], ["Resnet152, DenseNet, ResAttNet"], ["Resnet152, VGG19_bn, ResAttNet"], ["Resnet152, VGG19_bn, DenseNet"], ["DenseNet, VGG19_bn, ResAttNet"] ] print("Ensemble by Averaging logits") for i in range(len(combo)): criterion = nn.CrossEntropyLoss() ensemble_solver = Ensemble(combo[i]) top1_acc, top5_acc, val_loss = ensemble_solver.evaluate_all(criterion, self.dataloaders, self.dataset_sizes) fgsm_top1_acc, fgsm_top5_acc, fgsm_val_loss = ensemble_solver.evaluate_all(criterion, self.fgsm_dataloader, self.fgsm_dataset_sizes) blurred_top1_acc, blurred_top5_acc, blurred_val_loss = ensemble_solver.evaluate_all(criterion, self.blurred_dataloader, self.blurred_dataset_sizes) print(combo_names[i][0]) print("Validation top1_acc: {}, top5_acc:{}, fgsm_top1_acc:{}, blurred_top1_acc:{}".format(top1_acc, top5_acc, fgsm_top1_acc, blurred_top1_acc)) print() print("Ensemble by Majority Vote") for i in range(len(combo)): criterion = nn.CrossEntropyLoss() ensemble_solver = Ensemble(combo[i]) top1_acc, top5_acc, val_loss = ensemble_solver.evaluate_all(criterion, self.dataloaders, self.dataset_sizes, mode="maj vote") fgsm_top1_acc, fgsm_top5_acc, fgsm_val_loss = ensemble_solver.evaluate_all(criterion, self.fgsm_dataloader, self.fgsm_dataset_sizes, mode="maj vote") blurred_top1_acc, blurred_top5_acc, blurred_val_loss = ensemble_solver.evaluate_all(criterion, self.blurred_dataloader, self.blurred_dataset_sizes, mode="maj vote") print(combo_names[i][0]) print("Validation top1_acc: {}, top5_acc:{}, fgsm_top1_acc:{}, blurred_top1_acc:{}".format(top1_acc, top5_acc, fgsm_top1_acc, blurred_top1_acc)) print()
def ValidateFineTuning(dataset,validateall=True,validateat=0): datas = ReadValidationData(dataset) result = [] time = 0 params = GetParams(dataset) cnnfilename = params['FineTuning']['cnn_load_file'] rnnfilename = params['FineTuning']['rnn_load_file'] ensemblefilename = params['FineTuning']['ensemble_load_file'] finetuningfilename = params['FineTuning']['ensemble_save_file'] for data in datas: time += 1 if not validateall: if validateat > time: continue elif validateat < time: break input = data['input'] label = data['label'] params['FineTuning']['cnn_load_file'] = cnnfilename+'_'+str(time) params['FineTuning']['rnn_load_file'] = rnnfilename+'_'+str(time) params['FineTuning']['ensemble_load_file'] = ensemblefilename+'_'+str(time) params['FineTuning']['ensemble_save_file'] = finetuningfilename+'_'+str(time) r = None r = Ensemble(params['FineTuning'],input['train']['onehot'],input['train']['biofeat'],label['train'], input['test']['onehot'],input['test']['biofeat'],label['test'],issave=True, withbiofeature=True,rnn_trainable=True,cnn_trainable=True) result.append(r['loss']) print(result)
def plot_error(l): res = [] for alpha in l: res.append( test( Ensemble(5, linear_model.Lasso(alpha=alpha), [clf, clf1, clf2, clf3]))) plt.plot(l, res) plt.show()
def Pipeline(dataset, pretrainCNN=False, pretrainRNN=False, ensemble=False, fineTuning=False): data = ReadData(dataset) params = GetParams(dataset) input = data['input'] label = data['label'] r = None if pretrainCNN: r = CNN(params['CNNParams'], input['train']['onehot'], label['train'], input['test']['onehot'], label['test']) if pretrainRNN: r = RNN(params['RNNParams'], input['train']['onehot'], label['train'], input['test']['onehot'], label['test']) if ensemble: input_train_onehot, input_train_biofeat, y_train = input['train'][ 'onehot'], input['train']['biofeat'], label['train'] r = Ensemble(params['EnsembleParams'], input['train']['onehot'], input['train']['biofeat'], label['train'], input['test']['onehot'], input['test']['biofeat'], label['test'], withbiofeature=True, cnn_trainable=False, rnn_trainable=False) if fineTuning: r = Ensemble(params['FineTuning'], input['train']['onehot'], input['train']['biofeat'], label['train'], input['test']['onehot'], input['test']['biofeat'], label['test'], withbiofeature=True, cnn_trainable=True, rnn_trainable=True, load_weight=True) return r
def main(): # Load the classes data_dir = pathlib.Path('./data/tiny-imagenet-200/train/') CLASSES = sorted([item.name for item in data_dir.glob('*')]) im_height, im_width = 64, 64 models = load_models() data_transforms = transforms.Compose([ transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), transforms.Normalize((0, 0, 0), tuple(np.sqrt((255, 255, 255)))) ]) evalcsv_path = sys.argv[1] print("path to the eval.csv file:", evalcsv_path) # Loop through the CSV file and make a prediction for each line with open( 'eval_classified.csv', 'w' ) as eval_output_file: # Open the evaluation CSV file for writing # eval_dir = pathlib.Path('eval.csv') eval_dir = pathlib.Path(evalcsv_path) for line in eval_dir.open(): # for line in pathlib.Path(sys.argv[1]).open(): # Open the input CSV file for reading image_id, image_path, image_height, image_width, image_channels = line.strip( ).split(',') # Extract CSV info print(image_id, image_path, image_height, image_width, image_channels) with open(image_path, 'rb') as f: img = Image.open(f).convert('RGB') img = data_transforms(img)[None, :] ensemble_solver = Ensemble(models) predicted = ensemble_solver.evaluate_testdata(img) print("predicted class:", CLASSES[predicted]) print() # Write the prediction to the output file eval_output_file.write('{},{}\n'.format(image_id, CLASSES[predicted]))
def get_algorithm(algorithm_type, alpha): if algorithm_type == AlgorithmType.Random: return Random(alpha) elif algorithm_type == AlgorithmType.EFirst: return EFirst(alpha) elif algorithm_type == AlgorithmType.EGreedy: return EGreedy(alpha) elif algorithm_type == AlgorithmType.EGreedy_Disjoint: return EGreedy_Disjoint(alpha) elif algorithm_type == AlgorithmType.EGreedy_Hybrid: return EGreedy_Hybrid(alpha) elif algorithm_type == AlgorithmType.EGreedy_Seg: return Combo_Seg(alpha, AlgorithmType.EGreedy) elif algorithm_type == AlgorithmType.LinUCB_Disjoint: return LinUCB_Disjoint(alpha) elif algorithm_type == AlgorithmType.LinUCB_GP: return LinUCB_GP(alpha) elif algorithm_type == AlgorithmType.LinUCB_GP_All: return LinUCB_GP_All(alpha) elif algorithm_type == AlgorithmType.LinUCB_Hybrid: return LinUCB_Hybrid(alpha) elif algorithm_type == AlgorithmType.UCB: return UCB(alpha) elif algorithm_type == AlgorithmType.UCB_Seg: return Combo_Seg(alpha, AlgorithmType.UCB) elif algorithm_type == AlgorithmType.EGreedy_Lin: return EGreedy_Lin(alpha) elif algorithm_type == AlgorithmType.EGreedy_Seg_Lin: return Combo_Seg(alpha, AlgorithmType.EGreedy_Lin) elif algorithm_type == AlgorithmType.EGreedy_Lin_Hybrid: return EGreedy_Lin_Hybrid(alpha) elif algorithm_type == AlgorithmType.TS: return TS(alpha) elif algorithm_type == AlgorithmType.TS_Bootstrap: return TS_Bootstrap(alpha) elif algorithm_type == AlgorithmType.TS_Lin: return TS_Lin(alpha) elif algorithm_type == AlgorithmType.TS_Seg: return Combo_Seg(alpha, AlgorithmType.TS) elif algorithm_type == AlgorithmType.TS_Disjoint: return TS_Disjoint(alpha) elif algorithm_type == AlgorithmType.TS_Hybrid: return TS_Hybrid(alpha) elif algorithm_type == AlgorithmType.TS_Truncated: return TS_Truncated(alpha) elif algorithm_type == AlgorithmType.EGreedy_TS: return EGreedy_TS(alpha) elif algorithm_type == AlgorithmType.TS_Gibbs: return TS_Gibbs(alpha) elif algorithm_type == AlgorithmType.TS_Laplace: return TS_Laplace(alpha) elif algorithm_type == AlgorithmType.EGreedy_Annealing: return EGreedy_Annealing(alpha) elif algorithm_type == AlgorithmType.NN: return NN(alpha) elif algorithm_type == AlgorithmType.Ensemble: return Ensemble(alpha) elif algorithm_type == AlgorithmType.TS_RLR: return TS_RLR(alpha) else: raise NotImplementedError("Non-implemented algorithm." + algorithm_type.name)
######################## # EXECUTION REGRESSION # ######################## Regression = Task.Regression(fn.ML_cup, num_epoch, dim_output, hidden_units, batch_array, learning_rate_init, type_learning_rate, alfa, v_lambda, fun, weight, early_stopping, num_training) top_models = Regression.startexecution_k_fold() num_training = Regression.num_training ######################## # RANDOMIZATION PHASE # ######################## ensamble = Ensemble(top_models, 8) random_top_models = [] #take the best model and create a new_model for each with a perturbation of all hyperparameters for model in top_models: #save the hyperparameters of model alfa = [copy.deepcopy(model.NN.alfa)] v_lambda = [copy.deepcopy(model.NN.v_lambda)] learning_rate_init = [copy.deepcopy(model.NN.learning_rate)] hidden_units = [copy.deepcopy(model.NN.units)] batch_size = [copy.deepcopy(model.NN.batch_size)] #perturbation hidden_units, batch_size, learning_rate_init, alfa, v_lambda = Function.pertubation(
def RandomForest(X, y, epsilon, num_learners, depth, min_sample): dt = DecisionTree(epsilon, min_sample, depth) result = Ensemble(dt, num_learners, 0.8) result.fit(X, y) return result
return iarr if __name__ == "__main__": import pandas as pd from Ensemble import Ensemble from os import mkdir from os.path import exists, join, dirname import tempfile tempfile.tempdir = "/home/jin76872/Desktop/Mih/Data/tmp_superstar_ghecom" brd1_data = pd.read_csv( "/home/jin76872/Desktop/Mih/Data/SIENA/EnsembleAligner/BRD1.csv") ref_ID = brd1_data.loc[9].squeeze() e = Ensemble(root_dir="", ref_ID=ref_ID) hot_paths = glob( "/home/jin76872/Desktop/Mih/Data/SIENA/EnsembleAligner/BAZ2B/Hotspots/*/out.zip" )[:100] hot_paths += glob( "/home/jin76872/Desktop/Mih/Data/SIENA/EnsembleAligner/BRD1/Hotspots/*/out.zip" )[:100] # trunc_paths = e.shrink_hotspots(hot_paths) # with open("hot_paths.txt", "w") as f: # for h in trunc_paths: # f.write(h + "\n") with open("hot_paths.txt") as f: trunc_paths = [line.strip() for line in f.readlines()] brd1_hots = [join(t, "acceptor.ccp4") for t in trunc_paths if "BRD1" in t]
plot_ucs_size_composition_vs_ssd_cutoff(threeS, fourS, fiveS, ssd_range) return size_map_for_ssd_range with open(graph_summary_file, 'r') as f: for line in f: line = line.strip() summary = line.split('\t', 4) num_of_timestamps, num_of_nodes, min_num_of_edges, max_num_of_edges, total_num_of_edges = int( summary[0]), int(summary[1]), int(summary[2]), int( summary[3]), int(summary[4]) with open(datafile, 'r') as f: T = Ensemble() for line in f: line = line.strip() timestamp, v1, v2 = line.split('\t', 2) if v1 != v2: T.add_edge(timestamp, int(v1), int(v2)) else: T.add_node(int(v1)) plot_ssd_vs_rank(128) # plot_percent_of_nodes_vs_rank(1024, num_of_nodes) # plot_ssd_of_uncovered_nodes_vs_rank(2048, T) # print(plot_num_of_k_size_ucs_vs_ssd_cutoff_for_am([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])) # print(plot_num_of_k_size_ucs_vs_ssd_cutoff_for_lam([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])) plt.show()
help="Type of estimator (maximum likelihood (ml) or shrinkage") (options, args) = parser.parse_args() if not options.trajectory_filename or not options.topology_filename: parser.error("--trajectory and --topology options must be specified") # Construct reference if available try: reference = MDAnalysis.Universe(options.topology_filename, options.topology_filename) except: reference = None # Construct ensemble ensemble = Ensemble(topology=options.topology_filename, trajectory=options.trajectory_filename, atom_selection_string=options.atom_selection_string, frame_interval=options.frame_interval) # Align ensemble to reference if not options.no_align: ensemble.align(reference) # Select covariance estimator estimator = EstimatorML() if options.covariance_estimator == "shrinkage": estimator = EstimatorShrinkage() # Disable reference unless use_distance_to_reference is set if not options.use_distance_to_reference: reference = None
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from Ensemble import Ensemble from data import load_dataset import numpy as np # ============================================================================= # Crear un modelo con 5 clasificadores, se guardan en el directorio # clasificadores/, el directorio debe existir # ============================================================================= Test = Ensemble(5, 'clasificadores2') # ============================================================================= # Cargar datos # ============================================================================= # Dataset completo X, y = load_dataset() total_size = X.shape[0] train_size = int(np.floor(total_size * .8)) test_size = total_size - train_size print('- Dataset: %d samples' % total_size) print('- Training set: %d samples' % train_size) print('- Test set: %d samples' % test_size) np.random.seed(54) rp = np.random.permutation(total_size)
item_id = names[i].split('.')[0] result = None if (y[i] == 0): result = 1 else: result = 0 conf = confidence[i][y[i]] f.write("%s,%d,%f\n" % (item_id, result, conf)) ip = ImagesProcessor() images, y = ip.getImages('../imgs/test/dataset/', size=None, training=False) # Esto es lo que hay que usar para predecir el resultado final if True: ensemble = Ensemble() ensemble.load() X_predictions = ensemble.predict_small(images) y_hat = ensemble.predict_big(X_predictions) confidence = ensemble.ensemble_logistic_regression.predict_proba( X_predictions) printResult(y, y_hat, confidence) #score(y_hat, y) # Esto es lo que hay que usar para calcular al regression lineal y gurdarla if False: ensemble = Ensemble() ensemble.load() X_validation_predictions = ensemble.predict_small(images) ensemble.fit_big(X_validation_predictions, y) f = file("./ensemble_logistic_regression", 'wb')
def load_model(model_name): model = None if 'resnet152_ddn_jpeg'.__eq__(model_name): print("load model resnet152_ddn_jpeg") m = models.resnet152(pretrained=False) weight = './weights/jpeg_ddn_resnet152/jpeg_ddn_resnet152.pth' image_mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1) image_std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1) pretrained_model = NormalizedModel(model=m, mean=image_mean, std=image_std) loaded_state_dict = torch.load(weight) pretrained_model.load_state_dict(loaded_state_dict) model = pretrained_model elif 'wide_resnet101_2_dnn_jpeg'.__eq__(model_name): print("load model wide_resnet101_2_dnn_jpeg") m = models.wide_resnet101_2(pretrained=False) weight = './weights/jpeg_ddn_wide_resnet101/jpeg_ddn_wide_resnet101.pth' image_mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1) image_std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1) model = NormalizedModel(model=m, mean=image_mean, std=image_std) loaded_state_dict = torch.load(weight) model.load_state_dict(loaded_state_dict) elif 'densenet161_ddn_jpeg'.__eq__(model_name): print("load model densenet161_ddn_jpeg") m = models.densenet161(pretrained=False) weight = './weights/jpeg_ddn_densenet161/jpeg_ddn_densenet161.pth' image_mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1) image_std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1) model = NormalizedModel(model=m, mean=image_mean, std=image_std) loaded_state_dict = torch.load(weight) model.load_state_dict(loaded_state_dict) elif 'hrnet_w64_ddn_jpeg'.__eq__(model_name): print("load model: hrnet_w64_ddn_jpeg") model = timm.create_model('hrnet_w64', pretrained=False) image_mean = torch.tensor([0.5000, 0.5000, 0.5000]).view(1, 3, 1, 1) image_std = torch.tensor([0.5000, 0.5000, 0.5000]).view(1, 3, 1, 1) model = NormalizedModel(model=model, mean=image_mean, std=image_std) weight= './weights/jpeg_ddn_hrnet_w64/jpeg_ddn_hrnet_w64.pth' loaded_state_dict = torch.load(weight) model.load_state_dict(loaded_state_dict) elif 'Ensemble_dsn161_jpeg_rn162_jpeg_wrn101_jpeg'.__eq__(model_name): print("load model: Ensemble_dsn161_jpeg_rn162_jpeg_wrn101_jpeg") model1 = load_model("densenet161_ddn_jpeg") model2 = load_model("resnet152_ddn_jpeg") model3 = load_model("wide_resnet101_2_dnn_jpeg") model = Ensemble(model1, model2, model3) elif 'Ensemble_dsn161_jpeg_wrn101_jpeg_hrn_jpeg'.__eq__(model_name): print("load model: Ensemble_dsn161_jpeg_wrn101_jpeg_hrn_jpeg") model1 = load_model("densenet161_ddn_jpeg") model2 = load_model("wide_resnet101_2_dnn_jpeg") model3 = load_model("hrnet_w64_ddn_jpeg") model = Ensemble3_hrn(model1, model2, model3) elif 'Ensemble_dsn161_jpeg_wrn101_jpeg'.__eq__(model_name): print("load model: Ensemble_dsn161_jpeg_wrn101_jpeg") model1 = load_model("densenet161_ddn_jpeg") model2 = load_model("wide_resnet101_2_dnn_jpeg") model = Ensemble2(model1, model2) elif 'Ensemble_dsn161_jpeg_rn162_jpeg_wrn101_jpeg_hrn_jpeg'.__eq__(model_name): print("load model: Ensemble_dsn161_jpeg_rn162_jpeg_wrn101_jpeg_hrn_jpeg") model1 = load_model("densenet161_ddn_jpeg") model2 = load_model("resnet152_ddn_jpeg") model3 = load_model("wide_resnet101_2_dnn_jpeg") model4 = load_model("hrnet_w64_ddn_jpeg") model = Ensemble4(model1, model2, model3,model4) elif 'Ensemble_dsn161_jpeg_rn162_jpeg'.__eq__(model_name): print("load model: Ensemble_dsn161_jpeg_rn162_jpeg") model1 = load_model("densenet161_ddn_jpeg") model2 = load_model("resnet152_ddn_jpeg") model = Ensemble2(model1, model2) else: print("can not load model") return model
def __init__(self): self.Ensemble=Ensemble(3) super(Update, self).__init__()
# largest_ligand = ref_kr.find_largest_ligand() prot = Protein.from_file(join(dirname(largest_ligand), 'protein.mol2')) bs = Protein.BindingSiteFromMolecule( protein=prot, molecule=MoleculeReader(largest_ligand)[0], distance=7.0) s_paths_file = join(main_dir, "shrunk_hot_paths_{}.json".format(nrot)) if exists(s_paths_file): with open(s_paths_file, "r") as f: s_paths_dict = json.load(f) else: s_paths_dict = {} ensemble = Ensemble(root_dir=join(main_dir, e)) ensemble.reference_binding_site = bs #hot_paths = glob(join(ensemble.root_dir, '*', "fullsize_hotspots_{}".format(nrot), "out.zip")) s_paths = ensemble.shrink_hotspots(hotspot_paths=hot_paths, padding=2.0) s_paths_dict[e] = s_paths for probe in ["donor", "acceptor", "apolar"]: paths = [join(t, '{}.ccp4'.format(probe)) for t in s_paths] gr = GridEnsemble(paths) gr.get_ensemble_array() gr.save_gridensemble( join(main_dir, e, '{}_{}.p'.format(probe, nrot))) spd = json.dumps(s_paths_dict, sort_keys=True, indent=4,
clf = linear_model.Ridge(alpha=15, fit_intercept=True) #les coeffs ont été trouvés en en faisant varier 1 avec les 2 autres fixés clf2 = XGBRegressor(max_depth=2, learning_rate=0.08, n_estimators=955, subsample=0.96) #clf4 = neighbors.KNeighborsRegressor(weights = 'distance') clf1 = linear_model.Lasso(alpha=0.0005) clf3 = ensemble.RandomForestRegressor(n_estimators=100, max_depth=15) clf5 = Ensemble(5, linear_model.Lasso(alpha=0.0005), [clf, clf1, clf2, clf3]) """ clf = regularization.logReg(l=0.00001) clf.fit(X1,y) """ RMSE_Score = metrics.make_scorer(RMSE) def test(model): return (np.mean( cross_validation.cross_val_score(model, X1, y['SalePrice'], cv=5, scoring=RMSE_Score)))