Ejemplo n.º 1
0
def bp_to_npy(bp_data):
    blob = caffe_pb2.BlobProto()
    blob.ParseFromString(bp_data)
    arr = blobproto_to_array(blob)
    if arr.ndim is 4:    #with color,first dim is one
        arr = arr.reshape(arr.shape[1],arr.shape[2],arr.shape[3]) 
    return arr
Ejemplo n.º 2
0
def loadMean(meanFile):
    fp = open(meanFile, 'rb')
    proto_data = fp.read()
    fp.close()

    mean_blob = caffe_pb2.BlobProto()
    mean_blob.ParseFromString(proto_data)

    return blobproto_to_array(mean_blob)[0]
Ejemplo n.º 3
0
def load_mean(mean_file='models/cifar10/mean.binaryproto'):
    blob = caffe_pb2.BlobProto()
    data = open(mean_file, "rb").read()
    blob.ParseFromString(data)
    nparray = blobproto_to_array(blob)
    mean_data = nparray[0]
    channel_swap = (1, 2, 0)
    mean_data = mean_data.transpose(channel_swap)
    return mean_data
Ejemplo n.º 4
0
def _make_mean(meanfile):
    print('generating mean file...')
    mean_blob = caffe_pb2.BlobProto()
    mean_blob.ParseFromString(open(meanfile, 'rb').read())
    mean_npy = blobproto_to_array(mean_blob)
    mean_npy_shape = mean_npy.shape
    mean_npy = mean_npy.reshape(
        mean_npy_shape[1], mean_npy_shape[2], mean_npy_shape[3])
    print "done."
    return mean_npy
def extract_binaryproto(proto, output):
    from caffe.proto.caffe_pb2 import BlobProto
    from caffe.io import blobproto_to_array
    import numpy as np
    blob = BlobProto()
    data = open(proto, 'rb').read()
    blob.ParseFromString(data)
    mean = np.array(blobproto_to_array(blob)).squeeze(0) \
        .transpose([1, 2, 0]).astype(np.float32)
    pickle.dump(mean, open(output, 'wb'), protocol=2)
def _make_mean(meanfile):
    print('generating mean file...')
    mean_blob = caffe_pb2.BlobProto()
    mean_blob.ParseFromString(open(meanfile, 'rb').read())
    mean_npy = blobproto_to_array(mean_blob)
    mean_npy_shape = mean_npy.shape
    mean_npy = mean_npy.reshape(mean_npy_shape[1], mean_npy_shape[2],
                                mean_npy_shape[3])
    print "done."
    return mean_npy
Ejemplo n.º 7
0
def binaryfile_to_blobproto_to_array(file_path):
    # input the filepath save by function WriteProtoToBinaryFile in caffe
    # output the array data

    assert os.path.exists(file_path), 'File does not exists: %s' % file_path

    binary_data = open(file_path, 'rb').read()
    blob_proto = caffe_pb2.BlobProto()
    blob_proto.ParseFromString(binary_data)
    array_data = blobproto_to_array(blob_proto)

    return array_data
Ejemplo n.º 8
0
def mean(mean, binaryproto = True):
	if binaryproto:
		from caffe.proto import caffe_pb2
		from caffe.io import blobproto_to_array

		# Transform a protoblob to a numpy array
		blob = caffe_pb2.BlobProto()
		data = open(mean, "rb").read()
		blob.ParseFromString(data)
		nparray = blobproto_to_array(blob)
	else:
		np.load(mean)

	return nparray
Ejemplo n.º 9
0
from caffe import io as c
import numpy as np
import os, sys

if len(sys.argv) < 3:
    print 'Use: convertProtobinToNumpy protobinFile numpyOutput'
    sys.exit()

protoData = c.caffe_pb2.BlobProto()
f = open(sys.argv[1], 'rb')
protoData.ParseFromString(f.read())
f.close()
array = c.blobproto_to_array(protoData)
np.save(sys.argv[2], array[0].swapaxes(1, 0).swapaxes(2, 1)[:, :, ::-1])
A = np.load(sys.argv[2] + '.npy')
print 'Final matrix shape:', A.shape
Ejemplo n.º 10
0
MEAN_BIN = "lmdb/JD_mean.binaryproto"
MEAN_NPY = "lmdb/JD_mean.npy"

