Models: Error reshaping image in load_image_into_numpy_array

Created on 6 Oct 2017  路  6Comments  路  Source: tensorflow/models

Please go to Stack Overflow for help and support:

http://stackoverflow.com/questions/tagged/tensorflow

Also, please understand that many of the models included in this repository are experimental and research-style code. If you open a GitHub issue, here is our policy:

  1. It must be a bug or a feature request.
  2. The form below must be filled out.

Here's why we have that policy: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.


System information

  • What is the top-level directory of the model you are using:
    models/research/object_detection

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
    No

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
    Ubuntu 16.04
  • TensorFlow installed from (source or binary):
    conda
  • TensorFlow version (use command below):
python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
b'unknown' 1.3.0
  • Bazel version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:
  • Exact command to reproduce:

I get this error while running the object_detection_tutorial, but I will just put the relevant code

import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import glob

from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image

def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)

 image  = Image.open('small_images/RNAi.1.N2.E.D_A01-autolevel-300x300.jpeg')
image_np  = load_image_into_numpy_array(image)

## Output
ValueError                                Traceback (most recent call last)
<ipython-input-4-e1507f4b23ae> in <module>()
----> 1 image_np  = load_image_into_numpy_array(image)

<ipython-input-2-f35cf9e400b7> in load_image_into_numpy_array(image)
      2   (im_width, im_height) = image.size
      3   return np.array(image.getdata()).reshape(
----> 4       (im_height, im_width, 3)).astype(np.uint8)
      5 

ValueError: cannot reshape array of size 90000 into shape (300,300,3)

I have tried images with different sizes.

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with

python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"

Describe the problem

Describe the problem clearly here. Be sure to convey here why it's a bug in TensorFlow or a feature request.

In the object_detection_tutorial the load_image_into_numpy_array gets an error.

I'm not sure if its a bug or something wrong on my end.

Source code / logs

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.

Most helpful comment

I faced the same issue today and I tried converting the image to a numpy array using the OpenCV libraries and it worked.

import cv2

    #image_np = load_image_into_numpy_array(image)
    image_np = cv2.imread(image_path,1)

All 6 comments

I tried it out on the test_image, and its fine.

Is it because my images square?

I just cropped an image so it is not perfectly square, and it works.

I have been trying to get this function to work for my images, and I'm having a lot of trouble.

There are borders on the images that are not important, so I don't mind cropping them, but if I could do it within the code that would be great.

I have been playing with this: cropped = image.crop((0, 1, 300, 299)), but I still get an error.

For anyone else who is struggling with this, it seems that the error may come from image magick. I have an original bmp image, which works fine, but when I try to use the jpeg image, which I get with just convert image.bmp image.jpeg, the load_numpy_array dies.

However, if I take that image.jpeg and manually crop it using shotwell, its fine.

I'm closing this since the issue seems to be something wonky with the way I processed my images with imagemagick, as opposed to any of the code within tensorflow.

Thanks for the great project!

I faced the same issue today and I tried converting the image to a numpy array using the OpenCV libraries and it worked.

import cv2

    #image_np = load_image_into_numpy_array(image)
    image_np = cv2.imread(image_path,1)

I faced the same issue today and I tried converting the image to a numpy array using the OpenCV libraries and it worked.

import cv2

    #image_np = load_image_into_numpy_array(image)
    image_np = cv2.imread(image_path,1)

very easy and straightforward solution. Stay blessed.

Was this page helpful?
0 / 5 - 0 ratings