keras 分割Tensorflow数据集

5vf7fwbs  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(176)

我从硬盘上传了一个数据集:

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

我意识到这是一个非常小的数据集,只有12张图像,但我只是想习惯自动编码器的布局,并不担心结果。因此,我将其分为train_size和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)

自动编码器符合要求,并给我一个结果摘要,但是,我想在剩下的一个图像上测试结果。我想看看我是否可以查看剩下的图像,我尝试了:

cv2_imshow(test)

失败并显示以下消息:

---------------------------------------------------------------------------
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'

我想把测试图像转换成:

passed_image=autoencoder.predict(test)

它实际上完成了,但是,我也无法查看passed_image,它给了我另一个错误:

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

很明显,我不明白这些数据集是如何设置的,有人能帮我吗?我想确保我的测试图像实际上进入了预测,然后我想看到预测。

w8f9ii69

w8f9ii691#

在Web上搜索了很多次之后,我发现即使只有一个图像,我仍然必须使用test.as_numpy_iterator()

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

然后图像会显示

相关问题