DCGAN-tensorflow icon indicating copy to clipboard operation
DCGAN-tensorflow copied to clipboard

ValueError: could not broadcast input array from shape (96,96,4) into shape (96,96)

Open jpchen opened this issue 7 years ago • 28 comments

I'm having the similar trouble as in #26. Not sure if I'm doing something wrong or if there's a fix. I'm using my own image dataset all scaled to 96x96. Stack trace below.

Image: PNG 96x96 96x96+0+0 8-bit sRGB 7.2KB 0.000u 0:00.000

Traceback (most recent call last):
  File "main.py", line 99, in <module>
    tf.app.run()
  File "/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "main.py", line 82, in main
    dcgan.train(FLAGS)
  File "/home/ubuntu/GANs/model.py", line 186, in train
    sample_inputs = np.array(sample).astype(np.float32)

jpchen avatar Apr 17 '17 05:04 jpchen

Same problem

File "/Users/Syzygy/Downloads/DCGAN-tensorflow-master/model.py", line 222, in train batch_images = np.array(batch).astype(np.float32) ValueError: could not broadcast input array from shape (128,128,3) into shape (128,128)

Caselles avatar Apr 18 '17 14:04 Caselles

This seems to be a problem only with non-grayscale images (c_dim > 1)

jpchen avatar Apr 18 '17 17:04 jpchen

Up (same problem)

ludns avatar Apr 18 '17 19:04 ludns

I tried to convert all my png's to greyscale and set c_dim=1 and it works. But when c_dim>1, I have the same error

ludns avatar Apr 18 '17 19:04 ludns

@justinglibert . Hi Justin, how did you convert to images to greyscale?

atif93 avatar Apr 26 '17 05:04 atif93

I think the problem that @jpchen was suffered is occurred because he's using png image (which has 4 color channel because of transparency) but he added c_dim=1 which build a model for 1 color channel. I fixed this color channel problem by removing c_dim. Hope this solve the problem.

carpedm20 avatar Apr 26 '17 06:04 carpedm20

Nope same problem facing with JPEG and jpg format

sachin97mishra avatar Apr 26 '17 10:04 sachin97mishra

@carpedm20 . Thanks for the suggestion, but I have the same problem with 3-channel jpg images also.

atif93 avatar Apr 26 '17 16:04 atif93

I was able to fix this issue by:

  1. Ensuring that all images in my dataset were the exact same size.
cd data/mypics
# Create dir to store images that aren't the same size`
mkdir misshaped
# Identify misshaped images using Imagemagick's 'identify' tool 
and move to above dir. (Replace with your desired resolution)
identify * | grep -v "600x450" | awk '{ print $1 }' | xargs -I {} bash -c "mv {} misshaped"
  1. Ensuring that all images had the same number of colors. In my case I was using color JPGs and I found a handful of Grayscale images.
identify -format "%i %[colorspace]\n" IMG_*.jpg | grep -v sRGB
IMG_4959.jpg Gray
IMG_4960.jpg Gray
IMG_4961.jpg Gray
IMG_4962.jpg Gray
IMG_7356.jpg Gray
IMG_7630.jpg Gray

After I took care of these, it worked.

empireshades avatar Apr 26 '17 19:04 empireshades

I have same problem, and I want RGB picture, how to deal with it?

litingsjj avatar Jun 27 '17 15:06 litingsjj

Up. Same problem with jpg format

DNXie avatar Jun 29 '17 07:06 DNXie

I had the same problem ... After some debugging I realized that my dataset had a mix of 64x64x3 and 64x64x1 images... So, I just sorted the files by size and then trimmed the long tail which had a marked difference in size with the rest. And, thats it. The problem was gone. Hope this helps some1.

Sarnath avatar Jul 05 '17 05:07 Sarnath

i battled with this for days, with a 50k png dataset. eventually figured out that a few of my pngs didn't have an alpha channel. i've manually set the c_dim to 3 universally to ignore alpha, which is no longer throwing an error- dunno if it'll mess up results- will report back.

