Ask Your Question

Revision history [back]

layer tf.shape forbidden in opencv?

Hi,

i have got this error :

cv2.error: OpenCV(4.1.0-pre) G:\Lib\opencv\modules\dnn\src\dnn.cpp:524: error: (-2:Unspecified error) Can't create layer "mydataShape" of type "Shape" in function 'cv::dnn::dnn4_v20190122::LayerData::getLayerInstance'

in my graph I have got this :

batch_size = tf.shape(trainDataNode,name="mydataShape")[0]

I read this https://github.com/opencv/opencv/issues/10210#issuecomment-348454604 Does it mean that tf.shape cannot be used in opencv?

layer tf.shape forbidden in opencv?

Hi,

i have got this error :

cv2.error: OpenCV(4.1.0-pre) G:\Lib\opencv\modules\dnn\src\dnn.cpp:524: error: (-2:Unspecified error) Can't create layer "mydataShape" of type "Shape" in function 'cv::dnn::dnn4_v20190122::LayerData::getLayerInstance'

in my graph I have got this :

batch_size = tf.shape(trainDataNode,name="mydataShape")[0]

I read this https://github.com/opencv/opencv/issues/10210#issuecomment-348454604 and http://answers.opencv.org/question/176790/another-tensorflow-import-problem/

Does it mean that tf.shape cannot be used in opencv?

@dkurt

layer tf.shape forbidden in opencv?

Hi,

i have got this error :

cv2.error: OpenCV(4.1.0-pre) G:\Lib\opencv\modules\dnn\src\dnn.cpp:524: error: (-2:Unspecified error) Can't create layer "mydataShape" of type "Shape" in function 'cv::dnn::dnn4_v20190122::LayerData::getLayerInstance'

in my graph I have got this :

batch_size = tf.shape(trainDataNode,name="mydataShape")[0]

I read this https://github.com/opencv/opencv/issues/10210#issuecomment-348454604 and http://answers.opencv.org/question/176790/another-tensorflow-import-problem/

Does it mean that tf.shape cannot be used in opencv?

@dkurt

My code (sorry it is 200 lines)is a fork of https://github.com/matroid/dlwithtf . When NOSHAPE is True I can use .pb but when it is false I cannot.

def model(data,train=False):
    # couche de convolution tf.conv2d
    conv1 = tf.nn.conv2d(trainDataNode,conv1Filtres,strides=[1, 1, 1, 1],padding='SAME',name = 'nConv2d1')
    # ajout du biais tf.nn.bias_add
    conv1plusbiais = tf.nn.bias_add(conv1, conv1Biais,name = 'nC1plusB1')
    # Couche RELU pour non linéarité.
    relu1 = tf.nn.relu(conv1plusbiais)
    # Couche Max pooling. recherche du maximum dans un tenseur de taille 1x2x2x1 at décalage de 1x2x2x1
    # résultat un tenseur de taille à peu près égal à la moitié du tenseur d'entrée
    # padding same si un noyau est incomplet il sera complété 
    groupement1 = tf.nn.max_pool(relu1,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='SAME',name = 'maxPool1')
    conv2 = tf.nn.conv2d(groupement1,conv2Filtres,strides=[1, 1, 1, 1],padding='SAME')
    relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2Biais))
    groupement2 = tf.nn.max_pool(relu2,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='SAME',name = 'maxPool2')
    # Redimensionnement de la couche groupement1 pour
    # fully connected layers.
    tailleGroupement2 = groupement2.get_shape().as_list()
    reshape = tf.reshape(groupement2,[tailleLots, tailleGroupement2[1] * tailleGroupement2[2] * tailleGroupement2[3]], name = 'reshape1')
    # Couche de réseau de neurones connectés Fully connected layer. 
    # broadcasts the biases.
    hidden1= tf.nn.relu(tf.matmul(reshape, poidsRN1) + biaisRN1,name='hiddenRN1')
    hidden1d = tf.layers.dropout(hidden1, 0.5, seed=SEEDINIT,name = 'nDrop')
    hidden2 = tf.matmul(hidden1d, poidsRN2,name='matmulRN2') + biaisRN2
    # Predictions for the current training minibatch.
    train_prediction = tf.nn.softmax(hidden2,name = 'noeudPrediction')

    if train:
        train_prediction = tf.nn.softmax(hidden2)
        loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=trainLabelsNode, logits=hidden2),name = 'noeudPerte')

        # L2 regularization for the fully connected parameters.
        regularizers = (tf.nn.l2_loss(poidsRN1)+ tf.nn.l2_loss(biaisRN1)+tf.nn.l2_loss(poidsRN2)+ tf.nn.l2_loss(biaisRN2))
        # Add the regularization term to the loss.
        loss += 5e-4 * regularizers

        # Optimizer: set up a variable that's incremented once per batch and
        # controls the learning rate decay.
        batch = tf.Variable(0, dtype=tf.float32)
        # Decay once per epoch, using an exponential schedule starting at 0.01.
        learning_rate = tf.train.exponential_decay(
            0.01,                # Base learning rate.
            batch * BATCH_SIZE,  # Current index into the dataset.
            trainSize,          # Decay step.
            0.95,                # Decay rate.
            staircase=True, name = 'noeudTaux')
        # Use simple momentum for the optimization.
        optimizer = tf.train.MomentumOptimizer(learning_rate,0.9).minimize(loss,global_step=batch,name = 'noeudOptimiseur')
        return optimizer, loss, learning_rate
    train_prediction = tf.nn.softmax(hidden2, name='noeudPrediction')
    return train_prediction, None, None


