Ejemplo n.º 1
0
class Inference:
    def __init__(self):
        self.net = ResNet(inference_mode=True)
        self.net.build(input_shape=(None, *IMAGE_SIZE, CHANNELS))
        self.net.load_weights(PATH_TO_MODEL_WEIGHT)
        self.net.summary()

        self.dataset = DataController(PATH_TO_TEST_DATA, True, draw_cloud=True)
        self.data_generator = self.dataset.get_eval_data_generator(
            shuffle=True)

    def __call__(self):
        for images, gt_labels in self.data_generator:
            scores = tf.nn.softmax(self.net(images)).numpy()
            for i in range(INFER_BATCH_SIZE):
                label = int(scores[i][0] < scores[i][1])
                yield 'ALERT' if label else 'PASS'
Ejemplo n.º 2
0
def main():
    if VISUALIZATION:
        window_manager = VisualManager('eval')

    dataset = DataController(PATH_TO_TEST_DATA, inference_mode=True)
    measure = YieldMeasure()
    data_generator = dataset.get_eval_data_generator(shuffle=True)

    net = ResNet(inference_mode=True)
    net.build(input_shape=(None, *IMAGE_SIZE, CHANNELS))
    net.load_weights(PATH_TO_MODEL_WEIGHT)
    net.summary()

    inf_time = 0
    inf_num = 0

    for images, gt_labels in data_generator:
        gt_labels = gt_labels.numpy()
        t0 = time()
        pred_labels = tf.nn.softmax(
            net(images)).numpy().argmax(axis=1).astype('int32')
        inf_time += (time() - t0)
        inf_num += 1

        measure.append(gt_labels, pred_labels)

        if VISUALIZATION:
            visual_batch = []
            for i in range(INFER_BATCH_SIZE):
                frame = (images[i] * 255).numpy().astype('uint8')
                window_manager.draw_annotations(frame, pred_labels[i],
                                                gt_labels[i],
                                                dataset.invert_class_dict)
                visual_batch.append(frame)
            window_manager.desktop_show(concatenate(visual_batch, axis=1), 0)
    measure.show_rating()
    print(f"Median inference time: {(inf_time / inf_num) * 1000:.1f} ms")