Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

python - How to prepare targets for Sparse categorical entropy

I want to performing multiclass semantic segmentation. My images are grayscale.

image:(256,256,1)

I tried one hot encoding for multiclass segmentation and it works. The shape of my mask or target looks like this after one hot encoding. I have 8 classes.

mask:(256,256,8)

But one hot encoding is causing me memory error since my GPU cannot handle it and so I need to switch to another loss function which is sparse_categorical_crossentropy. But I am not sure how to encode my target for this. I read that sparse entropy has only integer targets. I labelled my class names in categories such as 'defect1', 'defect2', 'defect3' etc.

But how do i encode my target labels for this entropy. Is it just a single list of target values? How should it look like when i feed it into the network? Is there any particular encoding method to perform this? or any example of how the target should look like?

I am creating the dataset of images and target myself(custom dataset) so I did the annotations and labelled it. I am using U-net architecture.

I tried having my target to be (256,256) and last layer with out=layers.Conv2D(9,1,activation='softmax')(previous_layer). 9 is the number of classes. With this i am getting shape error

valueError: Error while checking target:expected output to have 4 dimensions but got array with shape (None,256,256).

I tried also the answers in the following link

So kept target shape to be (256*256,1) and used reshaping of last layer.

out=layers.Conv2D(9,1,activation='softmax')(previous_layer)
reshape=layers.Reshape((256*256,9))(out)

and this works but I am not sure if this implementation is correct because of the multiplication of image size how can a prediction be made and how to map the labels? My data is heavily imbalanced but still it shows accuracy around 0.995

question from:https://stackoverflow.com/questions/65599414/how-to-prepare-targets-for-sparse-categorical-entropy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If your target one-hot vectors were: [[1, 0, 0], [0, 1, 0], [0, 0, 1]], then they should be [0, 1, 2] to use sparse cross entropy.

If your target shape was (256, 256, 8) then its shape should be (256, 256).

I would advice to run model.summary() to see the shapes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...