def upload_file2(): if request.method == 'POST': f = request.files['file'] #自分のディレクトリにa.jpgとして保存する f.save(secure_filename("a.jpg")) #関数による判定を戻り値 return eval.evaluation("a.jpg")
def post(): if request.method == 'POST': if not request.files['file'].filename == u'': # アップロードされたファイルを保存 f = request.files['file'] img_path = FULL_PATH + UPLOAD_FOLDER + f.filename f.save(img_path) # eval.pyへアップロードされた画像を渡す result = eval.evaluation(img_path, '/home/feleskatze/www/flask/model.ckpt') if result == None: return redirect(url_for('index')) else: KANA = False rect = [[], [], [], []] for human in result: rect[0].append(human['x']) rect[1].append(human['y']) rect[2].append(human['width']) rect[3].append(human['height']) if human['rank'][0]['rate'] >= 90: KANA = True return render_template('index.html', img_path='./tmp/' + f.filename, rect=rect, result=result, KANA=KANA) else: # エラーなどでリダイレクトしたい場合 return redirect(url_for('index'))
def uploads_file(): language = request.form["language"] if language == None: language = "en" if request.files['file']: file = request.files['file'] ip = request.remote_addr user_agent = request.environ['HTTP_USER_AGENT'] date = datetime.datetime.today() with database.get_connection() as conn: with conn.cursor(cursor_factory=DictCursor) as cur: # 一時保存用のIDを得る cur.execute( "INSERT INTO images (ip,user_agent,created_at) VALUES(%s,%s,%s) RETURNING id;", (ip, json.dumps(user_agent), date)) record = cur.fetchone() id = record["id"] # 画像を一時保存して解析結果を得る(一時保存しなくても良い方法があるなら、上記のIDを得る処理は下記のDB保存処理と統合したい) img_path = os.path.join(app.config['UPLOAD_FOLDER'], str(id)) file.save(img_path) result = eval.evaluation(img_path) os.remove(img_path) # 画像をストレージに保存 file.seek(0) data = base64.b64encode(file.read()) headers = {'content-type': 'application/base64'} res = requests.post(POST_URL, headers=headers, data=data) res_result = json.loads(res.text) file_id = res_result["file_id"] # DBに保存 cur.execute( "UPDATE images SET file_id=%s, result=%s WHERE id=%s;", (file_id, json.dumps(result), id)) conn.commit() return redirect("/k?l=" + language + "&i=" + file_id) textdef = td.get_textdef(language) html = render_template('index.html', language=language, textdef=textdef, textdef_text=json.dumps(textdef)) return html
def post(): if request.method == 'POST': if not request.files['file'].filename == u'': # アップロードされたファイルを保存 f = request.files['file'] img_path = os.path.join(UPLOAD_FOLDER, secure_filename(f.filename)) f.save(img_path) # eval.pyへアップロードされた画像を渡す result = eval.evaluation(img_path, './model2.ckpt') else: result = [] return render_template('index.html', result=result) else: # エラーなどでリダイレクトしたい場合 return redirect(url_for('index'))
def calculate_action_values(self, state, player, legal): result=[] score = [] actions_states = [(p, self.board.next_state(state, p)) for p in legal] if len(actions_states)<3: result = actions_states score = [0] else: result ,score= eval.evaluation(actions_states) for t in xrange(len(score)): print result[t] print score[t] #actions_states = ((p, self.board.next_state(state, p)) for p in legal) return sorted( ({'action': p, 'percent': 100 * self.stats[(player, S)].value / self.stats[(player, S)].visits, 'wins': self.stats[(player, S)].value, 'plays': self.stats[(player, S)].visits} for p, S in result), key=lambda x: (x['percent'], x['plays']), reverse=True )
def train(args, data, gpu): n_item = data[0] items = data[1] n_user = data[2] adj_item, adj_adam = data[3], data[4] train_data, test_data = data[5], data[6] user2item = data[7] # item_embedding_matrix = data[8] print(args) # detect devices device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # tensorboard initialization tb_dir = '../runs' if not os.path.exists(tb_dir): os.makedirs(tb_dir) writer = SummaryWriter(tb_dir) weights_dir = '../weights' if not os.path.exists(weights_dir): os.makedirs(weights_dir) # log log_dir = os.path.join('../log', args.dataset) if not os.path.exists(log_dir): os.makedirs(log_dir) log_file = os.path.join(log_dir, 'TSCN.log') if logging.root.handlers: logging.root.handlers = [] logging.basicConfig(format='%(asctime)s : %(levelname)s: %(message)s', level=logging.INFO, filename=log_file) model = TSCN(args, n_item, n_user, adj_item, adj_adam) if gpu: model.to(device) else: model.to('cpu') # visualization of computation graph # input_to = torch.zeros((args.batch_size, args.dim + 1), dtype=torch.float32) # if torch.cuda.is_available(): # input_to = input_to.cuda() # writer.add_graph(model, input_to_model=input_to) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.l2_weight) loss_fn = nn.BCELoss() # 不带softmax激活函数的CrossEntropy函数 # train print('training ...') idx = 0 # for epoch in range(args.n_epochs): for epoch in range(args.n_epochs): running_loss = 0 start_time = time() for i in range(len(train_data)): optimizer.zero_grad() data = train_data[i] data = np.array(data) np.random.shuffle(data) user_list = data[:, 0] item_list = data[:, 1] labels = torch.from_numpy(data[:, 2]) # prepare user input user_input, n_idxs = prepare_batch_input(user_list, item_list, user2item, n_item) if torch.cuda.is_available() and gpu: user_input = torch.from_numpy(np.array(user_input)).cuda() item_input = torch.from_numpy(np.array(item_list)).cuda() n_idx_input = torch.from_numpy(np.array(n_idxs)).cuda() outputs = model(user_input, item_input, n_idx_input) loss = loss_fn(outputs, labels.cuda().float()) else: user_input = torch.from_numpy(np.array(user_input)) item_input = torch.from_numpy(np.array(item_list)) n_idx_input = torch.from_numpy(np.array(n_idxs)) outputs = model(user_input, item_input, n_idx_input) loss = loss_fn(outputs, labels.float()) loss.backward() optimizer.step() running_loss += loss.item() print('epoch {}: {}/{} loss = {:.4f}'.format( epoch + 1, i, n_user, loss.item())) end_time = time() # evaluation hr, ndcg = evaluation(model, test_data, user2item, args, n_item) eval_time = time() writer.add_scalar('epoch loss', running_loss / n_user, global_step=epoch + 1) writer.add_scalar('hit ratio@10', hr, global_step=epoch + 1) writer.add_scalar('NDCG @ 10', ndcg, global_step=epoch + 1) print( 'epocn {} loss:{:.4f}, train_time = [%.1f s], eval_time = [%.1f s]' .format(epoch + 1, running_loss / n_user, end_time - start_time, eval_time - end_time)) logging.info( 'epocn {} loss:{:.4f}, train_time = [%.1f s], eval_time = [%.1f s]' .format(epoch + 1, running_loss / n_user, end_time - start_time, eval_time - end_time)) torch.save(model, '../weights/TSCN_' + args.dataset + '.pth')
if opts.debug: console.setLevel(logging.DEBUG) else: console.setLevel(logging.INFO) console.setFormatter( logging.Formatter('%(asctime)s**%(levelname)s\t%(message)s', datefmt='%Y.%m.%d-%H:%M:%S')) logging.getLogger('').addHandler(console) # opts para = vars(opts) logging.debug("Options input: \n" + json.dumps(para, indent=2)) # cuda device device = torch.device("cuda:" + str(opts.cuda_device)) logging.info("Device Using: %s " % device.__str__()) # check mode if opts.mode == 'train': logging.info("Current core mode: Training") train.train(opts, device) elif opts.mode == 'eval': logging.info("Current core mode: Evaluating") eval.evaluation(opts, device) elif opts.mode == 'precaps': logging.info("Current core mode: Preprocessing captions") preprocess.preprocess_captions(opts) elif opts.mode == 'prefeats': logging.info("Current core mode: Preprocessing features") preprocess.preprocess_features(opts, device)
def run_simulation(self): # Plays out a "random" game from the current position, # then updates the statistics tables with the result. # A bit of an optimization here, so we have a local # variable lookup instead of an attribute access each loop. stats = self.stats #win_flag = 1 means no end but predict to win win_flag = 0 win_palyer = 0 visited_states = set() history_copy = self.history[:] state = history_copy[-1] player = self.board.current_player(state) expand = True for t in xrange(1, self.max_actions + 1): legal = self.board.legal_actions(history_copy) actions_states = [(p, self.board.next_state(state, p)) for p in legal] if all((player, S) in stats for p, S in actions_states): # If we have stats on all of the legal actions here, use UCB1. log_total = log( sum(stats[(player, S)].visits for p, S in actions_states) or 1) value, action, state = max( ((stats[(player, S)].value / (stats[(player, S)].visits or 1)) + self.C * sqrt(log_total / (stats[(player, S)].visits or 1)), p, S) for p, S in actions_states) else: # Otherwise, just make an arbitrary decision. #action, state = choice(actions_states) if (len(actions_states) < 3): action, state = choice(actions_states) else: result = [] score = [] result, score = eval.evaluation(actions_states) result = result[:3] score = score[:3] # result = self.eval.evaluation(actions_states) action, state = choice(result) # for test # print action # print state history_copy.append(state) # `player` here and below refers to the player # who moved into that particular state. if expand and (player, state) not in stats: expand = False stats[(player, state)] = Stat() if t > self.max_depth: self.max_depth = t visited_states.add((player, state)) player = self.board.current_player(state) if self.board.is_ended(history_copy): break """if t > 20: if score[0] > 500: win_player = player win_flag= 1 break elif score[0] < -300 : win_player = 3 - player win_flag= 1 break else: continue""" # Back-propagation end_values = self.end_values(history_copy) for player, state in visited_states: if (player, state) not in stats: continue S = stats[(player, state)] S.visits += 1 S.value += end_values[player]
from net import test from eval import evaluation if __name__ == '__main__': # get predicted classes and bboxes from test data in data folder # if train = true, a new model will be trained # if train = false, the saved model from ckpt will be used on the test set pred_class, pred_bboxes = test(train=False) # evaluate the model evaluation(pred_class, pred_bboxes)
import eval if __name__ == '__main__': eval.evaluation()
def iteration(): top = [('top 1', 1), ('top 3', 3), ('top 5', 5), ('top 10', 10)] files = ['valid_golden-all', 'resFINAL'] treshold = [1, .8, .6, .4] context_weight = [ { 'Sentence': 1, 'Window': 0, 'Segment': 0, 'Document': 0 }, # que sentence { 'Sentence': 0, 'Window': 1, 'Segment': 0, 'Document': 0 }, # que window { 'Sentence': 0, 'Window': 0, 'Segment': 1, 'Document': 0 }, # que segment { 'Sentence': 0, 'Window': 0, 'Segment': 0, 'Document': 1 }, # que document { 'Sentence': 1, 'Window': 1, 'Segment': 0, 'Document': 0 }, # close { 'Sentence': 1, 'Window': 1, 'Segment': 1, 'Document': 0 }, # section { 'Sentence': 1, 'Window': 1, 'Segment': 1, 'Document': 1 } ] # generic term_weight = [ { 'Original_value': 1, 'Attached_value': 0, 'Argument': 0 }, # pure { 'Original_value': 0, 'Attached_value': 1, 'Argument': 0 }, # attachment { 'Original_value': 0, 'Attached_value': 0, 'Argument': 1 }, # concept { 'Original_value': 1, 'Attached_value': 1, 'Argument': 0 }, # lexical { 'Original_value': 1, 'Attached_value': 1, 'Argument': 1 } ] # generic models = [ 'en_core_sci_lg', # large base scispacy 'en_core_sci_scibert', # BioBert in scispacy format 'en_ner_craft_md', # scispacy on CRAFT corpus 'en_ner_jnlpba_md', # scispacy on JNLPBA corpus 'en_ner_bc5cdr_md', # scispacy on BC5CDR corpus 'en_ner_bionlp13cg_md', # scispacy on BIONLP13CG corpus 'en_core_web_lg', # large base spacy 'en_core_web_trf' # Bert in spacy format ] # equation = ['harmonic', 'max'] for t in top: for file in files: args = pd.read_csv('input_files/' + file + '.csv', encoding='utf-8') for tresh in treshold: path = t[0] + re.split('\.', file)[0] + str(tresh) if file == 'valid_golden-all' and tresh != 1: continue if path not in os.listdir('results'): os.mkdir('results/' + path) os.mkdir('results/' + path + '/structural') os.mkdir('results/' + path + '/frequency') os.mkdir('results/' + path + '/semantic') print('#' * 15) print('# PREPARATION') with open('workfiles/candidats.csv', 'w', encoding='utf-8') as f: instances = filtre( args, pd.read_csv('datatables/resTables.csv', encoding='utf-8').Document.values, tresh) instances.to_csv(f, encoding='utf-8') if str(tresh) + 'clones.csv' not in os.listdir('workfiles'): detectClone(instances, tresh) print('# OK!') print('#' * 15) print('# STRUCTURAL SIMPLE') print('file: ' + file) print('treshold: ' + str(tresh)) print('selection: ' + t[0]) if 'structuralSimple.csv' not in os.listdir('results/' + path + '/structural'): structuralSimple = StructuralLinking.getStructuralScore( instances, False, t[1]) structuralSimple.to_csv('results/' + path + '/structural/structuralSimple.csv', encoding='utf-8') structuralSimple = pd.read_csv( 'results/' + path + '/structural/structuralSimple.csv', encoding='utf-8') eval.evaluation( structuralSimple, 'results/' + path + '/structural/EVAL_structuralSimple') del structuralSimple print('#' * 15) print('# STRUCTURAL GUIDED') print('file: ' + file) print('treshold: ' + str(tresh)) print('selection: ' + t[0]) if 'structuralGuided.csv' not in os.listdir('results/' + path + '/structural'): structuralGuided = StructuralLinking.getStructuralScore( instances, True, t[1]) structuralGuided.to_csv('results/' + path + '/structural/structuralGuided.csv', encoding='utf-8') structuralGuided = pd.read_csv( 'results/' + path + '/structural/structuralGuided.csv', encoding='utf-8') eval.evaluation( structuralGuided, 'results/' + path + '/structural/EVAL_structuralGuided') del structuralGuided for con_wei in context_weight: for ter_wei in term_weight: name = ''.join([x + str(con_wei[x]) for x in con_wei]) name += ''.join([x + str(ter_wei[x]) for x in ter_wei]) if name not in os.listdir('results/' + path + '/frequency'): os.mkdir('results/' + path + '/frequency/' + name) print('#' * 15) print('# FREQUENCY') print('file: ' + file) print('treshold: ' + str(tresh)) print('context: ' + str(con_wei)) print('term: ' + str(ter_wei)) print('selection: ' + t[0]) if 'frequencyPMI.csv' not in os.listdir('results/' + path + '/frequency/' + name): frequencyPMI, frequencyDICE, frequencyJACCARD = FrequencyLinking.getFrequencyScore( con_wei, ter_wei, tresh, t[1]) frequencyPMI.to_csv('results/' + path + '/frequency/' + name + '/frequencyPMI.csv', encoding='utf-8') frequencyDICE.to_csv('results/' + path + '/frequency/' + name + '/frequencyDICE.csv', encoding='utf-8') frequencyJACCARD.to_csv('results/' + path + '/frequency/' + name + '/frequencyJACCARD.csv', encoding='utf-8') frequencyPMI = pd.read_csv('results/' + path + '/frequency/' + name + '/frequencyPMI.csv', encoding='utf-8') eval.evaluation( frequencyPMI, 'results/' + path + '/frequency/' + name + '/EVAL_frequencyPMI') frequencyDICE = pd.read_csv('results/' + path + '/frequency/' + name + '/frequencyDICE.csv', encoding='utf-8') eval.evaluation( frequencyDICE, 'results/' + path + '/frequency/' + name + '/EVAL_frequencyDICE') frequencyJACCARD = pd.read_csv('results/' + path + '/frequency/' + name + '/frequencyJACCARD.csv', encoding='utf-8') eval.evaluation( frequencyJACCARD, 'results/' + path + '/frequency/' + name + '/EVAL_frequencyJACCARD') del frequencyPMI, frequencyDICE, frequencyJACCARD for model in models: print('#' * 15) print('# SEMANTIC') print('file: ' + file) print('treshold: ' + str(tresh)) print('model: ' + model) print('selection: ' + t[0]) if 'harmonic' + model + '.csv' not in os.listdir( 'results/' + path + '/semantic'): semanticH, semanticA, semanticM = SemanticLinking.getSemanticScore( model, tresh, t[1]) semanticH.to_csv('results/' + path + '/semantic/harmonic' + model + '.csv', encoding='utf-8') semanticA.to_csv('results/' + path + '/semantic/arithmetic' + model + '.csv', encoding='utf-8') semanticM.to_csv('results/' + path + '/semantic/max' + model + '.csv', encoding='utf-8') semanticH = pd.read_csv('results/' + path + '/semantic/harmonic' + model + '.csv', encoding='utf-8') semanticA = pd.read_csv('results/' + path + '/semantic/arithmetic' + model + '.csv', encoding='utf-8') semanticM = pd.read_csv('results/' + path + '/semantic/max' + model + '.csv', encoding='utf-8') eval.evaluation( semanticH, 'results/' + path + '/semantic/EVAL_harmonic' + model) eval.evaluation( semanticA, 'results/' + path + '/semantic/EVAL_arithmetic' + model) eval.evaluation( semanticM, 'results/' + path + '/semantic/EVAL_max' + model) del semanticH, semanticA, semanticM gc.collect()
'loss': loss_temp, 'loss_rpn_cls': loss_rpn_cls, 'loss_rpn_box': loss_rpn_box, 'loss_rcnn_cls': loss_rcnn_cls, 'loss_rcnn_box': loss_rcnn_box } logger.add_scalars("logs_s_{}/losses".format(args.session), info, (epoch - 1) * iters_per_epoch + step) loss_temp = 0 start = time.time() if epoch % args.evaluation_interval == 0: print("\n---- Evaluating Model ----") map = evaluation(name=args.imdbval_name, net=fasterRCNN) if map > best_map: # save_name = os.path.join(output_dir, 'faster_rcnn_{}_{}_{}.pth'.format(args.session, epoch, step)) save_name = os.path.join( output_dir, 'faster_rcnn_' + cfg['POOLING_MODE'] + '_best.pth') save_checkpoint( { 'session': args.session, 'epoch': epoch + 1, 'model': fasterRCNN.module.state_dict() if args.mGPUs else fasterRCNN.state_dict(),
temp_id = dev_batch.id1.cpu().data.numpy() temp_pattern = index2word[ dev_batch.pattern1.cpu().data.numpy()] temp_predicate = index2word[ dev_batch.predicate1.cpu().data.numpy()] temp_mention = index2char[ dev_batch.mention1.cpu().data.numpy()] temp_candidate = index2char[ dev_batch.candidate1.cpu().data.numpy()] temp_score = score.cpu().data.numpy() for i in range(dev_batch.batch_size): data_to_eval.append((int(temp_id[i]), list(temp_pattern[i]), list(temp_predicate[i]), list(temp_mention[i]), \ list(temp_candidate[i]), float(temp_score[i]))) #print("size of eval data", len(data_to_eval)) accuracy = evaluation(data_to_eval, "data/attentivecnn.valid.gold") print( dev_log_template.format( time.time() - start, epoch, iterations, 1 + batch_idx, len(train_iter), 100. * (1 + batch_idx) / len(train_iter), loss.data[0], "N/A ", best_dev_acc * 100.0, accuracy * 100.0)) if accuracy > best_dev_acc: iters_not_improved = 0 best_dev_acc = accuracy snapshot_path = os.path.join( args.save_path, args.dataset, args.specify_prefix + '_best_model.pt') torch.save(model, snapshot_path) else: iters_not_improved += 1
def main(): init() # Solve data input inpdb = input('Data input: ') data = fplay(inpdb) arr = [] for i in range(len(data)): arr.append(pktool(data[i], 0)) inp = input('Time analysis: ') engine = input('Engine: ') connect.connect('Engine\\' + engine + '.exe') print('Option 1: AlphaGomoku\nOption 2: Other') option = input('Option: ') timeinit(inp) time.sleep(0.3) print('Sleep 5 sec...') print('Total moves:', len(arr) - 2) b_ev = [] w_ev = [] b_num = [] w_num = [] print(arr) for i in range(len(arr)): if i == len(arr) - 1: if i % 2 == 0: b_ev.append(100-w_ev[-1]) w_ev.append(w_ev[-1]) b_num.append(2*i) else: w_ev.append(100-b_ev[-1]) b_ev.append(b_ev[-1]) b_num.append(2*i - 1) break if 2 < i < len(arr): print(Fore.LIGHTRED_EX + '[{}]:'.format(i), Style.RESET_ALL, arr[i]) put('BOARD') for j in range(i + 1): if i % 2 == j % 2: put(arr[j] + ',2') #time.sleep(0.1) else: put(arr[j] + ',1') put('DONE') output = connect.ms() if option == '2': print('Depth: {} - Evaluation: {} - Default output: {}'.format(output[0], round(100 - float(evaluation(output[1])), 2), output[1])) if i % 2 == 0: b_ev.append(round(100 - float(evaluation(output[1])), 2)) b_num.append(i) else: w_ev.append(round(float(evaluation(output[1])), 2)) w_num.append(i) else: print('Depth: {} - Evaluation: {}'.format(output[0], round(100 - float(output[1]), 2))) if i % 2 == 0: b_ev.append(100 - float(output[1])) w_ev.append(float(output[1])) b_num.append(2*i) else: w_ev.append(100 - float(output[1])) b_ev.append(float(output[1])) b_num.append(2*i - 1) put('RESTART') connect.kill() print('[+] Eval of black:', b_ev) print('[+] Eval of white:', w_ev) plt.title('Evaluate Graph ' + inp + ' sec') plt.plot(b_num, b_ev, 'b.-', label='Black evaluate') #plt.plot(w_num, w_ev, 'r.-', label='White evaluate') plt.legend(loc='best') plt.show()
def train(self): infos = {} if self.opt.start_from is not None and not self.opt.load_pretrained: # open old infos and check if models are compatible with open(os.path.join(self.opt.expr_dir, 'infos' + '.pkl')) as f: infos = pickle.load(f) total_iteration = infos.get('total_iter', 0) loaded_iteration = infos.get('iter', 0) loaded_epoch = infos.get('epoch', 1) val_result_history = infos.get('val_result_history', {}) loss_history = infos.get('loss_history', {}) lr_history = infos.get('lr_history', {}) # loading a best validation score if self.opt.load_best_score == True: best_val_score = infos.get('best_val_score', None) def clip_gradient(optimizer, grad_clip): for group in optimizer.param_groups: for param in group['params']: param.grad.data.clamp_(-grad_clip, grad_clip) def set_lr(optimizer, lr): for group in optimizer.param_groups: group['lr'] = lr for epoch in range(1, 1 + self.opt.max_epochs): if epoch < loaded_epoch: continue if epoch > self.opt.learning_rate_decay_start and self.opt.learning_rate_decay_start >= 1: fraction = (epoch - self.opt.learning_rate_decay_start) // self.opt.learning_rate_decay_every decay_factor = self.opt.learning_rate_decay_rate ** fraction self.opt.current_lr = self.opt.learning_rate * decay_factor set_lr(self.optimizer, self.opt.current_lr) else: self.opt.current_lr = self.opt.learning_rate # # Assign the scheduled sampling prob # if epoch > self.opt.scheduled_sampling_start and self.opt.scheduled_sampling_start >= 0: # fraction = (epoch - self.opt.scheduled_sampling_start) // self.opt.scheduled_sampling_increase_every # self.opt.ss_prob = min(self.opt.scheduled_sampling_increase_prob * fraction, self.opt.scheduled_sampling_max_prob) # self.decoder.ss_prob = self.opt.ss_prob for iter, (images, captions, lengths, imgids) in enumerate(self.trainloader): iter += 1 total_iteration += 1 if iter <= loaded_iteration: continue torch.cuda.synchronize() start = time.time() # Set mini-batch dataset images = Variable(images) captions = Variable(captions) if self.num_gpu > 0: images = images.cuda() captions = captions.cuda() lengths = [l-1 for l in lengths] targets = pack_padded_sequence(captions[:,1:], lengths, batch_first=True)[0] # Forward, Backward and Optimize self.model.zero_grad() outputs = self.model(images, captions[:,:-1], lengths) loss = self.criterion(outputs, targets) loss.backward() clip_gradient(self.optimizer, self.opt.grad_clip) self.optimizer.step() torch.cuda.synchronize() end = time.time() if iter % self.opt.log_step == 0: print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f, Perplexity: %5.4f' % (epoch, self.opt.max_epochs, iter, self.total_train_iter, loss.data[0], np.exp(loss.data[0]))) # make evaluation on validation set, and save model if (total_iteration % self.opt.save_checkpoint_every == 0): val_loss, predictions, lang_stats = evaluation(self.model, self.criterion, self.validloader, self.vocab, self.opt) val_result_history[total_iteration] = {'loss': val_loss, 'lang_stats': lang_stats, 'predictions': predictions} # Write the training loss summary # loss_history[total_iteration] = loss.data[0].cpu().numpy()[0] loss_history[total_iteration] = loss.data[0] lr_history[total_iteration] = self.opt.current_lr # Save model if is improving on validation result if self.opt.language_eval == 1: current_score = lang_stats['CIDEr'] else: current_score = - val_loss best_flag = False if best_val_score is None or current_score > best_val_score: best_val_score = current_score best_flag = True # Dump miscalleous informations infos['total_iter'] = total_iteration infos['iter'] = iter infos['epoch'] = epoch infos['best_val_score'] = best_val_score infos['opt'] = self.opt infos['val_result_history'] = val_result_history infos['loss_history'] = loss_history infos['lr_history'] = lr_history with open(os.path.join(self.opt.expr_dir, 'infos' + '.pkl'), 'wb') as f: pickle.dump(infos, f) if best_flag: checkpoint_path = os.path.join(self.opt.expr_dir, 'model-best.pth') torch.save(self.model.state_dict(), checkpoint_path) print("model saved to {}".format(self.opt.expr_dir)) with open(os.path.join(self.opt.expr_dir, 'infos' + '-best.pkl'), 'wb') as f: pickle.dump(infos, f)
def train(): # determine the device to run the model device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # device = 'cpu' use_gpu = True # tensorboard initialization writer = SummaryWriter('../runs') # load data train_data = trainset(SAMPLE_LENGTH) train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=False, num_workers=0) modelR = Net(NUM_RESIDENT, BATCH_SIZE) modelR.to(device) # define the optimizer and loss function optimizer = optim.Adam(modelR.parameters(), lr=LEARNING_RATE, weight_decay=L2_WEIGHT) loss_fn = nn.CrossEntropyLoss() # training stage for resident idx = 0 for epoch in range(N_EPOCHS): for i, data in enumerate(train_loader): idx += 1 optimizer.zero_grad() # reset the optimizer inputs, labels = data if epoch == 0 and i == 0: if torch.cuda.is_available() and use_gpu: writer.add_graph(modelR, input_to_model=inputs.cuda(), verbose=False) else: writer.add_graph(modelR, input_to_model=inputs, verbose=False) # jump off the last batch (batch size is not scalable in training) if inputs.shape[0] < BATCH_SIZE: continue # estimate whether GPU exists if torch.cuda.is_available() and use_gpu: outputs = modelR(inputs.cuda()) loss = loss_fn(outputs, labels[:, 0].cuda()) else: outputs = modelR(inputs) loss = loss_fn(outputs, labels[:, 0]) loss.backward() # back propagation optimizer.step() # weights update # loss visualization writer.add_scalar('resident loss', loss.item(), global_step=idx) print('epoch {} batch {} resident loss: {:.4f}'.format(epoch + 1, i, loss.item())) # save the model for resident prediction torch.save(modelR, '../weights/parameters' + time.strftime('modelR-%Y-%m-%d_%H-%M-%S', time.localtime(time.time())) + '.pkl') modelA = Net(NUM_ACTIVITY, BS) modelA.to(device) optimizer_A = optim.Adam(modelA.parameters(), lr=LR, weight_decay=L2) train_loader2 = Data.DataLoader(dataset=train_data, batch_size=BS, shuffle=False, num_workers=0) # nni code # best_loss = 0 # training stage for activity print('training activity ...') idx = 0 for epoch in range(N_EPOCHS): # nni code # running_loss = 0 for i, data in enumerate(train_loader2): idx += 1 optimizer_A.zero_grad() inputs, labels = data # jump off the last batch if inputs.shape[0] < BS: continue # estimate whether GPU exists if torch.cuda.is_available() and use_gpu: outputs = modelA(inputs.cuda()) loss = loss_fn(outputs, labels[:, 1].cuda()) else: outputs = modelA(inputs) loss = loss_fn(outputs, labels) loss.backward() # back propagation optimizer_A.step() # weights update # nni code # running_loss += loss.item() # idx += 1 # loss visualization writer.add_scalar('activity loss', loss.item(), global_step=idx) print('epoch {}, batch {} activity loss: {:.4f}'.format(epoch + 1, i, loss.item())) torch.save(modelR, '../weights/parameters' + time.strftime('modelA-%Y-%m-%d_%H-%M-%S', time.localtime(time.time())) + '.pkl') print('Done.') # evaluation test_data = testset(SAMPLE_LENGTH) test_loader1 = Data.DataLoader(dataset=test_data, batch_size=BATCH_SIZE, shuffle=False, num_workers=0) test_loader2 = Data.DataLoader(dataset=test_data, batch_size=BS, shuffle=False, num_workers=0) evaluation(modelR, modelA, train_loader, train_loader2)
def supervised_1view(data, FLAGS, do_val = True): """ :param data: input data :param do_val: whether do validation. :return sess: the current session. :return ae: the trained autoencoder. :return acc: the accuracy on validation/training batch. """ if FLAGS.initialize == True: file_dir = FLAGS.data_dir + 'initialize_encoder.mat' matContents = sio.loadmat(file_dir) AE_initialize = matContents sess = tf.Session() acc_record = deque([]) N_record = 5 with sess.graph.as_default(): # here we need to perform the parameter selection ae_shape = FLAGS.NN_dims_1 ae = AutoEncoder(ae_shape) if FLAGS.initialize: ae._setup_variables(FLAGS.initialize, AE_initialize) input_pl = tf.placeholder(tf.float32, shape=(None, FLAGS.dimension), name = 'input_pl') logits = ae.supervised_net(input_pl, FLAGS.num_hidden_layers+1) labels_placeholder = tf.placeholder(tf.float32, shape=(None, FLAGS.num_classes), name = 'target_pl') alpha_placeholder = tf.placeholder(tf.float32, None, name = 'alpha_pl') loss, sp, cr = loss_supervised(logits, labels_placeholder, ae, alpha_placeholder) train_op = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(loss) sess.run(tf.global_variables_initializer()) for step in range(FLAGS.supervised_train_steps): # gradually increase alpha, for fast convergence alpha = 0. + FLAGS.alpha * (1 - np.exp(-(step+0.)/300.)) if alpha >= FLAGS.alpha * (1.0-1e-2): alpha = FLAGS.alpha input_feed, target_feed = data.train.next_batch(FLAGS.batch_size) feed_dict_supervised = {input_pl: input_feed, labels_placeholder: target_feed, alpha_placeholder: alpha} accuracy, est = evaluation(logits, labels_placeholder) sess.run(train_op, feed_dict=feed_dict_supervised) acc_train, loss_value, penalty, cr_value, estimation = \ sess.run([accuracy, loss, sp, cr, est], feed_dict=feed_dict_supervised) # Print training process. if (step+1) % FLAGS.display_steps == 0 or step+1 == FLAGS.supervised_train_steps or step == 0: print(alpha) output ='Train step '+str(step+1)+' minibatch loss: '+ str(loss_value) + ' penalty:' + str(penalty)+ \ ' accuracy: ' + str(acc_train) print(output) acc = acc_train if do_val: if do_val: acc_val, _ = do_validation(sess, ae, data.validation, FLAGS) else: acc_val = acc_train acc_record.append(acc_val) if len(acc_record) > N_record: acc_record.popleft() else: pass acc_val_show = max(acc_record) output = 'accuracy on validation: ' + str(acc_val_show) print(output) acc = acc_val return sess, acc, ae
def supervised_unsupervised_1view(sess, ae, kmeans, data, FLAGS, do_validation_flag = True): ''' :param ae: the autoencoder to be trained. :param kmeans: the kmeans object indicating the cluster centers and assignments. :param data: DataSet object. :param do_validation_flag: whether to do validation. :return sess: the current session. :return acc: the accuracy on validation set/training batch. :return ae: the trained autoencoder. :return kmeans: the trained kmeans object. ''' num_train = data.train.num_examples num_hidden_layers = FLAGS.num_hidden_layers num_training = 100 train_epochs = int(np.floor(FLAGS.train_steps/num_training)) acc_record = deque([]) N_record = 5 with sess.graph.as_default(): input_batch_pl = tf.placeholder(tf.float32, shape=(None, FLAGS.dimension),name='input_pl') input_center_pl = tf.placeholder(tf.float32, shape=(None, FLAGS.hidden_layer_dim), name='input_center') input_target_pl = tf.placeholder(tf.float32, shape=(None, FLAGS.num_classes),name='input_target') logits = ae.supervised_net(input_batch_pl, num_hidden_layers+1) accuracy, _ = evaluation(logits, input_target_pl) loss, kmeans_loss, sp, cr = loss_supervised_unsupervised(ae, logits, input_target_pl, ae.supervised_net(input_batch_pl, FLAGS.num_hidden_layers), input_center_pl, FLAGS) train_op = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(loss) initialize_uninitialized(sess) for epochs in range(0, train_epochs): print('\n' +'Training circle: ' + str(epochs)) if epochs == 0: pass else: last_layer_train = do_get_hidden(sess, ae, data.train, num_hidden_layers, FLAGS) kmeans = KMeans(n_clusters=FLAGS.num_clusters,init='k-means++', max_iter=50, tol=0.01).fit(last_layer_train) centers = kmeans.cluster_centers_ # convert centers to center matrix assignment = kmeans.labels_ ASS = np.zeros([num_train, FLAGS.num_clusters]) for i in range(num_train): ASS[i, assignment[i]] = 1 center_matrix = np.dot(ASS, centers) center_set = DataSet(center_matrix, ASS) center_set._index_in_epoch = data.train.start_index input_feed, target_feed = data.train.next_batch(FLAGS.batch_size) centers_feed, _ = center_set.next_batch(FLAGS.batch_size, UNSUPERVISED = True) feed_dict_combined = {input_batch_pl: input_feed, input_target_pl: target_feed, input_center_pl: centers_feed} for step in range(num_training): _, loss_value, kl, penalty, acc_train = sess.run([train_op, loss, kmeans_loss, sp, accuracy], feed_dict=feed_dict_combined) if do_validation_flag: acc_val, _ = do_validation(sess, ae, data.validation, FLAGS) else: acc_val = acc_train acc_record.append(acc_val) if len(acc_record) > N_record: acc_record.popleft() else: pass # Write the summaries and print an overview fairly often. if (step+1) % FLAGS.display_steps == 0 or step+1 == num_training or step == 0: output = 'Train step ' + str(step+1) + ' minibatch loss: ' + str(loss_value) + \ ' penalty: ' + str(penalty) + ' accuracy: ' + str(acc_train) + ' Kmeans: ' + str(kl) print(output) # do validation if do_validation_flag: acc_val, _ = do_validation(sess, ae, data.validation, FLAGS) else: acc_val = acc_train acc_record.append(acc_val) if len(acc_record) > N_record: acc_record.popleft() else: pass acc = acc_val ass_val_show = max(acc_record) output = 'accuracy on validation: ' + str(ass_val_show) print(output) return sess, acc, ae, kmeans
def run_simulation(self, max_searching_depth): # Plays out a "random" game from the current position, # then updates the statistics tables with the result. # A bit of an optimization here, so we have a local # variable lookup instead of an attribute access each loop. 6 stats = self.stats visited_states = set() history_copy = self.history[:] state = history_copy[-1] player = self.board.current_player(state) expand = True # the most important part # Use UCB to evaluate the nodes and for t in xrange(1, self.max_actions + 1): legal = self.board.legal_actions(history_copy) actions_states = [(p, self.board.next_state(state, p)) for p in legal] if all((player, S) in stats for p, S in actions_states): log_total = log( sum(stats[(player, S)].visits for p, S in actions_states) or 1) value, action, state = max( ((stats[(player, S)].value / (stats[(player, S)].visits or 1)) + self.C * sqrt(log_total / (stats[(player, S)].visits or 1)), p, S) for p, S in actions_states ) else: # Otherwise, just make an arbitrary decision. if(len(actions_states)<3): action, state = choice(actions_states) else: result=[] score = [] result,score=eval.evaluation(actions_states) # result = self.eval.evaluation(actions_states) action, state = choice(result) # for test # print action # print state history_copy.append(state) # Expand # `player` here and below refers to the player # who moved into that particular state. if expand and (player, state) not in stats: expand = False stats[(player, state)] = Stat() if t > self.max_depth: self.max_depth = t visited_states.add((player, state)) player = self.board.current_player(state) if self.board.is_ended(history_copy): break # Back-propagation # end_values = self.end_values(history_copy) for player, state in visited_states: if (player, state) not in stats: continue S = stats[(player, state)] S.visits += 1 S.value += end_values[player]
def index(): result = eval.evaluation() print(result) return render_template('camera.html', result=result)
def train_model(model, num_epochs, batch_size, learning_rate, device, n_augmentation, train_dataset, test_dataset, reload, save_model): logging.info(f'''Starting training : Type : {model.name} Epochs: {num_epochs} Batch size: {batch_size} Data Augmentation: {n_augmentation} Learning rate: {learning_rate} Device: {device.type} Reloading model : {reload} Saving model : {save_model}''') # Variables initialization if reload: model.load_state_dict(torch.load('Weights/last.pth',map_location=torch.device(device))) optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) last_masks = [None] * len(train_dataset) last_truths = [None] * len(train_dataset) prev_epochs = 0 losses_train = [] losses_test = [] losses_test_19 = [] losses_test_91 = [] metrics_idx = [] auc = [] f1_score = [] metrics_idx.append(0) auc.append(0.5) f1_score.append(0) # Reloading previous runs if reload: try: prev_loss = np.loadtxt('Loss/last.pth') losses_train = list(prev_loss[:, 0]) losses_test = list(prev_loss[:, 1]) losses_test_19 = list(prev_loss[:, 2]) losses_test_91 = list(prev_loss[:, 3]) prev_epochs = len(losses_train) prev_metrics = np.loadtxt('Loss/last_metrics.pth') metrics_idx = list(prev_metrics[:, 0]) auc = list(prev_metrics[:, 1]) f1_score = list(prev_metrics[:, 2]) except: print("Failed to load previous loss values") changed = 10 # EPOCH MAIN LOOP for epochs in range(0, num_epochs): # New dataset with random augmentation at each epoch train_dataset = load_dataset(IMAGE_NUM[0:22], n_augmentation, batch_size=batch_size) # Adaptive learning rate logging.info(f'Epoch {epochs}') if len(losses_train) > 100: if np.linalg.norm(losses_train[-1:-4]) < 0.01 and changed < 1: changed = 10 learning_rate /= 2 logging.info(f'Learning rate going to {learning_rate}') optimizer.lr = learning_rate else: changed -= 1 torch.autograd.set_detect_anomaly(True) loss_train = 0 loss_test = 0 loss_test_19 = 0 loss_test_91 = 0 # Every epoch has a training and validation phase # TRAIN with tqdm(desc=f'Epoch {epochs}', unit='img') as progress_bar: model.train() for i, (images, ground_truth) in enumerate(train_dataset): # Get the correct data from the dataloader images = images[0, ...] ground_truth = ground_truth[0, ...] # Upload the images to the device images = images.to(device) last_truths[i] = ground_truth # Keep track to save the masks as images ground_truth = ground_truth.to(device) # Forward propagation mask_predicted = model(images) last_masks[i] = mask_predicted # Keep track to save the masks as images # Compute loss bce_weight = torch.Tensor([1, 8]).to(device) loss = compute_loss(mask_predicted, ground_truth, bce_weight=bce_weight) loss_train += loss.item() / len(train_dataset) progress_bar.set_postfix(**{'loss': loss.item()}) # Zero the gradient and back propagation optimizer.zero_grad() loss.backward() optimizer.step() progress_bar.update(1) # TEST test_metrics = evaluation(model, test_dataset, device, save_mask=False, plot_roc=False, print_metric=False) loss_test = test_metrics["loss"] # Metrics bookkeeping #print_metrics(metrics, len(train_dataset), phase) logging.info(f'Train loss {loss_train}') logging.info(f'Test loss {loss_test}') losses_train.append(loss_train) losses_test.append(loss_test) losses_test_19.append(loss_test_19) losses_test_91.append(loss_test_91) metrics_idx.append(prev_epochs + epochs) auc.append(test_metrics["AUC"]) f1_score.append(np.max(test_metrics["F1"])) # END OF EPOCH MAIN LOOP # Save the predicted masks in an image save_masks(last_masks, last_truths, str(device), max_img=50, shuffle=False, threshold=test_metrics["best_threshold"]) logging.info(f'Best threshold {test_metrics["best_threshold"]}') # Save model weights and metrics current_datetime = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') if save_model: placeholder_file('Weights/last.pth') torch.save(model.state_dict(), 'Weights/last.pth') placeholder_file('Weights/' + current_datetime + "-" + str(prev_epochs+num_epochs) + '.pth') torch.save(model.state_dict(), 'Weights/' + current_datetime + "-" + str(prev_epochs+num_epochs) + '.pth') logging.info(f'Model saved') # Save losses loss_to_save = np.stack([np.asarray(losses_train), np.asarray(losses_test), np.asarray(losses_test_19), np.asarray(losses_test_91)], axis=1) placeholder_file( 'Loss/' + 'learning_' + str(learning_rate) + '_epoch_' + str(num_epochs) + '_time_' + current_datetime + '.pth') np.savetxt( 'Loss/' + 'learning_' + str(learning_rate) + '_epoch_' + str(num_epochs) + '_time_' + current_datetime + '.pth', loss_to_save) placeholder_file('Loss/last.pth') np.savetxt('Loss/last.pth', loss_to_save) # Save other metrics metrics_to_save = np.stack([np.asarray(metrics_idx), np.asarray(auc), np.asarray(f1_score)], axis=1) placeholder_file('Loss/last_metrics.pth') np.savetxt('Loss/last_metrics.pth', metrics_to_save) # Plot train and test losses and metrics plt.plot([i for i in range(0, len(losses_train))], losses_train, label='Train Loss = '+str(round(losses_train[len(losses_train)-1], 3))) plt.plot([i for i in range(0, len(losses_test))], losses_test, label='Test Loss = '+str(round(losses_test[len(losses_test)-1].item(), 3))) #plt.plot([i for i in range(0, len(losses_test_19))], losses_test_19, label='Test Loss 19 = '+str(round(losses_test_19[len(losses_test_19)-1].item(), 3))) #plt.plot([i for i in range(0, len(losses_test_91))], losses_test_91, label='Test Loss 91 = '+str(round(losses_test_91[len(losses_test_91)-1].item(), 3))) plt.plot(metrics_idx, [1-auc_ for auc_ in auc], label='1 - AUC (AUC = '+ str(round(float(auc[len(auc)-1]), 3)) +')') plt.plot(metrics_idx, [1-f1 for f1 in f1_score], label='1 - F1 (F1 = '+ str(round(float(f1_score[len(f1_score)-1]), 3)) +')') plt.legend() plt.ylim(bottom=0, top=1) plt.xlabel("Epochs") plt.ylabel("Metric") plt.savefig("Loss.png") plt.show() plt.close("Loss.png")
def main(): parser = argparse.ArgumentParser(description='siamrpn tracking') parser.add_argument('--dataset', default='VOT2018', type=str, help='datasets') # parser.add_argument('--config', default=config, type=str,help='config file') #parser.add_argument('--snapshot', default=snapshot, type=str,help='snapshot of models to eval') parser.add_argument('--update_path', default='./models/vot2018.pth.tar', type=str, help='eval one special video') parser.add_argument('--video', default='', type=str, help='eval one special video') parser.add_argument('--vis', action='store_true', help='whether visualzie result') args = parser.parse_args() torch.set_num_threads(5) # load config dataset_root = '/home/ubuntu/pytorch/pytorch-tracking/UpdateNet/datasets/' + args.dataset # tracker model_path = './models/SiamRPNBIG.model' #update_path='./updatenet/models/checkpoint40.pth.tar' update_path = args.update_path step = 3 # 1-DaSiamRPN 2-Linear 3-UpdateNet 4 checkpoint gpu_id = 0 tracker = SiamRPNTracker(model_path, update_path, gpu_id, step) #1=dasiamrpn; 2 linear; 3 updatenet # create dataset dataset = DatasetFactory.create_dataset(name=args.dataset, dataset_root=dataset_root, load_img=False) #算法的名字 model_name = tracker.name if args.dataset in ['VOT2016', 'VOT2018', 'VOT2019']: # restart tracking total_lost = 0 #for v_idx, video in enumerate(dataset): for video in tqdm(dataset): if args.video != '': # test one special video if video.name != args.video: continue frame_counter = 0 lost_number = 0 toc = 0 pred_bboxes = [] state = dict() for idx, (img, gt_bbox) in enumerate(video): # print(idx) if len(gt_bbox) == 4: gt_bbox = [ gt_bbox[0], gt_bbox[1], gt_bbox[0], gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] ] tic = cv2.getTickCount() if idx == frame_counter: state = tracker.init( img, np.array(gt_bbox)) #注意gt_bbox和gt_bbox_的区别 cx, cy, w, h = get_axis_aligned_bbox( np.array(gt_bbox)) #1-based pred_bbox = [cx - w / 2, cy - h / 2, w, h] #1-based pred_bboxes.append(1) elif idx > frame_counter: state = tracker.update(img) pos = state['target_pos'] # cx, cy sz = state['target_sz'] # w, h pred_bbox = np.array( [pos[0] - sz[0] / 2, pos[1] - sz[1] / 2, sz[0], sz[1]]) #pred_bbox=np.array([pos[0]+1-(sz[0]-1)/2, pos[1]+1-(sz[1]-1)/2, sz[0], sz[1]]) overlap = vot_overlap(pred_bbox, gt_bbox, (img.shape[1], img.shape[0])) if overlap > 0: # not lost pred_bboxes.append(pred_bbox) else: # lost object pred_bboxes.append(2) frame_counter = idx + 5 # skip 5 frames lost_number += 1 else: pred_bboxes.append(0) toc += cv2.getTickCount() - tic if idx == 0: cv2.destroyAllWindows() if args.vis and idx > frame_counter: cv2.polylines( img, [np.array(gt_bbox, np.int).reshape( (-1, 1, 2))], True, (0, 255, 0), 3) if cfg.MASK.MASK: cv2.polylines( img, [np.array(pred_bbox, np.int).reshape( (-1, 1, 2))], True, (0, 255, 255), 3) else: bbox = list(map(int, pred_bbox)) cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[0] + bbox[2], bbox[1] + bbox[3]), (0, 255, 255), 3) cv2.putText(img, str(idx), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.putText(img, str(lost_number), (40, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow(video.name, img) cv2.waitKey(1) toc /= cv2.getTickFrequency() # save results video_path = os.path.join('results', args.dataset, model_name, 'baseline', video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: if isinstance(x, int): f.write("{:d}\n".format(x)) else: f.write(','.join([vot_float2str("%.4f", i) for i in x]) + '\n') # print('({:3d}) Video: {:12s} Time: {:4.1f}s Speed: {:3.1f}fps Lost: {:d}'.format( # v_idx+1, video.name, toc, idx / toc, lost_number)) total_lost += lost_number # print("{:s} total lost: {:d}".format(model_name, total_lost)) else: # OPE tracking #for v_idx, video in enumerate(dataset): for video in tqdm(dataset): if args.video != '': # test one special video if video.name != args.video: continue toc = 0 pred_bboxes = [] scores = [] track_times = [] state = dict() for idx, (img, gt_bbox) in enumerate(video): tic = cv2.getTickCount() if idx == 0: state = tracker.init( img, np.array(gt_bbox)) #注意gt_bbox和gt_bbox_的区别 cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) pred_bbox = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] scores.append(None) if 'VOT2018-LT' == args.dataset: pred_bboxes.append([1]) else: pred_bboxes.append(pred_bbox) else: state = tracker.update(img) pos = state['target_pos'] sz = state['target_sz'] pred_bbox = np.array( [pos[0] - sz[0] / 2, pos[1] - sz[1] / 2, sz[0], sz[1]]) pred_bboxes.append(pred_bbox) #scores.append(outputs['best_score']) toc += cv2.getTickCount() - tic track_times.append( (cv2.getTickCount() - tic) / cv2.getTickFrequency()) if idx == 0: cv2.destroyAllWindows() if args.vis and idx > 0: gt_bbox = list(map(int, gt_bbox)) pred_bbox = list(map(int, pred_bbox)) cv2.rectangle( img, (gt_bbox[0], gt_bbox[1]), (gt_bbox[0] + gt_bbox[2], gt_bbox[1] + gt_bbox[3]), (0, 255, 0), 3) cv2.rectangle(img, (pred_bbox[0], pred_bbox[1]), (pred_bbox[0] + pred_bbox[2], pred_bbox[1] + pred_bbox[3]), (0, 255, 255), 3) cv2.putText(img, str(idx), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.imshow(video.name, img) cv2.waitKey(1) toc /= cv2.getTickFrequency() # save results if 'VOT2018-LT' == args.dataset: video_path = os.path.join('results', args.dataset, model_name, 'longterm', video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: f.write(','.join([str(i) for i in x]) + '\n') result_path = os.path.join( video_path, '{}_001_confidence.value'.format(video.name)) with open(result_path, 'w') as f: for x in scores: f.write('\n') if x is None else f.write( "{:.6f}\n".format(x)) result_path = os.path.join(video_path, '{}_time.txt'.format(video.name)) with open(result_path, 'w') as f: for x in track_times: f.write("{:.6f}\n".format(x)) elif 'GOT-10k' == args.dataset: video_path = os.path.join('results', args.dataset, model_name, video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: f.write(','.join([str(i) for i in x]) + '\n') result_path = os.path.join(video_path, '{}_time.txt'.format(video.name)) with open(result_path, 'w') as f: for x in track_times: f.write("{:.6f}\n".format(x)) else: model_path = os.path.join('results', args.dataset, model_name) if not os.path.isdir(model_path): os.makedirs(model_path) result_path = os.path.join(model_path, '{}.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: f.write(','.join([str(i) for i in x]) + '\n') # print('({:3d}) Video: {:12s} Time: {:5.1f}s Speed: {:3.1f}fps'.format( # v_idx+1, video.name, toc, idx / toc)) evaluation(args.dataset, model_name)
# TriggerHMM.beta seems to work better with no smoothing operation after plenty of experiments TriggerHMM.beta = 0 TriggerHMM.alpha = 8e-6 TriggerPred = PredictionAlg() TriggerPred.test_read("trigger_test.txt") TriggerPred.solve(TriggerHMM, method) with open("trigger_result.txt", "w", encoding="UTF-8") as out: for i in range(len(TriggerPred.test_set)): if TriggerPred.test_set[i] == "": out.write("\n") continue out.write(TriggerPred.test_set[i] + "\t" + TriggerPred.answer[i] + "\t" + TriggerPred.pred[i] + "\n") if __name__ == '__main__': from eval import evaluation # pred2file("greedy") # pred2file("viterbi") pred2file("greedy") evaluation('trigger') evaluation('argument') pred2file("viterbi") evaluation('trigger') evaluation('argument')
parser.add_argument('--weather_past', help='input weather training data 1 (past) file name') parser.add_argument( '--weather_forecast', help='input weather training data 2 (forecast) file name') parser.add_argument('--output', default='submission.csv', help='output file name') args = parser.parse_args() # The following part is an example. # You can modify it at will. if args.mode == 'train': print('training mode') training(args.training, args.training2, args.weather_past, args.weather_forecast, args.output, epoches=100) elif args.mode == 'eval': print('eval mode') evaluation(args.training, args.training2, args.weather_past, args.weather_forecast, args.output) else: print('unknown mode, please try \'train\' or \'eval\'')