首页 > 解决方案 > InvalidArgumentError:reshape 的输入是一个具有 320 个值的张量,但请求的形状需要 5760 的倍数

问题描述

该项目是为了判断一张人脸图像是否是假的,这就是我使用二进制分类的原因。我正在尝试使用验证数据(虽然我不太确定它做了什么)来训练和测试我的模型,因为将训练和测试数据传递到模型中会导致模型仅输出 2 个准确度。我收到一个错误,说输入形状需要是 5760 的倍数,而我的是 320。

    train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
    training_set = 
    train_datagen.flow_from_directory("/content/drive/MyDrive/real_and_fake_face/output/train",
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')
    test_datagen = ImageDataGenerator(rescale = 1./255)
    test_set = 
    test_datagen.flow_from_directory('/content/drive/MyDrive/real_and_fake_face/output/test',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')
    val_datagen = ImageDataGenerator(rescale = 1./255)
    val_set = 
    val_datagen.flow_from_directory('/content/drive/MyDrive/real_and_fake_face/output/val',
                                              target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')```
Images are 600x600x3. Image count is about 1000 and is split into train, test, and validation class. Images are stored in Google Drive and imported using a module.

Full error message: 
```    InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-10-2180c8efba4c> in <module>()
    ----> 1 model.fit(training_set, steps_per_epoch= 50, epochs=20, validation_data= val_set, 
    validation_steps= 50)

    6 frames
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in 
     quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
         58     ctx.ensure_initialized()
         59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
    ---> 60                                         inputs, attrs, num_outputs)
         61   except core._NotOkStatusException as e:
         62     if name is not None:

    InvalidArgumentError:  Input to reshape is a tensor with 320 values, but the requested shape 
    requires a multiple of 5760
         [[node sequential/flatten/Reshape (defined at <ipython-input-10-2180c8efba4c>:1) ]] 
    [Op:__inference_train_function_11377]

    Function call stack:
    train_function```

标签: pythontensorflow

解决方案


推荐阅读