1

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

Community
  • 1
  • 1
S.EB
  • 1,966
  • 4
  • 29
  • 54

1 Answers1

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.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thank you very much Shai, your link was very helpful. I added a comment since there is a problem with adding the `Weighted Loss Layer` – S.EB Feb 20 '17 at 16:57
  • @Shai,i just wanted to make sure if i understand the the parameters in infogainLossLayer,if have two classes in my label(0,1) and zero represents the background,1 the other class obviously less represented,the first row of H is related to background and second row to the other class?does it make a difference if the H[0,0] and H[1,1] are expressed in values greater than one(if class 1 is 20% and class 0 is 80%),if i set H[0,0] to 2 and H[1,1] to 8,what might be the effect of setting the values like this?i would appreciate a lot if could help on this – Eliethesaiyan Aug 04 '17 at 10:02
  • @Eliethesaiyan I saw your question. Have you looked at the thread "common causes for Nan during training"? – Shai Aug 04 '17 at 10:10
  • 1
    @Shai,i knew it but never used infogainLoss before,the answer was there all along,i normalized them now,another problem was that i was using the previous version of InfoGainLoss,i didn't use softmax with it,i think it was the main reason.Thanks a lot – Eliethesaiyan Aug 04 '17 at 10:45