如何在Python中解决此问题(为Infogain Loss层创建权重)? [英] How to solve this issue in Python (creating weights for Infogain Loss layer)?
问题描述
我正在使用CNN进行语义分割.我每个班级都有不平衡的像素数.
基于此链接,我我正在尝试创建权重矩阵H
,以便为我的失衡类成员定义Infogain loss layer
.
我的数据有五个类.我在python中编写了以下代码:
读取示例图像:
I am working on semantic segmentation using CNNs. I have an imbalance number of pixels for each class.
Based on this link, I am trying to create weight matrix H
in order to define Infogain loss layer
for my imbalance class members.
My data has five classes. I wrote the following code in python:
Reads a sample image:
im=imread(sample_img_path)
计算每个类别的像素数
cl0=np.count_nonzero(im == 0) #0=background class
.
.
cl4=np.count_nonzero(im == 4) #4=class 4
输出: 39817 13751 1091 10460 417
output: 39817 13751 1091 10460 417
#Inverse class weights
#FORMULA=(total number of sample)/((number of classes)*(number of sample in class i))
w0=round(sum_/(no_classes*cl0),3)
w1=round(sum_/(no_classes*cl1),3)
w2=round(sum_/(no_classes*cl2),3)
w3=round(sum_/(no_classes*cl3),3)
w4=round(sum_/(no_classes*cl4),3)
print w0,w1,w2,w3,w4
L_1=[w0,w1,w2,w3,w4]
#weighting based on the number of pixel
print L_1
L=[round(i/sum(L_1),2) for i in L_1] #normalizing the weights
print L
print sum(L)
#creating the H matrix
H=np.eye(5)
print H
#H = np.eye( L, dtype = 'f4' )
d=np.diag_indices_from(H)
H[d]=L
print H
blob = caffe.io.array_to_blobproto(H.reshape((1,1,L,L)))
with open( 'infogainH.binaryproto', 'wb' ) as f :
f.write( blob.SerializeToString() )
print f
除去一些不重要的行后,输出如下:
The output, after removing some unimportant lines, is as follows:
(256, 256)
39817 13751 1091 10460 417
0.329 0.953 12.014 1.253 31.432
<type 'list'>
[0.329, 0.953, 12.014, 1.253, 31.432]
[0.01, 0.02, 0.26, 0.03, 0.68]
1.0
[[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1.]]
[[ 0.01 0. 0. 0. 0. ]
[ 0. 0.02 0. 0. 0. ]
[ 0. 0. 0.26 0. 0. ]
[ 0. 0. 0. 0.03 0. ]
[ 0. 0. 0. 0. 0.68]]
Traceback (most recent call last):
File "create_class_prob.py", line 59, in <module>
blob = caffe.io.array_to_blobproto(H.reshape((1,1,L,L)))
TypeError: an integer is required
可以看出,它给出了一个错误.我的问题可以分为两部分:
As it can be seen, it is giving an error. My question can be folded into two parts:
-
如何解决此错误? 我将
L
替换为5
,如下所示:
How to solve this error? I replaced
L
with5
as follows:
blob = caffe.io.array_to_blobproto(H.reshape((1,1,5,5)))
现在,它没有给出错误,最后一行显示了这一点:
Now, it is not giving error and last line shows this:
<closed file 'infogainH.binaryproto', mode 'wb' at 0x7f94b5775b70>
它创建了文件infogainH.binaryproto
,对吗?
- 对于数据库中的所有图像,此矩阵H是否应为常数?
我非常感谢您的帮助.
I really appreciate any help.
谢谢
推荐答案
-
您有一个简单的复制粘贴"错误.您从此答案复制了代码,其中
L
是代表类数的整数.另一方面,在您的代码中,L
是具有类权重的 list .在代码中用5替换L
确实可以解决问题.
You have a simple "copy-paste" bug. You copied your code from this answer where
L
was an integer representing the number of classes. In your code, on the other hand,L
is a list with the class weights. replacingL
with 5 in your code does indeed solves the problem.
H
应该是常数吗?确实由您决定.
Should H
be constant? This is really up to you to decide.
顺便说一句,AFAIK,当前的Caffe版本不支持逐像素信息丢失,您可能需要使用 PR#3855 .
BTW, AFAIK, current caffe version does not support pixel-wise infogain loss, you might need to use the code in PR #3855.
这篇关于如何在Python中解决此问题(为Infogain Loss层创建权重)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!