为了账号安全,请及时绑定邮箱和手机立即绑定

Python ConvNet 图像分类器 - 为二值图像分类拟合模型时出现“ValueError”

Python ConvNet 图像分类器 - 为二值图像分类拟合模型时出现“ValueError”

一只斗牛犬 2023-06-13 11:00:59
我是深度学习和 TensorFlow/Keras 的新手,所以我无法理解为什么我在尝试拟合模型以将图像分类为“狗”或“猫”时抛出错误。第一个代码块涉及创建和保存模型(使用 pickle),第二个代码块是训练实际卷积网络的部分。下载图像数据库,保存到文件目录,编写模型训练分类器。代码如下:import numpy as npimport matplotlib.pyplot as pltimport osimport cv2DATADIR = "Pictures\\kagglecatsanddogs_3367a\\PetImages" #Workspace directory changed for postingCATEGORIES = ["Dog", "Cat"]#Iterate between all photos of dogs and catsfor category in CATEGORIES:    path = os.path.join(DATADIR, category) #path to cats or dogs dir    for img in os.listdir(path):        img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) #Converts to grayscale, does not need color in this specific instance)        plt.imshow(img_array, cmap = "gray")        break    break#Print image dimensionsprint(img_array.shape)#All the images are different-shaped photos, so they must be normalized#Everything must be made the same shape#Decide on the image size you want to go withIMG_SIZE = 180new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))training_data = []def create_training_data(): #With goal of iterating through everything and building the dataset    for category in CATEGORIES:        path = os.path.join(DATADIR, category) #path to cats or dogs dir        class_num = CATEGORIES.index(category)        for img in os.listdir(path):            try:                img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) #Converts to grayscale, does not need color in this specific instance)                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))                training_data.append([new_array, class_num])            except Exception as e:                pass
查看完整描述

4 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

您应该对 y 的 numpy 数组进行转换过程,而不仅仅是 X。


X = []

y = []


for features,label in training_data:

    X.append(features)

    y.append(label)


print(X[0].reshape(-1, IMG_SIZE, IMG_SIZE, 1))


X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)

y = np.array(y)


查看完整回答
反对 回复 2023-06-13
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

import numpy as np 

X = np.array(X).reshape(-1,IMG_SIZE,IMG_SIZE,1)  

y = np.array(y) 


import tensorflow as tf 

from tensorflow.keras.models import Sequential 

from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten

from tensorflow.keras.layers import Conv2D, MaxPooling2D



import pickle  

. . .


并继续。我也遇到过同样的问题,但它在将数据加载到 NumPy 数组后起作用,正如我通过添加定义 X 和 y 的额外行所提到的那样。


查看完整回答
反对 回复 2023-06-13
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

只需添加


 y = np.array(y) 

在您之后的最后一个程序中


 #Load models generated in previous tutorial

 x = pickle.load(open("x.pickle", "rb"))

 y = pickle.load(open("y.pickle", "rb"))


 y = np.array(y) 


查看完整回答
反对 回复 2023-06-13
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

该模型期望输入采用 numpy 数组的形式。它收到的是一个整数列表。您必须将加载的数据转换为 numpy 数组,然后将它们传递到模型中



查看完整回答
反对 回复 2023-06-13
  • 4 回答
  • 0 关注
  • 97 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信