OpenCv dnn模塊擴展研究(1)--style transfer

1、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

2、示例代碼
 
代碼流程均較簡單:圖像轉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;
}
 
3、演示
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.python

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:ios

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

You can then install Python dependencies into a virtual environment:c++

virtualenv .env                  # Create the virtual environment source .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:git

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.github

The preprocessing script has the following flags:shell

  • --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 scriptbash

bash models/download_vgg16.sh

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

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

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:ui

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.


 



相關文章
相關標籤/搜索