-
Notifications
You must be signed in to change notification settings - Fork 3
/
caffeWrapper.py~
102 lines (73 loc) · 4.7 KB
/
caffeWrapper.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
usage from TFG folder: python tools/scripts/caffeWrapper.py
python tools/scripts/caffeWrapper.py foodCAT_googlenet_food101
python tools/scripts/caffeWrapper.py foodCAT_alexnet
python tools/scripts/caffeWrapper.py foodCAT_VGG_ILSVRC_19_layers
"""
import os
import sys
import caffe
PATH_TO_PROJECT=''
TEST=os.path.join(PATH_TO_PROJECT,'foodCAT/test.txt')
TEST_just_foodCAT=os.path.join(PATH_TO_PROJECT,'foodCAT/test_just_foodCAT.txt')
models = {"foodCAT_alexnet": os.path.join(PATH_TO_PROJECT, "models/foodCAT_alexnet/snapshots/bvlc_alexnet.caffemodel"),
"foodCAT_googlenet_food101": os.path.join(PATH_TO_PROJECT, "models/foodCAT_googlenet_food101/snapshots/first_TRAIN_73_91/ss_foodCAT_googlenet_food101_train_iter_490000.caffemodel"),
"foodCAT_VGG_ILSVRC_19_layers": os.path.join(PATH_TO_PROJECT, "models/foodCAT_VGG_ILSVRC_19_layers/snapshots/ss_foodCAT_VGG_ILSVRC_19_layers_train_iter_80000.caffemodel")}
# The solvers itselfs points to the network configuration (to TRAIN and VAL)
solvers = {"foodCAT_alexnet": os.path.join(PATH_TO_PROJECT, "models/foodCAT_alexnet/solver.prototxt"),
"foodCAT_googlenet_food101": os.path.join(PATH_TO_PROJECT, "models/foodCAT_googlenet_food101/solver.prototxt"),
"foodCAT_VGG_ILSVRC_19_layers": os.path.join(PATH_TO_PROJECT, "models/foodCAT_VGG_ILSVRC_19_layers/CLUSTER/solver.prototxt")}
####################### TEST ZONE
# We use the same file where we define the net and replace the TRAIN and VAL data layer by the TEST data layer
# i.e. Pointing to the test.txt in the data layer
# TODO Just use the same net from solvers replacing the TRAIN and VAL data layer by the TEST data layer (in a pycaffe way)
# Net pointing to the TEST data set
net_TEST = {"foodCAT_alexnet": os.path.join(PATH_TO_PROJECT, "models/foodCAT_alexnet/ TODO "),
"foodCAT_googlenet_food101": os.path.join(PATH_TO_PROJECT, "models/foodCAT_googlenet_food101/test.prototxt"),
"foodCAT_VGG_ILSVRC_19_layers": os.path.join(PATH_TO_PROJECT, "models/foodCAT_VGG_ILSVRC_19_layers/test.prototxt")}
# Net pointing to the TEST_just_foodCAT data set constrained just to the Catalan food
net_TEST_just_foodCAT = {"foodCAT_alexnet": os.path.join(PATH_TO_PROJECT, "models/foodCAT_alexnet/ TODO "),
"foodCAT_googlenet_food101": os.path.join(PATH_TO_PROJECT, "models/foodCAT_googlenet_food101/test_just_foodCAT.prototxt"),
"foodCAT_VGG_ILSVRC_19_layers": os.path.join(PATH_TO_PROJECT, "models/foodCAT_VGG_ILSVRC_19_layers/test_just_foodCAT.prototxt")}
diff_TEST_types = {'net_TEST':net_TEST, 'net_TEST_just_foodCAT':net_TEST_just_foodCAT}
####################### END TEST ZONE
####################### DEPLOY, to use it as the other people. REQUIRE IMAGE PREPROCESSING AS CAFFE DOES IN TRAIN. (OR NOT???)
deploy = {"foodCAT_alexnet": os.path.join(PATH_TO_PROJECT, "models/foodCAT_alexnet/ TODO "),
"foodCAT_googlenet_food101": os.path.join(PATH_TO_PROJECT, "models/foodCAT_googlenet_food101/deploy.prototxt"),
"foodCAT_VGG_ILSVRC_19_layers": os.path.join(PATH_TO_PROJECT, "models/foodCAT_VGG_ILSVRC_19_layers/CLUSTER/VGG_ILSVRC_19_layers_deploy.prototxt")}
mean = {"foodCAT_alexnet": os.path.join(PATH_TO_PROJECT, "models/foodCAT_alexnet/ TODO "),
"foodCAT_googlenet_food101": [104, 117, 123],
"foodCAT_VGG_ILSVRC_19_layers": [104, 117, 123]}
####################### END DEPLOY
def deployModel( modelName, modelType ):
""" modelType is which TEST dataset are you using: In our example could be 'net_TEST' or 'net_TEST_just_foodCAT'
modelName is which model we are using: Alexnet, googlenet, VGG_ILSVRC_19_layers, etc.
returns the net
"""
print 'Net definition: ', diff_TEST_types[modelType][modelName]
print 'weights: ', models[modelName]
# Set Caffe to GPU
caffe.set_device(0)
caffe.set_mode_gpu()
#caffe.set_mode_cpu()
# Assign net parameters
model_def = diff_TEST_types[modelType][modelName]
model_weights = models[modelName]
# Create the net is TEST mode
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)
return net
def hamming_distance(gt, est):
return sum([1 for (g, e) in zip(gt, est) if g == e]) / float(len(gt))
def check_accuracy(net, num_batches, batch_size = 128):
acc = 0.0
for t in range(num_batches):
net.forward()
gts = net.blobs['label'].data
ests = net.blobs['score'].data > 0
for gt, est in zip(gts, ests): #for each ground truth and estimated label vector
acc += hamming_distance(gt, est)
return acc / (num_batches * batch_size)
if __name__ == "__main__":
net = deployModel(sys.argv[1])