Пример #1
0
def _main(args):
    # Parse input arguments
    model_path = os.path.expanduser(args.model_path)
    data_path = os.path.expanduser(args.data_path)
    anchors_path = os.path.expanduser(args.anchors_path)
    classes_path = os.path.expanduser(args.classes_path)
    output_path = os.path.expanduser(args.output_path)

    batch = args.batch

    assert model_path.endswith('.h5'), 'model_path must have .h5 extension'
    assert output_path.endswith('.mat'), 'output_path must have .mat extension'

    # Extract anchors and classes from input files
    anchors = utils.get_anchors(anchors_path)
    classes = utils.get_classes(classes_path)
    images, boxes = utils.get_data(data_path)

    # Create model and load weights from file
    model_body, model = create_model(images.shape[1:-1], int(boxes.shape[-1]), anchors, classes)
    model_body.load_weights(model_path)
    model.summary()

    # Pass input data through the network in batches
    output = model_body.predict(images[0:batch, :, :, :, :])
    for i in range(batch, images.shape[0], batch):
        output = np.concatenate((output, model_body.predict(images[i:i + batch, :, :, :, :])))

    # Save output file
    if output_path != '':
        os.makedirs(os.path.dirname(output_path), exist_ok=True)
    scipy.io.savemat(output_path, mdict={'output': output})
    print('Results saved to file: {}'.format(output_path))
Пример #2
0
def load():
    """
    加载模型,回传模型和模型参数
    """

    # 模型参数
    class_names = get_classes(classes_path)
    num_classes = len(class_names)

    anchors = get_anchors(anchors_path)
    num_anchors = len(anchors)

    input_shape = (416, 416)

    train_weights_path = log_dir + '/ep169-loss17.356-val_loss6.844.h5'

    # 获取模型结构,加载权重
    image_input = Input(shape=(None, None, 3))

    model = yolo_body(image_input, num_anchors//3, num_classes)
    print('Get YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))

    model.load_weights(train_weights_path, by_name=True, skip_mismatch=True)
    print('Load weights {}.'.format(train_weights_path))

    # 回传参数
    param = [class_names, num_classes, anchors, input_shape]

    return model, param
Пример #3
0
def _main(args):
    os.chdir(os.path.join(os.getcwd(), 'yad2k-em3d'))
    # Parse input arguments
    anchors_path = os.path.expanduser(args.anchors_path)
    classes_path = os.path.expanduser(args.classes_path)
    data_path = os.path.expanduser(args.data_path)
    test_results = os.path.expanduser(args.test_results)
    # Extract anchors, classes, images, and boxes from input files
    anchors = utils.get_anchors(anchors_path)
    classes = utils.get_classes(classes_path)
    images, boxes = utils.get_data(data_path)
    test_results = scipy.io.loadmat(test_results)
    t = test_results['output']
    cv2.imshow("TESTING", images[20])
    cv2.waitKey(0)
    ipdb.set_trace()
    classes = [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20, 21, 22, 23, 24, 25
    ]
    class_names = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    ]
    drawn = utils.draw_boxes(images[0],
                             t[:1],
                             classes,
                             class_names,
                             scores=t[2])
    cv2.imshow('drawm', drawn)
    cv2.waitKey(0)
Пример #4
0
def main():
    args = make_args()
    config = configparser.ConfigParser()
    utils.load_config(config, args.config)
    for cmd in args.modify:
        utils.modify_config(config, cmd)
    with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f:
        logging.config.dictConfig(yaml.load(f))
    cache_dir = utils.get_cache_dir(config)
    model_dir = utils.get_model_dir(config)
    category = utils.get_category(
        config, cache_dir if os.path.exists(cache_dir) else None)
    anchors = utils.get_anchors(config)
    anchors = torch.from_numpy(anchors).contiguous()
    path, step, epoch = utils.train.load_model(model_dir)
    state_dict = torch.load(path, map_location=lambda storage, loc: storage)
    dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(
        config, state_dict), anchors, len(category))
    dnn.load_state_dict(state_dict)
    height, width = tuple(map(int, config.get('image', 'size').split()))
    resize = transform.parse_transform(config,
                                       config.get('transform', 'resize_test'))
    transform_image = transform.get_transform(
        config,
        config.get('transform', 'image_test').split())
    transform_tensor = transform.get_transform(
        config,
        config.get('transform', 'tensor').split())
    # load image
    image_bgr = cv2.imread('image.jpg')
    image_resized = resize(image_bgr, height, width)
    image = transform_image(image_resized)
    tensor = transform_tensor(image).unsqueeze(0)
    # Checksum
    for key, var in dnn.state_dict().items():
        a = var.cpu().numpy()
        print('\t'.join(
            map(str, [
                key, a.shape,
                utils.abs_mean(a),
                hashlib.md5(a.tostring()).hexdigest()
            ])))
    output = dnn(torch.autograd.Variable(tensor, volatile=True)).data
    for key, a in [
        ('image_bgr', image_bgr),
        ('image_resized', image_resized),
        ('tensor', tensor.cpu().numpy()),
        ('output', output.cpu().numpy()),
    ]:
        print('\t'.join(
            map(str, [
                key, a.shape,
                utils.abs_mean(a),
                hashlib.md5(a.tostring()).hexdigest()
            ])))
Пример #5
0
def generate_ngram_candidates(document, ontology):
    """
    Generate keyword candidates by building an ontology trie and performing
    a fuzzy match over it with ngrams extracted from the text.
    :param document - Document object
    :param ontology - Ontology object

    :return set of KeywordTokens
    """
    all_words = document.get_meaningful_words()
    tokens = set()

    # 1-grams
    anchors = get_anchors(all_words, ontology)
    tokens |= set(anchors)

    # 2-grams
    n = 2
    ngram_tokens = dict()
    for position, ngram in get_ngrams_around_anchors(n, all_words, anchors):
        # TODO potential filtering of ngrams e.g. for linguistic purposes
        form = " ".join(ngram)
        for hit in ontology.fuzzy_match(form):
            uri = ontology.get_uri_from_label(hit)
            add_token(uri, ngram_tokens, position, ontology, form=form)
    tokens |= set(ngram_tokens.values())

    # 3-grams
    n = 3
    ngram_tokens = dict()
    for position, ngram in get_ngrams_around_anchors(n, all_words, anchors):
        # TODO potential filtering of ngrams e.g. for linguistic purposes
        form = " ".join(ngram)
        for hit in ontology.fuzzy_match(form):
            uri = ontology.get_uri_from_label(hit)
            add_token(uri, ngram_tokens, position, ontology, form=form)
    tokens |= set(ngram_tokens.values())

    # 4-grams
    # n = 4
    # ngram_tokens = dict()
    # for position, ngram in get_ngrams_around_anchors(n, all_words, anchors):
    #     # TODO potential filtering of ngrams e.g. for linguistic purposes
    #     form = " ".join(ngram)
    #     for hit in ontology.fuzzy_match(form):
    #         uri = ontology.get_uri_from_label(hit)
    #         add_token(uri, ngram_tokens, position, ontology, form=form)
    # tokens |= set(ngram_tokens.values())

    tokens = remove_not_considered_keywords(tokens)

    return remove_nostandalone_candidates(tokens, ontology)
