コード例 #1
0
def net_load(model_name):

    Net = NET_FCN.Net(NumClasses=NUM_CLASSES)
    Net.load_state_dict(torch.load(liquid_segmentation_model))
    Net.eval()
    Net.half()

    return Net
コード例 #2
0
def net_load(model_name):

    Net=NET_FCN.Net(NumClasses=NUM_CLASSES, UseGPU=False)
    Net.load_state_dict(torch.load(liquid_segmentation_model, map_location='cpu'))
    Net.eval()
    # Net.half()

    return Net
#......................Input Parameters...................................................................

Image_Dir = "Data_Zoo/Materials_In_Vessels/Test_Images_All//"  #Images for evaluation
Label_Dir = "Data_Zoo/Materials_In_Vessels/FillLevelLabels"  #Ground truth  per pixel annotation for the images in Image dir
Trained_model_path = "TrainedModelWeights/FillLevelRecognitionNetWeights.torch"  # "Path to trained net weights

NUM_CLASSES = 3  #Number of classes the net predicts
#Classes = ["BackGround", "Empty Vessel","Liquid","Solid"] #names of classe the net predic
#Classes=["Background","Vessel"] #Classes predicted for vessel region prediction
Classes = ["BackGround", "Empty Vessel region", "Filled Vessel region"]  #
#Classes=["BackGround","Empty Vessel region","liquid","Solid"]
#Classes=["BackGround","Vessel","Liquid","Liquid Phase two","Suspension", "Emulsion","Foam","Solid","Gel","Powder","Granular","Bulk","Bulk Liquid","Solid Phase two","Vapor"]

# .........................Build FCN Net...............................................................................................
Net = FCN.Net(NumClasses=NUM_CLASSES)  #Build Net
Net.load_state_dict(torch.load(Trained_model_path))  # Load Traine model
print("Model weights loaded from: " + Trained_model_path)
Net.eval()
Net.half()
# -------------------------Data reader for validation image-----------------------------------------------------------------------------------------------------------------------------

ValidReader = Data_Reader.Data_Reader(
    Image_Dir, GTLabelDir=Label_Dir, BatchSize=1
)  # build reader that will be used to load images and labels from validation set

#--------------------Sum of intersection from all validation images for all classes and sum of union for all images and all classes----------------------------------------------------------------------------------
Union = np.float64(np.zeros(len(Classes)))  #Sum of union
Intersection = np.float64(np.zeros(len(Classes)))  #Sum of Intersection
fim = 0
print("Start Evaluating intersection over union for " +
MaxPixel = 34000  # Max pixel image can have (to keep oom out of memory problems) if the image larger it will be resized. Reduce if
TrainLossTxtFile = TrainedModelWeightDir + "TrainLoss.txt"  #Where train losses will be writen
ValidLossTxtFile = TrainedModelWeightDir + "ValidationLoss.txt"  # Where validation losses will be writen

Batch_Size = 1  # Number of images per training iteration (keep small to avoid out of  memory problems)
Weight_Decay = 1e-4  # Weight for the weight decay loss function
MAX_ITERATION = int(80010)  # Max  number of training iteration
NUM_CLASSES = 4  #Number of classes the model predict

UpdateEncoderBatchNormStatistics = False

#---------------------Create and Initiate net and create optimizer------------------------------------------------------------------------------------

Net = NET_FCN.Net(
    NumClasses=NUM_CLASSES,
    PreTrainedModelPath=Pretrained_Encoder_Weights,
    UpdateEncoderBatchNormStatistics=UpdateEncoderBatchNormStatistics
)  # Create net and load pretrained encoder path

if Trained_model_path != "":  # Optional initiate full net by loading a pretrained net
    Net.load_state_dict(torch.load(Trained_model_path))
#optimizer=torch.optim.SGD(params=Net.parameters(),lr=Learning_Rate,weight_decay=Weight_Decay,momentum=0.5)
optimizer = torch.optim.Adam(
    params=Net.parameters(), lr=Learning_Rate,
    weight_decay=Weight_Decay)  # Create adam optimizer

#----------------------------------------Create reader for data set--------------------------------------------------------------------------------------------------------------

TrainReader = Data_Reader.Data_Reader(
    ImageDir=Train_Image_Dir, GTLabelDir=Train_Label_Dir,
    BatchSize=Batch_Size)  #Reader for training data