├── .gitignore
├── CMakeLists.txt
├── README.html
├── README.md
├── RNN-train-sample
├── FW.lua
├── GRU.lua
├── README.md
├── RNN.lua
├── data.lua
├── longData.lua
├── main.lua
├── network.lua
├── testLongData.lua
├── test_FW.lua
├── test_FW_TT.lua
└── train-er-rnn.lua
├── _xLearn-demos
├── README-train
├── elab_logo.gif
├── face-detect-multiscale.lua
├── face-detect-segmented.lua
├── face-detect-singlescale.lua
├── face-detect-w-attention.lua
├── face-detect.ui
├── faces-attention.ui
├── haarcascade_frontalface_alt.xml
├── live-kinect.lua
├── live-kinect.ui
├── live-segm+sift.lua
├── live-segm+sift.ui
├── live-segm.lua
├── live-segm.ui
├── live-sift.lua
├── live-sift.ui
├── multithreaded.lua
├── opencv-tester.lua
├── opencv-tester.ui
├── temporal-diff.lua
├── test-cam.lua
├── test-opticalFlow.lua
├── test-segmentation.lua
├── test-stereo.lua
├── train-on-faces.lua
├── train-on-mnist-hessian.lua
├── train-on-mnist.lua
└── trainAutoencoder.lua
├── attention
├── .DS_Store
├── attention.lua
└── attention.ui
├── demo-core
├── README.md
├── display.lua
├── framecamera.lua
├── frameimage.lua
├── framevideo.lua
├── process.lua
└── spatial.lua
├── demo-learner
├── g.ui
├── glarge.ui
├── run.lua
└── ui.lua
├── demo-tracker
├── display.lua
├── frame.lua
├── process.lua
└── run.lua
├── demo-visor
├── README.md
└── run.lua
├── dok
├── graphicalmodels
│ └── index.dok
├── gui
│ └── index.dok
├── supervised
│ ├── convnet.png
│ ├── index.dok
│ ├── linear_regression.png
│ ├── logistic_argmax.png
│ ├── logistic_regression.png
│ ├── loss.png
│ ├── mse_loss.png
│ └── nll_loss.png
└── unsupervised
│ ├── auto_decoder.png
│ ├── auto_encoder.png
│ ├── index.dok
│ ├── mse_loss.png
│ ├── psd_encoder.png
│ ├── psd_loss.png
│ ├── sparse_coding.png
│ └── sparse_coding_optim.png
├── face-detector-elab
├── HDg.ui
├── PyramidPacker.lua
├── PyramidUnPacker.lua
├── g.ui
├── model.net
└── run.lua
├── filter-bank
├── g.ui
└── run.lua
├── flow
├── g.ui
└── run.lua
├── gabor-layer-demo
├── GaborLayer.lua
├── frame.ui
└── gabortest.lua
├── linear-regression
└── example-linear-regression.lua
├── live-kinect
├── g.ui
└── run.lua
├── load-data
└── load-images.lua
├── logistic-regression
├── example-logistic-regression.csv
└── example-logistic-regression.lua
├── mst-based-segmenter
└── run.lua
├── person-detector
├── HDg.ui
├── PyramidPacker.lua
├── PyramidUnPacker.lua
├── README.md
├── data.lua
├── g.ui
├── model.lua
├── model.net
├── preprocessing.lua
├── run.lua
├── rundemo.lua
├── test.lua
└── train.lua
├── profiling
├── .DS_Store
├── conv-cpu.lua
├── conv-gpu.lua
├── linear-cpu.lua
├── linear-gpu.lua
└── results.rtf
├── road-net
├── multinet-float.net
├── preproc.t7
├── run.lua
├── segmtools.lua
├── u1net.net
└── unsup-cl-30.net
├── saliency-itti
├── g.ui
└── run.lua
├── saliency-net
├── g.ui
├── run-qt.lua
└── run-sdl.lua
├── segment-color
├── g.ui
└── run.lua
├── simple-frame-grabber
├── g.ui
└── run.lua
├── temporal-difference
├── frame.lua
├── lib
│ ├── Makefile
│ ├── http.c
│ ├── http.h
│ ├── video_decoder.c
│ ├── videocap.c
│ ├── videocap.h
│ ├── videocodec.c
│ └── videocodec.h
└── run.lua
├── tensors
└── slicing.lua
├── tracker
├── .gitignore
├── README.md
├── compile.sh
├── display.lua
├── fastdist.c
├── fastdist.h
├── g.ui
├── process.lua
├── random.net
├── run.lua
├── source.lua
├── state.lua
├── supervised.net
├── ui.lua
└── unsupervised.net
├── train-a-digit-classifier
├── dataset-mnist.lua
└── train-on-mnist.lua
├── train-autoencoder
├── autoencoder-data.lua
└── train-autoencoder.lua
├── train-face-detector-elab
├── data.lua
├── model.lua
├── run.lua
├── test.lua
└── train.lua
├── train-on-cifar
└── train-on-cifar.lua
└── train-on-housenumbers
└── train-on-housenumbers.lua
/.gitignore:
--------------------------------------------------------------------------------
1 | # objects
2 | *.o
3 | *.so
4 | *.a
5 | *.log
6 | .DS_Store
7 | *.t7
8 | *.svg
9 | *.dot
10 | *.swp
11 |
12 | # Ignore all scratch dir.
13 | scratch/
14 |
15 | # Videos dir
16 | videos/
17 |
18 | # Ignore datasets
19 | datasets/
20 |
21 | # Ignore log folders
22 | log/
23 |
24 | results/
25 |
26 |
27 | train-face-detector-elab/face-dataset.zip
28 | train-face-detector-elab/face-dataset/*
29 |
30 | person-detector/INRIAPerson.zip
31 | person-detector/INRIAPerson/*
32 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 |
2 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)
3 | CMAKE_POLICY(VERSION 2.6)
4 | FIND_PACKAGE(Torch REQUIRED)
5 |
6 | ADD_TORCH_DOK(dok/supervised tutorials_supervised "Tutorials" "Supervised Learning" 1.0)
7 | ADD_TORCH_DOK(dok/unsupervised tutorials_unsupervised "Tutorials" "Unsupervised Learning" 1.1)
8 | ADD_TORCH_DOK(dok/graphicalmodels tutorials_graphical "Tutorials" "Graphical Models" 1.2)
9 | ADD_TORCH_DOK(dok/gui tutorials_gui "Tutorials" "Graphical User Interfaces" 1.3)
10 |
11 | SET(luasrc linear-regression/example-linear-regression.lua
12 | logistic-regression/example-logistic-regression.lua
13 | logistic-regression/example-logistic-regression.csv
14 | train-a-digit-classifier/train-on-mnist.lua
15 | train-a-digit-classifier/dataset-mnist.lua
16 | train-on-cifar/train-on-cifar.lua
17 | train-on-housenumbers/train-on-housenumbers.lua
18 | train-autoencoder/train-autoencoder.lua)
19 |
20 | ADD_TORCH_PACKAGE(tutorials "" "${luasrc}" "Tutorials")
21 |
--------------------------------------------------------------------------------
/README.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Torch7 Packages
5 |
12 |
13 |
14 | Demos & Turorials for Torch7.
15 | All the demos/tutorials provided in this repo require Torch7 to be installed, as well as some extra (3rd-party) packages.
16 | Install
17 | Torch7
18 | Follow instructions on: Torch7's homepage.
19 | 3rd-party packages
20 | Different demos/tutorials rely on different 3rd-party packages. If a demo crashes because it can't find a package then simply try to install it using torch-pkg:
21 | $ torch-pkg install image # an image library for Torch7
22 | $ torch-pkg install nnx # lots of extra neural-net modules
23 | $ torch-pkg install camera # a camera interface for Linux/MacOS
24 | $ torch-pkg install ffmpeg # a video decoder for most formats
25 | $ ...
26 | A complete list of packages can be obtained by doing:
27 | $ torch-pkg list
28 | or checking out this page.
29 | Documentation
30 | Torch's main documentation gets installed when you install Torch, and should be available here, or wherever you installed Torch.
31 | This repo also comes with documentation, in the form of tutorials. To make them available, simply issue the following command in this directory:
32 | $ torch-pkg deploy
33 | After that, a list of tutorials will be available in the documentation.
34 | Tutorials
35 | Each directory provides a tutorial or a demo, with no particular order. It's a good idea to have the local Torch documentation open on the side, for reference. As Torch is based on the Lua language, it's also a good idea to go through the Lua 5.1 book.
36 | Credits
37 | These demos were slowly put together by: Clement Farabet & Roy Lowrance.
38 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Demos & Tutorials for Torch7.
2 |
3 | All the demos/tutorials provided in this repo require Torch7 to be installed,
4 | as well as some extra (3rd-party) packages.
5 |
6 | ## Install
7 |
8 | ### Torch7
9 |
10 | Follow instructions on: [Torch7's homepage](http://www.torch.ch/).
11 |
12 | ### 3rd-party packages
13 |
14 | Different demos/tutorials rely on different 3rd-party packages. If a demo
15 | crashes because it can't find a package then simply try to install it using
16 | luarocks:
17 |
18 | ``` sh
19 | $ luarocks install image # an image library for Torch7
20 | $ luarocks install nnx # lots of extra neural-net modules
21 | $ luarocks install camera # a camera interface for Linux/MacOS
22 | $ luarocks install ffmpeg # a video decoder for most formats
23 | $ ...
24 | ```
25 |
26 | A complete list of packages can be obtained by doing:
27 |
28 | ``` sh
29 | $ luarocks list
30 | ```
31 |
32 | or checking out [this page](http://htmlpreview.github.io/?https://github.com/torch/rocks/blob/master/index.html).
33 |
34 | After that, a list of tutorials will be available in the documentation.
35 |
36 | ## Tutorials
37 |
38 | Each directory provides a tutorial or a demo, with no particular order.
39 | It's a good idea to have the torch documentation
40 | on the side, for reference. As Torch is based on the Lua language,
41 | it's also a good idea to go through the [Lua 5.1](http://www.lua.org/manual/5.1/)
42 | book.
43 |
44 | ## Credits
45 |
46 | These demos were slowly put together by: Clement Farabet & Roy Lowrance, and are now maintained by e-lab
47 |
48 |
49 | ### License
50 |
51 | MIT
52 |
--------------------------------------------------------------------------------
/RNN-train-sample/FW.lua:
--------------------------------------------------------------------------------
1 | --------------------------------------------------------------------------------
2 | -- Simple FW block
3 | --
4 | -- Notations used from: Using Fast Weights to Attend to the Recent Past
5 | -- URL: https://arxiv.org/abs/1610.06258
6 | --
7 | -- Written by: Alfredo Canziani, Jan 17
8 | --------------------------------------------------------------------------------
9 |
10 | local FW = {}
11 |
12 | local function normalise(d)
13 | local input = nn.Identity()()
14 | local mean = input - nn.Mean() - nn.Replicate(d)
15 | local centred = {input, mean} - nn.CSubTable()
16 | local std = centred - nn.Square() - nn.Mean() - nn.Sqrt()
17 | - nn.Replicate(d) - nn.AddConstant(1e-5)
18 | local output = {centred, std} - nn.CDivTable()
19 | return nn.gModule({input},{output})
20 | end
21 |
22 | function getUninitPrototype(n, d, nHL, K, opt)
23 |
24 | local opt = opt or {}
25 | local l = opt.lambda or 0.9
26 | local e = opt.eta or 0.5
27 | local S = opt.S or 1
28 | local LayerNorm = opt.LayerNorm or true
29 | local affine = {}
30 |
31 | local inputs = {}
32 | inputs[1] = nn.Identity()() -- input X
33 | for j = 1, 2 * nHL do
34 | table.insert(inputs, nn.Identity()()) -- previous states h[j] and A
35 | end
36 |
37 | local x, nIn
38 | local outputs = {}
39 | for j = 1, nHL do
40 | if j == 1 then
41 | x = inputs[j]:annotate{name = 'x[t]',
42 | graphAttributes = { style = 'filled', fillcolor = 'moccasin'}
43 | }
44 |
45 | nIn = n
46 | else
47 | x = outputs[2 * j - 3]
48 | nIn = d
49 | end
50 |
51 | local hPrev = inputs[2*j]:annotate{name = 'h^('..j..')[t-1]',
52 | graphAttributes = { style = 'filled', fillcolor = 'lightpink'}
53 | }
54 |
55 | local A = inputs[2*j + 1]:annotate{name = 'A^('..j..')[t-1]',
56 | graphAttributes = { style = 'filled', fillcolor = 'plum'}
57 | }
58 |
59 |
60 | local hPrevVect = hPrev - nn.View(-1, 1)
61 | local lambda_A = (A - nn.MulConstant(l)):annotate{name = 'lambda_A'}
62 | local eta_hhT = ({
63 | hPrevVect - nn.Identity(), hPrevVect - nn.Identity()
64 | } - nn.MM(false, true) - nn.MulConstant(e)):annotate{name = 'eta_hhT'}
65 | A = {lambda_A, eta_hhT} - nn.CAddTable()
66 | A:annotate{name = 'A^('..j..')[t]',
67 | graphAttributes = { style = 'filled', fillcolor = 'paleturquoise'}
68 | }
69 |
70 | table.insert(affine, nn.Linear(nIn + d, d))
71 | local dot = {x, hPrev} - nn.JoinTable(1)
72 | - affine[#affine]
73 | dot:annotate{name = 'dot'}
74 |
75 | local hs = (dot - nn.ReLU()):annotate{name = 'h_' .. 0,
76 | graphAttributes = { style = 'filled', fillcolor = 'tan'}
77 | }
78 | for s = 1, S do
79 | hs = {dot, {A, hs - nn.View(-1, 1)} - nn.MM()} - nn.CAddTable()
80 | if LayerNorm then
81 | hs = (hs - normalise(d)):annotate{name = 'LayerNorm',
82 | graphAttributes = { style = 'filled', fillcolor = 'lavender'}
83 | }
84 | end
85 | hs = (hs - nn.ReLU()):annotate{name = 'h_' .. s,
86 | graphAttributes = { style = 'filled', fillcolor = 'tan'}
87 | }
88 | end
89 |
90 | hs:annotate{name = 'h^('..j..')[t]',
91 | graphAttributes = { style = 'filled', fillcolor = 'skyblue'}
92 | }
93 |
94 |
95 | table.insert(outputs, hs)
96 | table.insert(outputs, A)
97 | end
98 |
99 | local logsoft = (outputs[#outputs - 1] - nn.Linear(d, K) - nn.LogSoftMax())
100 | :annotate{name = 'y\'[t]', graphAttributes = {
101 | style = 'filled', fillcolor = 'seagreen1'}}
102 | table.insert(outputs, logsoft)
103 |
104 | -- Output is table with {h, prediction}
105 | return nn.gModule(inputs, outputs), affine
106 | end
107 |
108 | -- Properly prototypes initialisation
109 | function FW.getPrototype(...)
110 | local proto, affine = getUninitPrototype(...)
111 |
112 | -- Init W_h to an identity mat scaled by 0.05
113 | for _, m in ipairs(affine) do
114 | local W = m.weight -- combined weight matrix
115 | local d = W:size(1) -- hidden size
116 | local n = W:size(2) - d -- input size
117 | local W_h = W:narrow(2, n+1, d) -- h[t-1] |-> h[t]
118 | W_h:copy(torch.eye(d)):mul(0.05)
119 | end
120 |
121 | return proto
122 | end
123 |
124 | return FW
125 |
--------------------------------------------------------------------------------
/RNN-train-sample/GRU.lua:
--------------------------------------------------------------------------------
1 | --------------------------------------------------------------------------------
2 | -- Simple GRU block
3 | --
4 | -- Notations used from: http://colah.github.io/posts/2015-08-Understanding-LSTMs/
5 | --
6 | -- Written by: Abhishek Chaurasia
7 | --------------------------------------------------------------------------------
8 |
9 | local GRU = {}
10 |
11 | function GRU.getPrototype(n, d, nHL, K)
12 | local inputs = {}
13 | inputs[1] = nn.Identity()() -- input X
14 | for j = 1, nHL do
15 | table.insert(inputs, nn.Identity()()) -- previous states h[j]
16 | end
17 |
18 | local x, nIn
19 | local outputs = {}
20 | for j = 1, nHL do
21 | if j == 1 then
22 | x = inputs[j]
23 | nIn = n
24 | else
25 | x = outputs[j-1]
26 | nIn = d
27 | end
28 |
29 | local hPrev = inputs[j+1]
30 |
31 | local z = {x, hPrev} - nn.JoinTable(1)
32 | - nn.Linear(nIn + d, d)
33 | - nn.Sigmoid()
34 |
35 | local r = {x, hPrev} - nn.JoinTable(1)
36 | - nn.Linear(nIn + d, d)
37 | - nn.Sigmoid()
38 |
39 | local prodRH = {r, hPrev} - nn.CMulTable()
40 | local hTilde = {x, prodRH} - nn.JoinTable(1)
41 | - nn.Linear(nIn + d, d)
42 | - nn.Tanh()
43 |
44 | local subZ = z - nn.AddConstant(-1) - nn.MulConstant(-1)
45 | local prodZH = {subZ, hPrev} - nn.CMulTable()
46 | local prodZhTilde = {z, hTilde} - nn.CMulTable()
47 |
48 | local nextH = {prodZH, prodZhTilde} - nn.CAddTable()
49 |
50 | table.insert(outputs, nextH)
51 | end
52 |
53 | local logsoft = (outputs[#outputs] - nn.Linear(d, K) - nn.LogSoftMax())
54 | :annotate{name = 'y\'[t]',
55 | graphAttributes = {
56 | style = 'filled',
57 | fillcolor = 'seagreen1'}}
58 | table.insert(outputs, logsoft)
59 |
60 | -- Output is table with {h, prediction}
61 | return nn.gModule(inputs, outputs)
62 | end
63 |
64 | return GRU
65 |
--------------------------------------------------------------------------------
/RNN-train-sample/README.md:
--------------------------------------------------------------------------------
1 | # RNN training example
2 |
3 | Train RNNs to recognize a sequence `abba` in a random `ababbaabbbaaababab` sequence of two characters.
4 |
5 | Version 1. `main.lua`: Send sequecne of 4 symbols to train but test on each character at a time.
6 |
7 | ```bash
8 | th main.lua
9 | ```
10 |
11 | The results will be not optimal (by default you're using `1` hidden layer with `d = 2` neurons), but you'll be able to appreciate a wide variety of combinations.
12 | True positives, true negatives, false positives and false negatives.
13 | Try to use more neurons or more hidden layers to improve performace (switch from `d = 2` neurons to `3`, and all will work perfectly, even with just one hidden layer).
14 | (Type `th main.lua -h` to see all the available options.)
15 |
16 | There are three different models available to train which can be selected by modifying variable `mode`:
17 |
18 | + Simple [RNN](RNN.lua)
19 | + Gated Recurrent Unit ([GRU](GRU.lua))
20 | + Fast Weights ([FW](FW.lua))
21 |
22 | Version 2. `train-er-rnn.lua`: is based on Element Research [rnn package](https://github.com/Element-Research/rnn/blob/master/examples/sequence-to-one.lua).
23 |
24 | ```
25 | th train-er-rnn.lua
26 | ```
27 |
28 | ## Train on longer sequences
29 |
30 | To make the task more challenging, we can increase the number of symbols that make up our `key` sequence.
31 | To experiment with longer random sequences, use the optionr:
32 |
33 | + `-ds randomSeq`,
34 | + `-S [int]` to specify the key sequence length,
35 | + `-T [int]` to set the unrolling through time steps,
36 | + `-mode [RNN|GRU|FW]` to select a specific model,
37 | + `-d [int]` for the hidden unit dimension,
38 | + `-nHL [int]` to set the number of hidden layers
39 |
--------------------------------------------------------------------------------
/RNN-train-sample/RNN.lua:
--------------------------------------------------------------------------------
1 | --------------------------------------------------------------------------------
2 | -- Simple RNN block
3 | --
4 | -- Written by: Abhishek Chaurasia
5 | --------------------------------------------------------------------------------
6 |
7 | local RNN = {}
8 |
9 | --[[
10 | +-----------+
11 | | |
12 | | +----+ |
13 | V | |--+
14 | +--->(+)->| h1 |
15 | |x1 | |------+
16 | | +----+ |
17 | | |
18 | | |
19 | | |
20 | | +-----------+ |
21 | | | | |
22 | +-----+ | | +----+ | | +---+
23 | | 1 | | V | |--+ +---->| |
24 | | x:2 +-+--->(+)->| h2 | +------->| y |
25 | | 3 | |x2 | |---+ +---->| |
26 | +-----+ | +----+ | +---+
27 | | |
28 | | |
29 | | |
30 | | +-----------+ |
31 | | | | |
32 | | | +----+ | |
33 | | V | |--+ |
34 | +--->(+)->| h3 | |
35 | x3 | |------+
36 | +----+
37 |
38 | --]]
39 |
40 | -- n : # of inputs
41 | -- d : # of neurons in hidden layer
42 | -- nHL : # of hidden layers
43 | -- K : # of output neurons
44 |
45 | -- Returns a simple RNN model
46 | function RNN.getPrototype(n, d, nHL, K)
47 | local inputs = {}
48 | table.insert(inputs, nn.Identity()()) -- input X
49 | for j = 1, nHL do
50 | table.insert(inputs, nn.Identity()()) -- previous states h[j]
51 | end
52 |
53 | local x, nIn
54 | local outputs = {}
55 | for j = 1, nHL do
56 | if j == 1 then
57 | x = inputs[j]:annotate{name = 'x[t]',
58 | graphAttributes = {
59 | style = 'filled',
60 | fillcolor = 'moccasin'}}
61 | nIn = n
62 | else
63 | x = outputs[j-1]
64 | nIn = d
65 | end
66 |
67 | local hPrev = inputs[j+1]:annotate{name = 'h^('..j..')[t-1]',
68 | graphAttributes = {
69 | style = 'filled',
70 | fillcolor = 'lightpink'}}
71 |
72 | -- Concat input with previous state
73 | local nextH = ({x, hPrev} - nn.JoinTable(1) - nn.Linear(nIn + d, d) - nn.Tanh())
74 | :annotate{name = 'h^('..j..')[t]',
75 | graphAttributes = {
76 | style = 'filled',
77 | fillcolor = 'skyblue'}}
78 |
79 | table.insert(outputs, nextH)
80 | end
81 |
82 | local logsoft = (outputs[#outputs] - nn.Linear(d, K) - nn.LogSoftMax())
83 | :annotate{name = 'y\'[t]',
84 | graphAttributes = {
85 | style = 'filled',
86 | fillcolor = 'seagreen1'}}
87 | table.insert(outputs, logsoft)
88 |
89 | -- Output is table with {h, prediction}
90 | return nn.gModule(inputs, outputs)
91 | end
92 |
93 | return RNN
94 |
--------------------------------------------------------------------------------
/RNN-train-sample/data.lua:
--------------------------------------------------------------------------------
1 | local data = {}
2 |
3 | function data.getData(trainSize, seqLength)
4 | -- Generate random sequence of 1s and 2s
5 | local s = torch.Tensor(trainSize):random(2)
6 |
7 | -- Labels for training
8 | local y = torch.ones(trainSize)
9 |
10 | for i = seqLength, trainSize do
11 | if s[i-3] == 1 and s[i-2] == 2 and s[i-1] == 2 and s[i] == 1 then
12 | y[i] = 2
13 | end
14 | end
15 |
16 | -- Mapping of input from R trainSize to trainSize x 2
17 | -- a => 1 => <1, 0>
18 | -- b => 2 => <0, 1>
19 |
20 | local x = torch.zeros(trainSize, 2)
21 | for i = 1, trainSize do
22 | if s[i] == 1 then
23 | x[i][1] = 1
24 | else
25 | x[i][2] = 1
26 | end
27 | end
28 | return x, y
29 | end
30 |
31 | return data
32 |
--------------------------------------------------------------------------------
/RNN-train-sample/longData.lua:
--------------------------------------------------------------------------------
1 | local data = {}
2 | local key
3 | local debug = false
4 |
5 | function data.getData(trainSize, seqLength)
6 | -- Get key
7 | if not key then
8 | key = torch.Tensor(seqLength):random(2)
9 | io.write('Key generated: ' .. sys.COLORS.blue)
10 | for _, k in ipairs(key:totable()) do
11 | io.write(k == 1 and 'a' or 'b')
12 | end
13 | io.write(sys.COLORS.none .. '\n')
14 | end
15 |
16 | -- Generate random sequence of 1s and 2s
17 | local s = torch.Tensor(trainSize):random(2)
18 |
19 | -- Labels for training
20 | local y = torch.ones(trainSize)
21 |
22 | -- Injects key for ~1/3 of cases
23 | local injections = math.floor(trainSize / seqLength / 3)
24 | assert(injections > 0,
25 | 'trainSize should be at least 3 times longer than seqLength')
26 | for inj = 1, injections do
27 | local idx = math.random(trainSize - seqLength + 1)
28 | s:narrow(1, idx, seqLength):copy(key)
29 | end
30 |
31 | for i = 1, trainSize - seqLength + 1 do
32 | if s:narrow(1, i, seqLength):eq(key):prod() == 1 then
33 | y[i+seqLength-1] = 2
34 | end
35 | end
36 |
37 | if debug then
38 | -- Debugging
39 | local none = sys.COLORS.none
40 | local red = sys.COLORS.red
41 | for i = 1, trainSize do
42 | local modifier
43 | if i > trainSize - seqLength + 1 or y[i+seqLength-1] == 1 then
44 | io.write(none)
45 | else
46 | io.write(red)
47 | end
48 | io.write(s[i] == 1 and 'a' or 'b')
49 | end
50 | io.write('\n')
51 | for _, k in ipairs(y:totable()) do
52 | io.write(k == 1 and ' ' or '|')
53 | end
54 | io.write('\n')
55 | end
56 |
57 | -- Mapping of input from R trainSize to trainSize x 2
58 | -- a => 1 => <1, 0>
59 | -- b => 2 => <0, 1>
60 |
61 | local x = torch.zeros(trainSize, 2)
62 | for i = 1, trainSize do
63 | if s[i] == 1 then
64 | x[i][1] = 1
65 | else
66 | x[i][2] = 1
67 | end
68 | end
69 | return x, y
70 | end
71 |
72 | function data.getKey()
73 | assert(key ~= nil, 'Key not yet generated')
74 | return key
75 | end
76 |
77 | function data.toggleDebug()
78 | debug = not debug
79 | print('Debug is ' .. (debug and 'ON' or 'OFF'))
80 | end
81 |
82 | return data
83 |
--------------------------------------------------------------------------------
/RNN-train-sample/network.lua:
--------------------------------------------------------------------------------
1 | --------------------------------------------------------------------------------
2 | -- A simple building block for RNN/LSTMs
3 | --
4 | -- Written by: Abhishek Chaurasia
5 | --------------------------------------------------------------------------------
6 |
7 | local network = {}
8 | nngraph.setDebug(true)
9 | -- n : # of inputs
10 | -- d : # of neurons in hidden layer
11 | -- nHL : # of hidden layers
12 | -- K : # of output neurons
13 |
14 | -- Links all the prototypes, given the # of sequences
15 | function network.getModel(n, d, nHL, K, T, mode)
16 | local prototype
17 | if mode == 'RNN' then
18 | local RNN = require 'RNN'
19 | prototype = RNN.getPrototype(n, d, nHL, K)
20 | elseif mode == 'GRU' then
21 | local GRU = require 'GRU'
22 | prototype = GRU.getPrototype(n, d, nHL, K)
23 | elseif mode == 'FW' then
24 | local FW = require 'FW'
25 | prototype = FW.getPrototype(n, d, nHL, K)
26 | else
27 | print("Invalid model type. Available options: (RNN/GRU)")
28 | end
29 |
30 | local clones = {}
31 | for i = 1, T do
32 | clones[i] = prototype:clone('weight', 'bias', 'gradWeight', 'gradBias')
33 | end
34 |
35 | local inputSequence = nn.Identity()() -- Input sequence
36 | local H0 = {} -- Initial states of hidden layers
37 | local H = {} -- Intermediate states
38 | local outputs = {}
39 |
40 | -- Linking initial states to intermediate states
41 | for l = 1, nHL do
42 | table.insert(H0, nn.Identity()())
43 | H0[#H0]:annotate{
44 | name = 'h^('..l..')[0]', graphAttributes = {
45 | style = 'filled', fillcolor = 'lightpink'
46 | }
47 | }
48 | table.insert(H, H0[#H0])
49 | if mode == 'FW' then
50 | table.insert(H0, nn.Identity()())
51 | H0[#H0]:annotate{
52 | name = 'A^('..l..')[0]', graphAttributes = {
53 | style = 'filled', fillcolor = 'plum'
54 | }
55 | }
56 | table.insert(H, H0[#H0])
57 | end
58 | end
59 |
60 | local splitInput = inputSequence - nn.SplitTable(1)
61 |
62 | for i = 1, T do
63 | local x = (splitInput - nn.SelectTable(i))
64 | :annotate{name = 'x['..i..']',
65 | graphAttributes = {
66 | style = 'filled',
67 | fillcolor = 'moccasin'}}
68 |
69 | local tempStates = ({x, table.unpack(H)} - clones[i])
70 | :annotate{name = mode .. '['..i..']',
71 | graphAttributes = {
72 | style = 'filled',
73 | fillcolor = 'skyblue'}}
74 |
75 | local predIdx = nHL + 1
76 | if mode == 'FW' then predIdx = 2 * nHL + 1 end
77 | outputs[i] = (tempStates - nn.SelectTable(predIdx)) -- Prediction
78 | :annotate{name = 'y\'['..i..']',
79 | graphAttributes = {
80 | style = 'filled',
81 | fillcolor = 'seagreen1'}}
82 |
83 | if i < T then
84 | local j = 0
85 | for l = 1, nHL do -- State values passed to next sequence
86 | j = j + 1
87 | H[j] = (tempStates - nn.SelectTable(j)):annotate{
88 | name = 'h^('..l..')['..i..']', graphAttributes = {
89 | style = 'filled', fillcolor = 'lightpink'}}
90 | if mode == 'FW' then
91 | j = j + 1
92 | H[j] = (tempStates - nn.SelectTable(j)):annotate{
93 | name = 'A^('..l..')['..i..']', graphAttributes = {
94 | style = 'filled', fillcolor = 'plum'}}
95 | end
96 | end
97 | else
98 | local j = 0
99 | for l = 1, nHL do -- State values passed to next sequence
100 | j = j + 1
101 | outputs[T + j] = (tempStates - nn.SelectTable(j)):annotate{
102 | name = 'h^('..l..')['..i..']', graphAttributes = {
103 | style = 'filled', fillcolor = 'lightpink'}}
104 | if mode == 'FW' then
105 | j = j + 1
106 | outputs[T + j] = (tempStates - nn.SelectTable(j)):annotate{
107 | name = 'A^('..l..')['..i..']', graphAttributes = {
108 | style = 'filled', fillcolor = 'plum'}}
109 | end
110 | end
111 | end
112 | end
113 |
114 | -- Output is table of {Predictions, Hidden states of last sequence}
115 | local g = nn.gModule({inputSequence, table.unpack(H0)}, outputs)
116 |
117 | return g, clones[1]
118 | end
119 |
120 | return network
121 |
--------------------------------------------------------------------------------
/RNN-train-sample/testLongData.lua:
--------------------------------------------------------------------------------
1 | D = require 'longData'
2 | D.toggleDebug()
3 |
4 | trainSize = 130
5 | seqLength = 7
6 | print('trainSize: ' .. trainSize)
7 | print('seqLength: ' .. seqLength)
8 | a, b = D.getData(trainSize, seqLength)
9 |
10 | print(D.getKey():view(1, -1))
11 |
12 | trainSize = 60
13 | seqLength = 7
14 | print('trainSize: ' .. trainSize)
15 | print('seqLength: ' .. seqLength)
16 | a, b = D.getData(trainSize, seqLength)
17 |
18 | print(D.getKey():view(1, -1))
19 |
--------------------------------------------------------------------------------
/RNN-train-sample/test_FW.lua:
--------------------------------------------------------------------------------
1 | -- Test file for FW
2 |
3 | require 'nngraph'
4 | FW = require 'FW'
5 | n = 2; d = 3; nHL = 4; K = 2; T = 4
6 | opt = {
7 | S = 4;
8 | }
9 | prototype = FW.getPrototype(n, d, nHL, K, opt)
10 | graph.dot(prototype.fg, 'FW_proto', 'FW_proto')
11 |
12 | x = torch.randn(n)
13 | h = torch.randn(d)
14 | A = torch.randn(d, d)
15 | print(prototype:forward{x, h, A, h, A, h, A, h, A})
16 | graph.dot(prototype.fg, 'FW_proto_dim', 'FW_proto_dim')
17 |
--------------------------------------------------------------------------------
/RNN-train-sample/test_FW_TT.lua:
--------------------------------------------------------------------------------
1 | require 'nngraph'
2 | network = require 'network'
3 | n = 2; d = 3; nHL = 3; K = 2; T = 4
4 | mode = 'FW'
5 | model, prototype = network.getModel(n, d, nHL, K, T, mode)
6 | graph.dot(model.fg, 'FW_model', 'FW_model')
7 |
8 | x = torch.randn(T, n)
9 | h = torch.randn(d)
10 | A = torch.randn(d, d)
11 | print(model:forward{x, h, A, h, A, h, A})
12 | graph.dot(model.fg, 'FW_model_dim', 'FW_model_dim')
13 |
--------------------------------------------------------------------------------
/RNN-train-sample/train-er-rnn.lua:
--------------------------------------------------------------------------------
1 | -- Eugenio Culurciello
2 | -- September 2016
3 | -- RNN training test: ABBA sequence detector
4 | -- based on: https://github.com/Element-Research/rnn/blob/master/examples/sequence-to-one.lua
5 |
6 | require 'nn'
7 | require 'rnn'
8 |
9 | -- torch.setdefaulttensortype('torch.FloatTensor')
10 | -- nngraph.setDebug(true)
11 |
12 | -- SET UP MODEL: --------------------------------------------------------------
13 |
14 | -- model hyper-parameters
15 | batchSize = 10
16 | rho = 4 -- sequence length
17 | hiddenSize = 10
18 | nIndex = 2 -- input words
19 | nClass = 2 -- output classes
20 | lr = 0.1
21 |
22 |
23 | -- build simple recurrent neural network
24 | r = nn.Recurrent(
25 | hiddenSize, nn.Identity(),
26 | nn.Linear(hiddenSize, hiddenSize), nn.Sigmoid(),
27 | rho
28 | )
29 |
30 | rnn = nn.Sequential()
31 | :add(nn.LookupTable(nIndex, hiddenSize))
32 | :add(nn.SplitTable(1,2))
33 | :add(nn.Sequencer(r))
34 | :add(nn.SelectTable(-1)) -- this selects the last time-step of the rnn output sequence
35 | :add(nn.Linear(hiddenSize, nClass))
36 | :add(nn.LogSoftMax())
37 |
38 | -- build criterion
39 | criterion = nn.ClassNLLCriterion()
40 |
41 | -- build input
42 | -- create a sequence of 2 numbers: {2, 1, 2, 2, 1, 1, 2, 2, 1 ..}
43 | ds = {}
44 | ds.size = 1000
45 | local target_seq = torch.LongTensor({1,2,2,1})
46 | ds.input = torch.LongTensor(ds.size,rho):random(nClass)
47 | ds.target = torch.LongTensor(ds.size):fill(1)
48 | -- initialize targets:
49 | local indices = torch.LongTensor(rho)
50 | for i=1, ds.size do
51 | if torch.sum(torch.abs(torch.add(ds.input[i], -target_seq))) == 0 then ds.target[i] = 2 end
52 | end
53 |
54 | indices:resize(batchSize)
55 |
56 | -- training
57 | local inputs, targets = torch.LongTensor(), torch.LongTensor()
58 | for iteration = 1, 10000 do
59 | -- 1. create a sequence of rho time-steps
60 | indices:random(1,ds.size) -- choose some random samples
61 | inputs:index(ds.input, 1,indices)
62 | targets:index(ds.target, 1,indices)
63 | -- print(inputs, targets)
64 |
65 | -- 2. forward sequence through rnn
66 | rnn:zeroGradParameters()
67 |
68 | local outputs = rnn:forward(inputs)
69 | local err = criterion:forward(outputs, targets)
70 |
71 | if iteration%10 == 0 then print(string.format("Iteration %d ; NLL err = %f ", iteration, err)) end
72 |
73 | -- 3. backward sequence through rnn (i.e. backprop through time)
74 | local gradOutputs = criterion:backward(outputs, targets)
75 | local gradInputs = rnn:backward(inputs, gradOutputs)
76 |
77 | -- 4. update
78 | rnn:updateParameters(lr)
79 | end
80 |
81 | -- testing:
82 | for iteration = 1, 10 do
83 | print('\n\nTESTING, iteration:', iteration)
84 | indices:random(1,ds.size) -- choose some random samples
85 | inputs:index(ds.input, 1,indices)
86 | targets:index(ds.target, 1,indices)
87 |
88 | local outputs = rnn:forward(inputs)
89 | max,idx = torch.max(outputs, 2)
90 | print('inputs:', inputs, 'targets:', targets, 'results:', idx)
91 | end
92 |
93 |
94 |
95 |
--------------------------------------------------------------------------------
/_xLearn-demos/README-train:
--------------------------------------------------------------------------------
1 | The example of training is in ext-exlern/demos
2 | folder in the file called train-on-faces.lua .
3 |
4 | In this example we are training a network on the dataset of faces we have
5 | 2 classes: faces and background.
6 |
7 | To adopt this example to your case you basically need to take care of 2 things:
8 | 1) Giving the correct path to your dataset of images
9 | 2) Setting the correct parameters for the network
10 |
11 | For (1) look at the line 79 in train-on-faces.lua file. We create 2 objects of type
12 | DataSet class called dataFace and dataBG. Like this:
13 |
14 | dataBG = DataSet{dataSetFolder=datasetPath..'bg', chanels=1,
15 | cacheFile=datasetPath..'bg'}
16 |
17 | here instead of datasetPath..'bg' you would give your path.
18 | We assume here that all the images of faces are stored in one folder and
19 | all images of background are sored in another folder.
20 |
21 |
22 |
23 | As for the (2) - the network parameters look at line 45.
24 | You should probably start by using the same layers that we are using,
25 | but you will have to change the parameters so that it would fit
26 | your initial image size.
27 | In our case the images in the dataset are 32x32.
28 | The idea is set the sizes of the layers to that you will g
29 | et to 1x1 in the end, so we do:
30 | 32x32 -- conv(5,5) --> 28x28 -- subsample(4,4) --> 7x7 -- conv(7,7) --> 1x1
31 |
32 |
33 | As for normalizing the images, it is now a part of the network,
34 | the very first layer called: nn.LocalNorm() on line 46
35 | in train-on-faces.lua file.
--------------------------------------------------------------------------------
/_xLearn-demos/elab_logo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/_xLearn-demos/elab_logo.gif
--------------------------------------------------------------------------------
/_xLearn-demos/live-kinect.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ----------------------------------------------------------------------
3 | -- WHAT: segments a image source.
4 | -- the image source must be provided on the command line.
5 | --
6 | require 'XLearn'
7 | require 'os'
8 | require 'mstsegm'
9 | require 'mincut'
10 | require 'kinect'
11 |
12 | -- parse args
13 | op = OptionParser('%prog -s SOURCE [options]')
14 | op:add_option{'-s', '--source', action='store', dest='source',
15 | help='image source, can be one of: camera | lena | video'}
16 | op:add_option{'-c', '--camera', action='store', dest='camidx',
17 | help='if source=camera, you can specify the camera index: /dev/videoIDX [default=0]'}
18 | op:add_option{'-p', '--path', action='store', dest='path',
19 | help='path to video'}
20 | options,args = op:parse_args()
21 |
22 | -- setup QT gui
23 | toolBox.useQT()
24 | widget = qtuiloader.load('live-kinect.ui')
25 | painter = qt.QtLuaPainter(widget.frame)
26 |
27 | -- video source
28 | -- source = nn.ImageSource{type = options.source or 'camera',
29 | -- path = options.path,
30 | -- cam_idx = options.camidx,
31 | -- fps = 20,
32 | -- width = 200,
33 | -- height = 150}
34 |
35 | -- displayers
36 | displayer_source = Displayer()
37 | displayer_depth = Displayer()
38 | displayer_segments = Displayer()
39 |
40 | -- global zoom
41 | zoom = 1
42 | kinect = kinect.Device(640,480)
43 | -- incremental segmentation
44 | do
45 | local nslices = 8
46 | local segmbuf
47 | local ringbuf = {}
48 | local ringbuf_i = 0
49 |
50 | -- incremental segm function
51 | function segm(img)
52 | -- new frame
53 | ringbuf_i = ringbuf_i + 1
54 |
55 | -- resize buffer
56 | segmbuf = segmbuf or torch.Tensor(img:size(1), img:size(2), img:size(3), nslices)
57 | -- store frame
58 | ringbuf[ringbuf_i] = torch.Tensor():resizeAs(img):copy(img)
59 |
60 | -- cleanup
61 | ringbuf[ringbuf_i-nslices] = nil
62 | collectgarbage()
63 |
64 | -- compute segm
65 | local segments
66 | if ringbuf_i > nslices then
67 | -- fill buffer
68 | for i = 1,nslices do
69 | segmbuf:select(4,i):copy(ringbuf[ringbuf_i-nslices+i])
70 | end
71 | -- segm
72 | segments = mstsegm.infer{image=segmbuf,
73 | connex=4,
74 | k=k, min=minsize,
75 | incremental=true}
76 | -- return last slice
77 | return segments:select(4,segments:size(4))
78 | else
79 | -- in the mean time, dont do anything
80 | return img
81 | end
82 | end
83 | end
84 |
85 | -- exec
86 |
87 | input = torch.Tensor(320,240,4)
88 | result = torch.Tensor()
89 | function run()
90 | -- get stream
91 | kinect:getRGBD()
92 | kinect:tilt(widget.verticalSlider2.value)
93 | image.scale(kinect.rgbd,input,'simple')
94 | frame = input:narrow(3,1,3)
95 | thre = widget.verticalSlider_1.value
96 |
97 | mincut.segmentation{image = input,
98 | result=result,
99 | threshold=thre}
100 |
101 | painter:gbegin()
102 | painter:showpage()
103 |
104 | displayer_source:show{tensor = frame, painter = painter, globalzoom=zoom,
105 | min=0,max=1, offset_x=0, offset_y=10,
106 | legend='camera image'}
107 | displayer_depth:show{tensor = input:select(3,4), painter = painter, globalzoom=zoom,
108 | min = 0, max = 1,
109 | offset_x = frame:size(1)+5, offset_y = 10,
110 | legend = 'depth image'}
111 | displayer_segments:show{tensor = result, painter = painter, globalzoom=zoom,
112 | min = 0, max = 1,
113 | offset_x = 0, offset_y = frame:size(2) + 20,
114 | legend = 'segmented image'}
115 |
116 | -- and params
117 | painter:setfont(qt.QFont{serif=false,italic=false,size=14})
118 | painter:moveto(330, 460); painter:show('Threshold = ' .. thre)
119 | painter:gend()
120 | end
121 |
122 | -- Loop Process
123 | local timer = qt.QTimer()
124 | timer.interval = 5
125 | timer.singleShot = true
126 | timer:start()
127 | qt.connect(timer, 'timeout()', function() run() timer:start() end)
128 |
129 | -- Start Process
130 | widget.windowTitle = "Live Segmentation on Kinect"
131 | widget:show()
132 |
--------------------------------------------------------------------------------
/_xLearn-demos/live-kinect.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 1000
10 | 700
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | 255
20 | 255
21 | 255
22 |
23 |
24 |
25 |
26 |
27 |
28 | 236
29 | 236
30 | 236
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | 255
40 | 255
41 | 255
42 |
43 |
44 |
45 |
46 |
47 |
48 | 229
49 | 229
50 | 229
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 | 229
60 | 229
61 | 229
62 |
63 |
64 |
65 |
66 |
67 |
68 | 229
69 | 229
70 | 229
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Form
79 |
80 |
81 | -
82 |
83 |
84 | 2047
85 |
86 |
87 | 50
88 |
89 |
90 | 250
91 |
92 |
93 | Qt::Vertical
94 |
95 |
96 |
97 | -
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 | 0
106 | 0
107 | 0
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 | 0
117 | 0
118 | 0
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 | 185
128 | 185
129 | 185
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 | QFrame::StyledPanel
138 |
139 |
140 | QFrame::Raised
141 |
142 |
143 |
144 |
145 | 910
146 | 530
147 | 22
148 | 111
149 |
150 |
151 |
152 | -30
153 |
154 |
155 | 30
156 |
157 |
158 | Qt::Vertical
159 |
160 |
161 | verticalSlider_1
162 | verticalSlider2
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
--------------------------------------------------------------------------------
/_xLearn-demos/live-segm.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ----------------------------------------------------------------------
3 | -- WHAT: segments a image source.
4 | -- the image source must be provided on the command line.
5 | --
6 | require 'XLearn'
7 | require 'os'
8 | require 'mstsegm'
9 |
10 | -- parse args
11 | op = OptionParser('%prog -s SOURCE [options]')
12 | op:add_option{'-s', '--source', action='store', dest='source',
13 | help='image source, can be one of: camera | lena | video'}
14 | op:add_option{'-c', '--camera', action='store', dest='camidx',
15 | help='if source=camera, you can specify the camera index: /dev/videoIDX [default=0]'}
16 | op:add_option{'-p', '--path', action='store', dest='path',
17 | help='path to video'}
18 | options,args = op:parse_args()
19 |
20 | -- setup QT gui
21 | toolBox.useQT()
22 | widget = qtuiloader.load('live-segm.ui')
23 | painter = qt.QtLuaPainter(widget.frame)
24 |
25 | -- video source
26 | source = nn.ImageSource{type = options.source or 'camera',
27 | path = options.path,
28 | cam_idx = options.camidx,
29 | fps = 20,
30 | width = 200,
31 | height = 150}
32 |
33 | -- displayers
34 | displayer_source = Displayer()
35 | displayer_segments = Displayer()
36 |
37 | -- global zoom
38 | zoom = 2
39 |
40 | -- profiler
41 | profiler = Profiler()
42 |
43 | -- incremental segmentation
44 | do
45 | local nslices = 8
46 | local segmbuf
47 | local ringbuf = {}
48 | local ringbuf_i = 0
49 |
50 | -- incremental segm function
51 | function segm(img)
52 | -- new frame
53 | ringbuf_i = ringbuf_i + 1
54 |
55 | -- resize buffer
56 | segmbuf = segmbuf or torch.Tensor(img:size(1), img:size(2), img:size(3), nslices)
57 | -- store frame
58 | ringbuf[ringbuf_i] = torch.Tensor():resizeAs(img):copy(img)
59 |
60 | -- cleanup
61 | ringbuf[ringbuf_i-nslices] = nil
62 | collectgarbage()
63 |
64 | -- compute segm
65 | local segments
66 | if ringbuf_i > nslices then
67 | -- fill buffer
68 | for i = 1,nslices do
69 | segmbuf:select(4,i):copy(ringbuf[ringbuf_i-nslices+i])
70 | end
71 | -- segm
72 | segments = mstsegm.infer{image=segmbuf,
73 | connex=4,
74 | k=k, min=minsize,
75 | incremental=true}
76 | -- return last slice
77 | return segments:select(4,segments:size(4))
78 | else
79 | -- in the mean time, dont do anything
80 | return img
81 | end
82 | end
83 | end
84 |
85 | -- exec
86 | function run()
87 |
88 | profiler:start('global', 'fps')
89 | profiler:start('get-frame')
90 | frame = source:forward()
91 | profiler:lap('get-frame')
92 |
93 | profiler:start('get-segments')
94 | segments = segm(frame)
95 | profiler:lap('get-segments')
96 |
97 | painter:gbegin()
98 | painter:showpage()
99 | profiler:start('display')
100 |
101 | displayer_source:show{tensor = frame:add(segments), painter = painter, globalzoom=zoom,
102 | min=0,max=2, offset_x=0, offset_y=10,
103 | legend='camera image'}
104 |
105 | displayer_segments:show{tensor = segments, painter = painter, globalzoom=zoom,
106 | min = 0, max = 1,
107 | offset_x = frame:size(1), offset_y = 10,
108 | legend = 'segmented image'}
109 |
110 | profiler:lap('display')
111 | profiler:lap('global')
112 |
113 | --disp times
114 | profiler:displayAll{painter = painter, x = 10, y = 420, zoom=1/2}
115 |
116 | -- and params
117 | minsize = widget.verticalSlider_1.value
118 | k = widget.verticalSlider_2.value
119 | painter:setfont(qt.QFont{serif=false,italic=false,size=14})
120 | painter:moveto(500, 420); painter:show('k = ' .. k)
121 | painter:moveto(500, 440); painter:show('min = ' .. minsize)
122 |
123 | painter:gend()
124 | end
125 |
126 | -- Loop Process
127 | local timer = qt.QTimer()
128 | timer.interval = 5
129 | timer.singleShot = true
130 | timer:start()
131 | qt.connect(timer, 'timeout()', function() run() timer:start() end)
132 |
133 | -- Start Process
134 | widget.windowTitle = "Live Segm"
135 | widget:show()
136 |
--------------------------------------------------------------------------------
/_xLearn-demos/live-segm.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 1000
10 | 700
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | 255
20 | 255
21 | 255
22 |
23 |
24 |
25 |
26 |
27 |
28 | 236
29 | 236
30 | 236
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | 255
40 | 255
41 | 255
42 |
43 |
44 |
45 |
46 |
47 |
48 | 229
49 | 229
50 | 229
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 | 229
60 | 229
61 | 229
62 |
63 |
64 |
65 |
66 |
67 |
68 | 229
69 | 229
70 | 229
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Form
79 |
80 |
81 | -
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 | 0
90 | 0
91 | 0
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | 0
101 | 0
102 | 0
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 | 185
112 | 185
113 | 185
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 | QFrame::StyledPanel
122 |
123 |
124 | QFrame::Raised
125 |
126 |
127 |
128 | -
129 |
130 |
131 | 100
132 |
133 |
134 | 0
135 |
136 |
137 | Qt::Vertical
138 |
139 |
140 |
141 | -
142 |
143 |
144 | 10000
145 |
146 |
147 | 50
148 |
149 |
150 | 2000
151 |
152 |
153 | Qt::Vertical
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
--------------------------------------------------------------------------------
/_xLearn-demos/live-sift.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ----------------------------------------------------------------------
3 | -- WHAT: computes and finds SIFT keypoints/descriptors
4 | -- the image source must be provided on the command line
5 | --
6 | require 'XLearn'
7 | require 'os'
8 | require 'vlfeat'
9 |
10 | -- parse args
11 | op = OptionParser('%prog -s SOURCE [options]')
12 | op:add_option{'-s', '--source', action='store', dest='source',
13 | help='image source, can be one of: camera | lena | video'}
14 | op:add_option{'-c', '--camera', action='store', dest='camidx',
15 | help='if source=camera, you can specify the camera index: /dev/videoIDX [default=0]'}
16 | op:add_option{'-p', '--path', action='store', dest='path',
17 | help='path to video'}
18 | options,args = op:parse_args()
19 |
20 | -- setup QT gui
21 | toolBox.useQT()
22 | widget = qtuiloader.load('live-sift.ui')
23 | painter = qt.QtLuaPainter(widget.frame)
24 |
25 | -- params
26 | width = 320
27 | height = 180
28 |
29 | -- video source
30 | source = nn.ImageSource{type = options.source or 'camera',
31 | path = options.path,
32 | cam_idx = options.camidx,
33 | fps = 20,
34 | width = width,
35 | height = height}
36 |
37 | -- displayers
38 | displayer_source = Displayer()
39 | displayer_matches = Displayer()
40 |
41 | -- global zoom
42 | zoom = 1
43 |
44 | -- profiler
45 | profiler = Profiler()
46 |
47 | -- exec
48 | function run()
49 |
50 | -- get live params
51 | profiler:start('global', 'fps')
52 | pk = widget.verticalSlider_1.value
53 | k = (widget.verticalSlider_2.value)/10 + 1
54 |
55 | profiler:start('get-frame')
56 | frame = source:forward()
57 | profiler:lap('get-frame')
58 |
59 | profiler:start('compute-sift')
60 | if sift_frames then
61 | if not sift_frames_prev then
62 | sift_frames_prev = torch.Tensor()
63 | sift_descs_prev = torch.Tensor()
64 | frame_prev = torch.Tensor()
65 | end
66 | frame_prev:resizeAs(frame):copy(frame)
67 | sift_frames_prev:resizeAs(sift_frames):copy(sift_frames)
68 | sift_descs_prev:resizeAs(sift_descs):copy(sift_descs)
69 | end
70 | sift_frames, sift_descs = vlfeat.sift{image=frame:select(3,2), edgeThresh=k, peakThresh=pk}
71 | if sift_frames_prev then
72 | matches = vlfeat.sift_match{descs1=sift_descs, descs2=sift_descs_prev, threshold=5}
73 | end
74 | profiler:lap('compute-sift')
75 |
76 | profiler:start('render-sift')
77 | sift_render = sift_render or torch.Tensor(1,1)
78 | vlfeat.sift_display{image=frame, frames=sift_frames, descriptors=descriptors,
79 | totensor=sift_render}
80 | if matches then
81 | sift_match_render = sift_match_render or torch.Tensor()
82 | vlfeat.sift_match_display{images={frame, frame_prev},
83 | matches=matches,
84 | frames={sift_frames, sift_frames_prev},
85 | totensor=sift_match_render}
86 | end
87 | profiler:lap('render-sift')
88 |
89 | -- display main image
90 | profiler:start('display')
91 | painter:gbegin()
92 | painter:showpage()
93 | displayer_source:show{tensor=sift_render, painter=painter, globalzoom=zoom,
94 | min=0, max=1, offset_x=0, offset_y=20,
95 | legend='SIFT features'}
96 | if sift_match_render then
97 | displayer_matches:show{tensor=sift_match_render, painter=painter, globalzoom=zoom,
98 | min=0, max=1, offset_x=0, offset_y=40+sift_render:size(2),
99 | legend='SIFT matches'}
100 | end
101 | profiler:lap('display')
102 |
103 | -- and params
104 | painter:setfont(qt.QFont{family='Papyrus',serif=false,bold=true,italic=false,size=12})
105 | painter:moveto(500, frame:size(2)*2*zoom + 80); painter:show('edgeThresh = ' .. k)
106 | painter:moveto(500, frame:size(2)*2*zoom + 100); painter:show('peakThresh = ' .. pk)
107 |
108 | --disp times
109 | profiler:lap('global')
110 | profiler:displayAll{painter=painter,
111 | x=10, y=frame:size(2)*2*zoom+80,
112 | zoom=1/2}
113 | painter:gend()
114 |
115 | -- collect
116 | collectgarbage()
117 | end
118 |
119 | -- Loop Process
120 | local timer = qt.QTimer()
121 | timer.interval = 5
122 | timer.singleShot = true
123 | timer:start()
124 | qt.connect(timer, 'timeout()', function() run() timer:start() end)
125 |
126 | -- Start Process
127 | widget.windowTitle = "Live SIFT Descriptors"
128 | widget:show()
129 |
--------------------------------------------------------------------------------
/_xLearn-demos/live-sift.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 1000
10 | 700
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | 255
20 | 255
21 | 255
22 |
23 |
24 |
25 |
26 |
27 |
28 | 236
29 | 236
30 | 236
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | 255
40 | 255
41 | 255
42 |
43 |
44 |
45 |
46 |
47 |
48 | 229
49 | 229
50 | 229
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 | 229
60 | 229
61 | 229
62 |
63 |
64 |
65 |
66 |
67 |
68 | 229
69 | 229
70 | 229
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Form
79 |
80 |
81 | -
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 | 0
90 | 0
91 | 0
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | 0
101 | 0
102 | 0
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 | 185
112 | 185
113 | 185
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 | QFrame::StyledPanel
122 |
123 |
124 | QFrame::Raised
125 |
126 |
127 |
128 | -
129 |
130 |
131 | 100
132 |
133 |
134 | 50
135 |
136 |
137 | Qt::Vertical
138 |
139 |
140 |
141 | -
142 |
143 |
144 | 100
145 |
146 |
147 | 0
148 |
149 |
150 | 3
151 |
152 |
153 | Qt::Vertical
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
--------------------------------------------------------------------------------
/_xLearn-demos/multithreaded.lua:
--------------------------------------------------------------------------------
1 | SIZE = 128
2 |
3 | local thread = require("thread")
4 | local queue = require("thread.queue")
5 |
6 | local fifo = queue.newqueue(SIZE)
7 | local output = thread.newmutex()
8 |
9 | function newcount()
10 | local mutex = thread.newmutex()
11 | local value = 1
12 | return function ()
13 | mutex:lock()
14 | local v = value
15 | value = value + 1
16 | mutex:unlock()
17 | return v
18 | end
19 | end
20 |
21 | local count = newcount()
22 |
23 | function consumer(fifo, output)
24 | while 1 do
25 | local value = fifo:remove()
26 | output:lock()
27 | io.write("consumer removed ", value, "\n")
28 | output:unlock()
29 | end
30 | end
31 |
32 | function producer(count, fifo, output)
33 | while 1 do
34 | local value = count()
35 | fifo:insert(value)
36 | output:lock()
37 | io.write("producer inserted ", value, "\n")
38 | output:unlock()
39 | end
40 | end
41 |
42 | -- run consumer function in a new thread
43 | thread.newthread(consumer, {fifo, output})
44 |
45 | -- run producer function in the main thread
46 | producer(count, fifo, output)
47 |
48 |
--------------------------------------------------------------------------------
/_xLearn-demos/temporal-diff.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 |
3 | -- e. culurciello
4 | -- temporal difference simulator
5 |
6 | require 'XLearn'
7 | require 'thread'
8 | require 'os'
9 | local queue = require 'thread.queue'
10 |
11 | -- need QT
12 | toolBox.useQT()
13 |
14 | -- resource file
15 | local ui = paths.thisfile('face-detect.ui')
16 |
17 | -- Camera frames
18 | local camera = nn.ImageSource('camera')
19 | local camFrame = torch.Tensor(640,480,3)
20 | local frameY = torch.Tensor(640,480,1)
21 | local frameY2 = torch.Tensor(640,480,1)
22 | local frameTD = torch.Tensor(640,480,1)
23 | --local frameS = torch.Tensor(640,480,1)
24 | local widget = qtuiloader.load(ui)
25 | local painter = qt.QtLuaPainter(widget.frame)
26 |
27 | local vslide = 0
28 | local hslide = 0
29 |
30 | -- first frame
31 | camFrame = camera:forward()
32 | image.rgb2y(camFrame, frameY)
33 |
34 | -- sobel
35 | local sobel = lab.new({1,1,1},{1,-9,1},{1,1,1})
36 |
37 | -- global results
38 | local detectorIsOn
39 | local ringBuffer = {}
40 | local ringBufferSize = 2
41 | local ringBufferP = -1
42 | local resultsReady = false
43 | local results = {}
44 | local speeds
45 |
46 | -- multithread mutex
47 | local mutexInput = thread.newmutex()
48 | local mutexResults = thread.newmutex()
49 | local fifo = queue.newqueue(SIZE)
50 |
51 | -- time
52 | local timeDisplay = os.clock()
53 | local timeDetect = os.clock()
54 | local timeUpdates = os.clock()
55 |
56 | -- displayers
57 | local disp_sob = Displayer()
58 | local disp_in = Displayer()
59 | local disp_temp = Displayer()
60 |
61 | -- display
62 | local function displayer(mutexInput, mutexResults)
63 | -- get another frame
64 | camFrame = camera:forward()
65 | image.rgb2y(camFrame, frameY2)
66 |
67 | -- temp diff w/ threshold
68 | frameTD:copy(frameY):mul(-1):add(frameY2)
69 | local thE = vslide/150
70 | image.threshold(frameTD, thE)
71 |
72 | --sobel operator
73 | frameS = image.convolve(frameY, sobel, 'valid')
74 |
75 | -- time
76 | local diff = os.clock() - timeDisplay
77 | timeDisplay = os.clock()
78 | local fps = 1/diff
79 |
80 | -- paint
81 | painter:gbegin()
82 | painter:showpage()
83 |
84 | -- disp cam image
85 | local zoom = 1/hslide
86 | disp_in:show{tensor=frameY, painter=painter,
87 | globalzoom=zoom, offset_x=0, offset_y=0}
88 |
89 | -- disp temp diff image
90 | disp_temp:show{tensor=frameTD, painter=painter,
91 | globalzoom=zoom, offset_x=0, offset_y=frameY:size(2)}
92 |
93 | -- disp sobel image
94 | disp_sob:show{tensor=frameS, painter=painter,
95 | globalzoom=zoom, offset_x=frameY:size(1), offset_y=0}
96 |
97 | -- disp FPS
98 | painter:moveto(10, frameY:size(2)*2*zoom+30)
99 | painter:show(string.format('FPS = %0f', fps))
100 |
101 |
102 | -- end of paint
103 | painter:grestore()
104 | painter:gend()
105 |
106 | -- keep current frame
107 | frameY:copy(frameY2)
108 |
109 | -- collect garbage
110 | collectgarbage()
111 | end
112 |
113 |
114 | -- Create QT events
115 | local timer = qt.QTimer()
116 | timer.interval = 10
117 | timer.singleShot = true
118 | qt.connect(timer,'timeout()',
119 | function()
120 | vslide = widget.verticalSlider.value
121 | hslide = widget.horizontalSlider.value
122 | detectorIsOn = widget.checkBox3.checked
123 | displayer(mutexInput, mutexResults)
124 | timer:start()
125 | end )
126 |
127 | -- Close Process
128 | local listener = qt.QtLuaListener(widget)
129 | qt.connect(listener,'sigClose()',
130 | function()
131 | camera:stop()
132 | timer:stop()
133 | timer:deleteLater()
134 | widget:deleteLater()
135 | end )
136 | qt.connect(listener,'sigShow(bool)',
137 | function(b)
138 | if b then timer:start() end
139 | end )
140 |
141 | widget.windowTitle = "Temporal Difference Imaging"
142 | widget:show()
143 |
144 |
--------------------------------------------------------------------------------
/_xLearn-demos/test-cam.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ----------------------------------------------------------------------
3 | -- WHAT: executes a pretrained neural net on an image source.
4 | -- the image source must be provided on the command line.
5 | --
6 | require 'XLearn'
7 | require 'os'
8 |
9 | -- parse args
10 | op = OptionParser('%prog -s SOURCE [options]')
11 | op:add_option{'-t', '--type', action='store', dest='type',
12 | help='optional type, depends on the kind of source. for camera: opencv | camiface | v4linux'}
13 | op:add_option{'-c', '--camera', action='store', dest='camidx',
14 | help='if source=camera, you can specify the camera index: /dev/videoIDX [default=0]'}
15 | options,args = op:parse_args()
16 |
17 | -- setup QT gui
18 | toolBox.useQT()
19 | painter = qtwidget.newwindow(800,600)
20 |
21 | -- video source
22 | source = nn.ImageSource('camera', options.type, options.camidx)
23 |
24 | -- displayers to hold print buffers
25 | displayer_source = Displayer()
26 | displayer_chanels = Displayer()
27 |
28 | -- global zoom
29 | zoom = 2/3
30 |
31 | -- profiler
32 | profiler = Profiler()
33 |
34 | local function loop()
35 | profiler:start('global','fps')
36 |
37 | profiler:start('get-frame')
38 | frame = source:forward()
39 | profiler:lap('get-frame')
40 |
41 | --frame = image.yuv2rgb(frame)
42 |
43 | painter:gbegin()
44 | painter:showpage()
45 | profiler:start('display')
46 |
47 | -- image from cam
48 | displayer_source:show{tensor=frame, painter=painter,
49 | globalzoom=zoom,
50 | min=0, max=1,
51 | inplace=true,
52 | offset_x=0, offset_y=0,
53 | legend='From Camera'}
54 |
55 | profiler:lap('display')
56 | profiler:lap('global')
57 |
58 | -- disp times
59 | profiler:displayAll{painter=painter, x=10, y=frame:size(2)*zoom+100, zoom=1/2}
60 | painter:gend()
61 | end
62 |
63 | -- Loop Process
64 | local timer = qt.QTimer()
65 | timer.interval = 1
66 | timer.singleShot = true
67 | timer:start()
68 | qt.connect(timer,'timeout()', function() loop() timer:start() end)
69 |
--------------------------------------------------------------------------------
/_xLearn-demos/test-opticalFlow.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 |
3 | ------------------------------------------------------------
4 | -- computes the optical flow-field of a pair of images
5 | --
6 |
7 | require 'XLearn'
8 | opencv = xrequire 'opencv'
9 | oflow = xrequire 'opticalFlow'
10 |
11 | if opencv then
12 | opencv.test_calcOpticalFlow()
13 | else
14 | print('please install opencv wrapper to see more (make USE_OPENCV=1)')
15 | end
16 |
17 | if oflow then
18 |
19 | oflow.testme()
20 |
21 | print([[
22 |
23 | To know more about these algos, start a Lua shell, and try:
24 | > require 'opticalFlow'
25 | > img1 = image.load('/path/to/img1.jpg')
26 | > img2 = image.load('/path/to/img2.jpg')
27 | > opticalFlow.infer()
28 | ... prints some online help on the function ...
29 | > flow_x, flow_y = opticalFlow.infer{pair={img1,img2}}
30 | > image.displayList{images={flow_x,flow_y}}
31 | ... the flow can be displayed in HSL space ...
32 | > hsl = opticalFlow.field2rgb(flow_x, flow_y)
33 | > image.display(hsl)
34 | ]])
35 | else
36 | print('please install 3rd party packages to see more (make USE_3RDPARTY=1)')
37 | end
38 |
39 |
--------------------------------------------------------------------------------
/_xLearn-demos/test-segmentation.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 |
3 | ------------------------------------------------------------
4 | -- tests a few segmentation algorithms available
5 | -- in the repo
6 | --
7 |
8 | require 'XLearn'
9 | mstsegm = xrequire 'mstsegm'
10 | powerwatersegm = xrequire 'powerwatersegm'
11 |
12 | if not mstsegm or not powerwatersegm then
13 | error('please install 3rd party packages (make USE_3RDPARTY=1)')
14 | end
15 |
16 | mstsegm.testme()
17 | powerwatersegm.testme()
18 |
19 | print([[
20 |
21 | To know more about these algos, start a Lua shell, and try:
22 | > require 'mstsegm'
23 | > img = image.load('/path/to/image.jpg')
24 | > mstsegm.infer()
25 | ... prints some online help on the function ...
26 | > result = mstsegm.infer{image=img}
27 | > image.display(result)
28 | > require 'powerwatersegm'
29 | > powerwatersegm.infer()
30 | ]])
31 |
--------------------------------------------------------------------------------
/_xLearn-demos/test-stereo.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 |
3 | ------------------------------------------------------------
4 | -- computes disparities from a pair of stereo images
5 | --
6 |
7 | require 'XLearn'
8 | stereo = xrequire 'stereo'
9 |
10 | if not stereo then
11 | error('please install stereo package (make USE_3RDPARTY=1)')
12 | end
13 |
14 | stereo.testme()
15 |
16 | print([[
17 |
18 | To know more about these algos, start a Lua shell, and try:
19 | > require 'stereo'
20 | > left = image.load('/path/to/left.jpg')
21 | > right = image.load('/path/to/right.jpg')
22 | > stereo.infer()
23 | ... prints some online help on the function ...
24 | > result = stereo.infer{left=left, right=right}
25 | > image.display(result)
26 | ]])
27 |
--------------------------------------------------------------------------------
/_xLearn-demos/trainAutoencoder.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env lua
2 | --------------------------------------------------------------------------------
3 | -- A simple autoencoder to learn sparse coding, using
4 | -- AutoEncoderTrainer
5 | --
6 | -- (related info : http://www.scholarpedia.org/article/Sparse_coding)
7 | --
8 | -- Authors: Benoit Corda, C. Farabet
9 | --------------------------------------------------------------------------------
10 | require 'XLearn'
11 |
12 | -- Option parsing
13 | op = OptionParser('trainAutoencoder [options]')
14 | op:add_option{'-p', '--path', action='store', dest='path', help='path to train set. should contain pngs.'}
15 |
16 | options,args = op:parse_args()
17 |
18 | if (options.path == nil) then
19 | op:print_help()
20 | op:fail('please provide path')
21 | end
22 |
23 | -------------------------------------------
24 | -- params
25 | -------------------------------------------
26 | winX = 9
27 | winY = 9
28 | woutX = 8
29 | woutY = 8
30 | overcompleteX = 1
31 | overcompleteY = 1
32 | goodForTrainingSizeX = 3*(winX+woutX-1) + 2*(winX-1)
33 | goodForTrainingSizeY = 3*(winY+woutY-1) + 2*(winY-1)
34 | autoencoderFileName = 'autoencoderTrained.torch'
35 | -- code rates
36 | maxiterCode = 40
37 | alphaCode = 0.4
38 | codeLearningRate = 0.005
39 | -- encoder rates
40 | alphaEncoder = 1
41 | encoderLearningRate = 0.01
42 | encoderLearningDecay = 0.01
43 | alphaEncoder = 1
44 | -- decoder rates
45 | alphaDecoder = 1
46 | decoderLearningRate = 0.01
47 | decoderLearningDecay = 0.01
48 | alphaDecoder = 1
49 | -------------------------------------------
50 | -- end params
51 | -------------------------------------------
52 |
53 |
54 | -- build modules and trainer
55 | encoder = nn.Sequential()
56 | encoder:add(nn.LcEncoder(winX, winY, woutX, woutY,
57 | overcompleteX, overcompleteY))
58 | encoder:add(nn.Tanh())
59 |
60 | decoder = nn.LcDecoder(winX, winY, woutX, woutY, overcompleteX, overcompleteY)
61 |
62 | -- Loss functions
63 | codeCriterion = nn.AbsCriterion()
64 | codeCriterion.sizeAverage = false
65 | decoderCriterion = nn.MSECriterion()
66 | decoderCriterion.sizeAverage = false
67 | encoderCriterion = nn.MSECriterion()
68 | encoderCriterion.sizeAverage = false
69 | -- trainer
70 | if toolBox.fileExists(autoencoderFileName) then
71 | trainer = nn.AutoEncoderTrainer()
72 | trainer:open(autoencoderFileName)
73 | print('load successful...')
74 | else
75 | trainer = nn.AutoEncoderTrainer(encoder, decoder,
76 | codeCriterion,
77 | decoderCriterion, encoderCriterion,
78 | fileName, maxiterCode)
79 | -- set params
80 | trainer.decoderLearningRate = decoderLearningRate
81 | trainer.encoderLearningRate = encoderLearningRate
82 | trainer.codeLearningRate = codeLearningRate
83 | trainer.alphaDecoder = alphaDecoder
84 | trainer.alphaEncoder = alphaEncoder
85 | trainer.alphaCode = alphaCode
86 | trainer.fileName = autoencoderFileName
87 | -- debug display
88 | local function display(trainer, example, id)
89 | if id % 50 == 0 then
90 | trainer.decoder:showFeatures{}
91 | trainer.encoder.modules[1]:showFeatures{}
92 | end
93 | end
94 | trainer.hookExample = display
95 | end
96 |
97 |
98 | -- Loading Training Dataset
99 | trainData = UnsupDataSet(options.path,'.jpg')
100 | -- reset code target
101 | targetx = overcompleteX * (goodForTrainingSizeX-winX+1)
102 | targety = overcompleteY * (goodForTrainingSizeY-winY+1)
103 | trainer.codeTarget = torch.Tensor(targetx,targety,1):zero()
104 |
105 |
106 | -- debugger
107 | --w=gfx.Window(200,200,'Input Data')
108 | function debug(trainer, example, id)
109 | --image.display(Example:select(3,1), 2, 'Input Data', w)
110 | w = image.qtdisplay{tensor=example:select(3,1),
111 | zoom=2,
112 | legend = 'Input Data',
113 | painter = w}
114 | if id % 100 == 0 then
115 | trainer.decoder:showFeatures{}
116 | trainer.encoder.modules[1]:showFeatures{}
117 | end
118 | end
119 | trainer.hookExample = debug
120 |
121 |
122 | --saving every ite
123 | trainer.savetime = 1
124 | --training time
125 | trainer:train(trainData,goodForTrainingSizeX,goodForTrainingSizeY)
126 |
127 |
128 |
--------------------------------------------------------------------------------
/attention/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/attention/.DS_Store
--------------------------------------------------------------------------------
/demo-core/README.md:
--------------------------------------------------------------------------------
1 | # e-Lab demo-core
2 |
3 | Core scripts for the demo-visor
4 |
--------------------------------------------------------------------------------
/demo-core/framecamera.lua:
--------------------------------------------------------------------------------
1 | local frame = {}
2 | local sys = assert(require('sys'))
3 | torch.setdefaulttensortype('torch.FloatTensor')
4 |
5 | local function prep_lua_camera(opt, source)
6 |
7 | assert(require('camera'))
8 | local cam = image.Camera {
9 | idx = opt.cam,
10 | width = source.w,
11 | height = source.h,
12 | }
13 | frame.forward = function(img)
14 | return cam:forward()
15 | end
16 | source.cam = cam
17 |
18 | end
19 |
20 | local function prep_libvideo_decoder_camera(opt, source)
21 |
22 | local cam = require('libvideo_decoder')
23 | if not cam.capture('/dev/video'..opt.cam, source.w, source.h, source.fps, 3) then
24 | error('cam.capture failed')
25 | end
26 |
27 | if opt.spatial == 1 then -- In spatial mode 1 we can already get the scaled image
28 | source.w = opt.is * source.w / source.h
29 | source.h = opt.is
30 | framefunc = cam.frame_resized
31 | else
32 | framefunc = cam.frame_rgb
33 | end
34 | local img_tmp = torch.FloatTensor(3, source.h, source.w)
35 |
36 | -- set frame forward function
37 | frame.forward = function(img)
38 | if not framefunc(img_tmp) then
39 | return nil
40 | end
41 | return img_tmp
42 | end
43 | source.cam = cam
44 |
45 | end
46 |
47 | local function prep_livecam(opt, source)
48 |
49 | local cam = require 'livecam'
50 | local status = false
51 | cam.capture('/dev/video'..opt.cam, source.w, source.h, source.fps)
52 | local img_tmp = torch.FloatTensor(3, source.h, source.w)
53 |
54 | -- set frame forward function
55 | frame.forward = function(img)
56 | cam.frame_rgb(img_tmp)
57 | return img_tmp
58 | end
59 | source.cam = cam
60 |
61 | end
62 |
63 | --[[
64 | opt fields
65 | is eye size
66 | input cam0, cam1...
67 | spatial spatial mode 0, 1 or 2
68 |
69 | source fields
70 | w image width
71 | h image height
72 | --]]
73 | function frame:init(opt, source)
74 |
75 | opt.cam = tonumber(string.sub(opt.input,4,-1)) -- opt.input is in the format cam0
76 | if sys.OS == 'macos' then
77 | prep_lua_camera(opt, source)
78 | elseif opt.livecam then
79 | prep_livecam(opt, source)
80 | else
81 | prep_libvideo_decoder_camera(opt, source)
82 | end
83 |
84 | end
85 |
86 | return frame
87 |
--------------------------------------------------------------------------------
/demo-core/frameimage.lua:
--------------------------------------------------------------------------------
1 | local frame = {}
2 | torch.setdefaulttensortype('torch.FloatTensor')
3 |
4 | --[[
5 | opt fields
6 | is eye size
7 | pyramid true if pyramid mode
8 | input filename
9 | batch batch size
10 |
11 | source fields
12 | w image width
13 | h image height
14 |
15 | in pyramid mode it fills source.resolutions with the resolutions that will be returned
16 | (indexed by res_idx)
17 | --]]
18 | function frame:init(opt, source)
19 |
20 | local fi = require('fastimage')
21 | local batch = opt.batch
22 | local filenames
23 | local first = true
24 | local resolutions = nil
25 | if opt.pyramid then
26 | source.resolutions = {}
27 | local i = 1
28 | local nextsize = {source.w, source.h}
29 | repeat
30 | source.resolutions[i] = nextsize
31 | nextsize = {math.floor(nextsize[1] * 0.707 + 0.5), math.floor(nextsize[2] * 0.707 + 0.5)}
32 | i = i+1
33 | until nextsize[1] < opt.is or nextsize[2] < opt.is
34 | fi.init(opt.input, opt.batch, torch.Tensor(source.resolutions), 0.5)
35 | else
36 | fi.init(opt.input, opt.batch, 0, 0, 0.5)
37 | end
38 | source.img, filenames = fi.load(nil)
39 | if source.img then
40 | source.w = source.img:size(4)
41 | source.origw = source.w
42 | source.h = source.img:size(3)
43 | source.origh = source.h
44 | end
45 | if batch == 1 and source.img then
46 | source.img = source.img[1]
47 | filenames = filenames[1]
48 | end
49 |
50 | frame.forward = function(img, res_idx)
51 | if first then
52 | first = false
53 | return source.img, filenames
54 | else
55 | source.img, filenames = fi.load(img, res_idx)
56 | if batch == 1 and source.img then
57 | source.img = source.img[1]
58 | filenames = filenames[1]
59 | end
60 | return source.img, filenames
61 | end
62 | end
63 |
64 | end
65 |
66 | return frame
67 |
--------------------------------------------------------------------------------
/demo-core/framevideo.lua:
--------------------------------------------------------------------------------
1 | local frame = {}
2 | torch.setdefaulttensortype('torch.FloatTensor')
3 |
4 | local pf = function(...) print(string.format(...)) end
5 | local Cb = sys.COLORS.blue
6 | local Cn = sys.COLORS.none
7 |
8 | --[[
9 | opt fields
10 | is eye size
11 | pyramid true if pyramid mode
12 | input filename
13 | batch batch size
14 | loglevel if w
15 |
16 | source fields
17 | w image width
18 | h image height
19 |
20 | in pyramid mode it fills source.resolutions with the resolutions that will be returned
21 | (indexed by res_idx)
22 | --]]
23 | function frame:init(opt, source)
24 |
25 | local vd = require('libvideo_decoder')
26 | local status = false
27 | local width = source.w
28 | status, source.h, source.w, source.length, source.fps = vd.init(opt.input);
29 | source.origw = source.w
30 | source.origh = source.h
31 | if not status then
32 | error("No video")
33 | else
34 | if opt.loglevel > 0 then
35 | pf(Cb..'video statistics: %s fps, %dx%d (%s frames)'..Cn,
36 | (source.fps and tostring(source.fps) or 'unknown'),
37 | source.h,
38 | source.w,
39 | (source.length and tostring(source.length) or 'unknown'))
40 | end
41 | end
42 | if opt.pyramid then
43 | source.resolutions = {}
44 | local i = 1
45 | source.h = math.floor(width * source.h / source.w + 0.5)
46 | source.w = width
47 | local nextsize = {source.w, source.h}
48 | repeat
49 | source.resolutions[i] = nextsize
50 | nextsize = {math.floor(nextsize[1] * 0.707 + 0.5), math.floor(nextsize[2] * 0.707 + 0.5)}
51 | i = i+1
52 | until nextsize[1] < opt.is or nextsize[2] < opt.is
53 | end
54 |
55 | if opt.spatial == 1 then -- In spatial mode 1 we can already get the scaled image
56 | source.w = opt.is * source.w / source.h
57 | source.h = opt.is
58 | framefunc = vd.frame_resized
59 | else
60 | framefunc = vd.frame_rgb
61 | end
62 | local img_tmp = torch.FloatTensor(opt.batch, 3, source.h, source.w)
63 |
64 | if pyramid then
65 | frame.forward = function(img, res_idx)
66 | return vd.frame_batch_resized(opt.batch, source.resolutions[res_idx][1], source.resolutions[res_idx][2], res_idx == 1)
67 | end
68 | else
69 | -- set frame forward function
70 | frame.forward = function(img)
71 | local n = opt.batch
72 | for i=1,opt.batch do
73 | if not framefunc(img_tmp[i]) then
74 | if i == 1 then
75 | return false
76 | end
77 | n = i-1
78 | break
79 | end
80 | end
81 | if n == opt.batch then
82 | img = img_tmp
83 | else
84 | img = img_tmp:narrow(1,1,n)
85 | end
86 | return img
87 | end
88 | end
89 |
90 | end
91 |
92 | return frame
93 |
--------------------------------------------------------------------------------
/demo-core/spatial.lua:
--------------------------------------------------------------------------------
1 | local ffi = require('ffi')
2 | -- local lib8cc = ffi.load('lib8cc.so') -- connected component library
3 | ffi.cdef [[
4 | int connectedComponent(int *outputMaskPointer, int *coordinates, int coordinatesSize, int height, int width);
5 | ]]
6 | local spatial = {}
7 | local maxblobs = 10
8 | local blobdata = torch.IntTensor(maxblobs, 4):zero() -- coordinates of blobs
9 |
10 | local function makespatial(net, img)
11 |
12 | local newnet = nn.Sequential()
13 | for i = 1, #net.modules do
14 | local module = net.modules[i]
15 | local name = module.__typename
16 | if name == 'nn.Linear' then
17 | local newmodule = nn.SpatialConvolutionMM(img:size(1), module.weight:size(1), img:size(2), img:size(3))
18 | newmodule.weight:copy(module.weight):resize(newmodule.weight:size())
19 | newmodule.bias:copy(module.bias)
20 | newnet:add(newmodule)
21 | img = newmodule:forward(img)
22 | elseif name == 'nn.Sequential' then
23 | newnet:add(makespatial(module, img))
24 | img = module:forward(img)
25 | elseif name ~= 'nn.Dropout' and
26 | name ~= 'nn.LogSoftMax' and
27 | name ~= 'nn.Reshape' and
28 | name ~= 'nn.View' then
29 | newnet:add(module)
30 | img = module:forward(img)
31 | end
32 | end
33 | return newnet
34 |
35 | end
36 |
37 | local function findblobs(plane)
38 |
39 | local padded = torch.IntTensor(plane:size(1) + 2, plane:size(2) + 2):zero()
40 | padded[{{2, plane:size(1) + 1}, {2, plane:size(2) + 1}}] = plane
41 | local nblobs = lib8cc.connectedComponent(torch.data(padded), torch.data(blobdata), maxblobs, padded:size(1), padded:size(2))
42 | return nblobs > 0 and blobdata[{{1,nblobs}}] or nil
43 |
44 | end
45 |
46 | -- Take blobs of results over threshold and put them in dsl
47 | local function insertblobs(dsl, results, target, idx, sw, sh, spatial, pdt)
48 |
49 | local blobs = findblobs(results:gt(pdt))
50 | if blobs then
51 | for i = 1,blobs:size(1) do
52 | local d = {}
53 | d.x1 = blobs[i][1]
54 | d.y1 = blobs[i][2]
55 | d.x2 = blobs[i][3]
56 | d.y2 = blobs[i][4]
57 | if (d.x2 + d.y2 > 0) and (spatial==1 or spatial==2 and (d.x2-d.x1 >= 2) and (d.y2-d.y1 >= 2)) then
58 | local entry = {}
59 | entry.class = target
60 | entry.value = results:sub(d.y1,d.y2,d.x1,d.x2):max()
61 | entry.idx = idx
62 | entry.x1 = (d.x1-1)*sw
63 | entry.y1 = (d.y1-1)*sh
64 | entry.dx = (d.x2-d.x1+1)*sw
65 | entry.dy = (d.y2-d.y1+1)*sh
66 | table.insert(dsl, entry)
67 | end
68 | end
69 | end
70 |
71 | end
72 |
73 | local function prep_localize(opt, source)
74 |
75 | spatial.localize = function(results, classes, revClasses)
76 |
77 | local dsl = {}
78 | -- scaling factors
79 | local sw = source.w/results:size(3)
80 | local sh = source.h/results:size(2)
81 |
82 | if opt.targets == nil or opt.targets == '' then
83 | for k=1,#classes do
84 | insertblobs(dsl, results[k], classes[k], k, sw, sh, opt.spatial, opt.pdt)
85 | end
86 | else
87 | for k, target in ipairs(opt.targets:split(',')) do
88 | if revClasses[target] then
89 | insertblobs(dsl, results[ revClasses[target] ], target, k, sw, sh, opt.spatial, opt.pdt)
90 | end
91 | end
92 | end
93 | return dsl
94 |
95 | end -- spatial.localize
96 |
97 | end
98 |
99 | local function modify_for_detection(net)
100 |
101 | -- remove softmax if it exists
102 | if net.modules[#net.modules].__typename == 'nn.SoftMax' then
103 | net:remove(#net.modules)
104 | end
105 | -- scale the score by 0.05, to be found experimentally
106 | net:add(nn.MulConstant(0.05, true))
107 | net:add(nn.Clamp(0, 1))
108 | return net
109 |
110 | end
111 |
112 | --[[
113 |
114 | opt fields
115 | is eye size
116 | spatial spatial mode 0, 1 or 2
117 | localize if we neeed localization
118 | detection if we need detection instead of classification
119 | pdt threshold for localization
120 | targets targets to localize
121 |
122 | source fields
123 | w image width
124 | h image height
125 |
126 | --]]
127 | function spatial:init(opt, net, source)
128 |
129 | if opt.spatial > 0 then
130 | local sampleimg = torch.Tensor(3, opt.is, opt.is)
131 | net = makespatial(net, sampleimg)
132 | if opt.localize then
133 | prep_localize(opt, source)
134 | end
135 | end
136 | if opt.detection or opt.localize then
137 | net = modify_for_detection(net)
138 | end
139 | self.net = net
140 | return net
141 |
142 | end
143 |
144 | return spatial
145 |
--------------------------------------------------------------------------------
/demo-learner/ui.lua:
--------------------------------------------------------------------------------
1 |
2 | -- global ui object
3 | local ui = {}
4 |
5 | -- setup gui
6 | local timer = qt.QTimer()
7 | timer.interval = 10
8 | timer.singleShot = true
9 | timer:start()
10 | qt.connect(timer,
11 | 'timeout()',
12 | function()
13 | controls(ui)
14 | getFrame(ui)
15 | process(ui)
16 | display(ui)
17 | timer:start()
18 | end)
19 | ui.timer = timer
20 |
21 | -- connect all buttons to actions
22 | ui.classes = {widget.pushButton_1, widget.pushButton_2, widget.pushButton_3,
23 | widget.pushButton_4, widget.pushButton_5}
24 |
25 | ui.progBars = {widget.progressBar_1, widget.progressBar_2, widget.progressBar_3,
26 | widget.progressBar_4, widget.progressBar_5}
27 |
28 | ui.objLabels = {widget.label_1, widget.label_2, widget.label_3,
29 | widget.label_4, widget.label_5}
30 |
31 | -- colors
32 | ui.colors = {'blue', 'green', 'orange', 'cyan', 'purple', 'brown', 'gray', 'red', 'yellow'}
33 |
34 | -- set current class to learn
35 | ui.currentId = 1
36 | ui.currentClass = ui.classes[ui.currentId].text:tostring()
37 |
38 | function ui.resetProgBars()
39 | -- init progressBars to dist of 100:
40 | for i=1,#ui.progBars do
41 | ui.progBars[i].value = 100
42 | end
43 | end
44 |
45 | function ui.resetObjLabels()
46 | -- init progressBars to dist of 100:
47 | for i=1,#ui.objLabels do
48 | ui.objLabels[i].text = 0
49 | end
50 | end
51 |
52 | ui.resetProgBars()
53 |
54 | -- reset
55 | qt.connect(qt.QtLuaListener(widget.pushButton_forget),
56 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
57 | function (...)
58 | ui.forget = true
59 | end)
60 |
61 | -- learn new prototype
62 | ui.learn = false
63 | for i,button in ipairs(ui.classes) do
64 | qt.connect(qt.QtLuaListener(button),
65 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
66 | function (...)
67 | ui.currentId = i
68 | ui.learn = true
69 | end)
70 | end
71 |
72 | -- mouse is used only on large GUI demo:
73 | if opt.largegui then
74 | -- connect mouse pos
75 | widget.frame.mouseTracking = true
76 | qt.connect(qt.QtLuaListener(widget.frame),
77 | 'sigMouseMove(int,int,QByteArray,QByteArray)',
78 | function (x,y)
79 | ui.mouse = {x=x,y=y}
80 | end)
81 |
82 | -- issue learning request
83 | ui.learnmouse = false
84 | qt.connect(qt.QtLuaListener(widget),
85 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
86 | function (...)
87 | if ui.mouse then
88 | ui.learngui = {x=ui.mouse.x, y=ui.mouse.y, id=ui.currentId}
89 | ui.learnmouse = true
90 | end
91 | end)
92 | end
93 | -- save session
94 | ui.save = false
95 | qt.connect(qt.QtLuaListener(widget.pushButton_save),
96 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
97 | function (...)
98 | ui.save = true
99 | end)
100 |
101 | -- load session
102 | ui.load = false
103 | qt.connect(qt.QtLuaListener(widget.pushButton_load),
104 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
105 | function (...)
106 | ui.load = true
107 | end)
108 |
109 | widget.windowTitle = 'Visual Learner'
110 | widget:show()
111 |
112 | -- provide log
113 | ui.log = {}
114 | ui.logit = function(str, color) table.insert(ui.log,{str=str, color=color or 'black'}) end
115 |
116 | -- return ui
117 | return ui
118 |
--------------------------------------------------------------------------------
/demo-visor/README.md:
--------------------------------------------------------------------------------
1 | # demo vision application
2 |
3 | This demo uses demo-core to process some input data with a network.
4 | The network can be loaded in two formats:
5 |
6 | 1) A single binary torch file containing a table with these fields:
7 |
8 | - net (the network)
9 | - labels (a table with the labels of the categories of the network)
10 | - mean (a vector with three floats to be subtracted from the input, optional)
11 | - std (a vector with the three floats that divide the input, optional)
12 |
13 | 2) A directory with three files:
14 |
15 | - model.net a binary torch file containing the network
16 | - categories.txt a text file with the names of the categories; the first line
17 | is supposed to be a label and will be ignored; if a comma is present in the line,
18 | only the text till the comma will be considered
19 | - stat.t7 a binary torch file containing a table with two fields, mean and std (optional)
20 |
21 | The input can be an image, a directory filled with images, a video file or a device
22 | name in the form cam0, cam1...
23 |
--------------------------------------------------------------------------------
/dok/graphicalmodels/index.dok:
--------------------------------------------------------------------------------
1 | ====== Graphical models ======
2 | {{anchor:tutorials.graphical}}
3 |
4 | In this tutorial, we're going to learn how to define a graphical model,
5 | using the gm package, and do several things withe the model:
6 |
7 | * inference
8 | * decoding
9 | * training
10 |
--------------------------------------------------------------------------------
/dok/gui/index.dok:
--------------------------------------------------------------------------------
1 | ====== Graphical User Interface ======
2 | {{anchor:tutorials.gui}}
3 |
4 | In this tutorial, we're going to learn how to construct GUIs
5 | (Grahical User Interfaces).
6 |
--------------------------------------------------------------------------------
/dok/supervised/convnet.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/supervised/convnet.png
--------------------------------------------------------------------------------
/dok/supervised/linear_regression.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/supervised/linear_regression.png
--------------------------------------------------------------------------------
/dok/supervised/logistic_argmax.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/supervised/logistic_argmax.png
--------------------------------------------------------------------------------
/dok/supervised/logistic_regression.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/supervised/logistic_regression.png
--------------------------------------------------------------------------------
/dok/supervised/loss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/supervised/loss.png
--------------------------------------------------------------------------------
/dok/supervised/mse_loss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/supervised/mse_loss.png
--------------------------------------------------------------------------------
/dok/supervised/nll_loss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/supervised/nll_loss.png
--------------------------------------------------------------------------------
/dok/unsupervised/auto_decoder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/unsupervised/auto_decoder.png
--------------------------------------------------------------------------------
/dok/unsupervised/auto_encoder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/unsupervised/auto_encoder.png
--------------------------------------------------------------------------------
/dok/unsupervised/mse_loss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/unsupervised/mse_loss.png
--------------------------------------------------------------------------------
/dok/unsupervised/psd_encoder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/unsupervised/psd_encoder.png
--------------------------------------------------------------------------------
/dok/unsupervised/psd_loss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/unsupervised/psd_loss.png
--------------------------------------------------------------------------------
/dok/unsupervised/sparse_coding.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/unsupervised/sparse_coding.png
--------------------------------------------------------------------------------
/dok/unsupervised/sparse_coding_optim.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/dok/unsupervised/sparse_coding_optim.png
--------------------------------------------------------------------------------
/face-detector-elab/PyramidUnPacker.lua:
--------------------------------------------------------------------------------
1 |
2 | local PyramidUnPacker, parent = torch.class('nn.PyramidUnPacker', 'nn.Module')
3 |
4 | local function getSizesTbl(net)
5 | local sizes_tbl = {}
6 | for i=1,#net.modules do
7 | dw = net.modules[i].dW
8 | dh = net.modules[i].dH
9 | kw = net.modules[i].kW
10 | kh = net.modules[i].kH
11 | if((dw ~= nil)and(dh ~= nil)and(kw ~= nil) and(kh ~= nil)) then
12 | table.insert(sizes_tbl, {kw=kw,kh=kh,dw=dw,dh=dh})
13 | end
14 | end
15 |
16 | return sizes_tbl
17 | end
18 |
19 | local function getRange(args)
20 | local sizes_tbl = args.sizes_tbl
21 | local idx_output = args.idx_output
22 |
23 | local x = torch.Tensor(#sizes_tbl+1)
24 | local y = torch.Tensor(#sizes_tbl+1)
25 | x[#sizes_tbl+1] = idx_output
26 | y[#sizes_tbl+1] = idx_output
27 |
28 | for k = #sizes_tbl,1,-1 do
29 | -- rightmost point of the image that affects x(k+1)
30 | x[k] = sizes_tbl[k].kw+ (x[k+1]-1) * sizes_tbl[k].dw
31 | -- leftmost point of the image that affects y(k+1)
32 | y[k] = 1 + (y[k+1]-1) * sizes_tbl[k].dw
33 | end
34 | local left_width = y[1]
35 | local right_width = x[1]
36 |
37 | for k = #sizes_tbl,1,-1 do
38 | -- rightmost point of the image that affects x(k+1)
39 | x[k] = sizes_tbl[k].kh+ (x[k+1]-1) * sizes_tbl[k].dh
40 | -- leftmost point of the image that affects y(k+1)
41 | y[k] = 1 + (y[k+1]-1) * sizes_tbl[k].dh
42 | end
43 |
44 | local left_height = y[1]
45 | local right_height = x[1]
46 |
47 |
48 | return left_width, right_width, left_height, right_height
49 | end
50 |
51 | local function getGlobalSizes(args)
52 | local sizes_tbl = args.sizes_tbl
53 |
54 | -- to find gobal kernel size we use recursive formula:
55 | -- glob_ker(n + 1) = 1
56 | -- glob_ker(n) = ker(n) + (glob_ker(n+1)-1)*step(n)
57 | --
58 | -- where: ker(n) - kernel size on layer n, step(n) - step size on layer n
59 | -- and n is number of layers that change the size of the input (convolution and subsample)
60 | local left_width1, right_width1, left_height1, right_height1 = getRange({sizes_tbl=sizes_tbl, idx_output=1})
61 | local ker_width = right_width1 - left_width1 +1
62 | local ker_height = right_height1 - left_height1 +1
63 |
64 | local step_width = 1
65 | local step_height = 1
66 |
67 | -- global step = MUL(step_1, step_2, ... , step_n)
68 | for i = 1, #sizes_tbl do
69 | step_width = step_width * sizes_tbl[i].dw
70 | step_height = step_height * sizes_tbl[i].dh
71 | end
72 |
73 | return step_width, step_height, ker_width, ker_height
74 | end
75 |
76 | function PyramidUnPacker:__init(network)
77 | parent.__init(self)
78 |
79 | -- infer params from given net
80 | self.step_width, self.step_height, self.ker_width, self.ker_height
81 | = getGlobalSizes({sizes_tbl=getSizesTbl(network)})
82 | end
83 |
84 | function PyramidUnPacker:forward(input, coordinates)
85 | self.out_tbl = {}
86 | self.coordinates = coordinates
87 | self.step_width = 4
88 | self.step_height = 4
89 | self.ker_width = 32
90 | self.ker_height = 32
91 | for i = 1, self.coordinates:size(1) do
92 | local start_x = math.floor((self.coordinates[i][1] - 1)/self.step_width) + 1
93 | local start_y = math.floor((self.coordinates[i][2] - 1)/self.step_height) + 1
94 | local width = math.floor((self.coordinates[i][5] - self.ker_width)/self.step_width) + 1
95 | local height = math.floor((self.coordinates[i][6] - self.ker_height)/self.step_height) + 1
96 | local temp = input:narrow(3, start_x, width)
97 | temp = temp:narrow(2, start_y, height)
98 | table.insert(self.out_tbl, temp)
99 | end
100 | return self.out_tbl
101 | end
102 |
103 | function PyramidUnPacker:backward(input, gradOutput)
104 | error('backward non implemented', 'PyramidUnPacker')
105 | end
106 |
107 | function PyramidUnPacker:write(file)
108 | parent.write(self,file)
109 | file:writeDouble(#self.scales)
110 | for i = 1,#self.scales do
111 | file:writeDouble(self.scales[i])
112 | end
113 | end
114 |
115 | function PyramidUnPacker:read(file)
116 | parent.read(self,file)
117 | local nbScales = file:readDouble()
118 | for i = 1,nbScales do
119 | self.scales[i] = file:readDouble()
120 | end
121 | end
122 |
--------------------------------------------------------------------------------
/filter-bank/g.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 1307
10 | 517
11 |
12 |
13 |
14 |
15 | 0
16 | 0
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 255
26 | 255
27 | 255
28 |
29 |
30 |
31 |
32 |
33 |
34 | 236
35 | 236
36 | 236
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | 255
46 | 255
47 | 255
48 |
49 |
50 |
51 |
52 |
53 |
54 | 229
55 | 229
56 | 229
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | 229
66 | 229
67 | 229
68 |
69 |
70 |
71 |
72 |
73 |
74 | 229
75 | 229
76 | 229
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | ArrowCursor
85 |
86 |
87 | true
88 |
89 |
90 | Qt::NoFocus
91 |
92 |
93 | Form
94 |
95 |
96 | -
97 |
98 |
99 |
100 | 0
101 | 0
102 |
103 |
104 |
105 |
106 | 1280
107 | 480
108 |
109 |
110 |
111 |
112 | 1280
113 | 480
114 |
115 |
116 |
117 | QFrame::StyledPanel
118 |
119 |
120 | QFrame::Raised
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
--------------------------------------------------------------------------------
/filter-bank/run.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env torch
2 | ------------------------------------------------------------
3 | -- a simple filter bank
4 | --
5 | -- Clement Farabet
6 | --
7 |
8 | require 'xlua'
9 | require 'torch'
10 | require 'qt'
11 | require 'qtwidget'
12 | require 'qtuiloader'
13 | require 'qttorch'
14 | require 'camera'
15 | require 'nn'
16 |
17 | -- parse args
18 | op = xlua.OptionParser('%prog [options]')
19 | op:option{'-c', '--camera', action='store', dest='camidx',
20 | help='camera index: /dev/videoIDX', default=0}
21 | opt,args = op:parse()
22 |
23 | -- setup GUI (external UI file)
24 | widget = qtuiloader.load('g.ui')
25 | win = qt.QtLuaPainter(widget.frame)
26 |
27 | -- setup camera
28 | camera = image.Camera(opt.camidx)
29 |
30 | -- threads
31 | torch.setnumthreads(4)
32 |
33 | -- filters
34 | filters = nn.SpatialConvolutionMap(nn.tables.random(3,16,1),5,5)
35 |
36 | -- profiler
37 | p = xlua.Profiler()
38 |
39 | -- process function
40 | function process()
41 | -- grab frame
42 | p:start('grab','fps')
43 | frame = camera:forward()
44 | p:lap('grab')
45 |
46 | -- transform
47 | p:start('filter','fps')
48 | frames = image.scale(frame,320,240)
49 | transformed = filters(frames)
50 | p:lap('filter')
51 | end
52 |
53 | -- display function
54 | function display()
55 | p:start('display','fps')
56 | win:gbegin()
57 | win:showpage()
58 | image.display{image=frame, min=0, max=1, win=win, saturate=false}
59 | image.display{image=transformed, min=-2, max=2, nrow=4,
60 | win=win, zoom=1/2, x=frame:size(3), saturate=false}
61 | win:gend()
62 | p:lap('display')
63 | end
64 |
65 | -- setup gui
66 | local timer = qt.QTimer()
67 | timer.interval = 10
68 | timer.singleShot = true
69 | qt.connect(timer,
70 | 'timeout()',
71 | function()
72 | p:start('full loop','fps')
73 | process()
74 | display()
75 | collectgarbage()
76 | p:lap('full loop')
77 | p:printAll()
78 | timer:start()
79 | end)
80 |
81 | widget.windowTitle = 'A random 16-filter bank'
82 | widget:show()
83 | timer:start()
84 |
--------------------------------------------------------------------------------
/flow/g.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 746
10 | 504
11 |
12 |
13 |
14 |
15 | 0
16 | 0
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 255
26 | 255
27 | 255
28 |
29 |
30 |
31 |
32 |
33 |
34 | 236
35 | 236
36 | 236
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | 255
46 | 255
47 | 255
48 |
49 |
50 |
51 |
52 |
53 |
54 | 229
55 | 229
56 | 229
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | 229
66 | 229
67 | 229
68 |
69 |
70 |
71 |
72 |
73 |
74 | 229
75 | 229
76 | 229
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | Form
85 |
86 |
87 | -
88 |
89 |
90 |
91 | 640
92 | 480
93 |
94 |
95 |
96 |
97 | 640
98 | 480
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 | 0
108 | 0
109 | 0
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 | 0
119 | 0
120 | 0
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 | 185
130 | 185
131 | 185
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 | QFrame::StyledPanel
140 |
141 |
142 | QFrame::Raised
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
--------------------------------------------------------------------------------
/flow/run.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ------------------------------------------------------------
3 | -- a simple optical flow demo
4 | --
5 |
6 | require 'xlua'
7 | require 'torch'
8 | require 'qt'
9 | require 'qtwidget'
10 | require 'qtuiloader'
11 | require 'opencv'
12 | useOpenCV = true
13 | require 'camera'
14 |
15 | -- parse args
16 | op = xlua.OptionParser('%prog [options]')
17 | op:option{'-c', '--camera', action='store', dest='camidx',
18 | help='if source=camera, you can specify the camera index: /dev/videoIDX',
19 | default=0}
20 | opt,args = op:parse()
21 |
22 | -- setup GUI (external UI file)
23 | widget = qtuiloader.load('g.ui')
24 | win = qt.QtLuaPainter(widget.frame)
25 |
26 |
27 | -- setup camera
28 | camdiv = 4
29 | camx = 640/camdiv
30 | camy = 480/camdiv
31 | camera = image.Camera(opt.camidx)
32 | img1=torch.Tensor(3,camy,camx)
33 | img2=torch.Tensor(3,camy,camx)
34 | frame=torch.Tensor(3,640,480)
35 | norm=torch.Tensor(3,camy,camx)
36 | angle=torch.Tensor(3,camy,camx)
37 | flow_x=torch.Tensor(3,camy,camx)
38 | flow_y=torch.Tensor(3,camy,camx)
39 | hsl = torch.Tensor(3,camy,camx)
40 | rgb = torch.Tensor(3,camy,camx)
41 |
42 | -- process function
43 | function process()
44 | -- grab frames
45 | img2 = img2:copy(img1)
46 | img1 = image.scale(camera:forward(),camx,camy)
47 |
48 | -- flow - from opencv.CalcOpticalFlow_testme() function:
49 | norm, angle, flow_x, flow_y = opencv.CalcOpticalFlow{pair={img2,img1}, method='LK'}
50 | -- local methods = {'LK', 'HS', 'BM'}
51 | -- see code here: /Users/eugenioculurciello/lua-local/share/torch/lua/opencv/init.lua
52 |
53 | hsl:select(1,1):copy(angle):div(360)
54 | hsl:select(1,2):copy(norm)--:div(math.max(norm:max(),1e-2))
55 | hsl:select(1,3):fill(0.5)
56 | rgb = image.hsl2rgb(hsl)
57 |
58 | --frame={norm,angle,flow_x,flow_y}
59 | frame = image.scale(rgb,640,480,'simple')
60 | --frame = image.scale(flow_x:div(math.max(flow_x:max(),1e-2)),640,480,'simple')
61 | end
62 |
63 | -- local methods = {'LK', 'HS', 'BM'}
64 | -- for i,method in ipairs(methods) do
65 | -- print(i,method)
66 | -- local norm, angle, flow_x, flow_y =
67 | -- opencv.CalcOpticalFlow{pair={img1,img2}, method=method}
68 | -- local hsl = torch.Tensor(3,img1:size(2), img1:size(3))
69 | -- hsl:select(1,1):copy(angle):div(360)
70 | -- hsl:select(1,2):copy(norm):div(math.max(norm:max(),1e-2))
71 | -- hsl:select(1,3):fill(0.5)
72 | -- local rgb = image.hsl2rgb(hsl)
73 | -- image.display{image={img1,img2,rgb},
74 | -- legend='cvOpticalFLow, method = ' .. method,
75 | -- legends={'input 1', 'input2', 'HSL-mapped flow'}}
76 | -- image.display{image={norm,angle,flow_x,flow_y},
77 | -- scaleeach=true,
78 | -- legend='cvOpticalFLow, method = ' .. method,
79 | -- legends={'norm','angle', 'flow x', 'flow y'}}
80 | --
81 |
82 | -- display function
83 | function display()
84 | zoom = 1
85 | win:gbegin()
86 | win:showpage()
87 | image.display{image=frame, win=win, zoom=zoom}
88 | win:gend()
89 | end
90 |
91 | -- setup gui
92 | local timer = qt.QTimer()
93 | timer.interval = 10
94 | timer.singleShot = true
95 | qt.connect(timer,
96 | 'timeout()',
97 | function()
98 | process()
99 | display()
100 | timer:start()
101 | end)
102 |
103 | -- calls back for all buttons
104 |
105 | widget.windowTitle = 'Optical Flow live'
106 | widget:show()
107 | timer:start()
108 |
--------------------------------------------------------------------------------
/gabor-layer-demo /GaborLayer.lua:
--------------------------------------------------------------------------------
1 | -- Clement's old gabor function
2 |
3 |
4 | require 'image'
5 | require 'lab'
6 | require 'xlua'
7 |
8 | -- For the similar Gabor code go to
9 | -- http://www.mathworks.com/matlabcentral/fileexchange/23253-gabor-filter/content/Gabor%20Filter/gabor_fn.m
10 |
11 |
12 | -- Size should be odd number
13 | -- angle (in rad)
14 | -- elipse_ratio = aspect ratio(0.5)
15 |
16 | -- test:
17 | --image.display{image=gabor(9,0.5,45,1,0.5), zoom =4}
18 |
19 | function gabor(size, sigma, angle, period, ellipse_ratio)
20 | -- init matrix
21 | local data = torch.zeros(size,size)
22 |
23 | -- image -> pixel
24 | period = period * size
25 | sigma = sigma * size
26 |
27 | -- set params
28 | local halfsize = math.floor(size/2)
29 | local sigma_x = sigma
30 | local sigma_y = sigma/ellipse_ratio
31 |
32 | for y=-halfsize,halfsize do
33 | for x=-halfsize,halfsize do
34 |
35 | x_angle = x*math.cos(angle) + y*math.sin(angle)
36 | y_angle = -x*math.sin(angle) + y*math.cos(angle)
37 | data[x+halfsize+1][y+halfsize+1]
38 | = math.exp(-0.5*(x_angle^2/sigma_x^2 + y_angle^2/sigma_y^2))
39 | * math.cos(2*math.pi*x_angle/period)
40 | end
41 | end
42 |
43 | -- return new tensor
44 | return data
45 | end
46 |
--------------------------------------------------------------------------------
/gabor-layer-demo /frame.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 709
10 | 504
11 |
12 |
13 |
14 |
15 | 0
16 | 0
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 255
26 | 255
27 | 255
28 |
29 |
30 |
31 |
32 |
33 |
34 | 236
35 | 236
36 | 236
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | 255
46 | 255
47 | 255
48 |
49 |
50 |
51 |
52 |
53 |
54 | 229
55 | 229
56 | 229
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | 229
66 | 229
67 | 229
68 |
69 |
70 |
71 |
72 |
73 |
74 | 229
75 | 229
76 | 229
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | Form
85 |
86 |
87 | -
88 |
89 |
90 |
91 | 640
92 | 960
93 |
94 |
95 |
96 |
97 | 640
98 | 960
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 | 0
108 | 0
109 | 0
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 | 0
119 | 0
120 | 0
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 | 185
130 | 185
131 | 185
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 | QFrame::StyledPanel
140 |
141 |
142 | QFrame::Raised
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
--------------------------------------------------------------------------------
/live-kinect/g.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 1000
10 | 700
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | 255
20 | 255
21 | 255
22 |
23 |
24 |
25 |
26 |
27 |
28 | 236
29 | 236
30 | 236
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | 255
40 | 255
41 | 255
42 |
43 |
44 |
45 |
46 |
47 |
48 | 229
49 | 229
50 | 229
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 | 229
60 | 229
61 | 229
62 |
63 |
64 |
65 |
66 |
67 |
68 | 229
69 | 229
70 | 229
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Form
79 |
80 |
81 | -
82 |
83 |
84 | 2047
85 |
86 |
87 | 50
88 |
89 |
90 | 250
91 |
92 |
93 | Qt::Vertical
94 |
95 |
96 |
97 | -
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 | 0
106 | 0
107 | 0
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 | 0
117 | 0
118 | 0
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 | 185
128 | 185
129 | 185
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 | QFrame::StyledPanel
138 |
139 |
140 | QFrame::Raised
141 |
142 |
143 |
144 |
145 | 910
146 | 530
147 | 22
148 | 111
149 |
150 |
151 |
152 | -30
153 |
154 |
155 | 30
156 |
157 |
158 | Qt::Vertical
159 |
160 |
161 | verticalSlider_1
162 | verticalSlider2
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
--------------------------------------------------------------------------------
/live-kinect/run.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ----------------------------------------------------------------------
3 | -- WHAT: segments a image source.
4 | -- the image source must be provided on the command line.
5 | --
6 | require 'xlua'
7 | require 'os'
8 | --require 'mstsegm'
9 | --require 'mincut'
10 | require 'kinect'
11 | require 'qt'
12 | require 'qtwidget'
13 | require 'qtuiloader'
14 |
15 | -- parse args
16 | op = xlua.OptionParser('%prog -s SOURCE [options]')
17 | op:option{'-s', '--source', action='store', dest='source',
18 | help='image source, can be one of: camera | lena | video'}
19 | op:option{'-c', '--camera', action='store', dest='camidx',
20 | help='if source=camera, you can specify the camera index: /dev/videoIDX [default=0]'}
21 | op:option{'-p', '--path', action='store', dest='path',
22 | help='path to video'}
23 | options,args = op:parse()
24 |
25 | -- setup QT gui
26 | -- toolBox.useQT()
27 | -- widget = qtuiloader.load('live-kinect.ui')
28 | -- painter = qt.QtLuaPainter(widget.frame)
29 |
30 | -- setup GUI (external UI file)
31 | if not win or not widget then
32 | widget = qtuiloader.load('g.ui')
33 | win = qt.QtLuaPainter(widget.frame)
34 | end
35 |
36 | -- video source
37 | -- source = nn.ImageSource{type = options.source or 'camera',
38 | -- path = options.path,
39 | -- cam_idx = options.camidx,
40 | -- fps = 20,
41 | -- width = 200,
42 | -- height = 150}
43 |
44 | -- displayers
45 | -- displayer_source = Displayer()
46 | -- displayer_depth = Displayer()
47 | -- displayer_segments = Displayer()
48 |
49 | -- global zoom
50 | zoom = 1
51 | kinect = kinect.Device(640,480)
52 | -- incremental segmentation
53 | do
54 | local nslices = 8
55 | local segmbuf
56 | local ringbuf = {}
57 | local ringbuf_i = 0
58 |
59 | -- incremental segm function
60 | function segm(img)
61 | -- new frame
62 | ringbuf_i = ringbuf_i + 1
63 |
64 | -- resize buffer
65 | segmbuf = segmbuf or torch.Tensor(img:size(1), img:size(2), img:size(3), nslices)
66 | -- store frame
67 | ringbuf[ringbuf_i] = torch.Tensor():resizeAs(img):copy(img)
68 |
69 | -- cleanup
70 | ringbuf[ringbuf_i-nslices] = nil
71 | collectgarbage()
72 |
73 | -- compute segm
74 | local segments
75 | if ringbuf_i > nslices then
76 | -- fill buffer
77 | for i = 1,nslices do
78 | segmbuf:select(4,i):copy(ringbuf[ringbuf_i-nslices+i])
79 | end
80 | -- segm
81 | segments = mstsegm.infer{image=segmbuf,
82 | connex=4,
83 | k=k, min=minsize,
84 | incremental=true}
85 | -- return last slice
86 | return segments:select(4,segments:size(4))
87 | else
88 | -- in the mean time, dont do anything
89 | return img
90 | end
91 | end
92 | end
93 |
94 | -- exec
95 |
96 | input = torch.Tensor(320,240,4)
97 | result = torch.Tensor()
98 | function run()
99 | -- get stream
100 | kinect:getRGBD()
101 | kinect:tilt(widget.verticalSlider2.value)
102 | image.scale(kinect.rgbd,input,'simple')
103 | frame = input:narrow(3,1,3)
104 | thre = widget.verticalSlider_1.value
105 |
106 | mincut.segmentation{image = input,
107 | result=result,
108 | threshold=thre}
109 |
110 | win:gbegin()
111 | win:showpage()
112 |
113 | displayer_source:show{tensor = frame, painter = win, globalzoom=zoom,
114 | min=0,max=1, offset_x=0, offset_y=10,
115 | legend='camera image'}
116 | displayer_depth:show{tensor = input:select(3,4), painter = win, globalzoom=zoom,
117 | min = 0, max = 1,
118 | offset_x = frame:size(1)+5, offset_y = 10,
119 | legend = 'depth image'}
120 | displayer_segments:show{tensor = result, painter = win, globalzoom=zoom,
121 | min = 0, max = 1,
122 | offset_x = 0, offset_y = frame:size(2) + 20,
123 | legend = 'segmented image'}
124 |
125 | -- and params
126 | win:setfont(qt.QFont{serif=false,italic=false,size=14})
127 | win:moveto(330, 460); win:show('Threshold = ' .. thre)
128 | win:gend()
129 | end
130 |
131 | -- Loop Process
132 | local timer = qt.QTimer()
133 | timer.interval = 5
134 | timer.singleShot = true
135 | timer:start()
136 | qt.connect(timer, 'timeout()', function() run() timer:start() end)
137 |
138 | -- Start Process
139 | widget.windowTitle = "Live Segmentation on Kinect"
140 | widget:show()
141 |
--------------------------------------------------------------------------------
/load-data/load-images.lua:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 | -- load-images.lua
3 | --
4 | -- This script shows how to load images from a directory, and sort
5 | -- the files according to their name
6 |
7 | -- This mostly demonstrates how to use Lua's table data structure,
8 | -- and interact with the file system.
9 |
10 | -- note: to run this script, simply do:
11 | -- torch load-images.lua
12 |
13 | -- By default, the script loads jpeg images. You can change that
14 | -- by specifying another extension:
15 | -- torch load-images.lua --ext png
16 |
17 | require 'torch'
18 | require 'xlua'
19 | require 'image'
20 |
21 | ----------------------------------------------------------------------
22 | -- 1. Parse command-line arguments
23 |
24 | op = xlua.OptionParser('load-images.lua [options]')
25 | op:option{'-d', '--dir', action='store', dest='dir', help='directory to load', req=true}
26 | op:option{'-e', '--ext', action='store', dest='ext', help='only load files of this extension', default='jpg'}
27 | opt = op:parse()
28 | op:summarize()
29 |
30 | ----------------------------------------------------------------------
31 | -- 2. Load all files in directory
32 |
33 | -- We process all files in the given dir, and add their full path
34 | -- to a Lua table.
35 |
36 | -- Create empty table to store file names:
37 | files = {}
38 |
39 | -- Go over all files in directory. We use an iterator, paths.files().
40 | for file in paths.files(opt.dir) do
41 | -- We only load files that match the extension
42 | if file:find(opt.ext .. '$') then
43 | -- and insert the ones we care about in our table
44 | table.insert(files, paths.concat(opt.dir,file))
45 | end
46 | end
47 |
48 | -- Check files
49 | if #files == 0 then
50 | error('given directory doesnt contain any files of type: ' .. opt.ext)
51 | end
52 |
53 | ----------------------------------------------------------------------
54 | -- 3. Sort file names
55 |
56 | -- We sort files alphabetically, it's quite simple with table.sort()
57 |
58 | table.sort(files, function (a,b) return a < b end)
59 |
60 | print('Found files:')
61 | print(files)
62 |
63 | ----------------------------------------------------------------------
64 | -- 4. Finally we load images
65 |
66 | -- Go over the file list:
67 | images = {}
68 | for i,file in ipairs(files) do
69 | -- load each image
70 | table.insert(images, image.load(file))
71 | end
72 |
73 | print('Loaded images:')
74 | print(images)
75 |
76 | -- Display a of few them
77 | for i = 1,math.min(#files,10) do
78 | image.display{image=images[i], legend=files[i]}
79 | end
80 |
--------------------------------------------------------------------------------
/mst-based-segmenter/run.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env torch
2 | ------------------------------------------------------------
3 | -- a scene segmenter, base on a ConvNet trained end-to-end
4 | -- to predict class distributions at dense locations.
5 | --
6 | -- Clement Farabet
7 | --
8 |
9 | require 'xlua'
10 | require 'torch'
11 | require 'qt'
12 | require 'qtwidget'
13 | require 'inline'
14 | require 'imgraph'
15 | require 'nnx'
16 |
17 | -- parse args
18 | op = xlua.OptionParser('%prog [options]')
19 | op:option{'-c', '--camera', action='store', dest='camidx',
20 | help='camera index: /dev/videoIDX (if no video given)',
21 | default=0}
22 | op:option{'-v', '--video', action='store', dest='video',
23 | help='video file to process'}
24 | op:option{'-f', '--fps', action='store', dest='fps',
25 | help='number of frames per second', default=10}
26 | op:option{'-t', '--time', action='store', dest='seconds',
27 | help='length to process (in seconds)', default=10}
28 | op:option{'-w', '--width', action='store', dest='width',
29 | help='resize video, width', default=320}
30 | op:option{'-h', '--height', action='store', dest='height',
31 | help='resize video, height', default=240}
32 | op:option{'-z', '--zoom', action='store', dest='zoom',
33 | help='display zoom', default=1}
34 | opt,args = op:parse()
35 |
36 | if not opt.video then
37 | -- load camera
38 | require 'camera'
39 | video = image.Camera(opt.camidx, opt.width, opt.height)
40 | else
41 | -- load video
42 | require 'ffmpeg'
43 | video = ffmpeg.Video{path=opt.video, width=opt.width, height=opt.height,
44 | fps=opt.fps, length=opt.seconds, delete=false}
45 | end
46 |
47 | -- setup GUI (external UI file)
48 | if not win or not widget then
49 | win = qtwidget.newwindow(opt.width*2*opt.zoom, opt.height*opt.zoom,
50 | 'A simple mst-based cartoonizer')
51 | end
52 |
53 | -- gaussian (a gaussian, really, is always useful)
54 | gaussian = image.gaussian(3)
55 |
56 | -- process function
57 | function process()
58 | -- (1) grab frame
59 | frame = video:forward()
60 |
61 | -- (2) compute affinity graph on input image
62 | frame_smoothed = image.convolve(frame, gaussian, 'same')
63 | graph = imgraph.graph(frame_smoothed)
64 |
65 | -- (3) cut graph using min-spanning tree
66 | mstsegm = imgraph.segmentmst(graph, 2, 20)
67 |
68 | -- (4) pool the input frame into the segmentation
69 | cartoon = imgraph.histpooling(frame:clone(), mstsegm)
70 | end
71 |
72 | -- display function
73 | function display()
74 | -- display input image + result
75 | image.display{image={frame,cartoon}, win=win, zoom=opt.zoom}
76 | end
77 |
78 | -- setup gui
79 | timer = qt.QTimer()
80 | timer.interval = 10
81 | timer.singleShot = true
82 | qt.connect(timer,
83 | 'timeout()',
84 | function()
85 | process()
86 | win:gbegin()
87 | win:showpage()
88 | display()
89 | win:gend()
90 | timer:start()
91 | end)
92 | timer:start()
93 |
--------------------------------------------------------------------------------
/person-detector/PyramidUnPacker.lua:
--------------------------------------------------------------------------------
1 |
2 | local PyramidUnPacker, parent = torch.class('nn.PyramidUnPacker', 'nn.Module')
3 |
4 | local function getSizesTbl(net)
5 | local sizes_tbl = {}
6 | for i=1,#net.modules do
7 | dw = net.modules[i].dW
8 | dh = net.modules[i].dH
9 | kw = net.modules[i].kW
10 | kh = net.modules[i].kH
11 | if((dw ~= nil)and(dh ~= nil)and(kw ~= nil) and(kh ~= nil)) then
12 | table.insert(sizes_tbl, {kw=kw,kh=kh,dw=dw,dh=dh})
13 | end
14 | end
15 |
16 | return sizes_tbl
17 | end
18 |
19 | local function getRange(args)
20 | local sizes_tbl = args.sizes_tbl
21 | local idx_output = args.idx_output
22 |
23 | local x = torch.Tensor(#sizes_tbl+1)
24 | local y = torch.Tensor(#sizes_tbl+1)
25 | x[#sizes_tbl+1] = idx_output
26 | y[#sizes_tbl+1] = idx_output
27 |
28 | for k = #sizes_tbl,1,-1 do
29 | -- rightmost point of the image that affects x(k+1)
30 | x[k] = sizes_tbl[k].kw+ (x[k+1]-1) * sizes_tbl[k].dw
31 | -- leftmost point of the image that affects y(k+1)
32 | y[k] = 1 + (y[k+1]-1) * sizes_tbl[k].dw
33 | end
34 | local left_width = y[1]
35 | local right_width = x[1]
36 |
37 | for k = #sizes_tbl,1,-1 do
38 | -- rightmost point of the image that affects x(k+1)
39 | x[k] = sizes_tbl[k].kh+ (x[k+1]-1) * sizes_tbl[k].dh
40 | -- leftmost point of the image that affects y(k+1)
41 | y[k] = 1 + (y[k+1]-1) * sizes_tbl[k].dh
42 | end
43 |
44 | local left_height = y[1]
45 | local right_height = x[1]
46 |
47 |
48 | return left_width, right_width, left_height, right_height
49 | end
50 |
51 | local function getGlobalSizes(args)
52 | local sizes_tbl = args.sizes_tbl
53 |
54 | -- to find gobal kernel size we use recursive formula:
55 | -- glob_ker(n + 1) = 1
56 | -- glob_ker(n) = ker(n) + (glob_ker(n+1)-1)*step(n)
57 | --
58 | -- where: ker(n) - kernel size on layer n, step(n) - step size on layer n
59 | -- and n is number of layers that change the size of the input (convolution and subsample)
60 | local left_width1, right_width1, left_height1, right_height1 = getRange({sizes_tbl=sizes_tbl, idx_output=1})
61 | local ker_width = right_width1 - left_width1 +1
62 | local ker_height = right_height1 - left_height1 +1
63 |
64 | local step_width = 1
65 | local step_height = 1
66 |
67 | -- global step = MUL(step_1, step_2, ... , step_n)
68 | for i = 1, #sizes_tbl do
69 | step_width = step_width * sizes_tbl[i].dw
70 | step_height = step_height * sizes_tbl[i].dh
71 | end
72 |
73 | return step_width, step_height, ker_width, ker_height
74 | end
75 |
76 | function PyramidUnPacker:__init(network)
77 | parent.__init(self)
78 |
79 | -- infer params from given net
80 | self.step_width, self.step_height, self.ker_width, self.ker_height
81 | = getGlobalSizes({sizes_tbl=getSizesTbl(network)})
82 | end
83 |
84 | function PyramidUnPacker:forward(input, coordinates)
85 | self.out_tbl = {}
86 | self.coordinates = coordinates
87 | self.step_width = 4
88 | self.step_height = 4
89 | self.ker_width = 46 -- size of CNN eye
90 | self.ker_height = 46
91 | for i = 1, self.coordinates:size(1) do
92 | local start_x = math.floor((self.coordinates[i][1] - 1)/self.step_width) + 1
93 | local start_y = math.floor((self.coordinates[i][2] - 1)/self.step_height) + 1
94 | local width = math.floor((self.coordinates[i][5] - self.ker_width)/self.step_width) + 1
95 | local height = math.floor((self.coordinates[i][6] - self.ker_height)/self.step_height) + 1
96 | local temp = input:narrow(3, start_x, width)
97 | temp = temp:narrow(2, start_y, height)
98 | table.insert(self.out_tbl, temp)
99 | end
100 | return self.out_tbl
101 | end
102 |
103 | function PyramidUnPacker:backward(input, gradOutput)
104 | error('backward non implemented', 'PyramidUnPacker')
105 | end
106 |
107 | function PyramidUnPacker:write(file)
108 | parent.write(self,file)
109 | file:writeDouble(#self.scales)
110 | for i = 1,#self.scales do
111 | file:writeDouble(self.scales[i])
112 | end
113 | end
114 |
115 | function PyramidUnPacker:read(file)
116 | parent.read(self,file)
117 | local nbScales = file:readDouble()
118 | for i = 1,nbScales do
119 | self.scales[i] = file:readDouble()
120 | end
121 | end
122 |
--------------------------------------------------------------------------------
/person-detector/README.md:
--------------------------------------------------------------------------------
1 | clnet-person
2 |
3 | example of training of a cl network on pedestrian dataset.
4 | Also includes cnn version
5 |
6 |
--------------------------------------------------------------------------------
/person-detector/model.lua:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 | -- Create CNN and loss to optimize.
3 | --
4 | -- Clement Farabet
5 | ----------------------------------------------------------------------
6 |
7 | require 'torch' -- torch
8 | require 'image' -- to visualize the dataset
9 | require 'nn' -- provides all sorts of trainable modules/layers
10 | --require 'Dropout' -- Hinton dropout technique
11 |
12 | if opt.type == 'cuda' then
13 | nn.SpatialConvolutionMM = nn.SpatialConvolution
14 | end
15 |
16 | ----------------------------------------------------------------------
17 | print(sys.COLORS.red .. '==> define parameters')
18 |
19 | -- 2-class problem:
20 | local noutputs = 2
21 |
22 | -- input dimensions:
23 | local nfeats = 3
24 | local width = 46
25 | local height = 46
26 |
27 | -- hidden units, filter sizes (for ConvNet only):
28 | local nstates = {32,64,128,128}
29 | local filtsize = {7,7,7}
30 | local poolsize = 2
31 |
32 | ----------------------------------------------------------------------
33 | print(sys.COLORS.red .. '==> construct CNN')
34 |
35 |
36 | local CNN = nn.Sequential()
37 |
38 | -- stage 1:
39 | CNN:add(nn.SpatialConvolutionMM(nfeats, nstates[1], filtsize[1], filtsize[1]))
40 | CNN:add(nn.Threshold())
41 | CNN:add(nn.SpatialMaxPooling(poolsize,poolsize,poolsize,poolsize))
42 |
43 | -- stage 2:
44 | CNN:add(nn.SpatialConvolutionMM(nstates[1], nstates[2], filtsize[2], filtsize[2]))
45 | CNN:add(nn.Threshold())
46 | CNN:add(nn.SpatialMaxPooling(poolsize,poolsize,poolsize,poolsize))
47 |
48 | -- stage 3:
49 | CNN:add(nn.SpatialConvolutionMM(nstates[2], nstates[3], filtsize[3], filtsize[3]))
50 |
51 | local classifier = nn.Sequential()
52 | -- stage 3: linear
53 | classifier:add(nn.Reshape(nstates[3]))
54 | classifier:add(nn.Linear(nstates[3], nstates[4]))
55 | classifier:add(nn.Threshold())
56 | classifier:add(nn.Linear(nstates[4], noutputs))
57 |
58 | -- stage 4 : log probabilities
59 | classifier:add(nn.LogSoftMax())
60 |
61 | for _,layer in ipairs(CNN.modules) do
62 | if layer.bias then
63 | layer.bias:fill(.2)
64 | if i == #CNN.modules-1 then
65 | layer.bias:zero()
66 | end
67 | end
68 | end
69 |
70 | model = nn.Sequential()
71 | model:add(CNN)
72 | model:add(classifier)
73 |
74 | -- Loss: NLL
75 | loss = nn.ClassNLLCriterion()
76 |
77 |
78 | ----------------------------------------------------------------------
79 | print(sys.COLORS.red .. '==> here is the CNN:')
80 | print(model)
81 |
82 | if opt.type == 'cuda' then
83 | model:cuda()
84 | loss:cuda()
85 | end
86 |
87 | -- return package:
88 | return {
89 | model = model,
90 | loss = loss,
91 | }
92 |
93 |
--------------------------------------------------------------------------------
/person-detector/model.net:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/person-detector/model.net
--------------------------------------------------------------------------------
/person-detector/run.lua:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 | -- Train a ConvNet as people detector
3 | --
4 | -- E. Culurciello
5 | -- Mon June 10 14:58:50 EDT 2014
6 | ----------------------------------------------------------------------
7 |
8 | require 'pl'
9 | require 'trepl'
10 | require 'torch' -- torch
11 | require 'image' -- to visualize the dataset
12 | require 'nn' -- provides all sorts of trainable modules/layers
13 |
14 | ----------------------------------------------------------------------
15 | print(sys.COLORS.red .. '==> processing options')
16 |
17 | opt = lapp[[
18 | -r,--learningRate (default 1e-3) learning rate
19 | -d,--learningRateDecay (default 1e-7) learning rate decay (in # samples)
20 | -w,--weightDecay (default 1e-5) L2 penalty on the weights
21 | -m,--momentum (default 0.1) momentum
22 | -d,--dropout (default 0.5) dropout amount
23 | -b,--batchSize (default 128) batch size
24 | -t,--threads (default 8) number of threads
25 | -p,--type (default float) float or cuda
26 | -i,--devid (default 1) device ID (if using CUDA)
27 | -s,--size (default small) dataset: small or full or extra
28 | -o,--save (default results) save directory
29 | --patches (default all) percentage of samples to use for testing'
30 | --visualize (default true) visualize dataset
31 | ]]
32 |
33 | -- nb of threads and fixed seed (for repeatable experiments)
34 | torch.setnumthreads(opt.threads)
35 | torch.manualSeed(1)
36 | torch.setdefaulttensortype('torch.FloatTensor')
37 |
38 | -- type:
39 | if opt.type == 'cuda' then
40 | print(sys.COLORS.red .. '==> switching to CUDA')
41 | require 'cunn'
42 | cutorch.setDevice(opt.devid)
43 | print(sys.COLORS.red .. '==> using GPU #' .. cutorch.getDevice())
44 | end
45 |
46 | ----------------------------------------------------------------------
47 | print(sys.COLORS.red .. '==> load modules')
48 |
49 | local data = require 'data'
50 | local train = require 'train'
51 | local test = require 'test'
52 |
53 | ----------------------------------------------------------------------
54 | print(sys.COLORS.red .. '==> training!')
55 |
56 | while true do
57 | train(data.trainData)
58 | test(data.testData)
59 | end
60 |
--------------------------------------------------------------------------------
/profiling/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/profiling/.DS_Store
--------------------------------------------------------------------------------
/profiling/conv-cpu.lua:
--------------------------------------------------------------------------------
1 |
2 | -- libs:
3 | require 'sys'
4 | require 'nnx'
5 | --require 'cunn'
6 |
7 | -- dev:
8 | --utorch.setDevice(arg[1] or 1)
9 | --print('DEVID = ' .. cutorch.getDevice())
10 |
11 | -- params:
12 | batchSize = 32
13 | isize = 64
14 | ninputs = 32
15 | nhiddens = 64
16 | stride = 1
17 | fsize = 8
18 |
19 | -- batch input:
20 | i = torch.randn(batchSize, ninputs, isize, isize)
21 |
22 | -- layers to benchmark:
23 | n = nn.Sequential()
24 | n:add( nn.SpatialConvolutionMM(ninputs, nhiddens, fsize, fsize, stride, stride) )
25 |
26 | -- pre-alloc states:
27 | n:forward(i)
28 | n:backward(i, n.output)
29 | --cutorch.synchronize()
30 |
31 | -- nb of operations:
32 | opsPerMAC = 2
33 | steps = 3
34 | ops = opsPerMAC * batchSize * steps * (
35 | ninputs*nhiddens*(fsize^2)*(((isize-fsize)/stride+1)^2)
36 | )
37 |
38 | -- benchmark:
39 | nbOfAverages = 3
40 | sys.tic()
41 | for t = 1,nbOfAverages do
42 | n:forward(i)
43 | n:backward(i, n.output)
44 | end
45 | --cutorch.synchronize()
46 | t = sys.toc()/nbOfAverages
47 |
48 | -- result:
49 | print('Fprop+Bprop+Acc - GFLOP/s:', ops/t/1e9)
50 |
51 |
--------------------------------------------------------------------------------
/profiling/conv-gpu.lua:
--------------------------------------------------------------------------------
1 |
2 | -- libs:
3 | require 'sys'
4 | require 'cunn'
5 |
6 | -- dev:
7 | cutorch.setDevice(arg[1] or 1)
8 | print('DEVID = ' .. cutorch.getDevice())
9 |
10 | -- params:
11 | batchSize = 128
12 | isize = 64
13 | ninputs = 32
14 | nhiddens = 64
15 | stride = 1
16 | fsize = 8
17 |
18 | -- batch input:
19 | -- i = torch.randn(batchSize, ninputs, isize, isize)
20 | i = torch.randn(ninputs, isize, isize, batchSize)
21 | i = i:cuda()
22 |
23 | -- layers to benchmark:
24 | n = nn.Sequential()
25 | -- n:add( nn.Transpose({1,4},{1,3},{1,2}) )
26 | n:add( nn.SpatialConvolutionCUDA(ninputs, nhiddens, fsize, fsize, stride, stride) )
27 | -- n:add( nn.Transpose({4,1},{4,2},{4,3}) )
28 | n:cuda()
29 |
30 | -- pre-alloc states:
31 | n:forward(i)
32 | n:backward(i, n.output)
33 | cutorch.synchronize()
34 |
35 | -- nb of operations:
36 | opsPerMAC = 2
37 | steps = 3
38 | ops = opsPerMAC * batchSize * steps * (
39 | ninputs*nhiddens*(fsize^2)*(((isize-fsize)/stride+1)^2)
40 | )
41 |
42 | -- benchmark:
43 | nbOfAverages = 3
44 | sys.tic()
45 | for t = 1,nbOfAverages do
46 | n:forward(i)
47 | n:backward(i,n.output)
48 | end
49 | cutorch.synchronize()
50 | t = sys.toc()/nbOfAverages
51 |
52 | -- result:
53 | print('Fprop+Bprop+Acc - GFLOP/s:', ops/t/1e9)
54 |
55 |
--------------------------------------------------------------------------------
/profiling/linear-cpu.lua:
--------------------------------------------------------------------------------
1 |
2 | require 'sys'
3 | require 'cunn'
4 |
5 | cutorch.setDevice(arg[1] or 1)
6 | print('DEVID = ' .. cutorch.getDevice())
7 |
8 | bs = 512
9 | ninputs = 4096
10 | nhiddens = 4096
11 | noutputs = 1000
12 |
13 | n = nn.Sequential()
14 | n:add( nn.Linear(ninputs, nhiddens) )
15 | n:add( nn.Threshold() )
16 | n:add( nn.Linear(nhiddens, noutputs) )
17 | n:add( nn.Threshold() )
18 |
19 | i = torch.randn(bs, ninputs)
20 |
21 | ops = (ninputs*nhiddens + nhiddens*noutputs) * 2 * bs * 3
22 |
23 | n:float()
24 | i = i:float()
25 |
26 | N=5
27 |
28 | -- pre-alloc states:
29 | n:forward(i)
30 | n:backward(i, n.output)
31 | cutorch.synchronize()
32 |
33 | sys.tic()
34 | for t = 1,N do
35 | n:forward(i)
36 | n:backward(i, n.output)
37 | end
38 | cutorch.synchronize()
39 | t = sys.toc()/N
40 | print('Fprop+Bprop+Acc - GFLOP/s:', ops/t/1e9)
41 |
42 |
--------------------------------------------------------------------------------
/profiling/linear-gpu.lua:
--------------------------------------------------------------------------------
1 |
2 | require 'sys'
3 | require 'cunn'
4 |
5 | cutorch.setDevice(arg[1] or 1)
6 | print('DEVID = ' .. cutorch.getDevice())
7 |
8 | bs = 512
9 | ninputs = 4096
10 | nhiddens = 4096
11 | noutputs = 1000
12 |
13 | n = nn.Sequential()
14 | n:add( nn.Linear(ninputs, nhiddens) )
15 | n:add( nn.Threshold() )
16 | n:add( nn.Linear(nhiddens, noutputs) )
17 | n:add( nn.Threshold() )
18 |
19 | i = torch.randn(bs, ninputs)
20 |
21 | ops = (ninputs*nhiddens + nhiddens*noutputs) * 2 * bs * 3
22 |
23 | n:float()
24 | i = i:float()
25 |
26 | N=5
27 |
28 | -- sys.tic()
29 | -- for t = 1,N do
30 | -- n:forward(i)
31 | -- n:backward(i, n.output)
32 | -- end
33 | -- t = sys.toc()/N
34 | -- print('MKL - GFLOP/s:', ops/t/1e9)
35 |
36 | n:cuda()
37 | i = i:cuda()
38 |
39 | -- pre-alloc states:
40 | n:forward(i)
41 | n:backward(i, n.output)
42 | cutorch.synchronize()
43 |
44 | sys.tic()
45 | for t = 1,N do
46 | n:forward(i)
47 | n:backward(i, n.output)
48 | end
49 | cutorch.synchronize()
50 | t = sys.toc()/N
51 | print('Fprop+Bprop+Acc - GFLOP/s:', ops/t/1e9)
52 |
53 |
--------------------------------------------------------------------------------
/profiling/results.rtf:
--------------------------------------------------------------------------------
1 | {\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf370
2 | {\fonttbl\f0\fswiss\fcharset0 ArialMT;\f1\fnil\fcharset0 LucidaGrande;\f2\fswiss\fcharset0 Helvetica;
3 | }
4 | {\colortbl;\red255\green255\blue255;}
5 | {\info
6 | {\author Eugenio Culurciello}}\margl1440\margr1440\vieww17500\viewh19200\viewkind0
7 | \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
8 |
9 | \f0\fs28 \cf0 \
10 | EC test May 2013:\
11 | \
12 | \pard\tx2835\li2835\fi-2836
13 |
14 | \f1\b\fs22 \cf0 Hardware Overview:\
15 | \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural
16 |
17 | \f2\b0\fs24 \cf0 \
18 | \pard\tx2835\li2835\fi-2836
19 |
20 | \f1\fs22 \cf0 Model Name: MacBook Pro\
21 | Model Identifier: MacBookPro10,1\
22 | Processor Name: Intel Core i7\
23 | Processor Speed: 2.6 GHz\
24 | Number of Processors: 1\
25 | Total Number of Cores: 4\
26 | L2 Cache (per Core): 256 KB\
27 | L3 Cache: 6 MB\
28 | Memory: 16 GB\
29 | Boot ROM Version: MBP101.00EE.B02\
30 | SMC Version (system): 2.3f35\
31 | Serial Number (system): C02J120CDKQ5\
32 | Hardware UUID: E4515A95-B99F-5FBD-A105-25BE76310224
33 | \f0\fs28 \
34 | \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
35 | \cf0 \
36 | eugenioculurciello@Pora ~/Code/torch/profiling $ torch linear-cpu.lua\
37 | Torch 7.0 Copyright (C) 2001-2011 Idiap, NEC Labs, NYU\
38 | DEVID = 1 \
39 | Fprop+Bprop+Acc - GFLOP/s: 140.25925408311 \
40 | eugenioculurciello@Pora ~/Code/torch/profiling $ torch linear-gpu.lua\
41 | Torch 7.0 Copyright (C) 2001-2011 Idiap, NEC Labs, NYU\
42 | DEVID = 1 \
43 | Fprop+Bprop+Acc - GFLOP/s: 274.81519512347 \
44 | eugenioculurciello@Pora ~/Code/torch/profiling $ torch conv-cpu.lua\
45 | Torch 7.0 Copyright (C) 2001-2011 Idiap, NEC Labs, NYU\
46 | DEVID = 1 \
47 | Fprop+Bprop+Acc - GFLOP/s: 55.582031507672 \
48 | eugenioculurciello@Pora ~/Code/torch/profiling $ torch conv-gpu.lua\
49 | Torch 7.0 Copyright (C) 2001-2011 Idiap, NEC Labs, NYU\
50 | DEVID = 1 \
51 | Fprop+Bprop+Acc - GFLOP/s: 96.082082210762 }
--------------------------------------------------------------------------------
/road-net/multinet-float.net:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/road-net/multinet-float.net
--------------------------------------------------------------------------------
/road-net/preproc.t7:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/road-net/preproc.t7
--------------------------------------------------------------------------------
/road-net/segmtools.lua:
--------------------------------------------------------------------------------
1 | ------------------------------------------------------------
2 | -- common tools for segmentation: display and so on
3 | --
4 | -- Clement Farabet
5 | --
6 |
7 | segmtools = {}
8 |
9 | --
10 | -- this function prints segmentation components' names at their
11 | -- centroid
12 | -- components are assumed to be a list of entries, each entry being
13 | -- an array: {[1]=x, [2]=y, [3]=size, [4]=class}
14 | -- size filtering is done: if size < minsize pixels, class is
15 | -- not shown (should probably be changed for something smarter)
16 | --
17 | function segmtools.overlayclasses (args)
18 | local painter = args.win or error 'arg win missing: a valid window/painter descriptor'
19 | local classes = args.classes or error 'arg classes missing: a list of classes'
20 | local components = args.components or error 'arg components missing: a list of components to tag'
21 | local zoom = args.zoom or 1
22 | local fontsize = args.font or (10*zoom)
23 | local minsize = args.minsize or 1
24 | local offx = args.offx or 0
25 | local offy = args.offy or 0
26 |
27 | font = qt.QFont{serif=false, italic=false, size=fontsize or 10}
28 | painter:setfont(font)
29 | painter:setcolor('black')
30 |
31 | if components.centroid_x then
32 | for i = 1,components:size() do
33 | local size = components.surface[i]
34 | if size > minsize then
35 | local class = classes[components.id2class[components.id[i]][1]]
36 | local x = components.centroid_x[i]*zoom + offx
37 | - (#class)*fontsize/5
38 | local y = components.centroid_y[i]*zoom + offy
39 | +fontsize/4
40 | painter:moveto(x,y)
41 | painter:show(class)
42 | end
43 | end
44 | else
45 | for _,component in ipairs(components) do
46 | local size = component[3]
47 | if size > minsize then
48 | local class = classes[component[4]]
49 | local x = component[1]*zoom + offx - (#class)*fontsize/5
50 | local y = component[2]*zoom + offy + fontsize/4
51 | painter:moveto(x,y)
52 | painter:show(class)
53 | end
54 | end
55 | end
56 | end
57 |
--------------------------------------------------------------------------------
/road-net/u1net.net:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/road-net/u1net.net
--------------------------------------------------------------------------------
/road-net/unsup-cl-30.net:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/e-lab/torch7-demos/1f86d58c1fa954bc9ed4c02b95b709b927894e66/road-net/unsup-cl-30.net
--------------------------------------------------------------------------------
/saliency-itti/g.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 746
10 | 504
11 |
12 |
13 |
14 |
15 | 0
16 | 0
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 255
26 | 255
27 | 255
28 |
29 |
30 |
31 |
32 |
33 |
34 | 236
35 | 236
36 | 236
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | 255
46 | 255
47 | 255
48 |
49 |
50 |
51 |
52 |
53 |
54 | 229
55 | 229
56 | 229
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | 229
66 | 229
67 | 229
68 |
69 |
70 |
71 |
72 |
73 |
74 | 229
75 | 229
76 | 229
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | Form
85 |
86 |
87 | -
88 |
89 |
90 |
91 | 640
92 | 480
93 |
94 |
95 |
96 |
97 | 640
98 | 480
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 | 0
108 | 0
109 | 0
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 | 0
119 | 0
120 | 0
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 | 185
130 | 185
131 | 185
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 | QFrame::StyledPanel
140 |
141 |
142 | QFrame::Raised
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
--------------------------------------------------------------------------------
/saliency-net/g.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 746
10 | 504
11 |
12 |
13 |
14 |
15 | 0
16 | 0
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 255
26 | 255
27 | 255
28 |
29 |
30 |
31 |
32 |
33 |
34 | 236
35 | 236
36 | 236
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | 255
46 | 255
47 | 255
48 |
49 |
50 |
51 |
52 |
53 |
54 | 229
55 | 229
56 | 229
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | 229
66 | 229
67 | 229
68 |
69 |
70 |
71 |
72 |
73 |
74 | 229
75 | 229
76 | 229
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | Form
85 |
86 |
87 | -
88 |
89 |
90 |
91 | 640
92 | 480
93 |
94 |
95 |
96 |
97 | 640
98 | 480
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 | 0
108 | 0
109 | 0
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 | 0
119 | 0
120 | 0
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 | 185
130 | 185
131 | 185
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 | QFrame::StyledPanel
140 |
141 |
142 | QFrame::Raised
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
--------------------------------------------------------------------------------
/saliency-net/run-sdl.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ------------------------------------------------------------
3 | -- a simple convnet saliency demo
4 | -- takes output of convnet 1st layer and displays it
5 | -- uses SDL graphic library
6 | -- uses face detector net
7 | --
8 | -- E. Culurciello, 2013
9 | --
10 | ------------------------------------------------------------
11 |
12 | require 'camera'
13 | require 'nnx'
14 | require 'ffmpeg'
15 | require 'luasdl'
16 |
17 | -- parse args
18 | op = xlua.OptionParser('%prog [options]')
19 | op:option{'-c', '--camera', action='store', dest='camidx',
20 | help='if source=camera, you can specify the camera index: /dev/videoIDX',
21 | default=0}
22 | op:option{'-n', '--network', action='store', dest='network',
23 | help='path to existing [trained] network',
24 | default='../face-detector/face.net'}
25 | --default='/Users/eugenioculurciello/Code/torch/neuflow-demos/scene-parsing/stanford.net'}
26 | op:option{'-v', '--video', action='store', dest='video',
27 | help='video file to process'}
28 | op:option{'-k', '--seek', action='store', dest='seek',
29 | help='seek number of seconds', default=0}
30 | op:option{'-f', '--fps', action='store', dest='fps',
31 | help='number of frames per second', default=10}
32 | op:option{'-t', '--time', action='store', dest='seconds',
33 | help='length to process (in seconds)', default=10}
34 | op:option{'-w', '--width', action='store', dest='width',
35 | help='resize video, width', default=600}
36 | op:option{'-h', '--height', action='store', dest='height',
37 | help='resize video, height', default=600}
38 | op:option{'-z', '--zoom', action='store', dest='zoom',
39 | help='display zoom', default=1}
40 | opt,args = op:parse()
41 |
42 | torch.setdefaulttensortype('torch.FloatTensor')
43 |
44 | -- profiler
45 | p = xlua.Profiler()
46 |
47 | -- init display SDL:
48 | luasdl.init(1200, 600)
49 |
50 | -- setup camera
51 | S_x = opt.width
52 | S_y = opt.height
53 | -- load video
54 | if opt.video then
55 | video = {}
56 | video.forward = function()
57 | return i
58 | end
59 | video = ffmpeg.Video{path=opt.video,
60 | width=1800, height=1000,
61 | fps=opt.fps, length=opt.seconds, seek=opt.seek,
62 | encoding='jpg',
63 | delete=false}
64 | else
65 | camera = image.Camera{}
66 | end
67 | img1=torch.Tensor(3,S_y,S_x)
68 | img2=torch.Tensor(3,S_y,S_x)
69 | frame=torch.Tensor(S_y,S_x) -- filter is 5x5 so lose 2 pix on each sides
70 |
71 | -- load network and clone 1st layer
72 | network = torch.load(opt.network):float()
73 | net2 = nn.Sequential()
74 | net2:add(network.modules[1])
75 | net2:add(network.modules[2])
76 | net2:add(nn.Sum(1))
77 |
78 | -- process function
79 | function process()
80 | img2=img2:copy(img1)
81 | --get frame:
82 | if opt.video then
83 | img1 = image.scale(image.crop(video:forward(), 1200, 500, 1800, 1000),S_x,S_y)
84 | else
85 | img1 = image.scale(camera:forward(),S_x,S_y)
86 | end
87 | -- process:
88 | nout = net2:forward(img1[2]) -- pass RGB
89 | out = image.scale(nout, S_x, S_y)
90 | frame:mul(0):add(out):mul(0.5) -- :add(-torch.min(frame)):div(torch.max(frame)):mul(0.2) -- reset, then normalize
91 | --colorop = (img1[2]-img1[1]) + (img1[2]-img1[3]) -- color opponency
92 | --colorop:mul(0.5)--:add(-torch.min(colorop)):div(torch.max(colorop)):mul(0.5) -- normalize
93 | --tmpdiff = (img1[2]-img2[2]) -- temp diff
94 | --tmpdiff:add(-torch.min(tmpdiff)):div(torch.max(tmpdiff)):mul(0.6) -- normalize
95 | --frame:add(colorop)--:add(tmpdiff):add(colorop) -- add temp diff, color opp
96 | end
97 |
98 | -- display function
99 | function display()
100 | luasdl.display(torch.cat(img1, frame:reshape(1,S_x,S_y):expand(3,S_x,S_y)))
101 | end
102 |
103 | -- display loop:
104 | while true do
105 | p:start('full loop','fps')
106 | p:start('prediction','fps')
107 | process()
108 | p:lap('prediction')
109 | p:start('display','fps')
110 | display()
111 | p:lap('display')
112 | p:lap('full loop')
113 | p:printAll()
114 | end
115 |
116 |
--------------------------------------------------------------------------------
/segment-color/g.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 746
10 | 504
11 |
12 |
13 |
14 |
15 | 0
16 | 0
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 255
26 | 255
27 | 255
28 |
29 |
30 |
31 |
32 |
33 |
34 | 236
35 | 236
36 | 236
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | 255
46 | 255
47 | 255
48 |
49 |
50 |
51 |
52 |
53 |
54 | 229
55 | 229
56 | 229
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | 229
66 | 229
67 | 229
68 |
69 |
70 |
71 |
72 |
73 |
74 | 229
75 | 229
76 | 229
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | Form
85 |
86 |
87 | -
88 |
89 |
90 |
91 | 640
92 | 480
93 |
94 |
95 |
96 |
97 | 640
98 | 480
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 | 0
108 | 0
109 | 0
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 | 0
119 | 0
120 | 0
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 | 185
130 | 185
131 | 185
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 | QFrame::StyledPanel
140 |
141 |
142 | QFrame::Raised
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
--------------------------------------------------------------------------------
/segment-color/run.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env qlua
2 | ------------------------------------------------------------
3 | -- a simple frame grabber demo.
4 | --
5 | -- Clement Farabet
6 | --
7 |
8 | require 'xlua'
9 | require 'torch'
10 | require 'qt'
11 | require 'qtwidget'
12 | require 'qtuiloader'
13 | xrequire('camera',true)
14 |
15 | -- parse args
16 | op = xlua.OptionParser('%prog [options]')
17 | op:option{'-c', '--camera', action='store', dest='camidx',
18 | help='if source=camera, you can specify the camera index: /dev/videoIDX',
19 | default=0}
20 | opt,args = op:parse()
21 |
22 | -- setup GUI (external UI file)
23 | widget = qtuiloader.load('g.ui')
24 | win = qt.QtLuaPainter(widget.frame)
25 |
26 | -- setup camera
27 | camera = image.Camera(opt.camidx)
28 |
29 | -- process function
30 | function process()
31 | -- grab frame
32 | frame = camera:forward()
33 |
34 | -- color segment:
35 | segnum = 4 -- since image is max 1 we get 4 colors/plane this way
36 | frame=torch.ceil(frame*segnum)
37 |
38 | end
39 |
40 | -- display function
41 | function display()
42 | zoom = 1
43 | win:gbegin()
44 | win:showpage()
45 | image.display{image=frame, win=win, zoom=zoom}
46 | win:gend()
47 | end
48 |
49 | -- setup gui
50 | local timer = qt.QTimer()
51 | timer.interval = 10
52 | timer.singleShot = true
53 | qt.connect(timer,
54 | 'timeout()',
55 | function()
56 | process()
57 | display()
58 | timer:start()
59 | end)
60 |
61 | -- calls back for all buttons
62 |
63 | widget.windowTitle = 'Color segmenter'
64 | widget:show()
65 | timer:start()
66 |
--------------------------------------------------------------------------------
/simple-frame-grabber/run.lua:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env torch
2 | ------------------------------------------------------------
3 | -- a simple frame grabber demo.
4 | --
5 | -- Clement Farabet
6 | --
7 |
8 | require 'xlua'
9 | require 'torch'
10 | require 'qt'
11 | require 'qtwidget'
12 | require 'qtuiloader'
13 | require 'camera'
14 |
15 | -- parse args
16 | op = xlua.OptionParser('%prog [options]')
17 | op:option{'-c', '--camera', action='store', dest='camidx',
18 | help='camera index: /dev/videoIDX', default=0}
19 | opt,args = op:parse()
20 |
21 | -- setup GUI (external UI file)
22 | widget = qtuiloader.load('g.ui')
23 | win = qt.QtLuaPainter(widget.frame)
24 |
25 | -- setup camera
26 | camera = image.Camera(opt.camidx)
27 |
28 | -- process function
29 | function process()
30 | -- grab frame
31 | frame = camera:forward()
32 |
33 | -- transform it
34 | if transform == 'rgb' then
35 | transformed = frame
36 | else
37 | transformed = image[transform](frame)
38 | end
39 | end
40 |
41 | -- display function
42 | function display()
43 | zoom = 1
44 | win:gbegin()
45 | win:showpage()
46 | image.display{image=frame, win=win, zoom=zoom}
47 | image.display{image=transformed, win=win, x=frame:size(3)*zoom, zoom=zoom}
48 | win:gend()
49 | end
50 |
51 | -- setup gui
52 | local timer = qt.QTimer()
53 | timer.interval = 10
54 | timer.singleShot = true
55 | qt.connect(timer,
56 | 'timeout()',
57 | function()
58 | process()
59 | display()
60 | timer:start()
61 | end)
62 |
63 | -- calls back for all buttons
64 | transform = 'rgb'
65 | qt.connect(qt.QtLuaListener(widget.rgb),
66 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
67 | function (...) transform = 'rgb' end)
68 |
69 | qt.connect(qt.QtLuaListener(widget.rgb2y),
70 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
71 | function (...) transform = 'rgb2y' end)
72 |
73 | qt.connect(qt.QtLuaListener(widget.rgb2yuv),
74 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
75 | function (...) transform = 'rgb2yuv' end)
76 |
77 | qt.connect(qt.QtLuaListener(widget.rgb2hsl),
78 | 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
79 | function (...) transform = 'rgb2hsl' end)
80 |
81 | widget.windowTitle = 'A Simple Frame Grabber'
82 | widget:show()
83 | timer:start()
84 |
--------------------------------------------------------------------------------
/temporal-difference/frame.lua:
--------------------------------------------------------------------------------
1 | local frame = {}
2 |
3 | local pf = function(...) print(string.format(...)) end
4 | local Cr = sys.COLORS.red
5 | local Cb = sys.COLORS.blue
6 | local Cg = sys.COLORS.green
7 | local Cn = sys.COLORS.none
8 | local THIS = sys.COLORS.blue .. 'THIS' .. Cn
9 |
10 | local function prep_libvideo_decoder_video(opt, source)
11 | local cam = assert(require('libvideo_decoder'))
12 | cam.loglevel(opt.loglevel)
13 |
14 | local status = false
15 | status, source.h, source.w, source.length, source.fps = cam.init(opt.vp);
16 | if not status then
17 | error("No video")
18 | else
19 | if opt.loglevel > 0 then
20 | pf(Cb..'video statistics: %s fps, %dx%d (%s frames)'..Cn,
21 | (source.fps and tostring(source.fps) or 'unknown'),
22 | source.h,
23 | source.w,
24 | (source.length and tostring(source.length) or 'unknown'))
25 | end
26 | end
27 |
28 | -- video library only handles byte tensors
29 | local img_tmp = torch.FloatTensor(opt.batch, 3, source.h, source.w)
30 |
31 | -- set frame forward function
32 | frame.forward = function(img)
33 | local n = opt.batch
34 | for i=1,opt.batch do
35 | if not cam.frame_rgb(img_tmp[i]) then
36 | if i == 1 then
37 | return false
38 | end
39 | n = i-1
40 | break
41 | end
42 | end
43 | if n == opt.batch then
44 | img = img_tmp:clone()
45 | else
46 | img = img_tmp:narrow(1,1,n):clone()
47 | end
48 |
49 | return img
50 | end
51 |
52 | source.cam = cam
53 | end
54 |
55 |
56 | function frame:init(opt, source)
57 | prep_libvideo_decoder_video(opt, source)
58 | end
59 |
60 | return frame
61 |
--------------------------------------------------------------------------------
/temporal-difference/lib/Makefile:
--------------------------------------------------------------------------------
1 | UNAME_S := $(shell uname -s)
2 | UNAME_P := $(shell uname -p)
3 | LBITS := $(shell getconf LONG_BIT)
4 |
5 | INCLUDE = -I. -I/usr/local/include -I$(HOME)/torch/install/include
6 | LDFLAGS := -lavutil -lavformat -lavcodec -lssl
7 | LIBOPTS = -shared -L/usr/local/lib/lua/5.1 -L/usr/local/lib
8 | CFLAGS = -O3 -fopenmp -c -fpic -Wall -DINCLUDEOPENSSL -DUSE_SHA1
9 | VPATH = fileio
10 | VIDEODEC_FILES = video_decoder.o
11 | LIBFILES = libvideo_decoder.so
12 | CC = gcc
13 |
14 | ifneq ($(filter arm%,$(UNAME_P)),)
15 | CFLAGS += -mfpu=neon
16 | endif
17 |
18 | ifeq ($(UNAME_S),Linux)
19 | VIDEODEC_FILES += videocap.o videocodec.o
20 | CFLAGS += -DDOVIDEOCAP
21 | endif
22 |
23 | ifeq ($(UNAME_S),Darwin)
24 | LDFLAGS += -lTH -lluajit -lluaT
25 | CFLAGS += -DDARWIN
26 | endif
27 |
28 |
29 | .PHONY : all
30 | all : $(LIBFILES)
31 |
32 | .c.o:
33 | $(CC) $(CFLAGS) $(INCLUDE) $<
34 |
35 | libvideo_decoder.so : $(VIDEODEC_FILES)
36 | $(CC) $(VIDEODEC_FILES) $(LIBOPTS) -o $@ $(LDFLAGS)
37 |
38 | install : $(LIBFILES)
39 | sudo cp $(LIBFILES) /usr/local/lib/lua/5.1/
40 |
41 | uninstall :
42 | sudo rm /usr/local/lib/lua/5.1/libvideo_decoder.so
43 |
44 | .PHONY : clean
45 | clean :
46 | rm -f *.o $(LIBFILES)
47 |
--------------------------------------------------------------------------------
/temporal-difference/lib/http.h:
--------------------------------------------------------------------------------
1 | #ifndef _HTTP_H_INCLUDED_
2 | #define _HTTP_H_INCLUDED_
3 |
4 | #ifdef INCLUDEOPENSSL
5 | #include
6 | #endif
7 |
8 | #include
9 | #include
10 |
11 | #define HTTPERR_NOERROR 0
12 | #define HTTPERR_FILENOTFOUND -1
13 | #define HTTPERR_SOCKET -2
14 | #define HTTPERR_RESOLVE -3
15 | #define HTTPERR_CONNECTIONREFUSED -4
16 | #define HTTPERR_COMM -5
17 | #define HTTPERR_MISSINGTLS -6
18 | #define HTTPERR_CERT -7
19 | #define HTTPERR_INITFIRST -8
20 | #define HTTPERR_PROTOCOL -9
21 | #define HTTPERR_NOTCONNECTED -10
22 | #define HTTPERR_DECODE -11
23 |
24 | int Resolve(const char *address, struct sockaddr_in *addr, int defaultport);
25 | #ifdef INCLUDEOPENSSL
26 | extern SSL_CTX *ssl_client_ctx; // Client SSL context
27 | #endif
28 |
29 | int postfile(const char *url, const char *path, const char *destfilename, const char *username,
30 | const char *password, const char *device, char *retbuf, size_t retbufsize);
31 | int https_init(const char *certfile);
32 | const char *http_error(int rc);
33 |
34 | // Defined in mpjpeg.c
35 | int mpjpeg_disconnect();
36 | int mpjpeg_connect(const char *url);
37 | int mpjpeg_getdata(char **data, unsigned *datalen, long *timestamp);
38 |
39 | #endif
40 |
--------------------------------------------------------------------------------
/temporal-difference/lib/videocap.h:
--------------------------------------------------------------------------------
1 | #ifndef _VIDEOCAP_H_INCLUDED_
2 | #define _VIDEOCAP_H_INCLUDED_
3 |
4 | #include
5 |
6 | #define VIDEOCAP_ERR_OK 0
7 | #define VIDEOCAP_ERR_SET_FORMAT -1
8 | #define VIDEOCAP_ERR_REQBUFS -2
9 | #define VIDEOCAP_ERR_MALLOC -3
10 | #define VIDEOCAP_ERR_ENQUEUE_BUFFERS -4
11 | #define VIDEOCAP_ERR_DEQUEUE_BUFFERS -5
12 | #define VIDEOCAP_ERR_START_STREAMING -6
13 | #define VIDEOCAP_ERR_QUERY -7
14 | #define VIDEOCAP_ERR_QUERYBUF -8
15 | #define VIDEOCAP_ERR_MMAP -9
16 | #define VIDEOCAP_ERR_SET_FRAMERATE -10
17 | #define VIDEOCAP_ERR_ENUM -12
18 |
19 | // Open the video device (normally /dev/videoN) and return a handle (0 if open was unsuccessful)
20 | void *videocap_open(const char *devname);
21 | // Return the capabilities of the opened device (check V4L2 documentation for the description of v4l2_capability)
22 | int videocap_capabilities(void *v, struct v4l2_capability *cap);
23 | // Return the available frame rates for the given resolution and format; index can range from 0 until an error is returned
24 | // format is one of the V4L2_FMT_ constants
25 | int videocap_framerates(void *v, int index, int w, int h, unsigned format, double *fps);
26 | // Return the available formats; index can range from 0 until an error is returned
27 | int videocap_formats(void *v, int index, char *desc, unsigned *pixelformat);
28 | // Start capture at the specified resolution, format, fps and number of buffers. If fps is zero, the default will be used
29 | int videocap_startcapture(void *v, int w, int h, unsigned format, int fps, int nbuffers);
30 | // Wait and return a pointer to a captured frame; tv contains the time of the capture
31 | int videocap_getframe(void *v, char **frame, struct timeval *tv);
32 | // Stop every activity and close the video capture device
33 | int videocap_close(void *v);
34 |
35 | #endif
36 |
--------------------------------------------------------------------------------
/temporal-difference/lib/videocodec.h:
--------------------------------------------------------------------------------
1 | #ifndef _VIDEOCODEC_H_INCLUDED_
2 | #define _VIDEOCODEC_H_INCLUDED_
3 |
4 | #include
5 |
6 | #define VIDEOCODEC_ERR_OK 0
7 | #define VIDEOCODEC_ERR_SET_FORMAT -1
8 | #define VIDEOCODEC_ERR_REQBUFS -2
9 | #define VIDEOCODEC_ERR_MALLOC -3
10 | #define VIDEOCODEC_ERR_ENQUEUE_BUFFERS -4
11 | #define VIDEOCODEC_ERR_DEQUEUE_BUFFERS -5
12 | #define VIDEOCODEC_ERR_START_STREAMING -6
13 | #define VIDEOCODEC_ERR_QUERY -7
14 | #define VIDEOCODEC_ERR_QUERYBUF -8
15 | #define VIDEOCODEC_ERR_MMAP -9
16 | #define VIDEOCODEC_ERR_SET_FRAMERATE -10
17 | #define VIDEOCODEC_ERR_SET_CODEC -11
18 | #define VIDEOCODEC_ERR_AVFORMAT -12
19 | #define VIDEOCODEC_ERR_BADURL -13
20 | #define VIDEOCODEC_ERR_WRITE -14
21 | #define VIDEOCODEC_ERR_NOTINIT -15
22 | #define VIDEOCODEC_ERR_FINISHED -16
23 |
24 | void *videocodec_open(const char *devname);
25 | void *videocodec_opendecoder(const char *devname);
26 | int videocodec_capabilities(void *v, struct v4l2_capability *cap);
27 | int videocodec_setformat(void *v, int w, int h, unsigned format, int fps);
28 | int videocodec_setcodec(void *v, int codec);
29 | int videocodec_setcodecparam(void *v, int id, int value);
30 | int videocodec_start(void *v);
31 | int videocodec_process(void *v, const char *inframe, unsigned inframelen, char **outframe, unsigned *outframelen, int *keyframe);
32 | int videocodec_close(void *v);
33 | int videocodec_createfile(void *v, const char *url, const char *format);
34 | int videocodec_writefile(void *v, const char *inframe, unsigned inframelen);
35 | int videocodec_writeencfile(void *v, const char *inframe, unsigned inframelen);
36 | #endif
37 |
--------------------------------------------------------------------------------
/temporal-difference/run.lua:
--------------------------------------------------------------------------------
1 | --------------------------------------------------------------------------------
2 | -- temporal difference demo
3 | -- E. Culurciello, May 2015
4 | --
5 | --------------------------------------------------------------------------------
6 |
7 | -- Requires --------------------------------------------------------------------
8 | require 'pl'
9 | require 'nn'
10 | require 'sys'
11 | require 'paths'
12 | require 'image'
13 | require 'qtwidget'
14 | local frame = assert(require('frame'))
15 |
16 | -- Local definitions -----------------------------------------------------------
17 | local pf = function(...) print(string.format(...)) end
18 | local Cr = sys.COLORS.red
19 | local Cb = sys.COLORS.blue
20 | local Cg = sys.COLORS.green
21 | local Cn = sys.COLORS.none
22 | local THIS = sys.COLORS.blue .. 'THIS' .. Cn
23 |
24 | -- Title definition -----------------------------------------------------------
25 | title = [[
26 | Driving video parser
27 | ]]
28 |
29 | -- Options ---------------------------------------------------------------------
30 | opt = lapp(title .. [[
31 | --nt (default 8) Number of threads for multiprocessing
32 | -z, --zoom (default 1) Zoom ouput window
33 | --vp (default localhost) file path or IP address of video stream
34 | --batch (default 1) Batch size of images for batch processing
35 | --fps (default 30) Frames per second (camera setting)
36 | --loglevel (default 1) Logging level from 0 (no logging) to 10
37 | ]])
38 |
39 | pf(Cb..title..Cn)
40 | torch.setdefaulttensortype('torch.FloatTensor')
41 | torch.setnumthreads(opt.nt)
42 | print('Number of threads used:', torch.getnumthreads())
43 |
44 | -- global objects:
45 | source = {} -- source object
46 | source.ldirn = paths.dirname(opt.vp) -- base directory of the source video
47 | source.fps = opt.fps
48 | -- local src = torch.FloatTensor(3, source.h, source.w)
49 |
50 | -- init application packages
51 | frame:init(opt, source)
52 |
53 | -- profiling timers
54 | local timer = torch.Timer()
55 | local t_loop = 1 -- init to 1s
56 |
57 |
58 | -- display
59 | local display = {} -- main display object
60 | local zoom = opt.zoom
61 | local loop_idx = 0
62 | local side = math.min(source.h, source.w)
63 | local z = side / 512 -- zoom
64 | local win_w = zoom*source.w
65 | local win_h = zoom*source.h
66 |
67 | -- offset in display (we process scaled and display)
68 | local offsd = zoom
69 | if opt.spatial == 1 then
70 | offsd = zoom*source.h/eye
71 | end
72 |
73 | if not win then
74 | win = qtwidget.newwindow(win_w, win_h, 'Driving Video Parser')
75 | else
76 | win:resize(win_w, win_h)
77 | end
78 |
79 | -- Set font size to a visible dimension
80 | win:setfontsize(zoom*20*z)
81 |
82 |
83 | display.forward = function(output, img, fps)
84 | win:gbegin()
85 | win:showpage()
86 |
87 | -- display frame
88 | image.display{image = img, win = win, zoom = zoom}
89 |
90 | win:gend()
91 | end
92 |
93 | -- set screen grab function
94 | display.screen = function()
95 | return win:image()
96 | end
97 |
98 | -- set continue function
99 | display.continue = function()
100 | return win:valid()
101 | end
102 |
103 | -- set close function
104 | display.close = function()
105 | if win:valid() then
106 | win:close()
107 | end
108 | end
109 |
110 |
111 | -- process
112 | process = {}
113 | function process.forward(src)
114 | process.p1 = process.p1 or src -- previous frame
115 | process.tdi = process.tdi or torch.Tensor(src):zero()
116 |
117 | process.td = torch.abs(process.p1 - src) -- temporal diff images
118 |
119 | process.p1 = src:clone() --copy frame as previous frame
120 |
121 | return process.td
122 | end
123 |
124 |
125 | -- create main functions
126 | local main = function()
127 | while display.continue() do
128 | timer:reset()
129 |
130 | src = frame.forward(src)
131 | if not src then
132 | break
133 | end
134 |
135 | local img = process.forward(src)
136 |
137 | display.forward(result, img, (1/t_loop))
138 |
139 | t_loop = timer:time().real
140 |
141 | collectgarbage()
142 | end
143 | if status then
144 | print('process done!')
145 | else
146 | print('Error ' .. err)
147 | end
148 | display.close()
149 | end
150 |
151 | -- execute main loop
152 | main()
153 |
--------------------------------------------------------------------------------
/tensors/slicing.lua:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 | -- slicing.lua
3 | --
4 | -- This script demonstrates tensor slicing / manipulation.
5 |
6 | -- To run this script, simply do:
7 | -- torch slicing.lua
8 | -- and then press 'y' or 'return' at each step, to keep going.
9 |
10 | -- little function to pause execution, and request user input
11 | function next()
12 | local answer = nil
13 | while answer ~= '' and answer ~= 'y' and answer ~= 'Y' and neverstall ~= true do
14 | io.write("continue ([y]/n/!)? ")
15 | io.flush()
16 | answer=io.read()
17 | if answer == '!' then
18 | neverstall = true
19 | end
20 | if answer == 'n' then
21 | print('exiting...')
22 | os.exit()
23 | end
24 | end
25 | print ''
26 | end
27 |
28 | print '----------------------------------------------------------------------'
29 | print 'creating a few tensors'
30 |
31 | t1 = torch.range(1,75):resize(3,5,5)
32 | print 't1 = torch.range(1,75):resize(3,5,5)'
33 | print 't1 = '
34 | print(t1)
35 |
36 | t2 = torch.range(1,25):resize(5,5)
37 | print 't2 = torch.range(1,25):resize(5,5)'
38 | print 't2 = '
39 | print(t2)
40 |
41 | print 'done.'
42 | print ''
43 |
44 | next()
45 | print '----------------------------------------------------------------------'
46 | print 'the most basic slicing is done using the [] operator'
47 | print ''
48 |
49 | print 't1 ='
50 | print( t1 )
51 |
52 | print 't1[2] ='
53 | print( t1[2] )
54 |
55 | next()
56 | print '----------------------------------------------------------------------'
57 | print 't1_1 is a view in the existing t1 tensor: changing the values'
58 | print 'in t1_1 directly affects t1:'
59 | print ''
60 |
61 | t1[2]:fill(7)
62 | print 't1[2]:fill(7)'
63 |
64 | print 't1[2] ='
65 | print( t1[2] )
66 |
67 | print 't1 ='
68 | print( t1 )
69 |
70 | next()
71 | print '----------------------------------------------------------------------'
72 | print 'more complex slicing can be done using the [{}] operator'
73 | print 'this operator lets you specify one list/number per dimension'
74 | print 'for example, t2 is a 2-dimensional tensor, therefore'
75 | print 'we should pass 2 lists/numbers to the [{}] operator:'
76 | print ''
77 |
78 | t2_slice1 = t2[{ {},2 }]
79 | t2_slice2 = t2[{ 2,{} }] -- equivalent to t2[2]
80 | t2_slice3 = t2[{ {2},{} }]
81 | t2_slice4 = t2[{ {1,3},{3,4} }]
82 | t2_slice5 = t2[{ {3},{4} }]
83 | t2_slice6 = t2[{ 3,4 }]
84 |
85 | print 't2 = '
86 | print(t2)
87 |
88 | print 't2[{ {},2 }] ='
89 | print(t2_slice1)
90 |
91 | print 't2[{ 2,{} }] ='
92 | print(t2_slice2)
93 |
94 | print 't2[{ {2},{} }] ='
95 | print(t2_slice3)
96 |
97 | print 't2[{ {1,3},{3,4} }] ='
98 | print(t2_slice4)
99 |
100 | print 't2[{ {3},{4} }] ='
101 | print(t2_slice5)
102 |
103 | print 't2[{ 3,4 }] ='
104 | print(t2_slice6)
105 |
106 | next()
107 | print '----------------------------------------------------------------------'
108 | print 'negative indexes can also be used:'
109 | print ''
110 |
111 | t2_slice7 = t2[{ {},{2,-2} }]
112 | t2_slice8 = t2[{ -1,-1 }]
113 |
114 | print 't2[{ {},{2,-2} }] ='
115 | print(t2_slice7)
116 |
117 | print 't2[{ -1,-1 }] ='
118 | print(t2_slice8)
119 |
120 | next()
121 | print '----------------------------------------------------------------------'
122 | print 'in basic Lua, the = operator cannot be overloaded (that speeds up the language parser'
123 | print 'a lot...), but you can use the [{}] operator to copy tensors, and subtensors:'
124 | print ''
125 |
126 | print 't3 = torch.Tensor(5)'
127 | print 't3[{}] = t2[{ {},1 }]'
128 |
129 | t3 = torch.Tensor(5)
130 | t3[{}] = t2[{ {},1 }]
131 |
132 | print 't3 ='
133 | print(t3)
134 |
135 | next()
136 | print '----------------------------------------------------------------------'
137 | print 'if you need to slice arbitrary subtensors, you will need to do it in steps:'
138 | print ''
139 |
140 | t4 = torch.Tensor(5,2)
141 | t4[{ {},1 }] = t2[{ {},2 }]
142 | t4[{ {},2 }] = t2[{ {},5 }]
143 |
144 | print [[
145 | t4 = torch.Tensor(5,2)
146 | t4[{ {},1 }] = t2[{ {},2 }]
147 | t4[{ {},2 }] = t2[{ {},5 }]
148 | ]]
149 |
150 | print 't4 ='
151 | print(t4)
152 |
153 |
154 |
155 |
156 |
157 |
158 |
--------------------------------------------------------------------------------
/tracker/.gitignore:
--------------------------------------------------------------------------------
1 | # files
2 | *.pyc
3 | *.sw?
4 |
5 | # folders
6 | /scratch
7 |
8 | # OS generated files
9 | .DS_Store
10 | .DS_Store?
11 | ._*
12 | .Spotlight-V100
13 | .Trashes
14 |
--------------------------------------------------------------------------------
/tracker/README.md:
--------------------------------------------------------------------------------
1 | # Tracking with Deep Neural Networks
2 |
3 | This script provides a real-time tracking with a video sequence
4 | from a camera or datasets (presented at CISS 2013)
5 |
6 | ## Install
7 |
8 | To run this program, you will need to install Torch7 and 'nnx' package
9 | In addition to the usual (torch, nnx, etc.) this demo requires the
10 | following modules to be installed via luarocks:
11 |
12 | ``` sh
13 | image
14 | torchffi
15 | ffmpeg
16 | ```
17 |
18 | ## Demonstration
19 |
20 | For the demonstration, use the following commands:
21 |
22 | ``` sh
23 | $ torch run.lua
24 | ```
25 |
--------------------------------------------------------------------------------
/tracker/compile.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # source (http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html)
4 |
5 | gcc -c -Wall -Werror -fpic fastdist.c
6 | gcc -shared -o libfastdist.so fastdist.o
7 |
8 | ## install in default path
9 | #
10 | #cp $PWD/libxillyconv.so /usr/lib
11 | #chmod 0755 /usr/lib/libxillyconv.so
12 | #ldconfig
13 | #gcc -Wall -o test xillyconv-test.c -lxillyconv
14 | #./test
15 |
--------------------------------------------------------------------------------
/tracker/fastdist.c:
--------------------------------------------------------------------------------
1 | /*
2 | fastdist.c
3 |
4 | different kinds of template matching
5 |
6 | Jonghoon Jin 2013
7 | */
8 |
9 | #include
10 | #include
11 | #include "fastdist.h"
12 |
13 | #define max(a,b) ((a)>(b) ? (a) : (b))
14 | #define abs(a) (a) < 0 ? -(a) : (a)
15 | #define square(a) (a)*(a)
16 |
17 | int RBF(float *input, int ichannels, int iheight, int iwidth,
18 | float *output, int numProto, float *code, float *weight, float *std)
19 | {
20 | int i,k,x,y;
21 | float dist, yi_hat, sigma;
22 |
23 | for (y=0; y 0 and maxLoad < nExample then
36 | nExample = maxLoad
37 | print(' loading only ' .. nExample .. ' examples')
38 | end
39 | print(' reading ' .. nExample .. ' examples with ' .. dim-1 .. '+1 dimensions...')
40 | local tensor = torch.Tensor(nExample, dim)
41 | tensor:storage():copy(f:readFloat(nExample*dim))
42 | print(' done')
43 |
44 | local dataset = {}
45 | dataset.tensor = tensor
46 |
47 | function dataset:normalize(mean_, std_)
48 | local data = tensor:narrow(2, 1, dim-1)
49 | local std = std_ or torch.std(data, 1, true)
50 | local mean = mean_ or torch.mean(data, 1)
51 | for i=1,dim-1 do
52 | tensor:select(2, i):add(-mean[1][i])
53 | if std[1][i] > 0 then
54 | tensor:select(2, i):mul(1/std[1][i])
55 | end
56 | end
57 | return mean, std
58 | end
59 |
60 | function dataset:normalizeGlobal(mean_, std_)
61 | local data = tensor:narrow(2, 1, dim-1)
62 | local std = std_ or data:std()
63 | local mean = mean_ or data:mean()
64 | data:add(-mean)
65 | data:mul(1/std)
66 | return mean, std
67 | end
68 |
69 | dataset.dim = dim-1
70 |
71 | function dataset:size()
72 | return nExample
73 | end
74 |
75 | local labelvector = torch.zeros(10)
76 |
77 | setmetatable(dataset, {__index = function(self, index)
78 | local input = tensor[index]:narrow(1, 1, dim-1)
79 | local class = tensor[index][dim]+1
80 | local label = labelvector:zero()
81 | label[class] = 1
82 | local example = {input, label}
83 | return example
84 | end})
85 |
86 | return dataset
87 | end
88 |
89 | function mnist.loadConvDataset(fileName, maxLoad, geometry)
90 | local dataset = mnist.loadFlatDataset(fileName, maxLoad)
91 | local cdataset = {}
92 |
93 | function cdataset:normalize(m,s)
94 | return dataset:normalize(m,s)
95 | end
96 | function cdataset:normalizeGlobal(m,s)
97 | return dataset:normalizeGlobal(m,s)
98 | end
99 | function cdataset:size()
100 | return dataset:size()
101 | end
102 |
103 | local iheight = geometry[2]
104 | local iwidth = geometry[1]
105 | local inputpatch = torch.zeros(1, iheight, iwidth)
106 |
107 | setmetatable(cdataset, {__index = function(self,index)
108 | local ex = dataset[index]
109 | local input = ex[1]
110 | local label = ex[2]
111 | local w = math.sqrt(input:nElement())
112 | local uinput = input:unfold(1,input:nElement(),input:nElement())
113 | local cinput = uinput:unfold(2,w,w)
114 | local h = cinput:size(2)
115 | local w = cinput:size(3)
116 | local x = math.floor((iwidth-w)/2)+1
117 | local y = math.floor((iheight-h)/2)+1
118 | inputpatch:narrow(3,x,w):narrow(2,y,h):copy(cinput)
119 | local example = {inputpatch, label}
120 | return example
121 | end})
122 | return cdataset
123 | end
124 |
--------------------------------------------------------------------------------
/train-face-detector-elab/model.lua:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 | -- Create CNN and loss to optimize.
3 | --
4 | -- Clement Farabet
5 | ----------------------------------------------------------------------
6 |
7 | require 'torch' -- torch
8 | require 'image' -- to visualize the dataset
9 | require 'nn' -- provides all sorts of trainable modules/layers
10 | --require 'Dropout' -- Hinton dropout technique
11 |
12 | if opt.type == 'cuda' then
13 | nn.SpatialConvolutionMM = nn.SpatialConvolution
14 | end
15 |
16 | ----------------------------------------------------------------------
17 | print(sys.COLORS.red .. '==> define parameters')
18 |
19 | -- 2-class problem: faces!
20 | local noutputs = 2
21 |
22 | -- input dimensions: faces!
23 | local nfeats = 1
24 | local width = 32
25 | local height = 32
26 |
27 | -- hidden units, filter sizes (for ConvNet only):
28 | local nstates = {16,32}
29 | local filtsize = {5, 7}
30 | local poolsize = 4
31 |
32 | ----------------------------------------------------------------------
33 | print(sys.COLORS.red .. '==> construct CNN')
34 |
35 | local CNN = nn.Sequential()
36 |
37 | -- stage 1: conv+max
38 | CNN:add(nn.SpatialConvolutionMM(nfeats, nstates[1], filtsize[1], filtsize[1]))
39 | CNN:add(nn.SpatialMaxPooling(poolsize,poolsize,poolsize,poolsize))
40 | CNN:add(nn.Threshold())
41 |
42 | -- stage 2: conv+max
43 | CNN:add(nn.SpatialConvolutionMM(nstates[1], nstates[2], filtsize[2], filtsize[2]))
44 | CNN:add(nn.Threshold())
45 |
46 | local classifier = nn.Sequential()
47 | -- stage 3: linear
48 | classifier:add(nn.Reshape(nstates[2]))
49 | classifier:add(nn.Linear(nstates[2], 2))
50 |
51 | -- stage 4 : log probabilities
52 | classifier:add(nn.LogSoftMax())
53 |
54 | for _,layer in ipairs(CNN.modules) do
55 | if layer.bias then
56 | layer.bias:fill(.2)
57 | if i == #CNN.modules-1 then
58 | layer.bias:zero()
59 | end
60 | end
61 | end
62 |
63 | local model = nn.Sequential()
64 | model:add(CNN)
65 | model:add(classifier)
66 |
67 | -- Loss: NLL
68 | loss = nn.ClassNLLCriterion()
69 |
70 |
71 | ----------------------------------------------------------------------
72 | print(sys.COLORS.red .. '==> here is the CNN:')
73 | print(model)
74 |
75 | if opt.type == 'cuda' then
76 | model:cuda()
77 | loss:cuda()
78 | end
79 |
80 | -- return package:
81 | return {
82 | model = model,
83 | loss = loss,
84 | }
85 |
86 |
--------------------------------------------------------------------------------
/train-face-detector-elab/run.lua:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 | -- Train a ConvNet on faces.
3 | --
4 | -- original: Clement Farabet
5 | -- new version by: E. Culurciello
6 | -- Mon Oct 14 14:58:50 EDT 2013
7 | ----------------------------------------------------------------------
8 |
9 | require 'pl'
10 | require 'trepl'
11 | require 'torch' -- torch
12 | require 'image' -- to visualize the dataset
13 | require 'nn' -- provides all sorts of trainable modules/layers
14 |
15 | ----------------------------------------------------------------------
16 | print(sys.COLORS.red .. '==> processing options')
17 |
18 | opt = lapp[[
19 | -r,--learningRate (default 1e-3) learning rate
20 | -d,--learningRateDecay (default 1e-7) learning rate decay (in # samples)
21 | -w,--weightDecay (default 1e-5) L2 penalty on the weights
22 | -m,--momentum (default 0.1) momentum
23 | -d,--dropout (default 0.5) dropout amount
24 | -b,--batchSize (default 128) batch size
25 | -t,--threads (default 8) number of threads
26 | -p,--type (default float) float or cuda
27 | -i,--devid (default 1) device ID (if using CUDA)
28 | -s,--size (default small) dataset: small or full or extra
29 | -o,--save (default results) save directory
30 | --patches (default all) percentage of samples to use for testing'
31 | --visualize (default true) visualize dataset
32 | ]]
33 |
34 | -- nb of threads and fixed seed (for repeatable experiments)
35 | torch.setnumthreads(opt.threads)
36 | torch.manualSeed(1)
37 | torch.setdefaulttensortype('torch.FloatTensor')
38 |
39 | -- type:
40 | if opt.type == 'cuda' then
41 | print(sys.COLORS.red .. '==> switching to CUDA')
42 | require 'cunn'
43 | cutorch.setDevice(opt.devid)
44 | print(sys.COLORS.red .. '==> using GPU #' .. cutorch.getDevice())
45 | end
46 |
47 | ----------------------------------------------------------------------
48 | print(sys.COLORS.red .. '==> load modules')
49 |
50 | local data = require 'data'
51 | local train = require 'train'
52 | local test = require 'test'
53 |
54 | ----------------------------------------------------------------------
55 | print(sys.COLORS.red .. '==> training!')
56 |
57 | while true do
58 | train(data.trainData)
59 | test(data.testData)
60 | end
61 |
62 |
--------------------------------------------------------------------------------
/train-face-detector-elab/test.lua:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 | -- This script implements a test procedure, to report accuracy
3 | -- on the test data. Nothing fancy here...
4 | --
5 | -- Clement Farabet
6 | ----------------------------------------------------------------------
7 |
8 | require 'torch' -- torch
9 | require 'xlua' -- xlua provides useful tools, like progress bars
10 | require 'optim' -- an optimization package, for online and batch methods
11 |
12 | ----------------------------------------------------------------------
13 | print(sys.COLORS.red .. '==> defining some tools')
14 |
15 | -- model:
16 | local t = require 'model'
17 | local model = t.model
18 | local loss = t.loss
19 |
20 | -- This matrix records the current confusion across classes
21 | local confusion = optim.ConfusionMatrix(classes) -- faces: yes, no
22 |
23 | -- Logger:
24 | local testLogger = optim.Logger(paths.concat(opt.save, 'test.log'))
25 |
26 | -- Batch test:
27 | local inputs = torch.Tensor(opt.batchSize,testData.data:size(2),
28 | testData.data:size(3), testData.data:size(4)) -- get size from data
29 | local targets = torch.Tensor(opt.batchSize)
30 | if opt.type == 'cuda' then
31 | inputs = inputs:cuda()
32 | targets = targets:cuda()
33 | end
34 |
35 | ----------------------------------------------------------------------
36 | print(sys.COLORS.red .. '==> defining test procedure')
37 |
38 | -- test function
39 | function test(testData)
40 | -- local vars
41 | local time = sys.clock()
42 |
43 | -- test over test data
44 | print(sys.COLORS.red .. '==> testing on test set:')
45 | for t = 1,testData:size(),opt.batchSize do
46 | -- disp progress
47 | xlua.progress(t, testData:size())
48 |
49 | -- batch fits?
50 | if (t + opt.batchSize - 1) > testData:size() then
51 | break
52 | end
53 |
54 | -- create mini batch
55 | local idx = 1
56 | for i = t,t+opt.batchSize-1 do
57 | inputs[idx] = testData.data[i]
58 | targets[idx] = testData.labels[i]
59 | idx = idx + 1
60 | end
61 |
62 | -- test sample
63 | local preds = model:forward(inputs)
64 |
65 | -- confusion
66 | for i = 1,opt.batchSize do
67 | confusion:add(preds[i], targets[i])
68 | end
69 | end
70 |
71 | -- timing
72 | time = sys.clock() - time
73 | time = time / testData:size()
74 | print("\n==> time to test 1 sample = " .. (time*1000) .. 'ms')
75 |
76 | -- print confusion matrix
77 | print(confusion)
78 |
79 | -- update log/plot
80 | testLogger:add{['% mean class accuracy (test set)'] = confusion.totalValid * 100}
81 | if opt.plot then
82 | testLogger:style{['% mean class accuracy (test set)'] = '-'}
83 | testLogger:plot()
84 | end
85 | confusion:zero()
86 |
87 | end
88 |
89 | -- Export:
90 | return test
91 |
92 |
--------------------------------------------------------------------------------