tensorflow - 为什么我的 Ml CNN Kaggle Cats / Dogs prection 只吐出一个错误?
问题描述
我对 ML 和计算机视觉非常陌生。我正在尝试对 Cats/Dogs 0 作为 Cats 和 1 as the Dogs 进行分类预测。但是我的 model.fit() 函数会吐出这个错误。
...
ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 10000]
这是我的机器学习模型:
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import cv2
#the images are stored in the Folders 'Cat/' and 'Dog/'
animal = ['Cat/','Dog/']
images_cat= []
images_dog=[]
# reads in the images
for x in animal:
for i in range(1,12500): # the are images from '1.jpg' till '12499.jpg' for each Cats and Dogs
try:
image_path = x+ str(i) +'.jpg'# this gets the path of the images for example 'Cat/1.jpg'
#print(image_path)
img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2GRAY)
img_resized = cv2.resize(img,dsize=(100,100))
if x == 'Cat/':
images_cat.append(img_resized)
elif x == 'Dog/':
images_dog.append(img_resized)
except cv2.error as e:
#some images spit out an errer and the apprently can't be read so therefore I just give them the first image to add to the list
if x == 'Cat/':
images_cat.append(images_cat[1])
elif x == 'Dog/':
images_dog.append(images_dog[1])
# assign targets to values
y_cat = np.zeros(len(images_cat)) # Cat == 0
y_dog = np.ones(len(images_dog)) # Dog == 1
# trainig_images = 80% test_images= 20%
training_sample_count = round(0.8* len(y_cat))
#list slicing the images to get 80% of the images as calculated above
X_cat_train = images_cat [:training_sample_count]
y_cat_train_fin = y_cat[:training_sample_count]
X_dog_train = images_dog [:training_sample_count]
y_dog_train_fin = y_dog[:training_sample_count]
# create the final training list
X_train = X_cat_train + X_dog_train
y_train=[]
y_train.append(y_cat_train_fin.data)
y_train.append(y_dog_train_fin.data)
y_train = np.reshape(y_train,(19998,))
np.shape(y_train)# output: (19998,)
#normalizing the data
X_train = [x / 255.0 for x in X_train]
X_train = np.reshape(X_train,(19998,10000))
np.shape(X_train) #output: (19998, 10000)
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout, MaxPooling2D, Conv2D, Flatten
model = Sequential()
model.add(Conv2D(32,kernel_size=(5,5),padding='same', activation ='relu'))
model.add(MaxPooling2D((3,3)))
model.add(Conv2D(32,kernel_size=(5,5),padding='same', activation ='relu'))
model.add(MaxPooling2D((3,3)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='adam', loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(
X_train,
y_train,
epochs=10,
batch_size=10000)
我还没有得到测试图像,但我基本上尝试为未来的数据训练这个模型(比如然后预测自己的猫或狗的图像)。如果有人可以帮助我解决我的问题,我会很高兴,因为我被困在 atm 中。干杯:)
解决方案
Conv2D 层需要 shape 的输入(batch_size, x, y, depth)
。您的 X_train 正在被重塑为只有(batch_size, x*y)
Conv2D 不期望的大小。
只是取出这个重塑可能会起作用:X_train = np.reshape(X_train,(19998,10000))
. 如果没有,您可以重塑为(19998, 100, 100, 1)
.
推荐阅读
- javascript - 如何在 JavaScript 中显示/隐藏元素?
- python - 用通用值填充 DataFrame 中的 NaN 值
- python - 将文件从服务器容器下载到 Docker 中的客户端容器
- xcode - M1 Mac 无法在模拟器中启动任何应用程序
- google-apps-script - discord/google forms webhook 仅在我发帖时才有效,我该如何解决这个问题
- python - 我在 Python 中遇到问题 - 从导入不起作用
- c# - c# 获取 oauth2 令牌但出错(远程服务器返回 400 错误请求)
- amazon-web-services - 无法在自定义 CloudFormation AMI (WinServer 2012) 中运行远程 PowerShell 命令
- cmake - 如何在 CMake 生成器表达式中转义 <> 字符?
- r - R错误“尝试在integerOneIndex中选择少于一个元素”