<jupyter_start><jupyter_text>My goal for task 1 is to take the cats and dogs database and use a CNN to classify the images as cat or dog.<jupyter_code>from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import tensorflow as tf
import keras 
import pathlib
from sklearn.metrics import classification_report
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.applications.vgg16 import VGG16
cats_training_path = pathlib.Path("dogs_cats/dataset/training_set/cats")
dogs_training_path = pathlib.Path("dogs_cats/dataset/training_set/dogs")
cats_test_path = pathlib.Path("dogs_cats/dataset/test_set/cats")
dogs_test_path = pathlib.Path("dogs_cats/dataset/test_set/dogs")
training_path = pathlib.Path("dogs_cats/dataset/training_set")
test_path = pathlib.Path("dogs_cats/dataset/test_set")
datagen = ImageDataGenerator(rescale=1./255)
data = datagen.flow_from_directory(    
    training_path,
    target_size = (200,200),
    class_mode = 'binary',
     batch_size=64,
)
print(data.class_indices)
#plot top make sure images render correctly
x, y= data.next()
plt.figure(figsize=(10, 10))
for i in range(9):
    image = x[i]
    ax = plt.subplot(3, 3, i+1)
    plt.imshow(image)
    plt.axis("off")
model = keras.models.Sequential()


model.add(keras.layers.Conv2D(32, 3, padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.MaxPooling2D())

model.add(keras.layers.Conv2D(32, 3, padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.MaxPooling2D())

model.add(keras.layers.Conv2D(64, 3, padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.MaxPooling2D())

model.add(keras.layers.Conv2D(128, 3, padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.MaxPooling2D())

model.add(keras.layers.Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(keras.layers.Dense(units = 128, activation = 'relu'))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units = 1, activation = 'sigmoid'))

model.compile(optimizer='adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
test_data = ImageDataGenerator(rescale=1./255)
validation_generator = test_data.flow_from_directory(
        test_path, 
        target_size=(200, 200),  
        batch_size=64,
        class_mode='binary')
history = model.fit(
    data,
    steps_per_epoch=25,
    epochs = 25,
    validation_data=validation_generator,
    validation_steps=25
)
_, acc = model.evaluate(validation_generator, steps=len(validation_generator), verbose=1)
print('Final Testing Accuracy With The Test Data > %.3f' % (acc * 100.0))
Y_pred = model.predict(validation_generator, 2000 //65)
y_pred=np.where(Y_pred>0.5, 1, 0)

img_rows, img_cols, img_channel = 200, 200, 3
vgg = VGG16(include_top=False, weights='imagenet', 
                                     input_shape=(img_rows, img_cols, img_channel))
image_size = 224
input_shape = (image_size, image_size, 3)

epochs = 5
batch_size = 16

pre_trained_model = VGG16(input_shape=input_shape, include_top=False, weights="imagenet")
    
for layer in pre_trained_model.layers[:15]:
    layer.trainable = False

for layer in pre_trained_model.layers[15:]:
    layer.trainable = True
    
last_layer = pre_trained_model.get_layer('block5_pool')
last_output = last_layer.output
    
# Flatten the output layer to 1 dimension
x = keras.layers.GlobalMaxPooling2D()(last_output)
# Add a fully connected layer with 512 hidden units and ReLU activation
x = keras.layers.Dense(512, activation='relu')(x)
# Add a dropout rate of 0.5
x = keras.layers.Dropout(0.5)(x)
# Add a final sigmoid layer for classification
x = keras.layers.Dense(1, activation='sigmoid')(x)


model_vgg.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.summary()
history = model_vgg.fit_generator(
    data,
    epochs=4,
    validation_data=validation_generator,
    validation_steps=25,
    steps_per_epoch=25)
plt.plot(history.history['accuracy'])

plt.plot(history.history['loss'])
train_datagen = ImageDataGenerator(
    rotation_range=15,
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest',
    width_shift_range=0.1,
    height_shift_range=0.1
)
train_generator = train_datagen.flow_from_directory(
    training_path,  
    class_mode='binary',
    target_size=(200,200),
    batch_size=64
)
history_task_3 = model.fit(
    data,
    epochs=25,
    validation_data=validation_generator,
    validation_steps=25,
    steps_per_epoch=25)
plt.plot(history_task_3.history['accuracy'])
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.plot(history_task_3.history['loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
Y_pred_3 = model.predict(validation_generator, 2000 //65)
y_pred_3=np.where(Y_pred>0.5, 1, 0)<jupyter_output>[[0]
 [0]
 [0]
 ...
 [0]
 [0]
 [0]]
{'fraction_non_alphanumeric': 0.12828103414249062, 'fraction_numerical': 0.035129267811328205, 'mean_word_length': 5.054958183990442, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '527927', 'license': 'MIT', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1783, 'n_words': 335, 'path': 'HW5/HW5.ipynb', 'repo_name': 'enisaras/Projects-in-Machine-Learning-and-AI-HWs', 'size': '5067'}