()
Create the models and start the evaluation process.
Create the models and start the evaluation process.
Create the models and start the evaluation process .
def val():
    """Create the models and start the evaluation process."""
    args = get_arguments()

    # gpu0 = args.gpu
    # os.environ["CUDA_VISIBLE_DE VICES"] = args.gpu
    h, w = args.input_size, args.input_size
    if args.whole:
        input_size = (1024, 2048)
    else:
        input_size = (h, w)
    import libs.models as models
    model = models.__dict__[args.arch](num_classes=args.num_classes)
    saved_state_dict = torch.load(args.restore_from)
    model.load_state_dict(saved_state_dict,strict=False)

    model.eval()
    model.cuda()
    if args.rgb == 1:
        IMG_MEAN = np.array((0.485, 0.456, 0.406), dtype=np.float32)
        IMG_VARS = np.array((0.229, 0.224, 0.225), dtype=np.float32)
    else:
        IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype=np.float32)
        IMG_VARS = np.array((1, 1, 1), dtype=np.float32)

    dataset = Cityscapes(args.data_dir, args.data_list, crop_size=(1024, 2048), mean=IMG_MEAN, vars=IMG_VARS,
                        scale=False, mirror=False, RGB=args.rgb)
    testloader = data.DataLoader(dataset, batch_size=1, shuffle=False, pin_memory=True)

    confusion_matrix = np.zeros((args.num_classes, args.num_classes))
    palette = get_palette(256)
    interp = nn.Upsample(size=(1024, 2048), mode='bilinear', align_corners=True)

    output_images = os.path.join(args.output_dir, "./images")
    output_results = os.path.join(args.output_dir, "./result")
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)
    if not os.path.exists(output_images):
        os.makedirs(output_images)
    if not os.path.exists(output_results):
        os.makedirs(output_results)

    for index, batch in enumerate(testloader):
        if index % 100 == 0:
            print('%d processd' % (index))
        image, label = batch
        with torch.no_grad():
            if args.whole:
                output = predict_multiscale(model, image, input_size, [1.0], args.num_classes, False)
            else:
                output = predict_sliding(model, image.numpy(), input_size, args.num_classes, True)

        seg_pred = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
        output_im = PILImage.fromarray(seg_pred)
        output_im.putpalette(palette)

        seg_gt = np.asarray(label[0].numpy(), dtype=np.int)

        ignore_index = seg_gt != 255
        seg_gt = seg_gt[ignore_index]
        seg_pred = seg_pred[ignore_index]
        confusion_matrix += get_confusion_matrix(seg_gt, seg_pred, args.num_classes)

    pos = confusion_matrix.sum(1)
    res = confusion_matrix.sum(0)
    tp = np.diag(confusion_matrix)

    IU_array = (tp / np.maximum(1.0, pos + res - tp))
    mean_IU = IU_array.mean()

    print({'meanIU': mean_IU, 'IU_array': IU_array})
    with open(os.path.join(args.output_dir, "result", 'result.txt'), 'w') as f:
        f.write(json.dumps({'meanIU': mean_IU, 'IU_array': IU_array.tolist()}))
def val ( ) : args = get_arguments ( ) # gpu0 = args.gpu # os.environ["CUDA_VISIBLE_DE VICES"] = args.gpu h , w = args . input_size , args . input_size if args . whole : input_size = ( 1024 , 2048 ) else : input_size = ( h , w ) import libs . models as models model = models . __dict__ [ args . arch ] ( num_classes = args . num_classes ) saved_state_dict = torch . load ( args . restore_from ) model . load_state_dict ( saved_state_dict , strict = False ) model . eval ( ) model . cuda ( ) if args . rgb == 1 : IMG_MEAN = np . array ( ( 0.485 , 0.456 , 0.406 ) , dtype = np . float32 ) IMG_VARS = np . array ( ( 0.229 , 0.224 , 0.225 ) , dtype = np . float32 ) else : IMG_MEAN = np . array ( ( 104.00698793 , 116.66876762 , 122.67891434 ) , dtype = np . float32 ) IMG_VARS = np . array ( ( 1 , 1 , 1 ) , dtype = np . float32 ) dataset = Cityscapes ( args . data_dir , args . data_list , crop_size = ( 1024 , 2048 ) , mean = IMG_MEAN , vars = IMG_VARS , scale = False , mirror = False , RGB = args . rgb ) testloader = data . DataLoader ( dataset , batch_size = 1 , shuffle = False , pin_memory = True ) confusion_matrix = np . zeros ( ( args . num_classes , args . num_classes ) ) palette = get_palette ( 256 ) interp = nn . Upsample ( size = ( 1024 , 2048 ) , mode = 'bilinear' , align_corners = True ) output_images = os . path . join ( args . output_dir , "./images" ) output_results = os . path . join ( args . output_dir , "./result" ) if not os . path . exists ( args . output_dir ) : os . makedirs ( args . output_dir ) if not os . path . exists ( output_images ) : os . makedirs ( output_images ) if not os . path . exists ( output_results ) : os . makedirs ( output_results ) for index , batch in enumerate ( testloader ) : if index % 100 == 0 : print ( '%d processd' % ( index ) ) image , label = batch with torch . no_grad ( ) : if args . whole : output = predict_multiscale ( model , image , input_size , [ 1.0 ] , args . num_classes , False ) else : output = predict_sliding ( model , image . numpy ( ) , input_size , args . num_classes , True ) seg_pred = np . asarray ( np . argmax ( output , axis = 2 ) , dtype = np . uint8 ) output_im = PILImage . fromarray ( seg_pred ) output_im . putpalette ( palette ) seg_gt = np . asarray ( label [ 0 ] . numpy ( ) , dtype = np . int ) ignore_index = seg_gt != 255 seg_gt = seg_gt [ ignore_index ] seg_pred = seg_pred [ ignore_index ] confusion_matrix += get_confusion_matrix ( seg_gt , seg_pred , args . num_classes ) pos = confusion_matrix . sum ( 1 ) res = confusion_matrix . sum ( 0 ) tp = np . diag ( confusion_matrix ) IU_array = ( tp / np . maximum ( 1.0 , pos + res - tp ) ) mean_IU = IU_array . mean ( ) print ( { 'meanIU' : mean_IU , 'IU_array' : IU_array } ) with open ( os . path . join ( args . output_dir , "result" , 'result.txt' ) , 'w' ) as f : f . write ( json . dumps ( { 'meanIU' : mean_IU , 'IU_array' : IU_array . tolist ( ) } ) )