-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
439 lines (365 loc) · 14.3 KB
/
functions.py
File metadata and controls
439 lines (365 loc) · 14.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"""
Author: Lee Taylor
functions.py : contains functions to support training and testing the model
"""
import os
import cv2
import torch
import random
import numpy as np
import pandas as pd
from os import listdir
from collections import deque
import scipy.ndimage as ndimage
from itertools import zip_longest
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def unpack_video(video_path):
"""
Unpack a video into numpy arrays representing the pixels in each frame.
"""
# Open the video file.
cap = cv2.VideoCapture(video_path)
# Check if the video was opened correctly.
if not cap.isOpened():
print(f"Error: Could not open video: {video_path}")
return None
frames = []
while True:
# Read the next frame.
ret, frame = cap.read()
# If the frame was not read correctly, we have reached the end of the video.
if not ret:
break
# Resize the frame.
frame = cv2.resize(frame, (112, 112))
# Append the frame (a numpy array) to the list of frames.
frames.append(frame)
# Close the video file.
cap.release()
# Convert the list of frames to a single numpy array.
video_data = np.stack(frames)
return video_data
def get_continuous_frames(video_data, frame_index=94, T=32):
"""
Given a video stored in a numpy array, selects T continuous frames
from the frame index where the index frame becomes the center of the
selected sequence. If T is even, the center will be the frame at index
`frame_index - T//2 + 1`.
If the frame_index is too close to the start or end of the video for
the sequence to fit, the sequence will start or end with the video
respectively.
Args:
video_data (numpy.array): The video data.
frame_index (int): The frame index.
T (int): The number of frames to select.
Returns:
sequence (numpy.array): The selected sequence of frames.
"""
half_T = T // 2
start_index = max(0, frame_index - half_T + T % 2)
end_index = min(len(video_data), start_index + T)
start_index = max(0, end_index - T) # adjust start if end is beyond video length
# print(f"DEBUG: start_index = {start_index}, end_index = {end_index}")
sequence = video_data[int(start_index):int(end_index)]
return sequence
def return_image_fns_dict(sanity_check=False):
"""
Sort frame name into dictionary/object
Video name, continuous frames, etc.etc
:return: dict ->
('0', {'image_no': '0', 'video_no': '1', 'scan_dir': 'r', 'view_': 't',
'y_actual': 'b', 'temporal_index': '94', 'video_filename': '1_r_t_b.avi'})
('10', {'image_no': '10', 'video_no': '1', 'scan_dir': 'r', 'view_': 't',
'y_actual': 'b', 'temporal_index': '89', 'video_filename': '1_r_t_b.avi'})
...
('9', {'image_no': '9', 'video_no': '0', 'scan_dir': 'l', 'view_': 't',
'y_actual': 'b', 'temporal_index': '40', 'video_filename': '0_l_t_b.avi'})
"""
try:
image_fns = listdir("data/data/images")
except FileNotFoundError as e:
image_fns = listdir("../data/data/images")
image_fns_dict = {}
# Loop over image file names
for image_fn in image_fns:
# Only process .jpg files
if image_fn.__contains__(".json"):
continue
# Preprocess frame information from frame name
numbers = image_fn.replace('.jpg', '')
n = numbers = numbers.split('_')
# Acquire frame information from frame name
temp_dict = {"image_no": numbers[0],
"video_no": numbers[1],
"scan_dir": numbers[2],
"view_": numbers[3],
"y_actual": numbers[4],
"temporal_index": numbers[5],
"video_filename": f"{n[1]}_{n[2]}_{n[3]}_{n[4]}.avi"}
image_fns_dict.update({numbers[0]: temp_dict.copy()})
# Dictionary sanity check
if sanity_check:
print("---[image_fns_dict]---")
for item in image_fns_dict.items():
print(item)
print()
# End function
return image_fns_dict
def return_video_fns_dict(sanity_check=False):
"""
Convert video file .avi into frames
Extract 30 to 32 frames near keyframe using temporal index
:return: dict ->
('0', {'video_no': '0', 'scan_dir': 'r', 'view_': 't', 'y_actual': 'b'})
...
('9', {'video_no': '9', 'scan_dir': 'l', 'view_': 't', 'y_actual': 'b'})
"""
try:
video_fns = listdir("data/data/videos")
except FileNotFoundError as e:
video_fns = listdir("../data/data/videos")
video_fns_dict = {}
# Loop over image file names
for video_fn in video_fns:
# Only process .jpg files
if video_fn.__contains__(".json"):
continue
# Preprocess frame information from frame name
numbers = video_fn.replace('.avi', '')
numbers = numbers.split('_')
# Acquire frame information from frame name
temp_dict = {
"video_no": numbers[0],
"scan_dir": numbers[1],
"view_": numbers[2],
"y_actual": numbers[3]
}
video_fns_dict.update({numbers[0]: temp_dict.copy()})
# Dictionary sanity check
if sanity_check:
print("---[video_fns_dict]---")
for item in video_fns_dict.items():
print(item)
print()
return video_fns_dict
def data_augment(data_):
""" Randomly apply one of the augmentation techniques to the 32 frames of ultrasound data_."""
# Randomly select an augmentation method
augment_methods = [random_flip_3d, random_rotation_3d, random_intensity_shift]
augment_method = random.choice(augment_methods)
data_augmented = None
# Apply selected augmentation method
if augment_method == random_flip_3d:
data_augmented = augment_method(data_)
elif augment_method == random_rotation_3d:
max_angle = 20 # You can adjust this parameter
data_augmented = augment_method(data_, max_angle)
elif augment_method == random_intensity_shift:
max_offset = 0.1 # You can adjust this parameter
max_scale_delta = 0.2 # You can adjust this parameter
data_augmented = augment_method(data_, max_offset, max_scale_delta)
return data_augmented
def random_flip_3d(volume):
"""
Randomly flip volume across different dimensions. Volume input should be in
format (frames, width, height, channels)
"""
if random.choice([True, False]):
volume = volume[::-1, :, :, :] # flip along frames
if random.choice([True, False]):
volume = volume[:, ::-1, :, :] # flip along width
if random.choice([True, False]):
volume = volume[:, :, ::-1, :] # flip along height
return volume
def random_rotation_3d(volume, max_angle):
"""
Randomly rotate volume along frames dimension. Volume input should be in
format (frames, width, height, channels)
"""
angle = random.uniform(-max_angle, max_angle)
volume_rot = np.empty(volume.shape, dtype=np.float32)
# Apply rotation to each frame
for i in range(volume.shape[0]):
for j in range(volume.shape[3]):
volume_rot[i, :, :, j] = ndimage.rotate(volume[i, :, :, j], angle, axes=(0, 1), reshape=False)
return volume_rot
def random_intensity_shift(volume, max_offset, max_scale_delta):
"""
Randomly shift intensity of volume. Volume input should be in
format (frames, width, height, channels)
"""
offset = random.uniform(-max_offset, max_offset)
scale = random.uniform(1 - max_scale_delta, 1 + max_scale_delta)
volume = volume.astype('float64') # Convert volume to float64
volume += offset
volume *= scale
return volume
class MyDataset(torch.utils.data.IterableDataset):
"""
Yield data for training purposes.
"""
def __init__(self, generator_function):
super(MyDataset).__init__()
self.generator_function = generator_function
def __iter__(self):
return self.generator_function()
def dataloader_test():
"""
Organise data for the training pipeline.
:yield: input (1, 32, 112, 112, 3), labels [1.0, 0.0]
"""
# Load image and video dictionaries
image_dict = return_image_fns_dict()
video_dict = return_video_fns_dict()
test_set_numbers = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
all_numbers = [x for x in range(50)]
for number in all_numbers:
if number not in test_set_numbers:
try:
del image_dict[str(number)]
except KeyError as e:
pass
print(f"image_dict.keys() = {list(image_dict.keys())}")
# for item in list(image_dict.values())[:1]:
# print(video_dict[item["video_no"]])
# print('_'.join(list(video_dict[item["video_no"]].values())) + '.avi')
"""
Write a function using an image filename to get the corresponding video
and unpack the 32 frames using the key frame from the image filename dictionary.
image_number, image_key_frame_index -> video_number -> 32_frames
"""
for item in image_dict.values():
# item = {'image_no': '0', 'video_no': '1', ..., 'temporal_index': '94', 'video_filename': '1_r_t_b.avi'}
video_name = '_'.join(list(video_dict[item["video_no"]].values())) + '.avi'
video_data = unpack_video(f'data/data/videos/{video_name}')
data = get_continuous_frames(video_data, frame_index=int(item["temporal_index"]))
data = np.expand_dims(data, axis=0)
data = np.transpose(data, (0, 4, 1, 2, 3))
# Calculate labels, b = benign, m = malignant, [b, m] -> if b : [1, 0] ? [0, 1]
labels = [0.0, 0.0]
if item["y_actual"] == 'b':
labels[0] = 1.0
else:
labels[1] = 1.0
labels = torch.tensor(labels)
# Pass C3D input and labels
data = torch.from_numpy(data).float()
yield data, labels
def video_names():
"""
:return: list of video names
"""
path = "data/data/videos"
return os.listdir(path)
def get_video_names_dict():
"""
Return a dictionary which contains key video number and video name.
"""
rv = {}
names = video_names()
for i in range(50):
for name in names:
# if name.__contains__(str(i)):
if name.split('_')[0] == str(i):
rv.update({str(i): name})
continue
return rv
def video_keyframe():
"""
Yield video number, key frame number.
"""
# Read data
file = "data/data/avi.xlsx"
df = pd.read_excel(file)
columns = df[['d', 'g', 'j', 'm']]
# Get the dictionary of video names {'0': '0_r_t_b.avi', ...}
video_names_dict = get_video_names_dict()
# Skip videos belonging to the test set
test_set_numbers = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
for number in test_set_numbers:
try:
del video_names_dict[str(number)]
except KeyError:
pass
# Sort the video_keys into class 1 (malignant) and class 0 (benign)
class_1_keys = []
class_0_keys = []
for index, row in columns.iterrows():
if index in test_set_numbers:
continue
for item in row:
if not str(item) == 'nan':
video_name = video_names_dict.get(str(index), '')
if video_name.split('_')[-1][0] == 'b':
class_0_keys.append((index, item))
elif video_name.split('_')[-1][0] == 'm':
class_1_keys.append((index, item))
# Calculate the difference in lengths
length_diff = abs(len(class_0_keys) - len(class_1_keys))
for _ in range(length_diff):
random_var = random.randint(0, len(class_1_keys) - 1)
class_1_keys.append(class_1_keys[random_var])
for _ in range(3):
class_1_keys.extend(class_1_keys)
class_0_keys.extend(class_0_keys)
print(f"len(c1) = {len(class_1_keys)}")
print(f"len(c0) = {len(class_0_keys)}")
# Iterate through class_1_keys and class_0_keys in an alternating fashion
class_1_keys_iter = iter(class_1_keys)
class_0_keys_iter = iter(class_0_keys)
while True:
try:
yield next(class_1_keys_iter)
except StopIteration:
break
try:
yield next(class_0_keys_iter)
except StopIteration:
break
def dataloader_augment():
"""
For each yield from the video_keyframe function, opens the video
and gets the 32-frame sequence. The video number corresponds to the video
name from the video_names function.
:yield: input (1, 32, 112, 112, 3), labels [1.0, 0.0]
"""
# Get the dictionary of video names {'0': '0_r_t_b.avi', ...}
video_names_dict = get_video_names_dict()
# Skip videos belonging to the test set
test_set_numbers = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
for number in test_set_numbers:
try:
del video_names_dict[str(number)]
except KeyError:
pass
for video_num, key_frame in video_keyframe():
# Get the name of the video.
try:
video_name = video_names_dict[str(video_num)]
except KeyError:
continue
video_path = os.path.join("data/data/videos", video_name)
# Unpack the video into frames.
video_data = unpack_video(video_path)
if video_data is None:
print(f"video_data skip = {video_data}")
continue # Skip to next video if current video couldn't be opened.
# Get the 32-frame sequence.
data = get_continuous_frames(video_data, frame_index=key_frame)
# Calculate labels, b = benign, m = malignant, [b, m] -> if b : [1, 0] ? [0, 1]
labels = [0.0, 0.0]
# print(f"{video_name.split('_')[-1][0]} = video_name.split('_')[-1][0]")
if video_name.split('_')[-1][0] == 'b':
labels[0] = 1.0
else:
labels[1] = 1.0
labels = torch.tensor(labels)
# Apply random modifications and iterate over the same 32 frames 5 times
modified_data = data_augment(data)
modified_data = np.transpose(modified_data, (3, 0, 1, 2))
# Normalize data to be between 0 and 1
modified_data = (modified_data - np.min(modified_data)) / (np.max(modified_data) - np.min(modified_data))
modified_data = torch.from_numpy(modified_data.copy()).float()
yield modified_data, labels
if __name__ == '__main__':
pass