RahimEnt
RahimEnt

Reputation: 199

read leveldb(.ldb)features extracted using caffe

I have used the following command for feature extraction: ./build/tools/extract_features.bin models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel examples/_temp/imagenet_val.prototxt fc7 examples/_temp/features 10 leveldb GPU 0 the I used the following code to read leveldb features: https://15519285443829437810.googlegroups.com/attach/b71d5c99c447fc2a/main.cpp?part=0.1&view=1&vt=ANaJVrHM26ydsY5Z2ognvhBaFtDzXnE_SiVf19DLkrNpf9Q34t5O4bJWy0nHH_HBnBAVx5wZusXd1joX93JBK0_r7XKEIc-5odz9_HPHV1RUo8MD3zNHgoY

everything is OK and I have one .ldb file now. How can I read this .ldb file? Can I change it to .text?

Upvotes: 1

Views: 933

Answers (1)

RahimEnt
RahimEnt

Reputation: 199

I have used the following code :

import caffe
import leveldb
import numpy as np
from caffe.proto import caffe_pb2
db = leveldb.LevelDB('/home/deep/rahim/caffe-master/examples/_temp/features')
datum = caffe_pb2.Datum()

for key, value in db.RangeIter():
    datum.ParseFromString(value)

    label = datum.label
    data = caffe.io.datum_to_array(datum)

    image = np.transpose(data, (1,2,0))

    np.save('feature.txt',image)

Then pass the feature.txt.npy to the following code which convert .npy to .txt:

import struct
import numpy as np
import os

def parseNPY(path, fileJustName):
    # load from the file
    inputFile = os.path.join(path, fileJustName + ".npy")
    matrices = np.load(inputFile)

    outputfile = os.path.join(path, fileJustName)
    for m in range(matrices.shape[0]):
        # file name for this matrix
        outFileFull = outputfile + "-" + str(m) + ".txt"
        # output matrix to a numbered file
        np.savetxt(outFileFull, matrices[m], fmt="%i", delimiter="\t")


 mypath = "/home/deep/rahim/caffe-master/python/"

 for path, paths, filenames in os.walk(mypath):
 # translate all filenames.
 for filename in filenames:
    fileJustName, fileExtension = os.path.splitext(filename)
    if fileExtension == ".npy":
        print(os.path.join(path, fileJustName))
        parseNPY(path, fileJustName)

Upvotes: 1

Related Questions