Cnnbyrohanga: # Create Datasets
Cnnbyrohanga: # Create Datasets
Cnnbyrohanga: # Create Datasets
In [6]: import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
In [7]: import os
path= "D:\dogs and cats dataset\dogs and cats dataset\datasets"
classes= os.listdir(path)
classes
['cats', 'dogs']
Out[7]:
pic_index = 210
img = mpimg.imread(img_path)
plt.imshow(img)
plt.show()
# Create datasets
train_datagen = image_dataset_from_directory(base_dir,
image_size=(200,200),
subset='training',
seed= 1,
validation_split=0.1,
batch_size= 32)
test_datagen = image_dataset_from_directory(base_dir,
image_size=(200,200),
subset='validation',
seed= 1,
validation_split=0.1,
batch_size=32)
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from keras import layers
model= tf.keras.models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(200, 200, 3)),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D(2, 2),
layers.Flatten(),
layers.Dense(512, activation='relu'),
layers.BatchNormalization(),
layers.Dense(512, activation='relu'),
layers.Dropout(0.1),
layers.BatchNormalization(),
layers.Dense(512, activation='relu'),
layers.Dropout(0.2),
layers.BatchNormalization(),
layers.Dense(1, activation='sigmoid')
])
model.summary()
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
history= model.fit(train_datagen,
epochs=10,
validation_data=test_datagen)
history_df = pd.DataFrame(history.history)
history_df.loc[:, ['loss', 'val_loss']].plot()
history_df.loc[:, ['accuracy', 'val_accuracy']].plot()
plt.show()
=================================================================
Total params: 3,902,529
Trainable params: 3,899,457
Non-trainable params: 3,072
_________________________________________________________________
Epoch 1/10
57/57 [==============================] - 26s 420ms/step - loss: 0.8835 - accuracy: 0.5409 - val_loss: 1.5360 - val_accuracy: 0.4653
Epoch 2/10
57/57 [==============================] - 29s 503ms/step - loss: 0.7315 - accuracy: 0.5810 - val_loss: 1.3872 - val_accuracy: 0.5693
Epoch 3/10
57/57 [==============================] - 23s 405ms/step - loss: 0.7084 - accuracy: 0.6030 - val_loss: 0.7866 - val_accuracy: 0.6337
Epoch 4/10
57/57 [==============================] - 28s 488ms/step - loss: 0.6659 - accuracy: 0.6326 - val_loss: 1.4096 - val_accuracy: 0.4703
Epoch 5/10
57/57 [==============================] - 25s 432ms/step - loss: 0.6272 - accuracy: 0.6678 - val_loss: 1.3085 - val_accuracy: 0.4604
Epoch 6/10
57/57 [==============================] - 27s 479ms/step - loss: 0.6253 - accuracy: 0.6584 - val_loss: 1.8041 - val_accuracy: 0.4604
Epoch 7/10
57/57 [==============================] - 24s 414ms/step - loss: 0.6094 - accuracy: 0.6908 - val_loss: 1.3604 - val_accuracy: 0.4851
Epoch 8/10
57/57 [==============================] - 24s 415ms/step - loss: 0.6091 - accuracy: 0.6733 - val_loss: 0.7924 - val_accuracy: 0.6089
Epoch 9/10
57/57 [==============================] - 26s 446ms/step - loss: 0.5557 - accuracy: 0.7144 - val_loss: 0.6400 - val_accuracy: 0.6386
Epoch 10/10
57/57 [==============================] - 25s 427ms/step - loss: 0.4878 - accuracy: 0.7699 - val_loss: 0.9569 - val_accuracy: 0.5693