不兼容的形状:[15,7,7]与[15]使用TensorFlow构建VGG19模型时的错误

3lxsmp7m  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(96)
Here is the error provided after this cell:
Epoch 1/10
2023-11-07 10:50:42.834617: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 192675840 exceeds 10% of free system memory.
2023-11-07 10:50:43.002592: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 192675840 exceeds 10% of free system memory.
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/workspaces/SI-GuidedProject-592631-1697551698/Project Development Phase/Model Build.ipynb Cell 11 line 1
----> 1 vgm.fit(x_train,epochs=10,validation_data=x_test)

File ~/.python/current/lib/python3.10/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File ~/.python/current/lib/python3.10/site-packages/tensorflow/python/eager/execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     52 try:
     53   ctx.ensure_initialized()
---> 54   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     55                                       inputs, attrs, num_outputs)
     56 except core._NotOkStatusException as e:
     57   if name is not None:

InvalidArgumentError: Graph execution error:

Detected at node 'Equal' defined at (most recent call last):
    File "/home/codespace/.python/current/lib/python3.10/runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
...
    File "/home/codespace/.python/current/lib/python3.10/site-packages/keras/utils/metrics_utils.py", line 893, in sparse_categorical_matches
      matches = tf.cast(tf.equal(y_true, y_pred), backend.floatx())
Node: 'Equal'
Incompatible shapes: [15,7,7] vs. [15]
     [[{{node Equal}}]] [Op:__inference_train_function_2441]

x

from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg19 import VGG19
from keras.layers import Dense, Flatten

train_datagen = ImageDataGenerator(rescale =1./255)

x_train = train_datagen.flow_from_directory(
    'train data path',
    target_size=(224,224),
    class_mode = 'categorical',
    batch_size = 15
)

vgm = VGG19(input_shape=(224, 224, 3),weights='imagenet',include_top=False)
for i in vgm.layers:
    i.trainable = False
y=Flatten()(vgm.output)
op_layer = Dense(63,activation='softmax')(y)

vgm.summary()
Layer (type)                Output Shape              Param #   
=================================================================
 input_2 (InputLayer)        [(None, 224, 224, 3)]     0         
                                                                 
 block1_conv1 (Conv2D)       (None, 224, 224, 64)      1792      
                                                                 
 block1_conv2 (Conv2D)       (None, 224, 224, 64)      36928     
                                                                 
 block1_pool (MaxPooling2D)  (None, 112, 112, 64)      0         
                                                                 
 block2_conv1 (Conv2D)       (None, 112, 112, 128)     73856     
                                                                 
 block2_conv2 (Conv2D)       (None, 112, 112, 128)     147584    
                                                                 
 block2_pool (MaxPooling2D)  (None, 56, 56, 128)       0         
                                                                 
 block3_conv1 (Conv2D)       (None, 56, 56, 256)       295168    
                                                                 
 block3_conv2 (Conv2D)       (None, 56, 56, 256)       590080    
                                                                 
 block3_conv3 (Conv2D)       (None, 56, 56, 256)       590080    
                                                                 
 block3_conv4 (Conv2D)       (None, 56, 56, 256)       590080    
...
Total params: 20,024,384
Trainable params: 0
Non-trainable params: 20,024,384
vgm.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
vgm.fit(x_train,epochs=1)

的数据

evrscar2

evrscar21#

您没有正确定义Model。使用函数式API,您需要像这样定义它:

model = Model(inputs=input_layer, outputs=output_layer)

字符串
在你的代码中,你还没有将你的 Backbone VGG模型连接到你的其他层,比如op_layer。当你对你的模型进行前向传递时,输出是你在回溯中看到的:TensorShape([15, 7, 7, 512]),如果你正在做一个分类任务,这很可能不是你想要的。
我为你更正了所有内容,并将其变成了一个简化的最小示例:

from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg19 import VGG19
from keras.layers import Dense, Flatten
from keras import Model

train_datagen = ImageDataGenerator(rescale=1. / 255)
x_train = train_datagen.flow_from_directory(
    'path/to/my/dataset',
    target_size=(224, 224),
    class_mode='categorical',
    batch_size=15
)

number_of_classes = x_train.num_classes

vgm = VGG19(input_shape=(224, 224, 3), weights='imagenet', include_top=False)

for i in vgm.layers:
    i.trainable = False
y = Flatten()(vgm.output)
op_layer = Dense(number_of_classes, activation='softmax')(y)

model = Model(inputs=vgm.inputs, outputs=op_layer)

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

model.fit(x_train, epochs=1)

相关问题