vision icon indicating copy to clipboard operation
vision copied to clipboard

Make number of input channels in classification models configurable

Open Ingwar opened this issue 6 years ago • 5 comments

🚀 Feature

Allow users to create classification models (ResNet, VGG, etc) with a number of input channels other than 3.

Motivation

Classification models from CV are often used as feature extractors in other fields (e.g., ResNet-based architectures sometimes used for speaker recognition tasks). But since data there is not actually an image, it may have different number of channels. So currently one is forced to copy and paste code from torchvision simply for the sake of adjusting number of channels in the first layer.

Pitch

Add new argument, input_channels with default value 3 to the constructors of classification models, and use it in the specification of the first convolutional layer. Optionally, add checks to the factory functions to ensure that pretrained=True only passed together with the default number of channels.

Ingwar avatar Apr 28 '20 23:04 Ingwar

Copy-pasting models in torchvision is the most flexible way, and I wouldn't say that it is not recommended, as this allows us to keep the implementation simple. This way users have full flexibility on how to modify the implementation for their own needs (which is generally needed for research), and the least amount of indirections / complexity the better. Another example is that disallowing loading pre-trained models with different than 3 input channels might not be what some users expect. E.g., users of RGB-D data might want to initialize the model from a pre-trained RGB model, and there is no way for us to handle all those possible cases.

For completeness, it is generally possible to change the number of input channels fairly easily with the current models, check for example https://github.com/pytorch/vision/issues/741#issuecomment-463187907, which allows you to re-use the weights for a pre-trained model. Here is one way of doing it is as follows:

model = models.resnet18()
model.conv1 = nn.Conv2d(4, 64, 7, stride=2, padding=3, bias=False)

As such, I'm closing the issue but let me now if you have further questions or disagree with the reasoning.

fmassa avatar Apr 30 '20 13:04 fmassa

Yes, at the end of the day, copy-pasting is the most flexible way, but it also the most error-prone (and inelegant) one, so I would prefer to resort to it only when significant change is required. I understand that this is my personal view and can not speak for the community here, but I think that we have something to gain and nothing to lose from implementing this change. It is relatively non-invasive, transparent for the existing users, and a hundred percent backward-compatible. The main cost here as far as I understand is an effort required for initial implementation and I'm willing to care of that part.

Another example is that disallowing loading pre-trained models with different than 3 input channels might not be what some users expect. E.g., users of RGB-D data might want to initialize the model from a pre-trained RGB model, and there is no way for us to handle all those possible cases.

Good point, thanks. What about providing a warning then? I won't take away any flexibility from users who know what they are doing and may prevent errors in a case when it was done by mistake.

Again, I'm willing to create PR with changes, if that may affect your opinion)

Ingwar avatar May 11 '20 14:05 Ingwar

Hi,

Thanks for proposing to work on this.

I think the issue is that, even though is is a reasonable thing to ask, there are many other options that are also reasonable but that do not work in combination. This means that we would start having to handle a number of corner cases due to conflicting options.

The pre-trained option is one of those -- there has been requests of users to let us load a pre-trained model with a different number of classes (say resnet18(pretrained=True, num_classes=2)) but replacing the classifier with a randomly-initialized one. This per se is also another reasonable request, but IMO if we combined both approaches it would become quite complicated to try to handle all possible use-cases (and it would still be kind of flaky).

As such, I think it's preferable to force to the users to understand a bit more the models that they are trying to use, as the users are those who know best what they are trying to achieve, and IMO hiding this complexity might not be beneficial in the long run.

But I might be wrong :-)

fmassa avatar May 11 '20 14:05 fmassa

So, if I got it right, it essentially boils down to the interaction of this option with pre-trained models - at least, I am not able to come up with a scenario where it would lead to the confusing or ambiguous behavior for the non-pretrained case. Feel free to correct me if I got wrong or if you have such scenario in mind :)

Let me think about it, maybe I'll be able to come up with something reasonable here :)

Ingwar avatar May 11 '20 22:05 Ingwar

It also interacts with https://github.com/pytorch/vision/issues/2200 for example, as another reasonable request, but which might add too much complexity.

fmassa avatar May 12 '20 13:05 fmassa