Github项目网址:https://github.com/lengstrom/fast-style-transfer
在Anaconda Prompt中输入以下命令,创建一个名为“tf-gpu”的新python环境,并激活切换到此环境下:
conda create -n tf-gpu tensorflow-gpu=2.1.0
conda activate tf-gpu
之后github项目中说需要安装jupyterlab再安装moviepy这个包或者直接使用命令“conda install moviepy”安装最新版本moviepy包。选择后者更为简便。
之后就可以使用git命令将项目clone到本地或者直接使用下载项目zip压缩包然后到本地解压即可:
然后下载网络上别人已经训练好的模型到自己指定位置。
之后输入命令:
python evaluate.py --checkpoint path/to/style/model.ckpt \
--in-path dir/of/test/imgs/ \
--out-path dir/for/results/
比如(依次为model地址,源图片所在地址,图片预计输出地址):
python evaluate.py --checkpoint ./model/udnie.ckpt --in-path ./rawed/tang2.jpg --out-path ./finished/tang2_udnie.jpg
关于视频的转换是类似的,在转换前需要在环境下安装ffmpeg这个包。命令为:“conda install ffmpeg”。
这一步骤可能出现的bug:
File "[mycondapath]\tf-gpu\lib\site-packages\moviepy\video\io\ffmpeg_writer.py", line 136, in write_frame
self.proc.stdin.write(img_array.tobytes())
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "transform_video.py", line 55, in <module>
main()
File "transform_video.py", line 51, in main
evaluate.ffwd_video(opts.in_path, opts.out, opts.checkpoint, opts.device, opts.batch_size)
File "[myrepopath]\fast-style-transfer\evaluate.py", line 62, in ffwd_video
style_and_write(frame_count)
File "[myrepopath]\fast-style-transfer\evaluate.py", line 55, in style_and_write
video_writer.write_frame(np.clip(_preds[i], 0, 255).astype(np.uint8))
File "[mycondapath]\tf-gpu\lib\site-packages\moviepy\video\io\ffmpeg_writer.py", line 180, in write_frame
raise IOError(error)
OSError: [Errno 32] Broken pipe
MoviePy error: FFMPEG encountered the following error while writing file out/guitar/fox.mp4:
b''```
通过此方法可以解决:
其他可能出现的问题在项目的issue中可以自行浏览。