如何将输入数据传递给现有的 TensorFlow 2.x 模型(使用 Java)?

huangapple go评论84阅读模式
英文:

How to pass input data to an existing tensorflow 2.x model in Java?

问题

以下是翻译好的部分:

我正在使用`tensorflow`迈出我的第一步在使用Python为MNIST数据创建了一个简单的模型之后我现在想将这个模型导入Java并在分类中使用它然而我无法将输入数据传递给模型

这是用于模型创建的Python代码

     from tensorflow.keras.datasets import mnist
     from tensorflow.keras.utils import to_categorical

     (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

     train_images = train_images.reshape((60000, 28, 28, 1))
     train_images = train_images.astype('float32')
     train_images /= 255

     test_images = test_images.reshape((10000, 28, 28, 1))
     test_images = test_images.astype('float32')
     test_images /= 255

     train_labels = to_categorical(train_labels)
     test_labels = to_categorical(test_labels)

     NrTrainimages = train_images.shape[0]
     NrTestimages = test_images.shape[0]

     import os
     import numpy as np

     from tensorflow.keras.callbacks import TensorBoard
     from tensorflow.keras.models import Sequential
     from tensorflow.keras.layers import Dense, Dropout, Flatten
     from tensorflow.keras.layers import Conv2D, MaxPooling2D
     from tensorflow.keras import backend as K

     # 网络结构
     model = Sequential()
     mnist_inputshape = train_images.shape[1:4]

     # 卷积块1
     model.add(Conv2D(32, kernel_size=(5,5), 
           activation = 'relu',
           input_shape=mnist_inputshape,
           name = 'Input_Layer'))
     model.add(MaxPooling2D(pool_size=(2,2)))
     # 卷积块2
     model.add(Conv2D(64, kernel_size=(5,5),activation= 'relu'))
     model.add(MaxPooling2D(pool_size=(2,2)))
     model.add(Dropout(0.5))

     # 预测块
     model.add(Flatten())
     model.add(Dense(128, activation='relu', name='features'))
     model.add(Dropout(0.5))
     model.add(Dense(64, activation='relu'))
     model.add(Dense(10, activation='softmax', name = 'Output_Layer'))

     model.compile(loss='categorical_crossentropy',
                  optimizer='Adam',
                  metrics=['accuracy'])

     LOGDIR = "logs"
     my_tensorboard = TensorBoard(log_dir = LOGDIR,
           histogram_freq=0,
           write_graph=True,
           write_images=True)
     my_batch_size = 128
     my_num_classes = 10
     my_epochs = 5

     history = model.fit(train_images, train_labels,
           batch_size=my_batch_size,
           callbacks=[my_tensorboard],
           epochs=my_epochs,
           use_multiprocessing=False,
           verbose=1,
           validation_data=(test_images, test_labels))

     score = model.evaluate(test_images, test_labels)

     modeldir = 'models'
     model.save(modeldir, save_format = 'tf')

对于Java,我试图调整在此处发布的App.java代码。

我正在努力替换这个片段:

     Tensor result = s.runner()
          .feed("input_tensor", inputTensor)
          .feed("dropout/keep_prob", keep_prob)
          .fetch("output_tensor")
          .run().get(0);

在这段代码中,使用了特定的输入张量来传递数据,而在我的模型中,只有层,没有单独的命名张量。因此,以下代码无法工作:

     Tensor<?> result = s.runner()
          .feed("Input_Layer/kernel", inputTensor)
          .fetch("Output_Layer/kernel")
          .run().get(0);

我应该如何在Java中传递数据并获取模型的输出?


<details>
<summary>英文:</summary>
I&#39;m doing my first steps with `tensorflow`. After having created a simple model for MNIST data in Python, I now want to import this model into Java and use it for classification. However, I don&#39;t manage to pass the input data to the model.
Here is the Python code for model creation:
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical.
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype(&#39;float32&#39;)
train_images /= 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype(&#39;float32&#39;)
test_images /= 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
NrTrainimages = train_images.shape[0]
NrTestimages = test_images.shape[0]
import os
import numpy as np
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras import backend as K
# Network architecture
model = Sequential()
mnist_inputshape = train_images.shape[1:4]
# Convolutional block 1
model.add(Conv2D(32, kernel_size=(5,5), 
activation = &#39;relu&#39;,
input_shape=mnist_inputshape,
name = &#39;Input_Layer&#39;))
model.add(MaxPooling2D(pool_size=(2,2)))
# Convolutional block 2
model.add(Conv2D(64, kernel_size=(5,5),activation= &#39;relu&#39;))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
# Prediction block
model.add(Flatten())
model.add(Dense(128, activation=&#39;relu&#39;, name=&#39;features&#39;))
model.add(Dropout(0.5))
model.add(Dense(64, activation=&#39;relu&#39;))
model.add(Dense(10, activation=&#39;softmax&#39;, name = &#39;Output_Layer&#39;))
model.compile(loss=&#39;categorical_crossentropy&#39;,
optimizer=&#39;Adam&#39;,
metrics=[&#39;accuracy&#39;])
LOGDIR = &quot;logs&quot;
my_tensorboard = TensorBoard(log_dir = LOGDIR,
histogram_freq=0,
write_graph=True,
write_images=True)
my_batch_size = 128
my_num_classes = 10
my_epochs = 5
history = model.fit(train_images, train_labels,
batch_size=my_batch_size,
callbacks=[my_tensorboard],
epochs=my_epochs,
use_multiprocessing=False,
verbose=1,
validation_data=(test_images, test_labels))
score = model.evaluate(test_images, test_labels)
modeldir = &#39;models&#39;
model.save(modeldir, save_format = &#39;tf&#39;)
For `Java`, I am trying to adapt the `App.java` code published [here][1].
I am struggling with replacing this snippet:
Tensor result = s.runner()
.feed(&quot;input_tensor&quot;, inputTensor)
.feed(&quot;dropout/keep_prob&quot;, keep_prob)
.fetch(&quot;output_tensor&quot;)
.run().get(0);
While in this code, a particular input tensor is used to pass the data, in my model, there are only layers and no individual named tensors. Thus, the following doesn&#39;t work:
Tensor&lt;?&gt; result = s.runner()
.feed(&quot;Input_Layer/kernel&quot;, inputTensor)
.fetch(&quot;Output_Layer/kernel&quot;)
.run().get(0);
How do I pass the data to and get the output from my model in Java?
[1]: https://github.com/kalaspuffar/tensorflow-mnist-test/tree/master/src/main/java/org/ea/tensorflow
</details>
# 答案1
**得分**: 1
我终于成功找到了解决方法。为了获取图中的所有张量名称,我使用了以下代码:
```java
for (Iterator it = smb.graph().operations(); it.hasNext();) {
Operation op = (Operation) it.next();
System.out.println("Operation name: " + op.name());
}

通过这个,我找到了以下有效的方法:

SavedModelBundle smb = SavedModelBundle.load("./model", "serve");
Session s = smb.session();

Tensor<Float> inputTensor = Tensor.<Float>create(imagesArray, Float.class);
Tensor<Float> result = s.runner()
        .feed("serving_default_Input_Layer_input", inputTensor)
        .fetch("StatefulPartitionedCall")
        .run().get(0).expect(Float.class);
英文:

I finally managed to find a solution. To get all the tensor names in the graph, I used the following code:

		for (Iterator it = smb.graph().operations(); it.hasNext();) {
Operation op = (Operation) it.next();
System.out.println(&quot;Operation name: &quot; + op.name());
}

From this, I figured out that the following works:

		SavedModelBundle smb = SavedModelBundle.load(&quot;./model&quot;, &quot;serve&quot;);
Session s = smb.session();
Tensor&lt;Float&gt; inputTensor = Tensor.&lt;Float&gt;create(imagesArray, Float.class);
Tensor&lt;Float&gt; result = s.runner()
.feed(&quot;serving_default_Input_Layer_input&quot;, inputTensor)
.fetch(&quot;StatefulPartitionedCall&quot;)
.run().get(0).expect(Float.class);

答案2

得分: 1

使用最新版本的TensorFlow Java,您无需自行查找模型签名或图形中输入/输出张量的名称。您只需调用以下方法:

try (SavedModelBundle model = SavedModelBundle.load("./model", "serve");
     Tensor<TFloat32> image = TFloat32.tensorOf(...); // 有许多方法可以传递图像字节
     Tensor<TFloat32> result = model.call(image).expect(TFloat32.DTYPE)) {
    System.out.println("Result is " + result.data().getFloat());
}

TensorFlow Java将自动处理将您的输入/输出张量映射到正确的节点。

英文:

With the newest version of TensorFlow Java, you don't need to search for yourself the name of the input/output tensors from the model signature or from the graph. You can simply call the following:

try (SavedModelBundle model = SavedModelBundle.load(&quot;./model&quot;, &quot;serve&quot;);
     Tensor&lt;TFloat32&gt; image = TFloat32.tensorOf(...); // There a many ways to pass you image bytes here
     Tensor&lt;TFloat32&gt; result = model.call(image).expect(TFloat32.DTYPE)) {
    System.out.println(&quot;Result is &quot; + result.data().getFloat());
  }
}

TensorFlow Java will automatically take care of mapping your input/output tensors to the right nodes.

huangapple
  • 本文由 发表于 2020年10月3日 20:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64184509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定