123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import time
- import numpy
- import torch
- from torch.utils.data import Dataset
- from draugr import batched_recycle
- from draugr.torch_utilities import to_tensor_generator
- __author__ = "Christian Heider Nielsen"
- __doc__ = r"""
- Created on 28/10/2019
- """
- 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}")
- def test_d2():
- 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)
- generator = to_tensor_generator(
- batched_recycle(numpy.random.sample(data_shape), batch_size),
- device=device,
- preload_next=False,
- )
- dataloader = torch.utils.data.DataLoader(
- numpy.random.sample(data_shape),
- batch_size=batch_size,
- shuffle=True,
- num_workers=1,
- pin_memory=False,
- )
- for _ in range(samples):
- s1 = time.time()
- for _, a in zip(range(batches), generator):
- model(a)
- s2 = time.time()
- for _, a in zip(range(batches), dataloader):
- model(a.to(device, dtype=torch.float))
- s3 = time.time()
- print(f"generator: {s2 - s1}")
- print(f"dataloader: {s3 - s2}")
- def test_d3():
- 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)
- generator = to_tensor_generator(
- batched_recycle(numpy.random.sample(data_shape), batch_size), device=device
- )
- dataloader = torch.utils.data.DataLoader(
- numpy.random.sample(data_shape),
- batch_size=batch_size,
- shuffle=True,
- num_workers=4,
- pin_memory=True,
- )
- for _ in range(samples):
- s1 = time.time()
- for _, a in zip(range(batches), dataloader):
- model(a.to(device, dtype=torch.float))
- s2 = time.time()
- for _, a in zip(range(batches), generator):
- model(a)
- s3 = time.time()
- print(f"dataloader: {s2 - s1}")
- print(f"generator: {s3 - s2}")
- def test_d4():
- 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])
- 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)
- 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=False,
- )
- generator = to_tensor_generator(
- inner_map(
- a_transform, batched_recycle(numpy.random.sample(data_shape), batch_size)
- ),
- device=device,
- )
- for _ in range(samples):
- s1 = time.time()
- for _, a in zip(range(batches), dataloader):
- assert batch_shape == a.shape, a.shape
- model(a.to(device, dtype=torch.float))
- s2 = time.time()
- for _, a in zip(range(batches), generator):
- assert batch_shape == a.shape, a.shape
- model(a)
- s3 = time.time()
- print(f"dataloader: {s2 - s1}")
- print(f"generator: {s3 - s2}")
- def test_d5():
- 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=False,
- )
- generator = to_tensor_generator(
- inner_map(
- a_transform, batched_recycle(numpy.random.sample(data_shape), batch_size)
- ),
- device=device,
- )
- 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}")
- 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}")
- 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}")
- if __name__ == "__main__":
- test_d7()
|