MosyMosy / Feature-Normalization

The "Revisiting Learnable Affines for Batch Norm in Few-Shot Transfer Learning" Code-base
MIT License
5 stars 1 forks source link

Replicating Fine-Affine experiments #1

Open superkirill opened 1 year ago

superkirill commented 1 year ago

I am struggling to find the implementation for the Fine-Affine experiments shown in Table 1, which seem to be the main contribution of the paper. Could you please help me with this?

MosyMosy commented 1 year ago

Hi, Superkirill Sure, you can repeat the experiments using the ImageNet_na.py file. train: python ImageNet_na.py --dir ./logs/ImageNet_na/ --arch resnet18 --data ./data/ILSVRC/Data/CLS-LOC --gpu 0

superkirill commented 1 year ago

Hi! But wouldn't it just disable the affines during pretraining on ImageNet? What about finetuning the affines jointly with the classifier during transferring to CD-FSL datasets?

MosyMosy commented 1 year ago

Ok. So to do the fine affine you need to replace lines 127 to 136 of the ImageNet_finetune.py with this code and do the fine-tuning without "--freeze_backbone"

###############################################################################################
    loss_fn = nn.CrossEntropyLoss().to(device)
    classifier_opt = torch.optim.SGD(classifier.parameters(), lr=0.01, momentum=0.9, dampening=0.9, weight_decay=0.001)

        if not params.freeze_backbone:
        pretrained_model.eval()
        for layer in pretrained_model.modules():
                if isinstance(layer, nn.BatchNorm2d):
                        layer.bias.requires_grad = True
                        layer.weight.requires_grad = True
    delta_opt = torch.optim.SGD(filter(lambda p: p.requires_grad, pretrained_model.parameters()), lr=0.01)
    ###############################################################################################