Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

encoding_decoding.py 3.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  1. import os
  2. import platform
  3. import statistics
  4. import torch
  5. import torch.utils.benchmark as benchmark
  6. import torchvision
  7. def print_machine_specs():
  8. print("Processor:", platform.processor())
  9. print("Platform:", platform.platform())
  10. print("Logical CPUs:", os.cpu_count())
  11. print(f"\nCUDA device: {torch.cuda.get_device_name()}")
  12. print(f"Total Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
  13. def get_data():
  14. transform = torchvision.transforms.Compose(
  15. [
  16. torchvision.transforms.PILToTensor(),
  17. ]
  18. )
  19. path = os.path.join(os.getcwd(), "data")
  20. testset = torchvision.datasets.Places365(
  21. root="./data", download=not os.path.exists(path), transform=transform, split="val"
  22. )
  23. testloader = torch.utils.data.DataLoader(
  24. testset, batch_size=1000, shuffle=False, num_workers=1, collate_fn=lambda batch: [r[0] for r in batch]
  25. )
  26. return next(iter(testloader))
  27. def run_encoding_benchmark(decoded_images):
  28. results = []
  29. for device in ["cpu", "cuda"]:
  30. decoded_images_device = [t.to(device=device) for t in decoded_images]
  31. for size in [1, 100, 1000]:
  32. for num_threads in [1, 12, 24]:
  33. for stmt, strat in zip(
  34. [
  35. "[torchvision.io.encode_jpeg(img) for img in decoded_images_device_trunc]",
  36. "torchvision.io.encode_jpeg(decoded_images_device_trunc)",
  37. ],
  38. ["unfused", "fused"],
  39. ):
  40. decoded_images_device_trunc = decoded_images_device[:size]
  41. t = benchmark.Timer(
  42. stmt=stmt,
  43. setup="import torchvision",
  44. globals={"decoded_images_device_trunc": decoded_images_device_trunc},
  45. label="Image Encoding",
  46. sub_label=f"{device.upper()} ({strat}): {stmt}",
  47. description=f"{size} images",
  48. num_threads=num_threads,
  49. )
  50. results.append(t.blocked_autorange())
  51. compare = benchmark.Compare(results)
  52. compare.print()
  53. def run_decoding_benchmark(encoded_images):
  54. results = []
  55. for device in ["cpu", "cuda"]:
  56. for size in [1, 100, 1000]:
  57. for num_threads in [1, 12, 24]:
  58. for stmt, strat in zip(
  59. [
  60. f"[torchvision.io.decode_jpeg(img, device='{device}') for img in encoded_images_trunc]",
  61. f"torchvision.io.decode_jpeg(encoded_images_trunc, device='{device}')",
  62. ],
  63. ["unfused", "fused"],
  64. ):
  65. encoded_images_trunc = encoded_images[:size]
  66. t = benchmark.Timer(
  67. stmt=stmt,
  68. setup="import torchvision",
  69. globals={"encoded_images_trunc": encoded_images_trunc},
  70. label="Image Decoding",
  71. sub_label=f"{device.upper()} ({strat}): {stmt}",
  72. description=f"{size} images",
  73. num_threads=num_threads,
  74. )
  75. results.append(t.blocked_autorange())
  76. compare = benchmark.Compare(results)
  77. compare.print()
  78. if __name__ == "__main__":
  79. print_machine_specs()
  80. decoded_images = get_data()
  81. mean_h, mean_w = statistics.mean(t.shape[-2] for t in decoded_images), statistics.mean(
  82. t.shape[-1] for t in decoded_images
  83. )
  84. print(f"\nMean image size: {int(mean_h)}x{int(mean_w)}")
  85. run_encoding_benchmark(decoded_images)
  86. encoded_images_cuda = torchvision.io.encode_jpeg([img.cuda() for img in decoded_images])
  87. encoded_images_cpu = [img.cpu() for img in encoded_images_cuda]
  88. run_decoding_benchmark(encoded_images_cpu)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...