<jupyter_start><jupyter_text>Building the Model - VGG16 From Scratch<jupyter_code>model = Sequential()
model.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding="same", activation="relu"))

model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"))

model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))


model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))

model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))

model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))


model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))

model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))

model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))

model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))


model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))

model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))

model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))

model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))


model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))

model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))

model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))

model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))<jupyter_output><empty_output><jupyter_text>Here I have started with initialising the model by specifying that the model is a sequential model. After initialising the model I add→ 2 x convolution layer of 64 channel of 3x3 kernal and same padding→ 1 x maxpool layer of 2x2 pool size and stride 2x2→ 2 x convolution layer of 128 channel of 3x3 kernal and same padding→ 1 x maxpool layer of 2x2 pool size and stride 2x2→ 3 x convolution layer of 256 channel of 3x3 kernal and same padding→ 1 x maxpool layer of 2x2 pool size and stride 2x2→ 3 x convolution layer of 512 channel of 3x3 kernal and same padding→ 1 x maxpool layer of 2x2 pool size and stride 2x2→ 3 x convolution layer of 512 channel of 3x3 kernal and same padding→ 1 x maxpool layer of 2x2 pool size and stride 2x2I also add relu(Rectified Linear Unit) activation to each layers so that all the negative values are not passed to the next layer.<jupyter_code>model.add(Flatten())

model.add(Dense(units=4096,activation="relu"))

model.add(Dense(units=4096,activation="relu"))

model.add(Dense(units=2, activation="softmax"))<jupyter_output><empty_output><jupyter_text>After creating all the convolution I pass the data to the dense layer so for that I flatten the vector which comes out of the convolutions and add→ 1 x Dense layer of 4096 units→ 1 x Dense layer of 4096 units→ 1 x Dense Softmax layer of 2 units<jupyter_code>from keras.optimizers import Adam

opt = Adam(lr=0.001)

model.compile(optimizer=opt, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])

model.summary()
from keras.callbacks import ModelCheckpoint, EarlyStopping

checkpoint = ModelCheckpoint("/content/drive/My Drive/classification_Dataset/vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)

early = EarlyStopping(monitor='val_acc', min_delta=0, patience=20, verbose=1, mode='auto')

hist = model.fit_generator(steps_per_epoch=200,generator=traindata, validation_data= testdata, validation_steps=80,epochs=100,callbacks=[checkpoint,early])
import matplotlib.pyplot as plt
plt.plot(hist.history["acc"])
plt.plot(hist.history['val_acc'])
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title("model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy","Validation Accuracy","loss","Validation Loss"])
plt.show()
from keras.preprocessing import image
import matplotlib.pyplot as plt
img = image.load_img("/content/drive/My Drive/classification_Dataset/cat_VS_dogs/test1/12500.jpg",target_size=(224,224))
img = np.asarray(img)
plt.imshow(img)
img = np.expand_dims(img, axis=0)
from keras.models import load_model
saved_model = load_model("/content/drive/My Drive/classification_Dataset/vgg16_1.h5")
output = saved_model.predict(img)
if output[0][0] > output[0][1]:
    print("cat")
else:
    print('dog')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14259722538094155, 'fraction_numerical': 0.05185353650216056, 'mean_word_length': 8.086776859504132, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '490706', 'license': 'MIT', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1504, 'n_words': 359, 'path': 'Classification_Cat_VS_Dogs_Custom_Modelling.ipynb', 'repo_name': 'R-aryan/Image_Classification_VGG16', 'size': '4397'}