def __init__(self, image_dir, strides, patch_size, batch_size):
        self.image_dir = image_dir
        self.strides = strides
        self.patch_size = patch_size
        self.batch_size = batch_size
        self.image_names = os.listdir(image_dir)
        
        self.image_name = ''
        self.image_id = 0
        self.x_y_list = []

        self.reach_end = False
        self.name_shape = dict()
        self.total_slides = 0

        self.read_img_timer = Timer(name='read_img')
        self.copy_img_timer = Timer(name='copy_img')
    def run(self):
        producer_timer = Timer(name='producer')
        producer_timer.tic()

        image_names = os.listdir(self.image_dir)
        log.info("image_nums: {}".format(len(image_names)))
        for image_id, image_name in enumerate(image_names):
            image_path = os.path.join(self.image_dir, image_name)
            img = cv2.imread(image_path).astype('float32')

            height, width, _ = img.shape
            image_shape = (width, height)

            x_num, y_num = calc_split_num(image_shape, self.patch_size, self.strides)

            log.info("id:{}, name: {}, shape: ({},{}), x_num:{}, y_num:{}".format(
                    image_id, image_name, height, width, x_num, y_num))

            # n = 1
            img = img.transpose((2, 0, 1))  # Change data layout from HWC to CHW

            for i in range(x_num):
                for j in range(y_num):
                    x = self.strides[0] * i if i < x_num - 1 else image_shape[0] - self.patch_size[0]
                    y = self.strides[1] * j if j < y_num - 1 else image_shape[1] - self.patch_size[1]
                    # print('[Producer]processing {} , x: {}, y: {}'.format(image_name, x, y))

                    crop_img = img[:, y:(y+self.patch_size[1]), x:(x+self.patch_size[0])].copy()
                    crop_img = crop_img[np.newaxis, :, :, :]
                    crop_img_meta=dict(name=image_name, x=x, y=y)
                    self.data.put(dict(img=crop_img, meta=crop_img_meta))

        self.data.put('quit')
        producer_timer.toc()
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(producer_timer.name, producer_timer.avg * 1000, producer_timer.total))
コード例 #3
0
def main():
    total_timer = Timer(name='total')
    total_timer.tic()

    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    # --------------------------- 1. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)
    # -----------------------------------------------------------------------------------------------------

    # ------------- 2. Load Plugin for inference engine and extensions library if specified --------------
    log.info("Loading Inference Engine")
    ie = IECore()
    log.info("Device info:")
    versions = ie.get_versions(args.device)
    print("{}{}".format(" " * 8, args.device))
    print("{}MKLDNNPlugin version ......... {}.{}".format(
        " " * 8, versions[args.device].major, versions[args.device].minor))
    print("{}Build ........... {}".format(" " * 8,
                                          versions[args.device].build_number))

    if args.cpu_extension and "CPU" in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
        log.info("CPU extension loaded: {}".format(args.cpu_extension))

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- 4. Configure input & output ---------------------------------------------
    # --------------------------- Prepare input blobs -----------------------------------------------------
    log.info("Preparing input blobs")
    assert (len(net.inputs.keys()) == 1
            ), "Sample supports topologies only with 1 input"

    input_name = next(iter(net.inputs.keys()))
    input_info = net.inputs[input_name]
    input_info.precision = 'FP32'

    # --------------------------- Prepare output blobs ----------------------------------------------------
    log.info('Preparing output blobs')
    assert (len(net.outputs.keys()) == 2
            ), "Sample supports topologies only with 2 output"

    loc_out_name = "797"
    class_out_name = "741"
    assert (loc_out_name in net.outputs.keys()) and (class_out_name
                                                     in net.outputs.keys())

    loc_out_info = net.outputs[loc_out_name]
    class_out_info = net.outputs[class_out_name]

    loc_out_info.precision = "FP32"
    class_out_info.precision = "FP32"
    # -----------------------------------------------------------------------------------------------------

    # -----------------------------------------------------------------------------------------------------
    log.info("Loading model to the device")
    exec_net = ie.load_network(network=net, device_name=args.device)

    # --------------------------- 3. Read and preprocess input --------------------------------------------
    # -----------------------------------------------------------------------------------------------------
    if not os.path.exists(args.result_dir):
        os.makedirs(args.result_dir)

    if args.voc_res_file and os.path.exists(args.voc_res_file):
        os.remove(args.voc_res_file)

    create_anchor_timer = Timer(name='create_anchor')
    read_img_timer = Timer(name='read_img')
    preprocess_timer = Timer(name='preprocess')
    infer_timer = Timer(name='infer')
    adapter_timer = Timer(name='adapter')
    patch_img_nms_timer = Timer(name='patch_img_nms')
    whole_img_nms_timer = Timer(name='whole_img_nms')
    add_offset_timer = Timer(name='add_offset')
    write_result_timer = Timer(name='write_result')

    create_anchor_timer.tic()
    adapter = RetinaNetAdapter(input_shape=args.patch_size)
    create_anchor_timer.toc()

    image_names = os.listdir(args.image_dir)
    log.info("image_nums: {}".format(len(image_names)))
    for image_id, image_name in enumerate(image_names):
        read_img_timer.tic()
        image_path = os.path.join(args.image_dir, image_name)
        img = cv2.imread(image_path).astype('float32')
        read_img_timer.toc()

        height, width, _ = img.shape
        image_shape = (width, height)
        strides = args.strides
        patch_size = args.patch_size
        x_num, y_num = calc_split_num(image_shape, patch_size, strides)

        log.info("id:{}, name: {}, shape: ({},{}), x_num:{}, y_num:{}".format(
            image_id, image_name, height, width, x_num, y_num))

        preprocess_timer.tic()
        img = img.transpose((2, 0, 1))  # Change data layout from HWC to CHW
        preprocess_timer.toc()

        result_all = []
        for i in range(x_num):
            for j in range(y_num):
                x = strides[0] * i if i < x_num - 1 else image_shape[
                    0] - args.patch_size[0]
                y = strides[1] * j if j < y_num - 1 else image_shape[
                    1] - args.patch_size[1]
                # print('processing {} , x: {}, y: {}'.format(image_name, x, y))

                preprocess_timer.tic()
                crop_img = img[:, y:y + patch_size[1],
                               x:x + patch_size[0]].copy()
                crop_img = crop_img[np.newaxis, :, :, :]
                preprocess_timer.toc()

                # --------------------------- Performing inference ----------------------------------------------------
                infer_timer.tic()
                res = exec_net.infer(inputs={input_name: crop_img})
                loc_out = res[loc_out_name][0]
                class_out = res[class_out_name][0]
                infer_timer.toc()

                adapter_timer.tic()
                result = adapter.process(loc_out, class_out)
                adapter_timer.toc()

                patch_img_nms_timer.tic()
                result, _ = nms(result, thresh=0.5, keep_top_k=100)
                patch_img_nms_timer.toc()

                # import pdb;pdb.set_trace()
                add_offset_timer.tic()
                result[:, 0] += x
                result[:, 1] += y
                result[:, 2] += x
                result[:, 3] += y
                result_all.append(result)
                add_offset_timer.toc()

        # import pdb;pdb.set_trace()
        whole_img_nms_timer.tic()
        result_all = np.concatenate(result_all, axis=0)
        nms_result, _ = nms(result_all, thresh=0.5)
        whole_img_nms_timer.toc()

        write_result_timer.tic()
        voc_format = '{} {:.4f} {} {} {} {}'
        pos_all = []
        voc_all = []
        for i in range(nms_result.shape[0]):
            x = int(nms_result[i, 0])
            y = int(nms_result[i, 1])
            w = max(int(nms_result[i, 2] - nms_result[i, 0]), 1)
            h = max(int(nms_result[i, 3] - nms_result[i, 1]), 1)
            p = float(nms_result[i, 4])
            pos = {'x': x, 'y': y, 'w': w, 'h': h, 'p': p}
            pos_all.append(pos)

            if args.voc_res_file:
                xmin = x
                ymin = y
                xmax = int(nms_result[i, 2])
                ymax = int(nms_result[i, 3])
                voc_str = voc_format.format(
                    os.path.splitext(image_name)[0], p, xmin, ymin, xmax, ymax)
                voc_all.append(voc_str)

        file_name = os.path.splitext(image_name)[0] + '.json'
        with open(os.path.join(args.result_dir, file_name), 'w') as f:
            json.dump(pos_all, f)

        if args.voc_res_file:
            with open(args.voc_res_file, 'a') as f:
                for voc_str in voc_all:
                    f.write(voc_str + '\n')

        write_result_timer.toc()

    total_timer.toc()
    # -----------------------------------------------------------------------------------------------------
    all_timers = []
    all_timers.extend([
        create_anchor_timer, read_img_timer, preprocess_timer, infer_timer,
        adapter_timer, patch_img_nms_timer, whole_img_nms_timer,
        add_offset_timer, write_result_timer, total_timer
    ])
    for timer in all_timers:
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            timer.name, timer.avg * 1000, timer.total))

    log.info("Execution successful\n")
コード例 #4
0
#%%
keys = [key  for (key, val) in art_nfiles.items() if val == 2]

n_picks = 3
idxs  = [random.randint(0,len(keys)) for _ in range(0,n_picks)]

songs = []

artists = list(map(keys.__getitem__, idxs))
print(f'Randomly selected artists {artists}')

for artist in artists:
    songs.append(list(art_midi[artist]))
#%%
with Timer('List generator'):
    D = {key: list(songs)  for key, songs in tqdm(art_midi.items())}

#%%
with open(filename, 'w') as f:
    D = {}
    D['erato'] = {}
    D['erato']['a'] = 2
    json.dump(D,f)


class MidiDataset():
    def __init__(self,filepath, artists, songs):
        self.filepath = filepath
        self.artists = artists
        self.songs = songs
コード例 #5
0
def main():
    total_timer = Timer(name='total')
    total_timer.tic()

    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    # --------------------------- 1. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)
    # -----------------------------------------------------------------------------------------------------

    # ------------- 2. Load Plugin for inference engine and extensions library if specified --------------
    log.info("Loading Inference Engine")
    ie = IECore()
    log.info("Device info:")
    versions = ie.get_versions(args.device)
    print("{}{}".format(" " * 8, args.device))
    print("{}MKLDNNPlugin version ......... {}.{}".format(
        " " * 8, versions[args.device].major, versions[args.device].minor))
    print("{}Build ........... {}".format(" " * 8,
                                          versions[args.device].build_number))

    if args.cpu_extension and "CPU" in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
        log.info("CPU extension loaded: {}".format(args.cpu_extension))

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- 4. Configure input & output ---------------------------------------------
    # --------------------------- Prepare input blobs -----------------------------------------------------
    log.info("Preparing input blobs")
    assert (len(net.inputs.keys()) == 1
            ), "Sample supports topologies only with 1 input"

    input_name = next(iter(net.inputs.keys()))
    input_info = net.inputs[input_name]
    input_info.precision = 'FP32'
    log.info('input shape: {}'.format(input_info.shape))

    # --------------------------- Prepare output blobs ----------------------------------------------------
    log.info('Preparing output blobs')
    assert (len(net.outputs.keys()) == 2
            ), "Sample supports topologies only with 2 output"

    loc_out_name = "797"
    class_out_name = "741"
    assert (loc_out_name in net.outputs.keys()) and (class_out_name
                                                     in net.outputs.keys())

    loc_out_info = net.outputs[loc_out_name]
    class_out_info = net.outputs[class_out_name]

    loc_out_info.precision = "FP32"
    class_out_info.precision = "FP32"
    # -----------------------------------------------------------------------------------------------------

    # -----------------------------------------------------------------------------------------------------
    log.info("Loading model to the device")
    # cpu_throughput = {'CPU_THROUGHPUT_STREAMS': 'CPU_THROUGHPUT_AUTO'}
    ie.set_config({'CPU_THROUGHPUT_STREAMS': 'CPU_THROUGHPUT_AUTO'},
                  args.device)
    ie.set_config({'CPU_BIND_THREAD': 'YES'}, args.device)
    exec_net = ie.load_network(network=net,
                               device_name=args.device,
                               num_requests=0)

    infer_requests = exec_net.requests
    request_queue = InferRequestsQueue(infer_requests)
    log.info('nreqs: {}, nstream:{}'.format(
        len(infer_requests),
        ie.get_config(args.device, 'CPU_THROUGHPUT_STREAMS')))

    # --------------------------- 3. Read and preprocess input --------------------------------------------
    # -----------------------------------------------------------------------------------------------------
    if not os.path.exists(args.result_dir):
        os.makedirs(args.result_dir)

    if args.voc_res_file and os.path.exists(args.voc_res_file):
        os.remove(args.voc_res_file)

    load_data_timer = Timer(name='load_data')
    post_process_timer = Timer(name='post_process')

    adapter = RetinaNetAdapter(input_shape=args.patch_size)

    # --------------------------- Performing inference ----------------------------------------------------
    result_all_images = defaultdict(list)
    data_loader = DataLoader(args.image_dir, args.strides, args.patch_size)
    while True:
        load_data_timer.tic()
        input_data = data_loader.next()
        load_data_timer.toc()

        if input_data == None:
            break

        infer_request = request_queue.get_idle_request()
        if not infer_request:
            raise Exception('No idle Infer Requests!')

        if infer_request.cur_meta == None:
            infer_request.start_async(input_name, input_data)
            continue

        # get result
        post_process_timer.tic()
        image_name = infer_request.cur_meta['image_name']
        x = infer_request.cur_meta['x']
        y = infer_request.cur_meta['y']

        loc_out = infer_request.request.outputs[loc_out_name][0]
        class_out = infer_request.request.outputs[class_out_name][0]

        ## start infer
        infer_request.start_async(input_name, input_data)

        ## post-process
        result = adapter.process(loc_out, class_out)
        result, _ = nms(result, thresh=0.5, keep_top_k=100)

        result[:, 0] += x
        result[:, 1] += y
        result[:, 2] += x
        result[:, 3] += y
        result_all_images[image_name].append(result)
        post_process_timer.toc()

    # wait the latest inference executions
    request_queue.wait_all()
    post_process_timer.tic()
    for infer_request in request_queue.requests:
        # get result
        image_name = infer_request.cur_meta['image_name']
        x = infer_request.cur_meta['x']
        y = infer_request.cur_meta['y']

        loc_out = infer_request.request.outputs[loc_out_name][0]
        class_out = infer_request.request.outputs[class_out_name][0]

        ## post-process
        result = adapter.process(loc_out, class_out)
        result, _ = nms(result, thresh=0.5, keep_top_k=100)

        result[:, 0] += x
        result[:, 1] += y
        result[:, 2] += x
        result[:, 3] += y
        result_all_images[image_name].append(result)
    post_process_timer.toc()

    post_process_timer.tic()
    ## process total image result
    for image_name, result_per_image in result_all_images.items():
        result_per_image = np.concatenate(result_per_image, axis=0)
        nms_result, _ = nms(result_per_image, thresh=0.5)

        voc_format = '{} {:.4f} {} {} {} {}'
        pos_all = []
        voc_all = []
        for i in range(nms_result.shape[0]):
            x = int(nms_result[i, 0])
            y = int(nms_result[i, 1])
            w = max(int(nms_result[i, 2] - nms_result[i, 0]), 1)
            h = max(int(nms_result[i, 3] - nms_result[i, 1]), 1)
            p = float(nms_result[i, 4])
            pos = {'x': x, 'y': y, 'w': w, 'h': h, 'p': p}
            pos_all.append(pos)

            if args.voc_res_file:
                xmin = x
                ymin = y
                xmax = int(nms_result[i, 2])
                ymax = int(nms_result[i, 3])
                voc_str = voc_format.format(
                    os.path.splitext(image_name)[0], p, xmin, ymin, xmax, ymax)
                voc_all.append(voc_str)

        file_name = os.path.splitext(image_name)[0] + '.json'
        with open(os.path.join(args.result_dir, file_name), 'w') as f:
            json.dump(pos_all, f)

        if args.voc_res_file:
            with open(args.voc_res_file, 'a') as f:
                for voc_str in voc_all:
                    f.write(voc_str + '\n')

    post_process_timer.toc()
    total_timer.toc()
    # -----------------------------------------------------------------------------------------------------
    all_timers = []
    # all_timers.extend([create_anchor_timer,
    #                    read_img_timer,
    #                    preprocess_timer,
    #                    infer_timer,
    #                    adapter_timer,
    #                    patch_img_nms_timer,
    #                    whole_img_nms_timer,
    #                    add_offset_timer,
    #                    write_result_timer,
    #                    total_timer])
    all_timers.extend([load_data_timer, post_process_timer, total_timer])
    for timer in all_timers:
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            timer.name, timer.avg * 1000, timer.total))

    log.info('infer: {:2f}s'.format(request_queue.get_duration_in_seconds()))
    log.info("Execution successful\n")
