Loading .nii images while saving memory

  • Context: Python 
  • Thread starter Thread starter BRN
  • Start date Start date
  • Tags Tags
    Images Memory
Click For Summary

Discussion Overview

The discussion revolves around the challenge of loading large .nii image datasets for a Deep Learning project involving the DCGAN algorithm, particularly under constraints of limited system memory. Participants explore potential solutions for efficiently managing memory usage while training a model with these datasets.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • The original poster describes the need to load large datasets (3GB, 5GB, and 8GB) on a system with only 4GB of RAM and 2GB of Swap memory, questioning how to do this without overloading memory.
  • One participant suggests using an iterator to load each file on demand instead of loading all files into memory at once.
  • The original poster expresses uncertainty about how to implement such an iterator, particularly in relation to the loss function of the GAN discriminator.
  • Another participant references TensorFlow's Keras API, suggesting a potential resource that may assist with loading images from a directory, although they do not provide a detailed solution.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to manage memory while loading the datasets. Multiple suggestions are presented, but no definitive solution is agreed upon.

Contextual Notes

The discussion does not clarify specific implementation details for the iterator or how it would integrate with the GAN's loss function, leaving these aspects unresolved.

BRN
Messages
107
Reaction score
10
Hello everybody,
for my Deep Learning exam, I have to develop a project that provides for the generation of 3D images in format .nii using the DCGAN algorithm trained with some real images of MRI scans of brains of patients with Alzahimer.

I have a serious problem. I should load three different datasets that weigh 3GB, 5GB and 8GB respectively. Unfortunately I am forced to use an old PC with only 4GB of RAM and 2GB of Swap memory, so I am impossible to upload the files at one time using this simple code:

[CODE lang="python" title="loading files"]
train_data = []
data_path = './data/ADNI_test/'
for i in range(len(filenames)):
mri_file = data_path + filenames
train_data.append(nib.load(mri_file).get_fdata())
[/CODE]

Would any of you know how to give me some alternative solution? Is there a way to upload the files a little at a time without overloading memory? In DCGAN algorithm the batch size is set to 64 files, but I will certainly have to decrease to 30.

Thank you!
 
Technology news on Phys.org
I wouldn't have thought it would be necessary to have all the image files in memory; can you provide the ML engine with an iterator that loads each file on demand?
 
  • Like
Likes   Reactions: BRN
I hope it can be done, but at the moment I don't know how.
Should the iterator be implemented directly in the loss function of the GAN discriminator?

[CODE lang="python" title="discriminator"]
def discriminator_model(strides, kernel_size, input_img, weight_initializer, downsample_layers):
rate = 0.2
filters = input_img.shape[1]

model = Sequential()

model.add(tf.keras.layers.Conv3D(strides, kernel_size, filters, input_img.shape, padding = 'same',
kernel_initializer = weight_initializer))
model.add(tf.keras.layers.LeakyReLU())
model.add(tf.keras.layers.Dropout(rate = rate))

for l in range(downsample_layers - 1):
filters = int(filters * 2)
model.add(tf.keras.layers.Conv3D(strides, kernel_size, filters, padding = 'same'))
model.add(tf.keras.layers.LeakyReLU())
model.add(tf.keras.layers.Dropout(rate = rate))

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(1))
return model

def discriminator_loss(real_output, fake_output):
real_loss = cross_entropy(tf.ones_like(real_output), real_output)
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
total_loss = real_loss + fake_loss
return total_loss
[/CODE]

where 'real_output' would be the real image that is compared with that artificially generated.

Some idea?
 

Similar threads

Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
5K