fromkeras_cv_attention_modelsimportvolomm=volo.VOLO_d1(pretrained="imagenet")""" Run predict """importtensorflowastffromtensorflowimportkerasfromskimage.dataimportchelseaimg=chelsea()# Chelsea the catimm=keras.applications.imagenet_utils.preprocess_input(img,mode='torch')pred=mm(tf.expand_dims(tf.image.resize(imm,mm.input_shape[1:3]),0)).numpy()pred=tf.nn.softmax(pred).numpy()# If classifier activation is not softmaxprint(keras.applications.imagenet_utils.decode_predictions(pred)[0])# [('n02124075', 'Egyptian_cat', 0.9692954),# ('n02123045', 'tabby', 0.020203391),# ('n02123159', 'tiger_cat', 0.006867502),# ('n02127052', 'lynx', 0.00017674894),# ('n02123597', 'Siamese_cat', 4.9493494e-05)]
attention_layers is __init__.py only, which imports core layers defined in model architectures. Like MHSAWithPositionEmbedding from botnet, HaloAttention from halonet.
model_surgery including functions used to change model parameters after built.
fromkeras_cv_attention_modelsimportmodel_surgery# Replace all ReLU with PReLUmm=model_surgery.replace_ReLU(keras.applications.ResNet50(),target_activation='PReLU')
AotNet
Keras AotNet is just a ResNet / ResNetV2 like framework, that set parameters like attn_types and se_ratio and others, which is used to apply different types attention layer.
# Mixing se and outlook and halo and mhsa and cot_attention, 21M parameters# 50 is just a picked number that larger than the relative `num_block`fromkeras_cv_attention_modelsimportaotnetattn_types=[None,"outlook",["mhsa","halo"]*50,"cot"]se_ratio=[0.25,0,0,0]mm=aotnet.AotNet50V2(attn_types=attn_types,se_ratio=se_ratio,deep_stem=True,strides=1)