コード例 #6
0
def main():
    total_timer = Timer(name='total')
    total_timer.tic()

    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    # --------------------------- 1. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)
    # -----------------------------------------------------------------------------------------------------

    # ------------- 2. Load Plugin for inference engine and extensions library if specified --------------
    log.info("Loading Inference Engine")
    ie = IECore()
    log.info("Device info:")
    versions = ie.get_versions(args.device)
    print("{}{}".format(" " * 8, args.device))
    print("{}MKLDNNPlugin version ......... {}.{}".format(
        " " * 8, versions[args.device].major, versions[args.device].minor))
    print("{}Build ........... {}".format(" " * 8,
                                          versions[args.device].build_number))

    if args.cpu_extension and "CPU" in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
        log.info("CPU extension loaded: {}".format(args.cpu_extension))

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- 4. Configure input & output ---------------------------------------------
    # --------------------------- Prepare input blobs -----------------------------------------------------
    log.info("Preparing input blobs")
    assert (len(net.inputs.keys()) == 1
            ), "Sample supports topologies only with 1 input"

    input_name = next(iter(net.inputs.keys()))
    input_info = net.inputs[input_name]
    # input_info.precision = 'FP32'
    input_info.precision = 'U8'

    # --------------------------- Prepare output blobs ----------------------------------------------------
    log.info('Preparing output blobs')
    assert (len(net.outputs.keys()) == 2
            ), "Sample supports topologies only with 2 output"

    loc_out_name = args.loc_out_name
    class_out_name = args.class_out_name
    assert (loc_out_name in net.outputs.keys()) and (class_out_name
                                                     in net.outputs.keys())

    loc_out_info = net.outputs[loc_out_name]
    class_out_info = net.outputs[class_out_name]

    loc_out_info.precision = "FP32"
    class_out_info.precision = "FP32"
    # -----------------------------------------------------------------------------------------------------

    # -----------------------------------------------------------------------------------------------------
    log.info("Loading model to the device")
    # ie.set_config({'CPU_THREADS_NUM': str(12)}, 'CPU')
    exec_net = ie.load_network(network=net, device_name=args.device)

    # # --------------------------- 3. Read and preprocess input --------------------------------------------
    # # -----------------------------------------------------------------------------------------------------
    if not os.path.exists(args.result_dir):
        os.makedirs(args.result_dir)

    if args.voc_res_file and os.path.exists(args.voc_res_file):
        os.remove(args.voc_res_file)

    # create_anchor_timer = Timer(name='create_anchor')
    # read_img_timer = Timer(name='read_img')
    # preprocess_timer = Timer(name='preprocess')
    # infer_timer = Timer(name='infer')
    # adapter_timer = Timer(name='adapter')
    # patch_img_nms_timer = Timer(name='patch_img_nms')
    # whole_img_nms_timer = Timer(name='whole_img_nms')
    # add_offset_timer = Timer(name='add_offset')
    # write_result_timer = Timer(name='write_result')

    queue = Queue()
    producer = Producer('Producer', queue, args)
    consumer = Consumer('Consumer', queue, exec_net, input_name, args)

    producer.start()
    consumer.start()

    producer.join()
    consumer.join()
    log.info('All threads finished!')

    total_timer.toc()
    # # -----------------------------------------------------------------------------------------------------
    all_timers = []
    # all_timers.extend([create_anchor_timer,
    #                    read_img_timer,
    #                    preprocess_timer,
    #                    infer_timer,
    #                    adapter_timer,
    #                    patch_img_nms_timer,
    #                    whole_img_nms_timer,
    #                    add_offset_timer,
    #                    write_result_timer,
    #                    total_timer])
    all_timers.extend([total_timer])
    for timer in all_timers:
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            timer.name, timer.avg * 1000, timer.total))

    log.info("Execution successful\n")
