如何在python上修复此值错误?在线教程

时间:2019-03-02 07:29:44

标签: python tensorflow training-data

我正在关注在线教程并正确地遵循了它,但是遇到了他没有遇到的错误。
这是我的完整代码:

import numpy as np
import cv2
import os
import matplotlib.pyplot as plt

DATADIR = "C:\\Users\\Jayson\\Desktop\\datasets"
CATEGORIES =["banana","coconut"]

for category in CATEGORIES:
     path = os.path.join(DATADIR, category)
     for img in os.listdir(path): 
          img_array = cv2.imread(os.path.join(path, img),cv2.IMREAD_GRAYSCALE)
          plt.imshow(img_array, cmap ="gray")
          plt.show()
          break
      break

IMG_SIZE = 70

new_array = cv2.resize(img_array,(IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap='gray')
plt.show()


def create_training_data():
       for category in CATEGORIES:
             path = os.path.join(DATADIR, category)
             class_num = CATEGORIES.index(category)
             for img in os.listdir(path):
           try: 
                img_array = cv2.imread(os.path.join(path, img),cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array,(IMG_SIZE, IMG_SIZE))
                training_data.append([new_array, class_num])
           except Exception as e:
                pass
create_training_data()
import random

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)

import pickle

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.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 = Sequential()
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(1))
model.add(Activation('sigmoid'))

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

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

我在第二个代码上遇到了这个错误,

  

ValueError:请提供单个数组或数组列表作为模型目标。您通过了:y = [0,1,0,0,1,1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1, 0,1,1,1,1,0,0,1,0,1,1,0,1,0,0,1,0,0,0,0,1,0,0,0,1, 0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,1, 0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,1,0,0,0,0, 1,0,0,0,0,1,1,1,1,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1,0, 1,0,0,0,1,1,1,0,0,0,1,0,0,1,1,1,0,1,0,0,0,0,0,0,0,1, 0,1,0,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,1,0,1,0,0,0,0, 0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1, 1,1,0,0,0,1,1,1,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0]

0 个答案:

没有答案