mean.binaryproto visualization for uint8 data

1,144 views
Skip to first unread message

Yoann

unread,
Jan 30, 2015, 8:15:24 AM1/30/15
to caffe...@googlegroups.com
Hello all,

I've successfully saved my dataset in LMDB format as float data, created the mean binaryproto file and visualized this file to make sure that everything was ok. However, it is not possible to crop float data in train_val.prototxt and thus I decided to save my data as uint8.

To save my data I just replaced "float" by "np.uint8" in my python script (see below). Then I called the same tool ./build/tools/compute_image_mean (...). And finally I used the same python script to visualize the mean file (see below). I just added the line 'out = out.astype(np.uint8)'.
But, the mean file saved as png appears completely black. 

It seems normal since the 'arr' variable looks like:

[[[ 0.00309061  0.00339213  0.00437208 ...,  0.00248756  0.0027137
    0.00241218]
  [ 0.00361827  0.00346751  0.00505051 ...,  0.0027137   0.00309061
    0.00278908]
  [ 0.0042967   0.00414594  0.00482436 ...,  0.00407056  0.00407056
    0.00361827] etc....

Did I miss something?

Here is the script to save the binaryproto as a png file (written by another caffe user on another thread):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
 
# Make sure that caffe is on the python path:
caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
 
blob = caffe.proto.caffe_pb2.BlobProto()
data = open( sys.argv[1] , 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
out = np.ascontiguousarray(out.transpose(1,2,0))
out = out.astype(np.uint8)
mpimg.imsave(sys.argv[2], out)

Here is the loow where I save data as uint8 (in_ is the path of one image):
for idx in range(int(math.ceil(len(Inputs)/1000.0))):
in_db_data = lmdb.open(lmdb_data_name, map_size=int(1e12))
with in_db_data.begin(write=True) as in_txn:
for in_idx, in_ in enumerate(Inputs[(1000*idx):(1000*(idx+1))]):
im = caffe.io.load_image(in_)
#plt.imshow(im)
#plt.show()
im_dat = caffe.io.array_to_datum(im.astype(float).transpose((2, 0, 1)))
in_txn.put('{:0>10d}'.format(1000*idx + in_idx), im_dat.SerializeToString())
string_ = str(1000*idx+in_idx+1) + ' / ' + str(len(Inputs))
sys.stdout.write("\r%s" % string_)
sys.stdout.flush()
in_db_data.close()
print('')

Yoann

unread,
Jan 30, 2015, 8:33:40 AM1/30/15
to caffe...@googlegroups.com
To get a point of comparison, the 'arr' variable for the mean file generated on the float data looks like:

[[[ 0.29685655  0.29646629  0.29816759 ...,  0.29304227  0.29310983
    0.29361176]
  [ 0.3050448   0.30444458  0.3056635  ...,  0.30154204  0.30190951
    0.30188578]
  [ 0.3077313   0.30739287  0.30698517 ...,  0.30331087  0.30332285
    0.30343989]

Which makes more sense.
Thus, do you know what is the problem?

Yoann

unread,
Feb 3, 2015, 10:09:14 AM2/3/15
to caffe...@googlegroups.com
My bad, never thought to read the caffe.io.load_image which loads an image as float.
Creating a new function reading an image as uint8 fixed my problem.

Yasser Souri

unread,
Feb 3, 2015, 10:46:13 AM2/3/15
to caffe...@googlegroups.com
Here is a trick I have used to visualize mean images:

https://gist.github.com/yassersouri/f617bf7eff9172290b4f

Anurag Gupta

unread,
Mar 27, 2019, 8:03:52 AM3/27/19
to Caffe Users
    import os, sys, glob, caffe
    import numpy as np
    mean_file= "path/to/file/mean.binaryproto"
    #convert mean file to image
    blob= caffe.proto.caffe_pb2.BlobProto()
    try:
        data = open( mean_file, 'rb' ).read()
    except:
        data = open( mean_file, 'r' ).read()
    blob.ParseFromString(data)
    arr = np.uint8(np.array( caffe.io.blobproto_to_array(blob) )[0])
    #a= arr[0];    b= arr[1];    c= arr[2]
    img= np.zeros([128,200,3])
    img[:,:,0]= arr[0];    img[:,:,1]= arr[1];    img[:,:,2]= arr[2]
    import cv2
    cv2.imwrite(mean_file.replace(".binaryproto", ".bmp"), img)
Reply all
Reply to author
Forward
Message has been deleted
0 new messages