Esempio n. 1
0
def annotate(flags, net, framework):
    log = get_logger()
    io = SharedFlagIO(flags, subprogram=True)
    flags = io.read_flags() if io.read_flags() is not None else flags
    for video in flags.video:
        frame_count = 0
        capture = cv2.VideoCapture(video)
        total_frames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
        annotation_file = f'{os.path.splitext(video)[0]}_annotations.csv'
        if os.path.exists(annotation_file):
            log.info("Overwriting existing annotations")
            os.remove(annotation_file)
        log.info(f'Annotating {video}')
        with open(annotation_file, mode='a') as file:
            file_writer = csv.writer(file,
                                     delimiter=',',
                                     quotechar='"',
                                     quoting=csv.QUOTE_MINIMAL)
            while capture.isOpened():
                frame_count += 1
                if frame_count % 10 == 0:
                    flags.progress = round((100 * frame_count / total_frames),
                                           0)
                    io.io_flags()
                ret, frame = capture.read()
                if ret:
                    frame = np.asarray(frame)
                    h, w, _ = frame.shape
                    im = framework.resize_input(frame)
                    this_inp = np.expand_dims(im, 0)
                    boxes = framework.findboxes(
                        np.concatenate(net(this_inp), 0))
                    pred = [
                        framework.process_box(b, h, w, flags.threshold)
                        for b in boxes
                    ]
                    pred = filter(None, pred)
                    time_elapsed = capture.get(cv2.CAP_PROP_POS_MSEC) / 1000
                    [
                        file_writer.writerow([time_elapsed, *result])
                        for result in pred
                    ]
                else:
                    break
                if flags.kill:
                    capture.release()
                    exit(1)
        capture.release()
Esempio n. 2
0
class TestBackend(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        open('tests/resources/checkpoint', 'w').close()
        with ZipFile('tests/resources/BCCD.v1-resize-416x416.voc.zip',
                     'r') as f:
            f.extractall('tests/resources/BCCD')
        time.sleep(5)

    def setUp(self):
        self.io = SharedFlagIO(subprogram=False)
        self.flags = self.io.flags

    def testBackendWrapperYoloV2(self):
        self.flags.model = 'tests/resources/yolov2-lite-3c.cfg'
        self.flags.dataset = 'tests/resources/BCCD/train'
        self.flags.labels = 'tests/resources/BCCD.classes'
        self.flags.annotation = 'tests/resources/BCCD/train'
        self.flags.backup = 'tests/resources/ckpt'
        self.flags.project_name = '_test'
        self.flags.trainer = 'adam'
        self.flags.lr = 0.00001
        self.flags.max_lr = 0.0001
        self.flags.step_size_coefficient = 10
        self.flags.load = 0
        self.flags.batch = 4
        self.flags.epoch = 1
        self.flags.train = True
        self.io.io_flags()
        proc = Popen([sys.executable, BACKEND_ENTRYPOINT],
                     stdout=PIPE,
                     shell=False)
        proc.communicate()
        self.assertEqual(proc.returncode, 0)
        self.flags.load = 1
        self.io.io_flags()
        proc = Popen([sys.executable, BACKEND_ENTRYPOINT],
                     stdout=PIPE,
                     shell=False)
        proc.communicate()
        self.flags.train = False
        self.flags.imgdir = 'tests/resources/BCCD/test'
        self.io.io_flags()
        proc = Popen([sys.executable, BACKEND_ENTRYPOINT],
                     stdout=PIPE,
                     shell=False)
        proc.communicate()
        self.flags.video = ['tests/resources/test.mp4']
        self.io.io_flags()
        proc = Popen([sys.executable, BACKEND_ENTRYPOINT],
                     stdout=PIPE,
                     shell=False)
        proc.communicate()
        self.assertEqual(proc.returncode, 0)

    def testBackendGradientExplosion(self):
        self.flags.model = 'tests/resources/yolov2-lite-3c.cfg'
        self.flags.dataset = 'tests/resources/BCCD/train'
        self.flags.labels = 'tests/resources/BCCD.classes'
        self.flags.annotation = 'tests/resources/BCCD/train'
        self.flags.backup = 'tests/resources/ckpt'
        self.flags.project_name = '_test'
        self.flags.trainer = 'adam'
        self.flags.lr = 100000000.0
        self.flags.max_lr = 100000000.0
        self.flags.load = 0
        self.flags.batch = 4
        self.flags.epoch = 1
        self.flags.train = True
        self.io.io_flags()
        proc = Popen([sys.executable, BACKEND_ENTRYPOINT],
                     stdout=PIPE,
                     shell=False)
        proc.communicate()
        self.assertNotEqual(proc.returncode, 0)

    def tearDown(self) -> None:
        self.io.cleanup_flags()

    @classmethod
    def tearDownClass(cls):
        for f in os.listdir('tests/resources/ckpt'):
            if f.endswith(
                ('.data-00000-of-00001', '.index', '.meta', '.profile')):
                f = os.path.join('tests/resources/ckpt', f)
                os.remove(f)
        os.remove('tests/resources/ckpt/checkpoint')
        rmtree('tests/resources/BCCD')
        try:
            rmtree('data/summaries/_test')
        except FileNotFoundError:
            pass
Esempio n. 3
0
import os
import sys

sys.path.append(os.getcwd())
from beagles.io.flags import SharedFlagIO
from beagles.backend.net import NetBuilder, train, predict, annotate

if __name__ == '__main__':
    io = SharedFlagIO(subprogram=True)
    flags = io.read_flags()
    flags.started = True
    net_builder = NetBuilder(flags=flags)
    net, framework, manager = net_builder()
    flags = io.read_flags()
    if flags.train:
        train(net_builder.annotation_data, net_builder.class_weights, flags,
              net, framework, manager)
    elif flags.video:
        annotate(flags, net, framework)
    else:
        predict(flags, net, framework)
    flags = io.read_flags()
    flags.progress = 100.0
    flags.done = True
    io.io_flags()
    exit(0)