Ayush - CNN - Deep Learning Cat&Dog
Ayush - CNN - Deep Learning Cat&Dog
import numpy as np
import matplotlib.pyplot as plt
In [3]: original_dataset_dir=r'C:\Users\DELL\Downloads\catsanddogs'
base_dir=r'C:\Users\DELL\Downloads\catsanddogs\cats_and_dogs_small'
os.mkdir(base_dir)
---------------------------------------------------------------------------
FileExistsError Traceback (most recent call last)
Input In [3], in <cell line: 3>()
1 original_dataset_dir=r'C:\Users\DELL\Downloads\catsanddogs'
2 base_dir=r'C:\Users\DELL\Downloads\catsanddogs\cats_and_dogs_small'
----> 3 os.mkdir(base_dir)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\DELL\\Downloads\\catsanddogs\\cats_and_dogs_small'
---------------------------------------------------------------------------
FileExistsError Traceback (most recent call last)
Input In [18], in <cell line: 2>()
1 train_dir = os.path.join(base_dir, 'train')
----> 2 os.mkdir(train_dir)
3 validation_dir = os.path.join(base_dir, 'validation')
4 os.mkdir(validation_dir)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\DELL\\Downloads\\catsanddogs\\cats_and_dogs_small\\train'
---------------------------------------------------------------------------
FileExistsError Traceback (most recent call last)
Input In [11], in <cell line: 2>()
1 train_cats_dir = os.path.join(train_dir, 'cats')
----> 2 os.mkdir(train_cats_dir)
3 train_dogs_dir = os.path.join(train_dir, 'dogs')
4 os.mkdir(train_dogs_dir)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\DELL\\Downloads\\catsanddogs\\cats_and_dogs_small\\train\\cats'
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [16], in <cell line: 1>()
----> 1 validation_cats_dir = os.path.join(validation_dir, 'cats')
2 os.mkdir(validation_cats_dir)
3 validation_dogs_dir = os.path.join(validation_dir, 'dogs')
In [17]: import os
# Define the 'base_dir' or 'validation_dir' where the path will come from
base_dir = r'C:\Users\DELL\Downloads\catsanddogs\cats_and_dogs_small'
validation_dir = os.path.join(base_dir, 'validation')
# Make sure 'base_dir' or 'validation_dir' is defined prior to using it.
# Now you can proceed with the rest of your code using 'validation_dir':
validation_cats_dir = os.path.join(validation_dir, 'cats')
os.mkdir(validation_cats_dir)
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
---------------------------------------------------------------------------
FileExistsError Traceback (most recent call last)
Input In [17], in <cell line: 10>()
6 # Make sure 'base_dir' or 'validation_dir' is defined prior to using it.
7
8 # Now you can proceed with the rest of your code using 'validation_dir':
9 validation_cats_dir = os.path.join(validation_dir, 'cats')
---> 10 os.mkdir(validation_cats_dir)
11 validation_dogs_dir = os.path.join(validation_dir, 'dogs')
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\DELL\\Downloads\\catsanddogs\\cats_and_dogs_small\\validation\\cats'
---------------------------------------------------------------------------
FileExistsError Traceback (most recent call last)
Input In [15], in <cell line: 2>()
1 test_cats_dir = os.path.join(test_dir, 'cats')
----> 2 os.mkdir(test_cats_dir)
3 test_dogs_dir = os.path.join(test_dir, 'dogs')
4 os.mkdir(test_dogs_dir)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\DELL\\Downloads\\catsanddogs\\cats_and_dogs_small\\cats'
In [14]: import os
# Now, use the 'test_dir' variable to create 'cats' and 'dogs' directories within it
test_cats_dir = os.path.join(test_dir, 'cats')
os.mkdir(test_cats_dir)
test_dogs_dir = os.path.join(test_dir, 'dogs')
os.mkdir(test_dogs_dir)
---------------------------------------------------------------------------
FileExistsError Traceback (most recent call last)
Input In [14], in <cell line: 8>()
6 # Now, use the 'test_dir' variable to create 'cats' and 'dogs' directories within it
7 test_cats_dir = os.path.join(test_dir, 'cats')
----> 8 os.mkdir(test_cats_dir)
9 test_dogs_dir = os.path.join(test_dir, 'dogs')
10 os.mkdir(test_dogs_dir)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\DELL\\Downloads\\catsanddogs\\cats_and_dogs_small\\cats'
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [6], in <cell line: 2>()
2 for fname in fnames:
3 src = os.path.join(original_dataset_dir, fname)
----> 4 dst = os.path.join(train_cats_dir, fname)
5 shutil.copyfile(src, dst)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [9], in <cell line: 2>()
2 for fname in fnames:
3 src = os.path.join(original_dataset_dir, fname)
----> 4 dst = os.path.join(validation_cats_dir, fname)
5 shutil.copyfile(src, dst)
In [11]: import os
import shutil
# Iterate through the filenames and copy the files from the source to the destination
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(train_dogs_dir, fname)
shutil.copyfile(src, dst)
In [34]: import os
import shutil
validation_dogs_dir = r'C:\Users\DELL\Downloads\catsanddogs\cats_and_dogs_small\validation_dogs_dir'
# Iterate through the filenames and copy the files from the source to the destination
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(validation_dogs_dir, fname)
shutil.copyfile(src, dst)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [35], in <cell line: 1>()
----> 1 print('total training cat images:', len(os.listdir(train_cats_dir)))
2 print('total training dog images:', len(os.listdir(validation_dogs_dir)))
3 print('total validation cat images:', len(os.listdir(validation_cats_dir)))
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
model = models.Sequential()
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
In [43]: model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 148, 148, 32) 896
=================================================================
Total params: 3453121 (13.17 MB)
Trainable params: 3453121 (13.17 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(150, 150), # Resizes images to 150x150
batch_size=20,
class_mode='binary' # Adjust for the type of problem (e.g., 'categorical' for multiclass)
)
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=20,
class_mode='binary'
)
In [ ]: history = model.fit_generator(
train_generator,
steps_per_epoch=2, # Number of steps per epoch
epochs=5, # Number of epochs
validation_data=validation_generator,
validation_steps=5 # Number of validation steps
)
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Input In [49], in <cell line: 1>()
----> 1 acc = history.history['accuracy']
2 val_acc = history.history['val_accuracy']
4 loss=history.history['loss']
KeyError: 'accuracy'
import cv2
import numpy as np
img = cv2.imread(r'C:\Users\DELL\Downloads\catsanddogs\cat.11.jpg')
img = cv2.resize(img,(150,150))
img = np.reshape(img,[1,150,150,3])
classes = model.predict(img)
print(classes)
#class_names = ['Dog', 'Cat']
#predicted_class_index = np.argmax(classes)
#predicted_class_name = class_names[predicted_class_index]
In [ ]:
In [ ]:
In [ ]:
In [ ]: import cv2
import numpy as np
# Read the JPEG image using OpenCV (replace 'image.jpg' with the path to your image)
img = cv2.imread(r'C:\Users\DELL\Downloads\catsanddogs\cat.11.jpg')
# Interpret the classes predictions as needed based on the model's output format
print(classes)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [21], in <cell line: 1>()
----> 1 img = cv2.imread(r'C:\Users\DELL\Downloads\catsanddogs\cat.11.jpg')
cv2.destroyAllWindows()
In [51]: plotImages(sample_training_images[:5])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [51], in <cell line: 1>()
----> 1 plotImages(sample_training_images[:5])
# OpenCV reads images in BGR format, but Matplotlib expects RGB, so convert it
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
In [ ]:
# OpenCV reads images in BGR format, but Matplotlib expects RGB, so convert it
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# OpenCV reads images in BGR format, but Matplotlib expects RGB, so convert it
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# OpenCV reads images in BGR format, but Matplotlib expects RGB, so convert it
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# OpenCV reads images in BGR format, but Matplotlib expects RGB, so convert it
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)