Exemple #1
0
    def __init__(self, cfg, cls_num, quad=False):
        super(DarkNet, self).__init__()
        
        self.module_defs = parse_config(cfg)
        # self.module_defs[0]['height'] = img_size
        self.module_list = create_model(self.module_defs, cls_num=cls_num, quad=quad)

        self.loss_names = ['loss', 'x', 'y', 'w', 'h', 'conf', 'cls' ]
 def __init__(self, cfg_file):
     cfg = parse_config(cfg_file)
     self.data_cfg = cfg['data_config']
     self.train_cfg = cfg['train_config']
     self.model_cfg = cfg['model_config']
     self.infer_cfg = cfg['infer_config']
     self.col_channels = 3  # assume RGB channels only
     self.frozen_model_file = os.path.join(self.infer_cfg.model_dir,
                                           self.infer_cfg.frozen_model)
     self.img_h, self.img_w = self.infer_cfg.network_input_shape
     self.labels = self.get_labels()
     self.anchors = self.generate_anchors()
     if self.infer_cfg.input_type != 'http':
         self.network_tensors = self.network_forward_pass()
Exemple #3
0
 def __init__(self, cfg_file):
     # Define model parameters
     cfg = parse_config(cfg_file)
     self.data_cfg = cfg['data_config']
     self.train_cfg = cfg['train_config']
     self.model_cfg = cfg['model_config']
     self.infer_cfg = cfg['infer_config']
     self.hparams = tf.contrib.training.HParams(
         **self.model_cfg.__dict__,
         num_classes=len(self.data_cfg.category) + 1)
     self.gpu_device = '/gpu:0'
     self.cpu_device = '/cpu:0'
     self.param_server_device = '/gpu:0'
     self.labels = None
Exemple #4
0
 def __init__(self, cfg_file):
     # Define model parameters
     cfg = parse_config(cfg_file)
     self.data_cfg = cfg['data_config']
     self.train_cfg = cfg['train_config']
     self.model_cfg = cfg['model_config']
     self.infer_cfg = cfg['infer_config']
     self.labels = self.get_labels()
     self.data_reader = ObjectDataReader(self.data_cfg)
     self.hparams = tf.contrib.training.HParams(**self.model_cfg.__dict__,
                                                num_classes=len(
                                                    self.labels))
     self.gpu_device = '/gpu:0'
     self.cpu_device = '/cpu:0'
     self.param_server_device = '/gpu:0'
Exemple #5
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    configs = parse_config(args.config_file)

    # load and convert model
    model = models.load_model(args.model_in,
                              convert=True,
                              backbone=args.backbone,
                              nms=args.nms,
                              anchor_config=configs['Anchors'])

    # save model
    model.save(args.model_out)
Exemple #6
0
 def __init__(self, cfg_file):
     cfg = parse_config(cfg_file)
     self.data_cfg = cfg['data_config']
     self.train_cfg = cfg['train_config']
     self.model_cfg = cfg['model_config']
     self.infer_cfg = cfg['infer_config']
     self.col_channels = 3  # assume RGB channels only
     self.patch_h, self.patch_w = self.infer_cfg.network_input_shape
     self.out_stride = self.infer_cfg.out_stride
     self.frozen_model_file = os.path.join(self.infer_cfg.model_dir,
                                           self.infer_cfg.frozen_model)
     self.img_h, self.img_w = self.infer_cfg.resize_shape
     self.strides_rows, self.strides_cols = self.infer_cfg.strides
     self.n_rows = int(np.ceil(self.img_h / self.patch_h))
     self.n_cols = int(np.ceil(self.img_w / self.patch_w))
     self.anchors = self.generate_anchors()
     with tf.device('/cpu:0'):
         self.preprocess_tensors = self.preprocess_graph()
     with tf.device('/gpu:0'):
         self.network_tensors = self.network_forward_pass()
     with tf.device('/cpu:0'):
         self.postprocess_tensors = self.postprocess_graph()
