Dividing up a Tensorflow Dataset

Viewed 35

I have a dataset I've uploaded from my drive:

data = tf.keras.utils.image_dataset_from_directory('/content/gdrive/MyDrive/Skyrmion Vision/testFiles/train/',batch_size=1,image_size=(171,256))

and I realize this is a super small dataset, there are only 12 images but, I'm just trying to get use to the layout of the autoencoder, not really worried about the results. So, I divided this up into a train_size and a test_size:

train_size = 11
val_size = 0
test_size = 1
test_size + val_size + train_size

train = data.take(train_size)
val = data.skip(train_size).take(val_size)
test = data.skip(train_size+val_size).take(test_size)
len(test)

The autoencoder complies and gives me a resulting summary, however, I want to test the result on the one remaining image. I thought I would see if I could view the image thats remaining and I tried:

cv2_imshow(test)

Which fails with this message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-32-7a07ce04e52f> in <module>
----> 1 cv2_imshow(test)

/usr/local/lib/python3.7/dist-packages/google/colab/patches/__init__.py in cv2_imshow(a)
     16       image.
     17   """
---> 18   a = a.clip(0, 255).astype('uint8')
     19   # cv2 stores colors as BGR; convert to RGB
     20   if a.ndim == 3:

AttributeError: 'TakeDataset' object has no attribute 'clip'

I want to put the test image into:

passed_image=autoencoder.predict(test)

which actually completes, but, I can't view the passed_image either, it gives me another error:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in fromarray(obj, mode)
   2713         try:
-> 2714             mode, rawmode = _fromarray_typemap[typekey]
   2715         except KeyError:

KeyError: ((1, 1, 256, 3), '|u1')

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
2 frames
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in fromarray(obj, mode)
   2714             mode, rawmode = _fromarray_typemap[typekey]
   2715         except KeyError:
-> 2716             raise TypeError("Cannot handle this data type: %s, %s" % typekey)
   2717     else:
   2718         rawmode = mode

TypeError: Cannot handle this data type: (1, 1, 256, 3), |u1

I'm clearly not understanding how these datasets are setup, can someone help me here? I want to make sure my test image is actually going into the prediction and then I want to see that prediction.

1 Answers

After much scouring on the web, I found that even though there is only one image, I still have to use test.as_numpy_iterator()

for batch in test.as_numpy_iterator(): 
    X, y = batch
    cv2_imshow(X[0])

and then the image will show

Related