Johan
Johan

Reputation: 11

Extract hidden layer values from pytorch model

I have trained ghostnet model which has last classifier layer. It is 1280 x 4 shape in my case.

(classifier): Linear(in_features=1280, out_features=4, bias=True)

I would like to get 1280 input values for this layer. How to do this? I see that this is possible to do using hooks, but the concept looks confusing. Do you know alternative ways to do this?

I tried some examples using hooks, but I am not sure that I understand what they do. My code for model evaluation is here:

def model_valid(model, val_data):
    model.eval()
    correct = 0
    total = 0
    score = 0
    pred = np.array([])
    lbs = np.array([])
 
    
    with torch.no_grad():
        for imgs, labels in tqdm(val_data):
            imgs = imgs.to(device = 'cuda')
            labels = labels.to(device = 'cuda')
            outputs = model(imgs)
            _, predicted = torch.max(outputs, dim = 1)
            _, labels = torch.max(labels, dim = 1)

            total += labels.shape[0]
            correct += int((predicted == labels).sum())

            # print(predicted)
            pred = np.append(pred, predicted.cpu().numpy(), axis = 0)
            lbs = np.append(lbs, labels.cpu().numpy(), axis = 0)
            
            print(f'model accuracy is {correct/total}')
    return correct/total, [lbs, pred]

I would like to have 1280 values from previous layer in addition to my four outputs.

Upvotes: 1

Views: 34

Answers (1)

karam dar
karam dar

Reputation: 1

Try this implementation provided here, and simply remove the classification layer.

Upvotes: 0

Related Questions