Exemple #7
0
def main(config_file=None):
    cwd = os.getcwd()

    # parse configuration file
    if config_file is None:
        config_file = sys.argv[-1]
        config_file = os.path.join(cwd, config_file)
    config_file_name = config_file.split('/')[-1]
    configs = parse_config(config_file)

    # save config file
    if configs['Train']['save_configs']:
        config_save_path = 'logs/' + configs['Name']
        config_save_path = os.path.join(cwd, config_save_path)
        if not os.path.exists(config_save_path):
            os.mkdir(os.path.join(cwd, config_save_path))
        config_dst_name = configs['Name'] + '.json'
        config_file_dst = os.path.join(config_save_path, config_dst_name)
        shutil.copy(config_file, config_file_dst)

    # make sure keras is the minimum required version
    check_keras_version()

    # optionally choose specific GPU
    if configs['Train']['gpu']:
        os.environ['CUDA_VISIBLE_DEVICES'] = configs['Train']['gpu']
    keras.backend.tensorflow_backend.set_session(get_session())

    # create the generators
    train_generator, validation_generator = create_generators(configs)

    # create the model
    if configs['Train']['load_snapshot'] is not None:
        print('Loading model, this may take a second...')
        model = models.load_model(configs['Train']['load_snapshot'],
                                  backbone=configs['Train']['backbone'],
                                  anchor_config=configs['Anchors'])
        # training_model = prediction_model = model
        training_model = model
        prediction_model = models.load_model(
            configs['Train']['load_snapshot'],
            backbone=configs['Train']['backbone'],
            convert=True,
            anchor_config=configs['Anchors'])

    else:
        weights = configs['Train']['weights']
        # default to imagenet if nothing else is specified
        if weights is None and configs['Train']['imagenet_weights']:
            weights = models.download_imagenet(configs['Train']['backbone'])

        print('Creating model, this may take a second...')
        backbone = configs['Train']['backbone']
        num_classes = train_generator.num_classes()
        multi_gpu = configs['Train']['multi_gpu']
        freeze_backbone = configs['Train']['freeze_backbone']

        modifier = freeze_model if freeze_backbone else None

        # Keras recommends initialising a multi-gpu model on the CPU to ease weight sharing, and to prevent OOM errors.
        # optionally wrap in a parallel model
        if multi_gpu > 1:
            with tf.device('/cpu:0'):
                retinanet = models.retinanet_backbone(
                    configs['Train']['backbone'])(num_classes,
                                                  backbone=backbone,
                                                  modifier=modifier)
                model = model_with_weights(retinanet,
                                           weights=weights,
                                           skip_mismatch=True)
            training_model = multi_gpu_model(model, gpus=multi_gpu)
        else:
            retinanet = models.retinanet_backbone(
                configs['Train']['backbone'])(num_classes,
                                              backbone=backbone,
                                              modifier=modifier)
            training_model = model = model_with_weights(retinanet,
                                                        weights=weights,
                                                        skip_mismatch=True)

        # make prediction model
        prediction_model = retinanet_bbox(model=model,
                                          anchor_param=configs['Anchors'])

        # compile model
        subnet_loss = {
            'regression':
            losses.smooth_l1(),
            'classification':
            losses.focal(configs['Train']['focal_alpha'],
                         configs['Train']['focal_gamma'])
        }

        # run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
        # run_metadata = tf.RunMetadata()

        if configs['Train']['lr_multiplier_layer']:
            optimizer = AdamWithLRMult(
                lr=configs['Train']['init_lr'],
                lr_multipliers=configs['Train']['lr_multiplier_layer'],
                debug_verbose=False,
                clipnorm=0.001)
        else:
            optimizer = keras.optimizers.adam(configs['Train']['init_lr'],
                                              clipnorm=0.001)

        training_model.compile(loss=subnet_loss, optimizer=optimizer)

    # this lets the generator compute backbone layer shapes using the actual backbone model
    if 'vgg' in configs['Train']['backbone'] or 'densenet' in configs['Train'][
            'backbone']:
        compute_anchor_targets = functools.partial(
            anchor_targets_bbox, shapes_callback=make_shapes_callback(model))
        train_generator.compute_anchor_targets = compute_anchor_targets
        if validation_generator is not None:
            validation_generator.compute_anchor_targets = compute_anchor_targets

    # create the callbacks
    callbacks = create_callbacks(
        model,
        training_model,
        prediction_model,
        validation_generator,
        configs,
    )

    # start training
    training_model.fit_generator(
        train_generator,
        validation_data=validation_generator,
        validation_steps=39,
        steps_per_epoch=configs['Train']['steps'],
        epochs=configs['Train']['epochs'],
        verbose=1,
        callbacks=callbacks,
    )
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("--camera_id", type=int, default=0)  # 调用摄像头
    parser.add_argument("--config_detection",
                        type=str,
                        default="./configs/yolov3.yml")
    parser.add_argument("--config_deepsort",
                        type=str,
                        default="./configs/deepsort.yml")
    parser.add_argument("--frame_interval", type=int, default=1)  # 输出视频帧间隔
    parser.add_argument("--show_window", dest="display",
                        default=True)  # 是否视频控制台显示
    parser.add_argument("--show_width", type=int, default=800)  # 输出视频宽度
    parser.add_argument("--show_height", type=int, default=600)  # 输出视频高度
    parser.add_argument("--output_path",
                        type=str,
                        default="./results/result.avi")  # 输出视频保存路径
    parser.add_argument("--use_cuda", action="store_true",
                        default=True)  # 是否使用GPU
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_arguments()
    cfg = parse_config()
    cfg.merge_from_file(args.config_detection)  # 获取检测配置文件
    cfg.merge_from_file(args.config_deepsort)  # 获取deepsort算法配置文件

    with VideoTracker(cfg, args) as vdo_trk:
        vdo_trk.run()
Exemple #9
0
def get_config():
    return parse_config()