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)
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')]
ŷ = model(image)
ŷ = ŷ .> 0.5
total += length(label)
correct += sum(ŷ .== label)
end
println(total)
println(correct)
println("Accuracy: $(correct / total)")