# Accuracy issues on Flux

**URL:** https://discourse.julialang.org/t/accuracy-issues-on-flux/92016
**Category:** Performance
**Tags:** question, flux
**Created:** [December 22, 2022, 6:58pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016 "2022-12-22T18:58:33Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 22, 2022, 6:58pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/1 "2022-12-22T18:58:33Z")

</div>

Hello guys. I have a problem with Flux. I have a very low accuracy (about 50%) on this code and I dont know why. i converted some tf code to Flux bu i dont get the issue. i mention that i am new to Julia and Flux so maybe i am doing something wrong.

I have trianing images with dogs and cats(8000) and test images(2000)

```julia
using Augmentor
using Flux
using FileIO
using Images
using CUDA
using ProgressMeter
using Statistics
train_datagen = ShearX(0.2) * ShearY(0.2) |> Zoom(0.2) |> FlipX()

# load data from dataset folder
base_dir = "dataset/training_set/"
subdirs = readdir(base_dir)

image_label_pairs = []
for subdir in subdirs
    subdir_path = joinpath(base_dir, subdir)
    for file in readdir(subdir_path)
        # Load the image
        img = Images.load(joinpath(subdir_path, file))
        # Create a tuple of the image and the label (subdirectory name)
        push!(image_label_pairs, (img, subdir))
    end
end

images = map((X) -> augment(X[1], train_datagen), image_label_pairs)
labels = map((X) -> X[2] == "cats" ? 1.0 : 0.0, image_label_pairs)

X = [channelview(imresize(x, (64, 64))) for x in images] |> gpu
X = Flux.batch(X) |> gpu
X = permutedims(X, (2, 3, 1, 4)) |> gpu
X = Float32.(X) |> gpu
y = labels |> gpu
train_loader = Flux.Data.DataLoader((X, y'), batchsize=32, shuffle=true)
model = Chain(
    Conv((3, 3), 3 => 32, relu),
    MaxPool((2, 2)),
    Conv((3, 3), 32 => 64, relu),
    MaxPool((2, 2)),
    Flux.flatten,
    Dense(12544, 128, relu),
    Dense(128, 1),
    sigmoid
) |> gpu

opt = ADAM()

loss(x, y) = Flux.binarycrossentropy(model(x), y)
params = Flux.params(model)
epochs = 25

train_loss = []
for epoch in 1:epochs
    println("Epoch = $epoch/$epochs:")
    train_batch_loss = []
    @showprogress for (image, label) in train_loader
        x = gpu(image)
        y = gpu(label)
        Flux.train!(loss, params, [(x, y)], opt)
        push!(train_batch_loss, loss(x, y))
    end
    push!(train_loss, mean(train_batch_loss))
    println("Loss: $(train_loss[end])")
end
# calculate accuracy
base_dir = "dataset/test_set/"
subdirs = readdir(base_dir)

image_label_pairs = []
for subdir in subdirs
    subdir_path = joinpath(base_dir, subdir)
    for file in readdir(subdir_path)
        # Load the image
        img = Images.load(joinpath(subdir_path, file))
        # Create a tuple of the image and the label (subdirectory name)
        push!(image_label_pairs, (img, subdir))
    end
end

images = map((X) -> X[1], image_label_pairs)
labels = map((X) -> X[2] == "cats" ? 1.0 : 0.0, image_label_pairs)

X_test = [channelview(imresize(x, (64, 64))) for x in images] |> gpu
X_test = Flux.batch(X_test) |> gpu
X_test = permutedims(X_test, (2, 3, 1, 4)) |> gpu
X_test = Float32.(X_test) |> gpu
y_test = labels |> gpu
test_loader = Flux.Data.DataLoader((X_test, y_test'), batchsize=20, shuffle=false)

correct = 0
total = 0
for (image, label) in [(X_test, y_test')]
    ŷ = model(image)
    ŷ = ŷ .> 0.5
    total += length(label)
    correct += sum(ŷ .== label)
end
println(total)
println(correct)
println("Accuracy: $(correct / total)")

```

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [December 22, 2022, 7:01pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/2 "2022-12-22T19:01:03Z")

</div>

It may be helpful to put Flux in the title and perhaps tag it as Flux as well.

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 22, 2022, 7:02pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/3 "2022-12-22T19:02:27Z")

