-
Notifications
You must be signed in to change notification settings - Fork 95
/
cnn_util.py
77 lines (40 loc) · 1.98 KB
/
cnn_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import caffe
import ipdb
import cv2
import numpy as np
import skimage
deploy = '/home/taeksoo/Package/caffe/models/bvlc_reference_caffenet/deploy.prototxt'
model = '/home/taeksoo/Package/caffe/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
mean = '/home/taeksoo/Package/caffe/python/caffe/imagenet/ilsvrc_2012_mean.npy'
class CNN(object):
def __init__(self, deploy=deploy, model=model, mean=mean, batch_size=10, width=227, height=227):
self.deploy = deploy
self.model = model
self.mean = mean
self.batch_size = batch_size
self.net, self.transformer = self.get_net()
self.net.blobs['data'].reshape(self.batch_size, 3, height, width)
self.width = width
self.height = height
def get_net(self):
caffe.set_mode_gpu()
net = caffe.Net(self.deploy, self.model, caffe.TEST)
transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(self.mean).mean(1).mean(1))
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
return net, transformer
def get_features(self, image_list, layers='fc7', layer_sizes=[4096]):
iter_until = len(image_list) + self.batch_size
all_feats = np.zeros([len(image_list)] + layer_sizes)
for start, end in zip(range(0, iter_until, self.batch_size), \
range(self.batch_size, iter_until, self.batch_size)):
image_batch = image_list[start:end]
caffe_in = np.zeros(np.array(image_batch.shape)[[0,3,1,2]], dtype=np.float32)
for idx, in_ in enumerate(image_batch):
caffe_in[idx] = self.transformer.preprocess('data', in_)
out = self.net.forward_all(blobs=[layers], **{'data':caffe_in})
feats = out[layers]
all_feats[start:end] = feats
return all_feats