深度学习模型不提供预测作为输入层是不兼容的
Posted
技术标签:
【中文标题】深度学习模型不提供预测作为输入层是不兼容的【英文标题】:Deep learning model not giving predictions as input layer is incompatible 【发布时间】:2020-12-30 19:58:12 【问题描述】:以下是使用 Kaggle 对手势识别进行图像分类的简单模型dataset # -- 编码:utf-8 -- """kaggle_dataset_code.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kfj2kPVrioXlWX_CDDOGEfxlwMUj5vs6
"""
!pip install kaggle
#You can download the kaggl.json file from your kaggle account. We are going to upload the kaggle.json file.
from google.colab import files
files.upload()
#making kaggle directory as kaggle website has guided.
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
#Giving specical permissions to the kaggle.json file.
!chmod 600 ~/.kaggle/kaggle.json
downloading the kaggle dataset from the website by copying the API token
!kaggle datasets download -d gti-upm/leapgestrecog
#Unzip the dataset
zip_data_path = "/content/leapgestrecog.zip"
from zipfile import ZipFile
file_name = "leapgestrecog.zip"
with ZipFile(file_name,'r') as zip:
zip.extractall()
print("done")
import cv2
image_data = []
CATEGORIES = ["01_palm", '02_l','03_fist','04_fist_moved','05_thumb','06_index','07_ok','08_palm_moved','09_c','10_down']
IMG_SIZE = 50
import os
unzipped_data_path = "/content/leapgestrecog/leapGestRecog/"
print(os.listdir(unzipped_data_path))
for dr in os.listdir(unzipped_data_path):
for category in CATEGORIES:
class_index = CATEGORIES.index(category)
path = os.path.join(unzipped_data_path, dr, category)
for image in os.listdir(path):
image_array = cv2.imread(os.path.join(path, image), cv2.IMREAD_GRAYSCALE)
image_data.append([cv2.resize(image_array, (IMG_SIZE, IMG_SIZE)), class_index])
#image data of a 19000th image
image_data[19000]
import random
random.shuffle(image_data)
input_data = []
label = []
for X, y in image_data:
input_data.append(X)
label.append(y)
import matplotlib.pyplot as plt # for plotting
plt.figure(1, figsize=(10,10))
for i in range(1,10):
plt.subplot(3,3,i)
plt.imshow(image_data[i][0], cmap='hot')
plt.xticks([])
plt.yticks([])
plt.title(CATEGORIES[label[i]][3:])
plt.show()
import numpy as np
input_data = np.array(input_data)
label = np.array(label)
input_data = input_data/255.0
import keras
label = keras.utils.to_categorical(label, num_classes=10,dtype='i1')
label[0]
input_data.shape = (-1, IMG_SIZE, IMG_SIZE, 1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(input_data, label, test_size = 0.3, random_state=0)
from keras.layers import Conv2D, Activation, MaxPool2D, Dense, Flatten, Dropout
model = keras.models.Sequential()
model.add(Conv2D(filters = 32, kernel_size = (3,3), input_shape = (IMG_SIZE, IMG_SIZE, 1)))
model.add(Activation('relu'))
model.add(Conv2D(filters = 32, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Conv2D(filters = 64, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
model.summary()
model.fit(X_train, y_train, epochs = 7, batch_size=32, validation_data=(X_test, y_test))
score = model.evaluate(X_test, y_test, batch_size=128)
print(score)
model.save("kaggle_dataset_model.h5")
但无论我尝试哪种型号,我都会收到类似的以下错误
ValueError: 层序号_2 的输入 0 与层不兼容:输入形状的轴 -1 的预期值为 1,但接收到的输入形状为 [None, 50, 50, 3]
我希望模型进行预测的代码如下
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1PWDO7aYA6Lhl9FgdgMHh8fj-vlLF_mTw
"""
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
# dimensions of our images
img_width = 50
img_height = 50
# load the model we saved
model = load_model('KaggleModelLeapGesture.h5')
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
from google.colab import files
from keras.preprocessing import image
uploaded = files.upload()
for fn in uploaded.keys():
# predicting images
path = fn
img = image.load_img(path, target_size=(50, 50))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(fn)
print(classes)
【问题讨论】:
您必须将代码作为文本放入问题本身,而不是作为外部链接。 @Dr.Snoopy 我已经包含了代码你现在可以识别错误了吗? 您的训练图像被加载为灰度图像,但在您的代码中进行预测您没有这样做,我建议您使用与为预测图像加载训练图像相同的代码。 @Dr.Snoopy 感谢您的指出。我已将测试代码中的行从 img = image.load_img(path, target_size=(50, 50)) 更改为 img = image.load_img(path , target_size=(50, 50),color_mode='grayscale') 但在手掌图片的情况下,我得到以下预测。 [[3.7882763e-19 8.0912974e-09 5.0884543e-19 1.0000000e+00 8.5896360e-27 3.1044631e-12 3.7023463e-13 2.7725446e-12 1.5253e-12 1.562454958e-2]跨度> 我不确定你需要什么样的指南,你有你的预测,所以问题似乎已经解决了。 【参考方案1】:正如Dr. Snoopy 建议的那样,该模型是在灰度图像上训练的,但您正在尝试在 RGB 图像上进行预测。请使用图像的灰度版本。
关于预测的下一个问题,模型的最后一层是 model.add(Dense(10, activation='softmax'))
- 这意味着您有 10 个类要预测,并且由于您使用了 softmax
函数,它给出了图像的概率属于这 10 个不同的类别。所有概率的总和将等于 1。
【讨论】:
有没有办法告诉最高预测,以便告诉图像属于哪个类别以上是关于深度学习模型不提供预测作为输入层是不兼容的的主要内容,如果未能解决你的问题,请参考以下文章
深度学习多变量时间序列预测:GRU算法构建时间序列多变量模型预测交通流量+代码实战
深度学习时间序列预测:LSTM算法构建时间序列单变量模型预测大气压( air pressure)+代码实战