1

I am currently working to train a dataset stored as numpy arrays using

train_dataset=tf.data.Dataset.from_tensor_slices(train_data)

Here, train_data is a numpy array of data without the associated labels. The model that I am running was created to work on datasets as DatasetV1Adapters(MNIST and dataset for pix to pix GANs). I have been looking for documentation for making the required correction for quite a while now(around 4 weeks). and this method hasn't solved my problem.

For the training process, I was running:

for images in train_dataset:
            #images=np.expand_dims(images, axis=0)
            disc_loss += train_discriminator(images)

Which would give me an error of

ValueError: Input 0 of layer conv2d_2 is incompatible with the layer: expected ndim=4, found ndim=3.

The array shape was [32,32,3] so the 100 from number of images was lost. I tried to run the commented out line images=np.expand_dims(images, axis=0). Thus I got [1,32,32,3] which matched my required dimensionality. I thought my problem would be solved, but instead I now have the following error:

ValueError: Input 0 of layer conv2d_4 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [1, 32, 32, 3]

Which I don't fully understand. It seems like the error is definitely related to the datasetV1Adapter as I get the same type of error was various codes. I have tried uploading my dataset to github, but as a 10GB folder, I am unable to actually upload it. Any help will be appreciated

EDIT: Followed @Sebastian-Sz's advice(Kind off). I set my channels in the model to three to accommodate RGB instead of grayscale. Running this code gave me

TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64

So I added

train_data = np.asarray(train_data, dtype=np.float)

Now I get an error saying:

Input 0 of layer dense_5 is incompatible with the layer: expected axis -1 of input shape to have value 6272 but received input with shape [1, 8192]

Which makes no sense to me

  • Are you sure the model is not expecting grayscale images (so in your case `1, 32, 32, 1`) ? – sebastian-sz Mar 01 '20 at 18:21
  • Now that I think of it. It is very likely. Altough I dont entirely know how to check. Or even how to solve it – Mr. Johnny Doe Mar 01 '20 at 18:26
  • Having `train_dataset` of images you can use [tf.image.rgb_to_grayscale](https://www.tensorflow.org/api_docs/python/tf/image/rgb_to_grayscale). Hence you should be able to run: `train_dataset = train_dataset.map(tf.image.rgb_to_grayscale)` or similar, to obtain grayscale images. If these, passed to the network don't cause an error it could be that your model was designed to handle only 1 channel input. – sebastian-sz Mar 01 '20 at 18:32
  • 1
    Thank you. However I just made changes to the model. I saw that channels was set to 1. I set it 3, and got a new error that I am going to put into an edit. Thank you – Mr. Johnny Doe Mar 01 '20 at 18:33
  • I just made the edits @sebastian-sz – Mr. Johnny Doe Mar 01 '20 at 18:51
  • Are you passing normalised images to the model? So, for example in range `[0, 1]` ? Most models expect images in this range and dtype of `float32` or `float64`. It could be that your data (images) are integer numbers in range 0-255, which could throw such error. – sebastian-sz Mar 01 '20 at 18:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208805/discussion-between-sebastian-sz-and-mr-johnny-doe). – sebastian-sz Mar 01 '20 at 19:04
  • No, I have not normalised it anywhere. As far as I can tell, my values range from 0-255. The conversion to float I followed based off of this answer https://stackoverflow.com/questions/50324495/how-to-convert-float16-to-uint8-in-python-for-exr-files?answertab=active#tab-top Are you saying I should infact normalise it? – Mr. Johnny Doe Mar 01 '20 at 19:05
  • We can continue this discussion in the link above :) As for normalising the images: yes this is a common practice. The easiest way to do this is to simply divide your data by maximum value (in your case 255): `image = image / 255. ` – sebastian-sz Mar 01 '20 at 19:12
  • Will do. Although, I just normalised as you said I should and the exact same error persists – Mr. Johnny Doe Mar 01 '20 at 19:13

0 Answers0