keras 我想把这两个模型连接起来

omhiaaxx  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(92)

我尝试连接以下两个模型:

input_layer = Input(shape=(227,227,3))
model1 = Sequential([
    Conv2D(20, kernel_size=(5,5), activation='relu' ),
    MaxPooling2D((2,2)),
    Conv2D(30, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Conv2D(40, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Conv2D(50, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Conv2D(60, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),
    
])(input_layer)

model2 = Sequential([
    Conv2D(20, kernel_size=(5,5), activation='relu', dilation_rate=(3)),
    MaxPooling2D((2,2)),
    Conv2D(30, kernel_size=(3,3), activation='relu', dilation_rate=(2)),
    MaxPooling2D((2,2)),
    Conv2D(40, kernel_size=(3,3), activation='relu', dilation_rate=(2)),
    MaxPooling2D((2,2)),
    Conv2D(50, kernel_size=(3,3), activation='relu', dilation_rate=(1)),
    MaxPooling2D((2,2)),
    Conv2D(60, kernel_size=(3,3), activation='relu', dilation_rate=(1)),
    MaxPooling2D((2,2)),
])(input_layer)

merged_model = Concatenate()([model1, model2])
merged_model = Flatten()(merged_model)
merged_model = Dense(1024, activation='relu')(merged_model)
merged_model = Dense(4, activation='softmax')(merged_model)`

但它显示了一个错误:
Concatenate层要求输入具有匹配的形状(串联轴除外)。接收:input_shape=[(None,5,5,60),(None,4,4,60)]
我尝试了ChatGPT,它要求我使用Flatten函数并flatten model 2,但随后它将转换为 KerasTensor,并且无法编译。我需要有关如何修复此问题或如何更改膨胀率的建议,以便两个输入形状变得相同。ChatGPT给了我这种方法:`

model1 = Sequential([
    Conv2D(20, kernel_size=(5,5), activation='relu' ),
    MaxPooling2D((2,2)),
    Conv2D(30, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Conv2D(40, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Conv2D(50, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Conv2D(60, kernel_size=(3,3), activation='relu'),
    MaxPooling2D((2,2)),  
])(input_layer)

model2 = Sequential([
    Conv2D(20, kernel_size=(5,5), activation='relu', dilation_rate=(3)),
    MaxPooling2D((2,2)),
    Conv2D(30, kernel_size=(3,3), activation='relu', dilation_rate=(2)),
    MaxPooling2D((2,2)),
    Conv2D(40, kernel_size=(3,3), activation='relu', dilation_rate=(2)),
    MaxPooling2D((2,2)),
    Conv2D(50, kernel_size=(3,3), activation='relu', dilation_rate=(1)),
    MaxPooling2D((2,2)),
    Conv2D(60, kernel_size=(3,3), activation='relu', dilation_rate=(1)),
    MaxPooling2D((2,2)),
])(input_layer)

model1 = Flatten()(model1)
model2 = Flatten()(model2)

merged_model = Concatenate()([model1, model2])
merged_model = Dense(1024, activation='relu')(merged_model)
merged_model = Dense(4, activation='softmax')(merged_model)`
cgyqldqp

cgyqldqp1#

我编译它没有任何问题。下面是代码:

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import keras

input_layer = layers.Input(shape=(227,227,3))
input_layer = layers.Input(shape=(227,227,3))
model1_out = keras.Sequential([
    layers.Conv2D(20, kernel_size=(5,5), activation='relu' ),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(30, kernel_size=(3,3), activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(40, kernel_size=(3,3), activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(50, kernel_size=(3,3), activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(60, kernel_size=(3,3), activation='relu'),
    layers.MaxPooling2D((2,2)),  
])(input_layer)

model2_out = keras.Sequential([
    layers.Conv2D(20, kernel_size=(5,5), activation='relu', dilation_rate=(3)),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(30, kernel_size=(3,3), activation='relu', dilation_rate=(2)),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(40, kernel_size=(3,3), activation='relu', dilation_rate=(2)),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(50, kernel_size=(3,3), activation='relu', dilation_rate=(1)),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(60, kernel_size=(3,3), activation='relu', dilation_rate=(1)),
    layers.MaxPooling2D((2,2)),
])(input_layer)

model1_out = layers.Flatten()(model1_out)
model2_out = layers.Flatten()(model2_out)

merged_out = layers.Concatenate()([model1_out, model2_out])
merged_out = layers.Dense(1024, activation='relu')(merged_out)
merged_out = layers.Dense(4, activation='softmax')(merged_out)

model = keras.Model(input_layer, merged_out)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy")

相关问题