Loading
Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more

Torch Tutorial for PlantVillage Challenge

Torch Tutorial for PlantVillage Challenge.

Our model will take input a tensor of size 3x224x224. It will output a 38 size tensor holding log probability for each of the 38 classes. We add SpatialBatchNormalization to the standard AlexNet model to speed up the training.

models/alexnet.lua

    require 'nn'

    local SpatialConvolution = nn.SpatialConvolution
    local SpatialMaxPooling = nn.SpatialMaxPooling

    function createModel()
        local nbClasses = 38
        local nbChannels = 3

        local features = nn.Sequential()

        features:add(SpatialConvolution(nbChannels,64,11,11,4,4,2,2)) -- 224 -> 55
        features:add(SpatialMaxPooling(3,3,2,2))             -- 55 ->  27
        features:add(nn.ReLU(true))
        features:add(nn.SpatialBatchNormalization(64))

        features:add(SpatialConvolution(64,192,5,5,1,1,2,2)) --  27 -> 27
        features:add(SpatialMaxPooling(3,3,2,2))             --  27 ->  13
        features:add(nn.ReLU(true))
        features:add(nn.SpatialBatchNormalization(192))

        features:add(SpatialConvolution(192,384,3,3,1,1,1,1)) --  13 ->  13
        features:add(nn.ReLU(true))
        features:add(nn.SpatialBatchNormalization(384))

        features:add(SpatialConvolution(384,256,3,3,1,1,1,1)) --  13 ->  13
        features:add(nn.ReLU(true))
        features:add(nn.SpatialBatchNormalization(256))

        features:add(SpatialConvolution(256,256,3,3,1,1,1,1)) --  13 ->  13
        features:add(SpatialMaxPooling(3,3,2,2))              -- 13 -> 6
        features:add(nn.ReLU(true))
        features:add(nn.SpatialBatchNormalization(256))

        local classifier = nn.Sequential()
        classifier:add(nn.View(256*6*6))

        classifier:add(nn.Dropout(0.5))
        classifier:add(nn.Linear(256*6*6, 4096))
        classifier:add(nn.ReLU(true))

        classifier:add(nn.Dropout(0.5))
        classifier:add(nn.Linear(4096, 4096))
        classifier:add(nn.ReLU(true))

        classifier:add(nn.Linear(4096, nbClasses))
        classifier:add(nn.LogSoftMax())

        local model = nn.Sequential()

        model:add(features):add(classifier)

        return model
    end

Since our last layer is LogSoftMax, we will use ClassNLLCriterion as our criterion.