from caffe.proto import caffe_pb2
from caffe.io import blobproto_to_array

print('generating mean file...')

mean_blob = caffe_pb2.BlobProto()
mean_blob.ParseFromString(open(MEAN_BIN, 'rb').read())

import numpy as np
mean_npy = blobproto_to_array(mean_blob)
mean_npy_shape = mean_npy.shape
mean_npy = mean_npy.reshape(mean_npy_shape[1], mean_npy_shape[2], mean_npy_shape[3])

np.save(file(MEAN_NPY, 'wb'), mean_npy)

print('done...')
from caffe import io as c
import numpy as np
import os, sys

if len(sys.argv) < 3:
    print "Use: convertProtobinToNumpy protobinFile numpyOutput"
    sys.exit()

protoData = c.caffe_pb2.BlobProto()
f = open(sys.argv[1], "rb")
protoData.ParseFromString(f.read())
f.close()
array = c.blobproto_to_array(protoData)
np.save(sys.argv[2], array[0].swapaxes(1, 0).swapaxes(2, 1)[:, :, ::-1])
A = np.load(sys.argv[2] + ".npy")
print "Final matrix shape:", A.shape
Ejemplo n.º 12
0
# --
#net_path : deploy.prototxt, model_path : caffemodel. 識別なので、Caffe.TESTと記載
net = caffe.Net(net_path, model_path, caffe.TEST)
# --

# for preprocess
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
# channel_swapはRGBをσ(RGB) = BGRにしたいときに用いる。
# 対して、set_transposeは、[H, W, K] => [K, H, W]としたいときとかに用いる。
transformer.set_transpose('data', (2, 0, 1))

mean_blob = caffe_pb2.BlobProto()
with open(mean_path, "rb") as f:
    mean_blob.ParseFromString(f.read())
mean_array = blobproto_to_array(mean_blob)
mean_array = np.asarray(mean_blob.data).reshape(
    (mean_blob.channels, mean_blob.height, mean_blob.width))
transformer.set_mean('data', mean_array)

#上記の説明参照
transformer.set_raw_scale('data', 255)
# scaling to 1/255 = 0.003..
transformer.set_input_scale('data', 0.00390625)

# sample test for 11181 images
cnt = 0  #number of the collect answer
num = 11181

numbers = [str(i + 60000).zfill(7) for i in range(1, num + 1)]
filename = [s + ".bmp" for s in numbers]
import sys
sys.path.append('/home/huogen/git/caffe/python')
from caffe.proto import caffe_pb2
import caffe.io
from caffe.io import blobproto_to_array
import numpy as np

blob = caffe_pb2.BlobProto()
data = open("RgbFull_dir_mean_vgg.binaryproto", "rb").read()
blob.ParseFromString(data)
nparray = np.array(blobproto_to_array(blob))
f = file("RgbFull_dir_mean_vgg.npy", "wb")
np.save(f, nparray[0])
f.close()
Ejemplo n.º 14
0
PRETRAINED = ROOT.replace(
    "python",
    "yamashita/deeplearning/caffemodel/set1/snapshot/bestgoromaru.caffemodel")

IMAGE_DIR = ROOT.replace("src/python", "data/rugby/video/video_5minsep/")

cascade = cv2.CascadeClassifier(
    "/usr//local/Cellar/opencv/2.4.11_1/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"
)

if __name__ == '__main__':

    blob = caffe_pb2.BlobProto()
    with open(MEAN_FILE, 'rb') as fp:
        blob.ParseFromString(fp.read())
    numpy_mean = blobproto_to_array(blob)
    numpy_mean = numpy_mean.mean(0)

    # load classifier
    net = caffe.Classifier(MODEL_FILE,
                           PRETRAINED,
                           mean=numpy_mean.mean(1).mean(1),
                           channel_swap=(2, 1, 0),
                           raw_scale=255,
                           image_dims=(100, 100))

    imglist = glob.glob(IMAGE_DIR + "*")

    for photo in imglist:
        photo = IMAGE_DIR + "0386.png"
        # load image file to be predicted
Ejemplo n.º 15
0
proto_file = os.path.join(conf_dir, "cifar10_quick_deploy.prototxt")
model_file = os.path.join(conf_dir, "cifar10_quick_iter_5000.caffemodel")
mean_file = os.path.join(conf_dir, "mean.binaryproto")

