i am using a 3D cnn for feature extraction and svm as a classifier and I am trying to calculate the acuuracy of my training
this is the logique for feature extraction and classification
def train_epoch(model, svm_classifier, train_loader, epoch,device):
model.eval() # Use ResNet in evaluation mode for feature extraction
all_features = []
all_labels = []
for images, labels in train_loader:
images = images.to(device)
with torch.no_grad(): # Extract features without gradients
features = model(images)
all_features.append(features.cpu().numpy())
all_labels.append(labels.cpu().numpy())
# Convert features and labels to numpy arrays for SVM training
all_features = np.concatenate(all_features, axis=0)
all_labels = np.concatenate(all_labels, axis=0)
# Shuffle and train SVM with extracted features
all_features, all_labels = shuffle(all_features, all_labels)
svm_classifier.fit(all_features, all_labels)
# Predict on the training data
train_predictions = svm_classifier.predict(all_features)
# Calculate training accuracy
train_accuracy = calculate_accuracy(all_labels, train_predictions)
print(f"Epoch [{epoch}] - SVM training complete. Training accuracy: {train_accuracy * 100:.2f}%")
and for the accuracy calculation function i used this
`def calculate_accuracy(outputs, targets):
with torch.no_grad():
batch_size = targets.size(0)
_, pred = outputs.topk(1, 1, largest=True, sorted=True)
pred = pred.t()
correct = pred.eq(targets.view(1, -1))
n_correct_elems = correct.float().sum().item()
return n_correct_elems / batch_size`
then this error occured
batch_size = targets.size(0)
^^^^^^^^^^^^^^^
TypeError: ‘int’ object is not callable
is it because I changed the tensors int np arrays ?