# Installing Theano
# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
# Installing Tensorflow
# Install Tensorflow from the website: https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html
# Installing Keras
# pip install --upgrade keras
# Part 1 - Building the CNN
# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('/content/drive/MyDrive/1_training_set',
target_size = (64, 64),
batch_size = 20,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/1_test_set',
target_size = (64, 64),
batch_size = 4,
class_mode = 'binary')
classifier.fit(training_set,
steps_per_epoch = 20,
epochs = 25,
validation_data = test_set,
validation_steps = 10)
import numpy as np
from tensorflow.keras.preprocessing import image
from PIL import Image
# Load the trained model
# Assuming you have already trained and saved the model as 'classifier'
model = classifier
# Load the image you want to classify
image_path = '/content/drive/MyDrive/1_predict/dog2.jpg' # Replace with the actual path of your image
img = image.load_img(image_path, target_size=(64, 64))
new_face = Image.open(image_path)
display(new_face)
# Preprocess the image
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0 # Rescale to match the normalization done during training
# Make predictions on the image
predictions = model.predict(img_array)
#predicted_class = np.argmax(predictions)
print("Predictions:", predictions)
predicted_class = np.round(predictions)
# Print the predicted class label
print("Predicted class label:", predicted_class)
if(predicted_class==0):
print('cat')
else: print('dog')
O/P:
Found 416 images belonging to 2 classes.
Found 42 images belonging to 2 classes.
/usr/local/lib/python3.11/dist-packages/keras/src/layers/convolutional/base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
/usr/local/lib/python3.11/dist-packages/keras/src/trainers/data_adapters/py_dataset_adapter.py:121: UserWarning: Your `PyDataset` class should call `super().__init__(**kwargs)` in its constructor. `**kwargs` can include `workers`, `use_multiprocessing`, `max_queue_size`. Do not pass these arguments to `fit()`, as they will be ignored.
self._warn_if_super_not_called()
Epoch 1/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 5s 144ms/step - accuracy: 0.5143 - loss: 0.6963 - val_accuracy: 0.4500 - val_loss: 0.6924
Epoch 2/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.6500 - loss: 0.6898 - val_accuracy: 0.5000 - val_loss: 0.6930
Epoch 3/25
/usr/local/lib/python3.11/dist-packages/keras/src/trainers/epoch_iterator.py:107: UserWarning: Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches. You may need to use the `.repeat()` function when building your dataset.
self._interrupted_warning()
20/20 ━━━━━━━━━━━━━━━━━━━━ 5s 190ms/step - accuracy: 0.5505 - loss: 0.6911 - val_accuracy: 0.5750 - val_loss: 0.6922
Epoch 4/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.5000 - loss: 0.6957 - val_accuracy: 0.5250 - val_loss: 0.6937
Epoch 5/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 2s 103ms/step - accuracy: 0.4921 - loss: 0.6922 - val_accuracy: 0.5000 - val_loss: 0.6919
Epoch 6/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 14ms/step - accuracy: 0.7000 - loss: 0.6866 - val_accuracy: 0.5000 - val_loss: 0.6912
Epoch 7/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 3s 128ms/step - accuracy: 0.5221 - loss: 0.6913 - val_accuracy: 0.5250 - val_loss: 0.6902
Epoch 8/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 16ms/step - accuracy: 0.7000 - loss: 0.6884 - val_accuracy: 0.5000 - val_loss: 0.6923
Epoch 9/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 4s 109ms/step - accuracy: 0.4940 - loss: 0.6942 - val_accuracy: 0.4750 - val_loss: 0.6906
Epoch 10/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 13ms/step - accuracy: 0.5000 - loss: 0.6893 - val_accuracy: 0.5500 - val_loss: 0.6902
Epoch 11/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 3s 141ms/step - accuracy: 0.5963 - loss: 0.6897 - val_accuracy: 0.5750 - val_loss: 0.6906
Epoch 12/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 12ms/step - accuracy: 0.7500 - loss: 0.6851 - val_accuracy: 0.4500 - val_loss: 0.6897
Epoch 13/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 3s 126ms/step - accuracy: 0.5078 - loss: 0.6903 - val_accuracy: 0.5000 - val_loss: 0.6885
Epoch 14/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 10ms/step - accuracy: 0.4500 - loss: 0.6892 - val_accuracy: 0.4750 - val_loss: 0.6914
Epoch 15/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 5s 136ms/step - accuracy: 0.5723 - loss: 0.6833 - val_accuracy: 0.5250 - val_loss: 0.6849
Epoch 16/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 16ms/step - accuracy: 0.4500 - loss: 0.6908 - val_accuracy: 0.5000 - val_loss: 0.6848
Epoch 17/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 5s 153ms/step - accuracy: 0.5518 - loss: 0.6864 - val_accuracy: 0.5500 - val_loss: 0.6847
Epoch 18/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 16ms/step - accuracy: 0.5500 - loss: 0.6924 - val_accuracy: 0.5000 - val_loss: 0.6836
Epoch 19/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 3s 126ms/step - accuracy: 0.5644 - loss: 0.6847 - val_accuracy: 0.5250 - val_loss: 0.6788
Epoch 20/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.4000 - loss: 0.6961 - val_accuracy: 0.5250 - val_loss: 0.6839
Epoch 21/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 2s 112ms/step - accuracy: 0.5238 - loss: 0.6879 - val_accuracy: 0.5750 - val_loss: 0.6849
Epoch 22/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 13ms/step - accuracy: 0.6000 - loss: 0.6776 - val_accuracy: 0.5750 - val_loss: 0.6805
Epoch 23/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 3s 129ms/step - accuracy: 0.6066 - loss: 0.6787 - val_accuracy: 0.6000 - val_loss: 0.6877
Epoch 24/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.4500 - loss: 0.6907 - val_accuracy: 0.6250 - val_loss: 0.6805
Epoch 25/25
20/20 ━━━━━━━━━━━━━━━━━━━━ 5s 134ms/step - accuracy: 0.6097 - loss: 0.6700 - val_accuracy: 0.5500 - val_loss: 0.6841
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 204ms/step
Predictions: [[0.631441]]
Predicted class label: [[1.]]
dog
Comments
Post a Comment