コード例 #1
0
def test_d1():
    channels_in = 3
    channels_out = 3

    samples = 4
    device = "cuda"
    batches = 10
    batch_size = 32
    data_shape = (batches * batch_size, channels_in, 512, 512)

    model = torch.nn.Sequential(
        torch.nn.Conv2d(channels_in, channels_out, (3, 3)),
        torch.nn.ReLU(),
        torch.nn.Conv2d(channels_out, channels_out, (3, 3)),
        torch.nn.ReLU(),
        torch.nn.Conv2d(channels_out, channels_out, (3, 3)),
        torch.nn.ReLU(),
    ).to("cuda")

    for _ in range(samples):
        s1 = time.time()
        for _, a in zip(
                range(batches),
                to_tensor_generator(
                    batched_recycle(numpy.random.sample(data_shape),
                                    batch_size),
                    device=device,
                    preload_next=False,
                ),
        ):

            model(a)

        s2 = time.time()
        for _, a in zip(
                range(batches),
                to_tensor_generator(
                    batched_recycle(numpy.random.sample(data_shape),
                                    batch_size),
                    device=device,
                ),
        ):
            model(a)

        s3 = time.time()

        print(s2 - s1)
        print(s3 - s2)
コード例 #2
0
    def main(model_name: str = "maskrcnn_pennfudanped", score_threshold=0.55):
        base_path = PROJECT_APP_PATH.user_data / 'maskrcnn'
        dataset_root = Path.home() / "Data"

        torch_seed(3825)

        dataset = PennFudanDataset(dataset_root / "PennFudanPed",
                                   Split.Training)
        categories = dataset.categories

        if True:
            model = load_model(model_name=model_name,
                               model_directory=base_path / 'models')
        else:
            model = get_pretrained_instance_segmentation_maskrcnn(
                dataset.response_channels)

        model.to(global_torch_device())
        cpu_device = torch.device("cpu")

        with torch.no_grad():
            with TorchEvalSession(model):
                for image in tqdm(
                        to_tensor_generator(
                            frame_generator(cv2.VideoCapture(0)),
                            device=global_torch_device(),
                        )):
                    prediction = model(
                        # torch_vision_normalize_batch_nchw(
                        uint_hwc_to_chw_float_tensor(image).unsqueeze(0)
                        #    )
                    )[0]

                    (boxes, labels, scores) = (
                        prediction["boxes"].to(cpu_device).numpy(),
                        prediction["labels"].to(cpu_device).numpy(),
                        torch.sigmoid(
                            prediction["scores"]).to(cpu_device).numpy(),
                    )

                    indices = scores > score_threshold

                    cv2.namedWindow(model_name, cv2.WINDOW_NORMAL)
                    cv2.imshow(
                        model_name,
                        draw_bounding_boxes(
                            quick_to_pil_image(image),
                            boxes[indices],
                            labels=labels[indices],
                            scores=scores[indices],
                            categories=categories,
                        ))

                    if cv2.waitKey(1) == 27:
                        break  # esc to quit
コード例 #3
0
def test_d7():
    import numpy

    channels_in = 3

    samples = 10
    device = "cuda"
    batches = 3
    batch_size = 32
    data_shape = (batches * batch_size, 256, 256, channels_in)
    batch_shape = torch.Size([batch_size, 256, 256, channels_in])
    dtype = torch.float

    class RandomDataset(Dataset):
        """ """
        def __init__(self):
            self.d = numpy.random.sample(data_shape)

        def __len__(self):
            return len(self.d)

        def __getitem__(self, item):
            return self.d[item]

    dataloader = torch.utils.data.DataLoader(
        RandomDataset(),
        batch_size=batch_size,
        shuffle=True,
        num_workers=1,
        pin_memory=True,
    )

    generator = to_tensor_generator(
        batched_recycle(numpy.random.sample(data_shape), batch_size),
        device=device,
        preload_next=True,
        dtype=dtype,
    )

    for _ in range(samples):
        s1 = time.time()
        for _, a in zip(range(batches), generator):
            assert batch_shape == a.shape, a.shape

        s2 = time.time()

        for _, a in zip(range(batches), dataloader):
            a = a.to(device, dtype=dtype)
            assert batch_shape == a.shape, a.shape
        s3 = time.time()

        print(f"generator: {s2 - s1}")
        print(f"dataloader: {s3 - s2}")
