TensorFlow——[基本图像分类]fashion-mnist及mnist_reader.py运行错误[TypeError: Invalid dimensions for image data]
生活随笔
收集整理的這篇文章主要介紹了
TensorFlow——[基本图像分类]fashion-mnist及mnist_reader.py运行错误[TypeError: Invalid dimensions for image data]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述
無。
問題分析
問題關鍵理解imshow函數的參數。
matplotlib.pyplot.imshow()需要數據是二維的數組或者第三維深度是3或4的三維數組,當第三維深度為1時,使用np.squeeze()壓縮數據成為二維數組。
解決方案
改寫?mnist_reader.py
def load_mnist(path, kind='train'):import osimport gzipimport numpy as np"""Load MNIST data from `path`"""labels_path = os.path.join(path,'%s-labels-idx1-ubyte.gz'% kind)images_path = os.path.join(path,'%s-images-idx3-ubyte.gz'% kind)with gzip.open(labels_path, 'rb') as lbpath:labels = np.frombuffer(lbpath.read(), dtype=np.uint8,offset=8)with gzip.open(images_path, 'rb') as imgpath:images = np.frombuffer(imgpath.read(), dtype=np.uint8,offset=16).reshape(len(labels), 28, 28) # 關鍵點return images, labelsdef load_data(path):train_images, train_labels = load_mnist(path, kind='train')test_images, test_labels = load_mnist(path, kind='t10k')return (train_images, train_labels), (test_images, test_labels)def load_data():return load_data('data/fashion')代碼測試
import mnist_reader import matplotlib.pyplot as plt train_images, train_labels = mnist_reader.load_mnist('../data/fashion', kind='train') test_images, test_labels = mnist_reader.load_mnist('../data/fashion', kind='t10k') print(train_images.shape) plt.figure() plt.imshow(train_images[0]) plt.colorbar() plt.grid(False) plt.show()運行結果?
?
參考文章
matplotlib繪圖imshow()函數報錯“TypeError: Invalid dimensions for image data”
Tensorflow學習第1課——從本地加載MNIST以及FashionMNIST數據
使用matplotlib.pyplot.imshow() 顯示圖像時出現“TypeError: Invalid dimensions for image data”的問題
總結
以上是生活随笔為你收集整理的TensorFlow——[基本图像分类]fashion-mnist及mnist_reader.py运行错误[TypeError: Invalid dimensions for image data]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow——本地加载fash
- 下一篇: TensorFlow 教程——基本分类: