|
133 | 133 | # Let's look at the frames decoded by CUDA decoder and compare them |
134 | 134 | # against equivalent results from the CPU decoders. |
135 | 135 | import matplotlib.pyplot as plt |
| 136 | +from torchvision.transforms.v2.functional import to_pil_image |
136 | 137 |
|
137 | 138 |
|
138 | 139 | def get_frames(timestamps: list[float], device: str): |
139 | 140 | decoder = VideoDecoder(video_file, device=device) |
140 | | - return [decoder.get_frame_played_at(ts) for ts in timestamps] |
141 | | - |
142 | | - |
143 | | -def get_numpy_images(frames): |
144 | | - numpy_images = [] |
145 | | - for frame in frames: |
146 | | - # We transfer to the CPU so they can be visualized by matplotlib. |
147 | | - numpy_image = frame.data.to("cpu").permute(1, 2, 0).numpy() |
148 | | - numpy_images.append(numpy_image) |
149 | | - return numpy_images |
| 141 | + return [decoder.get_frame_played_at(ts).data for ts in timestamps] |
150 | 142 |
|
151 | 143 |
|
152 | 144 | timestamps = [12, 19, 45, 131, 180] |
153 | 145 | cpu_frames = get_frames(timestamps, device="cpu") |
154 | 146 | cuda_frames = get_frames(timestamps, device="cuda:0") |
155 | | -cpu_numpy_images = get_numpy_images(cpu_frames) |
156 | | -cuda_numpy_images = get_numpy_images(cuda_frames) |
157 | 147 |
|
158 | 148 |
|
159 | | -def plot_cpu_and_cuda_images(): |
| 149 | +def plot_cpu_and_cuda_frames( |
| 150 | + cpu_frames: list[torch.Tensor], cuda_frames: list[torch.Tensor] |
| 151 | +): |
160 | 152 | n_rows = len(timestamps) |
161 | 153 | fig, axes = plt.subplots(n_rows, 2, figsize=[12.8, 16.0]) |
162 | 154 | for i in range(n_rows): |
163 | | - axes[i][0].imshow(cpu_numpy_images[i]) |
164 | | - axes[i][1].imshow(cuda_numpy_images[i]) |
| 155 | + axes[i][0].imshow(to_pil_image(cpu_frames[i].to("cpu"))) |
| 156 | + axes[i][1].imshow(to_pil_image(cuda_frames[i].to("cpu"))) |
165 | 157 |
|
166 | | - axes[0][0].set_title("CPU decoder") |
167 | | - axes[0][1].set_title("CUDA decoder") |
| 158 | + axes[0][0].set_title("CPU decoder", fontsize=24) |
| 159 | + axes[0][1].set_title("CUDA decoder", fontsize=24) |
168 | 160 | plt.setp(axes, xticks=[], yticks=[]) |
169 | 161 | plt.tight_layout() |
170 | 162 |
|
171 | 163 |
|
172 | | -plot_cpu_and_cuda_images() |
| 164 | +plot_cpu_and_cuda_frames(cpu_frames, cuda_frames) |
173 | 165 |
|
174 | 166 | # %% |
175 | 167 | # |
|
0 commit comments