How to check accuracy of the trained model on new dataset with less classes than original?

I have trained a model on 200 classes, and now I want to check its accuracy on a new dataset before performing transfer learning. New dataset has 29 classes only. So how can I disable all neurons other than 29 to perform accuracy calculation for the new dataset. I am trying something like code below but it will not perform classification on all the images.

spath = 'New Data Set Path' 
tfms = get_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.1, max_warp=0.)

data = ImageDataBunch.from_folder(path=spath, train='train', valid='val', ds_tfms=tfms, 
            test=None, valid_pct=None, no_check=True, size=(224,224), num_workers=6, bs=8)
data = data.normalize(imagenet_stats)

model = mymodel(class_num=200)
torch.cuda.set_device(1)

opt = optim.Adam

learn = Learner(data, model, metrics=[error_rate, accuracy, top_k_accuracy], callback_fns=[CSVLogger], opt_func=opt)

learn.load("My saved model for 200 classes")
learn = Learner(data, model, metrics=[error_rate, accuracy, top_k_accuracy], callback_fns=[CSVLogger], opt_func=opt)

####My training images have 494 images with 29 classes
interp = ClassificationInterpretation.from_learner(learn, ds_type=DatasetType.Train)

#Get results
print(np.sum(interp.confusion_matrix()))

But I only get 86 classification results.

I am new user of fastai so I am not sure that how to do this? I think there is some extra step required before performing classification on 29 classes. I don’t know about this step which I may be missing.

1 Like