print('+' * 80) # Fire the engines # Fire the engines if args.clf == 'fcn': print('Initializing FCN...') model = FCN(args.input_size, args.output_size) elif args.clf == 'svm': print('Initializing SVM...') model = SVM(args.input_size, args.output_size) elif args.clf == 'resnet18': print('Initializing ResNet18...') model = resnet.resnet18(num_channels=args.num_channels, num_classes=args.output_size) model.load_state_dict(torch.load(init_path)) print('Load init: {}'.format(init_path)) model = nn.DataParallel(model.to(device), device_ids=args.device_id) if 'sgd' in args.paradigm: optim = optim.SGD(params=model.parameters(), lr=args.lr) elif 'adam' in args.paradigm: optim = optim.Adam(params=model.parameters(), lr=args.lr) loss_fn = multiClassHingeLoss() if args.clf == 'svm' else F.nll_loss loss_type = 'hinge' if args.clf == 'svm' else 'nll' print('+' * 80) best = 0
padding0 = 32 - w % 32 padding1 = 32 - h % 32 image = cv2.copyMakeBorder(image, padding0 // 2, padding0 - padding0 // 2, padding1 // 2, padding1 - padding1 // 2, cv2.BORDER_REFLECT) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) image = transform(image).unsqueeze(0) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(device) state_dict = torch.load(path, map_location='cpu') model = FCN(21).to(device).eval() model.load_state_dict(state_dict) del state_dict with torch.no_grad(): image = image.to(device) output = model(image) predict = output.data.cpu().squeeze(0).numpy() predict = np.argmax(predict, axis=0) predict = colormap[predict] cv2.imwrite("res.png", predict)