import qualified Data.ByteString as B
import IHaskell.Display.Magic
import Control.Lens
import Data.Foldable as F
import Data.Word
:load NN
import NN
import Gen.Caffe.NetParameter as NP
import Gen.Caffe.LayerParameter as LP
:load NN/Examples/AlexNet.hs
import NN.Examples.AlexNet
f = "/tmp/x.graph.png"
showNet n = do
(png f . visualize . parse) n
B.readFile f
import NN.Graph
let l' = NN.Graph.layer'
showNet $ do
x <- l' $ dropout 0.5
y <- l' relu
return ()
showNet $ do
x <- l' $ def & ty Data
y <- l' relu
x >-> y
with y >- sequential [conv, softmax]
with x >- sequential [lrn, ip 10, accuracy 5]
return ()
:set -XRecordWildCards
import Data.Word
data Inception = Inception {_1x1, _3x3reduce, _3x3, _5x5reduce, _5x5, _poolProj :: Word32}
inception :: Node -> Inception -> G LayerParameter Node
inception input Inception{..} = do
columns' <- mapM sequential columns
concat'' <- layer' concat'
F.forM_ columns' $ \(bottom, top) -> input >-> bottom >> top >-> concat''
return concat''
where
columns = [
[conv & numOutputC' _1x1 & kernelSizeC' 1, relu],
[conv & numOutputC' _3x3reduce & kernelSizeC' 1, relu,
conv & numOutputC' _3x3 & kernelSizeC' 3 & padC' 1, relu],
[conv & numOutputC' _5x5reduce & kernelSizeC' 1, relu,
conv & numOutputC' _5x5 & kernelSizeC' 5 & padC' 2, relu],
[maxPool & sizeP' 3 & strideP' 3 & padP' 1,
conv & numOutputC' _poolProj & kernelSizeC' 1, relu]]
showNet $ do
x <- l' maxPool
inception x $ Inception 64 96 128 16 32 32
return ()
import NN.Backend.Caffe as Caffe
import NN.Passes
let optPasses = [addConnection] ++ optimizeInPlaceLayer ReLU ++ optimizeInPlaceLayer Dropout
let unOptPasses = [addConnection]
passes net = do
let x = NN.parse net
let unopt = optimizeWith unOptPasses x
let optimized = optimizeWith optPasses x
(optimized, unopt)
:set -XOverloadedStrings
import Control.Applicative
import Data.Maybe
import Data.GraphViz
import Data.GraphViz.Attributes.Colors.Brewer
import Data.GraphViz.Attributes.Complete
import qualified Data.Text.Lazy as L
import Gen.Caffe.LayerParameter as LP
fmtLabelParameter (_, lp) = [FontName "Source Code Pro", textLabel label, style filled, fillColor color']
where
scheme = BScheme Pastel1 3
color' = BC scheme $ (b2n . fst . inPlace') lp
label = (L.pack . asCaffe . layerTy) lp
b2n True = 1
b2n False = 2
inPlaceParams = nonClusteredParams { globalAttributes = [GraphAttrs [RankDir FromBottom]], fmtNode = fmtLabelParameter }
f = "/tmp/x.graph.png"
showInPlace n = do
(png f . visualizeWith inPlaceParams) n
B.readFile f
inPlace passes = (showInPlace . optimizeWith passes . NN.parse) alexNetSmall
inPlace (addConnection:optimizeInPlaceLayer ReLU)
inPlace [addConnection]
import NN.Backend.Torch as Torch
let net = do
sequential [conv & numOutputC' 5 & kernelSizeC' 3, relu, dropout 0.5, softmax]
return ()
showNet net
(putStr . fromJust . Torch.backend . parse) net
require("nn") local seq0 = nn.Sequential() seq0:add(nn.SpatialConvolutionMM(nil, 5, 3, 3, 1, 1, 0)) seq0:add(nn.Threshold()) seq0:add(nn.Dropout(0.5)) seq0:add(nn.LogSoftMax()) local criterion1 = nn.ClassNLLCriterion() return seq0, criterion1