コード例 #1
0
 def __init__(self, args, config):
     self.args = args
     self.config = config
     self.model_dir = utils.get_model_dir(config)
     self.category = utils.get_category(config)
     self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
     self.dnn = utils.parse_attr(config.get('model',
                                            'dnn'))(config, self.anchors,
                                                    len(self.category))
     self.dnn.eval()
     logging.info(
         humanize.naturalsize(
             sum(var.cpu().numpy().nbytes
                 for var in self.dnn.state_dict().values())))
     if torch.cuda.is_available():
         self.dnn.cuda()
     self.height, self.width = tuple(
         map(int,
             config.get('image', 'size').split()))
     output = self.dnn(
         torch.autograd.Variable(
             utils.ensure_device(torch.zeros(1, 3, self.height,
                                             self.width))))
     _, _, self.rows, self.cols = output.size()
     self.i, self.j = self.rows // 2, self.cols // 2
     self.output = output[:, :, self.i, self.j]
     dataset = Dataset(self.height, self.width)
     try:
         workers = self.config.getint('data', 'workers')
     except configparser.NoOptionError:
         workers = multiprocessing.cpu_count()
     self.loader = torch.utils.data.DataLoader(
         dataset, batch_size=self.args.batch_size, num_workers=workers)
コード例 #2
0
 def __init__(self, args, config):
     self.args = args
     self.config = config
     self.model_dir = utils.get_model_dir(config)
     self.cache_dir = utils.get_cache_dir(config)
     self.category = utils.get_category(config, self.cache_dir)
     self.draw_bbox = utils.visualize.DrawBBox(config, self.category)
     self.loader = self.get_loader()
     self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
     dnn = utils.parse_attr(config.get('model', 'dnn'))(config,
                                                        self.anchors,
                                                        len(self.category))
     path, self.step, self.epoch = utils.train.load_model(self.model_dir)
     checkpoint = torch.load(path,
                             map_location=lambda storage, loc: storage)
     dnn.load_state_dict(checkpoint['dnn'])
     logging.info(
         humanize.naturalsize(
             sum(var.cpu().numpy().nbytes
                 for var in dnn.state_dict().values())))
     self.inference = model.Inference(config, dnn, self.anchors)
     self.inference.eval()
     if torch.cuda.is_available():
         self.inference.cuda()
     path = self.model_dir + '.ini'
     if os.path.exists(path):
         self._config = configparser.ConfigParser()
         self._config.read(path)
     else:
         logging.warning('training config (%s) not found' % path)
     self.now = datetime.datetime.now()
     self.mapper = utils.load_functions(self.config.get('eval', 'mapper'))
コード例 #3
0
ファイル: eval.py プロジェクト: codealphago/yolo2-pytorch
 def __init__(self, args, config):
     self.args = args
     self.config = config
     self.model_dir = utils.get_model_dir(config)
     self.cache_dir = utils.get_cache_dir(config)
     self.category = utils.get_category(config, self.cache_dir)
     self.draw_bbox = utils.visualize.DrawBBox(config, self.category)
     self.loader = self.get_loader()
     self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
     self.path, self.step, self.epoch = utils.train.load_model(self.model_dir)
     state_dict = torch.load(self.path, map_location=lambda storage, loc: storage)
     dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(config, state_dict), self.anchors, len(self.category))
     dnn.load_state_dict(state_dict)
     logging.info(humanize.naturalsize(sum(var.cpu().numpy().nbytes for var in dnn.state_dict().values())))
     self.inference = model.Inference(config, dnn, self.anchors)
     self.inference.eval()
     if torch.cuda.is_available():
         self.inference.cuda()
     path = self.model_dir + '.ini'
     if os.path.exists(path):
         self._config = configparser.ConfigParser()
         self._config.read(path)
     else:
         logging.warning('training config (%s) not found' % path)
     self.now = datetime.datetime.now()
     self.mapper = dict([(inflection.underscore(name), member()) for name, member in inspect.getmembers(importlib.machinery.SourceFileLoader('', self.config.get('eval', 'mapper')).load_module()) if inspect.isclass(member)])
コード例 #4
0
def norm_bbox(data, pred, keys='yx_min, yx_max'):
    size, image = (data[key] for key in 'size, image'.split(', '))
    _size = size.float()
    height, width = image.size()[1:3]
    scale = _size.view(-1, 1, 2) / utils.ensure_device(
        torch.from_numpy(
            np.reshape(np.array([height, width], dtype=np.float32),
                       [1, 1, 2])))
    for key in keys.split(', '):
        data[key] = data[key] * scale
    rows, cols = pred['feature'].size()[-2:]
    scale = _size.view(-1, 1, 1, 2) / utils.ensure_device(
        torch.from_numpy(
            np.reshape(np.array([rows, cols], dtype=np.float32),
                       [1, 1, 1, 2])))
    for key in keys.split(', '):
        pred[key] = pred[key] * scale
