-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference_modules.py
More file actions
126 lines (103 loc) · 3.73 KB
/
Copy pathinference_modules.py
File metadata and controls
126 lines (103 loc) · 3.73 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import matplotlib.pyplot as plt
import os
import math
import random
from PIL import Image
import blobfile as bf
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision.transforms import ToTensor, ToPILImage, Compose
from torchvision import transforms
import cv2
import numpy as np
import matplotlib.pyplot as plt
import re
from PIL import Image
import torch.nn as nn
import pandas as pd
def visualize_masks_and_images(masks, images):
masks = np.array(masks)
images = np.array(images)
if masks.shape[0] != images.shape[0]:
raise ValueError("Mask & Image NumError")
num = masks.shape[0]
if masks.ndim == 4 and masks.shape[-1] == 1:
masks = masks.squeeze(-1)
if masks.ndim == 4 and masks.shape[1] == 1:
masks = masks.squeeze(1)
if images.ndim == 4 and images.shape[-1] in [1, 3]:
pass
elif images.ndim == 3:
pass
else:
raise ValueError("Image's shape Error")
fig, axes = plt.subplots(2, num, figsize=(num * 2, 4))
for i in range(num):
# First Row: Masks
ax = axes[0, i]
ax.imshow(masks[i], cmap='gray')
ax.axis('off')
title = "Ground Truth" if (i == 0 or i == num-1) else f"Mask {i}"
ax.set_title(title)
# Second Row: Images
ax = axes[1, i]
if images.ndim == 4 and images.shape[-1] == 3:
ax.imshow(images[i])
elif images.ndim == 4 and images.shape[-1] == 1:
ax.imshow(images[i].squeeze(-1), cmap='gray')
else:
ax.imshow(images[i], cmap='gray')
ax.axis('off')
title = "Ground Truth" if (i == 0 or i == num-1) else f"Image {i}"
ax.set_title(title)
# Visualize the results in a single row
def visualize_slices_in_row(interpolated_masks):
"""
Visualize all interpolated masks in a single row.
Args:
interpolated_masks (ndarray): 3D array of interpolated masks (r, c, num_slices).
Returns:
None
"""
num_slices = interpolated_masks.shape[0] # Number of slices
plt.figure(figsize=(15, 5)) # Adjust the figure size based on number of slices
for i in range(num_slices):
plt.subplot(1, num_slices, i + 1) # 1 row, num_slices columns
plt.imshow(interpolated_masks[i].reshape(interpolated_masks.shape[1],interpolated_masks.shape[2]), cmap='gray')
plt.title(f"Slice {i+1}")
plt.axis('off') # Hide axes for better visualization
plt.tight_layout()
plt.show()
import blobfile as bf
def list_image_files_recursively(data_dir, key=None):
results = []
for entry in sorted(bf.listdir(data_dir), key=key):
full_path = bf.join(data_dir, entry)
ext = entry.split(".")[-1]
if "." in entry and ext.lower() in ["jpg", "jpeg", "png", "gif"]:
results.append(full_path)
elif bf.isdir(full_path):
results.extend(_list_image_files_recursively(full_path))
return results
def extract_number(filename):
numbers = re.findall(r'\d+', filename)
if numbers:
return int(numbers[0])
return 0
def process_directory_fortestmask(image_path):
image = cv2.resize(cv2.imread(image_path), (256, 256))[:, :, 1]
_, image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
image_array = np.array(image)
return image_array
mask_preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
def interpolated_mask_preprocess(interpolated_masks):
test_mask = []
for i in range(len(interpolated_masks)):
tensor = mask_preprocess(interpolated_masks[i].astype(float))
test_mask.append(tensor)
test_masks = torch.stack(test_mask)
return test_masks