def accuracy(predictions, labels, alphabet): predictions = predictions.softmax().topk(axis=2).asnumpy() zipped = zip(decode(predictions, alphabet), decode(labels.asnumpy(), alphabet)) n_correct = 0 for pred, target in zipped: if pred == target: n_correct += 1 return n_correct
def accuracy_batch(self, predictions, labels, phase): n_correct = 0 edit_dis = 0.0 for gpu_prediction, gpu_label in zip(predictions, labels): gpu_prediction = gpu_prediction.softmax().asnumpy() zipped = zip(decode(gpu_prediction, self.alphabet), decode(gpu_label.asnumpy(), self.alphabet)) logged = False for (pred, pred_conf), (target, _) in zipped: if self.tensorboard_enable and not logged: self.writer.add_text(tag='{}/pred'.format(phase), text='pred: {} -- gt:{}'.format( pred, target), global_step=self.global_step) logged = True edit_dis += Levenshtein.distance(pred, target) if pred == target: n_correct += 1 return {'n_correct': n_correct, 'edit_dis': edit_dis}
def gey_summary(): args = ARGS() text = request.form['text'] lang = detect(text) print('Language: ', lang) if lang == 'ja': model_dir = model_dir_ja extractor = extractor_ja abstractor = abstractor_ja else: model_dir = model_dir_en extractor = extractor_en abstractor = abstractor_en setattr(args, 'model_dir', model_dir) setattr(args, 'batch', 1) setattr(args, 'beam', beam_size) setattr(args, 'div', 1.0) setattr(args, 'max_dec_word', 30) setattr(args, 'cuda', cuda) setattr(args, 'extractor', extractor) setattr(args, 'abstractor', abstractor) #text = request.form['text'] #print(text) setattr(args, 'text', text) text, result, source_text = decode(args, True) print(source_text) #print(type(result)) if lang == 'ja': result = result.replace(' ', '') else: result = result.split('\n\n') for i, r in enumerate(result): r = r[0].upper() + r[1:] result[i] = r result = '\n\n'.join(result) #print(result) with open('/mnt/binhna/summary/log.txt', 'a') as f: f.write( f"\n\n======================={datetime.now().strftime('%Y-%m-%d %H:%M:%S')}=======================\n" ) f.write(text) f.write( "\n=============================================================================================\n" ) f.write(result) return jsonify({'summary': result, 'highlight': source_text})