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:
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>
It created the file infogainH.binaryproto
, Is this correct?
- Is this matrix H should be constant for the all images in database?
I really appreciate any help.
Thanks