def evaluation(dev_query_embedding2id, passage_embedding2id, dev_I, dev_D, trec_prefix="real-dev_query_", test_set="trec2019", split_idx=-1, d2q_eval=False, d2q_qrels=None): if d2q_eval: qrels = d2q_qrels else: if args.data_type == 0: if not d2q_eval: if test_set == "marcodev": qrels = "../data/raw_data/msmarco-docdev-qrels.tsv" elif test_set == "trec2019": qrels = "../data/raw_data/2019qrels-docs.txt" elif args.data_type == 1: if test_set == "marcodev": qrels = "../data/raw_data/qrels.dev.small.tsv" else: logging.error("wrong data type") exit() trec_path = os.path.join(args.output_dir, trec_prefix + str(checkpoint_step) + ".trec") save_trec_file(dev_query_embedding2id, passage_embedding2id, dev_I, dev_D, trec_save_path=trec_path, topN=200) convert_trec_to_MARCO_id(data_type=args.data_type, test_set=test_set, processed_data_dir=args.data_dir, trec_path=trec_path, d2q_reversed_trec_file=d2q_eval) trec_path = trec_path.replace(".trec", ".formatted.trec") met = Metric() if split_idx >= 0: split_file_path = qrels + f"{args.dev_split_num}_fold.split_dict" with open(split_file_path, 'rb') as f: split = pickle.load(f) else: split = None ndcg10 = met.get_metric(qrels, trec_path, 'ndcg_cut_10', split, split_idx) mrr10 = met.get_mrr(qrels, trec_path, 'mrr_cut_10', split, split_idx) mrr100 = met.get_mrr(qrels, trec_path, 'mrr_cut_100', split, split_idx) logging.info( f" evaluation for {test_set}, trec_file {trec_path}, split_idx {split_idx} \ ndcg_cut_10 : {ndcg10}, \ mrr_cut_10 : {mrr10}, \ mrr_cut_100 : {mrr100}") return ndcg10
def diff_record_or_warning(local_block_num, remote_block_num, other_api): diff = remote_block_num - local_block_num msg = ("local:%s,remote(%s):%s,diff:%s" % (local_block_num, other_api, remote_block_num, diff)) logger.info('%s, %s', hostname, msg) Metric.metric(Metric.height_diff, diff, version=local_server_version) if abs(diff) >= max_height_diff: log_and_notify(hostname, msg)
def extract_incoming_block(self, message): pattern = '.*?#(?P<block_header_num>\d+)\s+@\s+(?P<timestamp>{})\s+?signed by\s+?(?P<producer_name>.+?)\s.*' \ '\[trxs: (?P<trx_count>\d+), lib: (?P<last_irreversible_block_num>\d+), conf: (?P<confirm_count>\d+), latency: (?P<latency>.*) ms\]' \ .format(timestamp_pattern) if not re.match(pattern, message): return msg_dict = self.extract_dict(pattern, message) if msg_dict is None: return producer_name = msg_dict['producer_name'] trx_count = msg_dict['trx_count'] confirm_count = msg_dict['confirm_count'] latency = msg_dict['latency'] logger.info('%s %s %s %s', producer_name, trx_count, confirm_count, latency) Metric.metric(Metric.latency, latency, producer_name) Metric.metric(Metric.trxs, trx_count, producer_name)
def main(opt): if opt.image_dir: stsimM_vectors, stsimM_class = [], {} for root, dirs, files in os.walk(opt.image_dir): for base_texture in range(0, 10): for distortion in range(0, 10): img = f"{base_texture}_{distortion}.tiff" vector = list(Metric().STSIM_M( cv2.imread(os.path.join(root, img), cv2.IMREAD_GRAYSCALE))) stsimM_vectors.append([int(base_texture + distortion)] + vector) if int(base_texture) in stsimM_class: stsimM_class[int(base_texture)].append(vector) else: stsimM_class[int(base_texture)] = [vector] if opt.save_features: np.save(open(opt.save_features, 'rb'), stsimM_vectors) if opt.load_distance_matrix: dist_matrix = np.load(open(opt.load_distance_matrix, 'rb')) elif opt.scope == 'global': if opt.distance_metric == 'var': dist_matrix = np.diag(np.var(stsimM_vectors[:, 1:], axis=0)) elif opt.distance_metric == 'cov': import pdb pdb.set_trace() dist_matrix = np.cov(np.array(stsimM_vectors)[:, 1:], rowvar=False) elif opt.scope == 'intraclass': if opt.distance_metric == 'var': dist_matrix = np.mean([ np.diag(np.var(distortions), axis=0) for distortions in stsimM_class.values() ], axis=0) elif opt.distance_metric == 'cov': dist_matrix = np.mean([ np.cov(distortions, rowvar=False) for distortions in stsimM_class.values() ], axis=0) if opt.save_distance_matrix: np.save(open(opt.save_distance_matrix, 'wb'), dist_matrix) dist_matrix = np.linalg.inv(dist_matrix) results = [] for base_texture in range(0, 10): texture_sim = [ mahalanobis(stsimM_class[base_texture][0], stsimM_class[base_texture][distortion], dist_matrix) for distortion in range(0, 10) ] results.append(texture_sim) if opt.save_results: np.save(open(opt.save_results, 'wb'), np.array(results)) evaluate(results, opt)
def get_bp_account_info(): get_global_info() get_account_info() get_issue_token() get_rewards_info() Metric.metric(Metric.rank, rank) bp = 'bp: %s' % bp_name votes = 'votes: %s' % votes2eos(bp_vote_weight) rank_info = 'rank: %s' % rank vote_rate = 'rate: %s' % (bp_vote_weight / total_vote_weight) vote_weight = 'weight: %s' % int(bp_vote_weight) all_reward = vote_pay + block_pay reward_info = 'reward: %s' % (all_reward) claim_info = 'claim: %s' % last_claim_time_str un_pay = 'unpay: %s' % un_claim_pay if just_get_rewards is None: notify(bp, votes, rank_info, vote_rate, vote_weight, reward_info, claim_info, un_pay) else: print(all_reward)
def evaluate(self, loader): self.model.eval() loss, metric = 0, Metric() for words, feats, arcs, rels in loader: mask = words.ne(self.args.pad_index) # ignore the first token of each sentence mask[:, 0] = 0 arc_scores, rel_scores = self.model(words, feats) loss += self.get_loss(arc_scores, rel_scores, arcs, rels, mask) arc_preds, rel_preds = self.decode(arc_scores, rel_scores, mask) # ignore all punctuation if not specified if not self.args.punct: mask &= words.unsqueeze(-1).ne(self.puncts).all(-1) metric(arc_preds, rel_preds, arcs, rels, mask) loss /= len(loader) return loss, metric
def train_epoch(self): losses = Metric() pixel_loss = Metric() feat_loss = Metric() discr_loss = Metric() len_train = len(self.train_loader) pb = tqdm(self.train_loader) self.model.train() # Make sure the criterion is also set to the correct state self.criterion.train() for i, (lr, hr) in enumerate(pb): # Note that the lr here means low-resolution (images) # rather than learning rate lr, hr = lr.cuda(), hr.cuda() sr = self.model(lr) if i % 1 == 0: with self.criterion.iqa_loss.learner(): # Train the IQA model dl = self.discr_learn(hr, hr, 0.0) # Good-quality images dl += self.discr_learn(sr.detach(), hr) # Bad-quality images dl /= 2 discr_loss.update(dl, n=self.batch_size) # Train the SR model loss, pl, fl = self.criterion(sr, hr) self.optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_value_(self.model.parameters(), 0.1) self.optimizer.step() # Update data losses.update(loss.data, n=self.batch_size) pixel_loss.update(pl.data, n=self.batch_size) feat_loss.update(fl.data, n=self.batch_size) # Log for this mini-batch desc = "[{}/{}] Loss {loss.val:.4f} ({loss.avg:.4f}) " \ "DL {discr.val:.4f} ({discr.avg:.4f}) " \ "PL {pixel.val:.4f} ({pixel.avg:.4f}) " \ "FL {feat.val:.6f} ({feat.avg:.6f})"\ .format(i+1, len_train, loss=losses, discr=discr_loss, pixel=pixel_loss, feat=feat_loss) pb.set_description(desc) self.logger.dump(desc)
def validate_epoch(self, epoch=0, store=False): self.logger.show_nl("Epoch: [{0}]".format(epoch)) losses = Metric(self.criterion) ssim = ShavedSSIM(self.scale) psnr = ShavedPSNR(self.scale) len_val = len(self.val_loader) pb = tqdm(self.val_loader) to_image = self.dataset.tensor_to_image self.model.eval() self.criterion.eval() with torch.no_grad(): for i, (name, lr, hr) in enumerate(pb): if self.phase == 'train' and i >= 16: # Do not validate all images on training phase pb.close() self.logger.warning("validation ends early") break lr, hr = lr.unsqueeze(0).cuda(), hr.unsqueeze(0).cuda() sr = self.model(lr) losses.update(sr, hr) lr = to_image(lr.squeeze(0), 'lr') sr = to_image(sr.squeeze(0)) hr = to_image(hr.squeeze(0)) psnr.update(sr, hr) ssim.update(sr, hr) pb.set_description( "[{}/{}]" "Loss {loss.val:.4f} ({loss.avg:.4f}) " "PSNR {psnr.val:.4f} ({psnr.avg:.4f}) " "SSIM {ssim.val:.4f} ({ssim.avg:.4f})".format(i + 1, len_val, loss=losses, psnr=psnr, ssim=ssim)) self.logger.dump("[{}/{}]" "{} " "Loss {loss.val:.4f} ({loss.avg:.4f}) " "PSNR {psnr.val:.4f} ({psnr.avg:.4f}) " "SSIM {ssim.val:.4f} ({ssim.avg:.4f})".format( i + 1, len_val, name, loss=losses, psnr=psnr, ssim=ssim)) if store: # lr_name = self.path_ctrl.add_suffix(name, suffix='lr', underline=True) # hr_name = self.path_ctrl.add_suffix(name, suffix='hr', underline=True) sr_name = self.path_ctrl.add_suffix(name, suffix='sr', underline=True) # self.save_image(lr_name, lr, epoch) # self.save_image(hr_name, hr, epoch) self.save_image(sr_name, sr, epoch) return psnr.avg
def train_epoch(self): losses = Metric() pixel_loss = Metric() feat_loss = Metric() len_train = len(self.train_loader) pb = tqdm(self.train_loader) self.model.train() # Make sure the criterion is also set to the correct state self.criterion.train() for i, (lr, hr) in enumerate(pb): # Note that the lr here means low-resolution (images) # rather than learning rate lr, hr = lr.cuda(), hr.cuda() sr = self.model(lr) loss, pl, fl = self.criterion(sr, hr) losses.update(loss.data, n=self.batch_size) pixel_loss.update(pl.data, n=self.batch_size) feat_loss.update(fl.data, n=self.batch_size) self.optimizer.zero_grad() loss.backward() self.optimizer.step() desc = "[{}/{}] Loss {loss.val:.4f} ({loss.avg:.4f}) " \ "PL {pixel.val:.4f} ({pixel.avg:.4f}) " \ "FL {feat.val:.6f} ({feat.avg:.6f})"\ .format(i+1, len_train, loss=losses, pixel=pixel_loss, feat=feat_loss) pb.set_description(desc) self.logger.dump(desc)
def metric_collect(self, key, value): logger.info("%s %s %s" % (self.pid, key, value)) Metric.metric(key, value)
def main(): args = get_config() world_size = flow.env.get_world_size() if args.train_global_batch_size is None: args.train_global_batch_size = args.train_batch_size * world_size else: assert args.train_global_batch_size % args.train_batch_size == 0 if args.val_global_batch_size is None: args.val_global_batch_size = args.val_batch_size * world_size else: assert args.val_global_batch_size % args.val_batch_size == 0 flow.boxing.nccl.set_fusion_threshold_mbytes(args.nccl_fusion_threshold_mb) flow.boxing.nccl.set_fusion_max_ops_num(args.nccl_fusion_max_ops) if args.with_cuda: device = "cuda" else: device = "cpu" print("Device is: ", device) print("Creating Dataloader") train_data_loader = OfRecordDataLoader( ofrecord_dir=args.ofrecord_path, mode="train", dataset_size=args.train_dataset_size, batch_size=args.train_global_batch_size, data_part_num=args.train_data_part, seq_length=args.seq_length, max_predictions_per_seq=args.max_predictions_per_seq, consistent=args.use_consistent, ) test_data_loader = OfRecordDataLoader( ofrecord_dir=args.ofrecord_path, mode="test", dataset_size=1024, batch_size=args.val_global_batch_size, data_part_num=4, seq_length=args.seq_length, max_predictions_per_seq=args.max_predictions_per_seq, consistent=args.use_consistent, ) print("Building BERT Model") hidden_size = 64 * args.num_attention_heads intermediate_size = 4 * hidden_size bert_model = BertForPreTraining( args.vocab_size, args.seq_length, hidden_size, args.num_hidden_layers, args.num_attention_heads, intermediate_size, nn.GELU(), args.hidden_dropout_prob, args.attention_probs_dropout_prob, args.max_position_embeddings, args.type_vocab_size, ) # Load the same initial parameters with lazy model. # from utils.compare_lazy_outputs import load_params_from_lazy # load_params_from_lazy( # bert_model.state_dict(), # "../../OneFlow-Benchmark/LanguageModeling/BERT/initial_model", # ) assert id(bert_model.cls.predictions.decoder.weight) == id( bert_model.bert.embeddings.word_embeddings.weight ) ns_criterion = nn.CrossEntropyLoss(reduction="mean") mlm_criterion = nn.CrossEntropyLoss(reduction="none") if args.use_consistent: placement = flow.env.all_device_placement("cuda") bert_model = bert_model.to_consistent( placement=placement, sbp=flow.sbp.broadcast ) else: bert_model.to(device) ns_criterion.to(device) mlm_criterion.to(device) optimizer = build_optimizer( args.optim_name, bert_model, args.lr, args.weight_decay, weight_decay_excludes=["bias", "LayerNorm", "layer_norm"], clip_grad_max_norm=1, clip_grad_norm_type=2.0, ) steps = args.epochs * len(train_data_loader) warmup_steps = int(steps * args.warmup_proportion) lr_scheduler = PolynomialLR(optimizer, steps=steps, end_learning_rate=0.0) lr_scheduler = flow.optim.lr_scheduler.WarmUpLR( lr_scheduler, warmup_factor=0, warmup_iters=warmup_steps, warmup_method="linear" ) def get_masked_lm_loss( logit, masked_lm_labels, label_weights, max_predictions_per_seq, ): label_id = flow.reshape(masked_lm_labels, [-1]) # The `positions` tensor might be zero-padded (if the sequence is too # short to have the maximum number of predictions). The `label_weights` # tensor has a value of 1.0 for every real prediction and 0.0 for the # padding predictions. pre_example_loss = mlm_criterion(logit, label_id) pre_example_loss = flow.reshape(pre_example_loss, [-1, max_predictions_per_seq]) numerator = flow.sum(pre_example_loss * label_weights) denominator = flow.sum(label_weights) + 1e-5 loss = numerator / denominator return loss class BertGraph(nn.Graph): def __init__(self): super().__init__() self.bert = bert_model self.ns_criterion = ns_criterion self.masked_lm_criterion = partial( get_masked_lm_loss, max_predictions_per_seq=args.max_predictions_per_seq ) self.add_optimizer(optimizer, lr_sch=lr_scheduler) self._train_data_loader = train_data_loader if args.grad_acc_steps > 1: self.config.set_gradient_accumulation_steps(args.grad_acc_steps) if args.use_fp16: self.config.enable_amp(True) grad_scaler = flow.amp.GradScaler( init_scale=2 ** 30, growth_factor=2.0, backoff_factor=0.5, growth_interval=2000, ) self.set_grad_scaler(grad_scaler) self.config.allow_fuse_add_to_output(True) self.config.allow_fuse_model_update_ops(True) def build(self): ( input_ids, next_sentence_labels, input_mask, segment_ids, masked_lm_ids, masked_lm_positions, masked_lm_weights, ) = self._train_data_loader() input_ids = input_ids.to(device=device) input_mask = input_mask.to(device=device) segment_ids = segment_ids.to(device=device) next_sentence_labels = next_sentence_labels.to(device=device) masked_lm_ids = masked_lm_ids.to(device=device) masked_lm_positions = masked_lm_positions.to(device=device) masked_lm_weights = masked_lm_weights.to(device=device) # 1. forward the next_sentence_prediction and masked_lm model prediction_scores, seq_relationship_scores = self.bert( input_ids, segment_ids, input_mask, masked_lm_positions ) # 2-1. loss of is_next classification result next_sentence_loss = self.ns_criterion( seq_relationship_scores.reshape(-1, 2), next_sentence_labels.reshape(-1) ) masked_lm_loss = self.masked_lm_criterion( prediction_scores, masked_lm_ids, masked_lm_weights ) total_loss = masked_lm_loss + next_sentence_loss total_loss.backward() return ( seq_relationship_scores, next_sentence_labels, total_loss, masked_lm_loss, next_sentence_loss, ) bert_graph = BertGraph() class BertEvalGraph(nn.Graph): def __init__(self): super().__init__() self.bert = bert_model self._test_data_loader = test_data_loader self.config.allow_fuse_add_to_output(True) def build(self): ( input_ids, next_sent_labels, input_masks, segment_ids, masked_lm_ids, masked_lm_positions, masked_lm_weights, ) = self._test_data_loader() input_ids = input_ids.to(device=device) input_masks = input_masks.to(device=device) segment_ids = segment_ids.to(device=device) next_sent_labels = next_sent_labels.to(device=device) masked_lm_ids = masked_lm_ids.to(device=device) masked_lm_positions = masked_lm_positions.to(device) with flow.no_grad(): # 1. forward the next_sentence_prediction and masked_lm model _, seq_relationship_scores = self.bert( input_ids, input_masks, segment_ids ) return seq_relationship_scores, next_sent_labels bert_eval_graph = BertEvalGraph() train_total_losses = [] for epoch in range(args.epochs): metric = Metric( desc="bert pretrain", print_steps=args.loss_print_every_n_iters, batch_size=args.train_global_batch_size * args.grad_acc_steps, keys=["total_loss", "mlm_loss", "nsp_loss", "pred_acc"], ) # Train bert_model.train() for step in range(len(train_data_loader)): bert_outputs = pretrain(bert_graph, args.metric_local) if flow.env.get_rank() == 0: metric.metric_cb(step, epoch=epoch)(bert_outputs) train_total_losses.append(bert_outputs["total_loss"]) # Eval bert_model.eval() val_acc = validation( epoch, len(test_data_loader), bert_eval_graph, args.val_print_every_n_iters, args.metric_local, ) save_model(bert_model, args.checkpoint_path, epoch, val_acc, args.use_consistent)
avg_not += ans avg /= float(n * n) avg_have /= float(G.number_of_edges()) avg_not /= float(n * n - G.number_of_edges()) print "total average: " + str(avg) print "average with edges: " + str(avg_have) print "average without edges: " + str(avg_not) precision = {} recall = {} for k in xrange(1, 20): TP = FP = TN = FN = 0 for i in xrange(n): lst = [] for j in xrange(n): lst.append((res[i][j], j)) lst.sort(reverse=True) for j in xrange(k): if lst[j][1] in G[i]: TP += 1 else: FP += 1 FN = G.number_of_edges() - TP TN = n * n - FN - FP - TP r = Metric.cal_metric(TP, FP, TN, FN) precision[k] = r["precision"] recall[k] = r["recall"] Metric.draw_pr(precision, recall)
def test_medical(device, model=None, test_organ='liver'): assert cfg['panet']['backbone'] == 'unet' dataset_name = 'ircadb' if model is None: model = PANetFewShotSeg( in_channels=cfg[dataset_name]['channels'], pretrained_path=None, cfg={ 'align': True }, encoder_type=cfg['panet']['backbone']).to(device) load_state(cfg[dataset_name]['model_name'], model) model.eval() transforms = Compose([ Resize(size=cfg['panet']['unet_inp_size']), ]) test_dataset = get_visceral_medical_few_shot_dataset(organs=[test_organ], patient_ids=range( 16, 20), shots=5, mode='test', transforms=transforms) testloader = DataLoader(test_dataset, batch_size=1, shuffle=True, num_workers=1, pin_memory=True, drop_last=True) metric = Metric(max_label=20, n_runs=1) for i_iter, (support, query) in enumerate(tqdm(testloader, position=0, leave=True)): support_images = [[]] support_fg_mask = [[]] support_bg_mask = [[]] # for i in range(len(support)): support_bg = support[1] == 0 support_fg = support[1] == 1 support_images[0].append(support[0].to(device)) support_fg_mask[0].append(support_fg.float().to(device)) support_bg_mask[0].append(support_bg.float().to(device)) query_images = [] query_labels = [] # for i in range(len(query)): query_images.append(query[0].to(device)) query_labels.append(query[1].to(device)) query_labels = torch.cat(query_labels, dim=0).long().to(device) query_pred, _ = model(support_images, support_fg_mask, support_bg_mask, query_images) print("Support ", i_iter) plt.subplot(1, 2, 1) try: plt.imshow( np.moveaxis(support[0].squeeze().cpu().detach().numpy(), 0, 2)) except np.AxisError: plt.imshow(support[0].squeeze().cpu().detach().numpy()) plt.subplot(1, 2, 2) plt.imshow(support[1].squeeze()) plt.show() print("Query ", i_iter) plt.subplot(1, 3, 1) try: plt.imshow( np.moveaxis(query[0].squeeze().cpu().detach().numpy(), 0, 2)) except np.AxisError: plt.imshow(query[0].squeeze().cpu().detach().numpy()) plt.subplot(1, 3, 2) plt.imshow(query[1].squeeze()) plt.subplot(1, 3, 3) plt.imshow(np.array(query_pred.argmax(dim=1)[0].cpu())) metric.record(np.array(query_pred.argmax(dim=1)[0].cpu()), np.array(query_labels[0].cpu()), n_run=0) plt.show() classIoU, meanIoU = metric.get_mIoU(n_run=0) classIoU_binary, meanIoU_binary = metric.get_mIoU_binary(n_run=0) print('classIoU', classIoU.tolist()) print('meanIoU', meanIoU.tolist()) print('classIoU_binary', classIoU_binary.tolist()) print('meanIoU_binary', meanIoU_binary.tolist()) print('classIoU: {}'.format(classIoU)) print('meanIoU: {}'.format(meanIoU)) print('classIoU_binary: {}'.format(classIoU_binary)) print('meanIoU_binary: {}'.format(meanIoU_binary))
def main(): hidden_size = 64 * args.num_attention_heads # H = 64, size per head intermediate_size = hidden_size * 4 print("Create Bert model for SQuAD") squad_model = SQuAD( args.vocab_size, seq_length=args.seq_length, hidden_size=hidden_size, hidden_layers=args.num_hidden_layers, atten_heads=args.num_attention_heads, intermediate_size=intermediate_size, hidden_act=nn.GELU(), hidden_dropout_prob=args.hidden_dropout_prob, attention_probs_dropout_prob=args.attention_probs_dropout_prob, max_position_embeddings=args.max_position_embeddings, type_vocab_size=args.type_vocab_size, initializer_range=0.02, ) # Load pretrain model from lazy trained model load_params_from_lazy(squad_model.state_dict(), args.model_load_dir) squad_model.to(device) if args.do_train: print("Create SQuAD training data decoders") train_decoders = SquadDecoder( args.train_data_dir, batch_size, args.train_data_part_num, args.seq_length ) optimizer = build_adamW_optimizer( squad_model, args.learning_rate, args.weight_decay_rate, weight_decay_excludes=["bias", "LayerNorm", "layer_norm"], ) lr_scheduler = PolynomialLR( optimizer, steps=args.iter_num, end_learning_rate=0.0 ) warmup_batches = int(args.iter_num * args.warmup_proportion) lr_scheduler = flow.optim.lr_scheduler.WarmUpLR( lr_scheduler, warmup_factor=0, warmup_iters=warmup_batches, warmup_method="linear", ) class SQuADGraph(nn.Graph): def __init__(self): super().__init__() self.squad_model = squad_model self.criterion = nn.CrossEntropyLoss() self.add_optimizer(optimizer, lr_sch=lr_scheduler) self._decoders = train_decoders if args.use_fp16: self.config.enable_amp(True) grad_scaler = flow.amp.GradScaler( init_scale=2 ** 30, growth_factor=2.0, backoff_factor=0.5, growth_interval=2000, ) self.set_grad_scaler(grad_scaler) def build(self): ( input_ids, input_mask, segment_ids, start_positions, end_positions, ) = self._decoders() input_ids = input_ids.to(device=device) input_mask = input_mask.to(device=device) segment_ids = segment_ids.to(device=device) start_positions = start_positions.to(device=device) end_positions = end_positions.to(device=device) start_logits, end_logits = self.squad_model( input_ids, segment_ids, input_mask ) start_logits = flow.reshape(start_logits, [-1, args.seq_length]) end_logits = flow.reshape(end_logits, [-1, args.seq_length]) start_loss = self.criterion(start_logits, start_positions.squeeze(1)) end_loss = self.criterion(end_logits, end_positions.squeeze(1)) total_loss = (start_loss + end_loss) * 0.5 total_loss.backward() return total_loss squad_graph = SQuADGraph() for epoch in range(args.num_epochs): squad_model.train() metric = Metric( desc="train", print_steps=args.loss_print_every_n_iter, batch_size=batch_size, keys=["total_loss"], ) for step in range(epoch_size): metric.metric_cb(step, epoch=epoch)(squad_finetune(squad_graph)) if args.save_last_snapshot: save_model(squad_model, args.model_save_dir, "last_snapshot") if args.do_eval: assert os.path.isdir(args.eval_data_dir) print("Create SQuAD testing data decoders") test_decoders = SquadDecoder( args.eval_data_dir, eval_batch_size, args.eval_data_part_num, args.seq_length, is_train=False, ) squad_model.eval() class SQuADEvalGraph(nn.Graph): def __init__(self): super().__init__() self.squad_model = squad_model self._decoders = test_decoders def build(self): (input_ids, input_mask, segment_ids, unique_ids) = self._decoders() input_ids = input_ids.to(device=device) input_mask = input_mask.to(device=device) segment_ids = segment_ids.to(device=device) unique_ids = unique_ids.to(device=device) with flow.no_grad(): start_logits, end_logits = self.squad_model( input_ids, segment_ids, input_mask ) return unique_ids, start_logits, end_logits squad_eval_graph = SQuADEvalGraph() squad_eval(num_eval_steps, squad_eval_graph, args.loss_print_every_n_iter)
def main(): args = get_config() if args.with_cuda: device = flow.device("cuda") else: device = flow.device("cpu") print("Creating Dataloader") train_data_loader = OfRecordDataLoader( ofrecord_dir=args.ofrecord_path, mode="train", dataset_size=args.train_dataset_size, batch_size=args.train_batch_size, data_part_num=args.train_data_part, seq_length=args.seq_length, max_predictions_per_seq=args.max_predictions_per_seq, consistent=False, ) test_data_loader = OfRecordDataLoader( ofrecord_dir=args.ofrecord_path, mode="test", dataset_size=1024, batch_size=args.val_batch_size, data_part_num=4, seq_length=args.seq_length, max_predictions_per_seq=args.max_predictions_per_seq, consistent=False, ) print("Building BERT Model") hidden_size = 64 * args.num_attention_heads intermediate_size = 4 * hidden_size bert_model = BertForPreTraining( args.vocab_size, args.seq_length, hidden_size, args.num_hidden_layers, args.num_attention_heads, intermediate_size, nn.GELU(), args.hidden_dropout_prob, args.attention_probs_dropout_prob, args.max_position_embeddings, args.type_vocab_size, ) # Load the same initial parameters with lazy model. # from utils.compare_lazy_outputs import load_params_from_lazy # load_params_from_lazy( # bert_model.state_dict(), # "../../OneFlow-Benchmark/LanguageModeling/BERT/initial_model", # ) bert_model = bert_model.to(device) if args.use_ddp: bert_model = ddp(bert_model) optimizer = build_optimizer( args.optim_name, bert_model, args.lr, args.weight_decay, weight_decay_excludes=["bias", "LayerNorm", "layer_norm"], clip_grad_max_norm=1, clip_grad_norm_type=2.0, ) steps = args.epochs * len(train_data_loader) warmup_steps = int(steps * args.warmup_proportion) lr_scheduler = PolynomialLR(optimizer, steps=steps, end_learning_rate=0.0) lr_scheduler = flow.optim.lr_scheduler.WarmUpLR(lr_scheduler, warmup_factor=0, warmup_iters=warmup_steps, warmup_method="linear") ns_criterion = nn.CrossEntropyLoss(reduction="mean") mlm_criterion = nn.CrossEntropyLoss(reduction="none") def get_masked_lm_loss( logit_blob, masked_lm_positions, masked_lm_labels, label_weights, max_prediction_per_seq, ): # gather valid position indices logit_blob = flow.gather( logit_blob, index=masked_lm_positions.unsqueeze(2).repeat( 1, 1, args.vocab_size), dim=1, ) logit_blob = flow.reshape(logit_blob, [-1, args.vocab_size]) label_id_blob = flow.reshape(masked_lm_labels, [-1]) # The `positions` tensor might be zero-padded (if the sequence is too # short to have the maximum number of predictions). The `label_weights` # tensor has a value of 1.0 for every real prediction and 0.0 for the # padding predictions. pre_example_loss = mlm_criterion(logit_blob, label_id_blob) pre_example_loss = flow.reshape(pre_example_loss, [-1, max_prediction_per_seq]) numerator = flow.sum(pre_example_loss * label_weights) denominator = flow.sum(label_weights) + 1e-5 loss = numerator / denominator return loss train_total_losses = [] for epoch in range(args.epochs): metric = Metric( desc="bert pretrain", print_steps=args.loss_print_every_n_iters, batch_size=args.train_batch_size, keys=["total_loss", "mlm_loss", "nsp_loss", "pred_acc"], ) # Train bert_model.train() for step in range(len(train_data_loader)): bert_outputs = pretrain( train_data_loader, bert_model, ns_criterion, partial( get_masked_lm_loss, max_prediction_per_seq=args.max_predictions_per_seq, ), optimizer, lr_scheduler, ) if flow.env.get_rank() == 0: metric.metric_cb(step, epoch=epoch)(bert_outputs) train_total_losses.append(bert_outputs["total_loss"]) # Eval bert_model.eval() val_acc = validation(epoch, test_data_loader, bert_model, args.val_print_every_n_iters) save_model(bert_model, args.checkpoint_path, epoch, val_acc, False)
def test_panet(device, model=None, dataset_name='voc', test_organ='liver'): if model is None: # pretrained_path='../data/vgg16-397923af.pth' model = PANetFewShotSeg( in_channels=cfg[dataset_name]['channels'], pretrained_path=None, cfg={ 'align': True }, encoder_type=cfg['panet']['backbone']).to(device) load_state(cfg[dataset_name]['model_name'], model) model.eval() if dataset_name == 'voc': transforms = Compose([ Resize(size=cfg['panet']['vgg_inp_size']), ]) elif dataset_name == 'ircadb': transforms = Compose([ Resize(size=cfg['panet']['unet_inp_size']), ]) if dataset_name == 'voc': test_dataset = get_pascal_few_shot_datasets( range(16, 21), cfg['panet']['test_iterations'], cfg['nshot'], cfg['nquery'], transforms) elif dataset_name == 'ircadb': test_dataset = get_ircadb_few_shot_datasets( organs=[test_organ], patient_ids=range(16, 21), iterations=cfg['panet']['test_iterations'], N_shot=cfg['nshot'], N_query=cfg['nquery'], transforms=transforms) testloader = DataLoader(test_dataset, batch_size=1, shuffle=True, num_workers=1, pin_memory=True, drop_last=True) metric = Metric(max_label=20, n_runs=1) for i_iter, (support, query) in enumerate(testloader): support_images = [[]] support_fg_mask = [[]] support_bg_mask = [[]] for i in range(len(support)): support_images[0].append(support[i][0].to(device)) support_fg_mask[0].append(support[i][1].to(device)) support_bg_mask[0].append(support[i][2].to(device)) query_images = [] query_labels = [] for i in range(len(query)): query_images.append(query[i][0].to(device)) query_labels.append(query[i][1].to(device)) query_labels = torch.cat(query_labels, dim=0).long().to(device) query_pred, _ = model(support_images, support_fg_mask, support_bg_mask, query_images) print("Support ", i_iter) for i in range(len(support)): plt.subplot(1, 2 * len(support), 2 * i + 1) try: plt.imshow( np.moveaxis(support[i][0].squeeze().cpu().detach().numpy(), 0, 2)) except np.AxisError: plt.imshow(support[i][0].squeeze().cpu().detach().numpy()) plt.subplot(1, 2 * len(support), 2 * i + 2) plt.imshow(support[i][1].squeeze()) plt.show() print("Query ", i_iter) for i in range(len(query)): plt.subplot(1, 3 * len(query), 3 * i + 1) try: plt.imshow( np.moveaxis(query[i][0].squeeze().cpu().detach().numpy(), 0, 2)) except np.AxisError: plt.imshow(query[i][0].squeeze().cpu().detach().numpy()) plt.subplot(1, 3 * len(query), 3 * i + 2) plt.imshow(query[i][1].squeeze()) plt.subplot(1, 3 * len(query), 3 * i + 3) plt.imshow(np.array(query_pred.argmax(dim=1)[i].cpu())) metric.record(np.array(query_pred.argmax(dim=1)[i].cpu()), np.array(query_labels[i].cpu()), n_run=0) plt.show() classIoU, meanIoU = metric.get_mIoU(n_run=0) classIoU_binary, meanIoU_binary = metric.get_mIoU_binary(n_run=0) print('classIoU', classIoU.tolist()) print('meanIoU', meanIoU.tolist()) print('classIoU_binary', classIoU_binary.tolist()) print('meanIoU_binary', meanIoU_binary.tolist()) print('classIoU: {}'.format(classIoU)) print('meanIoU: {}'.format(meanIoU)) print('classIoU_binary: {}'.format(classIoU_binary)) print('meanIoU_binary: {}'.format(meanIoU_binary))
def __call__(self, args): super(Train, self).__call__(args) train = Corpus.load(args.ftrain, self.fields) dev = Corpus.load(args.fdev, self.fields) test = Corpus.load(args.ftest, self.fields) train = TextDataset(train, self.fields, args.buckets) dev = TextDataset(dev, self.fields, args.buckets) test = TextDataset(test, self.fields, args.buckets) # set the data loaders train.loader = batchify(train, args.batch_size, True) dev.loader = batchify(dev, args.batch_size) test.loader = batchify(test, args.batch_size) print(f"{'train:':6} {len(train):5} sentences, " f"{len(train.loader):3} batches, " f"{len(train.buckets)} buckets") print(f"{'dev:':6} {len(dev):5} sentences, " f"{len(dev.loader):3} batches, " f"{len(train.buckets)} buckets") print(f"{'test:':6} {len(test):5} sentences, " f"{len(test.loader):3} batches, " f"{len(train.buckets)} buckets") print("Create the model") self.model = Model(args).load_pretrained(self.WORD.embed) print(f"{self.model}\n") self.model = self.model.to(args.device) if torch.cuda.device_count() > 1: self.model = nn.DataParallel(self.model) self.optimizer = Adam(self.model.parameters(), args.lr, (args.mu, args.nu), args.epsilon) self.scheduler = ExponentialLR(self.optimizer, args.decay**(1 / args.decay_steps)) total_time = timedelta() best_e, best_metric = 1, Metric() for epoch in range(1, args.epochs + 1): start = datetime.now() # train one epoch and update the parameters self.train(train.loader) print(f"Epoch {epoch} / {args.epochs}:") loss, train_metric = self.evaluate(train.loader) print(f"{'train:':6} Loss: {loss:.4f} {train_metric}") loss, dev_metric = self.evaluate(dev.loader) print(f"{'dev:':6} Loss: {loss:.4f} {dev_metric}") loss, test_metric = self.evaluate(test.loader) print(f"{'test:':6} Loss: {loss:.4f} {test_metric}") t = datetime.now() - start # save the model if it is the best so far if dev_metric > best_metric and epoch > args.patience: best_e, best_metric = epoch, dev_metric if hasattr(self.model, 'module'): self.model.module.save(args.model) else: self.model.save(args.model) print(f"{t}s elapsed (saved)\n") else: print(f"{t}s elapsed\n") total_time += t if epoch - best_e >= args.patience: break self.model = Model.load(args.model) loss, metric = self.evaluate(test.loader) print(f"max score of dev is {best_metric.score:.2%} at epoch {best_e}") print(f"the score of test at epoch {best_e} is {metric.score:.2%}") print(f"average time of each epoch is {total_time / epoch}s") print(f"{total_time}s elapsed")
def main(): parser = argparse.ArgumentParser() parser.add_argument('--ds', type=str, default='sentiment140', choices=('sentiment140', 'tsad', 'semeval'), help='dataset on which to train') parser.add_argument( '--no-val', action='store_true', default=False, help= 'if specified, do not create a validation set and use it to choose the best model, ' 'instead just use the model from the final iteration') parser.add_argument( '--test-model', type=str, help= 'specify the filename of a pre-trained model, which will be loaded ' 'and evaluated on the test set of the specified dataset') parser.add_argument('--arch', type=str, choices=('cnn', 'lstm', 'cnn-lstm', 'lstm-cnn', 'textcnn', 'bilstm'), help='model architecture to use') parser.add_argument( '--nonlin', type=str, choices=('sign11', 'qrelu', 'relu', 'threshrelu'), help='non-linearity to use in the specified architecture') parser.add_argument('--loss', type=str, default='crossent', choices=('crossent', ), help='the loss function to use for training') parser.add_argument('--tp-rule', type=str, default='SoftHinge', choices=[e.name for e in targetprop.TPRule], help='the TargetProp rule to use') parser.add_argument('--batch', type=int, default=64, help='batch size to use for training') parser.add_argument('--epochs', type=int, default=100, help='number of epochs to train for') parser.add_argument('--opt', type=str, default='adam', choices=('sgd', 'rmsprop', 'adam'), help='optimizer to use to train') parser.add_argument('--lr', type=float, help='starting learning rate') parser.add_argument('--momentum', type=float, default=0.9, help='momentum amount for SGD') parser.add_argument('--wtdecay', type=float, default=0, help='weight decay (L2 regularization) amount') parser.add_argument( '--lr-decay', type=float, default=0.1, help= 'factor by which to multiply the learning rate at each value in <lr-decay-epochs>' ) parser.add_argument( '--lr-decay-epochs', type=int, nargs='+', default=None, help= 'list of epochs at which to multiply the learning rate by <lr-decay>') parser.add_argument('--gpus', type=int, default=[0], nargs='+', help='which GPU device ID(s) to train on') parser.add_argument('--no-cuda', action='store_true', default=False, help='if specified, use CPU only') arg_list = deepcopy(sys.argv) args = parser.parse_args() if args.arch is None or args.nonlin is None or args.lr is None: print('ERROR: arch, nonlin, and lr arguments must be specified\n') parser.print_help() exit(-1) assert (args.ds == 'semeval') == (args.arch == 'bilstm') uses_tp = (args.nonlin == 'sign11' or args.nonlin == 'qrelu') curtime = datetime.now().strftime('%Y.%m.%d_%H.%M.%S') arch_str = args.arch op_str = args.opt + '_lr{}_mu{}_wd{}{}'.format( args.lr, args.momentum, args.wtdecay, 'noval' if args.no_val else '') tp_str = args.nonlin + ('-' + args.tp_rule if uses_tp else '') args.save = os.path.join( 'logs', args.ds, curtime + '.' + arch_str + '.' + args.loss + '.' + op_str + '.' + tp_str) if args.test_model: args.save = args.save + '_test' args.no_val = True args.tp_rule = targetprop.TPRule[args.tp_rule] gpu_str = ','.join(str(g) for g in args.gpus) if not args.no_cuda: os.environ['CUDA_VISIBLE_DEVICES'] = gpu_str args.cuda = not args.no_cuda and torch.cuda.is_available() np.random.seed(seed) torch.manual_seed(seed) if args.cuda: torch.cuda.manual_seed(seed) print('using cuda device{}: {}'.format( 's' if len(args.gpus) > 1 else '', gpu_str)) if os.path.exists(args.save): shutil.rmtree(args.save) os.makedirs(args.save, exist_ok=True) log_formatter = logging.Formatter( '%(asctime)s.%(msecs)03d [%(levelname)-5.5s] %(message)s', datefmt='%Y.%m.%d %H:%M:%S') root_logger = logging.getLogger() root_logger.setLevel(logging.DEBUG) print("logging to file '{}.log'".format(args.save)) file_handler = logging.FileHandler(args.save + '.log') file_handler.setFormatter(log_formatter) file_handler.setLevel(logging.DEBUG) root_logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) console_handler.setLevel(logging.DEBUG) root_logger.addHandler(console_handler) logging.info('command line call: {}'.format(" ".join(arg_list))) logging.info('arguments: {}'.format(args)) if not args.test_model: logging.info("training a deep network with the specified arguments") else: logging.info("testing model '{}' with the specified arguments".format( args.test_model)) backcompat.broadcast_warning.enabled = True warnings.filterwarnings("ignore", "volatile was removed and now has no effect") # ----- create datasets ----- train_loader, val_loader, test_loader, num_classes, embedding_vector = \ create_datasets(args.ds, args.batch, args.no_val, args.cuda, seed) metrics = { 'loss': Metric('loss', float('inf'), False), 'acc': Metric('acc', 0.0, True), 'p': Metric('p', 0.0, True), 'r': Metric('r', 0.0, True), 'f1': Metric('f1', 0.0, True) } if args.ds == 'semeval': metrics['f_0'] = Metric('f_0', 0.0, True) metrics['f_1'] = Metric('f_1', 0.0, True) metrics['f_avg'] = Metric('f_avg', 0.0, True) metrics = { 'train': deepcopy(metrics), 'val': deepcopy(metrics), 'test': deepcopy(metrics) } # ----- create loss function ----- loss_function = get_loss_function(args.loss) # either train a model from scratch or load and evaluate a model from disk if not args.test_model: # ----- create model ----- model = create_model(args, num_classes, embedding_vector) logging.info('created {} model:\n {}'.format(arch_str, model)) logging.info("{} model has {} parameters".format( arch_str, sum([p.data.nelement() for p in model.parameters()]))) print('num params: ', {n: p.data.nelement() for n, p in model.named_parameters()}) # ----- create optimizer ----- optimizer = get_optimizer(model, args) if val_loader: logging.info( 'evaluating training on validation data (train size = {}, val size = {}, test size = {})' .format(len(train_loader.dataset), len(val_loader.dataset), len(test_loader.dataset))) else: logging.info( 'not using validation data (train size = {}, test size = {})'. format(len(train_loader.dataset), len(test_loader.dataset))) cudnn.benchmark = True # ----- train the model ----- timers = {} best_model_state = train_model(args.epochs, model, optimizer, loss_function, train_loader, None if args.no_val else val_loader, test_loader, args.cuda, args.lr_decay, args.lr_decay_epochs, args.save, metrics, args, timers) logging.info('testing on trained model ({})'.format( 'final' if args.no_val else 'best')) model.load_state_dict(best_model_state) test_model(model, loss_function, test_loader, args.cuda, True, use_target=args.ds == 'semeval', use_f=args.ds == 'semeval') else: model = create_model(args, num_classes, embedding_vector) logging.info("loading test model from '{}'".format(args.test_model)) state = torch.load(args.test_model, map_location='cpu') model.load_state_dict(state['model_state']) test_model(model, loss_function, test_loader, args.cuda, True, use_target=args.ds == 'semeval', use_f=args.ds == 'semeval') print('') logging.info("log file: '{}.log'".format(args.save)) logging.info("log dir: '{}'".format(args.save)) if not args.test_model: logging.info("best F score model: '{}'".format( os.path.join(args.save, best_model_name)))
class Metrics: metrics = [] # region [Metrics] timezone = Metric("Timezone", tz_processor) metrics.append(timezone) # Jvm Heap size yg_allocated = Metric("Young gen: allocated", yg_allocated_processor) yg_allocated.legend = " KB" metrics.append(yg_allocated) yg_peak = Metric("Young gen: peak", yg_peak_processor) yg_peak.legend = " KB" metrics.append(yg_peak) og_allocated = Metric("Old gen: allocated", og_allocated_processor) og_allocated.legend = " KB" metrics.append(og_allocated) og_peak = Metric("Old gen: peak", og_peak_processor) metrics.append(og_peak) meta_allocated = Metric("Metaspace: allocated", meta_allocated_processor) metrics.append(meta_allocated) meta_peak = Metric("Metaspace: peak", meta_peak_processor) metrics.append(meta_peak) yom_allocated = Metric("Young + Old + Meta: allocated", yom_allocated_processor) metrics.append(yom_allocated) # dont use this yom_peak = Metric("Young + Old + Meta: peak", yom_peak_processor) # and this one metrics.append(yom_peak) # as these are just sum of max values heap_allocated = Metric("Heap: allocated", heap_allocated_processor) metrics.append(heap_allocated) heap_peak = Metric("Heap: peak", heap_peak_processor) metrics.append(heap_peak) pause_avg = Metric("Pause GC time: Average", pause_avg_processor) metrics.append(pause_avg) pause_max = Metric("Pause GC time: Max", pause_max_processor) metrics.append(pause_max) pause_duration_timerange = Metric( "Pause duration timerange array, split by 100ms range: 0-9 wheres the last one contains > 1 sec values", pause_duration_timerange_processor) metrics.append(pause_duration_timerange) minor_gc_count = Metric("Minor gc: count", minor_gc_count_processor) metrics.append(minor_gc_count) minor_gc_reclaimed = Metric("Minor gc: reclaimed", minor_gc_reclaimed_processor) metrics.append(minor_gc_reclaimed) minor_gc_time_total = Metric("Minor gc time: total", minor_gc_time_total_processor) metrics.append(minor_gc_time_total) minor_gc_time_avg = Metric("Minor gc time: avg", minor_gc_time_avg_processor) metrics.append(minor_gc_time_avg) minor_gc_time_stddev = Metric("Minor gc time: standard deviation", minor_gc_time_std_dev_processor) metrics.append(minor_gc_time_stddev) minor_gc_time_min = Metric("Minor gc time: Min", minor_gc_time_min_processor) metrics.append(minor_gc_time_min) minor_gc_time_max = Metric("Minor gc time: Min, Max", minor_gc_time_max_processor) metrics.append(minor_gc_time_max) minor_gc_interval_average = Metric("Minor gc: average interval", minor_gc_interval_average_processor) metrics.append(minor_gc_interval_average) # full_gc_count = Metric("full gc: count", full_gc_count_processor) metrics.append(full_gc_count) full_gc_reclaimed = Metric("full gc: reclaimed", full_gc_reclaimed_processor) metrics.append(full_gc_reclaimed) full_gc_time_total = Metric("full gc time: total", full_gc_time_total_processor) metrics.append(full_gc_time_total) full_gc_time_avg = Metric("full gc time: avg", full_gc_time_avg_processor) metrics.append(full_gc_time_avg) full_gc_time_stddev = Metric("full gc time: standard deviation", full_gc_time_std_dev_processor) metrics.append(full_gc_time_stddev) full_gc_time_max = Metric("full gc time: Min, Max", full_gc_time_max_processor) metrics.append(full_gc_time_max) full_gc_time_min = Metric("full gc time: Min, Max", full_gc_time_min_processor) metrics.append(full_gc_time_min) full_gc_interval_average = Metric("full gc: average interval", full_gc_interval_average_processor) metrics.append(full_gc_interval_average) total_pause_average = Metric("Total GC pause: average", total_pause_average_processor) metrics.append(total_pause_average) total_pause_stdev = Metric("Total GC pause: stddev", total_pause_stdev_processor) metrics.append(total_pause_stdev) total_pause_interval_average = Metric( "Total GC pause: average interval", total_pause_interval_average_processor) metrics.append(total_pause_interval_average) """ Total: count = full+minor counts Reclaimed = full+minor reclaimed time: f+m avg_time: new stdev: new min_max: min(mins), max(maxes) interval_avg: new """ pause_min = Metric("Pause: min", pause_min_processor) metrics.append(pause_min) ## object stats: total_created_bytes = Metric("Total created, kilobytes:", total_created_bytes_processor) metrics.append(total_created_bytes) total_promoted_bytes = Metric("Total promoted, kilobytes:", total_promoted_bytes_processor) metrics.append(total_promoted_bytes) average_creation_rate = Metric("Average creation rate:", average_creation_rate_processor) metrics.append(average_creation_rate) average_promotion_rate = Metric("Average promotion rate:", average_promotion_rate_processor) metrics.append(average_promotion_rate) total_program_duration = Metric("Duration:", total_program_duration_processor) metrics.append(total_program_duration) filename = Metric("Filename:", filename_processor) metrics.append(filename)