简体   繁体   中英

java tensorflow api : Invalid JPEG data, size 0

First, when i tried LabelImage example with inception model ver 5, everything was good.

Then i tried with an older inception model ( ver3 ) and i saw that both model have different input and output.

In ver5, our input tensor name is : "Input" , with dtype = FLOAT.

In ver 3, our input tensor name is "DecodeJpeg/contents" , with dtype = STRING.

So i change LabelExample example with new name for both input and output : Tensor result = s.runner().feed("input", string_tensor_image).fetch("output") >> s.runner().feed("DecodeJpeg/contents", image).fetch("softmax") .

Also, i changed create new Image tensor for STRING type :

Tensor float_tensor = s.runner().fetch(output.op().name()).run().get(0);
byte[] bytes = new byte[float_tensor.numBytes()*64];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
res.writeTo(buffer);
long[] shape = {};
Tensor string_tensor = Tensor.create(DataType.STRING, shape, buffer);
return string_tensor;

It looked good when i printed both tensor :

FLOAT tensor with shape [1, 224, 224, 3]
STRING tensor with shape []

But after feeding to the graph, i get this error : Exception in thread "main" java.lang.IllegalArgumentException: Invalid JPEG data, size 0 [[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"]

I have tried everything i can, but no results. How can i fix it ? This is both inception model ver3 and ver 5 :

ver5 : https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip

ver 3 : http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz

The two models seem to be different and take different inputs. In particular, the DecodeJpeg op takes as input the raw contents of the JPEG image , which is why it is returning an error saying that it isn't being fed a JPEG-encoded image.

So you'd want to do something like:

byte[] input = Files.readAllBytes(Paths.get("/path/to/image.jpg"));
Tensor image = Tensor.create(input);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM