File Exchange

image thumbnail

Deep Learning : Darknet Importer

version 1.0.3 (345 KB) by Kei Otsuka
Import Deep Neural Networks Trained in the Darknet Framework


Updated 03 Jul 2019

View Version History

View License

This example shows how to import trained network from Darknet and how to assemble it for image classification. Importer included in this submission can be used to import trained network such as Darknet19 and Darknet53 that are well known as feature extractor for YOLOv2 and YOLOv3.
Please see Live script - tb_darknet2ml.mlx(Live Script) that shows how to import trained network from Darnket and how to assemble it for image classification. And also, importer can be used to import YOLO for object detection, but post processing to produce object detections need to be added outside this example.

[Japanese] 本例題では、Darknet上で学習されたネットワークをMATLABにインポートしてDAG Networkオブジェクトに変換し、画像分類を行う流れをご紹介しています。本例題に含まれるImporterを利用することで、YOLOv2やYOLOv3の特徴抽出器として著名なDarknet19やDarknet53をインポートして利用することができます。一連の流れをLive Script - tb_darknet2ml.mlxでご紹介していますのでご覧ください。また、Importerを利用してYOLOv2等の物体検出用ネットワークをインポートすることもできますが、別途後処理を記述する必要があります。

[Keyward] 画像処理・画像分類・物体検出・ディープラーニング・DeepLearning・デモ・IPCVデモ

Cite As

Kei Otsuka (2021). Deep Learning : Darknet Importer (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

Atsushi Ohashi


yolov3/yolov4 reference here ,weclome to star~~~

Mahmoud Afifi

Kei Otsuka

@cui :
Thank you very much for sharing your work! I think that new modules and functionalities such as exporter you created would be really helpful for MATLAB users.

As for your question related to conv bias, if Conv layer is followed by BN layer, this importer set the parameter of conv bias to BN layer as an offset, and set 0 to bias of Conv layer. This is to get the same behavior as Darknet.

layer_conv.Bias = zeros(1,1,filters,'single');
layer_bn = batchNormalizationLayer('Name', lname);
layer_bn.Offset = conv_bias;


Dear,Kei Otsuka,
I refactored your code, rearranged the network architecture based on Module, supported groupConvolution, fullyconnected, etc. In addition, I added the exportDarknetworks function. You can refer to here


Dear,Kei Otsuka,
From your "createConv2d" function, why does the conv bias take 0 instead of reading from the weghts when there is a BN layer?
If bn
layer_conv.Bias = zeros(1,1,filters,'single'); % why it set to zero???

Kei Otsuka

Hi Artem,

If you are not familiar with custom layers, I would suggest to use YOLOv2 that is officially supported by Computer Vision Toolbox for training.

Darknet Importer can be used to import Darknet53 as an feature extractor for YOLOv2 network.


Dear Kei Otsuka,
Thank you very much for this code.
I try to train yolo but get the error
Error using upsample2dLayer/predict
Method 'predict' is not defined for class 'upsample2dLayer' or is removed from MATLAB's search path.
Would you suggest how to around it?

Kei Otsuka

Hi Roberto,

Thank you for pointing it out.
Since TrainedMean is read-only parameter on 18a or earlier, can you use R2018b or later version?

I will update the information of release compatibility for this submission.

Roberto Opromolla

I tried to import a trained network by typing this command

[lgraph, anchors, classes] = importDarknetNetwork('yolov3-tiny-obj_3000.weights', 'yolov3-tiny-obj.cfg');

but I get this error

Darknet weights file version info
Major : 0 Minor : 1 Revision : 0
Loading cfg file
Creating graph of network layers for deep learing
Image input layer : Height - 416 Width - 416 Channel - 3
2D conv layer 1 : Filtersize - 16 : Stride - 1 : Read param# - 432
You cannot set the read-only property 'TrainedMean' of BatchNormalizationLayer.
Error in createConv2d (line 109)
layer_bn.TrainedMean = bn_mean;
Error in importDarknetNetwork (line 86)
[lgraph, readSize] = createConv2d(lgraph, layerInfo, fid, i);

Could you suggest me how to solve it?


Dear @Kei Otsuka:
I'wil try ,thans very much! 非常感谢!

Kei Otsuka

You can confirm both the number of total parameters and read parameters in command window. If the numbers match up, weights would be loaded successfully. Since YOLOv3-tiny makes prediction at two scales, two unused output would be expected after importing it into MATLAB.


when I load"yolov3-tiny.cfg","yolov3-tiny.weights", after analyzeNetwork , there are three errors,1, missing out put layer, 2,unused output "conv2d_10" ;3,unused output "conv2d_13", is't I successful load cfg and weights files ?

Tohru Kikawada

Zohaib Khan

Thank you.
The code requires 'Deep Learning Toolbox Importer for TensorFlow-Keras Models' add-on. Please add it to the description.

MATLAB Release Compatibility
Created with R2019a
Compatible with R2019a and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!