コード例 #7
0
    def run(self):
        # if True:
        #     return
        # --------------------------- Performing inference ----------------------------------------------------
        consumer_timer = Timer(name='Consumer')
        consumer_timer.tic()
        infer_timer = Timer(name='infer')
        adapter_timer = Timer(name='adapter')
        patch_img_nms_timer = Timer(name='patch_img_nms')
        add_offset_timer = Timer(name='add_offset')
        whole_img_nms_timer = Timer(name='whole_img_nms')
        while True:

            data = self.data.get()
            if data == 'quit':
                break
            else:
                batch_imgs, batch_metas = data['img'], data['meta']
                # image_name = crop_img_meta['name']
                # x = crop_img_meta['x']
                # y = crop_img_meta['y']
            # import pdb;pdb.set_trace()
            # print('[Producer]forward {} , x: {}, y: {}'.format(crop_img_meta['name'],
            #                                                    crop_img_meta['x'],
            #                                                    crop_img_meta['y']))

            infer_timer.tic()
            res = self.exec_net.infer(inputs={self.input_name: batch_imgs})
            # print('batch_imgs.shape: ', batch_imgs.shape)
            # print('results.shape: ', res[self.loc_out_name].shape)
            infer_timer.toc()

            loc_outs = res[self.loc_out_name]
            class_outs = res[self.class_out_name]
            # import pdb;pdb.set_trace()
            for loc_out, class_out, meta in zip(loc_outs, class_outs,
                                                batch_metas):
                image_name, x, y = meta['image_name'], meta['x'], meta['y']
                if image_name == 'zero_img':
                    continue

                adapter_timer.tic()
                result = self.adapter.process(loc_out, class_out)
                adapter_timer.toc()

                patch_img_nms_timer.tic()
                result, _ = nms(result, thresh=0.5, keep_top_k=100)
                patch_img_nms_timer.toc()

                # # import pdb;pdb.set_trace()
                add_offset_timer.tic()
                result[:, 0] += x
                result[:, 1] += y
                result[:, 2] += x
                result[:, 3] += y
                self.result_all_images[image_name].append(result)
                add_offset_timer.toc()

        for image_name, result_per_image in self.result_all_images.items():
            whole_img_nms_timer.tic()
            result_per_image = np.concatenate(result_per_image, axis=0)
            nms_result, _ = nms(result_per_image, thresh=0.5)
            whole_img_nms_timer.toc()

            voc_format = '{} {:.4f} {} {} {} {}'
            pos_all = []
            voc_all = []
            for i in range(nms_result.shape[0]):
                x = int(nms_result[i, 0])
                y = int(nms_result[i, 1])
                w = max(int(nms_result[i, 2] - nms_result[i, 0]), 1)
                h = max(int(nms_result[i, 3] - nms_result[i, 1]), 1)
                p = float(nms_result[i, 4])
                pos = {'x': x, 'y': y, 'w': w, 'h': h, 'p': p}
                pos_all.append(pos)

                if self.voc_res_file:
                    xmin = x
                    ymin = y
                    xmax = int(nms_result[i, 2])
                    ymax = int(nms_result[i, 3])
                    voc_str = voc_format.format(
                        os.path.splitext(image_name)[0], p, xmin, ymin, xmax,
                        ymax)
                    voc_all.append(voc_str)

            file_name = os.path.splitext(image_name)[0] + '.json'
            with open(os.path.join(self.result_dir, file_name), 'w') as f:
                json.dump(pos_all, f)

            if self.voc_res_file:
                with open(self.voc_res_file, 'a') as f:
                    for voc_str in voc_all:
                        f.write(voc_str + '\n')

        consumer_timer.toc()
        all_timers = []
        all_timers.extend([
            consumer_timer, infer_timer, adapter_timer, patch_img_nms_timer,
            whole_img_nms_timer, add_offset_timer
        ])
        for timer in all_timers:
            log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
                timer.name, timer.avg * 1000, timer.total))
