DCGAN-tensorflow: ValueError: could not broadcast input array from shape (96,96,4) into shape (96,96)
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)
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 28 (1 by maintainers)
I had the same problem before. It is because there are grey scale images in my data. I solved this by:
Simply converting grey scale image to RGB image is OK.
I was able to fix this issue by:
After I took care of these, it worked.
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!
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
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.
I have same problem, and I want RGB picture, how to deal with it?
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.
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 removingc_dim
. Hope this solve the problem.This seems to be a problem only with non-grayscale images (c_dim > 1)