Пример #6
0
def generate_ngram_candidates(document, ontology):
    """
    Generate keyword candidates by building an ontology trie and performing
    a fuzzy match over it with ngrams extracted from the text.
    :param document - Document object
    :param ontology - Ontology object

    :return set of KeywordTokens
    """
    all_words = document.get_meaningful_words()
    tokens = set()

    # 1-grams
    anchors = get_anchors(all_words, ontology)
    tokens |= set(anchors)

    # 2-grams
    n = 2
    ngram_tokens = dict()
    for position, ngram in get_ngrams_around_anchors(n, all_words, anchors):
        # TODO potential filtering of ngrams e.g. for linguistic purposes
        form = " ".join(ngram)
        for hit in ontology.fuzzy_match(form):
            uri = ontology.get_uri_from_label(hit)
            add_token(uri, ngram_tokens, position, ontology, form=form)
    tokens |= set(ngram_tokens.values())

    # 3-grams
    n = 3
    ngram_tokens = dict()
    for position, ngram in get_ngrams_around_anchors(n, all_words, anchors):
        # TODO potential filtering of ngrams e.g. for linguistic purposes
        form = " ".join(ngram)
        for hit in ontology.fuzzy_match(form):
            uri = ontology.get_uri_from_label(hit)
            add_token(uri, ngram_tokens, position, ontology, form=form)
    tokens |= set(ngram_tokens.values())

    # 4-grams
    # n = 4
    # ngram_tokens = dict()
    # for position, ngram in get_ngrams_around_anchors(n, all_words, anchors):
    #     # TODO potential filtering of ngrams e.g. for linguistic purposes
    #     form = " ".join(ngram)
    #     for hit in ontology.fuzzy_match(form):
    #         uri = ontology.get_uri_from_label(hit)
    #         add_token(uri, ngram_tokens, position, ontology, form=form)
    # tokens |= set(ngram_tokens.values())

    tokens = remove_not_considered_keywords(tokens)

    return remove_nostandalone_candidates(tokens, ontology)
def main():
    args = make_args()
    config = configparser.ConfigParser()
    utils.load_config(config, args.config)
    for cmd in args.modify:
        utils.modify_config(config, cmd)
    with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f:
        logging.config.dictConfig(yaml.load(f))
    model_dir = utils.get_model_dir(config)
    category = utils.get_category(config)
    anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
    path, step, epoch = utils.train.load_model(model_dir)
    state_dict = torch.load(path, map_location=lambda storage, loc: storage)
    _model = utils.parse_attr(config.get('model', 'dnn'))
    dnn = _model(model.ConfigChannels(config, state_dict), anchors,
                 len(category))
    logging.info(
        humanize.naturalsize(
            sum(var.cpu().numpy().nbytes
                for var in dnn.state_dict().values())))
    dnn.load_state_dict(state_dict)
    height, width = tuple(map(int, config.get('image', 'size').split()))
    image = torch.autograd.Variable(
        torch.randn(args.batch_size, 3, height, width))
    output = dnn(image)
    state_dict = dnn.state_dict()
    d = utils.dense(state_dict[args.name])
    keep = torch.LongTensor(np.argsort(d)[:int(len(d) * args.keep)])
    modifier = utils.channel.Modifier(
        args.name,
        state_dict,
        dnn,
        lambda name, var: var[keep],
        lambda name, var, mapper: var[mapper(keep, len(d))],
        debug=args.debug,
    )
    modifier(output.grad_fn)
    if args.debug:
        path = modifier.dot.view(
            '%s.%s.gv' % (os.path.basename(model_dir),
                          os.path.basename(os.path.splitext(__file__)[0])),
            os.path.dirname(model_dir))
        logging.info(path)
    assert len(keep) == len(state_dict[args.name])
    dnn = _model(model.ConfigChannels(config, state_dict), anchors,
                 len(category))
    dnn.load_state_dict(state_dict)
    dnn(image)
    if not args.debug:
        torch.save(state_dict, path)
Пример #8
0
def _main(args):
    # Parse input arguments
    anchors_path = os.path.expanduser(args.anchors_path)
    classes_path = os.path.expanduser(args.classes_path)
    data_path = os.path.expanduser(args.data_path)
    output_path = os.path.expanduser(args.output_path)

    # Extract anchors, classes, images, and boxes from input files
    anchors = utils.get_anchors(anchors_path)
    classes = utils.get_classes(classes_path)
    images, boxes = utils.get_data(data_path)

    # Train YOLO model
    model_body, model = create_model(images.shape[1:-1], int(boxes.shape[-1]),
                                     anchors, classes)
    model.summary()
    train(model, classes, anchors, images, boxes, output_path=output_path)
Пример #9
0
def _main(args):
    # Parse input arguments
    anchors_path = os.path.expanduser(args.anchors_path)
    classes_path = os.path.expanduser(args.classes_path)
    data_path = os.path.expanduser(args.data_path)
    output_path = os.path.expanduser(args.output_path)
    # Extract anchors, classes, images, and boxes from input files
    anchors = utils.get_anchors(anchors_path)
    classes = utils.get_classes(classes_path)
    images, boxes = utils.get_data(data_path)
    # if you need to ensure the data being fed to the algorithm is correct, uncomment
    utils.check_data(images, boxes)

    # Train YOLO model
    model_body, model = create_model(images.shape[1:-1], int(boxes.shape[-1]),
                                     anchors, classes)
    model.summary()
    train(model, classes, anchors, images, boxes, output_path=output_path)
Пример #10
0
def get_yolov4(image):
    # inintial config
    ANCHORS = utils.get_anchors('./data/yolov4_anchors.txt', False)
    NUM_CLASS = len(utils.read_class_names('data/coco.names'))
    XYSCALE = [1.2, 1.1, 1.05]
    STRIDES = np.array([8, 16, 32])
    input_size = 608

    original_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    original_image_size = original_image.shape[:2]

    image_data = image_preporcess(np.copy(original_image),
                                  [input_size, input_size])
    image_data = image_data[np.newaxis, ...].astype(np.float32)
    # send grpc request
    request.inputs['input_1'].CopyFrom(
        tf.make_tensor_proto(image_data, dtype=types_pb2.DT_FLOAT))

    result_final = []
    result_future = stub.Predict(request, 10)
    result_1 = result_future.outputs['tf_op_layer_concat_10']
    result_final.append(np.reshape(np.array(result_1.ListFields()[2][1]), \
                                   (1, 76, 76, 3, 6)
                                   ))
    result_2 = result_future.outputs['tf_op_layer_concat_11']
    result_final.append(np.reshape(np.array(result_2.ListFields()[2][1]), \
                                   (1, 38, 38, 3, 6)
                                   ))
    result_3 = result_future.outputs['tf_op_layer_concat_12']
    result_final.append(np.reshape(np.array(result_3.ListFields()[2][1]), \
                                   (1, 19, 19, 3, 6)
                                   ))

    pred_bbox = utils.postprocess_bbbox(result_final, ANCHORS, STRIDES,
                                        XYSCALE)
    bboxes = utils.postprocess_boxes(pred_bbox, original_image_size,
                                     input_size, 0.45)
    bboxes = utils.nms(bboxes, 0.213, method='nms')

    #image = utils.draw_bbox(original_image, bboxes)
    #image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
    #cv2.imwrite('result.jpg', image)

    return bboxes
