Path: blob/master/modules/dnn/test/imagenet_cls_test_inception.py
16356 views
import numpy as np1import sys2import os3import argparse4import tensorflow as tf5from tensorflow.python.platform import gfile6from imagenet_cls_test_alexnet import MeanValueFetch, DnnCaffeModel, Framework, ClsAccEvaluation7try:8import cv2 as cv9except ImportError:10raise ImportError('Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, '11'configure environment variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)')1213# If you've got an exception "Cannot load libmkl_avx.so or libmkl_def.so" or similar, try to export next variable14# before running the script:15# LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_core.so:/opt/intel/mkl/lib/intel64/libmkl_sequential.so161718class TensorflowModel(Framework):19sess = tf.Session20output = tf.Graph2122def __init__(self, model_file, in_blob_name, out_blob_name):23self.in_blob_name = in_blob_name24self.sess = tf.Session()25with gfile.FastGFile(model_file, 'rb') as f:26graph_def = tf.GraphDef()27graph_def.ParseFromString(f.read())28self.sess.graph.as_default()29tf.import_graph_def(graph_def, name='')30self.output = self.sess.graph.get_tensor_by_name(out_blob_name + ":0")3132def get_name(self):33return 'Tensorflow'3435def get_output(self, input_blob):36assert len(input_blob.shape) == 437batch_tf = input_blob.transpose(0, 2, 3, 1)38out = self.sess.run(self.output,39{self.in_blob_name+':0': batch_tf})40out = out[..., 1:1001]41return out424344class DnnTfInceptionModel(DnnCaffeModel):45net = cv.dnn.Net()4647def __init__(self, model_file, in_blob_name, out_blob_name):48self.net = cv.dnn.readNetFromTensorflow(model_file)49self.in_blob_name = in_blob_name50self.out_blob_name = out_blob_name5152def get_output(self, input_blob):53return super(DnnTfInceptionModel, self).get_output(input_blob)[..., 1:1001]545556if __name__ == "__main__":57parser = argparse.ArgumentParser()58parser.add_argument("--imgs_dir", help="path to ImageNet validation subset images dir, ILSVRC2012_img_val dir")59parser.add_argument("--img_cls_file", help="path to file with classes ids for images, download it here:"60"https://github.com/opencv/opencv_extra/tree/master/testdata/dnn/img_classes_inception.txt")61parser.add_argument("--model", help="path to tensorflow model, download it here:"62"https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip")63parser.add_argument("--log", help="path to logging file")64parser.add_argument("--batch_size", help="size of images in batch", default=1)65parser.add_argument("--frame_size", help="size of input image", default=224)66parser.add_argument("--in_blob", help="name for input blob", default='input')67parser.add_argument("--out_blob", help="name for output blob", default='softmax2')68args = parser.parse_args()6970data_fetcher = MeanValueFetch(args.frame_size, args.imgs_dir, True)7172frameworks = [TensorflowModel(args.model, args.in_blob, args.out_blob),73DnnTfInceptionModel(args.model, '', args.out_blob)]7475acc_eval = ClsAccEvaluation(args.log, args.img_cls_file, args.batch_size)76acc_eval.process(frameworks, data_fetcher)777879