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.
Some things to check:
- 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.
- 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.
- 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.
- (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.
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.
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.
there aree equal numbers of cats and dogs.
What does the training accuracy look like? Does this go up over time?
can you show me a code to show to accuracy overtime? i
ok somehow i get too much accuracy now on the test set ;))
ok the first problem was augmeantation. Now o get aroun 75% accuracy on the test set. but it get stucked there
ANyone who can help me?
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.
everything works just fine on MNIST
which accuracy do you get with tensorflow?
around 98%
i will quit this task tbh…i is a waste of time with every architecture i cant get why it is not working. i better concentrate on other deep leanring that acually works.
Next time please lead with the working Tensorflow code.
Convolutional Neural Network
Importing the libraries
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
tf.version
Part 1 - Data Preprocessing
Preprocessing the Training set
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = train_datagen.flow_from_directory(‘dataset/training_set’,
target_size = (64, 64),
batch_size = 32,
class_mode = ‘binary’)
Preprocessing the Test set
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory(‘dataset/test_set’,
target_size = (64, 64),
batch_size = 32,
class_mode = ‘binary’)
Part 2 - Building the CNN
Initialising the CNN
cnn = tf.keras.models.Sequential()
Step 1 - Convolution
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation=‘relu’, input_shape=[64, 64, 3]))
Step 2 - Pooling
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
Adding a second convolutional layer
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation=‘relu’))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
Step 3 - Flattening
cnn.add(tf.keras.layers.Flatten())
Step 4 - Full Connection
cnn.add(tf.keras.layers.Dense(units=128, activation=‘relu’))
Step 5 - Output Layer
cnn.add(tf.keras.layers.Dense(units=1, activation=‘sigmoid’))
Part 3 - Training the CNN
Compiling the CNN
cnn.compile(optimizer = ‘adam’, loss = ‘binary_crossentropy’, metrics = [‘accuracy’])
Training the CNN on the Training set and evaluating it on the Test set
cnn.fit(x = training_set, validation_data = test_set, epochs = 25)
Part 4 - Making a single prediction
import numpy as np
from keras.preprocessing import image
test_image = image.load_img(‘dataset/single_prediction/cat_or_dog_1.jpg’, target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = cnn.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = ‘dog’
else:
prediction = ‘cat’
print(prediction)
this is the working tensorflow example if it helps you
Hi, it’s very helpful to use backticks for quoting code (see Please read: make it easier to help you) and so it’s easier to copy paste & have the correct formatting.
# Convolutional Neural Network
# Importing the libraries
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
tf.**version**
# Part 1 - Data Preprocessing
# Preprocessing the Training set
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = train_datagen.flow_from_directory(‘dataset/training_set’,
target_size = (64, 64),
batch_size = 32,
class_mode = ‘binary’)
# Preprocessing the Test set
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory(‘dataset/test_set’,
target_size = (64, 64),
batch_size = 32,
class_mode = ‘binary’)
# Part 2 - Building the CNN
# Initialising the CNN
cnn = tf.keras.models.Sequential()
# Step 1 - Convolution
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation=‘relu’, input_shape=[64, 64, 3]))
# Step 2 - Pooling
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
# Adding a second convolutional layer
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation=‘relu’))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
# Step 3 - Flattening
cnn.add(tf.keras.layers.Flatten())
# Step 4 - Full Connection
cnn.add(tf.keras.layers.Dense(units=128, activation=‘relu’))
# Step 5 - Output Layer
cnn.add(tf.keras.layers.Dense(units=1, activation=‘sigmoid’))
# Part 3 - Training the CNN
# Compiling the CNN
cnn.compile(optimizer = ‘adam’, loss = ‘binary_crossentropy’, metrics = [‘accuracy’])
# Training the CNN on the Training set and evaluating it on the Test set
cnn.fit(x = training_set, validation_data = test_set, epochs = 25)
# Part 4 - Making a single prediction
import numpy as np
from keras.preprocessing import image
test_image = image.load_img(‘dataset/single_prediction/cat_or_dog_1.jpg’, target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = cnn.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = ‘dog’
else:
prediction = ‘cat’
print(prediction)
ok