U-Net transfer learning

DVangelis: <p>Hi, the last couple days I have been experimenting with the U Net model and doing transfer learning by feature extraction ( freezing layers of the model ).<br>
I have successfully trained a U Net model from scratch for binary segmentation . My dataset consisted from 1100 slices of size 512 X 512.<br>
Now I try to apply the transfer learning method by feature extraction and see what is the result of freezing different layers of the model . For that I use a new dataset of 300 slices with sizes 200 X 200.<br>
Moreover, I have split my dataset to training and validation with a split percentage 0.9 .<br>
With whichever combination of layers I choose to freeze I get an overestimation of segmentation .<br>
I try to make an interpretation of that behaviour of my model . At first I thought that while using feature extraction I also need to tweak the hyperparameters of my model or the bad results are due to the size of my new dataset.<br>
It seems now that just by applying the initial trained model to the new dataset without training give the best results (still not satisfactory for the application that I want ).</p>
<p>My query or maybe why i post this topic is to ask for a guidance or any input to this matter  and maybe how you should approach such a task .<br>
All comments are welcome.</p>

InnovArul: <aside class="quote no-group" data-username="DVangelis" data-post="1" data-topic="152432">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dvangelis/48/47464_2.png" class="avatar"> DVangelis:</div>
<blockquote>
<p>With whichever combination of layers I choose to freeze I get an overestimation of segmentation .</p>
</blockquote>
</aside>
<p>Can you elaborate on what did you mean by “overestimation of segmentation”?<br>
Do you mean its biased towards one particular class or so?</p>

DVangelis: <p>Yes correct . I work with microscopy images of a material with two phases . During prediction  It makes an overestimation of one phase in comparison to the other .<br>
I hope now is clearer.</p>
<p>Also I forgot to mention in my first message I use IoU as metric for tracking the progress of my model</p>

Michal_Bogacz: <p>Have you checked if your dataset is not biased towards any of the classes? Maybe one is more populated? One has much worse examples?</p>

DVangelis: <p>Good one. Yes this can be one explanation of my problem. I just checked my dataset.  The problem is that the phase that I want to segmentate is evolving dynamically with time. Thus when I choose my training dataset is really hard not to be biased<br>
So what I try  is to segmentate a spatial temporal dataset with U-net. If the phase of interest in my material develop relative stable with time I get good results but when the phase develops more ‘randomly’ I get but predictions.   Maybe a more sophisticated model like a combination of LSTM and U net would help resolve that problem  but for now I would like to avoid make my life more complicated.</p>

A_Bdeir: <p>If you dont want to account for the time variable in the model, you could try to dynamically increase the penalty on false positives of the out of balance class (some ratio). That should discourage the model from bias but you’d have to test it.</p>

DVangelis: <p>Not sure of how I can do it that. You mean to use a criterium and when I get ‘overestimation’ to stop training my model ?<br>
Now I will try to use bigger dataset and try my phase in interest to be distributed more uniformly .</p>

Michal_Bogacz: <p>Well, I think the U-NETs are more then enough, I don’t know exactly how the data looks like, but if you want to try a real cannon, visual-transformers are always there. Although training a new one for this purpose might be very hard.<br>
Can you say if data is analyzed as sequence? Or singular instances?<br>
If sequence, than it is much harder, but if data is just an snap-shot image, you can always try to add some transformations to your images to increase the amount of training samples. This is superficial, but helps.</p>

DVangelis: <aside class="quote no-group" data-username="Michal_Bogacz" data-post="8" data-topic="152432">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/michal_bogacz/48/35190_2.png" class="avatar"> Michal_Bogacz:</div>
<blockquote>
<p>data is analyzed</p>
</blockquote>
</aside>
<p>I will have a look in visual-transformers.<br>
I would say that my data are sequence. I dont know how you define  sequence and instances datasets.<br>
But I believe that is a sequence as I have a phase, a (‘structure’ if you want ) that develops dynamically with time.</p>

Michal_Bogacz: <p>What I meant was if your base data is a photo or is it a several developing frames? you put one frame/slice at a time, and the next one does not connect to the previous in the process? or is the n+1 result based on n? I don’t understand the training procedure you have used, that’s why I ask. You may try to train the model on randomly ordered frames.</p>

DVangelis: <p>Ok thanks now I understood.<br>
So you suggest that I can use maybe shuffle in me dataloader and see if that helps correct ?</p>

Michal_Bogacz: <p>Yes, generally if you expose only one frame at a time to the model, you should avoid using somehow ordered data in training. Model should be trained to recognize one frame and not be influenced by other entries. During the process of weights optimization, frames which are similar to each other in a sequence, may influence the way in which model learns features, and so on.<br>
What I propose:</p>
<ul>
<li>try shuffling training data,</li>
<li>try augmenting data (rotations, cropping, maybe slight color changes and introduce some noise into some of the pictures) use all available data, merge augmented and non-augmented data together,</li>
<li>try keeping data well balanced,</li>
<li>from what i understand you have changed last layers in the network, you don’t have much training data, so keep the structure shallow</li>
<li>try overfitting on set of 2-5 frames, to make sure you have no additional problems in your architecture</li>
</ul>

DVangelis: <p>Ok thanks it is clear.</p>
<p>So again it is more about data preparation rather which model you train.</p>
<p>My Unet consists of 4 Convolutional blocks and 4 skip connections.</p>
<p>I have around 1000 images for training my first model from skratch and I would like to use transfer learning in a new task where my dataset for this task is 300 slices.</p>
<p>It seems that one of the limitations was that I had an unbalanced dataset.</p>
<p>Any advice on how to select a well balanced dataset</p>
<p>The limitation right now for generalizing my trained model is the lack of a workflow of how to select my training dataset. I just visually observe my images and I hope that they are balanced enough.</p>

phisad: <p>You canntry to upscale your images to 512x512 as during initial training</p>

DVangelis: <p>That would be really nice . But I am constrained from the nature of my images . I work with different datasets that each one has different image dimensions. I worked initially with the one that had the largest possible image size as I wanted to benefit from its large image size . But now I need to segmentate also the other remaining datasets that consist from images with smaller image size .</p>
<p>After preparing a new dataset with more even or balanced representation of my classes . I got better results . So in my case the problem was as it was mentioned above the lack of balance in my training dataset</p>