class PhotoStyle(): def __init__(self, model_path=MODEL_PATH, use_cuda=True): self.p_wct = PhotoWCT() self.p_wct.load_state_dict(torch.load(model_path)) self.use_cuda = use_cuda if use_cuda: self.p_wct.cuda(0) def stylize(self, content_image_path, style_image_path, output_image_path, content_seg_path=None, style_seg_path=None, smooth=True, verbose=False): process_stylization.stylization( p_wct=self.p_wct, content_image_path=content_image_path, style_image_path=style_image_path, content_seg_path=content_seg_path, style_seg_path=style_seg_path, output_image_path=output_image_path, cuda=self.use_cuda, smooth=smooth, verbose=verbose, )
def load_model(model='./PhotoWCTModels/photo_wct.pth', fast=True, cuda=1): """load model, fast=lighter version""" # Load model p_wct = PhotoWCT() p_wct.load_state_dict(torch.load(model)) if fast: from photo_gif import GIFSmoothing p_pro = GIFSmoothing(r=35, eps=0.001) else: from photo_smooth import Propagator p_pro = Propagator() if cuda: p_wct.cuda(0) return p_wct, p_pro
def setup(opts): p_wct = PhotoWCT() p_wct.load_state_dict(torch.load(PRETRAINED_MODEL_PATH)) if opts['propagation_mode'] == 'fast': from photo_gif import GIFSmoothing p_pro = GIFSmoothing(r=35, eps=0.001) else: from photo_smooth import Propagator p_pro = Propagator() if torch.cuda.is_available(): p_wct.cuda(0) return { 'p_wct': p_wct, 'p_pro': p_pro, }
class StyleTransfer_Engine: def __init__(self): # Load model self.p_wct = PhotoWCT() self.p_wct.load_state_dict( torch.load('./PhotoWCTModels/photo_wct.pth')) self.p_wct.cuda(0) self.p_pro = Propagator() def run(self, content, style): out = stylization(stylization_module=self.p_wct, smoothing_module=self.p_pro, cont_img=content, styl_img=style, cuda=1, save_intermediate=False, no_post=False) return out
def main(): parser = argparse.ArgumentParser( description='Photorealistic Image Stylization') parser.add_argument( '--model', default='./PhotoWCTModels/photo_wct.pth', help= 'Path to the PhotoWCT model. These are provided by the PhotoWCT submodule, please use `git submodule update --init --recursive` to pull.' ) parser.add_argument('--content_image_path', default='./images/content1.png') parser.add_argument('--content_seg_path', default=[]) parser.add_argument('--style_image_path', default='./images/style1.png') parser.add_argument('--style_seg_path', default=[]) parser.add_argument('--output_image_path', default='./results/example1.png') parser.add_argument('--cuda', type=int, default=1, help='Enable CUDA.') args = parser.parse_args() # Load model p_wct = PhotoWCT() try: p_wct.load_state_dict(torch.load(args.model)) except: print("Fail to load PhotoWCT models. PhotoWCT submodule not updated?") exit() if args.cuda: p_wct.cuda(0) process_stylization.stylization( p_wct=p_wct, content_image_path=args.content_image_path, style_image_path=args.style_image_path, content_seg_path=args.content_seg_path, style_seg_path=args.style_seg_path, output_image_path=args.output_image_path, cuda=args.cuda, )
folder = 'examples' cont_img_folder = os.path.join(folder, 'content_img') cont_seg_folder = os.path.join(folder, 'content_seg') styl_img_folder = os.path.join(folder, 'style_img') styl_seg_folder = os.path.join(folder, 'style_seg') outp_img_folder = os.path.join(folder, 'results') cont_img_list = [ f for f in os.listdir(cont_img_folder) if os.path.isfile(os.path.join(cont_img_folder, f)) ] cont_img_list.sort() # Load model p_wct = PhotoWCT() p_wct.load_state_dict(torch.load(args.model)) p_wct.cuda(0) for f in cont_img_list: print("Process " + f) content_image_path = os.path.join(cont_img_folder, f) content_seg_path = os.path.join(cont_seg_folder, f).replace(".png", ".pgm") style_image_path = os.path.join(styl_img_folder, f) style_seg_path = os.path.join(styl_seg_folder, f).replace(".png", ".pgm") output_image_path = os.path.join(outp_img_folder, f) process_stylization.stylization( p_wct=p_wct, content_image_path=content_image_path, style_image_path=style_image_path, content_seg_path=content_seg_path,
parser.add_argument('--fast', action='store_true', default=False) parser.add_argument('--no_post', action='store_true', default=False) parser.add_argument('--cuda', type=int, default=1, help='Enable CUDA.') parser.add_argument('--device', type=int, default=0, help='CUDA device.') args = parser.parse_args() # Load model p_wct = PhotoWCT(device=args.device) p_wct.load_state_dict(torch.load(args.model)) if args.fast: from photo_gif import GIFSmoothing p_pro = GIFSmoothing(r=35, eps=0.001) else: from photo_smooth import Propagator p_pro = Propagator() if args.cuda: p_wct.cuda(args.device) process_stylization.stylization(stylization_module=p_wct, smoothing_module=p_pro, content_image_path=args.content_image_path, style_image_path=args.style_image_path, content_seg_path=args.content_seg_path, style_seg_path=args.style_seg_path, output_image_path=args.output_image_path, cuda=args.cuda, save_intermediate=args.save_intermediate, no_post=args.no_post, device=args.device)
parser.add_argument('--cuda', type=int, default=1, help='Enable CUDA.') parser.add_argument('--mode', type=str, default="") args = parser.parse_args() # Load model p_wct = PhotoWCT(args) if args.mode == "original" or args.mode == "": p_wct.load_state_dict(torch.load(args.model)) if args.fast: from photo_gif import GIFSmoothing p_pro = GIFSmoothing(r=35, eps=0.001) else: from photo_smooth import Propagator p_pro = Propagator() if args.cuda: p_wct.cuda(args.cuda) process_stylization.stylization( stylization_module=p_wct, smoothing_module=p_pro, content_image_path=args.content_image_path, style_image_path=args.style_image_path, content_seg_path=args.content_seg_path, style_seg_path=args.style_seg_path, output_image_path=args.output_image_path, cuda=args.cuda, save_intermediate=args.save_intermediate, no_post=args.no_post )
parser.add_argument('--model', default='./PhotoWCTModels/photo_wct.pth', help='Path to the PhotoWCT model. These are provided by the PhotoWCT submodule, please use `git submodule update --init --recursive` to pull.') parser.add_argument('--content_image_path', default='./images/content1.png') parser.add_argument('--content_seg_path', default=[]) parser.add_argument('--style_image_path', default='./images/style1.png') parser.add_argument('--style_seg_path', default=[]) parser.add_argument('--output_image_path', default='./results/example1.png') parser.add_argument('--cuda', type=int, default=1, help='Enable CUDA.') args = parser.parse_args() # Load model p_wct = PhotoWCT() try: p_wct.load_state_dict(torch.load(args.model)) except: print("Fail to load PhotoWCT models. PhotoWCT submodule not updated?") exit() if args.cuda: p_wct.cuda(0) process_stylization.stylization( p_wct=p_wct, content_image_path=args.content_image_path, style_image_path=args.style_image_path, content_seg_path=args.content_seg_path, style_seg_path=args.style_seg_path, output_image_path=args.output_image_path, cuda=args.cuda, )