Last active
February 25, 2016 04:14
-
-
Save Drunkar/cb9f1fabc2bbc9e6cc19 to your computer and use it in GitHub Desktop.
Extract features and save to a file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import caffe | |
PROTOTEXT = "VGG_FACE_deploy.prototxt" | |
CAFFEMODEL = "VGG_FACE.caffemodel" | |
IMG_PATH = "ma_images/" | |
IMG_FILES = os.listdir(IMG_PATH) | |
OUTPUT_FILE = "train.csv" | |
#OUTPUT_FILE = "test.csv" | |
with open(OUTPUT_FILE, "w") as f: | |
for i, img_file in enumerate(IMG_FILES): | |
img = caffe.io.load_image(IMG_PATH + img_file) | |
img = img[:,:,::-1]*255.0 # convert RGB->BGR | |
avg = np.array([129.1863,104.7624,93.5940]) | |
img = img - avg # subtract mean (numpy takes care of dimensions :) | |
# adopt caffe format | |
img = img.transpose((2,0,1)) | |
img = img[None,:] # add singleton dimension | |
net = caffe.Net(PROTOTEXT, CAFFEMODEL, caffe.TEST) | |
net.forward_all( data = img ) | |
out = net.blobs["fc7"].data[0].flatten().tolist() | |
# write header | |
if i == 0: | |
headers = ["feature" + str(i) for i in range(len(out))] | |
header = ",".join(headers) | |
f.write("label," + header + "\n") | |
out = ",".join(map(str, out)) | |
label = "_".join(img_file[:-4].split("_")[2:]) | |
f.write(label + "," + out + "\n") | |
# reset net | |
net = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment