def __init__(self, yolo_model='tiny-yolo-voc'): yolo_meta = os.path.join(YOLO_MODELS_DIR, yolo_model + '.json') yolo_model = os.path.join(YOLO_MODELS_DIR, yolo_model + '.pb') yolo_args = argHandler() yolo_args.setDefaults() yolo_args['metaLoad'] = yolo_meta yolo_args['pbLoad'] = yolo_model yolo_args['gpu'] = 1.0 yolo_args['threshold'] = 0. self.yolo = TFNet(yolo_args)
def init_model(self): if (os.path.isfile("./built_graph/yolo.pb") & os.path.isfile("./built_graph/yolo.meta")): # if already saved, load from existing FLAGS = argHandler() FLAGS.setDefaults() FLAGS.pbLoad = "built_graph/yolo.pb" FLAGS.metaLoad = "built_graph/yolo.meta" self.tfnet = TFNet(FLAGS) else: self.tfnet = TFNet(self.options) self.tfnet.savepb()
def initModel(): global tfnet lst = [ "flow", "--model", "cfg/tiny-yolo-voc.cfg", "--load", "bin/tiny-yolo-voc.weights", "--threshold", "0.47", '--gpu', '0.7' ] # lst = ['--model', 'cfg/tiny-yolo-voc-1c.cfg', '--load', '125', '--demo', 'camera', '--gpu', '0.7'] flags = argHandler() flags.setDefaults() flags.parseArgs(lst) flags.demo = None try: flags.load = int(flags.load) except: pass tfnet = TFNet(flags)
def __init__(self, FLAGS, darknet=None): self.ntrain = 0 if isinstance(FLAGS, dict): from darkflow.defaults import argHandler newFLAGS = argHandler() newFLAGS.setDefaults() newFLAGS.update(FLAGS) FLAGS = newFLAGS self.FLAGS = FLAGS if self.FLAGS.pbLoad and self.FLAGS.metaLoad: self.say('\nLoading from .pb and .meta') self.graph = tf.Graph() device_name = FLAGS.gpuName \ if FLAGS.gpu > 0.0 else None with tf.device(device_name): with self.graph.as_default() as g: self.build_from_pb() return if darknet is None: darknet = Darknet(FLAGS) self.ntrain = len(darknet.layers) self.darknet = darknet args = [darknet.meta, FLAGS] self.num_layer = len(darknet.layers) #self.framework = create_framework(*args) self.framework = YOLOv2ForBusDetection(*args) self.meta = darknet.meta self.say('\nBuilding net ...') start = time.time() self.graph = tf.Graph() device_name = FLAGS.gpuName \ if FLAGS.gpu > 0.0 else None with tf.device(device_name): with self.graph.as_default() as g: self.build_forward() self.setup_meta_ops() self.say('Finished in {}s\n'.format(time.time() - start))
def __init__(self, cam_id=None, ip_cam=None, detector='yolo-voc'): if cam_id == None and ip_cam is None: raise ValueError('No Camera given') yolo_meta = os.path.join(CURR_DIR, 'detector', detector + '.json') yolo_model = os.path.join(CURR_DIR, 'detector', detector + '.pb') yolo_args = argHandler() yolo_args.setDefaults() yolo_args['metaLoad'] = yolo_meta yolo_args['pbLoad'] = yolo_model yolo_args['gpu'] = 1.0 yolo_args['threshold'] = 0. self.yolo_args = yolo_args self.cam_name = 'Trip-Wire Detection' self.cam = utils.WebCam( cam_id, self.cam_name) if cam_id is not None else utils.IPCam( ip_cam, self.cam_name) self.tw_start = (0, 0) self.tw_end = (0, 0) self.tw_slope = 0 self.tw_color = (0, 200, 0) self._create_logger()
def __init__(self, model_dir, model_name, max_batch_size, rebuild_engine=False): self.MODEL_NAME = model_name self.CWD_PATH = model_dir self.PATH_TO_MODEL = os.path.join(self.CWD_PATH, '%s.uff' % self.MODEL_NAME) self.PATH_TO_ENGINE = os.path.join(self.CWD_PATH, '%s.engine' % self.MODEL_NAME) self.PATH_TO_META = os.path.join(self.CWD_PATH, '%s.meta' % self.MODEL_NAME) with open(self.PATH_TO_META, 'r') as fp: self.meta = json.load(fp) self.FLAGS = argHandler() self.FLAGS.setDefaults() self.FLAGS.threshold = 0.1 self.yoloFramework = create_framework(self.meta, self.FLAGS) self.MAX_BATCH_SIZE = max_batch_size self.MAX_WORKSPACE_SIZE = 1 << 29 self.TRT_LOGGER = trt.Logger(trt.Logger.WARNING) self.DTYPE = trt.float16 # Model self.INPUT_NAME = 'input' self.INPUT_SHAPE = self.meta['inp_size'] self.OUTPUT_NAME = 'output' self.prepare_engine(rebuild_engine)
def build_model(self, verbose_level=0): prev_dir = os.getcwd() os.chdir(dir_path) if verbose_level == 1: print("TF_START") elif verbose_level >= 2: print("Importing tensorflow...") from darkflow.net.build import TFNet from darkflow.defaults import argHandler if verbose_level == 1: print("TF_END") elif verbose_level >= 2: print("Tensorflow imported") options = { "model": self.model, "load": self.weights, "labels": self.labels, "threshold": self.confidence_threshold, "gpu": self.gpu, } old_options_path = "built_graph/options.json" old_pb_path = "built_graph/yolo.pb" old_meta_path = "built_graph/yolo.meta" if verbose_level == 1: print("MODEL_START") elif verbose_level >= 2: print("Building model...") old = False if os.path.isfile(old_pb_path) and os.path.isfile( old_meta_path) and os.path.isfile(old_options_path): with open(old_options_path) as f: old_options = json.load(f) if old_options == options: if verbose_level >= 2: print("Loading model from identical model in storage") options = argHandler() options.gpu = old_options['gpu'] options.threshold = old_options['threshold'] options.model = old_options['model'] options.load = old_options['load'] options.pbLoad = old_pb_path options.metaLoad = old_meta_path old = True self.tfnet = TFNet(options) if not old: print("Saving model in cache") with open("built_graph/options.json", "w+") as f: json.dump(options, f) self.tfnet.savepb() if verbose_level == 1: print("MODEL_END") elif verbose_level >= 2: print("Model finished building") os.chdir(prev_dir) return self
import sys import math import time import cv2 import numpy as np from darkflow.net.build import TFNet from darkflow.defaults import argHandler from multiprocessing.pool import ThreadPool from pycocotools.coco import COCO import tensorflow as tf import masknet from time import time as timer from scipy.misc import imresize if __name__ == "__main__": FLAGS = argHandler() FLAGS.setDefaults() FLAGS.parseArgs(sys.argv) def _get_dir(dirs): for d in dirs: this = os.path.abspath(os.path.join(os.path.curdir, d)) if not os.path.exists(this): os.makedirs(this) requiredDirectories = [FLAGS.imgdir, FLAGS.binary, FLAGS.backup, os.path.join(FLAGS.imgdir, 'out')] if FLAGS.summary: requiredDirectories.append(FLAGS.summary) _get_dir(requiredDirectories)