简体   繁体   English

由于输入层不兼容,深度学习模型没有给出预测

[英]Deep learning model not giving predictions as input layer is incompatible

bELOW IS A SIMPLE MODEL FOR IMAGE CLASSIFICATION OF HAND GESTURE RECOGNITION using Kaggle dataset # - - coding: utf-8 - - """kaggle_dataset_code.ipynb下面是使用 Kaggle数据集进行手势识别图像分类的简单模型 # - - 编码: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")

but i get the similar following error no matter which model i try但无论我尝试哪种模型,我都会收到类似的以下错误

ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 50, 50, 3]值错误:层序列 2 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1,但接收到形状为 [None, 50, 50, 3] 的输入

The code where I want the model to make predictions is below我希望模型进行预测的代码如下

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)

As Dr. Snoopy suggested, the model is trained on Grey scale images, but you are trying to predict on RGB image.正如史努比博士所建议的那样,该模型是在灰度图像上训练的,但您正在尝试在 RGB 图像上进行预测。 Kindly use the grey scale version of the image.请使用图像的灰度版本。

Coming to your next question regarding the predictions, the last layer of your model is having model.add(Dense(10, activation='softmax')) - that means you have 10 class to be predicted and as you have used softmax function, it gives the probability of the image belonging to these 10 different classes.来到关于预测的下一个问题,模型的最后一层是model.add(Dense(10, activation='softmax')) - 这意味着你有 10 个类要预测,并且因为你使用了softmax函数,它给出了图像属于这 10 个不同类别的概率。 The sum of all the probability will be equal to 1.所有概率的总和将等于 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: Input 0 of layer "sequential_8" is in compatible with the layer - 深度学习 model - ValueError: Input 0 of layer "sequential_8" is incompatible with the layer - deep learning model 如何解释深度学习 model 的概率预测,它是最后一层的 sigmoid 激活的 output? - how to interpret a probability predictions of a deep learning model that is an output of a sigmoid activation of last layer? 输入层从深度学习模型的结构中消失 - The input layer disappears from the structure of a deep learning model 输入 0 与层 model_2 不兼容 - Input 0 is incompatible with layer model_2 在深度学习生成模型中创建掩盖层 - create a masking layer in deep learning generative model ValueError: TensorFlow2 Input 0 is in compatible with layer model - ValueError: TensorFlow2 Input 0 is incompatible with layer model ResNet: ValueError: Input 0 is in compatible with layer model_7 - ResNet: ValueError: Input 0 is incompatible with layer model_7 输入与聊天机器人预测层不兼容 model - Input incompatible with the layer for chatbot prediction model Python Keras model 输入与层不兼容 - Python Keras model input incompatible with layer 将模型保存为JSON后,使用Keras进行深度学习的模型无法给我正确的预测 - Deep learning model with Keras don't give me correct predictions after saving the model in JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM