title: Decoding B64 image in Tensorflow
body: <p>I'm having a terrible time dealing with image en/de-coding in TensorFlow Java. I need to handle the B64 because I have a saved model from Google AutoML vision that expects that input format. Just to be explicit the Maven import is:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;dependency&gt;
    &lt;groupId&gt;org.tensorflow&lt;/groupId&gt;
    &lt;artifactId&gt;tensorflow-core-platform&lt;/artifactId&gt;
    &lt;version&gt;0.4.0&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  <p>and the following minimal example shows the root issue:</p>
  <pre class="lang-java prettyprint-override"><code>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 = &quot;https://file-examples-com.github.io/&quot;
                  + &quot;uploads/2017/10/file_example_JPG_100kB.jpg&quot;;
          String localPath = &quot;/tmp/imgFile.jpg&quot;;
          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(&quot;Mime Type of &quot; + f.getName() + &quot; is &quot; +
                  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&lt;TString&gt; tensorAsOut = graph
              .opBuilder(&quot;Const&quot;, &quot;imgPixels&quot;, graph.baseScope())
              .setAttr(&quot;dtype&quot;, tensor.dataType())
              .setAttr(&quot;value&quot;, tensor)
              .build()
              .&lt;TString&gt; 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&lt;TUint8&gt; jpgOut = dJpg.image();
          s.run(jpgOut);
          s.close();
      }   
  }
  </code></pre>
  <p>It confirms I have a JPG file, and then fails to do the decoding, complaining the input format is not an image file, with succinct output:</p>
  <pre class="lang-none prettyprint-override"><code>Mime Type of imgFile.jpg is image/jpeg
  ...
  Exception in thread &quot;main&quot; 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)
  </code></pre>
  <p>Where am I going wrong?</p>