Пример #11
0
    def __init__(self,dataset_type):
        self.annot_path = cfg['train_annot_path'] if  dataset_type == 'train' else cfg['test_annot_path']
        self.input_size = cfg['train_input_size'] if dataset_type == 'train' else cfg['test_input_size']
        self.batch_size = cfg['train_batch_size'] if dataset_type == 'train' else cfg['test_batch_size']
        self.data_aug = cfg['train_data_augement'] if dataset_type == 'train' else cfg['test_data_augement']

        self.train_input_sizes = cfg['train_input_size']
        self.train_input_size = random.choice(self.train_input_sizes)
        self.strides = np.array(cfg['yolo_strides'])
        self.classes = utils.read_class_names(cfg['yolo_classes_names'])
        self.num_classes = len(self.classes)
        self.anchors = np.array(utils.get_anchors(cfg['yolo_anchors']))
        self.anchor_per_scale = cfg['yolo_anchor_per_scale']
        self.max_bbox_per_scale = 150

        self.annotations = self.load_annotation(dataset_type)
        self.num_samples = len(self.annotations)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Пример #12
0
    def __init__(self, opt, datatype="train"):
        self.label_path = utils.read_label_path(opt.data, datatype)
        self.image_path = utils.read_image_path(opt.data, datatype)
        self.classes = utils.read_class_names(opt.data)
        self.input_size = opt.img_size
        self.strides = np.array([8, 16, 32])
        self.output_sizes = self.input_size // self.strides
        self.batch_size = opt.batch_size
        self.num_classes = len(self.classes)

        self.max_bbox_per_scale = 150
        # 读取anchors
        self.anchors = np.array(utils.get_anchors(opt.data))
        self.anchor_per_scale = 3

        self.num_samples = len(self.label_path)
        # np.ceil起到向上取整的作用
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Пример #13
0
    def __init__(self, dataset_type):
        self.annot_path = cfg.TRAIN.ANNOT_PATH if dataset_type == 'train' else cfg.TRAIN.ANNOT_PATH
        self.input_sizes = cfg.TRAIN.INPUT_SIZE if dataset_type == 'train' else cfg.TEST.INPUT_SIZE
        self.batch_size = cfg.TRAIN.BATCH_SIZE if dataset_type == 'train' else cfg.TEST.BATCH_SIZE
        self.data_aug = cfg.TRAIN.DATA_AUG if dataset_type == 'train' else cfg.TEST.DATA_AUG

        self.train_input_sizes = cfg.TRAIN.INPUT_SIZE
        self.strides = np.array(cfg.YOLO.STRIDES)
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.num_classes = len(self.classes)
        self.anchors = np.array(utils.get_anchors())
        self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE
        self.max_bbox_per_scale = 150
        self.img_id_list = self.load_car_person_list('train') if dataset_type == 'train' \
            else self.load_car_person_list('test')
        # print("self.img_id_list: ", self.img_id_list)
        self.train_input_size = 416

        self.annotations = self.load_annotations()
        self.num_samples = len(self.img_id_list)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Пример #14
0
    def __init__(self, split='train'):
        self.config = load_config()

        self.data_path = '/home/aaron/tree_pcl_data/cropped'

        all_files = os.listdir(self.data_path)
        cutoff = int(len(all_files) * 0.8)
        if split == 'train':
            self.file_list = all_files[:cutoff]
        elif split == 'test':
            self.file_list = all_files[cutoff:]
        else:
            raise ValueError('split must be in [train, test]')

        # Calculate the size of the voxel grid in every dimensions
        self.voxel_W, self.voxel_H, self.voxel_D = get_num_voxels()

        self.anchors = get_anchors().reshape(-1, 7)

        # Because of the network architecure (deconvs), output feature map
        # shape is half that of the input
        self.feature_map_shape = (self.voxel_H // 2, self.voxel_W // 2)
Пример #15
0
    def __init__(self, split='train'):
        self.config = load_config()

        self.data_path = os.path.join(kitti_data_dir, '%sing' % split)
        self.lidar_path = os.path.join(self.data_path, 'cropped/')
        self.image_path = os.path.join(self.data_path, 'image_2/')
        self.calib_path = os.path.join(self.data_path, 'calib/')
        self.label_path = os.path.join(self.data_path, 'label_2/')

        # Open the split file, containing numbers of the examples in this split
        # with open(os.path.join(self.data_path, '%s.txt' % split)) as f:
        with open(os.path.join(self.data_path, 'test_net.txt')) as f:
            self.file_list = f.read().splitlines()

        # Calculate the size of the voxel grid in every dimensions
        self.voxel_W, self.voxel_H, self.voxel_D = get_num_voxels()

        self.anchors = get_anchors().reshape(-1, 7)

        # Because of the network architecure (deconvs), output feature map
        # shape is half that of the input
        self.feature_map_shape = (self.voxel_H // 2, self.voxel_W // 2)
def main():
    args = make_args()
    config = configparser.ConfigParser()
    utils.load_config(config, args.config)
    for cmd in args.modify:
        utils.modify_config(config, cmd)
    with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f:
        logging.config.dictConfig(yaml.load(f))
    model_dir = utils.get_model_dir(config)
    category = utils.get_category(config)
    anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
    try:
        path, step, epoch = utils.train.load_model(model_dir)
        state_dict = torch.load(path,
                                map_location=lambda storage, loc: storage)
    except (FileNotFoundError, ValueError):
        logging.warning('model cannot be loaded')
        state_dict = None
    dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(
        config, state_dict), anchors, len(category))
    logging.info(
        humanize.naturalsize(
            sum(var.cpu().numpy().nbytes
                for var in dnn.state_dict().values())))
    if state_dict is not None:
        dnn.load_state_dict(state_dict)
    height, width = tuple(map(int, config.get('image', 'size').split()))
    image = torch.autograd.Variable(
        torch.randn(args.batch_size, 3, height, width))
    output = dnn(image)
    state_dict = dnn.state_dict()
    graph = utils.visualize.Graph(config, state_dict)
    graph(output.grad_fn)
    diff = [key for key in state_dict if key not in graph.drawn]
    if diff:
        logging.warning('variables not shown: ' + str(diff))
    path = graph.dot.view(
        os.path.basename(model_dir) + '.gv', os.path.dirname(model_dir))
    logging.info(path)
Пример #17
0
def _main(args):
    # Parse input arguments
    model_path = os.path.expanduser(args.model_path)
    data_path = os.path.expanduser(args.data_path)
    anchors_path = os.path.expanduser(args.anchors_path)
    classes_path = os.path.expanduser(args.classes_path)
    output_path = os.path.expanduser(args.output_path)

    batch = args.batch

    assert model_path.endswith('.h5'), 'model_path must have .h5 extension'
    assert output_path.endswith('.mat'), 'output_path must have .mat extension'

    # Extract anchors and classes from input files
    anchors = utils.get_anchors(anchors_path)
    classes = utils.get_classes(classes_path)
    images, boxes = utils.get_data(data_path)

    # Create model and load weights from file
    model_body, model = create_model(images.shape[1:-1], int(boxes.shape[-1]),
                                     anchors, classes)
    model_body.load_weights(model_path)
    model.summary()
Пример #18
0
def main():
    args = make_args()
    config = configparser.ConfigParser()
    utils.load_config(config, args.config)
    for cmd in args.modify:
        utils.modify_config(config, cmd)
    with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f:
        logging.config.dictConfig(yaml.load(f))
    model_dir = utils.get_model_dir(config)
    category = utils.get_category(config)
    anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
    path, step, epoch = utils.train.load_model(model_dir)
    state_dict = torch.load(path, map_location=lambda storage, loc: storage)
    dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(
        config, state_dict), anchors, len(category))
    logging.info(
        humanize.naturalsize(
            sum(var.cpu().numpy().nbytes
                for var in dnn.state_dict().values())))
    dnn.load_state_dict(state_dict)
    height, width = tuple(map(int, config.get('image', 'size').split()))
    image = torch.autograd.Variable(
        torch.randn(args.batch_size, 3, height, width))
    output = dnn(image)
    state_dict = dnn.state_dict()
    closure = utils.walk.Closure(args.name, state_dict,
                                 type(dnn).scope, args.debug)
    closure(output.grad_fn)
    d = utils.dense(state_dict[args.name])
    channels = torch.LongTensor(np.argsort(d)[int(len(d) * args.remove):])
    utils.walk.prune(closure, channels)
    if args.debug:
        path = closure.dot.view(
            os.path.basename(model_dir) + '.gv', os.path.dirname(model_dir))
        logging.info(path)
    else:
        torch.save(state_dict, path)
