keras 在TensorFLow中输入传输模型的自定义数据集维度出错

bvjxkvbb  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(294)

有人能为我解释一下这个TensorFlow错误吗?我很难理解我做错了什么。
我在Tensorflow中有一个使用生成器构建的数据集。当我测试生成器的输出时,输出维度看起来是正确的(224 x 224 x 1)。但当我尝试训练模型时,我得到了一个错误:

WARNING:tensorflow:Model was constructed with shape (None, 224, 224, 1) for input   
KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 1), dtype=tf.float32,   
name='input_2'), name='input_2', description="created by layer 'input_2'"),   
but it was called on an input with incompatible shape (224, 224, 1, 1).

我不确定为什么这个输出的尺寸在末尾有一个额外的1
下面是创建生成器和模型的代码。df是一个 Dataframe ,其中包含指向数据和标签的文件路径。数据是可变维度的二维矩阵。我使用cv2.resize将其变为224x224,然后使用np.reshape将维度转换为(224x224x1)。然后我生成结果。

def datagen_row():
   # ======================== #
   # Import data
   # ======================== #
   df = get_data()
   rowsize = 224
   colsize = 224 
   # ======================== #
   # 
   # ======================== #
   for row in range(len(df)):
      data = get_data_from_filepath(df.iloc[row].file_path)      
      data = cv2.resize(data, dsize=(rowsize, colsize), interpolation=cv2.INTER_CUBIC)
      labels = df.iloc[row].label
      data = data.reshape( 224, 224, 1)
      yield data, labels

dataset = tf.data.Dataset.from_generator(
   datagen_row,
   output_signature=(
      tf.TensorSpec(shape = (int(os.getenv('rowsize')), int(os.getenv('colsize')), 1), dtype=tf.float32, name=None),
      tf.TensorSpec(shape=(), dtype=tf.int64, name=None)
   )
)

测试以下内容,我得到了预期的结果:

iterator = iter(dataset.batch(8))
x = iterator.get_next()
x[0].shape # TensorShape([8, 224, 224, 1])
x[1].shape # TensorShape([8])

x[0] # <tf.Tensor: shape=(8, 224, 224, 1), dtype=float32, numpy=array(... 
x[1] # <tf.Tensor: shape=(8,), dtype=int64, numpy=array([1, 1, 1, 1, 1, 1, 1, 1], dtype=int64)>

我尝试将其插入InceptionV3模型中进行分类

from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.layers import Input
from tensorflow.keras import layers

origModel = InceptionV3(weights = 'imagenet', include_top = False) 
inputs = layers.Input(shape = (224, 224, 1))
modified_inputs = layers.Conv2D(3, 3, padding = 'same', activation='relu')(inputs) 
x = origModel(modified_inputs)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(1024, activation = 'relu')(x)
x = layers.Dense(512, activation = 'relu')(x)
x = layers.Dense(256, activation = 'relu')(x)
x = layers.Dense(128, activation = 'relu')(x)
x = layers.Dense(64, activation = 'relu')(x)
x = layers.Dense(32, activation = 'relu')(x)
outputs = layers.Dense(2)(x)
model = tf.keras.Model(inputs, outputs)
model.summary() # 24.6 M trainable params

for layer in origModel.layers:
   layer.trainable = False

model.summary() # now shows 2.8 M trainable params

model.compile(
   optimizer = 'adam', 
   loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True),
   metrics = ['accuracy'] 
)

model.fit(dataset, epochs = 1, verbose = True, batch_size = 32)

下面是model.summary的输出

model.summary()
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_2 (InputLayer)        [(None, 224, 224, 1)]     0         
                                                                 
 conv2d_94 (Conv2D)          (None, 224, 224, 3)       30        
                                                                 
 inception_v3 (Functional)   (None, None, None, 2048)  21802784  
                                                                 
 global_average_pooling2d (G  (None, 2048)             0         
 lobalAveragePooling2D)                                          
                                                                 
 dense (Dense)               (None, 1024)              2098176   
                                                                 
 dense_1 (Dense)             (None, 512)               524800    
                                                                 
 dense_2 (Dense)             (None, 256)               131328    
                                                                 
 dense_3 (Dense)             (None, 128)               32896     
                                                                 
 dense_4 (Dense)             (None, 64)                8256      
                                                                 
 dense_5 (Dense)             (None, 32)                2080      
                                                                 
 dense_6 (Dense)             (None, 2)                 66        
                                                                 
=================================================================
Total params: 24,600,416
Trainable params: 2,797,632
Non-trainable params: 21,802,784
_________________________________________________________________
mrphzbgm

mrphzbgm1#

此代码在更改后工作

model.fit(dataset, epochs = 1, verbose = True, batch_size = 32)

model.fit(dataset.batch(2), epochs = 1, verbose = True, batch_size = 32)

因此...我将不得不考虑在model.fit中使用dataset.batchbatch_size

相关问题