Stomach_Cancer_Pytorch/tests/run_prediction_logging_demo.py

53 lines
1.7 KiB
Python

import os
import sys
import numpy as np
import torch
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(ROOT)
from experiments.Training.Xception_Identification_Test import Xception_Identification_Block_Training_Step
from utils.Stomach_Config import Model_Config, Training_Config, Loading_Config, Save_Result_File_Config
from Training_Tools.PreProcess import Training_Precesses
def one_hot(idx, num_classes):
v = np.zeros((num_classes,), dtype=np.int64)
v[idx] = 1
return v
def main():
images = []
for name in ["A051.jpg", "C1706.jpg", "a140.jpg", "a282.jpg"]:
p = os.path.join(ROOT, name)
if os.path.exists(p):
images.append(p)
if not images:
print("no images found")
return
num_classes = len(Loading_Config["Training_Labels"])
labels = [one_hot(i % num_classes, num_classes) for i in range(len(images))]
pre = Training_Precesses(Training_Config["Image_Size"])
dataset = pre.Setting_DataSet(images, labels, None, "Transform")
dataloader = pre.Dataloader_Sampler(dataset, 1, False)
step = Xception_Identification_Block_Training_Step(
Heads=Model_Config["Transformer_Heads"],
Deepths=Model_Config["Transformer_Depths"],
Experiment_Name=Training_Config["Three_Classes_Experiment_Name"],
Best_Model_Save_Root=Save_Result_File_Config["Three_Classes_Identification_Best_Model"],
)
model = step.Construct_Identification_Model_CUDA()
with torch.no_grad():
pass
step.Evaluate_Model(model, dataloader, index=0, identification_model_path="best_model( 2025-12-05 )-fold4.pt")
print("prediction logs saved")
if __name__ == "__main__":
main()