Package: keras-efficientnet-v2
Keras EfficientNetV2AsEfficientNetV2is included inkeras.applicationnow, merged this project intoGithub leondgarse/keras_cv_attention_models/efficientnet. Packagekeras-efficientnet-v2moved intostablestatus.Table of ContentsKeras EfficientNetV2Table of ContentsSummaryUsageTraining detail from articleDetailed conversion procedureProgressive train test on cifar10Related ProjectsSummaryMy own keras implementation ofOfficial efficientnetv2. ArticlearXiv 2104.00298 EfficientNetV2: Smaller Models and Faster Trainingby Mingxing Tan, Quoc V. Le.h5model weights converted from official publication.effv2-t-imagenet.h5model weights converted fromGithub rwightman/pytorch-image-models. which claimed both faster and better accuracy thanb3. Please notice thatPyTorchusing differentbn_epsilonandpaddingstrategy.V2 ModelParamsTop1InputImageNet21KImagenet21k-ft1kImagenetEffV2B07.1M78.7224v2b0-21k.h5v2b0-21k-ft1k.h5v2b0-imagenet.h5EffV2B18.1M79.8240v2b1-21k.h5v2b1-21k-ft1k.h5v2b1-imagenet.h5EffV2B210.1M80.5260v2b2-21k.h5v2b2-21k-ft1k.h5v2b2-imagenet.h5EffV2B314.4M82.1300v2b3-21k.h5v2b3-21k-ft1k.h5v2b3-imagenet.h5EffV2T13.6M82.5320v2t-imagenet.h5EffV2S21.5M84.9384v2s-21k.h5v2s-21k-ft1k.h5v2s-imagenet.h5EffV2M54.1M86.2480v2m-21k.h5v2m-21k-ft1k.h5v2m-imagenet.h5EffV2L119.5M86.9480v2l-21k.h5v2l-21k-ft1k.h5v2l-imagenet.h5EffV2XL206.8M87.2512v2xl-21k.h5v2xl-21k-ft1k.h5EfficientNetV1 noisy_student modelsfromGithub tensorflow/tpu/efficientnet. PaperPDF 1911.04252 Self-training with Noisy Student improves ImageNet classification. Parameterinclude_preprocessing=Falseis added, and the defaultFalsevalue makes expecting input value in range[-1, 1], same withEfficientNetV2. Defaultpretrainedisnoisy_student.V1 ModelParamsTop1Inputnoisy_studentImagenetEffV1B05.3M78.8224v1-b0-noisy_student.h5v1-b0-imagenet.h5EffV1B17.8M81.5240v1-b1-noisy_student.h5v1-b1-imagenet.h5EffV1B29.1M82.4260v1-b2-noisy_student.h5v1-b2-imagenet.h5EffV1B312.2M84.1300v1-b3-noisy_student.h5v1-b3-imagenet.h5EffV1B419.3M85.3380v1-b4-noisy_student.h5v1-b4-imagenet.h5EffV1B530.4M86.1456v1-b5-noisy_student.h5v1-b5-imagenet.h5EffV1B643.0M86.4528v1-b6-noisy_student.h5v1-b6-imagenet.h5EffV1B766.3M86.9600v1-b7-noisy_student.h5v1-b7-imagenet.h5EffV1L2480.3M88.4800v1-l2-noisy_student.h5Self tested imagenet accuracyrescale_modetorchmeans(image - [0.485, 0.456, 0.406]) / [[0.229, 0.224, 0.225]],tfmeans(image - 0.5) / 0.5Allresize_methodisbicubic.Some testing detail is not clear, so not exactly matching official reported results.Testing Detail isEfficientNetV2 self tested imagenet accuracy.modelinputrescale_modecentral_croptop 1top 5Reported top1EffV2B0224torch0.8750.787480.943860.787EffV2B1240torch0.950.79870.949360.798EffV2B2260torch0.950.806420.952620.805EffV2B3300torch0.950.820980.958960.821EffV2T320torch0.990.825060.962280.823 (input 288)EffV2S384tf0.990.83860.9670.839EffV2M480tf0.990.85090.9730.852EffV2L480tf0.990.8550.973240.857EffV2S ft1k384tf0.990.843280.972540.849EffV2M ft1k480tf0.990.856060.97750.862EffV2L ft1k480tf0.990.862940.97990.869EffV2XL ft1k512tf0.990.865320.978660.872UsageThis repo can be installed as a pip package, or justgit cloneit.pipinstall-Ukeras-efficientnet-v2# Orpipinstall-Ugit+https://github.com/leondgarse/keras_efficientnet_v2Define model and load pretrained weightsParameterpretrainedis added in value[None, "imagenet", "imagenet21k", "imagenet21k-ft1k"], default isimagenet. Model input value should be in range[-1, 1].# Will download and load `imagenet` pretrained weights.# Model weight is loaded with `by_name=True, skip_mismatch=True`.importkeras_efficientnet_v2model=keras_efficientnet_v2.EfficientNetV2S(pretrained="imagenet")# Run predictionimporttensorflowastffromtensorflowimportkerasfromskimage.dataimportchelseaimm=tf.image.resize(chelsea(),model.input_shape[1:3])# Chelsea the catpred=model(tf.expand_dims(imm/128.-1.,0)).numpy()print(keras.applications.imagenet_utils.decode_predictions(pred)[0])# [('n02124075', 'Egyptian_cat', 0.8642886), ('n02123159', 'tiger_cat', 0.030793495), ...]Or downloadh5model and load directlymm=keras.models.load_model('efficientnetv2-b3-21k-ft1k.h5')For"imagenet21k"pre-trained model, actualnum_classesis21843.Exclude model top layersby setnum_classes=0.importkeras_efficientnet_v2model=keras_efficientnet_v2.EfficientNetV2B0(dropout=1e-6,num_classes=0,pretrained="imagenet21k")print(model.output_shape)# (None, 7, 7, 1280)model.save('efficientnetv2-b0-21k-notop.h5')Use dynamic input resolutionby setinput_shape=(None, None, 3).importkeras_efficientnet_v2model=keras_efficientnet_v2.EfficientNetV2M(input_shape=(None,None,3),drop_connect_rate=0.2,num_classes=0,pretrained="imagenet21k-ft1k")print(model(np.ones([1,224,224,3])).shape)# (1, 7, 7, 1280)print(model(np.ones([1,512,512,3])).shape)# (1, 16, 16, 1280)include_preprocessingsetTruewill add pre-processingRescale + NormalizationafterInput. Means using input value in range[0, 255]. Default valueFalsemeans in range[-1, 1]. Works both forEfficientNetV2andEfficientNetV1.importkeras_efficientnet_v2model=keras_efficientnet_v2.EfficientNetV1B4(pretrained="noisy_student",include_preprocessing=True)fromskimage.dataimportchelseaimm=tf.image.resize(chelsea(),model.input_shape[1:3])# Chelsea the catpred=model(tf.expand_dims(imm,0)).numpy()# value in range [0, 255]print(keras.applications.imagenet_utils.decode_predictions(pred)[0])# [('n02124075', 'Egyptian_cat', 0.68414235), ('n02123159', 'tiger_cat', 0.04486668), ...]Training detail from articleTraining configures,Eval sizeis used as the defaultinput_shapefor each model type.ModelTrain sizeEval sizeDropoutRandaugMixupEffV2B01922240.200EffV2B11922400.200EffV2B22082600.300EffV2B32403000.300EffV2S3003840.2100EffV2M3844800.3150.2EffV2L3844800.4200.5EffV2XL3845120.4200.5EfficientNetV2-S architectureStageOperatorStride#Channels#Layers0Conv3x322411Fused-MBConv1, k3x312422Fused-MBConv4, k3x324843Fused-MBConv4, k3x326444MBConv4, k3x3, SE0.25212865MBConv6, k3x3, SE0.25116096MBConv6, k3x3, SE0.252256157Conv1x1 & Pooling & FC-12801Progressive training settings for EfficientNetV2S minS maxM minM maxL minM maxImage Size128300128380128380RandAugment515520525Mixup alpha0000.200.4Dropout rate0.10.30.10.40.10.5Imagenet training detailRMSProp optimizer with decay 0.9 and momentum 0.9batch norm momentum 0.99; weight decay 1e-5Each model is trained for 350 epochs with total batch size 4096Learning rate is first warmed up from 0 to 0.256, and then decayed by 0.97 every 2.4 epochsWe use exponential moving average with 0.9999 decay rateRandAugment (Cubuk et al., 2020)Mixup (Zhang et al., 2018)Dropout (Srivastava et al., 2014)and stochastic depth (Huang et al., 2016) with 0.8 survival probabilityDetailed conversion procedureconvert_effnetv2_model.pyis a modified version ofthe orignal effnetv2_model.py. Check detail byvimdiff convert_effnetv2_model.py ../automl/efficientnetv2/effnetv2_model.pyDelete somenames, as they may cause confliction in keras.Use.calldirectly callingsemodules and other blocks, so they will not beblocksinmodel.summary()Just useAddlayer instead ofutils.drop_connect, as whenis_training=False,utils.drop_connectfunctions likeAdd.Add anum_classesparameter outside ofmconfig.Add__main__part, which makes this can be run as a script. Refer to it for converting detail.Depends on official repo../
├──automl# Official repo├──keras_efficientnet_v2# This oneProcedure# See help infoCUDA_VISIBLE_DEVICES='-1'pythonconvert_effnetv2_model.py-h# Convert by specific model_type and dataset typeCUDA_VISIBLE_DEVICES='-1'pythonconvert_effnetv2_model.py-mb0-dimagenet21k# Convert by specific model_type and all its datasets ['imagenet', 'imagenet21k', 'imagenetft']CUDA_VISIBLE_DEVICES='-1'pythonconvert_effnetv2_model.py-ms-dall# Convert all model_types and and all datasetsCUDA_VISIBLE_DEVICES='-1'pythonconvert_effnetv2_model.py-mall-dallProgressive train test on cifar10Colab efficientnetV2_basic_test.ipynbimportkeras_efficientnet_v2fromtensorflowimportkerasfromkeras_efficientnet_v2importprogressive_train_testmodel=keras_efficientnet_v2.EfficientNetV2S(input_shape=(None,None,3),num_classes=10,classifier_activation='softmax',dropout=0.1)model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"])hhs=progressive_train_test.progressive_with_dropout_randaug(model,data_name="cifar10",lr_scheduler=None,total_epochs=36,batch_size=64,dropout_layer=-2,target_shapes=[128,160,192,224],# [128, 185, 242, 300] for final shape (300, 300)dropouts=[0.1,0.2,0.3,0.4],magnitudes=[5,8,12,15],)withopen("history_ev2s_imagenet_progressive_224.json","w")asff:json.dump(hhs,ff)Related Projectstfa.layers.StochasticDepthOfficial efficientnetv2Github rwightman/pytorch-image-models