-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can ImageGenerator handle sample weights for pixelwise segmentation? #6629
Comments
@mptorr I am in no way an expert here, but IMHO So I think you should test |
#6538 references several keras repositories with implementations that do this, and the issue itself is for extending ImageDataGenerator to support segmentation, which it does not do right now. |
Just an idea, do you compile the model with sample_weight_mode="temporal"?
|
I think he does:
@mptorr I think I have solved the problem of weighted segmentation with a (admittedly somewhat ugly) hack. Instead of finding a way to use Keras's built-in losses (which I had trouble finding good documentation for) I have built a custom loss function. The idea I had was that Once I had this idea it was pretty easy to make an image generator which "encodes" the weights to the mask and then a custom loss function which "decodes" the weights + mask and calculates weighted categorical crossentropy. It seems to work, but I do need to test it some more. YMMV. |
@kglspl sounds like it would work reasonably well, though you're right that it is slightly hacky. Any reference code or a link? |
@ahundt I haven't put it anywhere yet, but here's my loss function:
I tried cleaning it up a bit, hope it makes sense. Basically, instead of encoding Note that I am still a newbie in ML and while this makes sense to me I might be missing something. Also, this is work in progress as it was written yesterday. And lastly, I haven't actually used this particular loss function in NN yet (I did however run a similar one, but wasn't satisfied with its input form). In other words, use at your own risk. ;) |
@kglspl Nice start, is clipping the right option or would normalizing be better? There would probably also need to be an additional helper function that makes creating the weight-hot vector easy. |
@ahundt I am not sure if / how normalizing could be used in this case, can you provide an example of what you mean? The encoding function I use is data-specific, but I guess one could make a generic one, yes. The good news is that this approach really seems to work. I have successfully trained a NN yesterday and am tweaking the weights now to get better image segmentation. So this process can indeed be used as a (hackish) way to use custom weights in image segmentation with Keras. |
@kglspl clipping values to |
For me, the sample weighting is working for pixelwise segmentation with image generators. are you sure, there is not some other mistake? Looks to me, the input shape should be (32,64,64) and not (634,64,64), as you are using the batch size of 32. |
@ahundt That is by design - I expect weight-hot vectors, which means every element in last vector must be |
@maderafunk This is a circular error caused by the checks that Keras In the thread on SegDataGenerator (#6538), @ahundt, @Dref360 and @allanzelener discuss how to handle zoom changes to the generated images—this also applies to the weights matrix, because if a region with weight @kglspl I'll be experimenting with your suggestion, will get back to you with results. I presume it's OK to discuss here as it relates to Keras/SegDataGenerator. |
@mptorr I've modified my ImageGenerator to return masks as well and then I am running another generator around the generator, where I am calculating the sample weights from the new masks. I'm not sure if this is a proper way but it is working for me. This is some example code, is has not been tested:
|
@mptorr Ok then - I was afraid we were hijacking your issue, but if you see it as a viable workaround I'd be happy to share more details if needed (this approach works and I have successfully used it today to train my NN, but I am still working on making it easier to use). @ahundt Let me expand the explanation then. :) However today I discovered there is a small limitation to my encoding as explained above - you can't have weight Note that you can't encode different weights based on the type of misclassification this way - but in my case this is not needed (as I only have 2 classes anyway). If one needs this then some other encoding would be needed, but it shouldn't be difficult to invent appropriate one. |
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'm trying to use sample weighting with
ImageGenerator
.flow
for 2 generators with identicalseed=42
andbatch_size=32
sample_weight_mode='temporal'
This throws an error:
ValueError: Found a sample_weight array with shape (634, 64, 64). In order to use timestep-wise sample weighting, you should pass a 2D sample_weight array.
If I reshape
sample_weight
to (634, 4096) I get:ValueError: Found a sample_weight array with shape (634, 4096) for an input with shape (32, 1, 64, 64). sample_weight cannot be broadcast.
Is this my misunderstanding of how to use
ImageGenerator
or is it unable to handle this particular situation?If I do not use sample weights, the model runs and no errors are thrown.
Thanks in advance for any advice you can give.
Keras 2.0.4, Theano 0.9
The text was updated successfully, but these errors were encountered: