-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaffe_classificator.py
More file actions
51 lines (35 loc) · 1.35 KB
/
caffe_classificator.py
File metadata and controls
51 lines (35 loc) · 1.35 KB
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
import caffe
import numpy as np
def create_caffe_net():
deploy_prototxt_path = "caffe_model/deploy.prototxt"
caffe_model_path = "caffe_model/snapshot_iter_14435.caffemodel"
caffe.set_mode_cpu()
net = caffe.Net(deploy_prototxt_path, caffe_model_path, caffe.TEST)
return net
def create_caffe_transformer(net):
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_channel_swap('data', (2,1,0))
transformer.set_raw_scale('data', 255.0)
return transformer
def classify_image(image_path):
img = caffe.io.load_image(image_path)
window_size = 227
net = create_caffe_net()
net.blobs['data'].reshape(1, 3, window_size, window_size)
transformer = create_caffe_transformer(net)
net.blobs['data'].data[...] = transformer.preprocess('data', img)
output = net.forward()
return output
def classify_images(images_paths):
imgs = []
net = create_caffe_net()
window_size = 227
net.blobs['data'].reshape(len(images_paths), 3, window_size, window_size)
transformer = create_caffe_transformer(net)
for image in images_paths:
img = caffe.io.load_image(image)
imgs.append(transformer.preprocess('data', img))
net.blobs['data'].data[...] = imgs
output = net.forward()
return output