serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://bit.ly/deepface-py
MIT License
14.68k stars 2.22k forks source link

InceptionResNetV2 : is the unrolling wrong ? #1143

Closed AndreaLanfranchi closed 7 months ago

AndreaLanfranchi commented 7 months ago

I believe the InceptionResNetV2 function has been unrolled wrongly. Consider this code :

   [...]
    # 5x Block8 (Inception-ResNet-C block):

    branch_0 = Conv2D(
        192, 1, strides=1, padding="same", use_bias=False, name="Block8_1_Branch_0_Conv2d_1x1"
    )(x)
    branch_0 = BatchNormalization(
        axis=3,
    [...]

As the comment says (and as I've found in other implementations) the block-c should be repeated 5 times. However if we follow the repetitions we can see it goes through 6 iterations as shown here

   [...]
    mixed = Concatenate(axis=3, name="Block8_6_Concatenate")(branches)
    up = Conv2D(1792, 1, strides=1, padding="same", use_bias=True, name="Block8_6_Conv2d_1x1")(
        mixed
    )
    up = Lambda(scaling, output_shape=K.int_shape(up)[1:], arguments={"scale": 1})(up)
    x = add([x, up])

    # Classification block
    [...]

My strong recommendation is to never unroll such complex loops.

serengil commented 7 months ago

In that case, we should not be able to load the model weights.

AndreaLanfranchi commented 7 months ago

Not sure I follow.

serengil commented 7 months ago

Maybe the confusion is because of that FaceNet models has the backbone of InceptionResNetV1 instead of V2: https://github.com/serengil/deepface/blob/master/deepface/basemodels/Facenet.py#L1673

AndreaLanfranchi commented 7 months ago

That's a label ... not a value. And how is the function returning the model is named "V2" ?

serengil commented 7 months ago

should be v1

AndreaLanfranchi commented 7 months ago

Are you sure ? It seems incredibly matching V2 impl. Used this and this as comparison.

AndreaLanfranchi commented 7 months ago

You also wrote me earlier that you unrolled this impl (which is V2)

serengil commented 7 months ago

no, it should be v1. it is implemented from here: https://github.com/davidsandberg/facenet/blob/master/src/models/inception_resnet_v1.py

also documented the training stage here: https://sefiks.com/2018/09/03/face-recognition-with-facenet-in-keras/

also, reference repo mentioned that it is using v1 in its read me: https://github.com/davidsandberg/facenet

serengil commented 7 months ago

Long story short, I do not expect anything wrong in the model. If that would be possible (additional or missing layers), then pre-trained weight cannot be loaded.

Besides, FaceNet-512d and FaceNet-128d models are the most robust ones in deepface's portfolio according to the accuracy scores.

serengil commented 7 months ago

Do you mind to close this?

AndreaLanfranchi commented 7 months ago

Closing as addressed in #1144