<jupyter_start><jupyter_text>Mounting<jupyter_code>from google.colab import drive
drive.mount('/content/drive',force_remount=True)<jupyter_output>Mounted at /content/drive<jupyter_text>Extraction<jupyter_code>FLDR='drive/MyDrive/dogs_vs_cat_mod/'
import os
files=os.listdir(FLDR)
files
train_data=files[1]
train_data
import zipfile
archive=zipfile.ZipFile(FLDR+train_data)
archive.extractall(FLDR+'extracted')
files=os.listdir(FLDR)
files
test_data=files[2]
test_data
archive=zipfile.ZipFile(FLDR+test_data)
archive.extractall(FLDR+'extracted_2')
files=os.listdir(FLDR)
files
test_point_meta=files[0]
test_point_meta
import pandas as pd
df_meta=pd.read_csv(FLDR+test_point_meta)
df_meta.head()
def ret_name(name):
  return str(name)+'.jpg'
df_meta['id']=df_meta['id'].apply(ret_name)
df_meta.head()
df_meta.to_csv(FLDR+test_point_meta,index=False)<jupyter_output><empty_output><jupyter_text>Creating dataset for Generator<jupyter_code>files_in_test=FLDR+'extracted_2/test1/'
files=os.listdir(FLDR)
files_train=FLDR+'extracted/train'
files_train
files=[]
labels=[]
for file in os.listdir(files_train):
  print(file)
  files.append(file)
  if 'cat' in file:
    labels.append(0)
  else:
    labels.append(1)

import pandas as pd
df_train_meta= pd.DataFrame(list(zip(files,labels)),columns=['id','label'])
df_train_meta.head()
files=os.listdir(FLDR)
files
df_test_meta=pd.read_csv(FLDR+files[-1])
df_test_meta.head()
train=['extracted/train/' for i in range(len(df_train_meta))]
test=['extracted_2/test1/' for i in range(len(df_test_meta))]
df_test_meta['Ext']=test
df_train_meta['Ext']=train
from sklearn.model_selection import train_test_split
train,val= train_test_split(df_train_meta, test_size=0.15)
train.to_csv(FLDR+'train.csv')
val.to_csv(FLDR+'val.csv')
df_test_meta.to_csv(FLDR+'test.csv')
fldrs_req=["Train","Test","Validate"]
fldrs_within=["Cat","Dog"]
for fldr in fldrs_req:
  os.mkdir(FLDR+fldr)
  for fldr_2 in fldrs_within:
    os.mkdir(FLDR+fldr+'/'+fldr_2)
import shutil
i=0
while (i<len(train)):
  if train.iloc[i]['label']==0:
    shutil.move(FLDR+train.iloc[i]['Ext']+train.iloc[i]['id'],FLDR+'Train/Cat')
  if train.iloc[i]['label']==1:
    shutil.move(FLDR+train.iloc[i]['Ext']+train.iloc[i]['id'],FLDR+'Train/Dog')
  i+=1
i=0
while (i<len(val)):
  if val.iloc[i]['label']==0:
    shutil.move(FLDR+val.iloc[i]['Ext']+val.iloc[i]['id'],FLDR+'Validate/Cat')
  if val.iloc[i]['label']==1:
    shutil.move(FLDR+val.iloc[i]['Ext']+val.iloc[i]['id'],FLDR+'Validate/Dog')
  i+=1
i=0
test=df_test_meta
while (i<len(test)):
  if test.iloc[i]['label']==0:
    shutil.move(FLDR+test.iloc[i]['Ext']+test.iloc[i]['id'],FLDR+'Test/Cat')
  if test.iloc[i]['label']==1:
    shutil.move(FLDR+test.iloc[i]['Ext']+test.iloc[i]['id'],FLDR+'Test/Dog')
  i+=1<jupyter_output><empty_output><jupyter_text>Starting for Training<jupyter_code>import pandas as pd
