keras-cv
keras-cv copied to clipboard
modify extractor_levels in retinnet
I install kerascv as library then I use retinanet model as object detector in retinanet class I want to change extractor_levels = ["P3", "P4", "P5"] to add " P6", "P7" I tried to extend retinanet class but it didn't work.. could you help me, give me an advice please?
Hi,
Thanks for reporting the issue.
extractor levels basically are only 4 levels which are P2, P3, P4 and P5 which are coming from the backbone which is passed as argument to RetinaNet, we are ignoring the P2 and considering other 3 levels.
P6 will be computed based on P5 and P7 based on P6 as you can see below.
https://github.com/keras-team/keras-cv/blob/09601ba2e0cfc0d171dd9bbf559da963b517254c/keras_cv/models/object_detection/retinanet/feature_pyramid.py#L53-L55
Below is the sample code and output from P3 to P7 which is returned from the FeaturePyramid
import keras_cv
import keras
from keras_cv.models import ResNet50V2Backbone
from keras_cv.src.utils.train import get_feature_extractor
from keras_cv.models.retinanet import FeaturePyramid
resnet = ResNet50V2Backbone()
extractor_level = [resnet.pyramid_level_inputs[key] for key in resnet.pyramid_level_inputs.keys()]
model = get_feature_extractor(resnet,
layer_names=extractor_level,
output_keys=["P3", "P4", "P5"])
fpn = FeaturePyramid()
input_data = keras.ops.ones((8, 224, 224, 3))
outputs = model(input_data)
p3, p4, p5, p6, p7 = fpn(outputs)
print(p3.shape)
print(p4.shape)
print(p5.shape)
print(p6.shape)
print(p7.shape)
#Output
(8, 56, 56, 256)
(8, 28, 28, 256)
(8, 14, 14, 256)
(8, 7, 7, 256)
(8, 4, 4, 256)