def upload(): global result global imgname global idnum f = request.files['file'] img = Image.open(f.filename) app = Predict() result = app.predict(img) imgname = str(secure_filename(f.filename)) # store to cassandra global session session.execute("""INSERT INTO mnist (imgname, result) VALUES ('""" + imgname + """','""" + str(result) + """')""") '''session.execute("""INSERT INTO mnist (id, imgname, result) VALUES (1,imgname,result)""")''' '''session.execute( """ INSERT INTO demandtable (id, imgname, result) VALUES (%s, %s) """, (1,imgname,result) )''' return ''' <!doctype html> <html> <body> '''+str(result)+'''
def main(args): init_logger() set_seed(args) tokenizer = load_tokenizer(args) train_dataset = None dev_dataset = None test_dataset = None if args.do_train or args.do_eval: test_dataset = load_and_cache_examples(args, tokenizer, mode="test") if args.do_train: train_dataset = load_and_cache_examples(args, tokenizer, mode="train") trainer = Trainer(args, train_dataset, dev_dataset, test_dataset) predictor = Predict(args) if args.do_train: trainer.train() if args.do_eval: trainer.load_model() trainer.evaluate("test", "eval") if args.do_predict: predictor.predict()
def predict(): if request.method == 'POST': #In case no file was uploaded if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] #In case file has an empty name if file.filename == '': flash('No selected file') return redirect(request.url) #Everything is correct and we can run the prediction if file and allowed_file(file.filename): #save and read uploaded image filename = secure_filename(file.filename) file.save(secure_filename(file.filename)) image = Image.open(file.filename) #flatten_img = np.reshape(image, 784) app = Predict() x = app.predict(image) current_time = str(datetime.datetime.now()) session.execute( """ INSERT INTO demandtable (time, filename, result) VALUES (%s, %s, %s) """, (current_time, filename, x)) return '识别结果:' + x
def __init__(self, parent, posx, posy, *kwargs): self.predict = Predict() self.parent = parent self.posx = posx self.posy = posy self.sizex = 400 self.sizey = 400 self.b1 = "up" self.xold = None self.yold = None self.coords = [] self.drawing_area = tk.Canvas(self.parent, width=self.sizex, bg='black', height=self.sizey) self.drawing_area.place(x=self.posx, y=self.posy) self.drawing_area.bind("<Motion>", self.motion) self.drawing_area.bind("<ButtonPress-1>", self.b1down) self.drawing_area.bind("<ButtonRelease-1>", self.b1up) self.button = tk.Button(self.parent, text="Done!", width=10, bg='white', command=self.save) self.button.place(x=self.sizex / 7, y=self.sizey + 20) self.button1 = tk.Button(self.parent, text="Clear!", width=10, bg='white', command=self.clear) self.button1.place(x=(self.sizex / 7) + 80, y=self.sizey + 20) self.img2Predict = [] self.image = Image.new("RGB", (400, 400), (255, 255, 255)) self.draw = ImageDraw.Draw(self.image)
def semi_supervised(samples_path, write_path, beam_search): """use reference to predict source Args: samples_path (str): The path of reference write_path (str): The path of new samples """ pred = Predict() print('vocab_size: ', len(pred.vocab)) # Randomly pick a sample in test set to predict. count = 0 semi = [] with open(samples_path, 'r') as f: for picked in f: count += 1 source, ref = picked.strip().split('<sep>') prediction = pred.predict(ref.split(), beam_search=beam_search) semi.append(prediction + ' <sep> ' + ref) if count % 100 == 0: print(count) write_samples(semi, write_path, 'a') semi = []
def predict_dataset(dataset, encoder, nn, subtypes=None, split='test'): """ Run prediction for body and move over the testsets in the dataset object :param dataset: the dataset :param encoder: the encoder to be used :param nn: the network to be used to make the prediction :param subtypes: normally 'body' and 'move' :param split: whether to perform the prediction over the 'test' or 'train' splits :return: """ if subtypes is None: subtypes = ['body', 'move'] prediction = {} for name in subtypes: if split == 'test': prediction[name] = Predict(dataset[name].X_test, dataset[name].y_test, encoder.onehot[name]) else: prediction[name] = Predict(dataset[name].X_train, dataset[name].y_train, encoder.onehot[name]) call_predict = getattr(prediction[name], 'predict_{}_batch'.format(name)) call_predict(nn[name]) return prediction
def upload_model(self): predict = Predict() predict.saved_model_pb() hdfs_checkpoint_path = os.path.join( self.flags.output_path, os.path.basename(os.path.normpath(self.flags.checkpoint_path))) hdfs_saved_model_path = os.path.join( self.flags.output_path, os.path.basename(os.path.normpath(self.flags.saved_model_path))) temp_hdfs_checkpoint_path = hdfs_checkpoint_path + '-temp' temp_hdfs_saved_model_path = hdfs_saved_model_path + '-temp' self.hdfs_client.hdfs_upload(self.flags.checkpoint_path, temp_hdfs_checkpoint_path) self.hdfs_client.hdfs_upload(self.flags.saved_model_path, temp_hdfs_saved_model_path) self.hdfs_client.hdfs_delete(hdfs_checkpoint_path) self.hdfs_client.hdfs_delete(hdfs_saved_model_path) self.hdfs_client.hdfs_mv(temp_hdfs_checkpoint_path, hdfs_checkpoint_path) self.hdfs_client.hdfs_mv(temp_hdfs_saved_model_path, hdfs_saved_model_path)
def generate_predicted_files(): # set the proper symbol file and model file symbol_file_path = '../symbol_farm/symbol_10_160_17L_4scales_v1_deploy.json' model_file_path = '../saved_model/configuration_10_160_17L_4scales_v1_2019-09-20-13-08-26/train_10_160_17L_4scales_v1_iter_800000.params' my_predictor = Predict( mxnet=mxnet, symbol_file_path=symbol_file_path, model_file_path=model_file_path, ctx=mxnet.gpu(0), receptive_field_list=cfg.param_receptive_field_list, receptive_field_stride=cfg.param_receptive_field_stride, bbox_small_list=cfg.param_bbox_small_list, bbox_large_list=cfg.param_bbox_large_list, receptive_field_center_start=cfg.param_receptive_field_center_start, num_output_scales=cfg.param_num_output_scales) # set the val root, the path should look like XXXX/WIDER_val/images txt_file_path = '/media/heyonghao/HYH-4T-WD/public_dataset/head_detection/brainwash/brainwash/brainwash_test.idl' image_root = '/media/heyonghao/HYH-4T-WD/public_dataset/head_detection/brainwash/brainwash' predicted_file_root = './brainwash_testset_predicted_files_for_evaluation_' + os.path.basename( model_file_path).split('.')[0] if not os.path.exists(predicted_file_root): os.makedirs(predicted_file_root) fin = open(txt_file_path, 'r') resize_scale = 1 score_threshold = 0.05 NMS_threshold = 0.6 counter = 0 for line in fin: line = line.strip(';\n') im_path = re.findall('["](.*?)["]', line)[0] im = cv2.imread(os.path.join(image_root, im_path), cv2.IMREAD_COLOR) bboxes = my_predictor.predict(im, resize_scale=resize_scale, score_threshold=score_threshold, top_k=10000, NMS_threshold=NMS_threshold) # for bbox in bboxes: # cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 255, 0), 1) # cv2.imshow('im',im) # cv2.waitKey() predicted_file_name = im_path.replace('/', '_') predicted_file_name = predicted_file_name.replace('png', 'txt') fout = open(os.path.join(predicted_file_root, predicted_file_name), 'w') for bbox in bboxes: fout.write('head %.03f %d %d %d %d' % (bbox[4] if bbox[4] <= 1 else 1, math.floor(bbox[0]), math.floor(bbox[1]), math.ceil(bbox[2] - bbox[0]), math.ceil(bbox[3] - bbox[1])) + '\n') fout.close() counter += 1 print('[%d] is processed.' % counter)
def predict(): param = jsonify((request.data).decode('utf-8')) param = json.loads(param.json) param = param['article'] model = Predict(param) response = jsonify(model.predict()) return response
def main(): args = parse_arguments() print("Restore", args.restore) if args.train: ssan = SSCAN( args.batch_size, args.model_path, args.examples_path, args.vocab_size, args.train_file, args.restore ) ssan.train(args.iteration_count) if args.test: ocr_test = Predict( args.batch_size, args.model_path, args.examples_path, args.vocab_size, args.train_file, True ) ocr_test.pred()
def semi_supervised(samples_path, write_path, beam_search): """use reference to predict source Args: samples_path (str): The path of reference write_path (str): The path of new samples """ ########################################### # TODO: module 3 task 1 # ########################################### pred = Predict() print('vocab_size:', len(pred.vocab)) count = 0 semi = [] with open(samples_path, 'r') as f: for picked in f: count += 1 source, ref = picked.strip().split('<sep>') prediction = pred.predict(ref.split(), beam_search=beam_search) # 拼接ref的预测结果与ref,形成新的样本 semi.append(prediction + ' <sep> ' + ref) if count % 100 == 0: print(count) write_samples(semi, write_path, 'a') semi = []
def main(args): init_logger() tokenizer = load_tokenizer(args) train_dataset = None if args.do_predict else load_and_cache_examples( args, tokenizer, mode="train") dev_dataset = None test_dataset = None if args.do_predict else load_and_cache_examples( args, tokenizer, mode="test") if args.do_train: trainer = Trainer(args, train_dataset, dev_dataset, test_dataset) trainer.train() if args.do_eval: trainer = Trainer(args, train_dataset, dev_dataset, test_dataset) trainer.load_model() trainer.evaluate("test") if args.do_predict: predict = Predict(args, tokenizer) predict.load_model() sentences = [args.sentence] result_json = dict() result_json['result'] = int(predict.predict(sentences)) print(json.dumps(result_json, ensure_ascii=False))
def single_execution(config): """ Executes training/prediction for the config as a single individual process :param config: config object :return: """ os.environ["CUDA_VISIBLE_DEVICES"] = str(config.default_gpu)[-1] from data_processing import CDRSInferenceData, Dataset from train import Train from predict import Predict if config.cdrs_inference: dataset = CDRSInferenceData(config) else: dataset = Dataset(config) # training if config.train: print("Training") train = Train(config, dataset) train.train() # prediction else: print("Prediction") pred = Predict(config, dataset) pred.predict() return
def pred(): p = Predict('text-cnn.model', 100) #! Not working -_- preds are way off p.predict([ 'Whats up champ', 'call us to get some free goods', 'when will you are going ?' ])
def __init__(self, path, vocab=None): self.predict = Predict() self.rouge = Rouge() self.testset = SamplesDataset(path, vocab=self.predict.vocab) print('test vocab: ', len(self.testset.vocab)) self.refs = self.process() self.hypos = self.build_hypos() self.scores = self.get_scores()
def predict_frame(self, imgPath): Eng2Chi = {"green": "绿色", "blue": "蓝色", "yellow": "黄色"} q = Predict() afterprocess, old = q.preprocess(imgPath) colors, card_imgs = q.locate_carPlate(afterprocess, old) result, roi, color, divs = q.char_recogize(colors, card_imgs) # all list if len(result) == 0: print("未能识别到车牌") self.colorLabel.setText("抱歉未能识别到车牌") self.NumLabel.setText("抱歉,未能识别到车牌") else: for r in range(len(result)): print("#" * 10 + "识别结果是" + "#" * 10) print("车牌的颜色为:" + Eng2Chi[color[r]]) self.colorLabel.setText(Eng2Chi[color[r]]) final_color = Eng2Chi[color[r]] if final_color == "蓝色": self.colorLabel.setStyleSheet( "QLabel{background-color:blue;color:white;font: 20pt;alignment:center;}" ) if final_color == "黄色": self.colorLabel.setStyleSheet( "QLabel{background-color:yellow;color:black;font: 20pt;alignment:center;}" ) if final_color == "绿色": self.colorLabel.setStyleSheet( "QLabel{background-color:green;color:white;font: 20pt;alignment:center;}" ) self.colorLabel.setText(final_color) print(result[r]) result[r].insert(2, "-") final_result = ''.join(result[r]) self.NumLabel.setText(''.join(result[r])) print("#" * 25) # roi[r] = cv2.cvtColor(roi[r], cv2.COLOR_BGR2RGB) # QtImg = QtGui.QImage(roi[r].data, roi[r].shape[1], roi[r].shape[0], QtGui.QImage.Format_RGB888) # # 显示图片到label中 # self.location.resize(QtCore.QSize(roi[r].shape[1], roi[r].shape[0])) # self.location.setPixmap(QtGui.QPixmap.fromImage(QtImg)) if len(final_result ) >= 8 and u'\u4e00' <= final_result[0] <= u'\u9fff': #保存至MYSQL数据库 conn, cur = self.connetSQL() currenttime = datetime.datetime.now() print(currenttime.strftime('%Y-%m-%d %H:%M:%S')) try: cur.execute( "insert into plate(plate_num,plate_color,time)values(%s,%s,%s)", (final_result, final_color, currenttime)) conn.commit() except Exception as e: print("expect: ", e) finally: cur.close() conn.close()
def run_model(text): ''' run the model on given text ''' model_path = settings.model_path dictionary_path = settings.dictionary_path pp = Predict(model_path, dictionary_path) results = pp.run(text) return results
def predict_main(): BATCH_SIZE = 25 test_dataset = Feeder(False, True) test_loader = DataLoader(dataset=test_dataset, batch_size=25, shuffle=False) model = Model() predict = Predict(model, 'pt文件路径', test_loader) output = predict.predict_multi() output = F.softmax(output) # 若网络最后无softmax(推荐)则加上这句。 print(output)
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01): n = {} n["W"] = [ weight_init_std * np.random.randn(s1, s2) for s1, s2 in ((input_size, hidden_size), (hidden_size, output_size)) ] n["B"] = [np.zeros(s) for s in (hidden_size, output_size)] self._net = n self._p = Predict(sigmoid, softmax)
def read_queue(): try: servicebus_client = ServiceBusClient.from_connection_string( conn_str=conf.ML_QUEUE_CONNECTION_STR, logging_enable=True) with servicebus_client: # get the Queue Receiver object for the queue receiver = servicebus_client.get_queue_receiver( queue_name=conf.ML_QUEUE_NAME, max_wait_time=5) queue_empty = True with receiver: for msg in receiver: queue_empty = False msg_dict = json.loads(str(msg)) print(msg_dict) p = Predict(msg_dict['container_name'], msg_dict['blob_name'], msg_dict['request_id']) p.update_local_list(msg_dict['request_id'], "APPEND") receiver.complete_message(msg) p.main(reupload_flag=True) p.update_local_list(msg_dict['request_id'], "REMOVE") # receiver.complete_message(msg) return queue_empty except Exception as ex: print('Exception:') print(ex)
def test_run_predict(self): """ Test case to make sure that the results emitted during training match inference :return: """ # Arrange # get train factory model_factory_name = ModelFactoryServiceLocator().factory_names[0] # get datasetF img_dir = os.path.join(os.path.dirname(__file__), "data", "small_clips") # construct dataset dataset_factory = DatasetFactoryServiceLocator().get_factory( "Mot17DetectionFactory") dataset = dataset_factory.get_dataset(img_dir) # kick off a single training run train_factory = TrainFactory(model_factory_name, num_workers=1, epochs=1, batch_size=6, early_stopping=True, patience_epochs=2) output_dir = tempfile.mkdtemp() model_dir = tempfile.mkdtemp() pipeline = train_factory.get(dataset) score, expected_predictions, model_path = pipeline.run( dataset, dataset, output_dir, model_dir) # construct predictor sut = Predict(model_dict_path=model_path, model_factory_name=model_factory_name, num_classes=dataset.num_classes) # Create dataloader val_data_loader = DataLoader(dataset, num_workers=1, shuffle=False) # Act actual_predictions = sut.predict_batch(val_data_loader) # Assert for e, a in zip(expected_predictions, actual_predictions): for k in e: self.assertTrue( torch.all(torch.eq(e[k], a[k])), "Could not match key {} , \n{}\n {}".format(k, e[k], a[k]))
def ip(): cam = cv2.VideoCapture(0) cv2.namedWindow("test") predictor = Predict() ret, frame = cam.read() predictor.inference(frame) #while True: # ret, frame = cam.read() #print(frame.shape) # predictor.inference(frame) #return a # print (a) cam.release() cv2.destroyAllWindows()
def download_woff(file_name): # vfile.meituan.net/colorstone/9c846f7774c2b8280bd204adba5c669a2276.woff url = "https://vfile.meituan.net/colorstone/" + str(file_name) + ".woff" # print(url) file_name = str(file_name) + ".woff" # 这里判断一下 url 的文件是否存在 # 缓存中存在 result = decode_list.get(file_name) if result: # 如果存在的话 return str(result) # 不存在 url = re.sub(" ", "", str(url)) # 把文件下载下来 response = requests.get(url) download_path_ = str(download_path) + str(file_name) file_ = open(download_path_, 'wb') file_.write(response.content) response.close() file_.close() # 预测在这里 result = Predict.predict(download_path_) # 把预测结果放到缓存数组中去 提速 decode_list[file_name] = result return str(result)
def main(self, args): pred = Predict() parser = self.parse_args(pred.make_parser()) parsed = parser.parse_args(args) pred.temp_files_path = parsed.temp_dir # testデータ読み込み test_data = Dataset(pd.read_csv(parsed.input)) #モデル読み込み pred.model = pred.read_model(parsed.model_path) #予測を確率で出すかどうかの設定(classificationクラスのみ有効) pred.model.probability = parsed.probability #クラスラベルの処理 test_data_preprocessed = pred.preprocessing(test_data) #ターゲット列の分離 target_col_name = pred.model.target_colname y_test = test_data_preprocessed[target_col_name] x_test = test_data_preprocessed.drop(columns=target_col_name) #予測 pred_df = pred.model.predict(test_data, x_test) merged = pd.concat([pred_df, test_data], axis=1) #モデルの評価 result = self.all_metrics[parsed.metrics](y_test, pred_df) #評価結果の出力(暫定) print(parsed.metrics) print(result) # result.to_csv(parsed.metrics_file_name,index=False) #出力 #pred.set_output(merged,parsed.output) return
def test_predict(): for nm in ['nnr_k']: p = Predict(nm, 'np') departure_planned = '08:17' departure_actual = '08:17' departure_station = 'DIS' intermediate_actual = '09:02' intermediate_station = 'COL' arrival_planned = '10:05' arrival_actual = '10:11' arrival_station = 'LST' for m in range(1, 13): arrival_predict = p.predict(departure_planned, departure_actual, departure_station, arrival_planned, arrival_station, intermediate_actual, intermediate_station, datetime.datetime(2020, m, 1)) print(arrival_predict)
def predictResult(self, request, context): if not os.path.isdir('./PredictDir'): os.makedirs('./PredictDir') guid = '' for r in request: if not os.path.isdir('./PredictDir/{}'.format(r.guid)): os.makedirs('./PredictDir/{}'.format(r.guid)) if guid == '': guid = r.guid with open('./PredictDir/{}/{}'.format(r.guid, r.name), 'ab') as f: f.write(r.file) p = Predict() predictPath = p.predict(guid) with open(predictPath, 'rb') as f: lines = f.readlines() for line in tqdm(lines): yield basic_pb2.PredictResultFile(file=line)
def predict(): global values global customer global percentile if selected_filename == 'file_not_selected': st.write("Please select a customer data file to proceed") return customer = st.sidebar.selectbox("Select a customer for predicting churn", ("5575-GNVDE", "1452-KIOVK", "9763-GRSKD")) risk_factor = st.sidebar.slider("How critical retaining the customer is?", 0.0, 1.0, 0.50, 0.1) percentile = risk_factor prediction = Predict(train_data, model, customer) conditioned_sf = prediction.predict() predictions = prediction.predict_percentile(conditioned_sf, percentile) values = prediction.predict_remaining_value(predictions, percentile)
def modelsLoad(self): filelist = os.listdir(self.modeldir) for f in filelist: path = os.path.join(self.modeldir, f) id = f.split(".")[0] policy = f.split(".")[1] if policy == 'model': # 初始化 Predict 对象 pred = Predict() pred.load_model(path) self.indexclass[id] = pred else: # 初始化 Rule 对象 rule = Rule() rule.load_rule(path) self.indexclass[id] = rule logging.debug("load rule file " + id) self.predictPolicy[id] = policy logging.info('[Done] load all models')
def __init__(self, cfg_file_name): self.config = ConfigParser.ConfigParser() self.cur_dir = os.path.dirname(os.path.abspath(cfg_file_name)) self.cfg_parser(cfg_file_name) self.preprocess = Preprocess(cfg_file_name) self.cnt = 0 self.train_features = [] self.train_labels = [] self.test_features = [] self.test_labels = [] self.test_names = [] self._train = Train(self.space, self.params) self._predict = Predict() self._rule = Rule() self._tree = ClassTreePedict('./resource/cate_id.cfg', './model')
def setImg(self): self.hide() QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.CrossCursor) snapwindow = SnapWindow() snapwindow.setWindowModality(QtCore.Qt.ApplicationModal) snapwindow.exec_() QtWidgets.QApplication.restoreOverrideCursor() self.show() if snapwindow.result() == 0: qimage = snapwindow.returnSnap() if qimage is not None: img_raw = np.frombuffer( qimage.bits().asstring(qimage.height() * qimage.bytesPerLine()), dtype=np.uint8).reshape( (qimage.height(), qimage.bytesPerLine()))[:, :qimage.width()] # img_raw = np.array([[QtGui.qRed(qimage.pixel(x, y)) for x in range( # qimage.width())] for y in range(qimage.height())], dtype=np.uint8) fig = Figure() ax = fig.add_subplot(111) ax.imshow(img_raw, interpolation='gaussian', cmap=plt.cm.gray) ax.axis('off') self.addMpl(self.img, fig) self.statusBar().showMessage('Processing...') try: self.result1.setPlainText('') self.copy1.setText('Copy') self.copy1.setStyleSheet('') latex = self.predict.predict_img(img_raw) self.result1.setPlainText(latex) self.copyResult1() except: latex = '' del self.predict self.predict = Predict() self.statusBar().clearMessage()