【发布时间】:2020-01-28 15:40:47
【问题描述】:
我正在尝试从 U-net 绘制一组输出数据。该数组包含已为图像分割进行单热编码的 mnist 数据。
它的形状是: (28,28,11)
因此,对于原始图像中像素值为 0 的每个位置,one-hot 编码将放置一个数组 [0 0 0 0 0 0 0 0 0 0 1],表示该像素为空白。
另一方面,如果像素值 > 0,则 one-hot 数组将显示整个图像的分类。
EX:如果 mnist 图像为 2,则值 > 0 的每个像素将转换为数组 [0 0 1 0 0 0 0 0 0 0 0]。
我想知道是否有办法显示这样一个数组,数组的每个元素都由一个单热数组组成。
我尝试只在数据上使用 plt.imshow,但是,它会抛出一个错误,提示“TypeError:图像数据的尺寸无效”
这是我正在使用的代码
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
import skimage.transform
import cv2
import sys
from keras import Input
from keras import backend as K
from keras.utils import np_utils
from keras.models import Sequential, Model
from keras.utils import to_categorical
from keras.losses import categorical_crossentropy
from keras.optimizers import adam
from keras.layers import Conv2D, Dense, MaxPooling2D, Flatten, Dropout, GlobalAveragePooling2D
from keras.datasets import cifar10
from keras.datasets import mnist
from keras.utils import np_utils
import matplotlib.pyplot as plt
import numpy as np
np.set_printoptions(threshold=sys.maxsize)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
y_train = y_train[:10]
data = np.random.choice(255, (10,128,128))
## do you calculation of brightness here
## and expand it to one row per pixel
arr = data.reshape(-1,1)/255
## repeat labels to match the expanded pixel
labels = y_train.repeat(128*128).reshape(-1,1)
ind_row = np.arange(len(arr))
ind_col = np.where(arr>0, labels, 10).ravel()
one_hot_coded_arr = np.zeros((len(arr), 11))
one_hot_coded_arr[ind_row,ind_col]=1
## convert back to desired shape
one_hot_coded_arr = one_hot_coded_arr.reshape(-1, 128,128,11)
#print(one_hot_coded_arr[:28,:])
print(one_hot_coded_arr.shape)
plt.imshow(one_hot_coded_arr, interpolation='nearest')
plt.axis("off")
plt.show()
我想显示这样的图像: https://documentation.sas.com/api/docsets/casdlpg/8.4/content/images/mnistout2.png
但我一直遇到错误 “TypeError:图像数据的尺寸无效”
任何帮助都会很棒,谢谢!
【问题讨论】: