-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_util.py
102 lines (59 loc) · 3.15 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import caffe
import ipdb
import cv2
import numpy as np
import skimage
def crop_image(x, target_height=227, target_width=227):
image = skimage.img_as_float(skimage.io.imread(x)).astype(np.float32)
if len(image.shape) == 2:
image = np.tile(image[:,:,None], 3)
elif len(image.shape) == 4:
image = image[:,:,:,0]
height, width, rgb = image.shape
if width == height:
resized_image = cv2.resize(image, (target_height,target_width))
elif height < width:
resized_image = cv2.resize(image, (int(width * float(target_height)/height), target_width))
cropping_length = int((resized_image.shape[1] - target_height) / 2)
resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]
else:
resized_image = cv2.resize(image, (target_height, int(height * float(target_width) / width)))
cropping_length = int((resized_image.shape[0] - target_width) / 2)
resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]
return cv2.resize(resized_image, (target_height, target_width))
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_file = image_list[start:end]
image_batch = np.array(map(lambda x: crop_image(x, target_width=self.width, target_height=self.height), image_batch_file))
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