我正在研究猫与狗的分类模型,以加载以下数据。我要做的是使用OpenCV读取图像,然后使用边缘检测获取边缘并将图像的大小调整为200,200,因此最后我得到了黑白图像,白色是检测到的轮廓。
import os
import sys
import cv2
import random
from tqdm import tqdm
import matplotlib.pyplot as plt
train_images = os.listdir('data/train')
test_images = os.listdir('data/test')
test_images_data = []
for image in tqdm(test_images):
image_data = cv2.imread('data/test/' + image)
#Convert to GrayScale
#gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#convert color from BGR to RGB
image_data = cv2.cvtColor(image_data, cv2.COLOR_BGR2RGB)
image_data = cv2.resize(image_data, (200, 200))
#turn to only borders
edges = cv2.Canny(image_data, 150, 150)
test_images_data.append(edges)
train_images_data = []
train_images_labels = []
random.shuffle(train_images)
for image in tqdm(train_images):
image_data = cv2.imread('data/train/' + image)
#Convert to GrayScale
#gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#convert color from BGR to RGB
image_data = cv2.cvtColor(image_data, cv2.COLOR_BGR2RGB)
image_data = cv2.resize(image_data, (200, 200))
#turn to only borders
edges = cv2.Canny(image_data, 150, 150)
train_images_data.append(edges)
if image.startswith('cat'):
train_images_labels.append(0)
else:
train_images_labels.append(1)
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
model = Sequential()
model.add(Conv2D(32, (2, 2), input_shape=train_images_data[0].shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(train_images_data, train_images_labels, epochs=150, validation_split=0.2, batch_size=10)
问题是当我像这样运行它时出现此错误
ValueError: Input 0 is incompatible with layer conv2d_16: expected ndim=4, found ndim=3
我尝试了其他input_shape值,例如
(200, 200, 1)
(200, 200, -1)
(1, 200, 200)
(-1, 200, 200)
没有工作。
答案 0 :(得分:0)
输入形状:(200,200,1,),以使批次大小为最后一个尺寸,因此预期为4dim。
答案 1 :(得分:0)
问题有两个方面。首先,我需要将数据从列表转换为numpy数组,然后对其进行整形。
train_images_data = np.array(train_images_data)
train_images_data = train_images_data.reshape([-1, 200, 200,1])
现在这可以了
model.add(Conv2D(32, (3, 3), input_shape=(200, 200, 1)))