【问题标题】:Decoding B64 image in Tensorflow在 TensorFlow 中解码 B64 图像
【发布时间】:2022-01-22 21:20:49
【问题描述】:

我在处理 TensorFlow Java 中的图像编码/解码时遇到了麻烦。我需要处理 B64,因为我有一个来自 Google AutoML vision 的保存模型,它需要输入格式。明确地说,Maven 导入是:

<dependency>
  <groupId>org.tensorflow</groupId>
  <artifactId>tensorflow-core-platform</artifactId>
  <version>0.4.0</version>
</dependency>

以下最小示例显示了根本问题:

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import javax.activation.MimetypesFileTypeMap;

import org.apache.commons.codec.binary.Base64;
import org.tensorflow.Graph;
import org.tensorflow.Output;
import org.tensorflow.Session;
import org.tensorflow.op.image.DecodeJpeg;
import org.tensorflow.op.image.DecodeJpeg.Options;
import org.tensorflow.types.TString;
import org.tensorflow.types.TUint8;

public class tensorflowLoadMinimal{
    
    public static void main(String[] args) throws Exception{
        
            // Get a public JPG locally for example purposes
        String imgUrl = "https://file-examples-com.github.io/"
                + "uploads/2017/10/file_example_JPG_100kB.jpg";
        String localPath = "/tmp/imgFile.jpg";
        InputStream in = new URL(imgUrl).openStream();
        Files.copy(in, Paths.get(localPath), StandardCopyOption.REPLACE_EXISTING);
        
            // Sanity checking the JPG; base64 encode 
        File f = new File(localPath);
        System.out.println("Mime Type of " + f.getName() + " is " +
                new MimetypesFileTypeMap().getContentType(f));
        byte[] fileBytes = Files.readAllBytes(Paths.get(localPath));
        String encodedString = Base64.encodeBase64String(fileBytes);        

            // Make b64 string a tensor; wrap in TF structs 
        Graph graph = new Graph();
        Session s = new Session(graph);
        TString tensor = TString.scalarOf(encodedString);
        Output<TString> tensorAsOut = graph
            .opBuilder("Const", "imgPixels", graph.baseScope())
            .setAttr("dtype", tensor.dataType())
            .setAttr("value", tensor)
            .build()
            .<TString> output(0);
        
            // Try to decode b64 as Jpeg... and fail 
        Options[] opts = new Options[1];
        opts[0] = DecodeJpeg.channels(3L);
        DecodeJpeg dJpg = DecodeJpeg.create(graph.baseScope(), tensorAsOut, opts);
        Output<TUint8> jpgOut = dJpg.image();
        s.run(jpgOut);
        s.close();
    }   
}

它确认我有一个JPG文件,然后解码失败,抱怨输入格式不是图像文件,输出简洁:

Mime Type of imgFile.jpg is image/jpeg
...
Exception in thread "main" org.tensorflow.exceptions.TFInvalidArgumentException: Unknown image file format. One of JPEG, PNG, GIF, BMP required.
     [[{{node DecodeJpeg}}]]
    at org.tensorflow.internal.c_api.AbstractTF_Status.throwExceptionIfNotOK(AbstractTF_Status.java:87)
...
    at orc.tensorflowLoadMinimal.main(tensorflowLoadMinimal.java:55)

我哪里错了?

【问题讨论】:

    标签: java tensorflow deep-learning tensorflow2.0


    【解决方案1】:

    上面写着:

    Unknown image file format. One of JPEG, PNG, GIF, BMP required.
    

    这可以通过删除这个多余的部分来解决:

    // Try to decode b64 as Jpeg... and fail 
    Options[] opts = new Options[1];
    opts[0] = DecodeJpeg.channels(3L);
    DecodeJpeg dJpg = DecodeJpeg.create(graph.baseScope(), tensorAsOut, opts);
    Output<TUint8> jpgOut = dJpg.image();
    s.run(jpgOut);
    s.close();
    

    ...或者通过传递预期的参数JPEG,可能是DecodeJpeg.create()opts

    【讨论】:

    • (1) “多余”部分是必需的:“DecodeJpeg()”操作是驻留在已保存模型的执行图中的操作。我只是试图在这里隔离它们。此类命令需要在会话中运行才能实际执行。因此,虽然删除 's.run()' 行会导致程序正确完成,但它也不会做任何事情 (2) 预期的参数 JPEG 以变量的形式传递给 DecodeJpeg.create() 'tensorAsOut'——我认为它是 base64 字符串图像,包装为张量,包装为输出
    猜你喜欢
    • 2014-09-03
    • 2023-01-30
    • 2019-07-11
    • 2017-06-18
    • 2020-09-13
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    相关资源
    最近更新 更多