<jupyter_start><jupyter_text>Importing libraries<jupyter_code>import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.layers import Conv2D, MaxPooling2D, InputLayer
from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
from keras import applications
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Initialization<jupyter_code>model_weights = 'bottleneck_fc_model.h5'

## image dimensions
img_h, img_w = 150, 150

## epochs
epochs = 50

## batch Size
batch_size = 32

## total no of train and validation samples
nb_train_samples = 3000
nb_validation_samples = 800

## data set directory
train_data_dir = r'train'
validation_data_dir = r'validation'<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>## vgg16 network
model = applications.vgg16.VGG16(include_top = False, weights = 'imagenet', input_shape=(150,150,3))
print('Model loaded.')

top_model = Sequential()
# top_model.add(Dense(128))
top_model.add(Conv2D(128,(3,3),input_shape = model.output_shape[1:]))
top_model.add(BatchNormalization(axis=3))
top_model.add(Activation('relu'))
top_model.add(MaxPooling2D(pool_size = (2, 2)))
top_model.add(Flatten())
top_model.add(Dense(256, activation = 'relu'))
top_model.add(Dropout(0.6))
top_model.add(Dense(1, activation = 'sigmoid'))

top_model.load_weights(model_weights, by_name = True, skip_mismatch = True)

model = Model(inputs=model.input, outputs=top_model(model.output))

for layer in model.layers[:15]:
    layer.trainable = False
    
model.compile(loss = 'binary_crossentropy', 
             optimizer = tf.keras.optimizers.SGD(learning_rate = 1e-4, momentum = 0.9), 
             metrics = ['accuracy'])<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5
58892288/58889256 [==============================] - 0s 0us/step
58900480/58889256 [==============================] - 0s 0us/step
Model loaded.<jupyter_text>Data Preprocessing<jupyter_code>## data augmentation
train_datagen = ImageDataGenerator(rescale = 1./255, 
                                   shear_range = 0.3, 
                                   zoom_range = 0.2, 
                                   horizontal_flip = True, 
                                   rotation_range=20)

test_datagen = ImageDataGenerator(rescale=1. / 255)

## train data
train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                    target_size=(img_w, img_h),
                                                    batch_size=batch_size,
                                                    class_mode='binary')

## validation data
validation_generator = test_datagen.flow_from_directory(validation_data_dir,
                                                        target_size=(img_w, img_h),
                                                        batch_size=batch_size,
                                                        class_mode='binary')<jupyter_output>Found 3000 images belonging to 2 classes.
Found 800 images belonging to 2 classes.<jupyter_text>Model Train<jupyter_code>## training and testing model with the data set
history = model.fit(train_generator,
                    batch_size = batch_size,
                    epochs = epochs,
                    validation_data = validation_generator)

## plot accuracy graph
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Training and Validation accuracy')
plt.legend(['accuracy','validation accuracy'])
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.show()

## plot loss graph
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Training and Validation loss')
plt.legend(['loss','validation loss'])
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.show()<jupyter_output>Epoch 1/50
71/94 [=====================>........] - ETA: 1:52 - loss: 0.8299 - accuracy: 0.5428
{'fraction_non_alphanumeric': 0.12607944732297063, 'fraction_numerical': 0.03528250678509746, 'mean_word_length': 3.2405857740585775, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '641073', 'license': 'MIT', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1202, 'n_words': 228, 'path': 'models/final.ipynb', 'repo_name': 'Annanya481/Cat-Dog-Classifier', 'size': '4053'}