-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_loader.py
208 lines (167 loc) · 7.17 KB
/
dataset_loader.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# native python dependencies
import pandas as pd
from PIL import Image
import numpy as np
import os
# torch dependencies
from torch.utils.data import Dataset, DataLoader
import torch
from torchvision import transforms
# custom dependencies
from create_dataset import create_dataset, create_data_with_labels_csv, create_dir
class LaserDataset(Dataset):
def __init__(self, csv_file, transform=None, denoise=False, add_noise=False):
"""
Args:
csv_file (str): Path to CSV file with image paths and labels
transform (callable, optional): Optional transform to be applied on a sample
(e.g., Fourier transform)
denoise (bool, optional): Optional denoising mechanism
add_noise (bool, optional): Optinal Gaussian noise injection
"""
self.data = pd.read_csv(csv_file)
self.denoise = denoise
self.add_noise = add_noise
self.transform = transform or transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5], std=[0.5])
]) #or transform
# Ensure the columns have correct types
self.data['angle'] = self.data['angle'].astype(float)
self.data['PP1'] = self.data['PP1'].astype(str)
self.data['NP'] = self.data['NP'].astype(int)
self.data['EP1'] = self.data['EP1'].astype(int)
# Map "PP1" to integer values for categorical encoding
self.pp1_mapping = {'V': 0, 'H': 1, 'D': 2}
self.data['PP1_encoded'] = self.data['PP1'].map(self.pp1_mapping)
def __len__(self):
return len(self.data)
def apply_fft_denoise(self, image, threshold=0.1):
f_transform = np.fft.fft2(image)
f_shift = np.fft.fftshift(f_transform)
mask = np.ones_like(f_shift)
mask[f_shift < threshold * np.max(np.abs(f_shift))] = 0
return np.real(np.fft.ifft2(np.fft.ifftshift(f_shift * mask)))
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
# Load image
img_path = self.data.iloc[idx]['image_path']
# Load image using PIL
with Image.open(img_path) as img:
image = img.convert('L') # Convert to grayscale by
#ITU-R 601-2 luma transform: L = R * 299/1000 + G * 587/1000 + B * 114/1000)
# Apply denoising if enabled
if self.denoise:
image = self.apply_fft_denoise(image)
# Add Gaussian noise if enabled
if self.add_noise:
noise = np.random.normal(0, 0.1, image.shape)
image = np.clip(image + noise, 0, 255)
# Ensure image is in PIL format before applying transforms
if self.transform:
image = self.transform(image)
# Get labels
labels = {
'angle': torch.tensor(self.data.iloc[idx]['angle'], dtype=torch.float32),
'PP1': torch.tensor(self.data.iloc[idx]['PP1_encoded'], dtype=torch.long),
'NP': torch.tensor(self.data.iloc[idx]['NP'], dtype=torch.int32),
'EP1': torch.tensor(self.data.iloc[idx]['EP1'], dtype=torch.int32)
}
# Convert labels to tensors
# labels = {k: torch.tensor(v, dtype=torch.float32) for k, v in labels.items()}
return image, labels
def prepare_and_load_data(input_dirs, base_path, output_dir_images, excel_path, csv_output_path,
cropped_dim=64, num_workers=4, batch_size=32):
"""
Complete pipeline for data preparation and loading by
creating DataLoader instances for training, validation and testing
Sets train,test,val dataloaders
"""
# Generate augmented dataset
create_dataset(input_dirs, output_dir_images, base_path, desired_dim=cropped_dim)
# Create CSV files with labels
excel_data = pd.read_excel(excel_path, engine = 'openpyxl')
create_data_with_labels_csv(excel_data, output_dir_images, csv_output_path)
# Create dataloaders
print("Creating DataLoaders...\n")
print("Training DataLoader...")
train_loader = DataLoader(
LaserDataset(os.path.join(csv_output_path, 'training_data.csv')),
batch_size=batch_size, shuffle=True, num_workers=num_workers)
print("Validation DataLoader...")
val_loader = DataLoader(
LaserDataset(os.path.join(csv_output_path, 'validation_data.csv')),
batch_size=batch_size, shuffle=False, num_workers=num_workers)
print("Testing DataLoader...")
test_loader = DataLoader(
LaserDataset(os.path.join(csv_output_path, 'testing_data.csv')),
batch_size=batch_size, shuffle=False, num_workers=num_workers)
#print(type(test_loader))
return train_loader, val_loader, test_loader
def test_laser_dataset(csv_path, batch_size=32, denoise=False, add_noise=False):
"""Quick test function for LaserDataset"""
# Verify CSV path
try:
print("CSV head:")
print(pd.read_csv(csv_path).head())
except FileNotFoundError:
print(f"Error: File not found at {csv_path}")
return None, None
# Initialize dataset and dataloader
dataset = LaserDataset(csv_path, denoise=denoise, add_noise=add_noise)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True) # row-wise sampling
# Test first batch
images, labels = next(iter(dataloader))
print(images.shape)
print(labels)
# Print results
print(f"Batch shape: {images.shape}")
print("\nLabel sample:")
for k, v in labels.items():
if v.dim() == 0: # Single value
print("dim of data is zero")
print(f"{k}: {v.item()}")
else: # Tensor
print(f"{k}: {v[0].item()}")
return images, labels
def main(check_loader: bool = False) -> None:
# Define paths
input_dirs = [
'2020-4-30 tuning ripple period',
'2020-6-9 Crossed polarized',
'Paper Data/Double pulses',
'Paper Data/Repetition 6p & 2p 29-4-2020',
'Paper Data/Single pulses 2p',
'Paper Data/Single pulses 4 and half 6',
'Paper Data/Repetition 6p & 2p 29-4-2020/Details'
]
base_path = "./images"
excel_path = "./images/all_images.xlsx"
csv_output_path = "./datasets/data_with_labels_csv"
dim = 64 # set dimensions of augmented images
images_path = f'./datasets/2023_im_dataset_{dim}x{dim}'
output_dir_images = create_dir(images_path)
# Run complete pipeline
# train_loader, val_loader, test_loader = prepare_and_load_data(
# input_dirs,
# base_path,
# output_dir_images,
# excel_path,
# csv_output_path,
# cropped_dim=dim
# )
prepare_and_load_data(
input_dirs,
base_path,
output_dir_images,
excel_path,
csv_output_path,
cropped_dim=dim
)
# test on testing_data.csv:
if check_loader:
csv_path = "./datasets/data_with_labels_csv/testing_data.csv"
images, labels = test_laser_dataset(csv_path, denoise=False, add_noise=False)
if __name__ == "__main__":
main(check_loader=False)