Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can change the models two have two inputa. #25

Open
MammadTavakoli opened this issue May 4, 2024 · 1 comment
Open

How can change the models two have two inputa. #25

MammadTavakoli opened this issue May 4, 2024 · 1 comment

Comments

@MammadTavakoli
Copy link

I want design models based on some famous models that give two inputs. You designed more of them and I want change them to get two inputs same time.
can you help me how can I change your created models to do that?

@MammadTavakoli
Copy link
Author

MammadTavakoli commented May 5, 2024

I used this functions:

def rename_layers(model, prefix):
        for i, layer in enumerate(model.layers):
            # Use the private attribute _name to change the layer name
            layer.name = f"{prefix}_{layer.name}"
        return model


def create_model(model_name, learning_rate, loss_function, optimizer, class_num=3):
    if backend.image_data_format() == 'channels_last':
        bn_axis = 4
    else:
        bn_axis = 1

    inputs_ct = Input((None, None, None, 1))
    x = ZeroPadding3D(padding=(3, 3, 3), name='inputs_1_conv1_pad')(inputs_ct)
    x = Conv3D(64, (7, 7, 7),
                      strides=(2, 2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      name='inputs_1_conv1')(x)
    x = BatchNormalization(axis=bn_axis, name='inputs_1_bn_conv1')(x)
    x = Activation('relu')(x)
    x_ct = MaxPooling3D((3, 3, 3), strides=(2, 2, 2))(x)

    #
    inputs_pet = Input((None, None, None, 1))
    x = ZeroPadding3D(padding=(3, 3, 3), name='inputs_2_conv1_pad')(inputs_pet)
    x = Conv3D(64, (7, 7, 7),
                      strides=(2, 2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      name='inputs_2_conv1')(x)
    x = BatchNormalization(axis=bn_axis, name='inputs_2_bn_conv1')(x)
    x = Activation('relu')(x)
    x_pet = MaxPooling3D((3, 3, 3), strides=(2, 2, 2))(x)

    # x = add([x_1, x_2])
    x = concatenate ([x_ct, x_pet])
    input_tensor = Activation('relu')(x)
    ##########

    modelPoint, preprocess_input = Classifiers.get(model_name)

    model = modelPoint(include_top=True, input_tensor=input_tensor, classes=class_num, weights=None)

    #compile new model
    model.compile(optimizer=optimizer(learning_rate=learning_rate), 
                    loss=loss_function, 
                    metrics=['acc']) 
    return model

# model_name = 'resnet50'
# model = create_model(model_name)
# model.summary()


def create_model_2_model_padding(model_name, learning_rate, loss_function, optimizer, class_num=3):
    modelPoint, preprocess_input = Classifiers.get(model_name)

    inputs_ct = Input((None, None, None, 1))
    inputs_ct = ZeroPadding3D(padding=(3, 3, 3), name='inputs_ct_inputs_1_conv1_pad')(inputs_ct)

    inputs_pet = Input((None, None, None, 1))
    inputs_ct = ZeroPadding3D(padding=(3, 3, 3), name='inputs_pet_inputs_1_conv1_pad')(inputs_ct)


    model_ct = modelPoint(include_top=False, input_tensor=inputs_ct)
    model_ct = rename_layers(model_ct, 'model_ct')

    model_pet = modelPoint(include_top=False, input_tensor=inputs_pet)
    model_pet = rename_layers(model_pet, 'model_pet')


    combined = concatenate([model_ct.output, model_pet.output])

    # flattened = Flatten()(combined)
    fc1 = Dense(128, activation='relu', name="AddedDense1")(combined)
    output = Dense(class_num, activation='softmax', name="AddedDense2")(fc1)

    model = Model(inputs=[inputs_ct, inputs_pet], outputs=output)

    #compile new model
    model.compile(optimizer=optimizer(learning_rate=learning_rate), 
                    loss=loss_function, 
                    metrics=['acc'])
    return model


# model_name = 'resnet50'
# model = create_model_2_model_padding(model_name)
# model.summary()


def create_model_2_model(model_name, learning_rate, loss_function, optimizer, class_num=3):
    modelPoint, preprocess_input = Classifiers.get(model_name)

    inputs_ct = Input((None, None, None, 1))
    inputs_pet = Input((None, None, None, 1))


    model_ct = modelPoint(include_top=False, input_tensor=inputs_ct)
    model_ct = rename_layers(model_ct, 'model_ct')

    model_pet = modelPoint(include_top=False, input_tensor=inputs_pet)
    model_pet = rename_layers(model_pet, 'model_pet')


    combined = concatenate([model_ct.output, model_pet.output])

    # flattened = Flatten()(combined)
    fc1 =  Dense(128, activation='relu', name="AddedDense1")(combined)
    output =  Dense(class_num, activation='softmax', name="AddedDense2")(fc1)

    model =  Model(inputs=[inputs_ct, inputs_pet], outputs=output)

    #compile new model
    model.compile(optimizer=optimizer(learning_rate=learning_rate), 
                    loss=loss_function, 
                    metrics=['acc'])
    return model

# model_name = 'resnet50'
# model = create_model_2_model(model_name)
# model.summary()

def create_model_2_model_patch_size(model_name, patch_x, patch_y, patch_z, learning_rate, loss_function, optimizer, class_num=3):
    modelPoint, preprocess_input = Classifiers.get(model_name)

    inputs_ct = Input((patch_x, patch_y, patch_z, 1))
    inputs_pet = Input((patch_x, patch_y, patch_z, 1))


    model_ct = modelPoint(include_top=False, input_tensor=inputs_ct)
    model_ct = rename_layers(model_ct, 'model_ct')

    model_pet = modelPoint(include_top=False, input_tensor=inputs_pet)
    model_pet = rename_layers(model_pet, 'model_pet')


    combined = concatenate([model_ct.output, model_pet.output])

    flattened =  Flatten()(combined)
    fc1 =  Dense(128, activation='relu', name="AddedDense1")(flattened)
    output =  Dense(class_num, activation='softmax', name="AddedDense2")(fc1)

    model =  Model(inputs=[inputs_ct, inputs_pet], outputs=output)

    #compile new model
    model.compile(optimizer=optimizer(learning_rate=learning_rate), 
                    loss=loss_function, 
                    metrics=['acc'])
    return model

# patch_x = 20
# patch_y = 20
# patch_z = 60
# model_name = 'resnet50'
# model = create_model_2_model_size(model_name, patch_x, patch_y, patch_z)
# model.summary()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant