def main(): """ Generates all the tables and plots for the paper """ parser = argparse.ArgumentParser(description='Script to trigger the full benchmark analysis') parser.add_argument("--out_dir", help='Path to store all the output results. Default: "../../outdir"') parser.add_argument("--location", default="HGVSc", choices=("HGVSc","Consequence"), help='VCF field to extract location of the variant') referencesetmode = parser.add_argument_group('Tools performance on reference variant sets') referencesetmode.add_argument("--limitAnalysis", metavar='dataset', type=lambda s: list(map(str, s.split(","))), default=[], help='Set the analysis to be performed to the given datasets. Default: Run hcm. ' 'Choices: [hcm]') args = parser.parse_args() datasets = ['hcm'] if args.out_dir : PAPER_LOCATION = args.out_dir if not os.path.isdir(args.out_dir): os.mkdir(args.out_dir) os.mkdir(os.path.join(args.out_dir,"figures")) else: PAPER_LOCATION = "../../outdir" if all(x in datasets for x in args.limitAnalysis): for analysis in datasets: data = preprocess(args.location,analysis) generate_performance_comparison(data, filters, threshold_list, analysis, PAPER_LOCATION) df = apply_tool_predictions(data, threshold_list) generate_heatmap(df, filters, threshold_list, analysis, PAPER_LOCATION) else: print("Please limit your analysis to one (or more) of the following dataset options:\t{}".format(datasets)) exit(1)
def valid(self): print ("start valid") start = time.time() self.model.eval() loss_list = [] with torch.no_grad(): for i_batch, batch in enumerate(self.valid_data_loader): x = batch[0] x = x.to(self.device) x = preprocess(x, self.n_bits) # forward z, nll = self.model(x=x) # loss loss = torch.mean(nll) loss_list.append(loss.data.cpu().item()) mean_loss = np.mean(loss_list) with open(os.path.join(self.out_root, "valid_nll.txt"), "a") as f: f.write("{} \t {:.5f}".format(self.global_step, mean_loss)) f.write("\n") self.model.train() print ("end valid") print ("Valid elapsed time:{:.2f}".format(time.time() - start))
def Inference(self): start = time.time() loss_list = [] num_batchs = len(self.data_loader) for i_batch, batch in enumerate(self.data_loader): x = batch[0] if self.cuda: x = x.cuda() x = preprocess(x, n_bits=self.n_bits) # forward z, nll = self.model(x=x) # loss loss = torch.mean(nll) loss_list.append(loss.data.cpu().item()) print("batch: {}/{}, elapsed time:{:.2f}, loss:{:.5f}".format(i_batch, num_batchs, time.time()-start, loss.data.cpu().item())) mean_loss = np.mean(loss_list) with open(os.path.join(self.out_root, "test_nll.txt"), "w") as f: f.write("NLL: {:.5f}".format(mean_loss))
dir_ = '2020-2-15' for i, img in tqdm(enumerate(test_imgs)): #dir_ = str(i) if not os.path.exists('demo/%s' % dir_): os.makedirs('demo/%s' % dir_) path = img fname = os.path.basename(path) #out_file = '/home/shaohao/adversarial-attack-detection/visualization/%s'%fname out_file = 'demo/%s/%s_0.jpg' % (dir_, str(i)) img = Image.open(img) img = img.convert('RGB') img = np.asarray(img, dtype=np.float32).transpose((2, 0, 1)) raw_size = img.shape[1:] H, W = raw_size img = preprocess(img) bboxes, labels, scores = faster_rcnn.predict(img, raw_size) visualize((bboxes, labels, scores), path, out_file) if len(bboxes[0]) == 0: continue ''' check_img = tensor2img(img, mean, std) check_img = check_img.resize((W, H)) check_img.save('demo/%s/check.png'%dir_) check_img = Image.open('demo/%s/check.png'%dir_).convert('RGB') check_img = np.asarray(check_img, dtype=np.float32).transpose((2, 0, 1)) check_img = preprocess(check_img) bboxes, labels, scores = faster_rcnn.predict(check_img, raw_size) # for comparsion visualize((bboxes, labels, scores), 'demo/%s/check.png'%dir_, 'demo/%s/check_visualization.jpg'%dir_) #print(img.size())
def __init__(self, dev=False): self.contexts, self.questions, self.answers = datasets.preprocess( dev, 'squad')
def train(self): # set to training state self.model.train() # init glow starttime = time.time() # run num_batchs = len(self.train_data_loader) total_its = self.num_epochs * num_batchs for epoch in range(self.num_epochs): mean_nll = 0.0 #print(self.optim.param_groups[0]['lr']) for i_batch, batch in enumerate(self.train_data_loader): x = batch[0] x = x.to(self.device) x = preprocess(x, self.n_bits) # forward z, nll = self.model(x=x) # loss loss = torch.mean(nll) mean_nll = mean_nll + loss.data currenttime = time.time() elapsed = currenttime - starttime print("Iteration: {}/{} \t Elapsed time: {:.2f} \t Loss:{:.5f}".format(self.global_step, total_its, elapsed, loss.data)) if self.global_step % self.nll_gap == 0: with open(os.path.join(self.out_root, "NLL.txt"), "a") as nll_file: nll_file.write("{} \t {:.2f}\t {:.5f}".format(self.global_step, elapsed, loss.data) + "\n") # backward self.model.zero_grad() self.optim.zero_grad() loss.backward() # operate grad grad_norm = 0 if self.max_grad_clip is not None and self.max_grad_clip > 0: torch.nn.utils.clip_grad_value_(self.model.parameters(), self.max_grad_clip) if self.max_grad_norm is not None and self.max_grad_norm > 0: grad_norm = torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.max_grad_norm) # step if math.isnan(grad_norm): continue else: self.optim.step() # checkpoint if self.global_step % self.save_gap == 0 and self.global_step > 0: utils.save_model(self.model, self.optim, self.scheduler, self.checkpoints_dir, self.global_step) if self.global_step % self.valid_gap == 0 and self.global_step > 0: if self.valid_data_loader is not None: self.valid() if self.global_step % self.inference_gap == 0 and self.global_step > 0: self.save_inverse_images(x, z) self.save_sample_images(self.n_samples,self.sample_each_row, 1.0) self.global_step = self.global_step + 1 current_lr = self.optim.param_groups[0]['lr'] if self.scheduler is not None and current_lr > self.lr_update_threshold: self.scheduler.step() mean_nll = float(mean_nll / float(num_batchs)) with open(os.path.join(self.out_root, "Epoch_NLL.txt"), "a") as f: currenttime = time.time() elapsed = currenttime - starttime f.write("{} \t {:.2f}\t {:.5f}".format(epoch, elapsed, mean_nll) + "\n")