亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超14個贊

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 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號