Tanmay Bakshi tried to teach me ML, but I failed and I don't know why

I tried this code snippet from the book (…springboard to machine learning for all ages):

using Metalhead: VGG19, preprocess, load, labels
using Flux: onecold

model = VGG19()
class_labels = labels(model)

print("Enter the name of your file: ")
user_image = preprocess(load(readline()))
model_prediction = model(user_image)
top_class = onecold(model_prediction)[1]
class_name = labels[top_class]

println("I think this is $(class_name)")

But then I got this error message:

julia> include("test09.jl")
Enter the name of your file: mouse.jpg
ERROR: LoadError: MethodError: no method matching getindex(::typeof(labels), ::Int64)
Stacktrace:
 [1] top-level scope at C:\julia\julia-1.3.1\examples\test09.jl:11
 [2] include at .\boot.jl:328 [inlined]
 [3] include_relative(::Module, ::String) at .\loading.jl:1105
 [4] include(::Module, ::String) at .\Base.jl:31
 [5] include(::String) at .\client.jl:424
 [6] top-level scope at none:0
in expression starting at C:\julia\julia-1.3.1\examples\test09.jl:11

Unfortunately I could not resolve this issue.

It would be great, if someone could give me some help on this problem. I really got stuck.

Thank you!
Klaus

That error message is hard to interpret for someone new to the language. What it means is that you tried to index a function like an array. The error occurs here:

class_name = labels[top_class]

Note that labels is actually a function, not an array. That line is probably supposed to be this:

class_name = class_labels[top_class]
1 Like

I opened up an issue on github to see if it’s possible to print a more readable error message for this kind of situation.

1 Like

Just guessing, what if you tried labels(top_class). I can’t runt he code cos I don’t have the mouse jpeg.

WONDERFUL - you are a genius! You are a great reviewer of Julia-ML-books! Thanks a lot - with your help I can move forwards!

THANK YOU so much!

Dr. Nikolaus Albrecht

1 Like