Skip to content

Better variable naming in Darknet models. #1297

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

Closed
sebastian-sz opened this issue Jan 21, 2023 · 4 comments · Fixed by #1298
Closed

Better variable naming in Darknet models. #1297

sebastian-sz opened this issue Jan 21, 2023 · 4 comments · Fixed by #1298

Comments

@sebastian-sz
Copy link
Contributor

Currently when we inspect Darknet variables:

from keras_cv.models import DarkNet51
m = DarkNet51(include_rescaling=False, include_top=True, classes=1000, input_shape=(224, 224, 3))
for v in m.variables:
    print(v.name)

we will get very unclear information about the variables:

batch_normalization_56/moving_variance:0
conv2d_57/kernel:0
batch_normalization_57/gamma:0

essentially a flat list of convs + BN. This makes inspecting variables or transfering weights very difficult if not impossible.

The problem:

This will propagate on all Darknet based futere implementations: YoloV7 YoloV8, YoloX etc.

The fix:

In Darknet Conv Block the name variable is unused. One could change:
from

    model_layers = [  # line 50
        layers.Conv2D(
            filters,
            kernel_size,
            strides,
            padding="same",
            use_bias=use_bias,
        ),
        layers.BatchNormalization(),
    ]

to

    model_layers = [
        layers.Conv2D(
            filters,
            kernel_size,
            strides,
            padding="same",
            use_bias=use_bias,
            name=name+"_conv"
        ),
        layers.BatchNormalization(name=name+"_bn"),
    ]

After fix, the weights seem to be loading without issue on my machine and the variables have beautiful meaningful names:

dark5_conv4_conv/kernel:0
dark5_conv4_bn/gamma:0
dark5_conv4_bn/beta:0
dark5_conv4_bn/moving_mean:0
dark5_conv4_bn/moving_variance:0
@LukeWood
Copy link
Contributor

Thanks Sebastian! @quantumalaviya for more comments.

feel free to raise a PR addressing this

@quantumalaviya
Copy link
Contributor

Sure, we can do this. Thanks!

This makes inspecting variables or transfering weights very difficult if not impossible.

When you say "transferring weights" here, does it mean model.load_weights() is difficult or did you mean something else?

@sebastian-sz
Copy link
Contributor Author

@quantumalaviya Thanks!

does it mean model.load_weights()

Not really. Recently I played around with some Yolo implementations in Tensorflow and wanted to reuse existing darknet weights from this repo, but I gave up after noticing that it's a flat list of mostly integer names (with batchnorms appearing mostly before convs).

I was thinking that if we have this option to add names to layers / variables it is much more readable and allows such weights reuse.

@quantumalaviya
Copy link
Contributor

Ah, got it.
I will update #1296 keeping this in mind, thanks!

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

Successfully merging a pull request may close this issue.

3 participants