</div>

Thanks. I modified

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [December 22, 2022, 7:37pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/4 "2022-12-22T19:37:31Z")

</div>

It’s hard to read your code like that. Would be easier if you used three backticks before your code and after. 🙏🏻

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 22, 2022, 7:41pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/5 "2022-12-22T19:41:54Z")

</div>

ok. i really wanted to know how to do that here 🙂

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 22, 2022, 8:10pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/6 "2022-12-22T20:10:54Z")

</div>

can you help me now ? 🙂

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [December 22, 2022, 8:49pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/7 "2022-12-22T20:49:51Z")

</div>

Yes i can read your code fine now but I’m AFK right now so cannot test it. Will report back when I’ve had a chance to run it. 😌

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 23, 2022, 2:02pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/8 "2022-12-23T14:02:29Z")

</div>

you had time to try the code? 🙂 @DoktorMike

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [December 24, 2022, 10:13am UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/9 "2022-12-24T10:13:19Z")

</div>

Some things to check:

1. What are the distribution of labels in your training set? Is the split of classes balanced? If this is heavily weighted towards one class, but the test set isn’t, this could cause poor performance.
2. What are the training loss curves like? Does the training loss significantly go down vs epochs? If it does not, this could mean that the learning rate is too small or more epochs are needed.
3. Looking at confusion matrices can be useful in seeing why your classifier is performing poorly, this could tell you about issues with your test/train split or that you need to change the weights of each class.
4. (EDIT: This is for multiclass, binary expects “true” or “false”) Are the labels starting at zero? I think Flux expects 1 based labels, which could be worth checking. Sometimes you need to just increment each label by 1 to fix this. Could you change the labels to explicitly be booleans?

Sorry that I can’t be more specific with the guidance. These sorts of errors are very difficult to debug, without having access to the data at hand.

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 24, 2022, 11:13am UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/11 "2022-12-24T11:13:08Z")

</div>

i tried with 40 epochs. i still have 54% accuracy. After 40 the loss curve is getting flat.  
I tried to covert labels to bolean. same result. my slit is 8000 traing set and 2000 test set.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [December 24, 2022, 1:02pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/12 "2022-12-24T13:02:09Z")

</div>

What about the number of images of cats, and the number of images of dogs? That’s the split that matters more in this case.

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 24, 2022, 1:19pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/13 "2022-12-24T13:19:11Z")

</div>

there aree equal numbers of cats and dogs.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [December 24, 2022, 2:26pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/14 "2022-12-24T14:26:27Z")

</div>

What does the training accuracy look like? Does this go up over time?

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 24, 2022, 4:06pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/15 "2022-12-24T16:06:01Z")

</div>

can you show me a code to show to accuracy overtime? i

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 24, 2022, 4:42pm UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/16 "2022-12-24T16:42:07Z")

</div>

ok somehow i get too much accuracy now on the test set ;))

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 26, 2022, 11:16am UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/17 "2022-12-26T11:16:53Z")

</div>

ok the first problem was augmeantation. Now o get aroun 75% accuracy on the test set. but it get stucked there

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 28, 2022, 8:29am UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/18 "2022-12-28T08:29:02Z")

</div>

ANyone who can help me?

---

<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: [December 28, 2022, 8:36am UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/19 "2022-12-28T08:36:17Z")

</div>

try the same code on MNIST or Cifar10 from MLDatasets.jl and see if everything works as expected. If it doesn’t, post a fully reproducible script.

---

<div class="post-metadata">

### Author: ![Radu\_Mihai\_Diaconu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/radu_mihai_diaconu/32/45306_2.png) [@Radu\_Mihai\_Diaconu](https://discourse.julialang.org/u/Radu_Mihai_Diaconu)
#### Post date: [December 28, 2022, 10:36am UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/20 "2022-12-28T10:36:13Z")

</div>

everything works just fine on MNIST

---

<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: [December 28, 2022, 10:54am UTC](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016/21 "2022-12-28T10:54:44Z")

</div>

which accuracy do you get with tensorflow?

[Next page](https://discourse.julialang.org/t/accuracy-issues-on-flux/92016.md?page=2)
