欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

plotly分割顯示mnist的方法詳解

 更新時(shí)間:2022年03月17日 17:18:01   作者:Leibniz infinity sml  
這篇文章主要為大家詳細(xì)介紹了plotly分割顯示mnist的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

加載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 #修改點(diǎn): 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 顯示多個(gè)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()
#參數(shù)row、col從1開始,  不是從0開始的

plotly 顯示單個(gè)圖片

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]顯示如下:

請(qǐng)?zhí)砑訄D片描述

mnist單樣本分拆顯示

#mnist單樣本分割 分割成4*4小格子顯示出來, 以確認(rèn)分割的對(duì)不對(duì)。 以下代碼是正確的分割。 主要邏輯是:   (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單樣本分拆顯示結(jié)果: 由此圖可知 (7,4,7,4) [h, :, w, :] 是正常的取相鄰的像素點(diǎn)出而形成的4*4的小方格 ,這正是所需要的

請(qǐng)?zhí)砑訄D片描述

上圖顯示 的 橫坐標(biāo)拉伸比例大于縱坐標(biāo) 所以看起來像一個(gè)被拉橫了的手寫數(shù)字5 ,如果能讓plotly把橫縱拉伸比例設(shè)為相等 上圖會(huì)更像手寫數(shù)字5

可以用torch.swapdim進(jìn)一步改成以下代碼

    """
    mnist單樣本分割 分割成4*4小格子顯示出來, 重點(diǎn)邏輯是: (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單樣本錯(cuò)誤的分拆顯示

以下 mnist單樣本錯(cuò)誤的分拆顯示:

# mnist單樣本錯(cuò)誤的分拆顯示:
    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()

其結(jié)果為: 由此圖可知 (4,7, 4, 7) [:, h, :, w] 是間隔的取出而形成的4*4的小方格 

請(qǐng)?zhí)砑訄D片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!    

相關(guān)文章

  • python requests使用socks5的例子

    python requests使用socks5的例子

    今天小編就為大家分享一篇python requests使用socks5的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 淺談Python Pygame圖像的基本使用

    淺談Python Pygame圖像的基本使用

    今天給大家?guī)淼氖顷P(guān)于Python Pygame的相關(guān)知識(shí),文章圍繞著Pygame圖像的基本使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Python?設(shè)計(jì)模式中的創(chuàng)建型建造者模式

    Python?設(shè)計(jì)模式中的創(chuàng)建型建造者模式

    本文介紹Python設(shè)計(jì)模式中的創(chuàng)建型建造者模式,其表現(xiàn)為復(fù)雜對(duì)象的創(chuàng)建與表現(xiàn)相分離,這樣,同一個(gè)過程就有不同的表現(xiàn),想要?jiǎng)?chuàng)建一個(gè)由多個(gè)部分組成的對(duì)象,而且它的構(gòu)成需要一步接一步的完成。只有當(dāng)各個(gè)部分都完成了,這個(gè)對(duì)象才完整,下文相關(guān)自來哦,需要的朋友可以參考下
    2022-02-02
  • 解決matplotlib.pyplot在Jupyter notebook中不顯示圖像問題

    解決matplotlib.pyplot在Jupyter notebook中不顯示圖像問題

    這篇文章主要介紹了解決matplotlib.pyplot在Jupyter notebook中不顯示圖像問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python檢測(cè)是文件還是目錄的方法

    python檢測(cè)是文件還是目錄的方法

    這篇文章主要介紹了python檢測(cè)是文件還是目錄的方法,涉及Python針對(duì)文件及目錄的檢測(cè)技巧,需要的朋友可以參考下
    2015-07-07
  • python查找目錄下指定擴(kuò)展名的文件實(shí)例

    python查找目錄下指定擴(kuò)展名的文件實(shí)例

    這篇文章主要介紹了python查找目錄下指定擴(kuò)展名的文件,實(shí)例分析了Python文件查詢的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 使用卷積神經(jīng)網(wǎng)絡(luò)(CNN)做人臉識(shí)別的示例代碼

    使用卷積神經(jīng)網(wǎng)絡(luò)(CNN)做人臉識(shí)別的示例代碼

    這篇文章主要介紹了使用卷積神經(jīng)網(wǎng)絡(luò)(CNN)做人臉識(shí)別的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python裝飾器實(shí)現(xiàn)對(duì)異常代碼出現(xiàn)進(jìn)行自動(dòng)監(jiān)控的實(shí)現(xiàn)方法

    python裝飾器實(shí)現(xiàn)對(duì)異常代碼出現(xiàn)進(jìn)行自動(dòng)監(jiān)控的實(shí)現(xiàn)方法

    這篇文章主要介紹了python裝飾器實(shí)現(xiàn)對(duì)異常代碼出現(xiàn)進(jìn)行自動(dòng)監(jiān)控的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Python的SQLalchemy模塊連接與操作MySQL的基礎(chǔ)示例

    Python的SQLalchemy模塊連接與操作MySQL的基礎(chǔ)示例

    SQLalchemy是Python世界中驅(qū)動(dòng)MySQL的一款高人氣模塊,這里我們從入門開始來看一下Python的SQLalchemy模塊連接與操作MySQL的基礎(chǔ)示例:
    2016-07-07
  • python ---lambda匿名函數(shù)介紹

    python ---lambda匿名函數(shù)介紹

    這篇文章主要介紹了python lambda匿名函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評(píng)論