def predict_over_video(video_frame_list, window_width=9, stride=1): if window_width < 9: raise ValueError("window_width must be 9 or greater") print("Loading model...") model = load_model( modality="RGB", state_dict_file="pretrained_chkpt/rgb_hmdb_split1.pt" ) model.eval() print("Predicting actions over {0} frames".format(len(video_frame_list))) with torch.no_grad(): window_count = 0 for i in range(stride+window_width-1, len(video_frame_list), stride): window_frame_list = [video_frame_list[j] for j in range(i-window_width, i)] frames = load_frames(window_frame_list) batch = construct_input(frames) window_predictions = predict_input(model, batch) window_proba = F.softmax(window_predictions, dim=1) window_top_pred = window_proba.max(1) print(("Window:{0} Class pred:{1} Class proba:{2}".format( window_count, window_top_pred.indices.cpu().numpy()[0], window_top_pred.values.cpu().numpy()[0]) )) window_count += 1
def main(model_name): # img_pool = '/home/liupeng/workspace/Temporal_2D/data/images/' \ # 's_09_act_15_subact_02_ca_04/s_09_act_15_subact_02_ca_04_000' img_pool = '/home/liupeng/workspace/Temporal_2D/data/video/test.mp4.jpg/' start_idx = 70 cell, hide = None, None initial = True model = load_model(model_name) img_paths = [] img_points = [] with torch.no_grad(): for i in tqdm.tqdm(range(100)): img_path = img_pool + '{}.jpg'.format(start_idx + i) img, img_h, img_w = get_img(img_path) if i > 0: initial = False outputs, cell, hide = model(img, initial, cell, hide) output = outputs[0][0].detach().cpu().numpy() points = hp2points(output, cfg, img_h, img_w) img_paths.append(img_path) img_points.append(points) torch.cuda.empty_cache() vis_to_video(img_points, img_h, img_w)
def run(): questions = [u"我真的好喜欢你,你认为呢?", u"品尝大董意境菜时兴奋不已,并起身激情拥抱"] answers = [u"我也非常喜欢你。", "这个瞬间捕捉得很妙啊。"] model = load_model("model/seq2seq_model_weights.h5") enc_padding_ids = data_to_padding_ids(questions) prediction_embedding = model.predict_on_batch(enc_padding_ids) real_embedding = generate_real_embedding(answers) average_mse_list = [] for pre, real in zip(prediction_embedding, real_embedding): error = pre - real square_error = np.square(error) square_sum_error = np.sum(square_error, axis=-1) average_mse = np.sum(square_sum_error) / len(square_sum_error) average_mse_list.append(average_mse) print("score: " + str(average_mse_list) + " ---> 0")
def run(): questions = [u"我喜欢你?", u"品尝大董意境菜时兴奋不已,并起身激情拥抱"] model = load_model("model/seq2seq_model_weights.h5") prediction_words = common_prediction(model, questions) for index in range(len(questions)): print("------------------------------\n") print("问: " + questions[index]) answer = assembly_word(prediction_words[index]) print("答: " + answer) print("------------------------------\n")
def run(): questions = [u"我在这寒冷的夜里瑟瑟发抖", u"你在干嘛"] model = load_model("model/seq2seq_model_weights.h5") prediction_words = common_prediction(model, questions) for index in range(len(questions)): print("------------------------------\n") print("问: " + questions[index]) answer = assembly_word(prediction_words[index]) print("答: " + answer) print("------------------------------\n")
XT_files, XT_labels = load_descfile(opt.FilenameXT) for root, dirs, files in os.walk(opt.test_model_list): for file in files: if not file.endswith('.pth') or not file.startswith(opt.backbone): continue model_path = os.path.join(root, file) models_list.append(model_path) models_list = sorted(models_list, key=lambda x: os.path.getmtime(x), reverse=True) for model_path in models_list: try: load_model(model, model_path) except Exception as e: print('load embedding model error', model_path, str(e)) continue try: cls_path = model_path.replace(opt.backbone, 'cls') load_model(metric_fc, cls_path) except Exception as e: print('load classifier model error', model_path, str(e)) continue model.eval() metric_fc.eval() model_name = os.path.basename(model_path).replace('.pth', '')
save_path = output_dir + '/k/' + 'k' + str(k) + '_c' + str(cid) # stylized new_model = deepcopy(orig_model) new_model.state_dict()['style_bank.' + style_id + '.0.conv2d.weight'].copy_(mask) # style_id = list(map(int, style_id.split(' '))) stylized2(self.prep_img, new_model, save_path, [int(style_id)]) del new_model for jidx in range(0, k): getattr(self, 'label_{}_{}'.format(str(k), jidx + 1)).setPixmap( QPixmap(save_path.split('_c')[0] + '_c' + str(jidx) + '.png').scaled(80, 80, PyQt5.QtCore.Qt.KeepAspectRatio)) def checkchoice(self, state): checkBox = self.sender() if state == PyQt5.QtCore.Qt.Unchecked: self.style_elements.remove(checkBox.id_) elif state == PyQt5.QtCore.Qt.Checked: self.style_elements.append(checkBox.id_) print(self.style_elements) if __name__ == '__main__': orig_model = load_model() app = QApplication(sys.argv) MainWindow = MainWindow(orig_model) sys.exit(app.exec_())
import posixpath import BaseHTTPServer from SocketServer import ThreadingMixIn import urllib, urllib2 import cgi import shutil import mimetypes import re import time import test import tensorflow as tf import cv2 from cStringIO import StringIO sess = tf.Session() model_root = '../model/model.cpkt' tensorA, tensorB = test.load_model(sess, model_root + '.meta', model_root) def showTips(): print "" print '----------------------------------------------------------------------->> ' try: port = int(sys.argv[1]) except Exception, e: print '-------->> Warning: Port is not given, will use deafult port: 8080 ' print '-------->> if you want to use other port, please execute: ' print '-------->> python SimpleHTTPServerWithUpload.py port ' print "-------->> port is a integer and it's range: 1024 < port < 65535 " port = 8011 if not 1024 < port < 65535: port = 8080
from bottle import route, run import json from test import init, load_model, predict config, max_sent_len, vocab, inv_label_vocab = init() session, model = load_model(config) @route('/getEntities/<sent>') def index(sent): from bottle import response #try: output = predict (session, model, vocab, inv_label_vocab, max_sent_len, sent) response.content_type = 'application/json' ''' Check if this is a good or bad response. ''' return json.dumps(output) #except ValueError: # print "Error" # return '' run(host='localhost', port=7760, reloader=True)
return html + "<br><br>\n" def show(model, test, id2w, id2class): filename = 'show_naist.html' with open(filename, 'w') as f: f.write(' ') # for i in range(len(test)): for i in range(1): lxs, rxs, ts = seq_convert([test[i]]) with chainer.no_backprop_mode(), chainer.using_config('train', False): predict = model.predict(lxs, rxs, argmax=True) left_words = [id2w.get(int(idx), '') for idx in lxs[0]] right_words = [id2w.get(int(idx), '') for idx in rxs[0]] target = id2class.get(int(ts[0])) predict = id2class.get(int(predict[0])) # print('{} [{} {}] {}'.format(' '.join(left_words), predict, target, ' '.join(right_words))) left_scores = model.left_attn.scores[0].data.tolist() right_scores = model.right_attn.scores[0].data.tolist()[::-1] words = left_words + ['[{} {}]'.format(predict, target)] + right_words scores = left_scores + [0] + right_scores result = predict == target html = make_html(words, scores) with open(filename, 'a') as f: f.write('<b>{}</b>\n'.format(result)) f.write(html + '\n') if __name__ == '__main__': show(*load_model())