train=pd.read_csv(FLDR+'train.csv')
val=pd.read_csv(FLDR+'val.csv')
df_test_meta=pd.read_csv(FLDR+'test.csv')<jupyter_output><empty_output><jupyter_text>Initializing Image Data Generator<jupyter_code>from tensorflow.keras.applications.vgg19 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_set=ImageDataGenerator(
    rotation_range=20,
    zoom_range=0.1,
    shear_range=0.2,
    preprocessing_function=preprocess_input
)
val_set=ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = train_set.flow_from_directory(
    directory=FLDR+"Train/",
    target_size=(256, 256),
    batch_size=64,
    class_mode="binary"
)
validate_generator = val_set.flow_from_directory(
    directory=FLDR+"Validate/",
    target_size=(256, 256),
    batch_size=64,
    class_mode="binary"
)
test_generator = val_set.flow_from_directory(
    directory=FLDR+"Test/",
    target_size=(224, 224),
    batch_size=64,
    class_mode="binary"
)<jupyter_output>Found 12500 images belonging to 2 classes.<jupyter_text>Modelling<jupyter_code>from tensorflow.keras.applications import VGG19
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dropout, Flatten, Dense, Input
from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import ModelCheckpoint
base_model=VGG19(include_top=False, weights='imagenet',input_shape=(224,224,3))
from tensorflow.keras.layers import UpSampling2D,Conv2D,Concatenate
base_model.summary()
def get_model(shape=(256,256,3)):
  input_tensor=Input(shape)

  base_model=VGG19(include_top=False, weights='imagenet',input_tensor=input_tensor)
  layer_names = ["block5_pool", "block4_pool", "block3_pool", "block2_pool"] 
  layer_outputs = [base_model.get_layer(name).output for name in layer_names]

  feature_size=256

  C1,C2,C3,C4= layer_outputs

  P1=Conv2D(feature_size, kernel_size=1, strides=1, padding='same')(C1)
  P1_upsampled=UpSampling2D(size=(2, 2))(P1)
  P1=Conv2D(feature_size, kernel_size=3, strides=1, padding='same')(P1)

  P2=Conv2D(feature_size, kernel_size=1, strides=1, padding='same')(C2)
  P2=Concatenate()([P1_upsampled, P2])
  P2_upsampled=UpSampling2D(size=(2, 2))(P2)
  P2=Conv2D(feature_size, kernel_size=3, strides=1, padding='same')(P2)

  P3=Conv2D(feature_size, kernel_size=1, strides=1, padding='same')(C3)
  P3=Concatenate()([P2_upsampled, P3])
  P3_upsampled=UpSampling2D(size=(2, 2))(P3)
  P3=Conv2D(feature_size, kernel_size=3, strides=1, padding='same')(P3)

  P4=Conv2D(feature_size, kernel_size=1, strides=1, padding='same')(C4)
  P4=Concatenate()([P3_upsampled, P4])
  P4_upsampled=UpSampling2D(size=(2, 2))(P4)
  P4=Conv2D(feature_size, kernel_size=3, strides=1, padding='same')(P4)

  feature1 = Flatten()(P1)
  dp1 = Dropout(0.4)(feature1)
  preds1 = Dense(4, activation='relu')(dp1)

  feature2 = Flatten()(P2)
  dp2 = Dropout(0.4)(feature2)
  preds2 = Dense(4, activation='relu')(dp2)

  feature3 = Flatten()(P3)
  dp3 = Dropout(0.4)(feature3)
  preds3 = Dense(4, activation='relu')(dp3)

  feature4 = Flatten()(P4)
  dp4 = Dropout(0.4)(feature4)
  preds4 = Dense(4, activation='relu')(dp4)

  concatenated=Concatenate(axis=1)([preds1,preds2,preds3,preds4])
  out=Dense(1,activation='sigmoid')(concatenated)

  model = Model(inputs=base_model.input, outputs=out)
  for layer in model.layers:
    layer.trainable = True

  model.compile(optimizer=optimizers.Adam(lr=0.001), loss='binary_crossentropy',metrics=['accuracy'])

  return model

Model=get_model()
Model.summary()
from keras.utils.vis_utils import plot_model
plot_model(Model, to_file=fldr+'model_plot.png', show_shapes=True, show_layer_names=True)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>checkpoint = ModelCheckpoint(FLDR+"FPN_Model.h5", monitor='val_accuracy', save_best_only=True, mode='max') #creating checkpoint to save the best validation accuracy
callbacks_list = [checkpoint]
history = Model.fit_generator(train_generator, validation_data = validate_generator, steps_per_epoch=50, callbacks=callbacks_list,verbose=1,epochs = 30)<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>Model.evaluate_generator(train_generator)
Model.evaluate_generator(validation_generator)
Model.evaluate_generator(test_generator)
pred1=Model.predict_generator(test_generator)
import numpy as np
pred_n=np.round(pred_1)
pred_n=np.reshape((len(pred_n),)
true_lab=test_generator.classes
from sklearn.metrics import classification_report,confusion_matrix
print(classification_report(true_lab,pred_n))
print(confusion_matrix(true_lab,pred_n))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1525174076057847, 'fraction_numerical': 0.029726834493840386, 'mean_word_length': 8.649870801033591, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 31, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '119837', 'license': 'MIT', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2976, 'n_words': 375, 'path': 'Image Feature Extractors/Feature Pyramid Network/Feature_pyramid_network.ipynb', 'repo_name': 'abr-98/Guide-to-Computer-Vision', 'size': '7468'}