DCGAN-tensorflow
DCGAN-tensorflow copied to clipboard
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)
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)
This seems to be a problem only with non-grayscale images (c_dim > 1)
Up (same problem)
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
@justinglibert . Hi Justin, how did you convert to images to greyscale?
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.
Nope same problem facing with JPEG and jpg format
@carpedm20 . Thanks for the suggestion, but I have the same problem with 3-channel jpg images also.
I was able to fix this issue by:
- 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"
- 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.
I have same problem, and I want RGB picture, how to deal with it?
Up. Same problem with jpg format
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 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.
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. 😞
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!
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.
@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
@Caselles I have the same issue with yours. How did you finally resolve it?
thanks
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/
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.
MARK
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?
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 ?
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.
You can solve it by converting your images to rgb using PIL
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!
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
您好: 来信我已收到,我会尽快回复的。