How can I solve this problem in Python (creating weights for the Infogain Loss layer)?

I am working on semantic segmentation using CNN. I have a pixel count imbalance for each class. Based on this link , I am trying to create a weight matrix H

to define the Infogain loss layer

imbalance for my 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 deleting some irrelevant lines looks like this:

(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 you can see, this gives an error. My question can be broken down into two parts:

  • How do I resolve this error? I replaced L

    with the 5

    following:

    blob = caffe.io.array_to_blobproto(H.reshape((1,1,5,5)))

Now it doesn't give an error and the last line shows this:

<closed file 'infogainH.binaryproto', mode 'wb' at 0x7f94b5775b70>

      

He created the file infogainH.binaryproto

, is that correct?

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

I really appreciate any help.

thank

+1


source to share


1 answer


  • You have a simple "copy-paste" error. You copied your code from this answer where L

    was an integer representing the number of classes. On the other hand, your code L

    has a list with class weights. replacing L

    5 in your code really solves the problem.

  • Should it H

    be permanent? It really depends on you.



BTW, AFAIK, the current version of caffe does not support pixel data loss, you may need to use PR # 3855 code .

+1


source







All Articles