vital-ultrasound / ai-echocardiography-for-low-resource-countries

AI-assisted echocardiography for low-resource countries
1 stars 1 forks source link

Make use of PyTorch dataloaders #15

Closed mxochicale closed 2 years ago

mxochicale commented 2 years ago

🚀 Feature

The use of PyTorch dataloaders can create smooth integration of DL/ML models with our datasets that are in either avi or dicom format. That said, this issue is raised to follow up implementations of such PyTorch dataloaders with avi files and json files.

Motivation

In our today's meeting, 5thNov2021T1000, @gomezalberto suggested using PyTorch dataloaders because of various advantages such as loading, preprocessing, or augmenting data from a non trivial dataset. According to Alberto, we would need to find a good balance of parameters (such as batchsize or other) to be sure the computational cost is not very expensive.

Pitch

The implementation of PyTorch dataloaders will make use of *echo*.avi files from the filezilla server (e.g. 01NVb). I suggest to start with the echo files from participants 72 and its labels in json files.

Please use the following path structure or please suggest a better one structure to make this more efficient. Remember that the structure of data is based on how the data was collected.

mx19@sie133-lap:~/datasets/vital-us/echocardiography/videos-echo/01NVb-003-072$ tree -h
.
├── [4.0K]  T1
│   └── [1.3G]  01NVb-003-072-1-echo.mp4
├── [4.0K]  T2
│   ├── [1.2G]  01NVb-003-072-2-echo-cont.mp4
│   └── [4.0K]  extras
│       └── [238M]  01NVb-003-072-2-echo.mp4
└── [4.0K]  T3
    └── [1.1G]  01NVb-003-072-3-echo.mp4

4 directories, 4 files

json files are here, (that branch will be hopefully merged next week to main), and look like this:

mx19@sie133-lap:~/repositories/echocardiography/datasets/labelling-annotation/json_files/4CV/01NVb_003_072$ tree -s
.
├── [        973]  01NVb_003_072_T1_4CV.json
├── [       1152]  01NVb_003_072_T2_4CV.json
├── [       1060]  01NVb_003_072_T3_4CV.json
├── [     149311]  annotations.png
└── [       1062]  README.md

0 directories, 5 files

NOTE. Have a look to video_to_imageframes.py#L285 which might help to make use of the extraction of json labels

Alternatives

Not at the moment.

Additional context

These are few tutorials but feel free to add more

mxochicale commented 2 years ago

I have just committed 6b678fc which contains cosmetic changes to the scripts that leads to few list of things to do:

mxochicale commented 2 years ago

Notes from the today's meeting with AG.

config_files

Detecting frames (and clips) of 4CV and non-4CV

mxochicale commented 2 years ago

Previous commits successfully read frames but for some reason I am getting this one:

Notes

torch.as_tensor always tries to avoid copies of the data. One of the cases where as_tensor avoids copying the data is if the original data is a numpy array. https://stackoverflow.com/questions/48482787/pytorch-memory-model-torch-from-numpy-vs-torch-tensor

Traceback (most recent call last): File "dataloader_4CV.py", line 15, in data = dataset[video_index] File "/home/mx19/repositories/echocardiography/source/helpers/various.py", line 13, in wrap_func result = func(*args, **kwargs) File "/home/mx19/repositories/echocardiography/source/dataloaders/EchocardiographicVideoDataset.py", line 117, in getitem image_frame_array_1ch_i = cv.cvtColor(image_frame_array_3ch_i, cv.COLOR_BGR2GRAY ) #cv.COLOR_BGR2RGB cv.COLOR_BGR2GRAY cv2.error: OpenCV(4.5.4-dev) /tmp/pip-req-build-h45n7_hz/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

To try

mxochicale commented 2 years ago

Potential source of the problem with loading frames might be related to the absent timestamp in the last frames of the videos

        print(image_frame_index)
        print(frame_msec, current_frame_timestamp)
        print(type(frame_torch_chs_h_w), frame_torch_chs_h_w.shape)

23267
776342.2333333334 (12, 56, '342.233', '12:56:342.233')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23268
776375.6000000001 (12, 56, '375.600', '12:56:375.600')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23269
776408.9666666668 (12, 56, '408.967', '12:56:408.967')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23270
776442.3333333334 (12, 56, '442.333', '12:56:442.333')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23271
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23272
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23273
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23274
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23275
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23276
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23277
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23278
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23279
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23280
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23281
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23282
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23283
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23284
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 23285/23285 [00:37<00:00, 617.44it/s]
Function '__getitem__' executed in 37.7214s
20112
671070.4 (11, 11, '70.400', '11:11:70.400')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20113
671103.7666666667 (11, 11, '103.767', '11:11:103.767')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20114
671137.1333333334 (11, 11, '137.133', '11:11:137.133')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20115
671170.5000000001 (11, 11, '170.500', '11:11:170.500')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20116
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20117
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20118
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20119
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20120
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20121
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20122
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20123
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20124
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20125
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20126
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20127
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20128
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
20129
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20130/20130 [00:32<00:00, 610.75it/s]
Function '__getitem__' executed in 32.9680s
(rt-ai-echo-VE) mx19@sie133-lap:~/repositories/echocardiography/scripts/examples$ 

<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23209
774406.9666666667 (12, 54, '406.967', '12:54:406.967')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23210
774440.3333333334 (12, 54, '440.333', '12:54:440.333')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23211
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23212
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23213
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23214
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23215
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23216
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23217
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23218
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23219
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23220
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23221
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23222
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23223
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
23224
0.0 (0, 0, '0.000', '00:00:0.000')
<class 'torch.Tensor'> torch.Size([3, 1080, 1920])
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 23225/23225 [00:36<00:00, 630.84it/s]
Function '__getitem__' executed in 36.8243s
(rt-ai-echo-VE) mx19@sie133-lap:~/repositories/echocardiography/scripts/examples$