【问题标题】:conv2D input shape error with flow_from_dataframe带有 flow_from_dataframe 的 conv2D 输入形状错误
【发布时间】:2021-02-28 13:02:12
【问题描述】:

我正在尝试训练一个可以检测的 CNN。因此,我在一个名为“allImages”的文件夹中有图像。我将相应图像的信息存储在 csv 文件中。为了处理大量数据,我使用了 flow_from_dataframe 函数。

CSV 文件如下所示:

首先,我已经初始化了 flow_from_DataFrame 方法

traindf=pd.read_csv("face_features.csv",)

columns = ['LTX','LTY','LBX','LBY','RBX','RBY','RTX','RTY','TLLX','TLLY','TLRX','TLRY','BottomX','BottomY','TRLX','TRLY','TRRX','TRRY']

datagen=ImageDataGenerator(rescale=1./255.,validation_split=0.25)

train_generator=datagen.flow_from_dataframe(
dataframe=traindf,
directory="allImages/",
x_col='StringName',
y_col=columns,
subset="training",
batch_size=32,
seed=42,
class_mode = "input",
shuffle=True,
target_size=(158,163))

然后,我正在尝试构建和拟合模型。


model_layers = [
               
   tf.keras.layers.Conv2D( 158 , input_shape=(32, 158,163,3) , kernel_size=( 3 , 3 ), activation='relu' ),

   tf.keras.layers.Conv2D( 158 , kernel_size=( 3 , 3 ) , strides=2 , activation='relu' ),
   tf.keras.layers.BatchNormalization(),

   tf.keras.layers.Conv2D( 18 , kernel_size=( 3 , 3 ) , strides=1 , activation='relu' ),
   tf.keras.layers.Conv2D( 18 , kernel_size=( 3 , 3 ) , strides=1 , activation='relu' ),
   tf.keras.layers.Conv2D( 18 , kernel_size=( 3 , 3 ) , strides=1 ),

]
model = tf.keras.Sequential(model_layers)
model.compile( loss=tf.keras.losses.mean_squared_error , optimizer=tf.keras.optimizers.Adam( lr=0.0001 ) , metrics=[ 'mse' ] )



model.fit_generator(generator=train_generator,
                   steps_per_epoch=20,
                   epochs=20)

但不幸的是,keras 抛出了输入错误。

WARNING:tensorflow:Model was constructed with shape (None, 32, 158, 163, 
3) for input Tensor("conv2d_92_input:0", shape=(None, 32, 158, 163, 3), 
dtype=float32), but it was called on an input with incompatible shape (None, None, None, None).

当我试图获得 train_generator 的 x 形状时。我得到 (32, 158, 163, 3)。

谁能帮忙解决这个问题?

珍惜你的时间。

【问题讨论】:

    标签: python tensorflow keras conv-neural-network


    【解决方案1】:

    模型中第一层的input_shape中的batch size维度就不用提了:

    替换 -> tf.keras.layers.Conv2D( 158 , input_shape=(32, 158,163,3), kernel_size=( 3 , 3 ), activation='relu' )

    与 -> tf.keras.layers.Conv2D( 158, kernel_size=( 3 , 3 ), activation='relu', input_shape=(158,163,3))

    进一步阅读: 你可以查一下Shapes in Keras。

    【讨论】:

    • 谢谢你提到这一点。但不知何故,我遇到了一个新错误:Incompatible shapes: [32,55,58,18] vs. [32,158,163,3] [[node gradient_tape/mean_squared_error/BroadcastGradientArgs (defined at /Users/Desktop/face_landmarks_cleaned/untitled12.py:90) ]] [Op:__inference_train_function_2400] Function call stack: train_function
    猜你喜欢
    • 2018-08-15
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多