Tensorflow ValueError: Failed to find data adapter that can handle input

﹥>﹥吖頭↗ 提交于 2021-02-10 20:32:08

问题


Hello I'm trying to make the basic example of tensorflow minst using data from images on my pc. But I run into this error all the time: "ValueError: Failed to find data adapter that can handle input: , ( containing values of types {""})"

here's how i generate data:

import numpy as np # for array operations
import matplotlib.pyplot as plt # to show image
import os # to move through directories
import cv2 # to make image operations
import random
import pickle

DATADIR=r"C:\Users\...\mnist_png\training"
DIGITS = ["0","1","2","3","4","5","6","7","8","9"]

training_data = []

for digit in DIGITS:
    path = os.path.join(DATADIR, digit)
    class_num = DIGITS.index(digit)
    for img in os.listdir(path):  
        img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)               
        training_data.append([img_array, class_num])        

random.shuffle(training_data)

X = []
y = []

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

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

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()

and here's the tensorflow model that I get error from:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle

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

X=X/255.0

model = Sequential()

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

model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))

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


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

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

Please help me


回答1:


After

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

you have to add

y = np.array(y)


来源:https://stackoverflow.com/questions/58422670/tensorflow-valueerror-failed-to-find-data-adapter-that-can-handle-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!