plotly分割显示mnist的方法详解
作者:Leibniz 发布时间:2022-02-11 04:58:05
标签: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的小方格
来源:https://blog.csdn.net/hfcaoguilin/article/details/123519043


猜你喜欢
- 一般而言,有两种连接sql server 的方式,一是利用 sql server 自带的客户端工具,如企业管理器、查询分析器、事务探查器等;
- JavaScript onkeypress 事件用户按下或按住一个键盘按键时会触发 onkeypress 事件。注意:onkeypress
- 列表生成式基础语法[exp for iter_var in iterable (if conditional)]原理:首先迭代 iterab
- 如下所示:Description:将一个矩阵(二维数组)按对角线向右进行打印。(搜了一下发现好像是美团某次面试要求半小时手撕的题)Examp
- 前言:我们在打印一些 PDF 文件的时候可能会遇见加密不能打印的情况,需要提供密码才能打印。如果直接在浏览器中浏览 PDF 文件,它不能调取
- 引言现在本地创建一个excel表,以及两个sheet,具体数据如下:sheet1: sheet2:读取excel文件pandas.
- 1 、创建一个django项目使用django-admin.py startproject MyDjangoSite 参考这里2、建立视图f
- 由于requests是http类接口的核心,因此封装 * 虑问题比较多:1. 对多种接口类型的支持;2. 连接异常时能够重连;3. 并发处理的
- 平行坐标图,一种数据可视化的方式。以多个垂直平行的坐标轴表示多个维度,以维度上的刻度表示在该属性上对应值,相连而得的一个折线表示一个样本,以
- 字符串查找基本操作主要分为三个关键词:find()、index()、count()。这三个用法相同,格式都是为:自定义字符串名.关键词(‘子
- 目录1.准备工作2. 开始2.1 生成控件2.2 定义输入和计算函数2.3 绑定键盘事件2.4 循环3.全部代码4. 结束语做一个计算器,这
- 我们平日办公时用得最多的软件是Execl、Word或WPS Office等,你的计算机中一定储存着大量的XLS、DOC、WPS文件吧!网页制
- 本文实例讲述了python中MySQLdb模块用法。分享给大家供大家参考。具体用法分析如下:MySQLdb其实有点像php或asp中连接数据
- Application Name(应用程序名称):应用程序的名称。如果没有被指定的话,它的值为.NET SqlClient Data Pro
- function commafy() { var num = document.getElementById("NumA"
- 1.什么是事务:事务是一个不可分割的工作逻辑单元,在数据库系统上执行并发操作时事务是做为最小的控制单元来使用的。 他
- 栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。
- 本文实例为大家分享了tkinter+pygame+spider实现音乐播放器,供大家参考,具体内容如下1.确定页面SongSheet&nbs
- 本文实例讲述了PHP模板引擎Smarty中变量的使用方法。分享给大家供大家参考,具体如下:一、概述:Smarty 是 PHP 众多模板引擎中
- 很久之前就对jQuery.animate的实现非常感兴趣,不过前段时间很忙,直到前几天端午假期才有时间去研究。jQuery.animate的