MethodError: no method matching Float32(::RGB{N0f8})

Hello All,
I am trying to do CNN Classification using Julia Flux but while doing so in one of the codes I am getting above mentioned error.

using Flux
using FileIO
using Plots
using Images
using MLDataUtils: splitobs, shuffleobs
using IterTools

function resize_and_grayify(directory, im_name, width::Int64, height::Int64)
    resized_gray_img = Gray.(load(directory * "/" * im_name)) |> (x -> imresize(x, width, height))
    try
        save("preprocessed_" * directory * "/" * im_name, resized_gray_img)
    catch e
        if isa(e, SystemError)
            mkdir("preprocessed_" * directory)
            save("preprocessed_" * directory * "/" * im_name, resized_gray_img)
        end
    end
end

function process_images(directory, width::Int64, height::Int64)
    files_list = readdir(directory)
    map(x -> resize_and_grayify(directory, x, width, height), files_list)
end

n_resolution = 90

begin
    process_images("path to image", n_resolution, n_resolution)
    process_images("path to image", n_resolution, n_resolution);
end

begin
    optimised_dir = readdir("path to image")
    not_optimised_dir = readdir("path to image");
end

begin
    optimised1 = load.("path to image/" .* optimised_dir)
    not_optimised1 = load.("path to image/" .* not_optimised_dir);
end

optimised = optimised1
not_optimised = not_optimised1


data = vcat(optimised, not_optimised)

begin
    labels = vcat([0 for _ in 1:length(optimised)], [1 for _ in 1:length(not_optimised)])
    (x_train, y_train), (x_test, y_test) = splitobs(shuffleobs((data, labels)), at = 0.7)
end

function make_minibatch(X, Y, idxs)
    X_batch = Array{Float32}(undef, size(X[1])..., 1, length(idxs))
    for i in 1:length(idxs)
        X_batch[:, :, :, i] = Float32.(X[idxs[i]])
    end
    Y_batch = onehotbatch(Y[idxs], 0:1)
    return (X_batch, Y_batch)
end

begin
    batchsize = 3
    mb_idxs = partition(1:length(x_train), batchsize)
    train_set = [make_minibatch(x_train, y_train, i) for i in mb_idxs]
    test_set = make_minibatch(x_test, y_test, 1:length(x_test));
end

Can any one help me to solve the error?

I think you have already been pointed to

but let me point you there again - you are much more likely to receive help here if you provide people with runnable code that reproduces your error. In this example here, your error is produced by a call to make_minibatch(x_test, y_test), but both x_test and y_test are undefined.

Also make sure to use triple backticks ``` to make your code legible.

The error your getting should be reasonably obvious though - you are trying to convert a value of type RGB{N0f8} to Float32, and the error is telling you that no method doing this is defined. I don’t know much about image processing but I’m not sure what you would expect the result of a conversion of an RGB object (which presumably consists of three numbers) to Float32 to be.

Maybe this SO answer is relevant:

Hello,

Yes, I edited the code and provided the detailed code.

I cannot follow your code completely, but your problem is probably here. If X is an RGB color you cannot convert it with Float32(X) or Float32.(X).

If you have a single color value

julia> col = rand(RGB{Float64})
RGB{Float64}(0.7597468600795788,0.6400674481712467,0.07965676928592735)

julia> Float32.((red(col), green(col), blue(col)))
(0.75974685f0, 0.64006746f0, 0.07965677f0)

There may be some easier way that I’m not aware of. At least there is one for arrays of colors:

julia> cols = rand(RGB{Float64}, 2)
2-element Array{RGB{Float64},1} with eltype RGB{Float64}:
 RGB{Float64}(0.31792262743978283,0.04199078446140825,0.19240770194528178)
 RGB{Float64}(0.8813794939125559,0.7562364208708531,0.4894870930072063)

julia> Float32.(channelview(cols))
3Ă—2 Matrix{Float32}:
 0.317923   0.881379
 0.0419908  0.756236
 0.192408   0.489487
1 Like

A separate question is: why do you need to convert the colors into an array of Float32? Can you not work with RGBs the whole way? I don’t understand your use-case, but RGBs support a lot of operations, so perhaps consider staying in that domain entirely?

If you need Float32 colors you can do

julia> RGB{Float32}.(cols)
2-element Array{RGB{Float32},1} with eltype RGB{Float32}:
 RGB{Float32}(0.31792262f0,0.041990783f0,0.1924077f0)
 RGB{Float32}(0.8813795f0,0.75623643f0,0.48948708f0)

or use mapc:

julia> mapc.(Float32, cols)
2-element Array{RGB{Float32},1} with eltype RGB{Float32}:
 RGB{Float32}(0.31792262f0,0.041990783f0,0.1924077f0)
 RGB{Float32}(0.8813795f0,0.75623643f0,0.48948708f0)
1 Like