keras-team / keras-cv

Industry-strength Computer Vision workflows with Keras
Other
1.01k stars 330 forks source link

Add layer names [ConvMixer] #1571

Closed IMvision12 closed 1 year ago

Anirban0011 commented 1 year ago

@IMvision12 , kindly provide some description on the issue if possible , thanks!

IMvision12 commented 1 year ago

@Anirban0011 for example below code is of ConvNeXt:

def apply_head(x, num_classes, activation="softmax", name=None):
    if name is None:
        name = str(backend.get_uid("head"))
    x = layers.GlobalAveragePooling2D(name=name + "_head_gap")(x)
    x = layers.LayerNormalization(epsilon=1e-6, name=name + "_head_layernorm")(x)
    x = layers.Dense(num_classes, activation=activation, name=name + "_head_dense")(x)
    return x

we need to add the names to each layer in the model : name=name + "_head_gap" or "name=name + "_head_layernorm""

ConvMixer:

def apply_patch_embed(x, dim, patch_size, name=None):
     if name is None:
          name = str(backend.get_uid("patch_embed"))
    x = layers.Conv2D(filters=dim, kernel_size=patch_size, strides=patch_size, name=name + "conv")(x)
    x = tf.nn.gelu(x, name=name + "gelu")
    x = layers.BatchNormalization(name=name + "batchnorm")(x)
    return x

as shown above same needs to be done with apply_conv_mixer_layer and ConvMixer class

Anirban0011 commented 1 year ago

@IMvision12 Kinda like this ?

def apply_conv_mixer_layer(x, dim, kernel_size):
    if name is None:
        name = str(backend.get_uid("apply_conv_mixer_layer"))

    residual = x
    x = layers.DepthwiseConv2D(kernel_size=kernel_size, padding="same",name=name+"depthwiseconv" )(x)
    x = tf.nn.gelu(x, name=name + "gelu")
    x = layers.BatchNormalization(name=name + "batchnorm")(x)
    x = layers.Add(name=name + "add")([x, residual])

    x = layers.Conv2D(dim, kernel_size=1,name=name + "conv")(x)
    x = tf.nn.gelu(x,name=name + "gelu")
    x = layers.BatchNormalization(name=name + "batchnorm")(x)
    return x

For layers and functions appearing more than once , should we pass the name parameter at every instance ?

IMvision12 commented 1 year ago

@Anirban0011 yeah! if there are layers that are repeating then you can just add layer numbers for eg: conv_1, conv_2, also don't forget to add "_" : name=name + "_gelu".

For more understanding, you can refer to other model files like densenet.pyor convnext.py

Anirban0011 commented 1 year ago

@Anirban0011 yeah! if there are layers that are repeating then you can just add layer numbers for eg: conv_1, conv_2, also don't forget to add "_" : name=name + "_gelu"

Ah!, sure

Anirban0011 commented 1 year ago

@IMvision12 This is what I could find in the ConvMixer Class :

   if include_rescaling:
            x = layers.Rescaling(1 / 255.0)(x)
        x = apply_patch_embed(x, dim, patch_size)

        for _ in range(depth):
            x = apply_conv_mixer_layer(x, dim, kernel_size)

        if include_top:
            x = layers.GlobalAveragePooling2D(name="avg_pool_0")(x)
            x = layers.Dense(
                num_classes,
                activation=classifier_activation,
                name="predictions",
            )(x)
        else:
            if pooling == "avg":
                x = layers.GlobalAveragePooling2D(name="avg_pool_1")(x)
            elif pooling == "max":
                x = layers.GlobalMaxPooling2D(name="max_pool")(x)

I haven't passed a name to the rescaling layer , should that be fine?

IMvision12 commented 1 year ago

Names of layers should be followed by model name: name=name(model name) + "_avg_pool_0", also apply_conv_mixer_layer and apply_patch_embed function will have an additional argument name which is missing.

Refer this how name argument is passed and used: https://github.com/keras-team/keras-cv/blob/9d012b51ad9342b7fd60078c2c14e782320393b5/keras_cv/models/convnext.py#LL142C2-L142C2

Anirban0011 commented 1 year ago

Should this be fine ?

layer-name-add-convmixer

I have not updated the apply_patch_embed function in case you want to do it.

IMvision12 commented 1 year ago

Yeah @Anirban0011 do same for apply_patch_embed

Anirban0011 commented 1 year ago

Yeah @Anirban0011 do same for apply_patch_embed

Ok , will do if you say so, want to open a PR for this @IMvision12 ?

IMvision12 commented 1 year ago

@Anirban0011 you can open PR for this if you want!!! 😃

Anirban0011 commented 1 year ago

@Anirban0011 you can open PR for this if you want!!! 😃

Do you want to commit there ?, I would mention this issue either way!

IMvision12 commented 1 year ago

That's fine go ahead with a PR!

Anirban0011 commented 1 year ago

Updated code

IMvision12 commented 1 year ago

https://github.com/Anirban0011/keras-cv/blob/a2a2d56e217e2b22350dc78b7a2085e961909398/keras_cv/models/convmixer.py#L191

UnderScore will be repeated for apply_conv_mixer_layer and apply_patch_embed

Anirban0011 commented 1 year ago

https://github.com/Anirban0011/keras-cv/blob/a2a2d56e217e2b22350dc78b7a2085e961909398/keras_cv/models/convmixer.py#L191

UnderScore will be repeated for apply_conv_mixer_layer and apply_patch_embed

Yeah , thought so, check now:

https://github.com/Anirban0011/keras-cv/blob/layer-name-add-convmixer/keras_cv/models/convmixer.py

IMvision12 commented 1 year ago

Looks Good!

Anirban0011 commented 1 year ago

Looks Good!

Should open a PR then ?

IMvision12 commented 1 year ago

Yeah, in-depth review will be done by maintainers

Anirban0011 commented 1 year ago

Got it!