網站首頁 編程語言 正文
加載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的小方格?
總結
原文鏈接:https://blog.csdn.net/hfcaoguilin/article/details/123519043
相關推薦
- 2023-05-31 Pandas使用分隔符或正則表達式將字符串拆分為多列_python
- 2024-04-05 springboot注冊攔截器與返回統一標準響應格式
- 2023-01-01 詳解Python如何實現輸出顏色字體到終端界面_python
- 2022-04-04 git push報錯Proxy CONNECT aborted
- 2022-12-10 C++?Boost?Spirit精通教程_C 語言
- 2022-04-25 ASP.NET?Core?MVC中Tag?Helpers用法介紹_實用技巧
- 2022-02-24 C語言數據結構與算法之排序總結(一)_C 語言
- 2022-07-09 C++超詳細講解auto與nullptr的使用_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支