def main(): """ Reads command line arguments and starts either training, evaluation or inference. """ parser = argparse.ArgumentParser() parser.add_argument( '--config', default='', help='JSON-formatted file with configuration parameters') parser.add_argument('--mode', default='', help='The operational mode - train|evaluate|predict') parser.add_argument('--input', default='', help='The path to a PLY file to run through the model') parser.add_argument('--device', default='gpu', help='The device to use - cpu|gpu') parser.add_argument('--colors', default='', help='A file containing colors') cla = parser.parse_args() if cla.config == '': print 'Error: --config flag is required. Enter the path to a configuration file.' if cla.mode == 'evaluate': inference.evaluate(cla.config, cla.device) elif cla.mode == 'predict': inference.predict(cla.config, cla.input, cla.device, cla.colors) elif cla.mode == 'train': training.train(cla.config) else: print 'Error: invalid operational mode - options: train, evaluate or predict'
def main(): flags = parse_flags() hparams = parse_hparams(flags.hparams) if flags.mode == 'train': train.train(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, train_csv_path=flags.train_csv_path, train_clip_dir=flags.train_clip_dir, train_dir=flags.train_dir) elif flags.mode == 'eval': evaluation.evaluate(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, eval_csv_path=flags.eval_csv_path, eval_clip_dir=flags.eval_clip_dir, checkpoint_path=flags.checkpoint_path) else: assert flags.mode == 'inference' inference.predict(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, test_clip_dir=flags.test_clip_dir, checkpoint_path=flags.checkpoint_path, predictions_csv_path=flags.predictions_csv_path)
def testInference(self): binary_path = os.path.join('../binaries', 'index-linux') model_path = os.path.join('../test_data', 'model.json') test_data_dir = os.path.join('../test_data') tmp_dir = tempfile.mkdtemp() inference.predict(binary_path, model_path, test_data_dir, tmp_dir) with open(os.path.join(tmp_dir, 'data.json'), 'rt') as f: ys_values = json.load(f) # The output is a list of tensor data in the form of dict. # Example output: # [{"0":0.7567615509033203,"1":-0.18349379301071167,"2":0.7567615509033203,"3":-0.18349379301071167}] ys_values = [list(y.values()) for y in ys_values] with open(os.path.join(tmp_dir, 'shape.json'), 'rt') as f: ys_shapes = json.load(f) with open(os.path.join(tmp_dir, 'dtype.json'), 'rt') as f: ys_dtypes = json.load(f) self.assertAllClose(ys_values[0], [ 0.7567615509033203, -0.18349379301071167, 0.7567615509033203, -0.18349379301071167 ]) self.assertAllEqual(ys_shapes[0], [2, 2]) self.assertEqual(ys_dtypes[0], 'float32') # Cleanup tmp dir. shutil.rmtree(tmp_dir)
def main(argv): hparams = model.parse_hparams(flags.hparams) if flags.mode == 'train': def split_csv(scopes): return scopes.split(',') if scopes else None train.train(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, train_csv_path=flags.train_csv_path, train_clip_dir=flags.train_clip_dir, train_dir=flags.train_dir, epoch_batches=flags.epoch_num_batches, warmstart_checkpoint=flags.warmstart_checkpoint, warmstart_include_scopes=split_csv(flags.warmstart_include_scopes), warmstart_exclude_scopes=split_csv(flags.warmstart_exclude_scopes)) elif flags.mode == 'eval': evaluation.evaluate(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, eval_csv_path=flags.eval_csv_path, eval_clip_dir=flags.eval_clip_dir, eval_dir=flags.eval_dir, train_dir=flags.train_dir) else: assert flags.mode == 'inference' inference.predict(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, inference_clip_dir=flags.inference_clip_dir, inference_checkpoint=flags.inference_checkpoint, predictions_csv_path=flags.predictions_csv_path)
def main(): flags = parse_flags() hparams = parse_hparams(flags.hparams) if flags.mode == 'train': utils.resample(sample_rate=flags.sample_rate, dir=flags.train_clip_dir, csv_path=flags.train_csv_path) train.train(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, train_csv_path=flags.train_csv_path, train_clip_dir=flags.train_clip_dir+'/resampled', train_dir=flags.train_dir, sample_rate=flags.sample_rate) elif flags.mode == 'eval': #TODO uncomment #utils.resample(sample_rate=flags.sample_rate, dir=flags.eval_clip_dir, csv_path=flags.eval_csv_path) evaluation.evaluate(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, eval_csv_path=flags.eval_csv_path, eval_clip_dir=flags.eval_clip_dir+'/resampled', checkpoint_path=flags.checkpoint_path) else: assert flags.mode == 'inference' utils.resample(sample_rate=flags.sample_rate, dir=flags.test_clip_dir, csv_path='test') inference.predict(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, test_clip_dir=flags.test_clip_dir, checkpoint_path=flags.checkpoint_path, predictions_csv_path=flags.predictions_csv_path)
def executor(locals_dict): # Updating all user parameters start = timer() global a a = 0 def update_config(current_path, config_main): _dict = {} global a for key, value in config_main.items(): if key in locals_dict: _dict[key] = locals_dict[key] elif (current_path + "__" + key) in locals_dict: _dict[key] = locals_dict[(current_path + "__" + key)] print(current_path + "__" + key) elif isinstance(value, dict): if a == 1: if len(current_path) > 0: _dict[key] = update_config(current_path + "__" + key, value) else: _dict[key] = update_config(current_path + key, value) else: a = 1 _dict[key] = update_config(current_path, value) a = 0 else: _dict[key] = config_main[key] return _dict current_config = update_config("", config_main) with open(f'./assets/config-{now}.yaml', 'w') as file: yaml.safe_dump(current_config, file) with open('config.yaml', 'w') as config_data: yaml.safe_dump(current_config, config_data) if current_config['limit_gpu'] is not False: limit_gpu() if current_config['mode'] == 'training': print("[INFO]...Starting Training Job") train(current_config) if current_config['mode'] == 'inference': print("[INFO]...Starting Inference Job") predict(current_config) end = timer() print( f"[INFO]...Time taken by {current_config['mode']} is {(end - start)/60:.2f} min(s)" )
def run(): """Runs dilated residual network model in either train or predict mode""" config = parse_args() if not os.path.isdir(config.logs): os.makedirs(config.logs) if config.mode == 'train': train(config) elif config.mode == 'predict': predict(config) else: ValueError("Mode must be either train or predict")
def testInferenceWithNonExistingOutputNameFile(self): binary_path = os.path.join('../binaries', 'tfjs-inference-linux') model_path = os.path.join('../test_data', 'model.json') test_data_dir = os.path.join('../test_data') tmp_dir = tempfile.mkdtemp() inference.predict(binary_path, model_path, test_data_dir, tmp_dir, tf_output_name_file='non_exist.json') with self.assertRaises(FileNotFoundError): with open(os.path.join(tmp_dir, 'data.json'), 'rt') as f: json.load(f) # Cleanup tmp dir. shutil.rmtree(tmp_dir)
def do_infer(model, data, device): if _g.args.quiet: return print("Infering {} test examples...".format(_g.args.examples)) for i in range(_g.args.examples): print("Case {}:".format(i+1)) index = random.randrange(0, len(data.examples)) sample = data.examples[index] if _g.args.debug: _i.predict_and_plot(model, sample.before, sample.after, device) else: _i.predict(model, sample.before, sample.after, device)
def fileHandler(): if flask.request.method == 'GET': return jsonify({ "reponse": "Please POST a .txt file containing submission urls at this endpoint" }) if flask.request.method == 'POST': file = request.files['file'] # print(file) urls = file.read().splitlines() correct = 0 top3_correct = 0 total = len(urls) for url in urls: text, flair = get_data(url.decode()) pred, confi = predict(text) ## Something fishy ewww if flair in pred or pred not in flairs: top3_correct += 1 if flair == pred[0]: correct += 1 # print(correct) # print(total) return jsonify({ "top-1 accuracy": '%.4f' % (correct / total), "top-3 accuracy": '%.4f' % (top3_correct / total) })
def preprocess(): WIDTH = 484 HEIGHT = 240 ENSEMBLE_N = 3 # GET COLOR ENCODING AND ITS INDEX MAPPING colors = loadmat('../data/color150.mat')['colors'] root = '..' names = {} with open('../data/object150_info.csv') as f: reader = csv.reader(f) next(reader) for row in reader: names[int(row[0])] = row[5].split(";")[0] idx_map = create_idx_group() colors, names = edit_colors_names_group(colors, names) # SETUP MODEL cfg_path = os.path.join('..', 'config', 'ade20k-mobilenetv2dilated-c1_deepsup.yaml') #cfg_path="config/ade20k-resnet18dilated-ppm_deepsup.yaml" model = setup_model(cfg_path, root, gpu=0) model.eval() # GET DATA AND PROCESS IMAGE data = np.load(os.path.join('..', 'test_set', 'cls1_rgb.npy')) data = data[:, :, ::-1] img = ImageLoad_cv2(data, WIDTH, HEIGHT, ENSEMBLE_N, True) # MODEL FEED predictions = predict(model, img, ENSEMBLE_N, gpu=0, is_silent=False) return predictions, colors, names, idx_map
def reactRequestHandler(): if flask.request.method == 'GET': return jsonify( {"reponse": "please POST a valid submission url at this endpoint"}) if flask.request.method == 'POST': data = request.get_json() url = data['url'] # print(url) # for debuggin try: text, flair = get_data(url) pred, confi = predict(text) result = [ str(cls) + " : " + str('%.4f' % confidence) for cls, confidence in zip(pred, confi) ] return jsonify({ "top_3_pred": " ".join(result), "flair": flair, "text": text }) except: return jsonify({ "response": "Oops something went wrong! Check your internet connection and/or url" })
def infer_fitness_and_frequencies(obs_reads_df, proposed_gts, out_dir, options = ''): ''' Uses obs_reads_df, which describes observed frequencies of mutations (symbols) in distinct linkage groups, and proposed_gts, a list of full-length genotypes that may occur in the population. Infers the frequency of each full-length genotype at each timepoint, and a fitness value for each full-length genotype. Saves output objects to out_dir, and returns them in a dict. ''' return inf.predict(obs_reads_df, proposed_gts, out_dir, options = options)
def test_wiki2vec(self): acc = 0 n = 0 dataloader = DataLoader(dataset=self.dataset, batch_sampler=self.batch_sampler, collate_fn=collate_wiki2vec) for batch_num, batch in enumerate(dataloader): batch = u.tensors_to_device(batch, self.device) labels_for_batch = self._get_labels_for_batch(batch['label'], batch['candidate_ids']) predictions = predict(embedding=self.embedding, token_idx_lookup=self.token_idx_lookup, p_prior=0 if self.use_adaptive_softmax else batch['p_prior'], model=self.model, batch=batch, ablation=self.ablation, entity_embeds=self.model.entity_embeds, use_wiki2vec=True) acc += int((labels_for_batch == predictions).sum()) batch_size = len(predictions) n += batch_size self.experiment.record_metrics({'accuracy': acc / n, 'TP': acc, 'num_samples': n}) return acc, n
def main_pipeline(port): """ Runs main pipeline: motion -> take picture -> classify -> action if the input on pin_motion is raising high. :param port: pin of the motion sensor. In this case pin_motion. format: int :return: """ global pap_flag logger("INFO: motion detected") if pap_flag: logger("WARNING: main_pipeline is already running") return pap_flag = 1 img = take_pic() predicted_class = inference.predict(img, model) if predicted_class == 2: logger("INFO: pigeon detected") rotate_motor(rotation_time) if predicted_class == 0: logger("INFO: human detected") time.sleep(180) if predicted_class == 1: logger("INFO: no one detected") inference.save_image(img, predicted_class) pap_flag = 0 return
def send_prediction(im): bands = len(im.getbands()) imdata = np.array(im.getdata()).reshape(im.size[0], im.size[1], bands) prediction = predict(net, colors, imdata) strio = StringIO.StringIO() prediction.save(strio, 'PNG') strio.seek(0) return send_file(strio, mimetype='image/png')
def predict_process(path): flist = get_filelist(path) flist.sort() for ts_file in flist: if not is_infile(csv_file, ts_file): with open(csv_file, mode='a') as csv_file_predict: csv_write = csv.writer(csv_file_predict, delimiter=',') csv_write.writerow( [ts_file, predict(os.path.join(path, ts_file))])
def pred(): im = Image.open(request.files['image']) bands = len(im.getbands()) imdata = np.array(im.getdata()).reshape(im.size[0], im.size[1], bands) prediction = predict(net, colors, imdata) strio = StringIO.StringIO() prediction.save(strio, 'PNG') strio.seek(0) return send_file(strio, mimetype='image/png')
def do_real_input(model, device): if _g.args.quiet: return for i in range(_g.args.examples): print("Case {}:".format(i+1)) before = list(input('Input text: ')) after = input('Target? (y/n): ') while after != "y" and after != "n": after = input('Target? (y/n): ') if after == "y": after = list(input('Target text: ')) if _g.args.debug: _i.predict_and_plot(model, before, after, device) else: _i.predict(model, before, after, device) else: _i.predict(model, before, device=device)
def __get_pred_image(self): _, hint = self.painter.get_image() self.__status_update(1) line = self.origin_line hint = Image.fromarray(hint) img = predict(line, hint, None) if not isinstance(img, Image.Image): popup_err_dialog('Inference Err', img) return img
def handle_tests(test: Test): if test.ground_truth[0] != 5: with open(f"/user_tests/{test.id}{datetime.datetime.now()}_gt.json", 'w') as f: json.dump(test.json(), f) return {"message": test.ground_truth[0]} else: pred = predict(test.text) test.result = pred[1] test.softmax = pred[2] return test
def predict_upload(): #read the image from static directory image = cv2.imread('static/uploads/prediction_image.jpg') #get the predictions stage1, stage2, stage3, final = predict(image) #write the output to the static folder cv2.imwrite('static/result/stage1.jpg', stage1) cv2.imwrite('static/result/stage2.jpg', stage2) cv2.imwrite('static/result/stage3.jpg', stage3) cv2.imwrite('static/result/final.jpg', final) return render_template('predict.html')
def home(): form = InputForm() prediction_message = Prediction_Message() if form.validate_on_submit(): prediction_message.initial, prediction_message.pred1, prediction_message.pred2, prediction_message.pred3 = \ predict(model, form.tweet.data, glove.get_glove_emb(), sequence_length=62, use_gpu=False) # if form.tweet.data == 'helloworld': # flash('Entered correct input!', 'success') # return redirect(url_for('home')) # else: # flash('Invalid Tweet!', 'danger') return render_template('home.html', title='DeepFeels', form = form, prediction_message = prediction_message)
def show_image(image_name): print("hahahah", image_name) fig = Figure() image = Image.open(UPLOAD_FOLDER + '/' + image_name) model = get_model() top_probability, top_class = predict(image, model, topk=5) with open('categories.json') as f: cat_to_name = json.load(f) fig = view_classify(image, top_probability, top_class, cat_to_name) output = io.BytesIO() FigureCanvasAgg(fig).print_png(output) return Response(output.getvalue(), mimetype="image/png")
def hello_world(): if request.method == 'GET': return render_template('index.html', value="with food") if request.method == 'POST': if 'file' not in request.files: print("file not uploaded") return file = request.files['file'] image = file.read() bboxes, labels, scores = predict(image_bytes=image) return render_template('result.html', labels=labels, scores=scores, boxes=bboxes)
def main(): flags = parse_flags() hparams = parse_hparams(flags.hparams) if flags.mode == 'train': utils.resample(sample_rate=flags.sample_rate, dir=flags.train_clip_dir, csv_path=flags.train_csv_path) train.train(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, train_csv_path=flags.train_csv_path, train_clip_dir=flags.train_clip_dir + '/resampled', train_dir=flags.train_dir, sample_rate=flags.sample_rate) elif flags.mode == 'eval': #TODO uncomment #utils.resample(sample_rate=flags.sample_rate, dir=flags.eval_clip_dir, csv_path=flags.eval_csv_path) evaluation.evaluate(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, eval_csv_path=flags.eval_csv_path, eval_clip_dir=flags.eval_clip_dir + '/resampled', checkpoint_path=flags.checkpoint_path) else: assert flags.mode == 'inference' utils.resample(sample_rate=flags.sample_rate, dir=flags.test_clip_dir, csv_path='test') inference.predict(model_name=flags.model, hparams=hparams, class_map_path=flags.class_map_path, test_clip_dir=flags.test_clip_dir, checkpoint_path=flags.checkpoint_path, predictions_csv_path=flags.predictions_csv_path)
def line_segment(image, img, output_path): """ img: image needed to segment to lines. output_path: output folder of segmented images. :return: output image pathes """ #cv2.imshow("Original", img) #cv2.waitKey() # (1) Segment Lines line_segmentation = LineSegmentation(img=img, output_path=output_path) lines = line_segmentation.segment() # (2) Save lines to file output_image_path = line_segmentation.save_lines_to_file(lines) percTot = 0 count = 0 text = "" for m in lines: pd, pbperc = predict(cv2.cvtColor(m, cv2.COLOR_GRAY2RGB)) #cv2.imshow("Line",m) #cv2.waitKey() percTot += pbperc count += 1 text += pd + '\n' if count != 0: perc = percTot / count if count == 0: pd, pbperc = predict( cv2.cvtColor(cv2.imread(image), cv2.COLOR_GRAY2RGB)) perc = pbperc text = pd head, tail = ntpath.split(image) image = tail or ntpath.basename(head) filename = os.path.splitext(image)[0] fout = open('output/' + filename + '_output.txt', 'w') fperc = open('output/' + filename + '_probability.txt', 'w') fout.write(text) fperc.write(f"{perc:.2f}")
def single_mode(): '### Single Text & Sentiment' '#### Text here:' input_text = st.text_area('', max_chars=144) '#### Sentiment:' sentiment = st.selectbox('', ('negative', 'neutral', 'positive')) if input_text: d = {'text': input_text, 'sentiment': sentiment} df = pd.DataFrame(data=d, index=[0]) pred = inference.predict(df, model=model) '### Words explain sentiment:' st.write(pred[0])
def vision(): if not request.json or not 'url' in request.json: abort(400) # get url url = request.json['url'] t0 = time.time() # vectorize image x = vectorize(url) # predict with graph.as_default(): r = predict(x, model, ['cat', 'dog']) t1 = time.time() r['elapsed'] = t1 - t0 return jsonify(r), 201
def send(): if request.method == 'POST': img_file = request.files['img_file'] if img_file and allowed_file(img_file.filename): filename = secure_filename(img_file.filename) img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) img_url = UPLOAD_FOLDER + filename # inferenceを呼び出し、Captionを生成する img_caption = inference.predict(img_url) return render_template('index.html', img_url=img_url, img_caption=img_caption) else: return ''' <p>許可されていない拡張子です</p> ''' else: return redirect(url_for('index'))
def index(): if request.method == 'POST': text = request.form['text'] # remove static directory if it exists for files in glob.glob(os.path.join("static", "*")): os.remove(files) name, img, path = predict(text) img.save(os.path.join("static", "molecule.png")) path.save(os.path.join("static", "pathway.png")) result = { 'mol_name': name, 'mol_path': os.path.join("static", "molecule.png"), 'pathway_path': os.path.join("static", "pathway.png") } return render_template('show.html', result=result) return render_template( 'index.html') # by default it will look in templates folder
def make_prediction(net, colors, im, threshold, outfile): bands = len(im.getbands()) imdata = np.array(im.getdata()).reshape(im.size[0], im.size[1], bands) predicted = predict(net, colors, threshold, imdata) predicted.save(outfile, 'PNG')
caffe.set_mode_gpu() if args.gpu is not None: caffe.set_device(args.gpu) net = caffe.Net(args.model, args.weights, caffe.TEST) pixels_correct = 0 pixels_complete = 0 pixels_predicted_fg = 0 pixels_actual_fg = 0 outputs = [] for i in range(0, len(test_data)): prediction_image = predict(net, colors) image = net.blobs['data'].data image = np.squeeze(image[0, :, :, :]) label = net.blobs['label'].data label = np.squeeze(label).astype(np.uint8) predicted = net.blobs['prob'].data predicted = np.squeeze(predicted[0, :, :, :]) metrics = complete_and_correct(predicted, label, 3, 0.5) result = {'index': i, 'metrics': metrics} print(result) outputs.append(result) pixels_correct += sum(metrics['pixels_correct']) pixels_complete += sum(metrics['pixels_complete'])