Knet prediction with CNN

I’m new to neural nets with Julia and I’m guessing what I’ll ask is a very trivial question but I never struggled this much when I was getting started with the TensorFlow and I’m resisting really hard to not go back since I’m struggling really hard with the documentation and examples of Knet for neural networks.

I have the following example of a network that I can train, however I have issue with running predictions:

dtrn = minibatch(trainX, trainY, 200; xsize = (size(trainX,1),1,1,:))
dtst = minibatch(testX, testY, 200; xsize = (size(testX,1),1,1,:))

struct Conv; w; b; f; end
(c::Conv)(x) = c.f.(pool(conv4(c.w, x) .+ c.b))
Conv(w1,w2,cx,cy,f=relu) = Conv(param(w1,w2,cx,cy), param0(1,1,cy,1), f);

struct Dense; w; b; f; end
(d::Dense)(x) = d.f.(d.w * mat(x) .+ d.b)
Dense(i::Int,o::Int,f=relu) = Dense(param(o,i), param0(o), f);

struct Chain; layers; Chain(args…)=new(args); end
(c::Chain)(x) = (for l in c.layers; x = l(x); end; x)
(c::Chain)(x,y) = nll(c(x),y)

LeNet = Chain(Conv(5,1,1,10), Conv(5,1,10,20), Conv(5,1,20,50), Dense(3250,500), Dense(500,unqLabelsNum,identity))

progress!(adam(LeNet, ncycle(dtrn,10)))
accuracy(LeNet, dtst)

I get the accuracy score, however when I run

predict(LeNet, testX)

I get:
ERROR: MethodError: no method matching predict(::Chain, ::Array{Float32, 4})

or when I run:

predict(LeNet, dtst.x)

ERROR: MethodError: no method matching predict(::Chain, ::Matrix{Float32})

or even when I run


I get

MethodError: no method matching NNlib.DenseConvDims(::Matrix{Float32}, ::KnetArray{Float32, 4}; stride=(), padding=(), dilation=(), flipkernel=false)
Closest candidates are:
  NNlib.DenseConvDims(::AbstractArray, ::AbstractArray; kwargs...)

Could someone tell me how I can run a very simple prediction on a model that is already trained exactly as it is demonstrated in the Knet documentation?

Thanks in advance!

Hi, the error message indicates that this is a type issue: i.e. the first argument to the DenseConvDims operation is a Matrix (a cpu array), the second is a KnetArray (a gpu array). The data and model parameters should both be on the gpu or cpu. Please take a look at the examples in the README or tutorial folder in GitHub - denizyuret/Knet.jl: Koç University deep learning framework.

1 Like