【问题标题】:How I can fix the "value error" for my CNN如何修复 CNN 的“值错误”
【发布时间】:2021-08-26 10:16:52
【问题描述】:

首先,我的英语水平很差,很抱歉。

所以当我开始训练我的 CNN 时,它会返回这个错误:

ValueError: validation_split 仅支持张量或 NumPy 数组,在输入中发现以下类型:[, , , .... ]

我是 CNN 的初学者。我不知道错误在哪里,所以我把我的整个代码放在这里(由 sentdex YouTube 频道编写):

创建我的训练数据

import numpy as np

import os
import cv2
from tqdm import tqdm
import pickle
import random

DATADIR = "C:/content/datasets/Cats and dogs 2"

CATEGORIES = ["Dog", "Cat"]
IMG_SIZE = 100
training_data = []

def create_training_data():
    for category in CATEGORIES:  # do dogs and cats

        path = os.path.join(DATADIR,category)  # create path to dogs and cats
        class_num = CATEGORIES.index(category)  # get the classification  (0 or a 1). 0=dog 1=cat

        for img in tqdm(os.listdir(path)):  # iterate over each image per dogs and cats
            try:
                img_array = cv2.imread(os.path.join(path,img))  # convert to array
                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))  # resize to normalize data size
                training_data.append([new_array, class_num])  # add this to our training_data
            except Exception as e:  # in the interest in keeping the output clean...
                pass
            #except OSError as e:
            #    print("OSErrroBad img most likely", e, os.path.join(path,img))
            #except Exception as e:
            #    print("general exception", e, os.path.join(path,img))

create_training_data()
print(training_data)
random.shuffle(training_data)

X = []
y = []

for features,label in training_data:
    X.append(features)
    y.append(label)

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

pickle_out = open("X.pickle","wb")
pickle.dump(X, pickle_out)
pickle_out.close()

pickle_out = open("y.pickle","wb")
pickle.dump(y, pickle_out)
pickle_out.close() 

构建和训练神经网络

import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D

import pickle

pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)

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

X = X/255.0

model = Sequential()

model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors

model.add(Dense(64))

model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.fit(X, y, batch_size=32, epochs=3, validation_split=0.1)

也许“在数组 X 处重塑”很奇怪,pycharm 告诉我最后两个参数是意外的。

如果您发现我的代码有改进,请告诉我,

感谢您的帮助

【问题讨论】:

    标签: python tensorflow keras conv-neural-network


    【解决方案1】:

    ValueError:validation_split 仅支持 Tensors 或 NumPy 数组,在输入中找到以下类型:[, , , .... ]

    for features,label in training_data:
        X.append(features)
        y.append(label)
    
    X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
    

    将你的代码与错误结合起来,y是你代码中的一个列表,尝试将其转换为numpy数组。

    【讨论】:

    • 感谢您的回复!但我已经尝试这样做了,但我遇到了错误:样本不一样,基数问题
    猜你喜欢
    • 2019-09-17
    • 1970-01-01
    • 2021-07-16
    • 2017-12-18
    • 1970-01-01
    • 2020-12-19
    • 2019-05-24
    • 1970-01-01
    • 2019-11-13
    相关资源
    最近更新 更多