def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument("--epochs",
                        type=int,
                        default=10000,
                        help="number of epochs")
    parser.add_argument("--batch_size",
                        type=int,
                        default=20,
                        help="size of each image batch")
    parser.add_argument("--data_config",
                        type=str,
                        default="config/adc.data",
                        help="path to data config file")
    # parser.add_argument("--pretrained_weights", type=str, default="config/yolov3_ckpt_5.pth")  # models/model1/yolov3_ckpt_73.pth
    parser.add_argument("--pretrained_weights",
                        type=str)  # models/model1/yolov3_ckpt_73.pth
    parser.add_argument(
        "--n_cpu",
        type=int,
        default=0,
        help="number of cpu threads to use during batch generation")
    parser.add_argument("--img_size",
                        type=int,
                        default=[768, 1024],
                        help="size of each image dimension")
    parser.add_argument("--evaluation_interval",
                        type=int,
                        default=1,
                        help="interval evaluations on validation set")
    parser.add_argument("--multiscale",
                        default='False',
                        choices=['True', 'False'])
    parser.add_argument("--augment",
                        default='False',
                        choices=['True', 'False'])
    parser.add_argument("--save_path",
                        type=str,
                        default='models/weights_1350_0102',
                        help="save model path")
    parser.add_argument("--debug",
                        type=str,
                        default='False',
                        choices=['True', 'False'],
                        help="debug")
    parser.add_argument("--lr", type=float, default=0.01, help="learning rate")
    args = parser.parse_args(argv)

    args.debug = True if args.debug == 'True' else False
    args.multiscale = True if args.multiscale == 'True' else False
    args.augment = True if args.augment == 'True' else False
    print_args(args)

    print(
        datetime.datetime.strftime(datetime.datetime.now(),
                                   '%Y-%m-%d %H:%M:%S'))

    if args.debug:
        print('debug...')
        import shutil
        # if os.path.exists(args.save_path):
        #     shutil.rmtree(args.save_path)
        args.evaluation_interval = 1
        # debug模式下先删除save_path,并每间隔一轮验证一次

    # assert not os.path.exists(args.save_path)
    # os.makedirs(args.save_path)

    # adc.dat下有train和valid两个dat还有anchor.txt的路径
    data_config = parse_data_config(args.data_config)
    train_path = data_config["train"]
    valid_path = data_config["valid"]
    if args.debug:
        valid_path = train_path
    anchors = get_anchors(data_config['anchors']).to('cuda')

    model = ResNet(anchors).to('cuda')
    if args.pretrained_weights:
        print('pretrained weights: ', args.pretrained_weights)
        model.load_pretrained_weights(args.pretrained_weights)

    dataset = ListDataset(train_path,
                          img_size=args.img_size,
                          augment=args.augment,
                          multiscale=args.multiscale)
    eval = evaluate(path=valid_path,
                    img_size=args.img_size,
                    batch_size=args.batch_size,
                    debug=args.debug)

    if args.debug:
        dataset.img_files = dataset.img_files[:10 * args.batch_size]
    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=args.batch_size,
        shuffle=True,
        num_workers=args.n_cpu,
        collate_fn=dataset.collate_fn,
    )
    print('Number train sample: ', len(dataset))
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=args.lr,
                                 weight_decay=5e-5)
    # 这里优化器和学习率是不是要调节?
    print('\n### train ...')
    for epoch in range(args.epochs):
        model.train()

        lr = max(1e-10, args.lr * (0.95**epoch))
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr

        for batch_i, (imgs, targets, _) in enumerate(dataloader):
            imgs = Variable(imgs.to('cuda'))
            # 训练集有经过augment_sequential,而验证集没有
            # targets=([[0.0000, 0.7328, 0.2808, 0.0934, 0.0808],
            #         [1.0000, 0.5255, 0.5466, 0.0596, 0.1587],
            #         [1.0000, 0.5585, 0.8077, 0.0553, 0.2250],
            #         [3.0000, 0.4519, 0.4351, 0.1365, 0.2048]], device='cuda:0')
            targets = Variable(targets.to('cuda'), requires_grad=False)

            yolo_map, _ = model(imgs)
            #  yolo_map.shape : [4,]  其中每个yolo_map的格式如下: batch,featuremap_h,featuremap_w,anchor_num,(x,y,w,h,conf)
            loss, metrics = model.loss(yolo_map, targets)

            loss.backward()
            optimizer.step()
            optimizer.zero_grad()

            if (batch_i + 1) % 100 == 0 or (batch_i + 1) == len(dataloader):
                time_str = datetime.datetime.strftime(datetime.datetime.now(),
                                                      '%Y-%m-%d %H:%M:%S')
                lr = optimizer.param_groups[0]['lr']
                loss = metrics["loss"]
                xy = metrics["xy"]
                wh = metrics["wh"]
                conf = metrics["conf"]
                loss_str = 'loss: {:<8.2f}'.format(loss)
                loss_str += 'xy: {:<8.2f}'.format(xy)
                loss_str += 'wh: {:<8.2f}'.format(wh)
                loss_str += 'conf: {:<8.2f}'.format(conf)
                epoch_str = 'Epoch: {:4}({:4}/{:4})'.format(
                    epoch, batch_i + 1, len(dataloader))
                print('[{}]{} {} lr:{}'.format(time_str, epoch_str, loss_str,
                                               lr))
        print()
        if epoch % args.evaluation_interval == 0:
            print("\n---- Evaluating Model ----")
            save_model_epoch = 'yolov3_ckpt_{}.pth'.format(epoch)
            model.save_weights(os.path.join(args.save_path, save_model_epoch))
            print(save_model_epoch)
            example_save_path = args.save_path
            for conf in [0.1, 0.3, 0.5, 0.7]:
                metrics = eval(model,
                               iou_thres=0.5,
                               conf_thres=conf,
                               nms_thres=0.5,
                               save_path=example_save_path)
                example_save_path = None
                print(
                    'image_acc: {}\t{}\tbbox_acc: {}\tbbox_recall: {}'.format(
                        *metrics[1:]))

                names = ['image', 'ture', 'det', 'box_acc', 'image_acc']
                print('{:<10}{:<10}{:<10}{:<10}{:<10}'.format(*names))
                print('{:<10}{:<10}{:<10}{:<10}{:<10}'.format(*metrics[0][0]))
                print('{:<10}{:<10}{:<10}{:<10}{:<10}'.format(*metrics[0][1]))
                print()
