how to load a tensorflow PReLU layer using opencv DNN?

asked 2018-12-06 20:33:48 -0500

PhilT gravatar image

I am playing with the OpenCV DNN module to see if a simple tensorflow layer could be loaded.

I've followed the following steps.

1) build a simple model.

import os
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import *

if __name__ == '__main__':
    inputs = tf.placeholder(tf.float32, shape=[None, 10, 10, 3], name="input_image")
    with tf.variable_scope("DNN", reuse=reuse) as vs:
    tl.layers.set_name_reuse(reuse)

    inputs_level = InputLayer(inputs, name='input_level')

    net_feature = Conv2dLayer(inputs_level, shape=[3,3,3, 32], strides=[1,1,1,1], 
                    W_init=tf.contrib.layers.xavier_initializer(), 
                    name='init_conv')
    net_feature = PReluLayer(net_feature)

    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False))
    tl.layers.initialize_global_variables(sess)

    saver = tf.train.Saver()
    chkpt_dir = "_opencvdnn/"+"prelu"
    #os.mkdir("_opencvdnn")
    saver.save(sess, chkpt_dir)

2) generate a .pb file by running freeze_graph.py.

3) optimize the .pb by running the optimize_for_inference.py

However, when I am loading the model using the follwing line.

cvNet = cv2.dnn.readNetFromTensorflow('prelu.optimized.pb', 'prelu.opencv.pbtxt')

An exception is thrown:

OpenCV(3.4.4-dev) Error: Assertion failed (haveConst) in cv::dnn::experimental_dnn_34_v10::`anonymous-namespace'::TFImporter::populateNet, file E:\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 996

The net graph looks like this: image description

For your convenience to reproduce, I also attached the .pb file and .pbtxt ( with removed data) file. Please download at:

https://1drv.ms/f/s!AiwYAYFFwbTfguQ2l...

Best, PhilT

edit retag flag offensive close merge delete