Hi,
I added a few layers to the existing resnet34
for some trial purposes. I would like to extract the features from the output of the fc_4 layer
(128 features) during valloader after the training phase. Code snippet.
model_ft = models.resnet34(pretrained=use_pretrained)
model_ft.fc_1 = nn.Sequential( nn.Dropout(0.1), nn.Linear(1000, 512))
model_ft.fc_2 = nn.Sequential( nn.Dropout(0.1), nn.Linear(512, 512))
model_ft.fc_3 = nn.Sequential( nn.Dropout(0.1), nn.Linear(512, 256))
model_ft.fc_4 = nn.Sequential( nn.Dropout(0.1), nn.Linear(256, 128))
# model_ft.avg_pool2 = nn.AdaptiveAvgPool2d(output_size=(1, 1))
model_ft.fc_5 = nn.Linear(128, 2)
feature_extractor = feature_extractor.to(device)
print(feature_extractor)
with torch.no_grad():
model_ft .eval() # Set model to evaluate mode
feature_extractor.eval()
# Iterate over data.
for inputs, labels in dataloaders:
inputs = inputs.to(device)
labels = labels.to(device)
# feature extractor
feature_tensor = feature_extractor(inputs) # output now has the features corresponding to input x
feature_arr = feature_tensor.cpu().detach().numpy().flatten()
Any ideas to extract features from layer (fc_4
: 128 features). I need this to be used as a nth-length features of probability scores to predict later
Gives me the below error:
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1845 if has_torch_function_variadic(input, weight):
1846 return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-> 1847 return torch._C._nn.linear(input, weight, bias)
1848
1849
RuntimeError: CUDA error: CUBLAS_STATUS_INVALID_VALUE when calling `cublasSgemm( handle, opa, opb, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)`