{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.image as im\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport tensorflow as tf\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:19.664708Z","iopub.execute_input":"2024-10-04T05:04:19.665559Z","iopub.status.idle":"2024-10-04T05:04:23.650832Z","shell.execute_reply.started":"2024-10-04T05:04:19.665509Z","shell.execute_reply":"2024-10-04T05:04:23.650015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras \nfrom keras import layers, Sequential\nfrom keras.layers import Dense, Dropout\nfrom keras.utils  import plot_model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-04T05:04:23.652907Z","iopub.execute_input":"2024-10-04T05:04:23.654020Z","iopub.status.idle":"2024-10-04T05:04:23.660279Z","shell.execute_reply.started":"2024-10-04T05:04:23.653969Z","shell.execute_reply":"2024-10-04T05:04:23.659548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 0\nnp.random.seed(seed)\ntf.random.set_seed(seed)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:23.661759Z","iopub.execute_input":"2024-10-04T05:04:23.662031Z","iopub.status.idle":"2024-10-04T05:04:23.679946Z","shell.execute_reply.started":"2024-10-04T05:04:23.662001Z","shell.execute_reply":"2024-10-04T05:04:23.679120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -ltr","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:23.680905Z","iopub.execute_input":"2024-10-04T05:04:23.681183Z","iopub.status.idle":"2024-10-04T05:04:24.693579Z","shell.execute_reply.started":"2024-10-04T05:04:23.681151Z","shell.execute_reply":"2024-10-04T05:04:24.692552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test1','train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/{}.zip'.format(zip_file),'r') as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:24.697580Z","iopub.execute_input":"2024-10-04T05:04:24.697894Z","iopub.status.idle":"2024-10-04T05:04:35.038209Z","shell.execute_reply.started":"2024-10-04T05:04:24.697858Z","shell.execute_reply":"2024-10-04T05:04:35.037351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_DIR_path = './train'\nfile_names = os.listdir(Train_DIR_path)\nprint('There are {} number of images in the directory'.format(len(file_names)))","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:35.039506Z","iopub.execute_input":"2024-10-04T05:04:35.039815Z","iopub.status.idle":"2024-10-04T05:04:35.058446Z","shell.execute_reply.started":"2024-10-04T05:04:35.039781Z","shell.execute_reply":"2024-10-04T05:04:35.057575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Test_DIR_path = './test1'\n##file_names = os.listdir(Test_DIR_path)\n##print('There are {} no of images in the test directory'.format(len(file_names')))","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:35.060521Z","iopub.execute_input":"2024-10-04T05:04:35.060816Z","iopub.status.idle":"2024-10-04T05:04:36.077029Z","shell.execute_reply.started":"2024-10-04T05:04:35.060784Z","shell.execute_reply":"2024-10-04T05:04:36.076056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -ltr","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:36.078686Z","iopub.execute_input":"2024-10-04T05:04:36.079016Z","iopub.status.idle":"2024-10-04T05:04:37.102996Z","shell.execute_reply.started":"2024-10-04T05:04:36.078980Z","shell.execute_reply":"2024-10-04T05:04:37.101471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_dataframe(file_names):\n    files,labels = list(),list()\n    for file  in file_names:\n        files.append(file)\n        labels.append(file[:3])\n    df = pd.DataFrame({'filenames':files,'Labels':labels})\n    return  df","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:37.105244Z","iopub.execute_input":"2024-10-04T05:04:37.105955Z","iopub.status.idle":"2024-10-04T05:04:37.112699Z","shell.execute_reply.started":"2024-10-04T05:04:37.105899Z","shell.execute_reply":"2024-10-04T05:04:37.111565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = to_dataframe(file_names)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:37.114037Z","iopub.execute_input":"2024-10-04T05:04:37.114502Z","iopub.status.idle":"2024-10-04T05:04:37.145111Z","shell.execute_reply.started":"2024-10-04T05:04:37.114451Z","shell.execute_reply":"2024-10-04T05:04:37.144182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:37.146270Z","iopub.execute_input":"2024-10-04T05:04:37.146599Z","iopub.status.idle":"2024-10-04T05:04:37.162532Z","shell.execute_reply.started":"2024-10-04T05:04:37.146566Z","shell.execute_reply":"2024-10-04T05:04:37.161583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Data set label distribution \\n:', df['Labels'].value_counts())\n\nsns.countplot(x= df['Labels'])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:37.163684Z","iopub.execute_input":"2024-10-04T05:04:37.164054Z","iopub.status.idle":"2024-10-04T05:04:37.470129Z","shell.execute_reply.started":"2024-10-04T05:04:37.164009Z","shell.execute_reply":"2024-10-04T05:04:37.468983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat = [file for file in file_names if file[:3]=='cat']\ndogs = [file for file in file_names if file[:3]=='dog']","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:37.471489Z","iopub.execute_input":"2024-10-04T05:04:37.471863Z","iopub.status.idle":"2024-10-04T05:04:37.489599Z","shell.execute_reply.started":"2024-10-04T05:04:37.471817Z","shell.execute_reply":"2024-10-04T05:04:37.488699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = im.imread('./train/dog.11232.jpg')\nprint(image.shape)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:37.494650Z","iopub.execute_input":"2024-10-04T05:04:37.494983Z","iopub.status.idle":"2024-10-04T05:04:37.923041Z","shell.execute_reply.started":"2024-10-04T05:04:37.494948Z","shell.execute_reply":"2024-10-04T05:04:37.921971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = im.imread('./train/cat.12232.jpg')\nprint(image.shape)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:37.924337Z","iopub.execute_input":"2024-10-04T05:04:37.924818Z","iopub.status.idle":"2024-10-04T05:04:38.318122Z","shell.execute_reply.started":"2024-10-04T05:04:37.924759Z","shell.execute_reply":"2024-10-04T05:04:38.317083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(2,2,figsize=(15,7))\n\nfor i,c in enumerate(np.random.randint(0,len(cat),4),start=1):\n    plt.subplot(2,2,i)                  \n    image = im.imread('./train/'+cat[c])\n    print(image.shape)\n    plt.imshow(image)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:38.319556Z","iopub.execute_input":"2024-10-04T05:04:38.319929Z","iopub.status.idle":"2024-10-04T05:04:39.174103Z","shell.execute_reply.started":"2024-10-04T05:04:38.319887Z","shell.execute_reply":"2024-10-04T05:04:39.173031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nrandom_cat = random.sample(cat,4)\n\nfig,ax = plt.subplots(2,2,figsize=(15,7))\nfor i,img in enumerate(random_cat):\n    image = im.imread('./train/'+img)\n    ax[i//2,i%2].imshow(image)\n    print(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:39.175329Z","iopub.execute_input":"2024-10-04T05:04:39.175645Z","iopub.status.idle":"2024-10-04T05:04:40.119630Z","shell.execute_reply.started":"2024-10-04T05:04:39.175611Z","shell.execute_reply":"2024-10-04T05:04:40.118694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_images = random.sample(dogs,4)\n\nfig,ax = plt.subplots(2,2,figsize=(15,7))\n\nfor i,img in enumerate(dog_images):\n    image = im.imread('./train/'+img)\n    ax[i//2,i%2].imshow(image)\n    print(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:40.120958Z","iopub.execute_input":"2024-10-04T05:04:40.121337Z","iopub.status.idle":"2024-10-04T05:04:41.057848Z","shell.execute_reply.started":"2024-10-04T05:04:40.121280Z","shell.execute_reply":"2024-10-04T05:04:41.056960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_set,validation_set =train_test_split(df,test_size=0.2,random_state=seed) ","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.058990Z","iopub.execute_input":"2024-10-04T05:04:41.059362Z","iopub.status.idle":"2024-10-04T05:04:41.127960Z","shell.execute_reply.started":"2024-10-04T05:04:41.059315Z","shell.execute_reply":"2024-10-04T05:04:41.127151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.shape, validation_set.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.129277Z","iopub.execute_input":"2024-10-04T05:04:41.129627Z","iopub.status.idle":"2024-10-04T05:04:41.135687Z","shell.execute_reply.started":"2024-10-04T05:04:41.129592Z","shell.execute_reply":"2024-10-04T05:04:41.134725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7))\nsns.countplot(x =train_set['Labels'])\nprint('Test Set Distribution:',train_set['Labels'].value_counts())\nplt.title('Train set')\n\nplt.figure(figsize=(15,7))\nsns.countplot(x =validation_set['Labels'])\nprint('Validation set Distribution:',validation_set['Labels'].value_counts())\nplt.title('validation set')\n","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.136751Z","iopub.execute_input":"2024-10-04T05:04:41.137110Z","iopub.status.idle":"2024-10-04T05:04:41.590345Z","shell.execute_reply.started":"2024-10-04T05:04:41.137042Z","shell.execute_reply":"2024-10-04T05:04:41.589427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augumentation and Image Data Generator","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.591900Z","iopub.execute_input":"2024-10-04T05:04:41.592301Z","iopub.status.idle":"2024-10-04T05:04:41.608149Z","shell.execute_reply.started":"2024-10-04T05:04:41.592238Z","shell.execute_reply":"2024-10-04T05:04:41.607307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"width, height = 150,150\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.609271Z","iopub.execute_input":"2024-10-04T05:04:41.609605Z","iopub.status.idle":"2024-10-04T05:04:41.614065Z","shell.execute_reply.started":"2024-10-04T05:04:41.609566Z","shell.execute_reply":"2024-10-04T05:04:41.613170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.615107Z","iopub.execute_input":"2024-10-04T05:04:41.615407Z","iopub.status.idle":"2024-10-04T05:04:41.631160Z","shell.execute_reply.started":"2024-10-04T05:04:41.615372Z","shell.execute_reply":"2024-10-04T05:04:41.630236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255, #rescaling the pixels\n                                 rotation_range=15, #rotate the image for better prediction\n                                 zoom_range=0.2,\n                                 shear_range = 0.1,\n                                 horizontal_flip = True,\n                                 width_shift_range=0.1,\n                                 height_shift_range=0.1\n                                )\ntraining_data = train_datagen.flow_from_dataframe(dataframe=df,\n                                                 directory='./train',\n                                                 x_col = 'filenames',#feature\n                                                 y_col='Labels',#target\n                                                 target_size=(width,height),#same height,width for all data\n                                                 class_mode='categorical',# reads data like one_hot_encoding, binary works like label encoder\n                                                 batch_size=batch_size) #increase read speed","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.632438Z","iopub.execute_input":"2024-10-04T05:04:41.632898Z","iopub.status.idle":"2024-10-04T05:04:41.928909Z","shell.execute_reply.started":"2024-10-04T05:04:41.632854Z","shell.execute_reply":"2024-10-04T05:04:41.928138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data.class_indices","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.930101Z","iopub.execute_input":"2024-10-04T05:04:41.930513Z","iopub.status.idle":"2024-10-04T05:04:41.936584Z","shell.execute_reply.started":"2024-10-04T05:04:41.930466Z","shell.execute_reply":"2024-10-04T05:04:41.935655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_images_to_display = 8\n\nfor i in range(num_images_to_display):\n    image_batch, label_batch = next(training_data) \n    image = image_batch[0] \n    plt.imshow(image)  \n    plt.axis('off')  \n    plt.show()  ","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:41.937753Z","iopub.execute_input":"2024-10-04T05:04:41.938109Z","iopub.status.idle":"2024-10-04T05:04:44.963008Z","shell.execute_reply.started":"2024-10-04T05:04:41.938066Z","shell.execute_reply":"2024-10-04T05:04:44.961988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen=ImageDataGenerator(rescale=1./255) #rescaling the pixel\nvalidation_data = validation_datagen.flow_from_dataframe(dataframe=validation_set,\n                                                 directory='./train',\n                                                 x_col = 'filenames',#feature\n                                                 y_col='Labels',#target\n                                                 target_size=(width,height),#same height,width for all data\n                                                 class_mode='categorical',# reads data like one_hot_encoding, binary works like label encoder\n                                                 batch_size=batch_size) #increase read speed","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:44.964333Z","iopub.execute_input":"2024-10-04T05:04:44.965516Z","iopub.status.idle":"2024-10-04T05:04:45.037060Z","shell.execute_reply.started":"2024-10-04T05:04:44.965465Z","shell.execute_reply":"2024-10-04T05:04:45.036338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making CNN Model","metadata":{}},{"cell_type":"code","source":"from keras import Sequential\nfrom keras.layers import Dense, Dropout\n\n\nmodel = keras.Sequential([\n    layers.Conv2D(128,kernel_size=4,activation='relu',input_shape=(width,height,3)),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Conv2D(64,kernel_size=4,activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Conv2D(32,kernel_size=4,activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Conv2D(16,kernel_size=4,activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Conv2D(8,kernel_size=4,activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Flatten(),\n    \n    layers.Dense(32,activation='relu'),\n    Dropout(0.2),\n    layers.Dense(16,activation='relu'),\n    Dropout(0.2),\n    layers.Dense(8,activation='relu'),\n    Dropout(0.2),\n    layers.Dense(2,activation='softmax')\n])\n","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:45.038231Z","iopub.execute_input":"2024-10-04T05:04:45.038565Z","iopub.status.idle":"2024-10-04T05:04:45.590274Z","shell.execute_reply.started":"2024-10-04T05:04:45.038529Z","shell.execute_reply":"2024-10-04T05:04:45.589493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:45.591405Z","iopub.execute_input":"2024-10-04T05:04:45.591712Z","iopub.status.idle":"2024-10-04T05:04:45.625823Z","shell.execute_reply.started":"2024-10-04T05:04:45.591678Z","shell.execute_reply":"2024-10-04T05:04:45.624963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.utils.plot_model(model,show_layer_activations=True,show_layer_names=True,show_shapes=True,show_dtype=True,to_file='model.png')","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:45.627138Z","iopub.execute_input":"2024-10-04T05:04:45.627544Z","iopub.status.idle":"2024-10-04T05:04:46.486548Z","shell.execute_reply.started":"2024-10-04T05:04:45.627497Z","shell.execute_reply":"2024-10-04T05:04:46.485670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import optimizers\nopt = optimizers.Adam(learning_rate=0.01)\n\nmodel.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:46.487797Z","iopub.execute_input":"2024-10-04T05:04:46.488155Z","iopub.status.idle":"2024-10-04T05:04:46.505788Z","shell.execute_reply.started":"2024-10-04T05:04:46.488111Z","shell.execute_reply":"2024-10-04T05:04:46.504712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau,EarlyStopping,ModelCheckpoint\n\nearly_stopping = EarlyStopping(monitor='val_loss',patience=4,restore_best_weights=True)\nreduce_lr = ReduceLROnPlateau(monitor='val_loss',patience=4,min_lr=0.0001,factor=0.5)\ncheckpoint = ModelCheckpoint('model.keras',save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:46.506923Z","iopub.execute_input":"2024-10-04T05:04:46.507254Z","iopub.status.idle":"2024-10-04T05:04:46.513415Z","shell.execute_reply.started":"2024-10-04T05:04:46.507216Z","shell.execute_reply":"2024-10-04T05:04:46.512469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_data,\n                    validation_data=validation_data,\n                    verbose=1,\n                    callbacks=[early_stopping,reduce_lr,checkpoint],\n                    epochs=20,steps_per_epoch = 10,\n                    #steps_per_epoch=10,\n                   validation_steps = validation_set.shape[0]//batch_size)\n                   #steps_per_epoch = train_set.shape[0]//batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:04:46.514504Z","iopub.execute_input":"2024-10-04T05:04:46.514786Z","iopub.status.idle":"2024-10-04T05:05:44.791681Z","shell.execute_reply.started":"2024-10-04T05:04:46.514755Z","shell.execute_reply":"2024-10-04T05:05:44.790834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(validation_data)\nprint('Test Accuracy:',score[1])\nprint('Test loss:',score[0])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:44.793098Z","iopub.execute_input":"2024-10-04T05:05:44.793623Z","iopub.status.idle":"2024-10-04T05:05:51.900731Z","shell.execute_reply.started":"2024-10-04T05:05:44.793565Z","shell.execute_reply":"2024-10-04T05:05:51.899821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\nmodel1 = load_model('model.keras')","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:51.901838Z","iopub.execute_input":"2024-10-04T05:05:51.902149Z","iopub.status.idle":"2024-10-04T05:05:52.527180Z","shell.execute_reply.started":"2024-10-04T05:05:51.902115Z","shell.execute_reply":"2024-10-04T05:05:52.526352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.getcwd())","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.528634Z","iopub.execute_input":"2024-10-04T05:05:52.528954Z","iopub.status.idle":"2024-10-04T05:05:52.533950Z","shell.execute_reply.started":"2024-10-04T05:05:52.528919Z","shell.execute_reply":"2024-10-04T05:05:52.533028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer leraning","metadata":{}},{"cell_type":"code","source":"base_model = keras.applications.vgg16.VGG16(weights='imagenet',include_top=False,input_shape=(width,height,3))","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.534968Z","iopub.execute_input":"2024-10-04T05:05:52.535322Z","iopub.status.idle":"2024-10-04T05:05:52.863849Z","shell.execute_reply.started":"2024-10-04T05:05:52.535256Z","shell.execute_reply":"2024-10-04T05:05:52.862705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.trainable=False","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.865319Z","iopub.execute_input":"2024-10-04T05:05:52.865756Z","iopub.status.idle":"2024-10-04T05:05:52.871191Z","shell.execute_reply.started":"2024-10-04T05:05:52.865700Z","shell.execute_reply":"2024-10-04T05:05:52.870188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.872195Z","iopub.execute_input":"2024-10-04T05:05:52.872497Z","iopub.status.idle":"2024-10-04T05:05:52.913232Z","shell.execute_reply.started":"2024-10-04T05:05:52.872464Z","shell.execute_reply":"2024-10-04T05:05:52.912359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.919614Z","iopub.execute_input":"2024-10-04T05:05:52.919894Z","iopub.status.idle":"2024-10-04T05:05:52.925499Z","shell.execute_reply.started":"2024-10-04T05:05:52.919863Z","shell.execute_reply":"2024-10-04T05:05:52.924459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import *\n\nmodel = keras.models.Sequential()\nmodel.add(base_model)\nmodel.add(Flatten())\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(2,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.926536Z","iopub.execute_input":"2024-10-04T05:05:52.926833Z","iopub.status.idle":"2024-10-04T05:05:52.944418Z","shell.execute_reply.started":"2024-10-04T05:05:52.926801Z","shell.execute_reply":"2024-10-04T05:05:52.943448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.945761Z","iopub.execute_input":"2024-10-04T05:05:52.946343Z","iopub.status.idle":"2024-10-04T05:05:52.964583Z","shell.execute_reply.started":"2024-10-04T05:05:52.946272Z","shell.execute_reply":"2024-10-04T05:05:52.963718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.965700Z","iopub.execute_input":"2024-10-04T05:05:52.966050Z","iopub.status.idle":"2024-10-04T05:05:52.974742Z","shell.execute_reply.started":"2024-10-04T05:05:52.966003Z","shell.execute_reply":"2024-10-04T05:05:52.974004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_loss',patience=4,restore_best_weights=True)\nreduce_lr = ReduceLROnPlateau(monitor='val_loss',patience=4,min_lr=0.0001,factor=0.5)\ncheckpoint = ModelCheckpoint('TLmodel.keras',save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.975885Z","iopub.execute_input":"2024-10-04T05:05:52.976640Z","iopub.status.idle":"2024-10-04T05:05:52.984641Z","shell.execute_reply.started":"2024-10-04T05:05:52.976594Z","shell.execute_reply":"2024-10-04T05:05:52.983853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_data,epochs=30,validation_data=validation_data,steps_per_epoch=10,callbacks=[early_stopping,reduce_lr,checkpoint])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:05:52.985782Z","iopub.execute_input":"2024-10-04T05:05:52.986140Z","iopub.status.idle":"2024-10-04T05:10:04.342370Z","shell.execute_reply.started":"2024-10-04T05:05:52.986098Z","shell.execute_reply":"2024-10-04T05:10:04.341507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation = model.evaluate(validation_data)\nprint('Test Accuracy',evaluation[1])\nprint('Test Accuracy',evaluation[0])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:10:04.343623Z","iopub.execute_input":"2024-10-04T05:10:04.343934Z","iopub.status.idle":"2024-10-04T05:10:11.478245Z","shell.execute_reply.started":"2024-10-04T05:10:04.343901Z","shell.execute_reply":"2024-10-04T05:10:11.477342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'],label= 'Training Accuracy')\nplt.plot(history.history['val_accuracy'],label = 'Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.title('Model Accuracy')","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:10:11.479541Z","iopub.execute_input":"2024-10-04T05:10:11.479860Z","iopub.status.idle":"2024-10-04T05:10:11.775432Z","shell.execute_reply.started":"2024-10-04T05:10:11.479824Z","shell.execute_reply":"2024-10-04T05:10:11.774462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\nmodel2 = load_model('TLmodel.keras')","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:10:11.776861Z","iopub.execute_input":"2024-10-04T05:10:11.777543Z","iopub.status.idle":"2024-10-04T05:10:12.968425Z","shell.execute_reply.started":"2024-10-04T05:10:11.777491Z","shell.execute_reply":"2024-10-04T05:10:12.967367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fine Tuning by unfrezzing the weights","metadata":{}},{"cell_type":"code","source":"for layers in base_model.layers[0:2]:\n    layers.trainable= True","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:10:12.969702Z","iopub.execute_input":"2024-10-04T05:10:12.969995Z","iopub.status.idle":"2024-10-04T05:10:12.974670Z","shell.execute_reply.started":"2024-10-04T05:10:12.969963Z","shell.execute_reply":"2024-10-04T05:10:12.973695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:10:12.975923Z","iopub.execute_input":"2024-10-04T05:10:12.976358Z","iopub.status.idle":"2024-10-04T05:10:12.989982Z","shell.execute_reply.started":"2024-10-04T05:10:12.976276Z","shell.execute_reply":"2024-10-04T05:10:12.989108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_data,epochs=30,validation_data=validation_data,steps_per_epoch=10)","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:10:12.991079Z","iopub.execute_input":"2024-10-04T05:10:12.991398Z","iopub.status.idle":"2024-10-04T05:14:22.536960Z","shell.execute_reply.started":"2024-10-04T05:10:12.991364Z","shell.execute_reply":"2024-10-04T05:14:22.536009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation = model.evaluate(validation_data)\nprint('Test Accuracy',evaluation[1])\nprint('Test Accuracy',evaluation[0])","metadata":{"execution":{"iopub.status.busy":"2024-10-04T05:14:22.540326Z","iopub.execute_input":"2024-10-04T05:14:22.540665Z","iopub.status.idle":"2024-10-04T05:14:29.762999Z","shell.execute_reply.started":"2024-10-04T05:14:22.540629Z","shell.execute_reply":"2024-10-04T05:14:29.762137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}