everestpipkin avatar Jul 11 '17 18:07 everestpipkin

Same problem, all of my data is 400x300x3 and JPEG, but some images still occur error, and can't find a solution in addition to convert image to grayscale. 😞

Jasonnor avatar Jul 15 '17 03:07 Jasonnor

I have solved the problem. It is because my images contain both (112,96,3)s and (112,96, )s, which are one-channel images. I fixed it by using cv2 to read the image, it will automatically read all the images in 3 channels without changing the colors. However, cv2 read images in different order of channels with io. So if you want the image to be in the order of (r,g,b), you have to write another two lines of code to switch the channels.

Here is my code:

import cv2
img = cv2.imread(image_path) This is because cv2 read images in (b,g,r) (b, g, r)=cv2.split(img) img=cv2.merge([r,g,b])

Hope this will help you!

DNXie avatar Jul 15 '17 08:07 DNXie

In case it is helpful to anyone-

My problem was because even when images are expressly told to be sRGB, if they have less than 256 colors they default to palette or indexed color. This is why the (x,x,1) array as opposed to (x,x,3)! Its hard to sort them out by hand, because they are often as big, file-size-wise, as the true sRGBs.

I eventually fixed them with PNG:color-type=2 in imagemagick. More info here; http://www.imagemagick.org/discourse-server/viewtopic.php?t=19262

Now they're training without a hitch.

everestpipkin avatar Jul 16 '17 20:07 everestpipkin

@DNXie where did you implement your code:

at model.py or utils.py please can you give more explanation on how you resolved it.

thanks

jerofad avatar Aug 22 '17 09:08 jerofad

@Caselles I have the same issue with yours. How did you finally resolve it?

thanks

jerofad avatar Aug 22 '17 09:08 jerofad

Methods here works for me if you don't have image path but just urls: http://www.pyimagesearch.com/2015/03/02/convert-url-to-image-with-python-and-opencv/

hanhanwu avatar Aug 28 '17 02:08 hanhanwu

I had the same problem before. It is because there are grey scale images in my data. I solved this by:

from PIL import Image
img = Image.open(img_name).convert('RGB')
# your own image operations 

Simply converting grey scale image to RGB image is OK.

Zhangtd avatar Nov 30 '17 14:11 Zhangtd

MARK

ysh329 avatar Jul 28 '18 09:07 ysh329

Selection_066 Selection_068

so I am getting this error im_sz=320 and patch size =160. but code runs well when im_size=160 and patch size= 160.

ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 4, 4, 1024), (None, 5, 5, 1024)]

can anyone help here?

prativadas avatar Jul 18 '19 17:07 prativadas

Same problem

File "/Users/Syzygy/Downloads/DCGAN-tensorflow-master/model.py", line 222, in train batch_images = np.array(batch).astype(np.float32) ValueError: could not broadcast input array from shape (128,128,3) into shape (128,128)

have you found solution ?

GokulAarthishwaran avatar Nov 02 '20 15:11 GokulAarthishwaran

This seems to be a problem only with non-grayscale images (c_dim > 1)

it worked when I convert my image to grayscale. Thank you.

malindux avatar Nov 30 '20 12:11 malindux

You can solve it by converting your images to rgb using PIL

collinsanele avatar Feb 12 '21 12:02 collinsanele

I had the same problem before. It is because there are grey scale images in my data. I solved this by:

from PIL import Image
img = Image.open(img_name).convert('RGB')
# your own image operations 

Simply converting grey scale image to RGB image is OK.

This worked perfectly! Thanks!

kennethZhangML avatar Aug 17 '21 03:08 kennethZhangML

I had the same problem before. It is because there are grey scale images in my data. I solved this by:

from PIL import Image
img = Image.open(img_name).convert('RGB')
# your own image operations 

Simply converting grey scale image to RGB image is OK.

Great It Worked Perfectly! Thanks @Zhangtd

kalilinux-png avatar Sep 11 '22 11:09 kalilinux-png

您好:       来信我已收到,我会尽快回复的。

lihh1023 avatar Sep 11 '22 11:09 lihh1023