如何修复:ValueError:检查输入时出错:预期conv2d_130_输入具有形状(1,512,512),但获得具有形状(79,512,512)的数组

2024-04-25 22:28:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我是CNN的初学者

所以,我正在建立一个2D卷积神经网络,它可以预测脑肿瘤的类型,并且对NumPy阵列有疑问。我的模型的输入形状是(1512512)as(通道、img_高度、img_宽度)。第四维是num_图像,它似乎是由TensorFlow自动定义的。这只是一个简单的背景。我有3064“.mat”扩展文件和脑部肿瘤的MRI扫描。一切都准备好了。我将“.mat”文件转换为numpy矩阵,并将整个矩阵列表附加到单个numpy数组中,作为CNN的输入传递。我还将相应的标签(将输入传递到模型时链接到图像的索引)作为numpy数组。所有数字在图像和标签中均为浮点型

同样,我的输入形状是(1512512)。但是,在拟合模型时,我得到以下错误:

ValueError:检查输入时出错:预期conv2d\u 130\u输入具有形状(1,512,512),但获得具有形状(79,512,512)的数组。

所以,我正在切片我的NumPy数组来创建train_图像、train_标签、test_图像、test_标签。我已经验证了每个序列和测试集的长度是否与标签匹配。它们也是数组,我检查了多次。这是一个值错误。那么,我该如何解决这个问题呢

我甚至不知道输入形状是从哪里来的(79512512)。我有一个循环将f{n}.mat”图像转换为矩阵。我使用100张图片进行测试,80张图片进行训练,20张图片进行测试。我认为错误就在这里,输入形状是(通道,img hght,img wdth),但是要训练的图像数量被放在通道的值中。因此,输入被放置为(num_images、img hght、img wdth)。这是错误的,应该改变,但我不知道怎么做。或者,我可能错了,我所说的可能没有意义。我提供了所有的代码,在Colab上运行。如果您下载代码并希望运行它以帮助我,请确保更改图像路径。非常感谢

数据集:https://figshare.com/articles/brain_tumor_dataset/1512427/5

#Importing the necessary libraries through PIP to the Virtual Environment
try:
  !python -m pip install --upgrade pip #Quickly update PIP to latest version
  !python -m pip install pymatreader
  !python -m pip install pyswarm #An interesting library for testing purposes
  print("""
The following libraries are available and have been successfully fetched:
  >>> PyMatReader
  >>> Particle Swarm""")
except Exception:
  print("""
The following libraries have unavailable and have not been fetched:
  >>> PyMatReader
  >>> Particle Swarm""")
  pass
#Importing the necessary libraries to the Virtual Environment
from __future__ import absolute_import, division, print_function, unicode_literals
import random as rnd
from random import shuffle
import numpy as np
import sys
import scipy as sp
from scipy.ndimage import gaussian_filter
import pymatreader as pym
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.image as mplimg
import matplotlib.pyplot as plt
import PIL
from PIL import Image
import imageio
import sklearn as sk
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import image
import sklearn.metrics as skm

print("""
The following libraries have been successfully imported:
  >>> Future
  >>> Random (with shuffle)
  >>> NumPy
  >>> System
  >>> SciPy (with gaussian filter)
  >>> PyMatReader
  >>> Pandas
  >>> Seaborn
  >>> Matplotlib (with PyPlot & Image)
  >>> PIL (with Image)
  >>> Imageio
  >>> Sci-Kit Learn (with metrics & train_test_split)
  >>> Sci-kit Learn Feature Extraction (with Image)
""")

try:
  %tensorflow_version 2.x
  import keras
  import tensorflow as tf
  print("TensorFlow version 2.x is available and has been successfully imported.")
except Exception:
  %tensorflow_version 1.x
  import keras
  import tensorflow as tf
  print("TensorFlow version 2.x is unavailable. TensorFlow version 1.x has been imported instead.")
  pass

from tensorflow.keras import datasets, layers, models
import keras.preprocessing
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from keras.optimizers import Adam
import pyswarm
from pyswarm import pso

autoTune = tf.data.experimental.AUTOTUNE

print("""
The following deep learning optimizers have been successfully imported:
  >>> Adam
  >>> Particle Swarm (with pso)
""")

print("All libraries have been successfully imported.")
#Understanding the Image Data using Seaborn and Matplotlib
classNames = {1 : "Meningioma", 2 : "Glioma", 3 : "Pituitary Tumor", 4 : "Unkown", 5 : "Unkown"}
outputSize = len(classNames)

