# Conv\_mnist model-zoo example,

**URL:** <https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592>\
**Category:** Machine Learning\
**Created:** [August 18, 2021, 10:40am UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592 "2021-08-18T10:40:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 18, 2021, 10:40am UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/1 "2021-08-18T10:40:06Z")

</div>

I loaded mnist test data from Kaggle, i wanted to test my pretrained model from conv\_mnist, how to use pretrained model to predict image ?  
It says:

# LeNet5 “constructor”.

# The model can be adapted to any image size

# and any number of output classes.

but

img = test[1011,:] |\> Array  
img = vec(img)  
img = img/255  
img = reshape(img,28,28)  
img = transpose(img)  
Gray.(img)// image of 2

[@load](https://github.com/load) “runs/mnist.bson” model  
y\_pred = model(img)  
r = findmax(y\_pred) .- (0,1)  
r[2]  
error : DimensionMismatch(“Rank of x and w must match! (2 vs. 4)”)

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [August 18, 2021, 3:33pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/2 "2021-08-18T15:33:09Z")

</div>

I think the model expects batches of single channel images, try changing your reshape from `reshape(img, 28, 28)` to `reshape(img, 28, 28, 1, 1)`

---

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 18, 2021, 4:26pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/3 "2021-08-18T16:26:39Z")

</div>

now i got this error:

MethodError: no method matching -(::CartesianIndex{2}, ::Int64)  
Closest candidates are:  
-(::T, ::T) where T\<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:86  
-(::ForwardDiff.Dual{Tx, V, N} where {V, N}, ::Integer) where Tx at C:\Users\andre.julia\packages\ForwardDiff\5gUap\src\dual.jl:139  
-(::ForwardDiff.Dual{Tx, V, N} where {V, N}, ::Real) where Tx at C:\Users\andre.julia\packages\ForwardDiff\5gUap\src\dual.jl:139  
…

Stacktrace:  
[1] \_broadcast\_getindex\_evalf  
@ .\broadcast.jl:648 [inlined]  
[2] \_broadcast\_getindex  
@ .\broadcast.jl:621 [inlined]  
[3] (::Base.Broadcast.var"#19#20"{Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(-), Tuple{Tuple{Float32, CartesianIndex{2}}, Tuple{Int64, Int64}}}})(k::Int64)  
@ Base.Broadcast .\broadcast.jl:1098  
[4] ntuple  
@ .\ntuple.jl:49 [inlined]  
[5] copy  
@ .\broadcast.jl:1098 [inlined]  
[6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(-), Tuple{Tuple{Float32, CartesianIndex{2}}, Tuple{Int64, Int64}}})  
@ Base.Broadcast .\broadcast.jl:883  
[7] top-level scope  
@ In[17]:11  
[8] eval  
@ .\boot.jl:360 [inlined]  
[9] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base .\loading.jl:1116

---

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 18, 2021, 4:32pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/4 "2021-08-18T16:32:22Z")

</div>

this works:

# input

img = test[5011,:] |\> Array  
img = vec(img)  
img = img/255

# img = reshape(img, 28, 28, 1, 🙂

img = transpose(img)  
img = reshape(img,28,28,1,🙂  
y\_pred = model(img)  
print(findmax(y\_pred))

---

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 18, 2021, 4:43pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/5 "2021-08-18T16:43:12Z")

</div>

i thought kaggle mnist images were single channel

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [August 18, 2021, 4:50pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/6 "2021-08-18T16:50:56Z")

</div>

That’s true, but the model still expects a channel dimension and a batch dimension so you need to add those length-one axes.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 18, 2021, 4:52pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/7 "2021-08-18T16:52:20Z")

</div>

Please see [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/). In particular, we would need a complete MWE with proper code block formatting on Discourse (you can edit your posts, no need to create new ones).

More specifically, is there a reason you’re trying to use the version of MNIST hosted on Kaggle? MLDatasets.jl (which Flux uses for all of its demos) already has MNIST, and getting the data as an array is as simple as calling [https://juliaml.github.io/MLDatasets.jl/stable/datasets/MNIST/#MLDatasets.MNIST.traintensor](https://juliaml.github.io/MLDatasets.jl/stable/datasets/MNIST/#MLDatasets.MNIST.traintensor).

---

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 18, 2021, 5:02pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/8 "2021-08-18T17:02:41Z")

</div>

aa okey, i want use Fluxml for kaggle competition. for training im using model from MLDataset,  
just for testing purpose i wanted to evaluate dataset from Kaggle

```nohighlight
img = test[1, :] |> Array
img = img/255 # 0 to 1
img = reshape(img,28,28)
img = transpose(img)
Gray.(img)// shows image of 2
@load "runs/mnist.bson" model
img = test[1,:] |> Array
# img = vec(img)
img = img/255

# img = reshape(img, 28, 28)
img = transpose(img)
Gray.(img)

img = reshape(img,28,28,1,:)
y_pred = model(img)
print(y_pred)
# r = findmax(y_pred) .- (0,1)
# r[2]

```

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 18, 2021, 5:06pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/9 "2021-08-18T17:06:07Z")

</div>

Seems like you already figured out how to do that?

---

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 18, 2021, 5:09pm UTC](https://discourse.julialang.org/t/conv-mnist-model-zoo-example/66592/10 "2021-08-18T17:09:17Z")

</div>

yes, thanks for help
