Keras: Sample Weight Error

Created on 25 Jun 2017  路  10Comments  路  Source: keras-team/keras

Hi there!

I'm working on a brain lesion segmentation problem and I'm trying to implement a Unet with code inspired by: https://github.com/jocicmarko/ultrasound-nerve-segmentation

One of the issues I'm trying to overcome is class balance (lots more non-lesion voxels rather than lesion voxels). I tried using class_balance but that didn't work so now I'm trying to use sample_weight and that's also giving me all sorts of errors.

i) First thing I tried was to set sample_weight_mode to 'temporal' and feed in a weight matrix of the same shape as my target data:
target_data.shape -> (n_samples,512 rows/pixels, 512 cols/pixels, 1 channel )
Weight_map.shape -> (n_samples,512 rows/pixels, 512 cols/pixels, 1 channel )

output:
_ValueError: Found a sample_weight array with shape (100, 512, 512, 1). In order to use timestep-wise sample weighting, you should pass a 2D sample_weight array.

ii) Second thing I tried was to flatten the sample array so it would be of shape:
Weight_map.shape -> (n_samples,512x512x1).

output:
ValueError: Found a sample_weight array with shape (100, 262144) for an input with shape (100, 512, 512, 1). sample_weight cannot be broadcast.

iii) Next I tried following the advice of uschmidt83 (here) and flattening the output of my model along with the corresponding target data.
last_layer = keras.layers.Flatten()(second_last_layer)

target_data.shape -> (n_samples,512x512x1).
Weight_map.shape -> (n_samples,512x512x1).

output:
ValueError: Found a sample_weight array for an input with shape (100, 262144). Timestep-wise sample weighting (use of sample_weight_mode="temporal") is restricted to outputs that are at least 3D, i.e. that have a time dimension.

Oddly enough, even if I set _sample_weight=None_ I still get the same error as right above.

Any advice on how to fix this sample_weight error? Here is the basic code to reproduce the error
here

Also, if you have advice about how to deal the class imbalance problem, please let me know.

stale

Most helpful comment

I also had the same problem and I struggled with for a week. then it clicked with me and I made it work.
This is what I did:

  • I used reshape instead of Flatten. Flatten will give you a fixed 2 dimensional output. say your input is (number of samples,80,80,1), Flatten will give you (number of sample,6400) which is 2 D. instead use reshape to get (number of sample,6400,1). this is your 3-D output which works with "temporal".

  • the other thing you need to do is make sure your label is also 3-D. You probably have changed the shape of your labels to match the 2-D output from your model, so you will have something similar to (number of sample,6400) which will again generate an error. All you have to do is add a third dimension like this (number of sample,6400,1). This should make it work

All 10 comments

Got the same error without even any weighting of samples

Seems like a bug when the traceback says that it found a variable with inappropriate size, but Keras created this variable somewhere inside the call of fit_generator
Found a sample_weight array with shape (16, 10)
when I didn't feed any sample_weight

I found out that my generator yielded third output that I used as a mask, but Keras used it as a sample_weight, and this is documented in fit_generator so this is just my fault, no bugs here

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

I also had the same problem and I struggled with for a week. then it clicked with me and I made it work.
This is what I did:

  • I used reshape instead of Flatten. Flatten will give you a fixed 2 dimensional output. say your input is (number of samples,80,80,1), Flatten will give you (number of sample,6400) which is 2 D. instead use reshape to get (number of sample,6400,1). this is your 3-D output which works with "temporal".

  • the other thing you need to do is make sure your label is also 3-D. You probably have changed the shape of your labels to match the 2-D output from your model, so you will have something similar to (number of sample,6400) which will again generate an error. All you have to do is add a third dimension like this (number of sample,6400,1). This should make it work

@Anesouadou I have a similar problem. Your suggestion didn't work :(. If I make my label/sample weight 3D i.e., (batch, 3920, 1) and set sample_weight_mode to 'temporal' I get the following error:

ValueError: Found a sample_weight array with shape (None, 3920, 1). In order to use timestep-wise sample weighting, you should pass a 2D sample_weight array.

When I flatten the labels/sample_weights I then get this error:

ValueError: Found a sample_weight array for an input with shape (None, 3920). Timestep-wise sample weighting (use of sample_weight_mode="temporal") is restricted to outputs that are at least 3D, i.e. that have a time dimension.

It's contradicting itself!

@wmcnally I think you are confusing the correct format for each parameter.

  • output format is 3D (number of samples, flattened sample, 1) . the last dimension is added just to make your parameter 3D. you can add it using reshape function (don't use flatten).
  • label format the same as output format.
  • sample weight is 2D where each row corresponds to a sample and the number of rows is equal to the number of samples.
    I hope this makes it clearer

@Anesouadou oh, ok. So in that case, the shape of the 2D sample weight should be (number of samples, flattened sample), correct? Thanks in advance for your help.

@wmcnally yes, it should work just fine. Let me how it goes.

@Anesouadou I got it working. Thanks so much for the help.

Was this page helpful?
0 / 5 - 0 ratings