This post is to get feedback to ascertain that the steps I have taken to extract activations are correct.
Problem Statement - To extract activations for the test images passed into a learner object trained using resnet50.
FastAI Version - 1.0.39
Here are the codes -
from fastai.metrics import fbeta, accuracy_thresh from fastai.vision.learner import create_cnn, models from fastai.vision.data import ImageItemList, imagenet_stats from fastai.widgets import DatasetFormatter from fastai.callbacks.hooks import hook_output # get_data function contains ImageItemList object data = get_data('Classifier_df.csv',root_path,train_folder='train', suffix=None,size=224,val_idxs=None,test_folder='test',bs=1) #Defining the learner object learn = create_cnn(data, models.resnet50, metrics=[acc_02,fbeta_score]) # loading the object learn.load('classifier_v1') # Indexing the last layer of CNN of resnet50 layer_ls=[0, 7, 2] hook = hook_output(learn.model[layer_ls][layer_ls][layer_ls]) test_activations = DatasetFormatter.get_actns(learn,hook=hook, dl = data.test_dl, pool_dim=8) ''' Output would look something like- test_activations = tensor([[1.4966, 1.4966, 0.6284, ..., 0.2086, 0.0875, 0.0611], [0.4432, 0.6602, 0.6602, ..., 0.0588, 0.1247, 0.1628]]) The output is 512*512(262,144) long row vector for each image. '''
I m not sure whether I m doing it correctly.
Here are my concerns -
- Are these indexes correct for resnet50 last CNN layer, layer_ls=[0, 7, 2]?
- Vector size is 262,144 for every image. I m also unsure about this one?
Any help, suggestions highly appreciated!!