コード例 #8
0
    def run(self):
        producer_timer = Timer(name='producer')
        read_img_timer = Timer(name='read_img')
        transpose_img_timer = Timer(name='transpose_img')
        crop_img_timer = Timer(name='crop_img')
        put_data_timer = Timer(name='put_data')
        copy_data_timer = Timer(name='copy_data')

        producer_timer.tic()

        batch_imgs_list = []
        batch_idx = 0
        zero_meta = dict(image_name='zero_img', x=0, y=0)
        batch_imgs = np.zeros(
            (self.batch_size, 3, self.patch_size[1], self.patch_size[0]))
        batch_metas = [zero_meta] * self.batch_size

        image_names = os.listdir(self.image_dir)
        log.info("image_nums: {}".format(len(image_names)))
        for image_id, image_name in enumerate(image_names):
            image_path = os.path.join(self.image_dir, image_name)
            read_img_timer.tic()
            img = cv2.imread(image_path)
            read_img_timer.toc()

            height, width, _ = img.shape
            image_shape = (width, height)

            x_num, y_num = calc_split_num(image_shape, self.patch_size,
                                          self.strides)

            log.info(
                "id:{}, name: {}, shape: ({},{}), x_num:{}, y_num:{}".format(
                    image_id, image_name, height, width, x_num, y_num))

            # n = 1
            transpose_img_timer.tic()
            img = img.transpose(
                (2, 0, 1))  # Change data layout from HWC to CHW
            transpose_img_timer.toc()

            for i in range(x_num):
                for j in range(y_num):
                    x = self.strides[0] * i if i < x_num - 1 else image_shape[
                        0] - self.patch_size[0]
                    y = self.strides[1] * j if j < y_num - 1 else image_shape[
                        1] - self.patch_size[1]
                    # print('[Producer]processing {} , x: {}, y: {}'.format(image_name, x, y))

                    crop_img_timer.tic()
                    crop_img = img[:, y:(y + self.patch_size[1]),
                                   x:(x + self.patch_size[0])]
                    crop_img = crop_img[np.newaxis, :, :, :]
                    crop_meta = dict(image_name=image_name, x=x, y=y)
                    crop_img_timer.toc()

                    copy_data_timer.tic()
                    batch_imgs[batch_idx] = crop_img
                    batch_metas[batch_idx] = crop_meta
                    batch_idx += 1
                    copy_data_timer.toc()

                    # batch_imgs_list.append(crop_img)
                    # batch_metas.append(crop_meta)

                    if batch_idx == self.batch_size or (
                            image_id == len(image_names) - 1 and i == x_num - 1
                            and j == y_num - 1):
                        batch_idx = 0

                        put_data_timer.tic()

                        self.data.put(
                            dict(img=batch_imgs.copy(),
                                 meta=copy.deepcopy(batch_metas)))

                        batch_imgs = np.zeros(
                            (self.batch_size, 3, self.patch_size[1],
                             self.patch_size[0]))
                        batch_metas = [zero_meta] * self.batch_size

                        put_data_timer.toc()

                    # if len(batch_imgs_list) == self.batch_size or (image_id==len(image_names)-1 and i==x_num-1 and j==y_num-1):
                    #     if len(batch_imgs_list) != self.batch_size:
                    #         append_zero_num = self.batch_size - len(batch_imgs_list)
                    #         zero_img = np.zeros_like(crop_img)
                    #         zero_meta = dict(image_name='zero_img', x=0, y=0)
                    #         for _ in range(append_zero_num):
                    #             batch_imgs_list.append(zero_img)
                    #             batch_metas.append(zero_meta)

                    #     put_data_timer.tic()
                    #     batch_imgs = np.concatenate(batch_imgs_list)
                    #     self.data.put(dict(img=batch_imgs,
                    #                        meta=copy.deepcopy(batch_metas)))
                    #     put_data_timer.toc()

                    #     batch_imgs_list.clear()
                    #     batch_metas.clear()

        self.data.put('quit')
        producer_timer.toc()
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            producer_timer.name, producer_timer.avg * 1000,
            producer_timer.total))
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            read_img_timer.name, read_img_timer.avg * 1000,
            read_img_timer.total))
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            transpose_img_timer.name, transpose_img_timer.avg * 1000,
            transpose_img_timer.total))
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            crop_img_timer.name, crop_img_timer.avg * 1000,
            crop_img_timer.total))
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            copy_data_timer.name, copy_data_timer.avg * 1000,
            copy_data_timer.total))
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            put_data_timer.name, put_data_timer.avg * 1000,
            put_data_timer.total))
class BatchDataLoader(object):
    def __init__(self, image_dir, strides, patch_size, batch_size):
        self.image_dir = image_dir
        self.strides = strides
        self.patch_size = patch_size
        self.batch_size = batch_size
        self.image_names = os.listdir(image_dir)
        
        self.image_name = ''
        self.image_id = 0
        self.x_y_list = []

        self.reach_end = False
        self.name_shape = dict()
        self.total_slides = 0

        self.read_img_timer = Timer(name='read_img')
        self.copy_img_timer = Timer(name='copy_img')
        
    def next(self):
        if self.reach_end:
            return None
        batch_imgs = np.zeros((self.batch_size, 3, self.patch_size[1], self.patch_size[0]), dtype=np.uint8)
        batch_metas = [dict(image_name='None', x=0, y=0)] * self.batch_size

        for batch_idx in range(self.batch_size):
            if len(self.x_y_list) == 0:
                # success = self.read_image_and_update_info()
                success = self.read_image_and_update_info_gdal()

                if not success:
                    self.reach_end = True
                    return dict(img=batch_imgs, meta=batch_metas)

            x, y = self.x_y_list.pop(0)
            self.copy_img_timer.tic()
            batch_imgs[batch_idx] = self.image[:, 
                                               y : y + self.patch_size[1], 
                                               x : x + self.patch_size[0]]
            self.copy_img_timer.toc()
            # batch_imgs[batch_idx] = self.image.ReadAsArray(x, 
            #                                                y, 
            #                                                self.patch_size[0], 
            #                                                self.patch_size[1])

            batch_metas[batch_idx] = dict(image_name=self.image_name, x=x, y=y)


            # log.info('id: {}, image_name: {}, width: {}, height: {}, x: {}, y: {}'.format(
            #         self.image_id, self.image_name, self.image_shape[0], self.image_shape[1], x, y))
        return dict(img=batch_imgs, meta=batch_metas)


    def read_image_and_update_info(self):
        if self.image_id == len(self.image_names):
            return False

        image_name = self.image_names[self.image_id]
        image_path = os.path.join(self.image_dir, image_name)
        image = cv2.imread(image_path)  #bgr

        height, width, _ = image.shape
        image_shape = (width, height)
        # self.x_num, self.y_num, self.x_offset, self.y_offset = self.calc_split_num(image_shape, self.patch_size, self.strides)
        self.x_num, self.y_num = self.calc_split_num(image_shape, self.patch_size, self.strides)

        self.x_idx, self.y_idx = 0, 0 

        self.image = image.transpose((2, 0, 1))  # Change data layout from HWC to CHW
        self.image_name = image_name
        self.image_shape = image_shape
        self.image_id += 1

        self.x_idx_y_idx_list.clear()
        for i in range(self.x_num):
            for j in range(self.y_num):
                self.x_idx_y_idx_list.append((i, j))

        self.name_shape[image_name] = image_shape

        return True


    def read_image_and_update_info_gdal(self):
        if self.image_id == len(self.image_names):
            return False

        image_name = self.image_names[self.image_id]
        image_path = os.path.join(self.image_dir, image_name)
        
        # rgb
        self.read_img_timer.tic()
        image = gdal.Open(image_path, gdal.GA_ReadOnly).ReadAsArray()
        self.read_img_timer.toc()

        _, height, width = image.shape
        image_shape = (width, height)
        # width = image.RasterXSize   #栅格矩阵的列数
        # height = image.RasterYSize  #栅格矩阵的行数

        self.x_y_list = self.update_x_y_list(image_shape, self.patch_size, self.strides)

        self.image = image
        self.image_name = image_name
        self.image_shape = image_shape
        self.image_id += 1

        self.name_shape[image_name] = image_shape

        return True

    def update_x_y_list(self, image_shape, patch_size, strides):
        x_y_list = []
        drop_pixel = 400
        # drop_pixel = 0

        width, height = image_shape
        patch_w, patch_h = patch_size
        remain_w = width % patch_w
        remain_h = height % patch_h

        if remain_w < drop_pixel:
            x_offset = int(remain_w / 2)
            x_num = width // patch_w
            width -= remain_w
        else:
            x_offset = 0
            x_num = math.ceil((image_shape[0] - patch_size[0]) / strides[0]) + 1


        if remain_h < drop_pixel:
            # import pdb;pdb.set_trace()
            y_offset = int(remain_h / 2)
            y_num = height // patch_h
            height -= remain_h
        else:
            y_offset = 0
            y_num = math.ceil((image_shape[1] - patch_size[1]) / strides[1]) + 1

        for i in range(x_num):
            for j in range(y_num):
                x = x_offset + (strides[0] * i if i < x_num - 1 else width - patch_w)
                y = y_offset + (strides[1] * j if j < y_num - 1 else height - patch_h)
                x_y_list.append((x, y))

        self.total_slides += len(x_y_list)
        return x_y_list