예제 #1
0
파일: main.py 프로젝트: MondayYuan/FCN
def test(args, model):
    model.eval()

    input_image = Image.open(args.image).resize((256, 256))

    input_transform = Compose([
        ToTensor(),
        Normalize([.485, .456, .406], [.229, .224, .225]),
    ])

    image = torch.unsqueeze(input_transform(input_image), 0)
    if args.cuda:
        image = image.cuda()

    label = model(image)
    color_transform = Colorize()
    label = color_transform(label[0].data.max(0)[1])
    label = ToPILImage()(label)

    # label.show()
    # input_image.show()

    if args.resized_image:
        input_image.save(args.resized_image)
    label.save(args.label)
예제 #2
0
파일: main.py 프로젝트: MondayYuan/FCN
def evaluate(args, model, val_set, epoch):
    model.eval()
    val_loader = DataLoader(val_set,
                            num_workers=1,
                            batch_size=1,
                            shuffle=False)
    labels_truth = []
    labels_predict = []
    for step, (images, labels) in enumerate(val_loader):
        inputs = images
        targets = labels[:, 0]
        if args.cuda:
            inputs = inputs.cuda()
            targets = targets.cuda()
        outputs = model(inputs).max(1)[1]

        labels_truth.append(targets.cpu().numpy())
        labels_predict.append(outputs.cpu().numpy())

        if args.port > 0 and step == 0:
            image = inputs[0]
            image[0] = image[0] * .229 + .485
            image[1] = image[1] * .224 + .456
            image[2] = image[2] * .225 + .406

            board = Dashboard(args.port)

            board.image(image, f'input (epoch: {epoch})')
            board.image(Colorize()(outputs[0].cpu()),
                        f'output (epoch: {epoch})')
            board.image(Colorize()(targets[0].cpu()),
                        f'target (epoch: {epoch})')

    def label_accuracy_score(label_trues, label_preds, n_class):
        """Returns accuracy score evaluation result.
        - overall accuracy
        - mean accuracy
        - mean IU
        - fwavacc
        """
        hist = np.zeros((n_class, n_class))

        def _fast_hist(label_true, label_pred, n_class):
            mask = (label_true >= 0) & (label_true < n_class)
            hist = np.bincount(n_class * label_true[mask].astype(int) +
                               label_pred[mask],
                               minlength=n_class**2).reshape(n_class, n_class)
            return hist

        for lt, lp in zip(label_trues, label_preds):
            hist += _fast_hist(lt.flatten(), lp.flatten(), n_class)
        acc = np.diag(hist).sum() / hist.sum()
        with np.errstate(divide='ignore', invalid='ignore'):
            acc_cls = np.diag(hist) / hist.sum(axis=1)
        acc_cls = np.nanmean(acc_cls)
        with np.errstate(divide='ignore', invalid='ignore'):
            iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) -
                                  np.diag(hist))
        mean_iu = np.nanmean(iu)
        freq = hist.sum(axis=1) / hist.sum()
        fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
        return acc, acc_cls, mean_iu, fwavacc

    acc, acc_cls, mean_iu, fwavacc = label_accuracy_score(
        labels_truth, labels_predict, NUM_CLASSES)

    return acc_cls, mean_iu
예제 #3
0
파일: main.py 프로젝트: liminn/LEDNet
from torchvision.transforms import ToTensor, ToPILImage

from utils.dataset import VOC12, cityscapes
from utils.transform import Relabel, ToLabel, Colorize
from utils.visualize import Dashboard
from utils.loss import CrossEntropyLoss2d

import importlib
from utils.iouEval import iouEval, getColorEntry

from shutil import copyfile

NUM_CHANNELS = 3
NUM_CLASSES = 20  #pascal=22, cityscapes=20

color_transform = Colorize(NUM_CLASSES)
image_transform = ToPILImage()


#Augmentations - different function implemented to perform random augments on both image and target
class MyCoTransform(object):
    def __init__(self, enc, augment=True, height=512):
        self.enc = enc
        self.augment = augment
        self.height = height
        pass

    def __call__(self, input, target):
        # do something to both images
        input = Resize(self.height, Image.BILINEAR)(input)
        target = Resize(self.height, Image.NEAREST)(target)
예제 #4
0
    def __init__(self, config):
        self.config = config
        self.logger = logging.getLogger("ERFNetAgent")
        # Create an instance from the Model
        self.logger.info("Loading encoder pretrained in imagenet...")
        if self.config.pretrained_encoder:
            pretrained_enc = torch.nn.DataParallel(
                ERFNet(self.config.imagenet_nclasses)).cuda()
            pretrained_enc.load_state_dict(
                torch.load(self.config.pretrained_model_path)['state_dict'])
            pretrained_enc = next(pretrained_enc.children()).features.encoder
        else:
            pretrained_enc = None
        # define erfNet model
        self.model = ERF(self.config, pretrained_enc)
        # Create an instance from the data loader
        self.data_loader = VOCDataLoader(self.config)
        self.color_transform = Colorize(self.config.num_classes)
        self.image_transform = ToPILImage()
        # Create instance from the loss
        self.loss = CrossEntropyLoss(self.config)
        # Create instance from the optimizer
        self.optimizer = torch.optim.Adam(
            self.model.parameters(),
            lr=self.config.learning_rate,
            betas=(self.config.betas[0], self.config.betas[1]),
            eps=self.config.eps,
            weight_decay=self.config.weight_decay)
        # Define Scheduler
        lambda1 = lambda epoch: pow(
            (1 - ((epoch - 1) / self.config.max_epoch)), 0.9)
        self.scheduler = lr_scheduler.LambdaLR(self.optimizer,
                                               lr_lambda=lambda1)
        # initialize my counters
        self.current_epoch = 0
        self.current_iteration = 0
        self.best_valid_mean_iou = 0

        # Check is cuda is available or not
        self.is_cuda = torch.cuda.is_available()
        # Construct the flag and make sure that cuda is available
        self.cuda = self.is_cuda & self.config.cuda

        if self.cuda:
            torch.cuda.manual_seed_all(self.config.seed)
            self.device = torch.device("cuda")
            torch.cuda.set_device(self.config.gpu_device)
            self.logger.info("Operation will be on *****GPU-CUDA***** ")
            print_cuda_statistics()

        else:
            self.device = torch.device("cpu")
            torch.manual_seed(self.config.seed)
            self.logger.info("Operation will be on *****CPU***** ")

        self.model = self.model.to(self.device)
        self.loss = self.loss.to(self.device)
        # Model Loading from the latest checkpoint if not found start from scratch.
        self.load_checkpoint(self.config.checkpoint_file)

        # Tensorboard Writer
        self.summary_writer = SummaryWriter(log_dir=self.config.summary_dir,
                                            comment='FCN8s')