Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/test/imagenet_cls_test_inception.py
16356 views
1
import numpy as np
2
import sys
3
import os
4
import argparse
5
import tensorflow as tf
6
from tensorflow.python.platform import gfile
7
from imagenet_cls_test_alexnet import MeanValueFetch, DnnCaffeModel, Framework, ClsAccEvaluation
8
try:
9
import cv2 as cv
10
except ImportError:
11
raise ImportError('Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, '
12
'configure environment variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)')
13
14
# If you've got an exception "Cannot load libmkl_avx.so or libmkl_def.so" or similar, try to export next variable
15
# before running the script:
16
# LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_core.so:/opt/intel/mkl/lib/intel64/libmkl_sequential.so
17
18
19
class TensorflowModel(Framework):
20
sess = tf.Session
21
output = tf.Graph
22
23
def __init__(self, model_file, in_blob_name, out_blob_name):
24
self.in_blob_name = in_blob_name
25
self.sess = tf.Session()
26
with gfile.FastGFile(model_file, 'rb') as f:
27
graph_def = tf.GraphDef()
28
graph_def.ParseFromString(f.read())
29
self.sess.graph.as_default()
30
tf.import_graph_def(graph_def, name='')
31
self.output = self.sess.graph.get_tensor_by_name(out_blob_name + ":0")
32
33
def get_name(self):
34
return 'Tensorflow'
35
36
def get_output(self, input_blob):
37
assert len(input_blob.shape) == 4
38
batch_tf = input_blob.transpose(0, 2, 3, 1)
39
out = self.sess.run(self.output,
40
{self.in_blob_name+':0': batch_tf})
41
out = out[..., 1:1001]
42
return out
43
44
45
class DnnTfInceptionModel(DnnCaffeModel):
46
net = cv.dnn.Net()
47
48
def __init__(self, model_file, in_blob_name, out_blob_name):
49
self.net = cv.dnn.readNetFromTensorflow(model_file)
50
self.in_blob_name = in_blob_name
51
self.out_blob_name = out_blob_name
52
53
def get_output(self, input_blob):
54
return super(DnnTfInceptionModel, self).get_output(input_blob)[..., 1:1001]
55
56
57
if __name__ == "__main__":
58
parser = argparse.ArgumentParser()
59
parser.add_argument("--imgs_dir", help="path to ImageNet validation subset images dir, ILSVRC2012_img_val dir")
60
parser.add_argument("--img_cls_file", help="path to file with classes ids for images, download it here:"
61
"https://github.com/opencv/opencv_extra/tree/master/testdata/dnn/img_classes_inception.txt")
62
parser.add_argument("--model", help="path to tensorflow model, download it here:"
63
"https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip")
64
parser.add_argument("--log", help="path to logging file")
65
parser.add_argument("--batch_size", help="size of images in batch", default=1)
66
parser.add_argument("--frame_size", help="size of input image", default=224)
67
parser.add_argument("--in_blob", help="name for input blob", default='input')
68
parser.add_argument("--out_blob", help="name for output blob", default='softmax2')
69
args = parser.parse_args()
70
71
data_fetcher = MeanValueFetch(args.frame_size, args.imgs_dir, True)
72
73
frameworks = [TensorflowModel(args.model, args.in_blob, args.out_blob),
74
DnnTfInceptionModel(args.model, '', args.out_blob)]
75
76
acc_eval = ClsAccEvaluation(args.log, args.img_cls_file, args.batch_size)
77
acc_eval.process(frameworks, data_fetcher)
78
79