[DOCKER] Try Caffe and Pylearn2 together

environment

New Ubuntu container created with Docker

Docker Hub

For those who want to try it for the time being, the Docker container that built the environment of Caffe (including python wrapper) and Pylearn2 is Docker Hub Published in /). Please refer to the information of the repository for details.

Caffe

bash


# docker pull cordea/pycaffe

In this article, it is a container that has finished up to "make".

Pylearn2

bash


# docker pull cordea/pylearn2

It is a container that has finished up to "path".

Common

Create user

If you can be root, please skip it.

bash


# username="your user name"
# adduser --disabled-password --gecos '' $username
# adduser $username sudo
# echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
# su $username

Install only the basics for the time being There are other common packages, but ...

$ sudo apt-get update
$ sudo apt-get install python vim git wget

Caffe

This time, we will operate in ** CPU mode **, so we will not install the NVIDIA Graphics Driver.

Installation

$ sudo apt-get install make bc libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libblas-dev libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler libsvm-dev libsvm3 libsvm-tools

PyCaffe Anaconda Python is recommended, but I won't use it this time because it's troublesome with Linux.

If you put it in pip as officially (probably you need to install gfortran additionally)

$ sudo apt-get install python-pip
$ cd ~/caffe/python/
$ for req in $(cat requirements.txt); do sudo pip install $req; done

If you want to manage with ʻapt-get` as much as possible

$ sudo apt-get install python-pip python-scipy python-matplotlib python-scikits-learn ipython python-h5py python-leveldb python-networkx python-nose python-pandas python-dateutil python-protobuf python-yaml python-gflags python-skimage cython

CUDA

$ cd ~
$ cd 
$ chmod u+x cuda_6.5.14_linux_64.run 
$ ./cuda_6.5.14_linux_64.run 
Do you accept the previously read EULA? (accept/decline/quit): accept
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 340.29? ((y)es/(n)o/(q)uit): n
Install the CUDA 6.5 Toolkit? ((y)es/(n)o/(q)uit): y
Enter Toolkit Location [ default is /usr/local/cuda-6.5 ]: 
/usr/local/cuda-6.5 is not writable.
Do you wish to run the installation with 'sudo'? ((y)es/(n)o): y
Do you want to install a symbolic link at /usr/local/cuda? ((y)es/(n)o/(q)uit): y
Install the CUDA 6.5 Samples? ((y)es/(n)o/(q)uit): n
Installing the CUDA Toolkit in /usr/local/cuda-6.5 ...

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-6.5
Samples:  Not Selected

$ sudo ldconfig /usr/local/cuda-6.5/lib64/
$ rm cuda_6.5.14_linux_64.run

make

$ git clone https://github.com/BVLC/caffe
$ cd caffe/
$ cp Makefile.config.example Makefile.config  
$ echo "CPU_ONLY := 1" >> Makefile.config # 1/26 Addendum
$ make all
$ make test
$ make runtest

...

[----------] Global test environment tear-down
[==========] 457 tests from 98 test cases ran. (14811 ms total)
[  PASSED  ] 457 tests.

Tutorial

$ vim examples/mnist/lenet_solver.prototxt
$ ./data/mnist/get_mnist.sh 
$ ./examples/mnist/create_mnist.sh
$ ./examples/mnist/train_lenet.sh 

PyCaffe Tutorials

Tutorial is performed according to Easy image classification with Caffe.

$ cd ~/caffe/examples/imagenet/
$ wget https://raw.githubusercontent.com/sguada/caffe-public/master/models/get_caffe_reference_imagenet_model.sh
$ chmod u+x get_caffe_reference_imagenet_model.sh
$ ./get_caffe_reference_imagenet_model.sh
$ cd ~/caffe/data/ilsvrc12/
$ ./get_ilsvrc_aux.sh
$ cd ~/caffe/
$ wget http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz
$ tar xzvf 101_ObjectCategories.tar.gz
$ echo "export PYTHONPATH=$HOME/caffe/python:$PYTHONPATH" >> ~/.bashrc
$ source ~/.bashrc

Run

Classification by reference model

$ cd ~/caffe/python/
$ python classify.py --raw_scale 255 ../101_ObjectCategories/airplanes/image_0001.jpg ../result.npy

Follow the link above to create and run show_result.py.

$ cd ~/caffe/
$ python show_result.py data/ilsvrc12/synset
synset_words.txt  synsets.txt       
$ python show_result.py data/ilsvrc12/synset_words.txt result.npy 
#1 | n04552348 warplane, military plane | 84.8%
#2 | n04008634 projectile, missile |  5.5%
#3 | n02690373 airliner |  5.1%
If you get an error in classify.py

It seems that numpy's io and PyCaffe's io.py conflict. (Strange Issue using Python # 782)

$ python classify.py --raw_scale 255 ~/caffe/101_ObjectCategories/airplanes/image_0001.jpg ../result.npy
Traceback (most recent call last):
  File "classify.py", line 7, in <module>
    import numpy as np

...


  File "/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py", line 8, in <module>
    dtype_range = {np.bool_: (False, True),
AttributeError: 'module' object has no attribute 'bool_'

Rename ʻio.pytocaffe_io.py, which is an unreasonable method. If you rename it here, you also need to rename it with feature.py` created in the next" Classification using Caffe as a feature extractor ".

