示例#1
0
def process(network_info, images_dir, output_dir, threshold=0.8):
    session, input_tensor, output_tensor, img_size, cls_labels = load_from_xml(
        network_info)
    print("Input tensor: {}".format(input_tensor))
    print("Output tensor: {}".format(output_tensor))
    print("Image size: {}".format(img_size))
    print("Classes: {}".format(cls_labels))
    print()
    print("Parsing source directory... (this can take some time)")
    if img_size[2] == 3:
        img_type = 'rgb'
    else:
        img_type = 'greyscale'
    gen = InferenceGenerator(images_dir, 64, img_size, img_type)

    print("Files: {}".format(len(gen.filenames)))
    print("Batches: {}".format(len(gen)))

    workers = np.min((multiprocessing.cpu_count(), 8))
    print("Workers: {}".format(workers))
    print()

    filenames = []
    cls_index = []
    cls_names = []
    score = []
    enq = OrderedEnqueuer(gen, use_multiprocessing=True)
    enq.start(workers=workers, max_queue_size=multiprocessing.cpu_count() * 4)
    output_generator = enq.get()
    for i in range(len(gen)):
        print("\r{} / {}".format(i, len(gen)), end='')
        batch_filenames, batch_images = next(output_generator)
        result = session.run(output_tensor,
                             feed_dict={input_tensor: batch_images})
        cls = np.argmax(result, axis=1)
        scr = np.max(result, axis=1)
        cls_name = [cls_labels[i] for i in cls]
        filenames.extend(batch_filenames)
        cls_index.extend(cls)
        cls_names.extend(cls_name)
        score.extend(scr)
    enq.stop()
    print()
    print("Done")
    print("See {} for results".format(output_dir))

    parents = [Path(f).parent.name for f in filenames]
    files = [Path(f).name for f in filenames]

    df = pd.DataFrame(
        data={
            'filename': filenames,
            'parent': parents,
            'file': files,
            'class': cls_names,
            'class_index': cls_index,
            'score': score
        })
    os.makedirs(output_directory, exist_ok=True)
    df.to_csv(os.path.join(output_dir, "inference.csv"))
示例#2
0
    def predict_generator(self,
                          generator,
                          steps,
                          max_queue_size=10,
                          workers=1,
                          use_multiprocessing=False,
                          verbose=0):
        """Generates predictions for the input samples from a data generator.
        The generator should return the same kind of data as accepted by `predict_on_batch`.

        generator = DataGenerator class that returns:
            x = Input data as a 3D Tensor (batch_size, max_input_len, dim_features)
            x_len = 1D array with the length of each data in batch_size

        # Arguments
            generator: Generator yielding batches of input samples
                    or an instance of Sequence (tensorflow.keras.utils.Sequence)
                    object in order to avoid duplicate data
                    when using multiprocessing.
            steps:
                Total number of steps (batches of samples)
                to yield from `generator` before stopping.
            max_queue_size:
                Maximum size for the generator queue.
            workers: Maximum number of processes to spin up
                when using process based threading
            use_multiprocessing: If `True`, use process based threading.
                Note that because this implementation relies on multiprocessing,
                you should not pass non picklable arguments to the generator
                as they can't be passed easily to children processes.
            verbose:
                verbosity mode, 0 or 1.

        # Returns
            A numpy array(s) of predictions.

        # Raises
            ValueError: In case the generator yields
                data in an invalid format.
        """

        self.model_pred._make_predict_function()
        is_sequence = isinstance(generator, Sequence)

        allab_outs = []
        steps_done = 0
        enqueuer = None

        try:
            if is_sequence:
                enqueuer = OrderedEnqueuer(
                    generator, use_multiprocessing=use_multiprocessing)
            else:
                enqueuer = GeneratorEnqueuer(
                    generator, use_multiprocessing=use_multiprocessing)

            enqueuer.start(workers=workers, max_queue_size=max_queue_size)
            output_generator = enqueuer.get()

            if verbose == 1:
                progbar = Progbar(target=steps)

            while steps_done < steps:
                x = next(output_generator)
                outs = self.predict_on_batch(x)

                if not isinstance(outs, list):
                    outs = [outs]

                for i, out in enumerate(outs):
                    allab_outs.append([int(c) for c in out])

                steps_done += 1
                if verbose == 1:
                    progbar.update(steps_done)

        finally:
            if enqueuer is not None:
                enqueuer.stop()

        return allab_outs