chooseImgNum = 2978
example = pym.read_mat(f'/content/gdrive/My Drive/My Files/Neuroimaging/Neuroimaging Datasets/MATLAB Files/{chooseImgNum}.mat')
cjdata = example['cjdata']
pid = cjdata['PID']
img = cjdata['image']
label = cjdata['label']

tumorBorder = cjdata['tumorBorder']
tumorMask = cjdata['tumorMask']
print("Tumor Border is: \n", tumorBorder, "\n")
print("Tumor Mask is: \n", tumorMask, "\n")

def printImage():
  plt.figure(figsize=(5, 5))
  plt.imshow(img, cmap=None)

def matrixConv(): #Data Visualization only
  matrix = np.asmatrix(tumorBorder)
  plt.figure(figsize=(5, 5))
  return matrix

def applyGrayscale():
  plt.figure(figsize=(5, 5))
  plt.imshow(img, cmap='gray')

print("""
      Below is the original image followed by a grayscale application:
____________________________________________________________________________
""")

printImage()
applyGrayscale()
#Preprocessing Brain Images from Dataset
range1 = np.arange(0, 100)
imgMatrices = []
imgNum = 1
i = 1

while imgNum in range1:
  imgNum = pym.read_mat(f'/content/gdrive/My Drive/My Files/Neuroimaging/Neuroimaging Datasets/MATLAB Files/{imgNum}.mat')
  cjdata = imgNum['cjdata']
  imgMatrix = cjdata['image']
  # plt.figure(figsize=(5, 5))
  # plt.imshow(image_matrix, cmap='gray')
  imgMatrixNP = np.asmatrix(imgMatrix)
  imgArrayNP = np.asarray(imgMatrixNP)
  imgMatrices.append(imgArrayNP)
  imgNum = i
  i = i + 1

print("The length of the image input list is:", len(imgMatrices))

imgMatricesNP = np.asarray(imgMatrices)
print("The length of the converted image input array is:", len(imgMatricesNP), "\n")

print("The image input array:")
imgMatricesNP #Prints the raw array
#Supervised Learning: Understanding Cancer Type labels
np.set_printoptions(threshold=3)
#np.set_printoptions(threshold=sys.maxsize) #To check the content of the entire array

rawMatData = pym.read_mat('/content/gdrive/My Drive/My Files/Neuroimaging/Neuroimaging Datasets/cvind.mat')
print("Labels file in \".mat\" format converted to dictionary format:", rawMatData)

matDataList = list(rawMatData.values())
print("Labels converted to list format:", matDataList)

matDataArray = np.asarray(matDataList)
print("Labels converted to array format:", matDataArray, "\n")
shapedMatDataArray = matDataArray.reshape(-1, 3064, 1)
print("Reshaped labels in array format:\n", shapedMatDataArray, "\n")

matData = pd.DataFrame(matDataArray)
print("Labels converted to a Pandas DataFrame:")
matData #Prints out the DataFrame
#Viewing labels based on image number
def imgLabelCheck(n):
  callback = matData.at[0, n-1]
  print(f"Image Number {n} has the following Cancer Type: {classNames[callback]}.")
  return

pickImg = 1 #Choose an image number to look for its Cancer Type
imgLabelCheck(pickImg)
#Preparing the Datasets: Looping Train Set & Test Set
print("___________________________________________________________________________________\n")

train_images = np.array([imgMatricesNP[0:79]])
print("Training images range is:\n", train_images, "\n")

uppTrBn = len(train_images)
loqTrRng = 0
uppTrRng = 79
train_labels = np.asarray(matData.loc[:, loqTrRng:uppTrRng], dtype=float, order='A')
print("Training labels range is:", train_labels)

print("___________________________________________________________________________________\n")

test_images = np.array([imgMatricesNP[80:100]])
print("Testing images range is: \n", test_images, "\n")

uppTsBn = len(test_images)
loqTsRng = 80
uppTsRng = 100
test_labels = np.asarray(matData.loc[:, loqTsRng:uppTsRng], dtype=float, order='A')
print("Testing labels range is:", test_labels)

print("___________________________________________________________________________________")
#train_labels #Verify if the ranges are in fact NumPy arrays
#test_labels
#Defining the Convolutional Neural Network
model = models.Sequential()

model.add(layers.Conv2D(512, (3, 3), activation='relu', data_format="channels_first", input_shape=(1, 512, 512))) #The Input Layer
#model.add(layers.Dropout(0.5, noise_shape=None, seed=None)) #Optional Dropout Layer

