How to solve this issue in Python (creating weights for Infogain Loss layer)?

烂漫一生 提交于 2019-12-11 06:36:24

问题


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)

Counts the number of pixels of each class

cl0=np.count_nonzero(im == 0)   #0=background class
.
.
cl4=np.count_nonzero(im == 4)   #4=class 4

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:

  1. How to solve this error? I replaced L with 5 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>

It created the file infogainH.binaryproto, Is this correct?

  1. Is this matrix H should be constant for the all images in database?

I really appreciate any help.

Thanks


回答1:


  1. 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. replacing L with 5 in your code does indeed solves the problem.

  2. Should H be constant? This is really up to you to decide.

BTW, AFAIK, current caffe version does not support pixel-wise infogain loss, you might need to use the code in PR #3855.



来源:https://stackoverflow.com/questions/42344936/how-to-solve-this-issue-in-python-creating-weights-for-infogain-loss-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!