RuntimeError: Exception thrown in otbApplication Application_Execute: /src/otb/otb/Modules/Remote/otbtf/include/otbTensorflowGraphOperations.cxx:178: itk::ERROR: Tensor name "prediction" not found

I ran into this issue where I created a Simple CNN model and used otbtf TensorflowModelTrain defined below but I’m runtime error because of this statement, “pp.SetParameterString(“training.source2.placeholder”, “prediction”)”.

Please share the thoughts on how to resolve this

app = otbApplication.Registry.CreateApplication("TensorflowModelTrain")

    # Set the input parameters
    app.SetParameterStringList("training.source1.il", [datapath + out_patches_A])
    app.SetParameterInt("training.source1.patchsizex", 16)
    app.SetParameterInt("training.source1.patchsizey", 16)
    app.SetParameterString("training.source1.placeholder", "x")

    app.SetParameterStringList("training.source2.il", [datapath + out_labels_A])
    app.SetParameterInt("training.source2.patchsizex", 1)
    app.SetParameterInt("training.source2.patchsizey", 1)
    app.SetParameterString("training.source2.placeholder", "prediction")

    app.SetParameterString("model.dir", "/home/otbuser/all/data/model2.pb/")
    app.SetParameterStringList("training.targetnodes", ["optimizer"])
    app.SetParameterString("validation.mode", "class")

    app.SetParameterStringList("validation.source1.il", [datapath + out_patches_B])
    app.SetParameterString("validation.source1.name", "x")

    app.SetParameterStringList("validation.source2.il", [datapath + out_labels_B])
    app.SetParameterString("validation.source2.name", "prediction")

    app.SetParameterString("model.saveto", "/home/otbuser/all/data/")

    # Execute the application
    app.Execute()

This is the CNN model,

class Model2(tf.keras.Model):
    def __init__(self, nclasses):
        super(Model2, self).__init__()
        self.nclasses = nclasses
        self.conv1 = tf.keras.layers.Conv2D(16, (5, 5), padding="valid", activation=tf.nn.relu)
        self.pool1 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)
        self.conv2 = tf.keras.layers.Conv2D(16, (3, 3), padding="valid", activation=tf.nn.relu)
        self.pool2 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)
        self.conv3 = tf.keras.layers.Conv2D(32, (2, 2), padding="valid", activation=tf.nn.relu)
        self.flatten = tf.keras.layers.Flatten()
        self.estimated = tf.keras.layers.Dense(128, activation=tf.nn.relu)
        self.estimated2 = tf.keras.layers.Dense(nclasses, activation=None)

    def call(self, inputs):
        x = self.conv1(inputs)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.pool2(x)
        x = self.conv3(x)
        features = self.flatten(x)
        estimated = self.estimated(features)
        estimated2 = self.estimated2(estimated)
        estimated_label = tf.argmax(estimated2, axis=1, name="prediction")
        return (estimated2, estimated_label)

Hi @zeros_ones ,

To build your model in python please read the otbtf API documentation and the tutorial for keras

I don’t know how it is possible to train a SavedModel build with keras from the otbcli_TensorflowModelTrain application. You could try to find the names of the actual inputs/outputs/targets using saved_model_cli show --dir <your savedmodel dir> --all, but I never tried.

If you intend to use TF2/Keras, I recommend to train your model using the keras API.

Hope that helps,

Rémi