How to convert an array to Matrrix of ColorTypes.Gray


#1
using Flux, Flux.Data.MNIST, Statistics
using Flux: onehotbatch, onecold, crossentropy, throttle
using Base.Iterators: repeated, partition

# load the images
imgs = MNIST.images()
raw_labels=MNIST.labels()
# average the values in the image
imgs0 = imgs[raw_labels .== 0]
avg_imgs0 = reduce(+, float.(imgs0))./length(imgs0)

I see that eltype(imgs[1]) is ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}} and imgs[1] is a 28x28 matrix. But I can’t seem to convert avg_imgs0 which has eltype of Float back to imgs[1]'s eltype

I tried

#import Base.ColorTypes
ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}}.(avg_imgs0)

How do I convert avg_imgs0 back to a matrix containing ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}}?


#2

Please try run your code.

What is raw_labels. What is Base.ColorTypes?


#3

Anyway, you can do Gray.(avg_imgs0).

Capture


#4

I’ve fixed my code, but

Gives error UnderVarError: Gray not defined.

I am using Julia v1.1


#5

This seems to work

]add ColorTypes
using ColorTypes
Gray.(avg_imgs0)