From 97ab1e1d86e5503fa5494ca2b0713419bbb2d86c Mon Sep 17 00:00:00 2001 From: Francisco Facioni Date: Mon, 17 Sep 2018 11:13:08 +0100 Subject: [PATCH] Adds darknet for testing --- mmdnn/conversion/examples/darknet/darknet.py | 10 +++- tests/test_conversion_imagenet.py | 51 ++++++++++++++++++-- tests/test_keras.py | 2 +- tests/test_tensorflow.py | 2 +- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/mmdnn/conversion/examples/darknet/darknet.py b/mmdnn/conversion/examples/darknet/darknet.py index 0297e998..4838cb60 100644 --- a/mmdnn/conversion/examples/darknet/darknet.py +++ b/mmdnn/conversion/examples/darknet/darknet.py @@ -42,8 +42,6 @@ class METADATA(Structure): _fields_ = [("classes", c_int), ("names", POINTER(c_char_p))] - - lib = CDLL("./mmdnn/conversion/examples/darknet/libdarknet.so", RTLD_GLOBAL) lib.network_width.argtypes = [c_void_p] lib.network_width.restype = c_int @@ -77,6 +75,7 @@ class METADATA(Structure): network_predict = lib.network_predict network_predict.argtypes = [c_void_p, POINTER(c_float)] +network_predict.restype = POINTER(c_float) reset_rnn = lib.reset_rnn reset_rnn.argtypes = [c_void_p] @@ -85,6 +84,9 @@ class METADATA(Structure): load_net.argtypes = [c_char_p, c_char_p, c_int] load_net.restype = c_void_p +free_network = lib.free_network +free_network.argtypes = [c_void_p] + do_nms_obj = lib.do_nms_obj do_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float] @@ -113,6 +115,10 @@ class METADATA(Structure): predict_image.argtypes = [c_void_p, IMAGE] predict_image.restype = POINTER(c_float) +network_outputs = lib.network_outputs +network_outputs.argtypes = [c_void_p] +network_outputs.restype = c_int + def classify(net, meta, im): out = predict_image(net, im) res = [] diff --git a/tests/test_conversion_imagenet.py b/tests/test_conversion_imagenet.py index 68087f10..c33b2c6c 100644 --- a/tests/test_conversion_imagenet.py +++ b/tests/test_conversion_imagenet.py @@ -6,6 +6,7 @@ import sys import imp import numpy as np +import ctypes from mmdnn.conversion.examples.imagenet_test import TestKit import utils @@ -319,6 +320,48 @@ def CntkEmit(original_framework, architecture_name, architecture_path, weight_pa return converted_predict + @staticmethod + def DarknetEmit(original_framework, architecture_name, architecture_path, weight_path, image_path): + try: + from mmdnn.conversion.examples.darknet import darknet as cdarknet + from mmdnn.conversion.darknet.darknet_emitter import DarknetEmitter + + # IR to code + converted_file = original_framework + '_darknet_' + architecture_name + "_converted" + converted_file = converted_file.replace('.', '_') + emitter = DarknetEmitter((architecture_path, weight_path)) + emitter.run(converted_file + '.py', converted_file + '.npy', 'test') + del emitter + del DarknetEmitter + + # import converted model + imported = imp.load_source('DarknetModel', converted_file + '.py') + + imported.make_net(converted_file + '.cfg') + imported.gen_weight(converted_file + '.npy', converted_file + '.weights') + + original_framework = checkfrozen(original_framework) + func = TestKit.preprocess_func[original_framework][architecture_name] + img = func(image_path) + img = np.ascontiguousarray(img, dtype=np.float32) + + model_converted = cdarknet.load_net((converted_file + '.cfg').encode(), (converted_file + '.weights').encode(), 0) + outputs = cdarknet.network_outputs(model_converted) + output = cdarknet.network_predict(model_converted, img.ctypes.data_as(ctypes.POINTER(ctypes.c_float))) + predict = np.ctypeslib.as_array(output, shape=(outputs,)) + converted_predict = np.squeeze(predict) + + cdarknet.free_network(model_converted) + del sys.modules['DarknetModel'] + os.remove(converted_file + '.py') + os.remove(converted_file + '.npy') + os.remove(converted_file + '.cfg') + os.remove(converted_file + '.weights') + + return converted_predict + except ImportError: + print("Cannot import Darknet. Darknet Emit is not tested.") + return None @staticmethod def TensorflowEmit(original_framework, architecture_name, architecture_path, weight_path, test_input_path): @@ -798,7 +841,7 @@ def OnnxEmit(original_framework, architecture_name, architecture_path, weight_pa }, 'keras' : { - 'vgg19' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], + 'vgg19' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit, DarknetEmit], 'inception_v3' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'resnet50' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'densenet' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], @@ -811,7 +854,7 @@ def OnnxEmit(original_framework, architecture_name, architecture_path, weight_pa }, 'mxnet' : { - 'vgg19' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], + 'vgg19' : [DarknetEmit, CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'imagenet1k-inception-bn' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'imagenet1k-resnet-18' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'imagenet1k-resnet-152' : [CaffeEmit, CntkEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], @@ -834,7 +877,7 @@ def OnnxEmit(original_framework, architecture_name, architecture_path, weight_pa }, 'tensorflow' : { - 'vgg19' : [CaffeEmit, CoreMLEmit, CntkEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], + 'vgg19' : [CaffeEmit, CoreMLEmit, CntkEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit, DarknetEmit], 'inception_v1' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], # TODO: CntkEmit 'inception_v3' : [CaffeEmit, CoreMLEmit, CntkEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'resnet_v1_152' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], # TODO: CntkEmit @@ -878,7 +921,7 @@ def OnnxEmit(original_framework, architecture_name, architecture_path, weight_pa 'alexnet' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'densenet201' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'inception_v3': [CaffeEmit, CoreMLEmit, KerasEmit, PytorchEmit, TensorflowEmit], # Mxnet broken https://github.com/apache/incubator-mxnet/issues/10194 - 'vgg19' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], + 'vgg19' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit, DarknetEmit], 'vgg19_bn' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], 'resnet152' : [CaffeEmit, CoreMLEmit, KerasEmit, MXNetEmit, PytorchEmit, TensorflowEmit], } diff --git a/tests/test_keras.py b/tests/test_keras.py index 0b43adce..644e5a9d 100644 --- a/tests/test_keras.py +++ b/tests/test_keras.py @@ -27,7 +27,7 @@ def get_test_table(): else: return { 'keras' : { - 'vgg19' : [TestModels.CaffeEmit, TestModels.CntkEmit, TestModels.CoreMLEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit], + 'vgg19' : [TestModels.CaffeEmit, TestModels.CntkEmit, TestModels.CoreMLEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit, TestModels.DarknetEmit], 'inception_v3' : [TestModels.CaffeEmit, TestModels.CntkEmit, TestModels.CoreMLEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit], 'resnet50' : [TestModels.CaffeEmit, TestModels.CntkEmit, TestModels.CoreMLEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit], 'densenet' : [TestModels.CaffeEmit, TestModels.CntkEmit, TestModels.CoreMLEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit], diff --git a/tests/test_tensorflow.py b/tests/test_tensorflow.py index b5fde243..228457cf 100644 --- a/tests/test_tensorflow.py +++ b/tests/test_tensorflow.py @@ -15,7 +15,7 @@ def get_test_table(): return { 'tensorflow' : { - 'vgg19' : [TestModels.CaffeEmit, TestModels.CoreMLEmit, TestModels.CntkEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit], + 'vgg19' : [TestModels.CaffeEmit, TestModels.CoreMLEmit, TestModels.CntkEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.DarknetEmit], 'inception_v1' : [TestModels.CaffeEmit, TestModels.CoreMLEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit], 'inception_v3' : [TestModels.CaffeEmit, TestModels.CoreMLEmit, TestModels.CntkEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit], 'resnet_v1_152' : [TestModels.CaffeEmit, TestModels.CoreMLEmit, TestModels.KerasEmit, TestModels.MXNetEmit, TestModels.PytorchEmit, TestModels.TensorflowEmit],