コード例 #5
0
def norm_bbox_pred(pred, keys='yx_min, yx_max'.split(', ')):
    rows, cols = pred['feature'].size()[-2:]
    scale = utils.ensure_device(
        torch.from_numpy(
            np.reshape(np.array([rows, cols], dtype=np.float32),
                       [1, 1, 1, 2])))
    for key in keys:
        pred[key] = pred[key] / scale
    return keys
コード例 #6
0
def norm_bbox_data(data, keys='yx_min, yx_max'.split(', ')):
    height, width = data['image'].size()[1:3]
    scale = utils.ensure_device(
        torch.from_numpy(
            np.reshape(np.array([height, width], dtype=np.float32),
                       [1, 1, 2])))
    for key in keys:
        data[key] = data[key] / scale
    return keys
コード例 #7
0
 def __init__(self, args, config):
     self.args = args
     self.config = config
     self.model_dir = utils.get_model_dir(config)
     self.cache_dir = utils.get_cache_dir(config)
     self.category = utils.get_category(config, self.cache_dir)
     self.draw_bbox = utils.visualize.DrawBBox(self.category)
     self.loader = self.get_loader()
     self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
     self.path, self.step, self.epoch = utils.train.load_model(
         self.model_dir)
     state_dict = torch.load(self.path,
                             map_location=lambda storage, loc: storage)
     dnn = utils.parse_attr(config.get('model', 'dnn'))(
         model.ConfigChannels(config, state_dict), self.anchors,
         len(self.category))
     dnn.load_state_dict(state_dict)
     logging.info(
         humanize.naturalsize(
             sum(var.cpu().numpy().nbytes
                 for var in dnn.state_dict().values())))
     self.inference = model.Inference(config, dnn, self.anchors)
     self.inference.eval()
     if torch.cuda.is_available():
         self.inference.cuda()
     path = self.model_dir + '.ini'
     if os.path.exists(path):
         self._config = configparser.ConfigParser()
         self._config.read(path)
     else:
         logging.warning('training config (%s) not found' % path)
     self.now = datetime.datetime.now()
     self.mapper = dict([
         (inflection.underscore(name), member())
         for name, member in inspect.getmembers(
             importlib.machinery.SourceFileLoader(
                 '', self.config.get('eval', 'mapper')).load_module())
         if inspect.isclass(member)
     ])
コード例 #8
0
 def __init__(self, args, config):
     self.args = args
     self.config = config
     self.model_dir = utils.get_model_dir(config)
     self.category = utils.get_category(config)
     self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
     self.dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(config), self.anchors, len(self.category))
     self.dnn.eval()
     logging.info(humanize.naturalsize(sum(var.cpu().numpy().nbytes for var in self.dnn.state_dict().values())))
     if torch.cuda.is_available():
         self.dnn.cuda()
     self.height, self.width = tuple(map(int, config.get('image', 'size').split()))
     output = self.dnn(torch.autograd.Variable(utils.ensure_device(torch.zeros(1, 3, self.height, self.width)), volatile=True))
     _, _, self.rows, self.cols = output.size()
     self.i, self.j = self.rows // 2, self.cols // 2
     self.output = output[:, :, self.i, self.j]
     dataset = Dataset(self.height, self.width)
     try:
         workers = self.config.getint('data', 'workers')
     except configparser.NoOptionError:
         workers = multiprocessing.cpu_count()
     self.loader = torch.utils.data.DataLoader(dataset, batch_size=self.args.batch_size, num_workers=workers)
コード例 #9
0
ファイル: eval.py プロジェクト: codealphago/yolo2-pytorch
def norm_bbox_pred(pred, keys='yx_min, yx_max'.split(', ')):
    rows, cols = pred['feature'].size()[-2:]
    scale = utils.ensure_device(torch.from_numpy(np.reshape(np.array([rows, cols], dtype=np.float32), [1, 1, 1, 2])))
    for key in keys:
        pred[key] = pred[key] / scale
    return keys
コード例 #10
0
ファイル: eval.py プロジェクト: codealphago/yolo2-pytorch
def norm_bbox_data(data, keys='yx_min, yx_max'.split(', ')):
    height, width = data['image'].size()[1:3]
    scale = utils.ensure_device(torch.from_numpy(np.reshape(np.array([height, width], dtype=np.float32), [1, 1, 2])))
    for key in keys:
        data[key] = data[key] / scale
    return keys