Пример #20
0
def train_yolo_v3():
    if hyper_parameters.FLAGS.load_model is not None:
        checkpoints_dir = "checkpoints/" + hyper_parameters.FLAGS.load_model.lstrip(
            "checkpoints/")
    else:
        current_time = datetime.now().strftime("%Y%m%d-%H%M")
        checkpoints_dir = "checkpoints/{}".format(current_time)
        try:
            os.makedirs(checkpoints_dir)
        except os.error:
            pass

    labels = json_convert.load_label(hyper_parameters.FLAGS.labels_file)
    anchors = utils.get_anchors(hyper_parameters.FLAGS.anchors_path)
    anchors = np.array(anchors, dtype=np.float32)
    tf.reset_default_graph()
    graph = tf.Graph()
    with graph.as_default():
        yolo_v3 = DETECTERSUBNET(
            'yolo_v3',
            image_size=hyper_parameters.FLAGS.image_size,
            anchors=anchors,
            batch_size=hyper_parameters.FLAGS.batch_size,
            num_anchors=hyper_parameters.FLAGS.num_anchors,
            learning_rate=hyper_parameters.FLAGS.learning_rate,
            num_classes=hyper_parameters.FLAGS.num_classes,
            num_id1=hyper_parameters.FLAGS.num_id1,
            num_id2=hyper_parameters.FLAGS.num_id2,
            num_id3=hyper_parameters.FLAGS.num_id3,
            num_id4=hyper_parameters.FLAGS.num_id4,
            num_id5=hyper_parameters.FLAGS.num_id5,
            norm=hyper_parameters.FLAGS.norm,
            threshold=hyper_parameters.FLAGS.threshold,
            max_num_boxes_per_image=hyper_parameters.FLAGS.
            max_num_boxes_per_image)

        reader = Reader(hyper_parameters.FLAGS.X,
                        batch_size=hyper_parameters.FLAGS.batch_size,
                        image_size=hyper_parameters.FLAGS.image_size)
        x, image_ids, image_heights, image_widths = reader.feed()

        loss = yolo_v3.model(x)
        last_layer_optimizer, yolo_optimizer = yolo_v3.yolo_v3_optimizer(loss)

        saver = tf.train.Saver()

        config = tf.ConfigProto(log_device_placement=True,
                                allow_soft_placement=True)
        config.gpu_options.allow_growth = True

        if train_mode is True:
            with graph.as_default():
                summary_op = tf.summary.merge_all()
                train_writer = tf.summary.FileWriter(checkpoints_dir, graph)

            with tf.Session(graph=graph, config=config) as sess:
                if hyper_parameters.FLAGS.load_model is not None:
                    checkpoint = tf.train.get_checkpoint_state(checkpoints_dir)
                    meta_graph_path = checkpoint.model_checkpoint_path + ".meta"
                    restore = tf.train.import_meta_graph(meta_graph_path)
                    restore.restore(
                        sess, tf.train.latest_checkpoint(checkpoints_dir))
                    step = int(meta_graph_path.split("-")[2].split(".")[0])
                else:
                    sess.run(tf.global_variables_initializer())
                    step = 0

                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(sess=sess, coord=coord)

                try:
                    while not coord.should_stop() and step < 400000:

                        img_ids, img_heights, img_widths = sess.run(
                            [image_ids, image_heights, image_widths])
                        heatmaps = labels_generator.get_detector_heatmap(
                            img_ids, img_heights, img_widths, labels)

                        optimizer = yolo_optimizer

                        # if step < 100000 // 3:
                        #     optimizer = last_layer_optimizer
                        _, loss_val, summary = sess.run(
                            [optimizer, loss, summary_op],
                            feed_dict={
                                yolo_v3.Y_true_data: heatmaps[0],
                                yolo_v3.Y_true_boxes: heatmaps[1]
                            })

                        train_writer.add_summary(summary, step)
                        train_writer.flush()
                        if (step + 1) % 100 == 0:
                            logging.info('-----------Step %d:-------------' %
                                         (step + 1))
                            logging.info('  loss   : {}'.format(loss_val))

                        if step % 10000 == 0:
                            save_path = saver.save(sess,
                                                   checkpoints_dir +
                                                   "/model.ckpt",
                                                   global_step=step)
                            logging.info("Model saved in file: %s" % save_path)

                        step += 1

                except KeyboardInterrupt:
                    logging.info('Interrupted')
                    coord.request_stop()

                except Exception as e:
                    coord.request_stop(e)

                finally:
                    save_path = saver.save(sess,
                                           checkpoints_dir + "/model.ckpt",
                                           global_step=step)
                    logging.info("Model saved in file: %s" % save_path)
                    # When done, ask the threads to stop.
                    coord.request_stop()
                    coord.join(threads)
