Caffe 이미지 특징 추출(Python/C++)
1.Caffe 특징 추출(C++구현)
Caffe 프레임 워 크 는 해당 하 는 도구(build/tools/extractfeatures.bin)도구 extract features,공식 튜 토리 얼,사용 방법 은 다음 과 같 습 니 다.
extract_features.bin xxx.cafemodel xxxx.prototxt layer-name output-path mini-batches db-style xxx.cafemodel:훈련 된 모델 매개 변수 xxxx.prototxt:모델 정의(추출 할 그림 의 경로,mean-file 등 포함)layername:추출 할 특징의 이름(eg.fc6 fc7),중간 에 공백 으로 output-path:추출 한 feature 저장 경로 mini-batches:매번 batch크기 db -style:feature 에 저 장 된 형식(leveldb/lmdb)
자세 한 내용 은 공식 튜 토리 얼 을 참고 하여 상세 하 게 쓰 세 요.-장점:간단 하고 직접 사용 가능-부족:유연성 이 높 지 않 으 므 로 코드 를 작성 하여 해당 하 는 feature 를 분석 해 야 합 니 다.
2.Caffe 특징 추출(Python 구현)
본 고 는 Caffe 공식 튜 토리 얼 feature visualization 의 일부 코드 를 참고 하여 Python 을 이용 하여 이 루어 졌 으 며 그림 의 평균 값(mean-file)을 계산 하고 특징 추출 과 보존 을 하 였 으 며 구체 적 인 과정 은 다음 과 같다.1)입력 한 그림 의 평균 값 을 npy 형식 으로 저장 합 니 다.
#**input**
#work_path:working path containing the input file
#list_name:every line containing 'image_path label'
#save_name:where to save the mean file
#image_size: the mean_file's size
#channel: the mean_file's channnel
#**output**
#mean_file value(3*227*227)
def compute_image_mean(work_path,list_name = 'train.txt', save_name = '227_227_mean.npy', image_size = (227,227), channnel = 3):
mean = np.zeros((channnel,) + image_size, dtype=np.float64)
list = os.path.join(work_path,list_name)
with open(list, 'r') as f:
lines = f.readlines()
sample_size = len(lines)
count = 0
for i, line in enumerate(lines):
if(count%1000 == 0):
print ('Finish:%d\r
'%count)
image_name, label = line[:-1].split()
img = cv2.imread(image_name).astype(np.float32)
res = cv2.resize(img, image_size)
res = res.transpose(2, 0, 1)
mean += res
count += 1
mean = mean / sample_size
save_file = os.path.join(work_path,save_name)
np.save(save_file, mean)
return mean
2)전방 향 전 파 를 통 해 Feature 를 추출 하여 npy 형식 으로 저장
#**input**
#net:Caffe net
#mean_file:image mean file(c*h*w)
#input_file:input image file(image_path label)
#output_prefix_file(output_path),every image has a output_path
#layer_name:eg. 'fc6 fc7'
def computer_features(net,mean_file,input_file,output_prefix_file, layer_names = 'fc6 fc7'):
# load the mean image for subtraction
mu = np.load(mean_file)
# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1)) # move image channels to outermost dimension
transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2, 1, 0)) # swap channels from RGB to BGR
# set the size of the input (we can skip this if we're happy
# with the default; we can also change it later, e.g., for different batch sizes)
net.blobs['data'].reshape(batch_size, # batch size
3, # 3-channel (BGR) images
227, 227) # image size is 227x227
features = layer_names.split()
inputlines = []
outputlines = []
with open(input_file, 'r') as f:
for line in f:
inputlines.append(line)
with open(output_prefix_file, 'r') as f:
for line in f:
outputlines.append(line)
assert len(inputlines) == len(outputlines)
i = 0
for input,output in zip(inputlines,outputlines):
input = input.strip('
\t')
output = output.strip('
\t')
image_path,label = input.split()
# print image_path
input_image = caffe.io.load_image(image_path)
transformed_image = transformer.preprocess('data', input_image)
net.blobs['data'].data[...] = transformed_image
net.forward()
if not os.path.isdir(output):
os.makedirs(output)
for feature in features:
output_file = os.path.join(output,feature+'.npy')
np.save(output_file, net.blobs[feature].data[0])
if (i%1000 == 0):
print ('Finish:%d\r
' % i)
i += 1
model_def = 'models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
net = caffe.Net(model_def, # defines the structure of the model
model_weights, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)
mean_file = args.root + args.mean
input_file = args.root + args.inputs
output_file = args.root + args.outputs
caffe.set_mode_cpu()
computer_features(net,mean_file,input_file,output_file)
상기 코드 는 매번 1 장의 그림 만 처리 하고 데이터 의 양 이 많 으 면 느 릴 수 있 으 므 로 대량의 모델 로 계산 하여 GPU 의 장점 을 발휘 하 는 것 을 권장 합 니 다.변경 도 간단 합 니 다.만약 에 Batch-size 버 전이 필요 하 다 면 개인 적 으로 교류 할 수 있 습 니 다.python 을 이용 하여 caffe 특징 을 추출 하 는 가장 관건 적 인 것 은 net.blobs[fc6].data 입 니 다.다음 매개 변 수 를 추출 하여 저장 하면 됩 니 다.