model = caffe.Classifier(proto_file, model_file)

if commands.getstatusoutput("nvidia-smi")[0] == 0:
    caffe.set_mode_gpu()
    print >> sys.stderr, "Use GPU for training / testing."
else:
    caffe.set_mode_cpu()
    print >> sys.stderr, "Cannot find GPU, use CPU instead."

blob = caffe_pb2.BlobProto()
blob.ParseFromString(open(mean_file).read())
mean = blobproto_to_array(blob)[0]

tform = caffe.io.Transformer({"data": model.blobs["data"].data.shape})
tform.set_mean("data", mean)
tform.set_transpose("data", (2, 0, 1))


def convert_db_to_arr(db_path):
    db = leveldb.LevelDB(db_path)
    data, labels = [], []

    for item in db.RangeIter():
        datum = caffe_pb2.Datum()
        datum.ParseFromString(item[1])
        data.append(datum_to_array(datum))
        labels.append(datum.label)
#PRETRAINED = ROOT.replace("python", "yamashita/deeplearning/caffemodel/set0/snapshot/bestgoromaru.caffemodel")

##set2での学習
MODEL_FILE = ROOT.replace("python", "yamashita/deeplearning/caffemodel/set1/deploy.prototxt")
PRETRAINED = ROOT.replace("python", "yamashita/deeplearning/caffemodel/set1/snapshot/bestgoromaru.caffemodel")

IMAGE_DIR = ROOT.replace("src/python", "data/rugby/video/video_5minsep/")

cascade = cv2.CascadeClassifier("/usr//local/Cellar/opencv/2.4.11_1/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml")

if __name__ == '__main__':

    blob = caffe_pb2.BlobProto()
    with open(MEAN_FILE, 'rb') as fp:
        blob.ParseFromString(fp.read())
    numpy_mean = blobproto_to_array(blob)
    numpy_mean = numpy_mean.mean(0)

    # load classifier
    net = caffe.Classifier(MODEL_FILE, PRETRAINED,
                           mean=numpy_mean.mean(1).mean(1),
                           channel_swap=(2,1,0),
                           raw_scale=255,
                           image_dims=(100, 100))
    
    imglist = glob.glob(IMAGE_DIR+"*") 

    for photo in imglist:
        photo = IMAGE_DIR + "0386.png"
        # load image file to be predicted
        input_image = caffe.io.load_image(photo)
Ejemplo n.º 17
0
MakemodelLabelDictFile = '/home/ygao/Projects/VehicleRecogntition/Data/V1_AAuMColor/_LabelList_SL/Makemodel/Makemodel_ClassLabelDict.txt'

LabelClassNameDict = Va.ReadLabelClassNameDict(MakemodelLabelDictFile ) 


caffe.set_mode_cpu()
net = caffe.Net( NetPrototxtFile, NetBinaryModel, caffe.TEST )

data_shape = net.blobs['data'].data.shape
print( 'Data layer shape: {0}'.format(data_shape ) )

# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
meanfd = open( MeanFile, 'rb')
meanblob = caffe_pb2.BlobProto()
meanblob.MergeFromString( meanfd.read() )
meandata= io.blobproto_to_array(meanblob)[0]
print( 'mean data shape is {0}'.format( meandata.shape ) )

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', meandata) # mean pixel
#transformer.set_raw_scale('data',  0.00390625)  # the reference model operates on images in [0,255] range instead of [0,1]. python represent image in [0,1] already
transformer.set_raw_scale('data',  255)
transformer.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB

