Tensorflow.Keras говорит, что ввод отличается от ожидаемого ввода - PullRequest
0 голосов
/ 23 февраля 2019

Я пишу простой CNN, чтобы классифицировать различные черты лица мультфильма.Я использую этот набор данных.Когда я пытаюсь запустить свой код, я получаю следующую ошибку:

Traceback (most recent call last):
  File "cartoonKerasPy.py", line 86, in <module>
    model.fit(x_train, y_train, batch_size=8, epochs=3)
  File "C:\Users\befbr\Anaconda3\envs\Cartoon AI\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1536, in fit
    validation_split=validation_split)
  File "C:\Users\befbr\Anaconda3\envs\Cartoon AI\lib\site-packages\tensorflow\python\keras\engine\training.py", line 992, in _standardize_user_data
    class_weight, batch_size)
  File "C:\Users\befbr\Anaconda3\envs\Cartoon AI\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1154, in _standardize_weights
    exception_prefix='target')
  File "C:\Users\befbr\Anaconda3\envs\Cartoon AI\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 293, in standardize_input_data
    str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 160 arrays: [0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0...

Вот мой код:

print("Importing Packages")
import os
import tensorflow as tf
from tensorflow import keras as k
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import pandas as pd
from PIL import Image

def createModel():
    model = k.models.Sequential([
      k.layers.Conv3D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(500,500, 4, 1)),
      k.layers.MaxPooling3D(pool_size=2),
      k.layers.Dropout(0.2),
      k.layers.Flatten(),
      k.layers.Dense(1000, activation='relu'),
      k.layers.Dense(1, activation='softmax')

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

def getFile(dir, getEveryNthLine):
    allFiles = list(os.listdir(dir))
    # print(allFiles)
    fileNameList = []
    numOfFiles = len(allFiles)
    i = 0
    for fichier in allFiles:
        if(i % getEveryNthLine == 0):
                # print(fichier)
                if(fichier.endswith(".csv")):
                    fileNameList.append(dir + "/" + fichier[0:-4])
        i += 1
    return fileNameList

print("Creating Model")
model = createModel()

# print("Model Summary")
# model.summary()

print("\nLoad Files")
files = getFile("F:/cartoonset10k/", 100)
print("Loaded " + str(len(files)) + " file names")

print("Split Data Into Train And Test")
train, test = train_test_split(files, test_size=0.2)

x_train = []
y_train = []
x_test = []
y_test = []

def getLabels(filePath):
    df = []
    with open(filePath, "r") as file:
        for line in list(file):
            tempList = line.replace("\n", "").replace('"', "").replace(" ", "").split(",")
            df.append({
                "attr": tempList[0],
                "value":int(tempList[1]),
                "maxValue":int(tempList[2])
            })
    return df

for i in range(len(train)):

    x_train.append([list(Image.open(train[i] + ".png").getdata())])
    y_train.append(pd.DataFrame(getLabels(train[i] + ".csv"))["value"][1])

print("Finished Formating\n\n")

x_train = np.reshape(x_train, (len(train), 500, 500, 4, 1))

with tf.device('/gpu'):
    model.fit(x_train, y_train, batch_size=8, epochs=3)

Кто-нибудь знает, что его вызывает?Я думаю, что проблема связана с формой ввода, но я могу быть очень не прав.Где я могу найти правильную форму ввода?

...