LoginSignup
1
1

More than 5 years have passed since last update.

Numpy array を str byte に変換する [TensorFlow]

Posted at

前書き

初投稿です.いつもQiitaさんにはお世話になっていますのでアウトプットしてみました.

ええと,かの有名なinception-2015-12-05.tgzの中のclassify_image_graph_def.pbで定義してあるInception-v3のgraphは,ndarrayを直接入力にすることができません.

そのまま入れようとすると,

ValueError: Cannot feed value of shape (720, 1280, 3) for Tensor 'image_feed:0', which has shape '()'

みたいなエラーが出ると思います.

OpenCVのVideoCaptureでwebカメラから取得した画像をモデルにぶち込もうとした時にこの問題に当たりました.

対処

最初はndarrayを一度ローカルに保存しておいて,tf.gfile.FastGFile(image_path, 'rb').read() を用いて再度ロードしていたんですけど,さすがにダサいので,

image = np.ndarray([227, 227, 3]) # こういうnumpy arrayがあったとします

image_tensor = tf.convert_to_tensor(np.uint8(image[:, :, ::-1].copy()))
encoded = tf.image.encode_jpeg(image_tensor)
encoded_data = sess.run(encoded)

logits = sess.run('softmax:0', {'DecodeJpeg/contents:0': encoded_data})

みたいな感じで変換するといいと思います.
以上です.

参考

How do I convert a numpy Array to a data type that tensorflow can classify? : https://stackoverflow.com/questions/42439360/how-do-i-convert-a-numpy-array-to-a-data-type-that-tensorflow-can-classify

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1