一、opencv的示例模型文件

 
使用Torch模型【OpenCV对各种模型兼容并包,起到胶水作用】,
下载地址:
fast_neural_style_eccv16_starry_night.t7
fast_neural_style_instance_norm_feathers.t7
http://cs.stanford.edu/people/jcjohns/fast-neural-style/models/instance_norm/feathers.t7

二、示例代码
 
代码流程均较简单:图像转Blob,forward,处理输出结果,显示。【可以说是OpenCV Dnn使用方面的经典入门,对于我们对流程配置、参数理解都有很好帮助】
 
c++代码如下:
 
// This script is used to run style transfer models from '
// https://github.com/jcjohnson/fast-neural-style using OpenCV
 
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
 
using namespace cv;
using namespace cv::dnn;
using namespace std;
 
 
int main(int argc, char **argv)
{
    string modelBin = "../../data/testdata/dnn/fast_neural_style_instance_norm_feathers.t7";
    string imageFile = "../../data/image/chicago.jpg";
 
    float scale = 1.0;
    cv::Scalar mean { 103.939, 116.779, 123.68 };
    bool swapRB = false;
    bool crop = false;
    bool useOpenCL = false;
 
    Mat img = imread(imageFile);
    if (img.empty()) {
        cout << "Can't read image from file: " << imageFile << endl;
        return 2;
    }
 
    // Load model
    Net net = dnn::readNetFromTorch(modelBin);
    if (useOpenCL)
        net.setPreferableTarget(DNN_TARGET_OPENCL);
 
    // Create a 4D blob from a frame.
    Mat inputBlob = blobFromImage(img,scale, img.size(),mean,swapRB,crop);
 
    // forward netword
    net.setInput(inputBlob);
    Mat output = net.forward();
 
    // process output
    Mat(output.size[2], output.size[3], CV_32F, output.ptr<float>(0, 0)) += 103.939;
    Mat(output.size[2], output.size[3], CV_32F, output.ptr<float>(0, 1)) += 116.779;
    Mat(output.size[2], output.size[3], CV_32F, output.ptr<float>(0, 2)) += 123.68;
 
    std::vector<cv::Mat> ress;
    imagesFromBlob(output, ress);
 
    // show res
    Mat res;
    ress[0].convertTo(res, CV_8UC3);
    imshow("reslut", res);
 
    imshow("origin", img);
 
    waitKey();
    return 0;
}

 
三、演示
fast_neural_style_instance_norm_feathers.t7的演示效果

fast_neural_style_eccv16_starry_night.t7的演示效果:
 

我认为对简笔画的效果不是太好
通过重新作用于原始图片,我认识到这个模型采用的很可能是局部图片

那么这些模型如何训练出来?这里也给出了很多帮助:

Training new models

To train new style transfer models, first use the scriptscripts/make_style_dataset.py to create an HDF5 file from folders of images.You will then use the script train.lua to actually train models.

Step 1: Prepare a dataset

You first need to install the header files for Python 2.7 and HDF5. On Ubuntuyou should be able to do the following:

sudo apt-get -y install python2.7-dev
sudo apt-get install libhdf5-dev

You can then install Python dependencies into a virtual environment:

virtualenv .env                  # Create the virtual environmentsource .env/bin/activate         # Activate the virtual environment
 
pip install -r requirements.txt 

# Install Python dependencies# Work for a while ...

 
deactivate                      

# Exit the virtual environment

With the virtual environment activated, you can use the scriptscripts/make_style_dataset.py to create an HDF5 file from a directory oftraining images and a directory of validation images:

python scripts/make_style_dataset.py \
  --train_dir path/to/training/images \
  --val_dir path/to/validation/images \
  --output_file path/to/output/file.h5

All models in thisrepository were trained using the images from theCOCO dataset.

The preprocessing script has the following flags:

  • --train_dir: Path to a directory of training images.
  • --val_dir: Path to a directory of validation images.
  • --output_file: HDF5 file where output will be written.
  • --height, --width: All images will be resized to this size.
  • --max_images: The maximum number of images to use for trainingand validation; -1 means use all images in the directories.
  • --num_workers: The number of threads to use.

Step 2: Train a model

After creating an HDF5 dataset file, you can use the script train.lua totrain feedforward style transfer models. First you need to download aTorch version of theVGG-16 modelby running the script

bash models/download_vgg16.sh

This will download the file vgg16.t7 (528 MB) to the models directory.

You will also need to installdeepmind/torch-hdf5which gives HDF5 bindings for Torch:

luarocks install https://raw.githubusercontent.com/deepmind/torch-hdf5/master/hdf5-0-0.rockspec

You can then train a model with the script train.lua. For basic usage thecommand will look something like this:

