def main(): if args.style_option == 0: best_image_bgr = stylize(args, False) result = Image.fromarray(np.uint8(np.clip(best_image_bgr[:, :, ::-1], 0, 255.0))) result.save(args.output_image) elif args.style_option == 1: best_image_bgr = stylize(args, True) if not args.apply_smooth: result = Image.fromarray(np.uint8(np.clip(best_image_bgr[:, :, ::-1], 0, 255.0))) result.save(args.output_image) else: # Pycuda runtime incompatible with Tensorflow from smooth_local_affine import smooth_local_affine content_input = np.array(Image.open(args.content_image_path).convert("RGB"), dtype=np.float32) # RGB to BGR content_input = content_input[:, :, ::-1] # H * W * C to C * H * W content_input = content_input.transpose((2, 0, 1)) input_ = np.ascontiguousarray(content_input, dtype=np.float32) / 255. _, H, W = np.shape(input_) output_ = np.ascontiguousarray(best_image_bgr.transpose((2, 0, 1)), dtype=np.float32) / 255. best_ = smooth_local_affine(output_, input_, 1e-7, 3, H, W, args.f_radius, args.f_edge).transpose(1, 2, 0) result = Image.fromarray(np.uint8(np.clip(best_ * 255., 0, 255.))) result.save(args.output_image) elif args.style_option == 2: args.max_iter = 2 * args.max_iter tmp_image_bgr = stylize(args, False) result = Image.fromarray(np.uint8(np.clip(tmp_image_bgr[:, :, ::-1], 0, 255.0))) args.init_image_path = os.path.join(args.serial, "tmp_result.png") result.save(args.init_image_path) best_image_bgr = stylize(args, True) if not args.apply_smooth: result = Image.fromarray(np.uint8(np.clip(best_image_bgr[:, :, ::-1], 0, 255.0))) result.save(args.output_image) else: from smooth_local_affine import smooth_local_affine content_input = np.array(Image.open(args.content_image_path).convert("RGB"), dtype=np.float32) # RGB to BGR content_input = content_input[:, :, ::-1] # H * W * C to C * H * W content_input = content_input.transpose((2, 0, 1)) input_ = np.ascontiguousarray(content_input, dtype=np.float32) / 255. _, H, W = np.shape(input_) output_ = np.ascontiguousarray(best_image_bgr.transpose((2, 0, 1)), dtype=np.float32) / 255. best_ = smooth_local_affine(output_, input_, 1e-7, 3, H, W, args.f_radius, args.f_edge).transpose(1, 2, 0) result = Image.fromarray(np.uint8(np.clip(best_ * 255., 0, 255.))) result.save(args.output_image)
def main(): if args.style_option == 0: best_image_bgr = stylize(args, False) result = Image.fromarray(np.uint8(np.clip(best_image_bgr[:, :, ::-1], 0, 255.0))) result.save(args.output_image) elif args.style_option == 1: best_image_bgr = stylize(args, True) if not args.apply_smooth: result = Image.fromarray(np.uint8(np.clip(best_image_bgr[:, :, ::-1], 0, 255.0))) result.save(args.output_image) else: # Pycuda runtime incompatible with Tensorflow from smooth_local_affine import smooth_local_affine content_input = np.array(Image.open(args.content_image_path).convert("RGB"), dtype=np.float32) # RGB to BGR content_input = content_input[:, :, ::-1] # H * W * C to C * H * W content_input = content_input.transpose((2, 0, 1)) input_ = np.ascontiguousarray(content_input, dtype=np.float32) / 255. _, H, W = np.shape(input_) output_ = np.ascontiguousarray(best_image_bgr.transpose((2, 0, 1)), dtype=np.float32) / 255. best_ = smooth_local_affine(output_, input_, 1e-7, 3, H, W, args.f_radius, args.f_edge).transpose(1, 2, 0) result = Image.fromarray(np.uint8(np.clip(best_ * 255., 0, 255.))) result.save(args.output_image) elif args.style_option == 2: args.max_iter = 2 * args.max_iter tmp_image_bgr = stylize(args, False) result = Image.fromarray(np.uint8(np.clip(tmp_image_bgr[:, :, ::-1], 0, 255.0))) args.init_image_path = os.path.join(args.serial, "tmp_result.png") result.save(args.init_image_path) best_image_bgr = stylize(args, True) if not args.apply_smooth: result = Image.fromarray(np.uint8(np.clip(best_image_bgr[:, :, ::-1], 0, 255.0))) result.save(args.output_image) else: from smooth_local_affine import smooth_local_affine content_input = np.array(Image.open(args.content_image_path).convert("RGB"), dtype=np.float32) # RGB to BGR content_input = content_input[:, :, ::-1] # H * W * C to C * H * W content_input = content_input.transpose((2, 0, 1)) input_ = np.ascontiguousarray(content_input, dtype=np.float32) / 255. _, H, W = np.shape(input_) output_ = np.ascontiguousarray(best_image_bgr.transpose((2, 0, 1)), dtype=np.float32) / 255. best_ = smooth_local_affine(output_, input_, 1e-7, 3, H, W, args.f_radius, args.f_edge).transpose(1, 2, 0) result = Image.fromarray(np.uint8(np.clip(best_ * 255., 0, 255.))) result.save(args.output_image)
def run_deep_photostyle(self): """ 画風変換を実施する :return: """ style_const = StyleConst() # Neural Art WebAPIに関する情報も渡す style_const.material_detail = self.material_detail # Input Options # 素材フォルダはあらかじめ決めている style_const.content_image_path = "tmp/content_image.jpg" style_const.style_image_path = "tmp/style_image.jpg" style_const.content_seg_path = "tmp/content_seg.jpg" style_const.style_seg_path = "tmp/style_seg.jpg" # output Options style_const.init_image_path = "" style_const.output_image = "outputs/best_stylized/best_stylized.png" style_const.serial = "outputs" # Training Optimizer Options #style_const.max_iter = 1000 style_const.max_iter = int(self.parameters["max_iter"]) style_const.learning_rate = 1.0 style_const.print_iter = 1 style_const.save_iter = 100 style_const.lbfgs = True # Weight Options #style_const.content_weight = 5e0 style_const.content_weight = float(self.parameters["content_weight"]) #style_const.style_weight = 1e2 style_const.style_weight = float(self.parameters["style_weight"]) style_const.tv_weight = 1e-3 style_const.affine_weight = 1e4 # style Options style_const.style_option = 0 style_const.apply_smooth = True # Smoothing Argment style_const.f_radius = 15 style_const.f_edge = 1e-1 beset_image_bgr = stylize(style_const, False) result = Image.fromarray( np.uint8(np.clip(beset_image_bgr[:, :, ::-1], 0, 255.0))) result.save(style_const.output_image)
def main(): style_const = StyleConst() # Input Options #style_const.content_image_path = "inputs/content_image/neuralart_orange_logo.jpg" #style_const.style_image_path = "inputs/style_image/hokusai.jpg" #style_const.content_seg_path = "inputs/content_seg/neuralart_orange_logo.jpg" #style_const.style_seg_path = "inputs/style_seg/hokusai_seg.jpg" style_const.content_image_path = "tmp/content_image.jpg" style_const.style_image_path = "tmp/style_image.jpg" style_const.content_seg_path = "tmp/content_seg.jpg" style_const.style_seg_path = "tmp/style_seg.jpg" # output Options style_const.init_image_path = "" style_const.output_image = "outputs/best_stylized/best_stylized.png" style_const.serial = "outputs" # Training Optimizer Options style_const.max_iter = 1000 style_const.learning_rate = 1.0 style_const.print_iter = 1 style_const.save_iter = 100 style_const.lbfgs = True # Weight Options style_const.content_weight = 5e0 style_const.style_weight = 1e2 style_const.tv_weight = 1e-3 style_const.affine_weight = 1e4 # style Options style_const.style_option = 0 style_const.apply_smooth = True # Smoothing Argment style_const.f_radius = 15 style_const.f_edge = 1e-1 beset_image_bgr = stylize(style_const, False) result = Image.fromarray( np.uint8(np.clip(beset_image_bgr[:, :, ::-1], 0, 255.0))) result.save(style_const.output_image)