1
1
"""
2
- convert2image(array) -> Array{Gray}
2
+ convert2image(array; black_digits=false ) -> Array{Gray}
3
3
4
4
Convert the given MNIST horizontal-major tensor (or feature matrix)
5
- to a vertical-major `Colorant` array. The values are also color
5
+ to a vertical-major `Colorant` array. If `black_digits` is `true`, the values are also color
6
6
corrected according to the website's description, which means that
7
7
the digits are black on a white background.
8
8
@@ -16,17 +16,23 @@ julia> MNIST.convert2image(MNIST.traintensor(1)) # first training image
16
16
[...]
17
17
```
18
18
"""
19
- function convert2image (array:: AbstractArray{T} ) where {T<: Number }
19
+ function convert2image (array:: AbstractArray{T} ; black_digits :: Bool = false ) where {T<: Number }
20
20
nlast = size (array)[end ]
21
21
array = reshape (array, 28 , 28 , :)
22
22
array = permutedims (array, (2 , 1 , 3 ))
23
23
if size (array)[end ] == 1 && nlast != 1
24
24
array = dropdims (array, dims= 3 )
25
25
end
26
26
if any (x -> x > 1 , array) # simple check if x in [0,1]
27
- img = _colorview (Gray, array ./ T (255 ))
27
+ array = array ./ T (255 ) # avoid changing the input array
28
+ if black_digits
29
+ array .= one (eltype (array)) .- array
30
+ end
28
31
else
29
- img = _colorview (Gray, array)
32
+ if black_digits
33
+ array = one (eltype (array)) .- array
34
+ end
30
35
end
31
- img
36
+
37
+ return _colorview (Gray, array)
32
38
end
0 commit comments