th train.lua \
  -h5_file path/to/dataset.h5 \
  -style_image path/to/style/image.jpg \
  -style_image_size 384 \
  -content_weights 1.0 \
  -style_weights 5.0 \
  -checkpoint_name checkpoint \
  -gpu 0

The full set of options for this script are described here.


OpenCv dnn模块扩展研究(1)--style transfer的更多相关文章

  1. 如何使用 Opencv dnn 模块调用 Caffe 预训练模型?

    QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt"; QString mo ...

  2. 手把手教你使用LabVIEW OpenCV DNN实现手写数字识别(含源码)

    @ 目录 前言 一.OpenCV DNN模块 1.OpenCV DNN简介 2.LabVIEW中DNN模块函数 二.TensorFlow pb文件的生成和调用 1.TensorFlow2 Keras模 ...

  3. OpenCV自带dnn的Example研究(4)— openpose

    这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...

  4. OpenCV自带dnn的Example研究(3)— object_detection

    这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...

  5. [C4W4] Convolutional Neural Networks - Special applications: Face recognition & Neural style transfer

    第四周:Special applications: Face recognition & Neural style transfer 什么是人脸识别?(What is face recogni ...

  6. fast neural style transfer图像风格迁移基于tensorflow实现

    引自:深度学习实践:使用Tensorflow实现快速风格迁移 一.风格迁移简介 风格迁移(Style Transfer)是深度学习众多应用中非常有趣的一种,如图,我们可以使用这种方法把一张图片的风格“ ...

  7. (E2E_L2)GOMfcTemplate在vs2017上的运行并融合Dnn模块

    GOMfcTemplate一直运行在VS2012上运行的,并且开发出来了多个产品.在技术不断发展的过程中,出现了一些新的矛盾:1.由于需要使用DNN模块,而这个模块到了4.0以上的OpenCV才支持的 ...

  8. 神经风格转换Neural Style Transfer a review

    原文:http://mp.weixin.qq.com/s/t_jknoYuyAM9fu6CI8OdNw 作者:Yongcheng Jing 等 机器之心编译 风格迁移是近来人工智能领域内的一个热门研究 ...

  9. 课程四(Convolutional Neural Networks),第四 周(Special applications: Face recognition & Neural style transfer) —— 2.Programming assignments:Art generation with Neural Style Transfer

    Deep Learning & Art: Neural Style Transfer Welcome to the second assignment of this week. In thi ...

随机推荐

  1. ISCC之Re1

    IDA打开,调试选ELF,跟踪main函数 发现有一个not_the_flag函数,跟进去 这里判断了一下a1的值是否为42,大致判断引号里面的有可能是flag,直接放到Linux下运行 提交不要有任 ...

  2. egg.js 完整实例2后台管理系统

    项目地址 github.com/richard1015… 技术栈 Vue.js.iview.websocket.Amap 演示地址: 后台管理 schoolmgr.zhuzhida.vip 前台展示 ...

  3. 【独家】K8S漏洞报告 | CVE-2019-1002101解读

    kubectl cp漏洞CVE-2019-1002101分析 Kube-proxy IPVS添加flag ipvs-strict-arp 近期bug fix数据分析 ——本期更新内容 kubectl ...

  4. 文本编辑器Vim/Neovim任意代码执行漏洞(CVE-2019-12735)

    受影响版本: Vim < 8.1.1365, Neovim < 0.3.6 前提:开启modeline 0x01 开启modeline 在你的home下的.vimrc文件中增加一行: se ...

  5. sshd安全加固

    常见的防护措施: ——用户限制,黑白名单 ——更改验证方式(密码——>密钥对) ——防火墙..... 修改 sshd 配置文件 /etc/ssh/sshd_config -port 3389 # ...

  6. oracle删除重复数据,只保留一条

    比如,某个表要按照id和name重复,就算重复数据 delete from 表名 where rowid not in (select min(rowid) from 表名 group by id,n ...

  7. java spring boot 导出/下载文本文件操作(包含写文本文件)

    内容简介 本文主要内容为使用java把内容写入文本文件,并实现下载/导出的功能. 实现步骤 1. controller层 @ResponseBody @RequestMapping(value = & ...

  8. 解决 spring boot 线程中使用@Autowired注入Bean的方法,报java.lang.NullPointerException异常

    问题描述 在开发中,因某些业务逻辑执行时间太长,我们常使用线程来实现.常规服务实现类中,使用 @Autowired 来注入Bean,来调用其中的方法.但如果在线程类中使用@Autowired注入的Be ...

  9. K Edit Distance

    Description Given a set of strings which just has lower case letters and a target string, output all ...

  10. YAML_14 tags给指定的任务定义一个调用标识,以后不用重复整个过程,只需要执行tags标签的部分

    ansible]# vim adhttp.yml --- - hosts: cache   remote_user: root   tasks:     - copy:         src: /r ...