после обучения моей модели я запускаю вывод с помощью этого скрипта
Код: Выделить всё
import torch
import json
import numpy as np
from sklearn.metrics import accuracy_score
def run_inference(opt, model, inference_loader, class_names,
svm_classifier):
model.eval()
all_predictions = []
with torch.no_grad():
for data in inference_loader:
inputs, labels = data
inputs = inputs.to(opt.device)
# CNN feature extraction
outputs = model(inputs)
# features should be numpy for SVM
features = outputs.cpu().numpy()
svm_predictions = svm_classifier.predict(features)
all_predictions.extend(svm_predictions)
# prediction should be int (not int64) for the svm clasifer
all_predictions = [int(pred) for pred in all_predictions]
# Save predictions in a jsonfile
save_path = opt.result_path / 'inference_predictions.json'
with open(save_path, 'w') as f:
json.dump({
'results': all_predictions,
'class_names': class_names
}, f)
print(f"Inference results saved to {save_path}")
Подробнее здесь: https://stackoverflow.com/questions/791 ... -in-3d-cnn