-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
44 lines (36 loc) · 1.31 KB
/
Copy pathutils.py
File metadata and controls
44 lines (36 loc) · 1.31 KB
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
# Standard library imports
import os
# Third-party imports
import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision.transforms as T
from PIL import Image
from thop import profile
from torch.utils.data import Dataset, random_split
def create_results_dir(base_dir):
class_dir = os.path.join(base_dir)
os.makedirs(class_dir, exist_ok=True)
return class_dir
# Function to print GPU usage
def print_gpu_usage():
print(f"Allocated: {torch.cuda.memory_allocated() / 1024**3:.2f} GB")
print(f"Cached: {torch.cuda.memory_reserved() / 1024**3:.2f} GB")
# Display sample images and their ground truth labels
def show_samples(dataloader, num_samples=5):
plt.figure(figsize=(15, num_samples * 5))
dataiter = iter(dataloader)
for i in range(num_samples):
images, labels, _ = next(dataiter)
for j in range(images.size(0)):
image = images[j].cpu().permute(1, 2, 0).numpy()
label = labels[j].cpu().numpy()
ax = plt.subplot(num_samples, 2, 2 * i + 1)
ax.imshow(image)
ax.set_title('Image')
ax.axis('off')
ax = plt.subplot(num_samples, 2, 2 * i + 2)
ax.imshow(label, cmap='gray')
ax.set_title('Ground Truth')
ax.axis('off')
plt.show()