net.blobs['data'].reshape(1,3, data_shape[-2], data_shape[-1] )
t_data = transformer.preprocess('data', caffe.io.load_image(ExampleImage))
net.blobs['data'].data[...] = t_data
print( "t_data shape: {0}".format(t_data.shape) )
#plt.imshow(transformer.deprocess('data', net.blobs['data'].data[0]))
#plt.show()
Ejemplo n.º 18
0
def init_net():
    pycaffe_dir = os.path.dirname(__file__)

    parser = argparse.ArgumentParser()
    parser.add_argument("--gpu",
                        action='store_true',
                        help="Switch for gpu computation.")
    parser.add_argument(
        "--center_only",
        action='store_true',
        help="Switch for prediction from center crop alone instead of " +
        "averaging predictions across crops (default).")
    parser.add_argument(
        "--images_dim",
        default='227,227',
        help="Canonical 'height,width' dimensions of input images.")
    parser.add_argument(
        "--input_scale",
        type=float,
        help="Multiply input features by this scale to finish preprocessing.")
    parser.add_argument(
        "--raw_scale",
        type=float,
        default=255.0,
        help="Multiply raw input by this scale before preprocessing.")
    parser.add_argument(
        "--channel_swap",
        default='2,1,0',
        help="Order to permute input channels. The default converts " +
        "RGB -> BGR since BGR is the Caffe default by way of OpenCV.")
    parser.add_argument(
        "--ext",
        default='jpg',
        help="Image file extension to take as input when a directory " +
        "is given as the input file.")
    args = parser.parse_args()

    image_dims = [int(s) for s in args.images_dim.split(',')]

    if not os.path.exists(MEAN_FILE):
        print("creating mean file..")
        blob = caffe_pb2.BlobProto()
        with open(MEAN_FILE.rstrip("npy") + "binaryproto", "rb") as fp:
            blob.ParseFromString(fp.read())
        np.save("places365CNN_mean.npy", blobproto_to_array(blob))

    channel_swap = [int(s) for s in args.channel_swap.split(',')]

    # if args.gpu:
    #     caffe.set_mode_gpu()
    #     caffe.set_device(2)
    #     print("GPU mode")
    # else:
    #     caffe.set_mode_cpu()
    #     print("CPU mode")

    # Make classifier.
    classifier = caffe.Classifier(MODEL_FILE,
                                  PRETRAINED,
                                  image_dims=image_dims,
                                  mean=np.load(MEAN_FILE).reshape(
                                      (3, 256, 256)).mean(1).mean(1),
                                  input_scale=args.input_scale,
                                  raw_scale=args.raw_scale,
                                  channel_swap=channel_swap)
    return classifier
Ejemplo n.º 19
0
def init_net():
    pycaffe_dir = os.path.dirname(__file__)

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--gpu",
        action='store_true',
        help="Switch for gpu computation."
    )
    parser.add_argument(
        "--center_only",
        action='store_true',
        help="Switch for prediction from center crop alone instead of " +
             "averaging predictions across crops (default)."
    )
    parser.add_argument(
        "--images_dim",
        default='227,227',
        help="Canonical 'height,width' dimensions of input images."
    )
    parser.add_argument(
        "--input_scale",
        type=float,
        help="Multiply input features by this scale to finish preprocessing."
    )
    parser.add_argument(
        "--raw_scale",
        type=float,
        default=255.0,
        help="Multiply raw input by this scale before preprocessing."
    )
    parser.add_argument(
        "--channel_swap",
        default='2,1,0',
        help="Order to permute input channels. The default converts " +
             "RGB -> BGR since BGR is the Caffe default by way of OpenCV."
    )
    parser.add_argument(
        "--ext",
        default='jpg',
        help="Image file extension to take as input when a directory " +
             "is given as the input file."
    )
    args = parser.parse_args()

    image_dims = [int(s) for s in args.images_dim.split(',')]

    if not os.path.exists(MEAN_FILE):
        print("creating mean file..")
        blob = caffe_pb2.BlobProto()
        with open(MEAN_FILE.rstrip("npy")+"binaryproto", "rb") as fp:
            blob.ParseFromString(fp.read())
        np.save("places365CNN_mean.npy", blobproto_to_array(blob))

    channel_swap = [int(s) for s in args.channel_swap.split(',')]

    # if args.gpu:
    #     caffe.set_mode_gpu()
    #     caffe.set_device(2)
    #     print("GPU mode")
    # else:
    #     caffe.set_mode_cpu()
    #     print("CPU mode")

    # Make classifier.
    classifier = caffe.Classifier(MODEL_FILE, PRETRAINED,
        image_dims=image_dims,
        mean=np.load(MEAN_FILE).reshape((3, 256, 256)).mean(1).mean(1),
        input_scale=args.input_scale,
        raw_scale=args.raw_scale,
        channel_swap=channel_swap
    )
    return classifier
Ejemplo n.º 20
0
def load_mean(mean_file=MEAN_FILE):
    blob = caffe_pb2.BlobProto()
    data = open(mean_file, "rb").read()
    blob.ParseFromString(data)
    nparray = blobproto_to_array(blob)
    return nparray[0]