コード例 #4
0
def test_d1():
    channels_in = 3
    channels_out = 3

    samples = 10
    device = "cuda"
    batches = 3
    batch_size = 32
    data_shape = (batches * batch_size, channels_in, 512, 512)

    model = torch.nn.Sequential(
        torch.nn.Conv2d(channels_in, channels_out, (3, 3)),
        torch.nn.ReLU(),
        torch.nn.Conv2d(channels_out, channels_out, (3, 3)),
        torch.nn.ReLU(),
        torch.nn.Conv2d(channels_out, channels_out, (3, 3)),
        torch.nn.ReLU(),
    ).to(device)

    for _ in range(samples):
        s1 = time.time()
        for _, a in zip(
                range(batches),
                to_tensor_generator(
                    batched_recycle(numpy.random.sample(data_shape),
                                    batch_size),
                    device=device,
                    preload_next=False,
                ),
        ):
            model(a)

        s2 = time.time()
        for _, a in zip(
                range(batches),
                torch.utils.data.DataLoader(
                    numpy.random.sample(data_shape),
                    batch_size=batch_size,
                    shuffle=True,
                    num_workers=1,
                    pin_memory=False,
                ),
        ):
            model(a.to(device, dtype=torch.float))

        s3 = time.time()

        print(f"generator: {s2 - s1}")
        print(f"dataloader: {s3 - s2}")
コード例 #5
0
ファイル: conversion.py プロジェクト: cnheider/draugr
    def asd2():
        """ """
        import cv2
        import torch
        from PIL import Image
        from tqdm import tqdm

        from draugr.opencv_utilities import frame_generator
        from draugr.torch_utilities import global_torch_device, to_tensor_generator

        with torch.no_grad():
            for image in tqdm(
                to_tensor_generator(
                    frame_generator(cv2.VideoCapture(0)),
                    device=global_torch_device(),
                )
            ):
                cv2.namedWindow("window_name", cv2.WINDOW_NORMAL)
                cv2.imshow("window_name", numpy.array(quick_to_pil_image(image)))

                if cv2.waitKey(1) == 27:
                    break  # esc to quit
コード例 #6
0
def test_d6():
    from torchvision.transforms import transforms
    import numpy
    from draugr import inner_map

    a_transform = transforms.Compose([
        transforms.ToPILImage("RGB"),
        transforms.Resize(224),
        transforms.CenterCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
    ])

    channels_in = 3
    channels_out = 3

    samples = 10
    device = "cuda"
    batches = 3
    batch_size = 32
    data_shape = (batches * batch_size, 256, 256, channels_in)
    batch_shape = torch.Size([batch_size, channels_in, 224, 224])

    class RandomDataset(Dataset):
        """ """
        def __init__(self):
            self.d = numpy.random.sample(data_shape)

        def __len__(self):
            return len(self.d)

        def __getitem__(self, item):
            return a_transform(self.d[item])

    dataloader = torch.utils.data.DataLoader(
        RandomDataset(),
        batch_size=batch_size,
        shuffle=True,
        num_workers=1,
        pin_memory=True,
    )

    generator = to_tensor_generator(
        inner_map(a_transform,
                  batched_recycle(numpy.random.sample(data_shape),
                                  batch_size)),
        device=device,
        preload_next=True,
    )

    for _ in range(samples):
        s1 = time.time()
        for _, a in zip(range(batches), generator):
            assert batch_shape == a.shape, a.shape

        s2 = time.time()

        for _, a in zip(range(batches), dataloader):
            assert batch_shape == a.shape, a.shape
        s3 = time.time()

        print(f"generator: {s2 - s1}")
        print(f"dataloader: {s3 - s2}")