Andrei.Mouraviev
Andrei.Mouraviev

Reputation: 113

Keras sample_weight array error

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.

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.*

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.*

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: https://gist.github.com/andreimouraviev/2642384705034da92d6954dd9993fb4d

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

Upvotes: 3

Views: 3492

Answers (1)

user3709710
user3709710

Reputation: 131

Weight needs to be a 1D array, where as target has an extra channel like input.

Can you try sample_weight_mode=temporal with the following dimensions:

input_image  ->  (n_samples, 512, 512, 1)
target_label ->  (n_samples, 262144, 1)
weight_map   ->  (n_samples, 262144)

The following link contains information about class weights: https://github.com/fchollet/keras/issues/2115

Upvotes: 1

Related Questions