My tensorflow model for this example is simple and has been successfully loaded. My question is, to run inference, what do I pass the tf::Session? How do I create the feedDict mentioned here and what are my "outputOps"?
Here's the model in python:
model = tf.keras.Sequential([
tf.keras.layers.Convolution2D(filters, (5, 5), input_shape=(BoardDepth, BoardLength, BoardLength), data_format='channels_first', name='Input'),
tf.keras.layers.Activation('relu', name='Relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2), name='Pool'),
tf.keras.layers.Flatten(name="Flatten"),
tf.keras.layers.Dense(BoardSize, activation='softmax', name='Output'),
])
optimizer = tf.train.AdamOptimizer(learning_rate=0.0018)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
Here's the method used to train the model which uses a generator to feed images in:
earlyExit = keras.callbacks.EarlyStopping(monitor='val_acc', min_delta=0, patience=0)
# Train the model
history = model.fit_generator(generator=gen.generator(),
steps_per_epoch=gen.stepsPerEpoch(),
validation_data=valGen.generator(),
validation_steps=valGen.stepsPerEpoch(),
epochs=numEpochs,
verbose=2, workers=1, callbacks=[earlyExit])
And here's the method used to save the model:
K.set_learning_phase(0)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(tf.trainable_variables())
saver.save(sess, './models/test/latestModel')
In case you didn't follow the link above inference is supposed to be done with something like this:
// and run the inference to your liking
auto feedDict = ...
auto outputOps = ...
std::vector<tensorflow::Tensor> outputTensors;
status = session->Run(feedDict, outputOps, {}, &outputTensors);
I've tried following the example here but cannot seem to figure out what I need to pass as outputOps. Do I need to only pass the name of the output node, or do I need to pass every single operation in the model? The different configurations I've tried so far haven't worked.
My code so far looks something like this:
auto shape = tf::TensorShape({ BoardDepth, BoardSize, BoardSize});
// Placeholder until we're using actual input data
float inData[inputSize] = { 0.0 };
tf::Tensor input(tf::DT_FLOAT, shape);
std::copy_n(inData, inputSize, input.flat<float>().data());
std::vector<std::pair<std::string, tf::Tensor>> inputs = {
{ "Input/Conv2D", input },
};
std::vector<std::string> outOps = { "Output/bias:0"};
std::vector<tf::Tensor> outputs;
status = session->Run(inputs, outOps, {}, &outputs);
The input/output names are just set to names I could find that don't throw errors. I really don't know what I'm supposed to be setting them to in order for the model to function as it was trained.