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

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-float32-rgb-n0f8/92701>\
**Category:** Machine Learning\
**Created:** [January 9, 2023, 8:45am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-float32-rgb-n0f8/92701 "2023-01-09T08:45:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hardik\_Sakpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hardik_sakpal/32/45588_2.png) [@Hardik\_Sakpal](https://discourse.julialang.org/u/Hardik_Sakpal)\
**Post date:** [January 9, 2023, 8:45am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-float32-rgb-n0f8/92701/1 "2023-01-09T08:45:23Z")

</div>

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.

```julia
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?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 9, 2023, 9:13am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-float32-rgb-n0f8/92701/2 "2023-01-09T09:13:18Z")

</div>

I think you have already been pointed to

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

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:

> <https://stackoverflow.com/questions/74274551/how-to-convert-rgbn0f8-type-to-arrayfloat64>

---

<div class="post-metadata">

**Author:** ![Hardik\_Sakpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hardik_sakpal/32/45588_2.png) [@Hardik\_Sakpal](https://discourse.julialang.org/u/Hardik_Sakpal)\
**Post date:** [January 9, 2023, 9:37am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-float32-rgb-n0f8/92701/3 "2023-01-09T09:37:24Z")

</div>

Hello,

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

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 9, 2023, 10:49am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-float32-rgb-n0f8/92701/4 "2023-01-09T10:49:11Z")

</div>

> [@Hardik\_Sakpal](#):
>
> ```julia
> for i in 1:length(idxs)
> X_batch[:, :, :, i] = Float32.(X[idxs[i]])
> end
> 
> ```

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
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
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

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 9, 2023, 10:56am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-float32-rgb-n0f8/92701/5 "2023-01-09T10:56:43Z")

</div>

> [@Hardik\_Sakpal](#):
>
> ```julia
> 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
> 
> ```

A separate question is: why do you need to convert the colors into an array of `Float32`? Can you not work with `RGB`s 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
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
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)

```