$ cd ~/caffe/python/
$ aftfile="caffe_io"
$ for file in `find . -name "*.py"`; do; cat $file | sed -e "s/import [\w\.]*io/import $aftfile/g" | sed -e "s/caffe\.io/caffe\.$aftfile/g" > $file".tmp";mv $file".tmp" $file; done
$ mv "caffe/io.py" "caffe/"$aftfile".py"

Classification using Caffe as a feature extractor

I've completely forgotten to switch back to the previous branch, so maybe there's something a little different.

$ cd ~/caffe
$ cp examples/imagenet/imagenet_deploy.prototxt examples/imagenet/imagenet_feature.prototxt
$ vim examples/imagenet/imagenet_feature.prototxt

Change ʻimagenet_feature.prototxt` as follows.

imagenet_feature.prototxt


...

154   top: "fc6wi"

...

162   bottom: "fc6wi"

...

deeplearning-tutorials Create a file with features in libsvm format.

$ cd ~/caffe/
$ git clone https://github.com/CORDEA/deeplearning-tutorials
$ mv ~/caffe/deeplearning-tutorials/caffe/feature.py ~/caffe/
$ python feature.py

Or

$ cd ~/caffe/
$ wget https://raw.githubusercontent.com/CORDEA/deeplearning-tutorials/master/caffe/feature.py
$ python feature.py

train command to svm-train predict command becomes svm-predict Please note that each name has changed.

$ svm-scale -s scale.txt train.txt > train_scaled.txt
$ svm-scale -r scale.txt test.txt > test_scaled.txt
$ svm-train -c 0.03 train_scaled.txt caltech101.model

Fine tuning

Prepare the necessary files.

When git clone is done in" Classification using Caffe as a feature extractor "

$ cd ~/caffe/
$ mv ~/caffe/deeplearning-tutorials/caffe/fine_tuning.py ~/caffe/
$ python fine_tuning.py

When wget

$ cd ~/caffe/
$ wget https://raw.githubusercontent.com/CORDEA/deeplearning-tutorials/master/caffe/fine_tuning.py
$ python fine_tuning.py

Since the specifications of convert_imageset.bin have changed, it will not work according to the reference article. I don't know which of the current flags 1 is specified in the reference article, but I'm proceeding with the judgment that it is probably -gray.

$ build/tools/convert_imageset.bin -gray -resize_width 256 -resize_height 256 101_ObjectCategories/ train.txt caltech101_train_leveldb
$ build/tools/convert_imageset.bin -gray -resize_width 256 -resize_height 256 101_ObjectCategories/ val.txt caltech101_val_leveldb
$ build/tools/compute_image_mean.bin caltech101_train_leveldb caltech101_mean.binaryproto

Please note that the reference destination has changed from the reference article.

$ cp ~/caffe/models/bvlc_reference_caffenet/*.prototxt ~/caffe/
$ cd ~/caffe/
$ sed -i -e 's/fc8/fc8ft/g' train_val.prototxt deploy.prototxt

Please change the following part with an editor (such as vi).

solver.prototxt


1 net: "train_val.prototxt"

...

4 base_lr: 0.001

...

14 solver_mode: CPU

train_val.prototxt


8     source: "caltech101_train_leveldb"
9     backend: LEVELDB

...

14     mean_file: "caltech101_mean.binaryproto"

...

25     source: "caltech101_val_leveldb"
26     backend: LEVELDB

...

31     mean_file: "caltech101_mean.binaryproto"

...

321     num_output: 102

deploy.prototxt


5 input_dim: 256
6 input_dim: 256

...

204     num_output: 102
$ build/tools/caffe train -solver solver.prototxt

Pylearn2

Installation

$ sudo apt-get install python-setuptools python-pip python-dev python-numpy python-scipy python-yaml python-matplotlib liblapack-dev python-nose2 cython
$ sudo pip install theano
$ cd ~
$ git clone https://github.com/lisa-lab/pylearn2.git
$ cd pylearn2
$ sudo python setup.py develop

path

$ echo "export PATH=$HOME/pylearn2/pylearn2/scripts/:$PATH" >> ~/.bashrc
$ echo "export PYLEARN2_DATA_PATH=$HOME/.data/lisa/data" >> ~/.bashrc
$ source ~/.bashrc

Tutorials

Gaussian-Bernoulli RBM examples using CIFAR10

$ cd ~/pylearn2/pylearn2/scripts/datasets/
$ ./download_cifar10.sh
$ cd ~/pylearn2/pylearn2/scripts/tutorials/grbm_smd/
$ python make_dataset.py
$ train.py cifar_grbm_smd.yaml
$ show_weights.py cifar_grbm_smd.pkl --out weights_result.png #If you have a GUI--out does not have to be
$ plot_monitor.py cifar_grbm_smd.pkl --out monitor_result.png
Put e, b, s or h in the list somewhere to plot epochs, batches, seconds, or hours, respectively.
Enter a list of channels to plot (example: A, C,F-G, h, <test_err>) or q to quit or o for options: b,L,M
set x_axis to example
A. bias_hid_max:cifar_grbm
B. bias_hid_mean:cifar_grbm
C. bias_hid_min:cifar_grbm
D. bias_vis_max:cifar_grbm
E. bias_vis_mean:cifar_grbm
F. bias_vis_min:cifar_grbm
G. h_max:cifar_grbm
H. h_mean:cifar_grbm
I. h_min:cifar_grbm
J. learning_rate:cifar_grbm
K. objective:cifar_grbm
L. reconstruction_error:cifar_grbm
M. total_seconds_last_epoch:cifar_grbm
N. training_seconds_this_epoch:cifar_grbm

Put e, b, s or h in the list somewhere to plot epochs, batches, seconds, or hours, respectively.
Enter a list of channels to plot (example: A, C,F-G, h, <test_err>) or q to quit or o for options: q

Confirm image

When running with docker

Just copy to local with docker cp

% container="pylearn2" # Enter the container ID or name
% for file in `echo weights_result.png monitor_result.png`;do;docker cp $container:/home/cordea/pylearn2/pylearn2/scripts/tutorials/grbm_smd/$file /host/path/to/dir/;done
% open *.png # for Mac

SdA examples using MNIST

$ cd ~/pylearn2/pylearn2/scripts/datasets/
$ python download_mnist.py
$ cd ~/pylearn2/pylearn2/scripts/tutorials/stacked_autoencoders/tests
$ python test_dae.py

Use the data you prepared

** If you want to use csv as it is, here will be helpful. ** **

This time I will introduce how to create pkl from csv.

Convert the image to csv after resize and gray-scale.

$ cd ~
$ git clone https://github.com/CORDEA/deeplearning-tutorials.git
$ cd deeplearning-tutorials/pylearn2
$ mkdir in

convertImage.py is created assuming the following directory structure. Please use the directory name as label information.

.
├── convertImage.py
    ├── in
        ├── hoge # label name
        |   ├── hogehoge.png
        |   ...
        |
        ├── huge
        |   ├── hugehuge.png
        |   ...
        ...
$ python convertImage.py

Make the created train.csv into a pkl file.

$ python createpkl.py

I think that you can use train.pkl by referring to the sample of~ / pylearn2 / pylearn2 / scripts / tutorials /.

Example

The number of classes is illustrated as 10.

$ cd ~/pylearn2/pylearn2/scripts/tutorials/dbm_demo/
$ cp ~/deeplearning-tutorials/pylearn2/train.pkl ./

rbm.yaml


13,21c13
<     dataset: &data !obj:pylearn2.datasets.binarizer.Binarizer {
<         # We use the "raw" tag to specify the underlying dataset defining
<         # the sampling probabilities should be MNIST.
<         raw: &raw_train !obj:pylearn2.datasets.mnist.MNIST {
<             which_set: "train",
<             start: 0,
<             stop: %(train_stop)i
<         }
<     },
---
>     dataset: !pkl: "train.pkl",

train_dbm.py


22     hyper_params = {'detector_layer_dim': 10,
$ python train_dbm.py rbm.yaml
$ show_weights.py dbm.pkl --out figure.png

error

pylearn2/pylearn2/space/__init__.py


794                 raise TypeError(Cannot safely cast batch dtype %s to space's dtype %s. % (batch.dtype, self.dtype))

I got an error here, but even if I messed with the data, it didn't go well.

pylearn2/pylearn2/space/__init__.py


794                 # raise TypeError("Cannot safely cast batch dtype %s to "
795                 print "Might not be safely cast batch dtype %s to space's dtype %s." \
796                         % (batch.dtype, self.dtype)

pylearn2/pylearn2/format/target_format.py


 98             try:
 99                 print "Run the conversion to int64 from %s" % (targets.dtype)
100                 targets = np.array([[int(r) for r in label] for label in targets])
101             except:
102                 raise TypeError("need an integer array for targets, %s" % (targets.dtype))

For now, I'm forcibly moving it like this ...

Thank you for your hard work.

reference

Caffe

Pylearn2

-[Pylearn2 I want to get started --laughing blog](http://laughing.hatenablog.com/entry/2013/11/30/pylearn2_%E5%85%A5%E9%96%80%E3%81%97% E3% 81% 9F% E3% 81% 84% E7% B7% A8)

-Deep Learning with Caffe, focusing on places where you can easily trip

-Handwriting recognition using Pylearn2

-Installation and tutorial of the now deep learning library "Pylearn2"

-[Machine learning for men-Let's get common features of A ◯ actresses with RBM--- New kensuke-mi's diary](http://kensuke-mi.xyz/kensuke-mi_diary/2014/11/ rbma.html)

-Grayscale and ASCII art of images with Python / PIL --Soleil cou coupé

Recommended Posts

Try Caffe and Pylearn2 together
Try using pytest-Overview and Samples-
Build TOPPERS / ASP3 and try it
Install fabric on Ubuntu and try
Try and learn iptablse, port forwarding