Skip to content
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

If I have two classes,how should I set the class weighting? #12

Open
luciaL opened this issue Oct 23, 2017 · 4 comments
Open

If I have two classes,how should I set the class weighting? #12

luciaL opened this issue Oct 23, 2017 · 4 comments

Comments

@luciaL
Copy link

luciaL commented Oct 23, 2017

Thank you so much for you sharing.It really helps me a lot.Here is Another question. If I have two classes,how should I set the class weighting? Looking forward to your answer.
Thank you so much .

@luciaL luciaL changed the title the loss funtion here is the IoU loss in the paper? If I have two classes,how should I set the class weighting? Oct 24, 2017
@szkocot
Copy link

szkocot commented Oct 28, 2017

model.compile(... , sample_weight_mode="temporal")
You can calculate class weights with sklearn.utils.class_weight.compute_class_weight. Just make sure to flatten N-dim array.
Then if you have 4 classes you should create this kind of matrix:

class_weights = np.zeros((224,224, 4))
class_weights[:,:, 0] += 0.25
class_weights[:,:, 1] += 4
class_weights[:,:, 2] += 2
class_weights[:,:, 3] += 6

And finally:
model.fit(... , class_weight=class_weights)

Note, that it has been taken from keras's github. Works for me.

@luciaL
Copy link
Author

luciaL commented Oct 30, 2017

Thank you so much.I will try it.

@deepitapai
Copy link

Thanks @szymko1995 for this. I'm struggling with the logic behind this. Can you help me here?

@szkocot
Copy link

szkocot commented Jun 30, 2018

Okay. So, when you have 4 classes, that you want to segment output matrix of NN will be WxHxN_classes.
You set calculated weights (by for example sklearn.utils.class_weight.compute_class_weight) and paste them into each of 3-axis's WxH matrices. This way of setting weights for segmentation NN has been proposed by someone on StackOverflow or keras' GitHub as the output of segCNN isn't typical.
Idk if API in Keras for this kind of NN has changed over a time, but I think it should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants