juliaflux.jl

How to load an image for inference in Flux.jl?


I have a model which I trained using a specific dataset. I did not originally break the set up into a train and test set (which I should have). With that said, I want to do some adhoc testing to see how the model performs when I give it specific images. I tried doing something like Images.load("/Users/logankilpatrick/Desktop/train/dog.10697.jpg") to load the image and then pass it directly to the model but I get inout size mismatch errors. What is the correct way to load the image?


Solution

  • To use an image for inference, you need to do a few steps as shown below:

    x = Images.load("/Users/logankilpatrick/Desktop/train/dog.10697.jpg")
    x = Images.imresize(x, (224,224)...) # 224x224 depends on the model size
    x = permutedims(channelview(x), (3,2,1))
    # Channelview returns a view of A, splitting out (if necessary) the color channels of A into a new first dimension.
    
    x = reshape(x, size(x)..., 1) # Add an extra dim to show we only have 1 image
    float32.(x) # Convert to float32 instead of float64
    
    model(x)
    

    Note that a few of these may change depending on the model you are using and other factors but this is the general idea of what you need to do. It is likely work it to write up a simple function that does this for you.