volume = 'f:/'
#Chargement des données
trainData = np.load(volume+"tmp/data/train_dataImpair.mnist.npy")
trainLabels = np.load(volume+"tmp/data/train_labelsImpair.mnist.npy")
testData = np.load(volume+"tmp/data/test_dataImpair.mnist.npy")
testLabels = np.load(volume+"tmp/data/test_labelsImpair.mnist.npy")

#
#Définition du modèle 
#
trainDataNode = tf.placeholder(tf.float32,shape=[None, 28, 28, 1],name = 'nDonnees')
if NOSHAPE:
    tailleLots = BATCH_SIZE
else:
    tailleLots = tf.shape(trainDataNode,name="mydataShape")[0]
#evalData = tf.placeholder(tf.float32,shape=[None, 28, 28, 1],name = 'nImage')
trainLabelsNode = tf.placeholder(tf.int64, shape=(None,),name = 'nLabel')

#Couche de convolution : 
# 32 filtres 5x5 à optimiser opérant sur une image en niveau de gris
conv1Filtres = tf.Variable(
    tf.truncated_normal([5, 5, 1, 32],
    stddev=0.1,seed=SEEDINIT,dtype=tf.float32),
    name = 'nFiltres1')
# ajout de biais après filtrage
conv1Biais = tf.Variable(tf.zeros([32], dtype=tf.float32),name = 'nBiais1')
conv2Filtres = tf.Variable(tf.truncated_normal([5, 5, 32, 64],
                                               stddev=0.1, seed=SEEDINIT,
                                               dtype=tf.float32), name = 'nConv2')
conv2Biais = tf.Variable(tf.zeros([64], dtype=tf.float32),name = 'nC2plusB2')

# fully connected, depth 512.
poidsRN1 = tf.Variable(tf.truncated_normal([7*7 * 64, 512],
                                           stddev=0.1,seed=SEEDINIT,
                                           dtype=tf.float32),
                       name='poidsRN1')
biaisRN1 = tf.Variable(tf.constant(0.1, shape=[512],dtype=tf.float32),
                       name='biaisRN1')

poidsRN2 = tf.Variable(tf.truncated_normal([512, NBLABEL],
                                           stddev=0.1,
                                           seed=SEEDINIT,
                                           dtype=tf.float32),
                       name='poidsRN2')
