Skip to content

Commit

Permalink
Adds darknet for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Facioni committed Dec 10, 2018
1 parent 3dd4cbc commit 97ab1e1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
10 changes: 8 additions & 2 deletions mmdnn/conversion/examples/darknet/darknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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]

Expand Down Expand Up @@ -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 = []
Expand Down
51 changes: 47 additions & 4 deletions tests/test_conversion_imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import imp
import numpy as np
import ctypes

from mmdnn.conversion.examples.imagenet_test import TestKit
import utils
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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
Expand Down Expand Up @@ -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],
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 97ab1e1

Please sign in to comment.