model.add(layers.MaxPooling2D((2, 2), padding='same')) #MaxPooling Layer 1
model.add(layers.Conv2D(1024, (3, 3), activation='relu', padding='same')) #Hidden Convolutional Layer 1
model.add(layers.Dropout(0.5, noise_shape=None, seed=None)) #Optional Dropout Layer

model.add(layers.MaxPooling2D((2, 2), padding='same')) #MaxPooling Layer 2
model.add(layers.Conv2D(1024, (3, 3), activation='relu', padding='same')) #Hidden Convolutional Layer 2
#model.add(layers.Dropout(0.5, noise_shape=None, seed=None)) #Optional Dropout Layer

model.add(layers.MaxPooling2D((2, 2), padding='same')) #MaxPooling Layer 3
model.add(layers.Conv2D(1024, (3, 3), activation='relu', padding='same')) #Hidden Convolutional Layer 3
model.add(layers.Dropout(0.5, noise_shape=None, seed=None)) #Optional Dropout Layer

model.add(layers.MaxPooling2D((2, 2), padding='same')) #MaxPooling Layer 4
model.add(layers.Conv2D(1024, (3, 3), activation='relu', padding='same')) #Hidden Convolutional layer 4
#model.add(layers.Dropout(0.5, noise_shape=None, seed=None)) #Optional Dropout Layer

model.add(layers.MaxPooling2D((2, 2), padding='same')) #MaxPooling Layer 5
model.add(layers.Conv2D(1024, (3, 3), activation='relu', padding='same')) #Hidden Convolutional Layer 5
model.add(layers.Dropout(0.5, noise_shape=None, seed=None)) #Optional Dropout Layer

model.add(layers.MaxPooling2D((2, 2), padding='same')) #MaxPooling Layer 6
model.add(layers.Conv2D(1024, (3, 3), activation='relu', padding='same')) #Hidden Convolutional Layer 6
#model.add(layers.Dropout(0.5, noise_shape=None, seed=None)) #Optional Dropout Layer

model.add(layers.Flatten()) #The Flattening Layer

model.add(layers.Dense(512, activation='relu')) #Dense Layer 1
model.add(layers.Dense(256, activation='relu')) #Dense Layer 2
model.add(layers.Dense(128, activation='relu')) #Dense Layer 3
model.add(layers.Dense(64, activation='relu')) #Dense Layer 4
model.add(layers.Dense(32, activation='relu')) #Dense Layer 5
model.add(layers.Dense(16, activation='relu')) #Dense Layer 6

model.add(layers.Dense(outputSize, activation='softmax')) #The Output Layer

model.summary()
#Compiling the Convolutional Neural Network with an Optimizer
#The Adam Optimizer is ideal for biological image classification.
#The Optimizer automatically performs forward and backward propagation.

model.compile(
    optimizer='Adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
    loss_weights=None,
    sample_weight_mode=None,
    weighted_metrics=None,
    target_tensors=None
  )

print("The Neuroimaging Model has been successfully compiled.")
#Training the Convolutional Neural Network
history = model.fit(train_images, train_labels, epochs=10, batch_size=1, verbose=1,
                    validation_data=(test_images, test_labels))

print("\nThe Neuroimaging Model has been successfully trained.")

此页上的每个代码框表示Colab或Jupyter笔记本的单个代码单元。再次欢迎并感谢所有帮助!(模型未完全构建,但添加层仅用于实验


Tags: theimportnoneaddlayerlabelsmodellayers
1条回答
网友
1楼 · 发布于 2024-04-25 22:28:56

添加行:

train_images = np.reshape(train_images, (-1,1,512,512))

在代码的下面一行之后

train_images = np.array([imgMatricesNP[0:79]])

获取单个图像“input_shape=(1, 512, 512)而不是(79, 512, 512)”,因为模型期望输入形状为(1, 1, 512, 512)(根据尺寸(批次大小、通道、高度、宽度)),而当前代码提供的输入形状为(1, 79, 512, 512)。如果您有足够的计算资源,请将批处理大小增加到8(例如),以便总输入形状为(8, 1, 512, 512)

另外,在test_images上执行类似的操作:

test_images = np.reshape(test_images, (-1,1,512,512))

行后:

test_images = np.array([imgMatricesNP[80:100]])

PS:另外,您的目的似乎是从输入imgMatricesNP中剪切前80个图像。但是,使用imgMatricesNP[0:79],您只会得到前79个图像(因为Python中排除了切片的最后一个索引)。因此,修正将是:

train_images = np.array([imgMatricesNP[0:80]])

并分配uppTrRng=80

希望这有帮助!:)

相关问题 更多 >