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