@author: Melon
"""

import tensorflow as tf
import os

import yolov3_tiny, utils

if __name__ == '__main__':
    os.environ['CUDA_VISIBLE_DEVICES'] = '2'
    sess = tf.Session()

    image_h, image_w = 480, 640
    CLASSES = ['rubbish']
    anchors = utils.get_anchors('./anchors.txt', image_h, image_w)
    num_classes = len(CLASSES)

    ckpt_file = './checkpoint/yolov3_mix_v2_tiny_low_q/Rubbish.ckpt-20000'
    model = yolov3_tiny.yolov3_tiny(num_classes, anchors)

    with tf.Graph().as_default() as graph:
        sess = tf.Session(graph=graph)
        inputs = tf.placeholder(
            tf.float32,
            [1, image_h, image_w, 3])  # placeholder for detector inputs
        print("=>", inputs)

        with tf.variable_scope('yolov3-tiny'):
            feature_map = model.forward(inputs, is_training=False)
Пример #22
0
def _main():
    # 显存分配
    gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)

    # 模型参数
    class_names = get_classes(classes_path)
    num_classes = len(class_names)

    anchors = get_anchors(anchors_path)

    input_shape = (416, 416)

    # 创建模型
    model = create_model(num_classes, weight_path, anchors, input_shape)

    # Callback
    logging = TensorBoard(log_dir=log_dir)
    checkpoint = ModelCheckpoint(
        log_dir + '/' +
        'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5',
        monitor='val_loss',
        save_weights_only=True,
        save_best_only=True,
        period=3)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.1,
                                  patience=3,
                                  verbose=1)
    early_stopping = EarlyStopping(monitor='val_loss',
                                   min_delta=0,
                                   patience=10,
                                   verbose=1)

    # 打乱/分割训练集
    val_split = 0.1
    with open(train_path) as f:
        lines = f.readlines()

    np.random.seed(10101)
    np.random.shuffle(lines)
    np.random.seed(None)

    num_val = int(len(lines) * val_split)
    num_train = len(lines) - num_val

    # Train with frozen layers first, to get a stable loss.
    # Adjust num epochs to your dataset. This step is enough to obtain a not bad model.
    if not True:
        model.compile(
            optimizer=Adam(lr=1e-3),
            loss={
                # use custom yolo_loss Lambda layer.
                'yolo_loss': lambda y_true, y_pred: y_pred
            })

        batch_size = 64
        print('Train on {} samples, val on {} samples, with batch size {}.'.
              format(num_train, num_val, batch_size))
        model.fit_generator(data_generator_wrapper(lines[:num_train],
                                                   batch_size, input_shape,
                                                   anchors, num_classes),
                            steps_per_epoch=max(1, num_train // batch_size),
                            validation_data=data_generator_wrapper(
                                lines[num_train:], batch_size, input_shape,
                                anchors, num_classes),
                            validation_steps=max(1, num_val // batch_size),
                            epochs=100,
                            initial_epoch=0,
                            callbacks=[logging, checkpoint])

        model.save_weights(log_dir + '/trained_weights_stage_1.h5')
        print("Save path: ", log_dir, '/trained_weights_stage_1.h5')
    else:
        new_weight = log_dir + '/ep071-loss19.917-val_loss10.860.h5'
        assert new_weight.endswith(
            '.h5'), 'Keras model or weights must be a .h5 file.'
        model.load_weights(new_weight)

    # Unfreeze and continue training, to fine-tune.
    # Train longer if the result is not good.

    if True:
        for i in range(len(model.layers)):
            model.layers[i].trainable = True

        model.compile(optimizer=Adam(lr=1e-4),
                      loss={
                          'yolo_loss': lambda y_true, y_pred: y_pred
                      })  # recompile to apply the change
        print('Unfreeze all of the layers.')

        batch_size = 4  # note that more GPU memory is required after unfreezing the body
        print('Train on {} samples, val on {} samples, with batch size {}.'.
              format(num_train, num_val, batch_size))

        model.fit_generator(data_generator_wrapper(lines[:num_train],
                                                   batch_size, input_shape,
                                                   anchors, num_classes),
                            steps_per_epoch=max(1, num_train // batch_size),
                            validation_data=data_generator_wrapper(
                                lines[num_train:], batch_size, input_shape,
                                anchors, num_classes),
                            validation_steps=max(1, num_val // batch_size),
                            epochs=200,
                            initial_epoch=100,
                            callbacks=[logging, checkpoint])
        # callbacks=[logging, checkpoint, reduce_lr, early_stopping])

        model.save_weights(log_dir + '/trained_weights_final.h5')
        print("Save path: ", log_dir, '/trained_weights_final.h5')
Пример #23
0
    def __init__(self, inputs, training, data_format=None):
        """Creates the model.
        Args:
            n_classes: Number of class labels.
            model_size: The input size of the model.
            max_output_size: Max number of boxes to be selected for each class.
            iou_threshold: Threshold for the IOU.
            confidence_threshold: Threshold for the confidence score.
            data_format: The input format.
        Returns:
            None.
        """
        self.iou_loss_thresh = 0.5
        self.num_class = 2
        self.anchor_per_scale = 3
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.n_classes = len(self.classes)
        self.model_size = cfg.YOLO.MODEL_SIZE
        self.max_output_size = cfg.YOLO.MAX_OUTPUT_SIZE
        self.iou_threshold = cfg.YOLO.IOU_THRESHOLD
        self.data_format = data_format
        self.strides = np.array(cfg.YOLO.STRIDES)
        self._giou_loss_backup = None

        if self.data_format == 'channels_first':
            inputs = tf.transpose(inputs, [0, 3, 1, 2])

        with tf.variable_scope('yolo_v3_model'):
            self.anchors = utils.get_anchors()

            route1, route2, inputs = darknet53(inputs=inputs, training=training, data_format=self.data_format)
            route, inputs = yolo_conv_block(inputs=inputs, filters=512, training=training, data_format=self.data_format,
                                            name='yolo_conv_block_1')
            self.conv_lbbox = yolo_detection_layer(inputs, n_classes=self.n_classes,
                                                                    anchors=self.anchors[2],
                                                                    img_size=self.model_size,
                                                                    data_format=self.data_format,
                                                                    name='con_lbbox_layer',
                                                                    strides=self.strides[2])
            inputs = con2d_fixed_padding(route, filters=256, kernel_size=1, data_format=self.data_format, name='conv_lbbox')
            inputs = batch_norm(inputs, training=training, data_format=self.data_format)
            inputs = tf.nn.leaky_relu(inputs, alpha=cfg.YOLO.LEAKY_RELU)

            upsample_output_size = route2.get_shape().as_list()
            inputs = upsample(inputs, out_shape=upsample_output_size, data_format=self.data_format)

            # get the channel axis
            axis = 1 if self.data_format == 'channels_first' else 3
            inputs = tf.concat((inputs, route2), axis=axis)

            route, inputs = yolo_conv_block(inputs, filters=256, training=training, data_format=self.data_format,
                                            name='yolo_conv_block_2')
            self.conv_mbbox = yolo_detection_layer(inputs, n_classes=self.n_classes,
                                                                    anchors=self.anchors[1],
                                                                    img_size=self.model_size,
                                                                    data_format=self.data_format,
                                                                    name='con_mbbox_layer',
                                                                    strides=self.strides[1])
            inputs = con2d_fixed_padding(route, filters=128, kernel_size=1, data_format=self.data_format, name='conv_mbbox')
            inputs = batch_norm(inputs, training=training, data_format=self.data_format)
            inputs = tf.nn.leaky_relu(inputs, alpha=cfg.YOLO.LEAKY_RELU)
            upsample_output_size = route1.get_shape().as_list()
            inputs = upsample(inputs, out_shape=upsample_output_size, data_format=self.data_format)
            inputs = tf.concat((inputs, route1), axis=axis)

            route, inputs = yolo_conv_block(inputs, filters=128, training=training, data_format=self.data_format,
                                            name='yolo_conv_block_3')
            self.conv_sbbox = yolo_detection_layer(inputs, n_classes=self.n_classes,
                                                                    anchors=self.anchors[0],
                                                                    img_size=self.model_size,
                                                                    data_format=self.data_format,
                                                                    name='conv_sbbox',
                                                                    strides=self.strides[0])

            with tf.variable_scope('pred_sbbox'):
                self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0])
                print("self.pred_sbbox: ", self.pred_sbbox.shape)

            with tf.variable_scope('pred_mbbox'):
                self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[1], self.strides[1])

            with tf.variable_scope('pred_lbbox'):
                self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[2], self.strides[2])
Пример #24
0
import cv2
import glob
import numpy as np
import os
import os.path as osp
from utils import get_anchors, get_classes, preprocess_image
from model import yolo_body

input_shape = (416, 416)
anchors = get_anchors('voc_anchors_416.txt')
classes = get_classes('voc_classes.txt')
num_classes = len(classes)
model, prediction_model = yolo_body(anchors=anchors, score_threshold=0.1)
model.load_weights('checkpoints/pascal_21_9.4463_12.8289_0.8334_0.8535.h5', by_name=True)
batch_size = 1
image_paths = glob.glob('datasets/VOC2007/JPEGImages/*.jpg')
num_images = len(image_paths)
colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)]


def show_image(image, name, contours=None):
    image = image.astype(np.uint8)
    cv2.namedWindow(name, cv2.WINDOW_NORMAL)
    if contours is not None:
        if isinstance(contours, list):
            cv2.drawContours(image, contours, -1, (0, 0, 255), 2)
        else:
            cv2.drawContours(image, [contours], -1, (0, 0, 255), 2)
    cv2.imshow(name, image)

Пример #25
0
import dataLoader
import os
from keras.utils import plot_model
from utils import get_anchors
from kmeans import YOLO_Kmeans

# Define main paths
projectDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
busDetectorDir = (os.path.dirname(os.path.abspath(__file__)))
dataDir = os.path.join(projectDir, "Data")
annotationsPath = os.path.join(dataDir, 'annotationsTrain.txt')
imagesDir = os.path.join(dataDir, "busesTrain")
logDir = os.path.join(busDetectorDir, 'logs/000/')

# Anchors
anchors = get_anchors('yolo_anchors.txt')
anchors = [
    10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373,
    326
]
anchors = np.array(anchors).reshape(-1, 2)

# Classes
classes = ['green', 'orange', 'white', 'silver', 'blue', 'red']
num_classes = len(classes)

input_shape = (3648, 2736)

# Hyper parameters

batch_size = 32
Пример #26
0
def get_detector_heatmap(image_ids, image_heights, image_widths, labels):
    def get_detector_heatmap_each_scale(boxes_data_, best_anchors_,
                                        anchors_mask, grid_size, num_classes):

        num_anchors = len(anchors_mask)
        boxes_data_shape = boxes_data_.shape

        best_anchors_mask = np.isin(best_anchors_, anchors_mask, invert=True)
        best_anchors_ = best_anchors_ * 1
        best_anchors_ -= min(anchors_mask)
        best_anchors_[best_anchors_mask] = 0

        boxes_data_mask = np.ones_like(best_anchors)
        boxes_data_mask[best_anchors_mask] = 0
        boxes_data_mask = np.expand_dims(boxes_data_mask, -1)
        boxes_data_ = boxes_data_ * boxes_data_mask

        i__ = np.floor(boxes_data_[:, :, 1] * grid_size[0]).astype('int32')
        j = np.floor(boxes_data_[:, :, 0] * grid_size[1]).astype('int32')

        boxes_data_ = boxes_data_.reshape([-1, boxes_data_.shape[-1]])
        best_anchors_ = best_anchors_.reshape([-1, 1])
        i__ = i__.reshape([-1, 1])
        j = j.reshape([-1, 1])

        classes = boxes_data_[:, -1].reshape([-1]).astype(np.int)
        one_hot_array = np.zeros([boxes_data_.shape[0], num_classes])
        one_hot_array[np.arange(boxes_data_.shape[0]), classes] = 1

        boxes_data_mask = boxes_data_[:, 2] > 0
        boxes_data_[boxes_data_mask, 4] = 1
        boxes_data_ = np.concatenate([boxes_data_, one_hot_array], axis=-1)

        y_true = np.zeros([
            boxes_data_shape[0] * int(grid_size[0]) * int(grid_size[1]) *
            num_anchors, 5 + num_classes
        ])

        image_offset = np.repeat(
            np.linspace(0,
                        y_true.shape[0],
                        boxes_data_shape[0],
                        endpoint=False,
                        dtype=np.int),
            boxes_data_.shape[0] / boxes_data_shape[0]).reshape([-1, 1])
        grid_offset = num_anchors * (grid_size[0] * i__ + j)

        indexing_array = np.array(image_offset + grid_offset + best_anchors_,
                                  dtype=np.int32)
        indexing_array = indexing_array[boxes_data_mask, :]
        indexing_array = indexing_array.reshape([-1])

        y_true[indexing_array, :] = boxes_data_[boxes_data_mask]
        y_true = y_true.reshape([
            boxes_data_shape[0],
            int(grid_size[0]) * int(grid_size[1]) * num_anchors,
            num_classes + 5
        ])
        boxes_data_ = boxes_data_.reshape(
            [boxes_data_shape[0], boxes_data_shape[1], -1])

        return y_true, boxes_data_[..., 0:4]

    anchors = utils.get_anchors(hyper_parameters.FLAGS.anchors_path)
    anchors = np.array(anchors, dtype=np.float32)

    boxes_data = []

    for i in range(len(image_ids)):
        label = labels[image_ids[i].decode('utf-8')]
        origin_height = image_heights[i]
        origin_width = image_widths[i]

        box = build_data(
            label,
            origin_height,
            origin_width,
            max_boxes=hyper_parameters.FLAGS.max_num_boxes_per_image)
        boxes_data.append(box)

    boxes_data = np.array(boxes_data)

    boxes_xy = (boxes_data[:, :, 0:2] + boxes_data[:, :, 2:4]) // 2
    boxes_hw = boxes_data[:, :, 2:4] - boxes_data[:, :, 0:2]

    boxes_data[:, :, 0] = boxes_xy[..., 0] / hyper_parameters.FLAGS.image_size
    boxes_data[:, :, 1] = boxes_xy[..., 1] / hyper_parameters.FLAGS.image_size
    boxes_data[:, :, 2] = boxes_hw[..., 0] / hyper_parameters.FLAGS.image_size
    boxes_data[:, :, 3] = boxes_hw[..., 1] / hyper_parameters.FLAGS.image_size

    hw = np.expand_dims(boxes_hw, -2)
    anchors_broad = np.expand_dims(anchors, 0)

    anchor_maxes = anchors_broad / 2.
    anchor_mins = -anchor_maxes
    box_maxes = hw / 2.
    box_mins = -box_maxes
    intersect_mins = np.maximum(box_mins, anchor_mins)
    intersect_maxes = np.minimum(box_maxes, anchor_maxes)
    intersect_hw = np.maximum(intersect_maxes - intersect_mins, 0.)
    intersect_area = intersect_hw[..., 0] * intersect_hw[..., 1]
    box_area = hw[..., 0] * hw[..., 1]
    anchor_area = anchors[..., 0] * anchors[..., 1]
    iou = intersect_area / (box_area + anchor_area - intersect_area)
    best_anchors = np.argmax(iou, axis=-1)

    large_obj_image_size = hyper_parameters.FLAGS.image_size // 32
    medium_obj_image_size = hyper_parameters.FLAGS.image_size // 16
    small_obj_image_size = hyper_parameters.FLAGS.image_size // 8

    large_obj_detectors, large_obj_boxes = get_detector_heatmap_each_scale(
        boxes_data,
        best_anchors_=best_anchors,
        anchors_mask=[6, 7, 8],
        grid_size=(large_obj_image_size, large_obj_image_size),
        num_classes=hyper_parameters.FLAGS.num_classes)
    medium_obj_detectors, medium_obj_boxes = get_detector_heatmap_each_scale(
        boxes_data,
        best_anchors_=best_anchors,
        anchors_mask=[3, 4, 5],
        grid_size=(medium_obj_image_size, medium_obj_image_size),
        num_classes=hyper_parameters.FLAGS.num_classes)
    small_obj_detectors, small_obj_boxes = get_detector_heatmap_each_scale(
        boxes_data,
        best_anchors_=best_anchors,
        anchors_mask=[0, 1, 2],
        grid_size=(small_obj_image_size, small_obj_image_size),
        num_classes=hyper_parameters.FLAGS.num_classes)

    yolo_true_data = np.concatenate(
        [large_obj_detectors, medium_obj_detectors, small_obj_detectors],
        axis=1)
    yolo_true_boxes = np.concatenate(
        [large_obj_boxes, medium_obj_boxes, small_obj_boxes], axis=1)

    heatmaps = [yolo_true_data, yolo_true_boxes]

    return heatmaps
Пример #27
0
# Description : Define yolo3 model,loss function,etc.
#
#=====================================================

import numpy as np
import tensorflow as tf
import utils
import common
import backbone
import getConfig

cfg = {}
cfg = getConfig.getConfig()

NUM_CLASS = len(utils.read_class_names(cfg['yolo_classes_names']))
ANCHORS = utils.get_anchors(cfg['yolo_anchors'])
STRIDES = np.array(cfg['yolo_strides'])
IOU_LOSS_THRESHOLD = cfg['yolo_iou_loss_threshold']

def yolo3(input_layer):
    branch_1,branch_2,conv = backbone.backbone(input_layer)

    conv = common.conv(conv,(1,512))
    conv = common.conv(conv,(3,1024))
    conv = common.conv(conv,(1,512))
    conv = common.conv(conv,(3,1024))
    conv = common.conv(conv,(1,512))

    conv_l_branch = common.conv(conv,(3,1024))
    l_output = common.conv(conv_l_branch,(1,3 * (NUM_CLASS + 5)),activation = False,bn = False)
Пример #28
0
anchors_s = tp.Numpy.Placeholder((cfg.YOLO.ANCHOR_PER_SCALE, 2))
anchors_l = tp.Numpy.Placeholder((cfg.YOLO.ANCHOR_PER_SCALE, 2))

func_config = flow.FunctionConfig()
model = Yolov3_tiny(cfg, trainable=False)


@flow.global_function(type="predict", function_config=func_config)
def test_job(images: test_images, anchors_s: anchors_s, anchors_l: anchors_l) \
        -> tp.Numpy:
    pred = model.predict(images, anchors_s, anchors_l)

    return pred


anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS))
anchors[0, ...] /= cfg.YOLO.STRIDES[0]
anchors[1, ...] /= cfg.YOLO.STRIDES[1]


def predict(original_image):
    '''

    :param original_image: [H, W, 3]
    :return: (xmin, ymin, xmax, ymax, score, class)
    '''

    image = utils.image_preporcess(np.copy(original_image),
                                   [cfg.TEST.INPUT_SIZE, cfg.TEST.INPUT_SIZE])
    image = image[np.newaxis, ...]
    image_ = np.transpose(image, [0, 3, 1, 2])
Пример #29
0
import tensorflow as tf
import numpy as np
from config import cfg
from utils import read_class_names, get_anchors

NUM_CLASS = len(read_class_names(cfg.YOLO.CLASSES))
ANCHORS = get_anchors(cfg.YOLO.ANCHORS)
STRIDES = np.array(cfg.YOLO.STRIDES)


def decode(conv_output, i=0):
    """
    :return [batch_size, output_size, output_size, anchor_per_scale, 5 + num_classes]
    box format: (x, y, w, h, score, probability)
    """

    conv_shape = tf.shape(conv_output)
    batch_size = conv_shape[0]
    output_size = conv_shape[1]

    conv_output = tf.reshape(
        conv_output, (batch_size, output_size, output_size, 3, 5 + NUM_CLASS))

    conv_raw_dxdy = conv_output[:, :, :, :, 0:2]
    conv_raw_dwdh = conv_output[:, :, :, :, 2:4]
    conv_raw_conf = conv_output[:, :, :, :, 4:5]
    conv_raw_prob = conv_output[:, :, :, :, 5:]

    y = tf.tile(
        tf.range(output_size, dtype=tf.int32)[:, tf.newaxis], [1, output_size])
    x = tf.tile(
        model_dict = self.state_dict()
        if by_name:
            pretrianed_dict_update = {}
            for k, v in pretrianed_dict.items():
                if k in model_dict:
                    vv = model_dict[k]
                    if v.size() == vv.size():
                        pretrianed_dict_update[k] = v
            model_dict.update(pretrianed_dict_update)
        else:
            model_dict.update(pretrianed_dict)
        self.load_state_dict(model_dict)


if __name__ == '__main__':
    # import os
    # os.environ['CUDA_VISIBLE_DEVICES'] = '3'
    #
    anchors = get_anchors(
        '/data1/chenww/my_research/Two-Stage-Defect-Detection/detector/config/small_8cls/anchors.txt'
    ).to('cuda')
    model = ResNet(anchors).to('cuda')
    model_info(model, verbose=True)
    # print(model)

    # input = torch.randn(1, 3, 224, 224)
    # map, outputs = model(input)
    # print([o.size() for o in map])
    # print()
    # torch.save(model.state_dict(), 'model.pth')
Пример #31
0
def output_to_boxes(prob_score_map, reg_map):
    '''
    Convert VoxelNet output to bounding boxes for visualization

    Parameters:
        prob_score_map (arr): (BS, 2, H, W), probability score map
        reg_map (arr): (BS, 14, H, W), regression map (deltas)

    Returns:
        arr: boxes in center notation
        arr: boxes in corner notation
        arr: scores for the boxes
    '''
    config = load_config()
    batch_size, _, _, _ = prob_score_map.shape
    device = prob_score_map.device

    # Convert regression map back to bounding boxes (center notation)
    batch_boxes3d = delta_to_boxes3d(reg_map, get_anchors())

    batch_boxes2d = batch_boxes3d[:, :, [0, 1, 4, 5, 6]]
    batch_probs = prob_score_map.reshape((batch_size, -1))

    batch_boxes3d = batch_boxes3d.cpu().numpy()
    batch_boxes2d = batch_boxes2d.cpu().numpy()
    batch_probs = batch_probs.cpu().numpy()

    return_box3d = []
    return_score = []
    for batch_id in range(batch_size):
        # Remove boxes under the threshold
        ind = np.where(
            batch_probs[batch_id, :] >= config['nms_score_threshold'])
        tmp_boxes3d = batch_boxes3d[batch_id, ind, ...].squeeze()
        tmp_boxes2d = batch_boxes2d[batch_id, ind, ...].squeeze()
        tmp_scores = batch_probs[batch_id, ind].squeeze()

        # Convert center notation 3d boxes to corner notation 2d boxes
        corner_box2d = center_to_corner_box2d(tmp_boxes2d)

        # Convert from xxyy to xyxy
        boxes2d = corner_to_standup_box2d(corner_box2d)

        # Apply NMS to get rid of duplicates
        ind, cnt = nms(
            torch.from_numpy(boxes2d).to(device),
            torch.from_numpy(tmp_scores).to(device),
            config['nms_threshold'],
            20,
        )
        try:
            ind = ind[:cnt].cpu().detach().numpy()
        except IndexError:
            print('Unable to select NMS-detected boxes, returning None')
            return None, None

        tmp_boxes3d = tmp_boxes3d[ind, ...]
        tmp_scores = tmp_scores[ind]
        return_box3d.append(tmp_boxes3d)
        return_score.append(tmp_scores)

    return_box3d = np.array(return_box3d)

    # Convert center notation 3d boxes to corner notation 3d boxes
    ret_box3d_corner = box3d_center_to_corner_batch(return_box3d)

    return ret_box3d_corner, return_box3d