How to change the number of classes using new data

I trained a resnet34 model with dataset A first, and that dataset has 50 classes. I then would like to use the same weights to train on dataset B, predicting only two classes as that’s what’s in dataset B. In short, I’d like to do my own transfer learning based on training 50 classes on dataset A.

However, I can’t figure out how to both use the pre-trained dataset A weights and change the architecture so that it only produces two classes. I have difficulty finding out, from within the fastai library, which line actually loads the weights (and indeed whether the weights should include weights of all layers or without the last fc layer, which depends on how many classes the data has); I also have difficulty figuring out how to change the number of classes from 50 to 2. The reason is that every time I initiate a learner, the learning takes in the data information and decides the number of classes before constructing the architecture. So if I initialize with dataset A, it would be a 50 class learner; and if I initialize with dataset B, it would be a 2 classer learner alright, but with no pre-trained weights on dataset A. Anyone know how to do this? Thanks!

I’m facing the same problem.
Can you suggest any solution \ directions to solution?

See these posts: