-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemonstrate_augmentations.py
More file actions
347 lines (287 loc) · 12 KB
/
demonstrate_augmentations.py
File metadata and controls
347 lines (287 loc) · 12 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
# Create directory for visualization outputs
os.makedirs('/home/ubuntu/augmentation_demo', exist_ok=True)
# Function to load and crop a spectrogram image
def load_spectrogram(file_path):
"""Load a spectrogram image and crop to the spectrogram area."""
image = Image.open(file_path).convert('L')
# Crop to spectrogram area (removing axes, colorbar, etc.)
image = image.crop((80, 80, 800, 350))
return image
# Augmentation functions from our data preprocessing module
def time_shift(spectrogram, max_shift_percent=0.2):
"""Randomly shift the spectrogram in time."""
width = spectrogram.shape[1]
shift_amount = int(width * np.random.uniform(-max_shift_percent, max_shift_percent))
shifted = np.zeros_like(spectrogram)
if shift_amount > 0:
shifted[:, shift_amount:] = spectrogram[:, :width-shift_amount]
elif shift_amount < 0:
shifted[:, :width+shift_amount] = spectrogram[:, -shift_amount:]
else:
shifted = spectrogram
return shifted
def time_mask(spectrogram, max_mask_percent=0.2, num_masks=2):
"""Apply random masking in time dimension."""
width = spectrogram.shape[1]
masked = spectrogram.copy()
for _ in range(num_masks):
mask_width = int(width * np.random.uniform(0, max_mask_percent))
mask_start = np.random.randint(0, width - mask_width)
masked[:, mask_start:mask_start + mask_width] = 0
return masked
def freq_mask(spectrogram, max_mask_percent=0.2, num_masks=2):
"""Apply random masking in frequency dimension."""
height = spectrogram.shape[0]
masked = spectrogram.copy()
for _ in range(num_masks):
mask_height = int(height * np.random.uniform(0, max_mask_percent))
mask_start = np.random.randint(0, height - mask_height)
masked[mask_start:mask_start + mask_height, :] = 0
return masked
def freq_shift(spectrogram, max_shift_percent=0.2):
"""Randomly shift the spectrogram in frequency."""
height = spectrogram.shape[0]
shift_amount = int(height * np.random.uniform(-max_shift_percent, max_shift_percent))
shifted = np.zeros_like(spectrogram)
if shift_amount > 0:
shifted[shift_amount:, :] = spectrogram[:height-shift_amount, :]
elif shift_amount < 0:
shifted[:height+shift_amount, :] = spectrogram[-shift_amount:, :]
else:
shifted = spectrogram
return shifted
def amplitude_scale(spectrogram, min_factor=0.5, max_factor=1.5):
"""Randomly scale the amplitude of the spectrogram."""
scale_factor = np.random.uniform(min_factor, max_factor)
return spectrogram * scale_factor
def add_gaussian_noise(spectrogram, max_noise_percent=0.1):
"""Add random Gaussian noise to the spectrogram."""
noise_level = np.random.uniform(0, max_noise_percent)
noise = np.random.normal(0, noise_level * np.mean(spectrogram), spectrogram.shape)
return spectrogram + noise
def apply_augmentations(spectrogram, p=0.5):
"""Apply a random combination of augmentations to the spectrogram."""
augmented = spectrogram.copy()
# Time-domain augmentations
if np.random.random() < p:
augmented = time_shift(augmented)
if np.random.random() < p:
augmented = time_mask(augmented)
# Frequency-domain augmentations
if np.random.random() < p:
augmented = freq_shift(augmented)
if np.random.random() < p:
augmented = freq_mask(augmented)
# Intensity augmentations
if np.random.random() < p:
augmented = amplitude_scale(augmented)
if np.random.random() < p:
augmented = add_gaussian_noise(augmented)
return augmented
# Function to demonstrate SimCLR augmentation pairs
def demonstrate_simclr_pairs(file_path, num_pairs=5):
"""Generate multiple SimCLR augmentation pairs from a single spectrogram."""
# Load and convert image to numpy array
image = load_spectrogram(file_path)
spectrogram = np.array(image)
# Create figure for visualization
plt.figure(figsize=(15, num_pairs * 4))
# Original spectrogram
plt.subplot(num_pairs + 1, 2, 1)
plt.imshow(spectrogram, aspect='auto', cmap='viridis')
plt.title('Original Spectrogram')
plt.colorbar()
plt.subplot(num_pairs + 1, 2, 2)
plt.imshow(spectrogram, aspect='auto', cmap='viridis')
plt.title('Original Spectrogram')
plt.colorbar()
# Generate augmentation pairs
for i in range(num_pairs):
# Create two different augmentations of the same spectrogram
augmented1 = apply_augmentations(spectrogram, p=0.8)
augmented2 = apply_augmentations(spectrogram, p=0.8)
# Plot the pair
plt.subplot(num_pairs + 1, 2, 2*i + 3)
plt.imshow(augmented1, aspect='auto', cmap='viridis')
plt.title(f'Augmented View 1 - Pair {i+1}')
plt.colorbar()
plt.subplot(num_pairs + 1, 2, 2*i + 4)
plt.imshow(augmented2, aspect='auto', cmap='viridis')
plt.title(f'Augmented View 2 - Pair {i+1}')
plt.colorbar()
plt.tight_layout()
# Extract category from filename for saving
filename = os.path.basename(file_path)
category = "unknown"
if filename.startswith('env_noise'):
category = 'env_noise'
elif 'bio_signal_whale' in filename:
category = 'bio_whale'
elif 'bio_signal_fish' in filename:
category = 'bio_fish'
elif 'bio_signal_coral' in filename:
category = 'bio_coral'
elif 'manmade_boat' in filename:
category = 'manmade_boat'
elif 'manmade_ship' in filename:
category = 'manmade_ship'
elif 'manmade_submarine' in filename:
category = 'manmade_submarine'
elif 'manmade_speedboat' in filename:
category = 'manmade_speedboat'
elif 'transient' in filename:
category = 'transient'
# Save the figure
plt.savefig(f'/home/ubuntu/augmentation_demo/simclr_pairs_{category}.png')
plt.close()
return f'/home/ubuntu/augmentation_demo/simclr_pairs_{category}.png'
# Function to demonstrate individual augmentations
def demonstrate_individual_augmentations(file_path):
"""Show the effect of each individual augmentation type."""
# Load and convert image to numpy array
image = load_spectrogram(file_path)
spectrogram = np.array(image)
# Create figure for visualization
plt.figure(figsize=(15, 15))
# Original spectrogram
plt.subplot(4, 2, 1)
plt.imshow(spectrogram, aspect='auto', cmap='viridis')
plt.title('Original Spectrogram')
plt.colorbar()
# Time shift
plt.subplot(4, 2, 2)
shifted = time_shift(spectrogram, max_shift_percent=0.3)
plt.imshow(shifted, aspect='auto', cmap='viridis')
plt.title('Time Shift')
plt.colorbar()
# Time mask
plt.subplot(4, 2, 3)
time_masked = time_mask(spectrogram, max_mask_percent=0.2, num_masks=3)
plt.imshow(time_masked, aspect='auto', cmap='viridis')
plt.title('Time Masking')
plt.colorbar()
# Frequency mask
plt.subplot(4, 2, 4)
freq_masked = freq_mask(spectrogram, max_mask_percent=0.2, num_masks=3)
plt.imshow(freq_masked, aspect='auto', cmap='viridis')
plt.title('Frequency Masking')
plt.colorbar()
# Frequency shift
plt.subplot(4, 2, 5)
freq_shifted = freq_shift(spectrogram, max_shift_percent=0.3)
plt.imshow(freq_shifted, aspect='auto', cmap='viridis')
plt.title('Frequency Shift')
plt.colorbar()
# Amplitude scaling
plt.subplot(4, 2, 6)
scaled = amplitude_scale(spectrogram, min_factor=0.3, max_factor=1.8)
plt.imshow(scaled, aspect='auto', cmap='viridis')
plt.title('Amplitude Scaling')
plt.colorbar()
# Gaussian noise
plt.subplot(4, 2, 7)
noisy = add_gaussian_noise(spectrogram, max_noise_percent=0.2)
plt.imshow(noisy, aspect='auto', cmap='viridis')
plt.title('Gaussian Noise')
plt.colorbar()
# Combined augmentations
plt.subplot(4, 2, 8)
combined = apply_augmentations(spectrogram, p=1.0) # Apply all augmentations
plt.imshow(combined, aspect='auto', cmap='viridis')
plt.title('Combined Augmentations')
plt.colorbar()
plt.tight_layout()
# Extract category from filename for saving
filename = os.path.basename(file_path)
category = "unknown"
if filename.startswith('env_noise'):
category = 'env_noise'
elif 'bio_signal_whale' in filename:
category = 'bio_whale'
elif 'bio_signal_fish' in filename:
category = 'bio_fish'
elif 'bio_signal_coral' in filename:
category = 'bio_coral'
elif 'manmade_boat' in filename:
category = 'manmade_boat'
elif 'manmade_ship' in filename:
category = 'manmade_ship'
elif 'manmade_submarine' in filename:
category = 'manmade_submarine'
elif 'manmade_speedboat' in filename:
category = 'manmade_speedboat'
elif 'transient' in filename:
category = 'transient'
# Save the figure
plt.savefig(f'/home/ubuntu/augmentation_demo/individual_augmentations_{category}.png')
plt.close()
return f'/home/ubuntu/augmentation_demo/individual_augmentations_{category}.png'
# Function to demonstrate augmentations across different signal types
def demonstrate_augmentations_across_categories(data_dir):
"""Show how augmentations affect different types of underwater acoustic signals."""
# Get all spectrogram files
all_files = [f for f in os.listdir(data_dir) if f.endswith('.png')]
# Categorize files
categories = {
'env_noise': [],
'bio_whale': [],
'bio_fish': [],
'bio_coral': [],
'manmade_boat': [],
'manmade_ship': [],
'manmade_submarine': [],
'manmade_speedboat': [],
'transient': []
}
for file in all_files:
if file.startswith('env_noise'):
categories['env_noise'].append(file)
elif 'bio_signal_whale' in file:
categories['bio_whale'].append(file)
elif 'bio_signal_fish' in file:
categories['bio_fish'].append(file)
elif 'bio_signal_coral' in file:
categories['bio_coral'].append(file)
elif 'manmade_boat' in file:
categories['manmade_boat'].append(file)
elif 'manmade_ship' in file:
categories['manmade_ship'].append(file)
elif 'manmade_submarine' in file:
categories['manmade_submarine'].append(file)
elif 'manmade_speedboat' in file:
categories['manmade_speedboat'].append(file)
elif 'transient' in file:
categories['transient'].append(file)
# Sample one file from each non-empty category
sample_files = []
for category, files in categories.items():
if files: # If category is not empty
sample_files.append(os.path.join(data_dir, random.choice(files)))
# Demonstrate augmentations for each sample
simclr_demo_files = []
individual_demo_files = []
for file_path in sample_files:
simclr_demo_file = demonstrate_simclr_pairs(file_path, num_pairs=3)
individual_demo_file = demonstrate_individual_augmentations(file_path)
simclr_demo_files.append(simclr_demo_file)
individual_demo_files.append(individual_demo_file)
return simclr_demo_files, individual_demo_files
# Main function to run the demonstration
def main():
data_dir = '/home/ubuntu/data'
print("Demonstrating SimCLR augmentations for underwater acoustic spectrograms...")
simclr_demo_files, individual_demo_files = demonstrate_augmentations_across_categories(data_dir)
print("SimCLR augmentation pair demonstrations saved to:")
for file in simclr_demo_files:
print(f" - {file}")
print("\nIndividual augmentation demonstrations saved to:")
for file in individual_demo_files:
print(f" - {file}")
print("\nDemonstration complete!")
if __name__ == "__main__":
main()