plotly分割顯示mnist的方法詳解
加載mnist
import numpy def loadMnist() -> (numpy.ndarray,numpy.ndarray,numpy.ndarray,numpy.ndarray): """ :return: (xTrain,yTrain,xTest,yTest) """ global _TRAIN_SAMPLE_CNT global PIC_H global PIC_W global _TEST_SAMPLE_CNT global PIC_HW from tensorflow import keras #修改點: tensorflow:2.6.2,keras:2.6.0 此版本下, import keras 換成 from tensorflow import keras import tensorflow print(f"keras.__version__:{keras.__version__}")#2.6.0 print(f"tensorflow.__version__:{tensorflow.__version__}")#2.6.2 # avatar_img_path = "/kaggle/working/data" import os import cv2 xTrain:numpy.ndarray; label_train:numpy.ndarray; xTest:numpy.ndarray; label_test:numpy.ndarray yTrain:numpy.ndarray; yTest:numpy.ndarray #%userprofile%\.keras\datasets\mnist.npz (xTrain, label_train), (xTest, label_test) = keras.datasets.mnist.load_data() # x_train.shape,y_train.shape, x_test.shape, label_test.shape # (60000, 28, 28), (60000,), (10000, 28, 28), (10000,) _TRAIN_SAMPLE_CNT,PIC_H,PIC_W=xTrain.shape PIC_HW=PIC_H*PIC_W xTrain=xTrain.reshape((-1, PIC_H * PIC_W)) xTest=xTest.reshape((-1, PIC_H * PIC_W)) _TEST_SAMPLE_CNT=label_test.shape[0] from sklearn import preprocessing #pytorch 的 y 不需要 oneHot #_label_train是1列多行的樣子. _label_train.shape : (60000, 1) yTrain=label_train # y_train.shape:(60000) ; y_train.dtype: dtype('int') CLASS_CNT=yTrain.shape[0] yTest=label_test # y_test.shape:(10000) ; y_test.dtype: dtype('int') xTrainMinMaxScaler:preprocessing.MinMaxScaler; xTestMinMaxScaler:preprocessing.MinMaxScaler xTrainMinMaxScaler=preprocessing.MinMaxScaler() xTestMinMaxScaler=preprocessing.MinMaxScaler() # x_train.dtype: dtype('uint8') -> dtype('float64') xTrain=xTrainMinMaxScaler.fit_transform(xTrain) # x_test.dtype: dtype('uint8') -> dtype('float64') xTest = xTestMinMaxScaler.fit_transform(xTest) return (xTrain,yTrain,xTest,yTest)
xTrain:torch.Tensor;yTrain:torch.Tensor; xTest:torch.Tensor; yTest:torch.Tensor(xTrain,yTrain,xTest,yTest)=loadMnist()
plotly 顯示多個mnist樣本
import plotly.express import plotly.graph_objects import plotly.subplots import numpy xTrain:numpy.ndarray=numpy.random.random((2,28,28)) #xTrain[0].shape:(28,28) #fig:plotly.graph_objects.Figure=None fig=plotly.subplots.make_subplots(rows=1,cols=2,shared_xaxes=True,shared_yaxes=True) #共1行2列 fig.add_trace(trace=plotly.express.imshow(img=xTrain[0]).data[0],row=1,col=1) #第1行第1列 fig.add_trace(trace=plotly.express.imshow(img=xTrain[1]).data[0],row=1,col=2) #第1行第2列 fig.show() #參數row、col從1開始, 不是從0開始的
plotly 顯示單個圖片
import numpy xTrain:numpy.ndarray=numpy.random.random((2,28,28)) #xTrain[0].shape:(28,28) import plotly.express import plotly.graph_objects plotly.express.imshow(img=xTrain[0]).show() #其中plotly.express.imshow(img=xTrain[0]) 的類型是 plotly.graph_objects.Figure
xTrain[0]顯示如下:
mnist單樣本分拆顯示
#mnist單樣本分割 分割成4*4小格子顯示出來, 以確認分割的對不對。 以下代碼是正確的分割。 主要邏輯是: (7,4,7,4) [h, :, w, :] fig:plotly.graph_objects.Figure=plotly.subplots.make_subplots(rows=7,cols=7,shared_xaxes=True,shared_yaxes=True,vertical_spacing=0,horizontal_spacing=0) xTrain0Img:torch.Tensor=xTrain[0].reshape((PIC_H,PIC_W)) plotly.express.imshow(img=xTrain0Img).show() xTrain0ImgCells:torch.Tensor=xTrain0Img.reshape((7,4,7,4)) for h in range(7): for w in range(7): print(f"h,w:{h},{w}") fig.add_trace(trace=plotly.express.imshow(xTrain0ImgCells[h,:,w,:]).data[0],col=h+1,row=w+1) fig.show()
mnist單樣本分拆顯示結果: 由此圖可知 (7,4,7,4) [h, :, w, :] 是正常的取相鄰的像素點出而形成的4*4的小方格 ,這正是所需要的
上圖顯示 的 橫坐標拉伸比例大于縱坐標 所以看起來像一個被拉橫了的手寫數字5 ,如果能讓plotly把橫縱拉伸比例設為相等 上圖會更像手寫數字5
可以用torch.swapdim進一步改成以下代碼
""" mnist單樣本分割 分割成4*4小格子顯示出來, 重點邏輯是: (7, 4, 7, 4) [h, :, w, :] :param xTrain: :return: """ fig: plotly.graph_objects.Figure = plotly.subplots.make_subplots(rows=7, cols=7, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0, horizontal_spacing=0) xTrain0Img: torch.Tensor = xTrain[0].reshape((PIC_H, PIC_W)) plotly.express.imshow(img=xTrain0Img).show() xTrain0ImgCells: torch.Tensor = xTrain0Img.reshape((7, 4, 7, 4)) xTrain0ImgCells=torch.swapdims(input=xTrain0ImgCells,dim0=1,dim1=2)#交換 (7, 4, 7, 4) 維度1、維度2 即 (0:7, 1:4, 2:7, 3:4) for h in range(7): for w in range(7): print(f"h,w:{h},{w}") fig.add_trace(trace=plotly.express.imshow(xTrain0ImgCells[h, w]).data[0], col=h + 1, row=w + 1) # [h, w, :, :] 或 [h, w] fig.show()
mnist單樣本錯誤的分拆顯示
以下 mnist單樣本錯誤的分拆顯示:
# mnist單樣本錯誤的分拆顯示: fig: plotly.graph_objects.Figure = plotly.subplots.make_subplots(rows=7, cols=7, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0, horizontal_spacing=0) xTrain0Img: torch.Tensor = xTrain[0].reshape((PIC_H, PIC_W)) plotly.express.imshow(img=xTrain0Img).show() xTrain0ImgCells: torch.Tensor = xTrain0Img.reshape((4,7, 4, 7)) #原本是: (7,4,7,4) for h in range(7): for w in range(7): print(f"h,w:{h},{w}") fig.add_trace(trace=plotly.express.imshow(xTrain0ImgCells[:, h, :, w]).data[0], col=h + 1, row=w + 1) #原本是: [h,:,w,:] fig.show()
其結果為: 由此圖可知 (4,7, 4, 7) [:, h, :, w] 是間隔的取出而形成的4*4的小方格
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
解決matplotlib.pyplot在Jupyter notebook中不顯示圖像問題
這篇文章主要介紹了解決matplotlib.pyplot在Jupyter notebook中不顯示圖像問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04python裝飾器實現對異常代碼出現進行自動監(jiān)控的實現方法
這篇文章主要介紹了python裝飾器實現對異常代碼出現進行自動監(jiān)控的實現方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Python的SQLalchemy模塊連接與操作MySQL的基礎示例
SQLalchemy是Python世界中驅動MySQL的一款高人氣模塊,這里我們從入門開始來看一下Python的SQLalchemy模塊連接與操作MySQL的基礎示例:2016-07-07