[英]Matrix size-incompatible - Keras Tensorflow
I'm trying to train a simple model over some picture data that belongs to 10 classes.我正在尝试在属于 10 个类的一些图片数据上训练一个简单的 model。 The images are in B/W format (not gray scale), I'm using the image_dataset_from_directory to import the data into python as well as split it into validation/training sets.图像是黑白格式(不是灰度),我使用 image_dataset_from_directory 将数据导入 python 并将其拆分为验证/训练集。 My code is as below:我的代码如下:
My Imports我的进口
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense
Read Image Data读取图像数据
trainDT = tf.keras.preprocessing.image_dataset_from_directory(
data_path,
labels="inferred",
label_mode="categorical",
class_names=['0','1','2','3','4','5','6','7','8','9'],
color_mode="grayscale",
batch_size=4,
image_size=(256, 256),
shuffle=True,
seed=44,
validation_split=0.1,
subset='validation',
interpolation="bilinear",
follow_links=False,
)
Model Creation/Compile/Fit Model 创建/编译/拟合
model = Sequential([
Dense(units=128, activation='relu', input_shape=(256,256,1), name='h1'),
Dense(units=64, activation='relu',name='h2'),
Dense(units=16, activation='relu',name='h3'),
layers.Flatten(name='flat'),
Dense(units=10, activation='softmax',name='out')
],name='1st')
model.summary()
model.compile(optimizer='adam' , loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=trainDT, validation_data=train_data, epochs=10, verbose=2)
The model training returns an error: model 训练返回错误:
InvalidArgumentError Traceback (most recent call last)
....
/// anaconda paths and anaconda python code snippets in the error reporting \\\
....
InvalidArgumentError: Matrix size-incompatible: In[0]: [1310720,3], In[1]: [1,128]
[[node 1st/h1/Tensordot/MatMul (defined at <ipython-input-38-58d6507e2d35>:1) ]] [Op:__inference_test_function_11541]
Function call stack:
test_function
I don't understand where the size mismatch comes from, I've spent a few hours looking around for a solution and trying different things but nothing seems to work for me.我不明白尺寸不匹配的来源,我花了几个小时四处寻找解决方案并尝试不同的东西,但似乎没有任何东西对我有用。 Appreciate any help, thank you in advance!感谢任何帮助,在此先感谢您!
Hy mhk777 Hope you are doing well. Hy mhk777 希望你一切都好。 Brother, I think that you are confusing dense layers with convolution layers.兄弟,我认为您将密集层与卷积层混淆了。 You have to apply some convolution layers to the image before giving it to dense layers.在将图像提供给密集层之前,您必须对图像应用一些卷积层。 If you don't want to apply convolution than you have to give 2d array to the dense layer ie (number of samples, data)如果您不想应用卷积,则必须将二维数组提供给密集层,即(样本数,数据)
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
model = models.Sequential()
# Here are convolutional Layer
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256,256,1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
# Here are your dense layers
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.summary()
model.compile(optimizer='adam' , loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=trainDT, validation_data=train_data, epochs=10, verbose=2)
Dense
layers expect flat input (not 3d tensor), but you are sending (256,256,1)
shaped tensor into the first dense layer. Dense
层需要平面输入(不是 3d 张量),但是您将(256,256,1)
形状的张量发送到第一个密集层。 If you want to use dense layers from the beginning then you will need to move the flatten to be the first layer or you will need to properly reshape your data.如果你想从一开始就使用密集层,那么你需要将 flatten 移动到第一层,或者你需要正确地重塑你的数据。
model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax")
])
Also, the flatten between 2 dense layers makes no sense because the output of a dense layer is flat anyway.此外,两个密集层之间的展平没有任何意义,因为密集层的 output 无论如何都是平坦的。
From the structure of your model (especially the flatten placement), I assume that those dense layers were supposed to be convolutional layers instead.从您的 model 的结构(尤其是展平位置)来看,我假设那些密集层应该是卷积层。
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax")
])
Convolutional layers can process 2D input and they will also produce more dimensional output which you need to flatten before passing it to the dense top (note that you can add more convolutional layers).卷积层可以处理 2D 输入,它们还会产生更多维度的 output,您需要先将其展平,然后再将其传递到密集顶部(请注意,您可以添加更多卷积层)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.