Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inceptionv4 pretrained model #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

Cadene
Copy link

@Cadene Cadene commented Jan 23, 2017

@soumith
Copy link
Member

soumith commented Jan 23, 2017

is it the same input normalization as all other models in the current zoo?

@Cadene
Copy link
Author

Cadene commented Jan 23, 2017

I am not sure, but it might be this i.e. sub(0.5) then mul(2.0)

I will evaluate the model on ImageNet in the near futur to be sure that everything is alright.

@apaszke
Copy link
Contributor

apaszke commented Jan 23, 2017

We could probably adjust the first layer weights to match the torchvision style normalization.

@Cadene
Copy link
Author

Cadene commented Jan 24, 2017

We could also add the preprocessing information that way, i.e. as an attribute.

@Cadene
Copy link
Author

Cadene commented Jan 24, 2017

I used the imagenet example to evaluate the model.
I've got 80.062 % accuracy top1 on valset and 94.926 accuracy top 5.
I am currently downloading the testset.

Beware, the targets are associated to the labels in the same order, but inceptionv4 has 1001 classes. The first class (index 0) is said to be the "dummy class". Thus, I shifted the targets number that way target = target.cuda(async=True).add(1)

I preprocessed images that way:

normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5],
                                 std=[0.5, 0.5, 0.5])
...
val_loader = torch.utils.data.DataLoader(
        datasets.ImageFolder(valdir, transforms.Compose([
            transforms.Scale(342),
            transforms.CenterCrop(299),
            transforms.ToTensor(),
            normalize
        ])),
        batch_size=args.batch_size, shuffle=False,
        num_workers=args.workers, pin_memory=True)

@Cadene
Copy link
Author

Cadene commented Jan 25, 2017

@apaszke I am not sure, but I don't think it's possible, because you must add a bias by color channel (the normalization in pytorch vision is made by channel).

The problem is the following.
In Tensorflow, the input image is normalized by mean=0.5, std=0.5.
In PyTorch/Vision, the input image is normalized by channel mean=0.485, 0.456, 0.406, std=0.229, 0.224, 0.225.
How to adjust the pretrained weights of the first convolutional layers to match the Pytorch normalization ?
Not a difficult problem, but here we have a strong constraint. We can't use a bias per color channel, because the conv2d has only one bias.

Let's simplify the equation. We have x one pixel, w one weight, y the output.
(x - 0.5) / 0.5 * w = y

Without biases, we have to find an epsilon e such as x, w and y remain unchanged.
(x - 0.485 ) / 0.229 * (w * e) = y
(x - 0.485 ) / 0.229 * (w * e) = (x - 0.5) / 0.5 * w

(x - 0.485 ) / 0.229 * e = (x - 0.5) / 0.5
e = (x - 0.5) / 0.5 * 0.229 / (x - 0.485)

Thus, without biases per color channel you can't do it, because the epsilon depends on the input x.

@Cadene
Copy link
Author

Cadene commented Jan 25, 2017

If it is not possible, I think we should find a nice way to associate pretrained parameters to their normalization.

Let me explain myself. I saw that you retrained VGG on ImageNet. That's really cool and we could do the same with InceptionV4, but it would be nice also that for instance people who want to reproduce results from papers using the VGG pretrained with caffe (with a different kind of normalization maybe) should be able to download the pretrained parameters of their choice associated with an information about how to normalize the input (be it on imagenet or any other dataset).

brettkoonce added a commit to brettkoonce/fastai that referenced this pull request Mar 27, 2018
brettkoonce added a commit to brettkoonce/fastai that referenced this pull request Mar 28, 2018
brettkoonce added a commit to brettkoonce/fastai that referenced this pull request Mar 31, 2018
brettkoonce added a commit to brettkoonce/fastai that referenced this pull request Mar 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants