在将数据拟合到模型时,为什么会产生值错误?

时间:2020-05-20 06:09:25

标签: python tensorflow keras

调用此函数时

值错误

from tensorflow.keras.utils import to_categorical
from PIL import Image

def get_data_generator(df, indices, for_training, batch_size=16):
    images, labels = [], []
    while True:
        for i in indices:
            r = df.iloc[i]
            file, label= r['file'], r['label']
            im = Image.open(file)           
            im = np.array(im)/255.0
            images.append(np.array(im))
            labels.append(np.array([np.array(to_categorical(int(Char2Num(i)), N_LABELS)) for i in label]))
            if (len(images)>= batch_size):
                #print(np.array(images), np.array(labels))              
                yield np.array(images), np.array(labels)
                images, labels=[], []
        if not for_training:
            break

来自此代码:

from tensorflow.keras.callbacks import ModelCheckpoint

batch_size = 64     
valid_batch_size = 64
train_gen = get_data_generator(df, train_idx, for_training=True, batch_size=batch_size)
print(train_gen)
valid_gen = get_data_generator(df, valid_idx, for_training=True, batch_size=valid_batch_size)
print(valid_gen)
callbacks = [ModelCheckpoint("./model_checkpoint", monitor='val_loss')]
history=model.fit(train_gen,steps_per_epoch=len(train_idx)//batch_size,epochs=5,callbacks=callbacks,validation_data=valid_gen,validation_steps=len(valid_idx)//valid_batch_size)                   

错误如下:

ValueError:检查输入时出错:预期input_1具有4维,但数组的形状为(无,无,无)

0 个答案:

没有答案