def exp(model, n_epochs, opu, batch_size, binary_layer, lr, optimizer, weight_decay, smoke_test, opu_output, opu_input, is_scheduler, dataset, features_model, milestones): device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(f'- Current torch.device is: {device}') net, file_name = get_model(model=model, binary_layer=binary_layer, n_epochs=n_epochs, opu_output=opu_output, opu_input=opu_input, device=device, dataset=dataset, opu=opu) if features_model is not None: net = features_loading(net, dataset, device, features_model) if optimizer == 'SGD': print( f'- Optimizer = {optimizer}, Starting lr = {lr}, Momentum = {0.9}, Weight decay = {weight_decay}' ) optimizer = torch.optim.SGD(params=net.parameters(), lr=lr, momentum=0.9, weight_decay=weight_decay) else: print( f'- Optimizer = {optimizer}, Starting lr = {lr}, Weight decay = {weight_decay}' ) optimizer = torch.optim.Adam(params=net.parameters(), lr=lr, weight_decay=weight_decay) if smoke_test: train_samples = 5000 else: train_samples = None if dataset == 'cifar10': train_dl, test_dl = cifar10(batch_size=batch_size, num_workers=16, subsample=train_samples) if dataset == 'cifar100': train_dl, test_dl = cifar100(batch_size=batch_size, num_workers=16, subsample=train_samples) net = training(net.to(device), train_dl, test_dl, device, n_epochs, optimizer, is_scheduler, milestones) train_acc = compute_score(net, train_dl, device) test_acc = compute_score(net, test_dl, device) print(f'- Train acc {train_acc:.2f}%, Test acc {test_acc:.2f}%') return train_acc, test_acc, net, file_name
def train(train_loader, model, optim, epoch, device, logger, moving_loss): model.train() loss = nn.CrossEntropyLoss() smooth_const = 0.1 batches = len(train_loader) start = time.time() for step, (v, q, a, gt, _, _) in enumerate(train_loader): data_time = time.time() - start v = v.to(device) q = q.to(device) a = a.to(device) gt = gt.to(device) logits = model(v, q, a) output = loss(logits, gt) optim.zero_grad() output.backward() nn.utils.clip_grad_norm_(model.parameters(), 0.25) optim.step() moving_loss = (output.item() if epoch == 0 and step ==0 else (1 - smooth_const) * moving_loss + smooth_const * output.item()) batch_time = time.time() - start score = compute_score(logits, gt) logger.batch_info(epoch, step, batches, data_time, moving_loss, score, batch_time) start = time.time() return moving_loss
def train(train_loader, model, optim, epoch, device, logger): model.train() batches = len(train_loader) start = time.time() for step, (v, q, a, q_lens, _, _) in enumerate(train_loader): data_time = time.time() - start v = v.to(device) q = q.to(device) a = a.to(device) q_lens = q_lens.to(device) logits = model(v, q, q_lens) loss = F.binary_cross_entropy_with_logits(logits, a) * a.size(1) optim.zero_grad() loss.backward() nn.utils.clip_grad_norm_(model.parameters(), 0.25) optim.step() batch_time = time.time() - start score = compute_score(logits, a) logger.batch_info(epoch, step, batches, data_time, loss.item(), score, batch_time) start = time.time()
def test_epoch_end(self, outputs): avg_loss = torch.stack([x['test_loss'] for x in outputs]).mean() sum_correct = sum([x['test_correct'] for x in outputs]) all_pred = np.array(list(chain(*[x['test_id_pred'] for x in outputs]))) logits = nn.Sigmoid()(torch.Tensor( list(chain(*[x['test_logits'] for x in outputs])))).reshape(-1, 1) y_true = np.array(list(chain(*[x['test_y_true'] for x in outputs]))) score, acc = compute_score(self.ds_test, all_pred) #compute_precision_recall(y_true, logits) logits = np.hstack((np.zeros((logits.shape[0], 1)), logits)) for l in logits: l[0] = 1 - l[1] logger.info(score) f1 = f1_score(y_true, all_pred[:, 1], labels=np.unique(all_pred)) # Precision Recall self.logger.log_metrics({ 'pr': wandb.plots.precision_recall(y_true, logits, ["no_target", "target"]) }) logs = { 'test_avg_loss': avg_loss, 'test_acc': acc, 'test_f1_score': f1 } return {'test_loss': avg_loss, 'log': logs}
def evaluate(val_loader, model, epoch, device, logger, r): model.eval() root = r idx2ans, _ = pickle.load(open(os.path.join(root, 'dict_ans.pkl'), 'rb')) preds_d = dict() batches = len(val_loader) for step, (v, q, a, q_lens, _, _, q_id) in enumerate(tqdm(val_loader, ascii=True)): v = v.to(device) q = q.to(device) a = a.to(device) q_lens = q_lens.to(device) logits = model(v, q, q_lens) # (batch_size, num_ans) loss = F.binary_cross_entropy_with_logits(logits, a) * a.size(1) score = compute_score(logits, a) preds = torch.max(logits, 1)[1].data for idx, pred, ans in zip(q_id, preds, a.cpu().numpy()): #preds_d[idx] = (idx2ans[pred], ans[pred]) preds_d[idx] = idx2ans[pred] logger.batch_info_eval(epoch, step, batches, loss.item(), score) pickle.dump(preds_d, open('base_model_preds.pkl', 'wb')) score = logger.batch_info_eval(epoch, -1, batches) return score
def solve_m_roommates(n, m, preferences): """ Group the n people by m. Start by a random solution and make some 2-permutations. """ start = list(range(n)) shuffle(start) solution = [[start[i + j] for j in range(0, m)] for i in range(0, n, m)] min_score = utils.compute_score(preferences, solution) print(f"Basic solution: {min_score}") no_progress_round = 0 while no_progress_round < 100: a1 = randint(0, n - 1) a2 = randint(0, n - 1) solution[a1 // m][a1 % m], solution[a2 // m][a2 % m] = solution[a2 // m][a2 % m], solution[a1 // m][a1 % m] new_score = utils.compute_score(preferences, solution) if new_score < min_score: no_progress_round = 0 min_score = new_score else: solution[a1 // m][a1 % m], solution[a2 // m][a2 % m] = solution[a2 // m][a2 % m], solution[a1 // m][a1 % m] no_progress_round += 1 return solution
def test(self, val_loader, epoch, silence=False): batch_time = AverageMeter() # switch to evaluate mode self.model.eval() end = time.time() sample_cnt = 0 all_loss, all_correct, all_tp, all_fp, all_tn, all_fn = 0, 0, 0, 0, 0, 0 y_test, y_score, y_pred, y_prob = [], [], [], [] for i, (input, target, name) in enumerate(val_loader): input_var = torch.autograd.Variable(input, volatile=True) target_var = torch.autograd.Variable( target, volatile=True).float().unsqueeze(1) sample_cnt += target_var.size(0) output = self.model(input_var) prob = torch.sigmoid(output) y_test.append(target_var.data.numpy().tolist()) y_prob.append(prob.data.numpy().tolist()) loss = self.criterion(output, target_var) correct, tp, fp, tn, fn = compute_score(output, target_var) print('file :{} correct:{} probdata:{}'.format( name, correct, prob.data[0].numpy())) all_correct += correct all_tp += tp all_fp += fp all_tn += tn all_fn += fn all_loss += loss.data[0] # measure elapsed time batch_time.update(time.time() - end) end = time.time() if not silence: accuracy = all_correct / sample_cnt if all_tp + all_fp != 0: precision = float(all_tp) / (all_tp + all_fp) else: precision = 0 if all_tp + all_fn != 0: recall = float(all_tp) / (all_tp + all_fn) else: recall = 0 f1 = 2 * precision * recall / (precision + recall) y_test = np.array(y_test) y_prob = np.array(y_prob) flat_test, flat_prob = np.reshape(y_test, -1), np.reshape(y_prob, -1) print('flat test:{}'.format(flat_test)) print('flat prob:{}'.format(flat_prob)) # roc_auc = roc_auc_score(flat_test,flat_prob) roc_auc = 0.9 print('epoch {} evaluation accuracy:{}, total_cnt:{}, precision:{}, recall:{}, f1:{}, roc:{} tp{} fp{} tn{} fn{}'.format(epoch, \ accuracy, sample_cnt , precision, recall, f1, roc_auc, all_tp/sample_cnt, all_fp/sample_cnt, all_tn/sample_cnt, all_fn/sample_cnt)) return all_loss, accuracy
def solve_2_roommates_conv(n, m, preferences): """ Start from a random solution and make 2-permutation to have a better score """ start = list(range(n)) shuffle(start) solution = [[start[i], start[i + 1]] for i in range(0, n, 2)] min_score = utils.compute_score(preferences, solution) print(f"Basic solution: {min_score}") no_progress_round = 0 while no_progress_round < 30: a1 = randint(0, n - 1) a2 = randint(0, n - 1) solution[a1 // 2][a1 % 2], solution[a2 // 2][a2 % 2] = solution[a2 // 2][a2 % 2], solution[a1 // 2][a1 % 2] new_score = utils.compute_score(preferences, solution) if new_score < min_score: no_progress_round = 0 min_score = new_score else: solution[a1 // 2][a1 % 2], solution[a2 // 2][a2 % 2] = solution[a2 // 2][a2 % 2], solution[a1 // 2][a1 % 2] no_progress_round += 1 return solution
def acc_and_idx(model, binary_layer, opu, n_epochs, opu_output, opu_input, device, dataset): net, net_name = get_model(model=model, binary_layer=binary_layer, opu=opu, n_epochs=n_epochs, opu_output=opu_output, opu_input=opu_input, sign_back=False, device=device, dataset=dataset) path = 'results/c' + dataset[5:] + '/models/' net.load_state_dict(torch.load(path + net_name + '.pt')) net.eval() if dataset == 'cifar10': dataloader = c10_test else: dataloader = c100_test acc, indexes = compute_score(net, dataloader, device=device, save_correct_labels=True) return acc, indexes
def train(self, train_loader, epoch): batch_time = AverageMeter() data_time = AverageMeter() # switch to train mode self.model.train() lr = adjust_learning_rate(self.optimizer, self.args.lr, self.args.decay_rate, epoch, self.args.epochs) # TODO: add custom print('Epoch {:3d} lr = {:.6e}'.format(epoch, lr)) end = time.time() sample_cnt, all_loss, all_correct, all_tp, all_fp, all_tn, all_fn = 0, 0, 0, 0, 0, 0, 0 for i, (input, target, name) in enumerate(train_loader): # measure data loading time data_time.update(time.time() - end) #target = target.cuda(async=True) input_var = torch.autograd.Variable(input) target_var = torch.autograd.Variable(target).float().unsqueeze(1) # compute output output = self.model(input_var) loss = self.criterion(output, target_var) sample_cnt += target_var.size(0) correct, tp, fp, tn, fn = compute_score(output, target_var) all_correct += correct all_tp += tp all_fp += fp all_tn += tn all_fn += fn all_loss += loss.data[0] # compute gradient and do SGD step self.optimizer.zero_grad() loss.backward() self.optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() accuracy = all_correct / sample_cnt if all_tp + all_fp != 0: precision = float(all_tp) / (all_tp + all_fp) else: precision = 0 if all_tp + all_fn != 0: recall = float(all_tp) / (all_tp + all_fn) else: recall = 0 f1 = 2 * precision * recall / (precision + recall) print('epoch {} training accuracy:{}, total_cnt:{}, precision:{}, recall:{}, f1:{}'.format(\ epoch, accuracy, sample_cnt, precision, recall, f1)) return all_loss, accuracy, lr
def training(net, train_dl, test_dl, device, n_epochs, optimizer, is_scheduler, milestones): if is_scheduler: scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=2, factor=0.5, threshold=0.01) if milestones: scheduler2 = torch.optim.lr_scheduler.MultiStepLR(optimizer, [45, 60], gamma=0.5) print(f'- Total number of epochs = {n_epochs}') for e in range(n_epochs): loss_stat = 0 for i, (x, y) in enumerate(train_dl): x, y = x.to(device), y.to(device) optimizer.zero_grad() y_hat = net(x) loss = torch.nn.functional.cross_entropy(y_hat, y) loss.backward() optimizer.step() loss_stat += loss.item() loss_stat = loss_stat / len(train_dl) if is_scheduler: scheduler.step(loss_stat) if milestones: scheduler2.step() train_score = compute_score(net, train_dl, device) test_score = compute_score(net, test_dl, device) print( f'Epoch {e + 1}, loss = {loss_stat:.3f}, train = {train_score:.2f}%, ' f'test = {test_score:.2f}% lr = {optimizer.param_groups[0]["lr"]}') return net
def _update_subject_patch(self, revised_subject_patch_dict, selected_candidate_id): # patch_category relates to priority selected_modified_method = revised_subject_patch_dict[ selected_candidate_id]["modified_method"] selected_patch_quality = PATCH_CATEGORY_QUALITY_DICT[ revised_subject_patch_dict[selected_candidate_id] ["patch_category"]] revised_subject_patch_dict[selected_candidate_id]["validated"] = True # modified_method: computed_score cached_result = {} for id, patch_data in revised_subject_patch_dict.items(): if not revised_subject_patch_dict[id]["validated"]: cur_modified_method = revised_subject_patch_dict[id][ "modified_method"] if cur_modified_method == selected_modified_method: num_match, num_diff = [1, 0] else: num_match, num_diff = [0, 1] if selected_patch_quality == "GOOD": revised_subject_patch_dict[id][ "true_positive"] += num_match revised_subject_patch_dict[id][ "false_positive"] += num_diff if selected_patch_quality == "BAD": revised_subject_patch_dict[id][ "true_negative"] += num_match revised_subject_patch_dict[id][ "false_negative"] += num_diff if cur_modified_method not in cached_result: computed_score = compute_score( revised_subject_patch_dict[id]["true_positive"], revised_subject_patch_dict[id]["false_positive"], revised_subject_patch_dict[id]["true_negative"], revised_subject_patch_dict[id]["false_negative"], self._formula) cached_result[cur_modified_method] = computed_score else: computed_score = cached_result[cur_modified_method] revised_subject_patch_dict[id]["priority"] = ( revised_subject_patch_dict[id]["init_priority"] + computed_score)
def evaluate(val_loader, model, epoch, device, logger): model.eval() batches = len(val_loader) for step, (v, q, a, _, _) in enumerate(tqdm(val_loader, ascii=True)): v = v.to(device) q = q.to(device) a = a.to(device) logits = model(v, q) loss = F.binary_cross_entropy_with_logits(logits, a) * a.size(1) score = compute_score(logits, a) logger.batch_info_eval(epoch, step, batches, loss.item(), score) score = logger.batch_info_eval(epoch, -1, batches) return score
def step(i): global p1, p2, v1, v2 mask1, mask2 = lines_mask(p1, p2, ps, thickness, expand) mask_all = mask1 + mask2 dp1 = 0 dp2 = 0 dp1_pan = compute_pan(grad, mask1, pan_m) dp2_pan = compute_pan(grad, mask2, pan_m) dp1 += dp1_pan dp2 += dp2_pan dp1_rot, dp2_rot = compute_rot(p1, p2, ps, grad, mask_all, rot_m) dp1 += dp1_rot dp2 += dp2_rot dp1_spr, dp2_spr = compute_spring(p1, p2, spr_m, l / 50, l / 10) dp1 += dp1_spr dp2 += dp2_spr v1 += dp1 * decent_rate / momentum v2 += dp2 * decent_rate / momentum v1 *= 1.0 - damping v2 *= 1.0 - damping p1 += v1 p2 += v2 movement = np.sqrt(np.sum(np.square(v1))) + np.sqrt(np.sum(np.square(v2))) movements.append(movement) score = compute_score(mask_all, edges) * (10**4) # score = compute_score(mask_all,edges) scores.append(score) print(f"step={len(movements)}, movement={movement:.2f}, score={score:.2f}") plot() if len(movements) > 5: if np.mean(np.array(movements[-5:])) <= 0.15: animate.event_source.stop()
def end_game(self, end_time): """End the game -- store the end_time for score calculation, put score in database if player won the game""" self.game_over = True self.end_time = end_time self.put() if self.pairs == NUMBER_OF_PAIRS: diff = self.end_time - self.start_time time = diff.days * 86400 + diff.seconds score = Score(user = self.user, date = self.start_time.date(), time = time, steps = self.steps, score = compute_score(time, self.steps)) user = self.user.get() user.score += score.score user.put() score.put()
def end_game(self, end_time): """End the game -- store the end_time for score calculation, put score in database if player won the game""" self.game_over = True self.end_time = end_time self.put() if self.pairs == NUMBER_OF_PAIRS: diff = self.end_time - self.start_time time = diff.days * 86400 + diff.seconds score = Score(user=self.user, date=self.start_time.date(), time=time, steps=self.steps, score=compute_score(time, self.steps)) user = self.user.get() user.score += score.score user.put() score.put()
def evaluate(val_loader, model, epoch, device, logger): model.eval() loss = nn.CrossEntropyLoss() batches = len(val_loader) for step, (v, q, a, gt, _, _) in enumerate(tqdm(val_loader, ascii=True)): v = v.to(device) q = q.to(device) a = a.to(device) gt = gt.to(device) logits = model(v, q, a) output = loss(logits, gt) score = compute_score(logits, gt) logger.batch_info_eval(epoch, step, batches, output.item(), score) score = logger.batch_info_eval(epoch, -1, batches) return score
def evaluate(val_loader, model, epoch, device, logger): model.eval() preds_d = [] batches = len(val_loader) for step, (j, a, q_id) in enumerate(tqdm(val_loader, ascii=True)): j = j.to(device) a = a.to(device) logits = model(j) loss = F.binary_cross_entropy_with_logits(logits, a) * a.size(1) scores, score = compute_score(logits, a) preds_d += [(idx.item(), pred) for idx, pred in zip(q_id, scores)] logger.batch_info_eval(epoch, step, batches, loss.item(), score) pickle.dump(preds_d, open('tr_nonyn_yn_model_preds.pkl', 'wb')) score = logger.batch_info_eval(epoch, -1, batches) return score
def get_rmses(self, train, preds, trues): train_x, train_y = features_target_split(df=train, drop_index=self.drop_index) err_t = utils.compute_score(self.predict(train_x), train_y) err_v = utils.compute_score(preds, trues) return err_t, err_v
def assembletests(self, moralflag, path): outlist = [] myarrtrans = zip(*self.allexperiments) input_dict = {} for j in range(len(self.cols) - 1): y = set(myarrtrans[j]) for index, value, flag in [tup for tup in path if tup[0] == j]: if flag: if isinstance(value, str): y = {value} else: y = {element for element in y if element >= value} else: if isinstance(value, str): y = y - {value} else: y = {element for element in y if element < value} outlist.append(list(y)) input_dict[self.my_inputs[j]] = y costs = [] experiments = [] isempty = False for key in input_dict: if len(input_dict[key]) == 0: isempty = True if not isempty: if self.use_score: permutations = load_permutations(input_dict) else: permutations = load_combinatorial(input_dict) for d in permutations: x = [] for param in self.my_inputs: value = d[param] x.append(value) if (x not in self.expers): experiments.append(x) costs.append( compute_score(x, self.my_inputs, self.pv_goodness, moralflag)) else: logging.debug("Skipping: " + str(x)) # Executing experiments in ascending order of costs allrets = [] indices = [t[0] for t in sorted(enumerate(costs), key=lambda x: x[1])] if len(indices) > self.k: indices = indices[:self.k] requests = set() for i in indices: if (len(self.allexperiments) + len(requests)) < self.max_iter: e = experiments[i] self.workflow(e) if self.is_poller_not_sync: time.sleep(1) self.is_poller_not_sync = False requests.add(tuple(e)) while len(requests) > 0: if len(requests) > self.max_instances: self.max_instances = len(requests) socks = dict(self.poller.poll(1000)) if socks: if socks.get(self.receiver) == zmq.POLLIN: msg = self.receiver.recv(zmq.NOBLOCK) exp = ast.literal_eval(msg) self.allexperiments.append(exp) result_value = exp[-1] for i in range(len(self.my_inputs)): key = self.my_inputs[i] v = exp[i] if key not in self.pv_goodness: self.pv_goodness[key] = {} if v not in self.pv_goodness[key]: self.pv_goodness[key][v] = {'good': 0, 'bad': 0} if eval(result_value): self.pv_goodness[key][v]['good'] += 1 else: self.pv_goodness[key][v]['bad'] += 1 requests.discard(tuple(exp[:-1])) x = copy.deepcopy(exp) x[-1] = eval(x[-1]) allrets.append(x[-1]) self.allresults.append(x) self.expers.append(x[:-1]) self.rets.append(x[-1]) else: for tup in requests: exp = list(tup) # self.workflow(exp) if self.is_poller_not_sync: time.sleep(1) self.is_poller_not_sync = False if (1 == len(set(allrets))): if ((moralflag == 'good') and (not allrets[0])) or ((moralflag == 'bad') and allrets[0]): return False else: return True elif (0 == len(set(allrets))): return True else: return False
opu_input=args.opu_input, indexes=args.indexes, binary_layer=args.binary_layer, opu=args.opu) if args.dataset == 'cifar10': _, test_dl = cifar10(batch_size=100) else: _, test_dl = cifar100(batch_size=100) if args.indexes: msg = 'Test score={}. With indexes accuracy has to be 100%' print( msg.format( compute_score(attack_transfer.model, test_dl, attack_transfer.device, indexes=torch.load(args.indexes)))) else: msg = 'Test score={}, to be compared to eps=0 attack' print( msg.format( compute_score(attack_transfer.model, test_dl, attack_transfer.device))) epsilons = [0, .01, .02, .03, .04, .05, .06, .07, .08, .09, .1] fgsm_acc, pgd_acc = list(), list() for eps in epsilons: fgsm_acc.append(attack_transfer.transfer_attack('fgsm', eps)) pgd_acc.append(attack_transfer.transfer_attack('pgd', eps)) print(f'{eps}, fgsm = {fgsm_acc[-1]}, pgd = {pgd_acc[-1]}')
else: solution[a1 // 2][a1 % 2], solution[a2 // 2][a2 % 2] = solution[a2 // 2][a2 % 2], solution[a1 // 2][a1 % 2] no_progress_round += 1 return solution if __name__ == "__main__": n, m, _, preferences = utils.get_preferences("data/sample_2.txt") assert n % 2 == 0 solution = solve_2_roommates_greedy(n, m, preferences) score = utils.compute_score(preferences, solution) print(f"Found a solution with a greedy algorithm of score {score}\n") utils.save_solution("output/solution_" + str(score) + ".txt", solution) solution = solve_2_roommates_conv(n, m, preferences) score = utils.compute_score(preferences, solution) print(f"Found a solution with a score of {score}") utils.save_solution("output/solution_" + str(score) + ".txt", solution)
def main(args): if not os.path.exists('result/'): os.mkdir('result/') if not os.path.exists('rules/'): os.mkdir('rules/') results = [] fold = 10 results_out = str(args) + '\n' true_rules_out = '' pred_rules_out = '' for i in range(fold): path = 'dataset/' + args.dataset + '/' + str(fold) + '_fold/' train_data, test_data, num_nodes, num_rels, node_features = train_test_triples( path, i, args.ftype, args.n_hidden) complete_data = np.concatenate((train_data, test_data)) print("Edges: ", len(complete_data)) if test_data.shape[0] == 0: continue # create model in_feat = node_features.shape[1] node_features = torch.from_numpy(np.array(node_features)).float() # check cuda use_cuda = args.gpu >= 0 and torch.cuda.is_available() if use_cuda: torch.cuda.set_device(args.gpu) if use_cuda: node_features = node_features.cuda() model_state_file = args.dataset + '_' + args.ftype + '_model_state.pth' model = BinaryPredict(in_feat, args.n_hidden, num_rels, num_bases=args.n_bases, num_hidden_layers=args.n_layers, dropout=args.dropout, use_cuda=use_cuda, reg_param=args.regularization, node_features=node_features, relation_features=None) if use_cuda: model.cuda() # optimizer optimizer = torch.optim.Adam(model.parameters(), lr=args.lr) # validation and testing triples train_idx = np.arange(len(train_data)) valid_idx = sorted(random.sample(list(train_idx), len(train_idx) // 5)) train_idx = sorted(list(set(train_idx) - set(valid_idx))) valid_data = train_data[valid_idx] train_data = train_data[train_idx] valid_data = torch.LongTensor(valid_data) test_data = torch.LongTensor(test_data) # build test graph test_graph, test_rel, test_norm, edge_norm = utils.build_test_graph( num_nodes, num_rels, complete_data) test_node_id = torch.arange(0, num_nodes, dtype=torch.long) test_rel = torch.from_numpy(test_rel) edge_norm = torch.from_numpy(edge_norm).unsqueeze(1) test_rel = test_rel.long() test_norm = torch.from_numpy(test_norm) if use_cuda: test_node_id = test_node_id.cuda() test_rel, test_norm = test_rel.cuda(), test_norm.cuda() edge_norm = edge_norm.cuda() test_graph.ndata.update({ 'id': test_node_id, 'norm': test_norm }) #'id': test_node_id, test_graph.edata.update({'type': test_rel, 'norm': edge_norm}) # build adj list and calculate degrees for sampling adj_list, degrees = utils.get_adj_and_degrees(num_nodes, train_data) # training loop print("start training...") epoch = 0 best_f1 = 0 while True: model.train() epoch += 1 g, node_id, edge_type, node_norm, data, labels, edge_norm = \ utils.generate_sampled_graph_and_labels( train_data, args.graph_batch_size, args.graph_split_size, num_rels, adj_list, degrees, args.negative_sample) node_id = torch.from_numpy(node_id).long() edge_type = torch.from_numpy(edge_type) edge_type = edge_type.long() node_norm = torch.from_numpy(node_norm) data, labels = torch.from_numpy(data), torch.from_numpy(labels) deg = g.in_degrees(range(g.number_of_nodes())).float() edge_norm = torch.from_numpy(edge_norm).unsqueeze(1) if use_cuda: node_id, deg = node_id.cuda(), deg.cuda() edge_type, node_norm, edge_norm = edge_type.cuda( ), node_norm.cuda(), edge_norm.cuda() data, labels = data.cuda(), labels.cuda() g.ndata.update({'id': node_id, 'norm': node_norm}) g.edata['type'] = edge_type g.edata['norm'] = edge_norm loss = model.get_loss(g, data, labels) loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), args.grad_norm) # clip gradients optimizer.step() optimizer.zero_grad() # validation if epoch % args.evaluate_every == 0: # perform validation on CPU because full graph is too large if use_cuda: model.cpu() model.eval() scores_val, labels_val = utils.compute_score( test_graph, model, valid_data) # predicted probability scores_val = scores_val.detach().numpy() best_thred, f1_val = utils._select_threshold( labels_val, scores_val) if f1_val < best_f1: if epoch >= args.n_epochs: break else: best_f1 = f1_val torch.save( { 'state_dict': model.state_dict(), 'epoch': epoch, 'best_threshold': best_thred }, model_state_file) if use_cuda: model.cuda() print("training done") print("\nstart testing:") # use best model checkpoint checkpoint = torch.load(model_state_file) if use_cuda: model.cpu() # test on CPU model.eval() model.load_state_dict(checkpoint['state_dict']) print("Using best epoch: {}".format(checkpoint['epoch'])) best_thred = checkpoint['best_threshold'] scores_test, labels_test = utils.compute_score(test_graph, model, test_data) scores_test = scores_test.detach().numpy() labels_test_pred = (scores_test > best_thred) * np.ones_like(scores_test) test_rules, tmp1 = utils.find_rules_bt(path, i, test_data, labels_test) pred_rules, tmp2 = utils.find_rules_bt(path, i, test_data, labels_test_pred) true_rules_out += 'fold ' + str(i) + '\n' true_rules_out += tmp1 + '\n' pred_rules_out += 'fold ' + str(i) + '\n' pred_rules_out += tmp2 + '\n' precision_test, recall_test, f1_test = utils.metrics( test_rules, pred_rules) results_out += "Test Precision: {:.4f} | Test Recall: {:.4f} | Test F1: {:.4f} \n".format( precision_test, recall_test, f1_test) print( "Test Precision: {:.4f} | Test Recall: {:.4f} | Test F1: {:.4f} \n" .format(precision_test, recall_test, f1_test)) results.append([precision_test, recall_test, f1_test]) mean_p, mean_r, mean_f1 = np.mean(np.array(results), axis=0) results_out += "Mean values over " + str( fold ) + " fold: Precision: {:.4f} | Recall: {:.4f} | F1: {:.4f}".format( mean_p, mean_r, mean_f1) print("Mean values over " + str(fold) + " fold: Precision: {:.4f} | Recall: {:.4f} | F1: {:.4f}".format( mean_p, mean_r, mean_f1)) file_name = args.ftype + '_' + args.dataset + '.txt' f = open('result/' + file_name, 'w', encoding='utf-8') f.write(results_out) f.close() f = open('rules/true_' + file_name, 'w', encoding='utf-8') f.write(true_rules_out) f.close() f = open('rules/pred_' + file_name, 'w', encoding='utf-8') f.write(pred_rules_out) f.close()
def phase1(model): """ Execute phase 1 of the visual HTM experiments. """ # TRAINING ############################################# # Load a single instance of 8 im = utils.load_digit('8_calibri') im_focus_win = utils.ImFocusWin(im, FOCUS_WIN_SIZE) im_focus_win.set_move_id('vert_cascade', 5) hist_err = [] # Train for 20 passes for i in range(20 * TRAINING_ITERS): # Run through HTM predCols = model.get_predCols() model.process(im_focus_win.grab_window()) activeCols = model.get_activeCols() score = utils.compute_score(activeCols, predCols) hist_err.append(score) # Move three places; saccade movement is not continuous im_focus_win.next() im_focus_win.next() im_focus_win.next() # Plot the error over time while training on this digit utils.plot_pred_err(hist_err, 'Calibri 8') # TESTING ############################################### # Prevent anymore synapse updates model.stop_learning() # Collects avg error from all digits from all fonts all_avgs = {} # Test with instances of other digits in various fonts for font in FONTS: # Store this font's per-digit avg prediction error avgs = OrderedDict() # Iterate through each digit in this font for j in range(10): # Reset the prediction state model.reset_tm() hist_err = [] # Load the digit instance im = utils.load_digit('%d_%s' % (j, font)) im_focus_win = utils.ImFocusWin(im, FOCUS_WIN_SIZE) im_focus_win.set_move_id('vert_cascade', 5) # Test for 4 passes to get average response for i in range(20 * 4): # Run through HTM predCols = model.get_predCols() model.process(im_focus_win.grab_window()) activeCols = model.get_activeCols() score = utils.compute_score(activeCols, predCols) # First prediction error is always going to be maximal if(i != 0): hist_err.append(score) # Move three places; saccade movement is not continuous im_focus_win.next() im_focus_win.next() im_focus_win.next() avgs[str(j)] = np.mean(hist_err) # Collect to overall container all_avgs[font] = avgs # Plot the average error per digit per font utils.plot_avgs(all_avgs, 'Calibri 8')
import datetime _d_e = ('d', 'e') _a_to_c = ('a', 'b', 'c') _all = _a_to_c + _d_e inputs = _all sum_score = 0 for input_ in inputs: start_t = time.time() # read input R, C, F, N, B, T, rides = read_data(input_) print("INPUT", input_) print("bonus", B) # find schedule schedule = ridecentric_solution(R, C, F, N, B, T, rides) # print score score = compute_score(schedule=schedule, bonus=B, max_t=T) # print('schedule', schedule) print('score', input_, ':', score) sum_score += score # create output write_submission(schedule.to_car_ride_nbrs(), out_name=f'{input_}_{score:,}') print(f'duration: {datetime.timedelta(seconds=time.time() - start_t)}') print('total score', "{:,}".format(sum_score))
identity_points = identity_points.cuda() gt_points = gt_points.cuda() pointsReconstructed = model(pose_points, identity_points) rec_loss = torch.mean((pointsReconstructed - gt_points)**2) edg_loss = 0 for i in range(len(random_sample)): f = new_face[i].cpu().numpy() v = identity_points[i].transpose(0, 1).cpu().numpy() edg_loss = edg_loss + utils.compute_score( pointsReconstructed[i].unsqueeze(0), f, utils.get_target(v, f, 1)) edg_loss = edg_loss / len(random_sample) l2_loss = rec_loss rec_loss = rec_loss + 0.0005 * edg_loss rec_loss.backward() optimizer_G.step() total_loss = total_loss + rec_loss print('####################################') print(epoch) print(time.time() - start) mean_loss = total_loss / (j + 1) print('mean_loss', mean_loss.item())
def main(args): fold = 10 topk = 100 if not os.path.exists('result/'): os.mkdir('result/') if not os.path.exists('rules/'): os.mkdir('rules/') path = 'dataset/' + args.dataset + '/' + str(fold) + '_fold' + '/' complete_data, num_nodes, num_rels, node_features = load_whole_data(path, args.ftype, args.n_hidden) train_data = complete_data test_data = complete_data in_feat = node_features.shape[1] node_features = torch.from_numpy(np.array(node_features)).float() # check cuda use_cuda = args.gpu >= 0 and torch.cuda.is_available() if use_cuda: torch.cuda.set_device(args.gpu) if use_cuda: node_features = node_features.cuda() model_state_file = args.dataset + '_' + args.ftype + '_all_data_model_state.pth' model = BinaryPredict(in_feat, args.n_hidden, num_rels, num_bases=args.n_bases, num_hidden_layers=args.n_layers, dropout=args.dropout, use_cuda=use_cuda, reg_param=args.regularization, node_features=node_features) if use_cuda: model.cuda() # optimizer optimizer = torch.optim.Adam(model.parameters(), lr=args.lr) train_idx = np.arange(len(train_data)) valid_idx = sorted(random.sample(list(train_idx), len(train_idx) // 5)) train_idx = sorted(list(set(train_idx) - set(valid_idx))) valid_data = train_data[valid_idx] train_data = train_data[train_idx] valid_data = torch.LongTensor(valid_data) test_data = torch.LongTensor(test_data) # build test graph test_graph, test_rel, test_norm, edge_norm = utils.build_test_graph( num_nodes, num_rels, complete_data) test_node_id = torch.arange(0, num_nodes, dtype=torch.long) test_rel = torch.from_numpy(test_rel) edge_norm = torch.from_numpy(edge_norm).unsqueeze(1) test_rel = test_rel.long() test_norm = torch.from_numpy(test_norm) if use_cuda: test_node_id = test_node_id.cuda() test_rel, test_norm = test_rel.cuda(), test_norm.cuda() edge_norm = edge_norm.cuda() test_graph.ndata.update({'id': test_node_id, 'norm': test_norm}) # 'id': test_node_id, test_graph.edata.update({'type': test_rel, 'norm': edge_norm}) # build adj list and calculate degrees for sampling adj_list, degrees = utils.get_adj_and_degrees(num_nodes, train_data) # training loop print("start training...") epoch = 0 best_f1 = 0 while True: model.train() epoch += 1 g, node_id, edge_type, node_norm, data, labels, edge_norm = \ utils.generate_sampled_graph_and_labels( train_data, args.graph_batch_size, args.graph_split_size, num_rels, adj_list, degrees, args.negative_sample) node_id = torch.from_numpy(node_id).long() edge_type = torch.from_numpy(edge_type) edge_type = edge_type.long() node_norm = torch.from_numpy(node_norm) data, labels = torch.from_numpy(data), torch.from_numpy(labels) deg = g.in_degrees(range(g.number_of_nodes())).float() edge_norm = torch.from_numpy(edge_norm).unsqueeze(1) if use_cuda: node_id, deg = node_id.cuda(), deg.cuda() edge_type, node_norm, edge_norm = edge_type.cuda(), node_norm.cuda(), edge_norm.cuda() data, labels = data.cuda(), labels.cuda() g.ndata.update({'id': node_id, 'norm': node_norm}) g.edata['type'] = edge_type g.edata['norm'] = edge_norm loss = model.get_loss(g, data, labels) loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), args.grad_norm) # clip gradients optimizer.step() optimizer.zero_grad() # validation if epoch % args.evaluate_every == 0: # perform validation on CPU because full graph is too large if use_cuda: model.cpu() model.eval() scores_val, labels_val = utils.compute_score(test_graph, model, valid_data) # predicted probability scores_val = scores_val.detach().numpy() best_thred, f1_val = utils._select_threshold(labels_val, scores_val) if f1_val < best_f1: if epoch >= args.n_epochs: break else: best_f1 = f1_val torch.save({'state_dict': model.state_dict(), 'epoch': epoch, 'best_threshold': best_thred}, model_state_file) if use_cuda: model.cuda() print("training done") print("\nstart testing:") # use best model checkpoint checkpoint = torch.load(model_state_file) if use_cuda: model.cpu() # test on CPU model.eval() model.load_state_dict(checkpoint['state_dict']) print("Using best epoch: {}".format(checkpoint['epoch'])) scores_test, labels_test = utils.compute_score(test_graph, model, test_data) scores_test = np.array(scores_test) labels_test = np.array(labels_test) pred_rules = utils.find_rules_bt_all(path, test_data, labels_test, scores_test, topk) file_name = args.ftype + '_' + args.dataset + '_all_data.txt' f = open('rules/pred_' + file_name, 'w', encoding='utf-8') f.write(pred_rules) f.close()
def run_files(image_path, image_size, template_path, template_size=100, outfile='results.png', rect=((0, 0), (0, 0)), title='QATM Piece Match', verbose=True): if verbose: print("Processing: %s %s " % (image_path, template_path)) original = cv2.imread(template_path) m = max(original.shape[0], original.shape[1]) scale = template_size / m resized = cv2.resize( original, (int(original.shape[0] * scale), int(original.shape[1] * scale)), interpolation=cv2.INTER_NEAREST) template = resized[..., ::-1] w, h = (template.shape[0], template.shape[1]) image = cv2.imread(image_path)[..., ::-1] m = max(image.shape[0], image.shape[1]) scale = image_size / m image = cv2.resize( image, (int(image.shape[1] * scale), int(image.shape[0] * scale)), interpolation=cv2.INTER_NEAREST) # process images template_ = np.expand_dims(preprocess_input(template), axis=0) image_ = np.expand_dims(preprocess_input(image), axis=0) if w * h <= 4000: val = model.predict([template_, image_]) else: # used when image is too big val = model_bkup.predict([template_, image_]) # compute geometry average on score map val = np.log(val) gray = val[0, :, :, 0] gray = cv2.resize(gray, (image.shape[1], image.shape[0])) score = compute_score(gray, w, h) score[score > -1e-7] = score.min() score = np.exp( score / (h * w)) # reverse number range back after computing geometry average # plot result x, y, ww, hh = locate_bbox(score, w, h) image_plot = cv2.rectangle(image.copy(), (int(x), int(y)), (int(x + ww), int(y + hh)), (255, 0, 0), 3) p1 = ((int( (rect[0][0] + rect[1][0]) * 0.5)), int( (rect[0][1] + rect[1][1]) * 0.5)) p2 = (int(x + ww * 0.5), int(y + hh * 0.5)) image_plot = cv2.line(image_plot, p1, p2, (255, 255, 255), 2) image_plot = cv2.rectangle(image_plot, rect[0], rect[1], (0, 0, 255), 3) image_gt = cv2.rectangle(image.copy(), rect[0], rect[1], (0, 0, 255), 3) fig, ax = plt.subplots(1, 4, figsize=(20, 5)) ax[0].imshow(image_gt) ax[0].set_title('Ground Truth Blue') ax[1].imshow(template) ax[1].set_title('Piece') ax[2].imshow(image_plot) ax[2].set_title('Ground Truth Blue - Best Guess Red') ax[3].imshow(score, 'jet') ax[3].set_title('Per Pixel Score') fig.suptitle(title, fontsize=20) plt.savefig(outfile) fig.clf()