biaisRN2 = tf.Variable(tf.constant(0.1, shape=[NBLABEL], dtype=tf.float32),
                       name='biaisRN2')
trainSize = trainLabels.shape[0]
optimizer, loss, learning_rate = model(trainDataNode,True)
train_prediction, _, _ = model(trainDataNode,False)

# Préparation de la sauvegarde
enregistrer=tf.train.Saver()

num_epochs = 1 #NUM_EPOCHS
# Training computation: logits + cross-entropy loss.
#logits = model(train_data_node, True)

# Pour tensorboard
tf.summary.scalar("Erreur ", loss)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('/tmp/myLeNetEx2Impair', tf.get_default_graph())


with tf.Session() as sess:
    # Initialisation des variables du graphe
    tf.global_variables_initializer().run()
    # Boucle pour ajuster le modèle
    for step in range(0,100):
        # Extraction d'un lot de données
        offset = (step * BATCH_SIZE) % (trainSize - BATCH_SIZE)
        batch_data = trainData[offset:(offset + BATCH_SIZE), ...]
        batch_labels = trainLabels[offset:(offset + BATCH_SIZE)]
        # Préparation pour initialiser les données (images et labels)
        feed_dict = {trainDataNode: batch_data,trainLabelsNode: batch_labels}
        # Optimisation du modèle
        sess.run(optimizer, feed_dict=feed_dict)
        # Affichage 
        if step % EVAL_FREQUENCY == 0:
            # Extraction des noeuds du graphe
            l, summary,lr, predictions = sess.run([loss, merged, learning_rate,train_prediction],feed_dict=feed_dict)
            train_writer.add_summary(summary, step)
            print('Pas  ',step ,' -> epoque ' ,float(step) * BATCH_SIZE / trainSize)
            print('Minibatch perte : ',l,'  learning rate: ', lr)
            print('Minibatch Erreur: ', TauxErreur(predictions, batch_labels))
            print('Validation Erreur: ',TauxErreur(EvaluationDonneesTest(testData, sess), testLabels))


#    freeze_graph.freeze_graph("saved_model.pb","save.config",False,)
    tf.train.write_graph(tf.get_default_graph(),volume+"tmp/",
                     'saved_model.pbtxt', as_text=True)

    chemin = enregistrer.save(sess,volume+'tmp/model_final/a.ckpt')
    tf.train.write_graph(tf.get_default_graph(), volume+"tmp/model_final",
                         'saved_model.pbtxt', as_text=True)
    input_graph_path = volume+'tmp/model_final/saved_model.pbtxt'
    checkpoint_path = volume+'tmp/model_final/a.ckpt'
    input_binary = False
    output_node_names = "noeudPrediction"
    restore_op_name = "save/restore_all"
    filename_tensor_name = "save/Const:0"
    output_frozen_graph_name = volume+'tmp/model_final/frozen_a.pb'
    output_optimized_graph_name = volume+'tmp/model_final/optimized_a.pb'
    clear_devices = True

    print('FROZEN_GRAPH')
    freeze_graph.freeze_graph(input_graph_path, "",
                                input_binary, checkpoint_path, output_node_names,
                                restore_op_name, filename_tensor_name,
                                output_frozen_graph_name, clear_devices, "")
    print('OPTIMIZE_FOR_INFERENCE')
    inputGraph = tf.GraphDef()
    with tf.gfile.Open(output_frozen_graph_name, "rb") as f:
        data2read = f.read()
        inputGraph.ParseFromString(data2read)
    outputGraph = optimize_for_inference_lib.optimize_for_inference(
        inputGraph,
        ["nDonnees"],
        ["noeudPrediction"],
        tf.float32.as_datatype_enum)
    f = tf.gfile.FastGFile(volume+'tmp/model_final/OptimizedGraph.pb', "w")
    f.write(outputGraph.SerializeToString()) 
    print('****** END *****')