anilsathyan7 / Portrait-Segmentation

Real-time portrait segmentation for mobile devices
MIT License
638 stars 133 forks source link

SlimNet training #26

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hello, how did you train SlimNet with GTX 1080 Ti ?, I tried to train the network in Google Colab (slim512.ipynb), but 13 GB GPU memory is not enough, the environment crashes.

anilsathyan7 commented 3 years ago

In colab tesla T4 or P100 has 16GB GPU memory...I just referred my local code and the batch size was 64 itself.. In any case, you can reduce the batch size to a lower value.. say 48 or 32 and contiue the training...

ghost commented 3 years ago

I use slim512.ipynb notebook without any changes, but even if I set batch size = 2, memory using grow and environment crush. I use portrait256 dataset with 32700 images.

anilsathyan7 commented 3 years ago

It could be the issue with dataloader, i suppose..

Please modify the last few lines in 'DataLoader' class as follows:-

        if shuffle:
            # Prefetch, shuffle then batch
            data = data.batch(batch_size).repeat().prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
            #data = data.prefetch(tf.data.experimental.AUTOTUNE).shuffle(random.randint(0, len(self.image_paths))).repeat().batch(batch_size)
        else:
            # Batch and prefetch
            data = data.repeat().batch(batch_size).prefetch(tf.data.experimental.AUTOTUNE)

        return data
ghost commented 3 years ago

Training started, but loss and val_loss is always nan

test

ghost commented 3 years ago

Maybe something wrong with dataset portrait256.zip?

Training images and masks are .png files.

ghost commented 3 years ago

data_example.zip - 500 KB

anilsathyan7 commented 3 years ago

For training the sliment model using the current code, you need to convert the mask images to raw segmentaion masks i.e it should contain only two (pixel)values: 0 for background and 1 for foreground(currently it is 255 for foreground in portrait256).

For training with AISegment dataset, i had already prepared such a dataset. So you need to perform some preprocessing either before training or inside the data loder function:-

Here is the basic idea

// Convert to binary mask
mask[mask>=127]=1
mask[mask<127]=0

Refer: https://github.com/anilsathyan7/Portrait-Segmentation/issues/22#issuecomment-688373468

ghost commented 3 years ago

I give up, tried with all possible datasets, still the same..., i'm not python programmer...

anilsathyan7 commented 3 years ago

Just try modifying the data loader '_resize_data' function as follows:-

# mask = tf.image.resize(mask, [self.image_size, self.image_size], method='nearest')
mask = tf.image.resize(mask, [self.image_size, self.image_size], method='nearest') //255
ghost commented 3 years ago

Yes it works, train and detect, Thanks a lot, you're awesome.