OpenCv dnn模块扩展研究(1)--style transfer
一、opencv的示例模型文件
// 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;
}







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:
You can then install Python dependencies into a virtual environment:
# Install Python dependencies# Work for a while ...
# 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:
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
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.rockspecYou can then train a model with the script train.lua. For basic usage thecommand will look something like this:
The full set of options for this script are described here.
OpenCv dnn模块扩展研究(1)--style transfer的更多相关文章
- 如何使用 Opencv dnn 模块调用 Caffe 预训练模型?
QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt"; QString mo ...
- 手把手教你使用LabVIEW OpenCV DNN实现手写数字识别(含源码)
@ 目录 前言 一.OpenCV DNN模块 1.OpenCV DNN简介 2.LabVIEW中DNN模块函数 二.TensorFlow pb文件的生成和调用 1.TensorFlow2 Keras模 ...
- OpenCV自带dnn的Example研究(4)— openpose
这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...
- OpenCV自带dnn的Example研究(3)— object_detection
这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...
- [C4W4] Convolutional Neural Networks - Special applications: Face recognition & Neural style transfer
第四周:Special applications: Face recognition & Neural style transfer 什么是人脸识别?(What is face recogni ...
- fast neural style transfer图像风格迁移基于tensorflow实现
引自:深度学习实践:使用Tensorflow实现快速风格迁移 一.风格迁移简介 风格迁移(Style Transfer)是深度学习众多应用中非常有趣的一种,如图,我们可以使用这种方法把一张图片的风格“ ...
- (E2E_L2)GOMfcTemplate在vs2017上的运行并融合Dnn模块
GOMfcTemplate一直运行在VS2012上运行的,并且开发出来了多个产品.在技术不断发展的过程中,出现了一些新的矛盾:1.由于需要使用DNN模块,而这个模块到了4.0以上的OpenCV才支持的 ...
- 神经风格转换Neural Style Transfer a review
原文:http://mp.weixin.qq.com/s/t_jknoYuyAM9fu6CI8OdNw 作者:Yongcheng Jing 等 机器之心编译 风格迁移是近来人工智能领域内的一个热门研究 ...
- 课程四(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 ...
随机推荐
- linux系统编程面试题
说明:所有题目均摘录于网络以及我所见过的面试题目,欢迎补充! 无特殊说明情况下,下面所有题s目都是linux下的32位C程序. 1.堆和栈有什么区别? (1)栈由操作系统分配和释放:堆则是程序员手动去 ...
- 大规模异常滥用检测:基于局部敏感哈希算法——来自Uber Engineering的实践
uber全球用户每天会产生500万条行程,保证数据的准确性至关重要.如果所有的数据都得到有效利用,t通过元数据和聚合的数据可以快速检测平台上的滥用行为,如垃圾邮件.虚假账户和付款欺诈等.放大正确的数据 ...
- 结构型模式(五) 外观模式(Facade)
一.动机(Motivate) 在软件系统开发的过程中,当组件的客户(即外部接口,或客户程序)和组件中各种复杂的子系统有了过多的耦合,随着外部客户程序和各子系统的演化,这种过多的耦合面临很多变化的挑战. ...
- HTTPS——https下的静态资源中如何使用http的地址
前言 今天在改博皮的时候,将一些图片上传到七牛,但是引入的时候出问题了,因为七牛cnd设置的不是https域名,所以加载资源的时候导致自动转为https请求. 步骤 错误的写法 background: ...
- [唐胡璐]Selenium技巧 - 利用MonteScreenRecorder录制视频
我们可以用以下方式在Selenium Webdriver中capture video. 基本步骤: 从 http://www.randelshofer.ch/monte/,下载“MonteScreen ...
- 大小端示例-arm c51
大小端系列文章https://blog.csdn.net/liming0931/article/details/100016425 MDK(Keil5,STM32F407)C语言: #include ...
- 【爬虫】大杀器——phantomJS+selenium
[爬虫]大杀器——phantomJS+selenium 视频地址 江湖上有一个传说,得倚天屠龙者可称霸武林.爬虫中也有两个大杀器,他们结合在一起时,无往不利,不管你静态网站还是动态网站,通吃. pha ...
- Tensorflow细节-P194-组合训练数据
import tensorflow as tf files = tf.train.match_filenames_once("data.tfrecords-*") filename ...
- 脚本安装redis
#!/bin/bash read -p 'input the version you want(like 5.0.5):' version read -p 'input redis password: ...
- 洛谷 P1032 字串变换 题解
每日一题 day19 打卡 Analysis 广搜+map判重 用find寻找字串,再用replace替换字串 这里的map相当于正常广搜的一个book的作用 #include<iostream ...