# How to implement custom image dataset for cnn in Julia Flux?

**URL:** <https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505>\
**Category:** Machine Learning\
**Created:** [January 4, 2023, 12:47pm UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505 "2023-01-04T12:47:19Z")\
**Posts on this page:** 7\
**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 4, 2023, 12:47pm UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505/1 "2023-01-04T12:47:19Z")

</div>

Hello,  
I want to train a CNN model using Julia Flux but I am not able to load and preprocess the custom image dataset for training. Can anyone provide me the code snippet for loading the custom image dataset and preprocessing for the dataset.

---

<div class="post-metadata">

**Author:** ![Rasmus\_Hoier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rasmus_hoier/32/24036_2.png) [@Rasmus\_Hoier](https://discourse.julialang.org/u/Rasmus_Hoier)\
**Post date:** [January 5, 2023, 11:48am UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505/2 "2023-01-05T11:48:07Z")

</div>

A bit more information might make it easier for people to help you.

What format is the dataset saved in?  
What preprocessing do you want to do?

You could also share your code here. Maybe someone can spot the error.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 5, 2023, 11:53am UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505/3 "2023-01-05T11:53:55Z")

</div>

Hi and welcome! You might get a better response if you follow

> [@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 …

---

<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, 7:27am UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505/4 "2023-01-09T07:27:51Z")

</div>

Hello,  
The dataset is saved in the form of image.png. and for preprocessing I need to resize the images and save them between 0 to 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, 7:28am UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505/5 "2023-01-09T07:28:47Z")

</div>

Hello,  
Thank you I will follow that.

---

<div class="post-metadata">

**Author:** ![Rasmus\_Hoier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rasmus_hoier/32/24036_2.png) [@Rasmus\_Hoier](https://discourse.julialang.org/u/Rasmus_Hoier)\
**Post date:** [January 9, 2023, 3:33pm UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505/6 "2023-01-09T15:33:42Z")

</div>

Have a look at [Julia Images](https://juliaimages.org/latest/). There is a function `imresize` for resizing images.

You can load a single image and convert it to a Float32 array like this. The elements should already be between 0 and 1.

```julia
using Images
img = load("img.png")
arr = convert(Array{Float32, 3}, channelview(img))

```

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [January 9, 2023, 7:00pm UTC](https://discourse.julialang.org/t/how-to-implement-custom-image-dataset-for-cnn-in-julia-flux/92505/7 "2023-01-09T19:00:00Z")

</div>

You could also find useful the following resources:

> **[GitHub - FluxML/DataAugmentation.jl: Flexible data augmentation library for...](https://github.com/FluxML/DataAugmentation.jl)**
>
> Flexible data augmentation library for machine and deep learning - GitHub - FluxML/DataAugmentation.jl: Flexible data augmentation library for machine and deep learning

[https://juliaml.github.io/MLDatasets.jl/stable/containers/overview/](https://juliaml.github.io/MLDatasets.jl/stable/containers/overview/)

> **[GitHub - jeremiedb/ImageNetTrain.jl: ImageNet trianing with Flux.jl](https://github.com/jeremiedb/ImageNetTrain.jl)**
>
> ImageNet trianing with Flux.jl. Contribute to jeremiedb/ImageNetTrain.jl development by creating an account on GitHub.
