{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Extraction data from zip files**","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T20:55:08.034447Z","iopub.execute_input":"2023-08-31T20:55:08.035028Z","iopub.status.idle":"2023-08-31T20:55:08.045958Z","shell.execute_reply.started":"2023-08-31T20:55:08.034971Z","shell.execute_reply":"2023-08-31T20:55:08.044754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1 : Extraction\n\nwe will extaract the data and use only 2000 of the total data to make it faster and we will try to reach high accuracy ","metadata":{}},{"cell_type":"code","source":"import zipfile\noriginal_bath ='/kaggle/input/dogs-vs-cats/train.zip'\ndirectory_to_extract_to= \"/kaggle/working/data\"\nwith zipfile.ZipFile(original_bath, 'r') as zip_ref:\n    zip_ref.extractall(directory_to_extract_to)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:55:08.048515Z","iopub.execute_input":"2023-08-31T20:55:08.049061Z","iopub.status.idle":"2023-08-31T20:55:16.944896Z","shell.execute_reply.started":"2023-08-31T20:55:08.049024Z","shell.execute_reply":"2023-08-31T20:55:16.943719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\noriginal_bath ='/kaggle/input/dogs-vs-cats/test1.zip'\ndirectory_to_extract_to= \"/kaggle/working/data\"\nwith zipfile.ZipFile(original_bath, 'r') as zip_ref:\n    zip_ref.extractall(directory_to_extract_to)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:26:53.614556Z","iopub.execute_input":"2023-08-31T21:26:53.615185Z","iopub.status.idle":"2023-08-31T21:27:00.798478Z","shell.execute_reply.started":"2023-08-31T21:26:53.615147Z","shell.execute_reply":"2023-08-31T21:27:00.797421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os , shutil\n\n\noriginal_bath = \"/kaggle/working/data\"\nbase_dir = '/kaggle/working//cats_and_dogs_small'\ntrain_dir = os.path.join(base_dir , 'train')\nvalidation_dir = os.path.join(base_dir , 'validation')\ntest_dir = os.path.join(base_dir , 'test')\n\nos.mkdir(base_dir)\n\nos.mkdir(train_dir)\nos.mkdir(validation_dir)\nos.mkdir(test_dir)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:38:41.717722Z","iopub.execute_input":"2023-08-31T21:38:41.718471Z","iopub.status.idle":"2023-08-31T21:38:41.760904Z","shell.execute_reply.started":"2023-08-31T21:38:41.718438Z","shell.execute_reply":"2023-08-31T21:38:41.758317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cats_dir = os.path.join(train_dir , 'cats')\ntrain_dogs_dir = os.path.join(train_dir , 'dogs')\nos.mkdir(train_cats_dir)\nos.mkdir(train_dogs_dir)\n\n\nvalidation_cats_dir = os.path.join(validation_dir , 'cats')\nvalidation_dogs_dir = os.path.join(validation_dir , 'dogs')\nos.mkdir(validation_cats_dir)\nos.mkdir(validation_dogs_dir)\n\n\ntest_cats_dir = os.path.join(test_dir , 'cats')\ntest_dogs_dir = os.path.join(test_dir , 'dogs')\nos.mkdir(test_cats_dir)\nos.mkdir(test_dogs_dir)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:55:16.998778Z","iopub.status.idle":"2023-08-31T20:55:16.999314Z","shell.execute_reply.started":"2023-08-31T20:55:16.999025Z","shell.execute_reply":"2023-08-31T20:55:16.999053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames :\n    src  = os.path.join(original_bath+'/train' , fname)\n    dst = os.path.join(train_cats_dir , fname)\n    shutil.copyfile(src , dst)\nfnames = ['cat.{}.jpg'.format(i) for i in range(1000 , 1500)]\nfor fname in fnames :\n    src  = os.path.join(original_bath+'/train' , fname)\n    dst = os.path.join(validation_cats_dir , fname)\n    shutil.copyfile(src , dst)\nfnames = ['cat.{}.jpg'.format(i) for i in range(1500 , 2000)]\nfor fname in fnames :\n    src  = os.path.join(original_bath+'/train' , fname)\n    dst = os.path.join(test_cats_dir , fname)\n    shutil.copyfile(src , dst)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:55:17.001509Z","iopub.status.idle":"2023-08-31T20:55:17.002634Z","shell.execute_reply.started":"2023-08-31T20:55:17.002355Z","shell.execute_reply":"2023-08-31T20:55:17.002384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = ['dog.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames :\n    src  = os.path.join(original_bath+'/train' , fname)\n    dst = os.path.join(train_dogs_dir , fname)\n    shutil.copyfile(src , dst)\nfnames = ['dog.{}.jpg'.format(i) for i in range(1000 , 1500)]\nfor fname in fnames :\n    src  = os.path.join(original_bath+'/train' , fname)\n    dst = os.path.join(validation_dogs_dir , fname)\n    shutil.copyfile(src , dst)\nfnames = ['dog.{}.jpg'.format(i) for i in range(1500  , 2000)]\nfor fname in fnames :\n    src  = os.path.join(original_bath+'/train' , fname)\n    dst = os.path.join(test_dogs_dir , fname)\n    shutil.copyfile(src , dst)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:55:17.003999Z","iopub.status.idle":"2023-08-31T20:55:17.005111Z","shell.execute_reply.started":"2023-08-31T20:55:17.004828Z","shell.execute_reply":"2023-08-31T20:55:17.004859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total training cat images:', len(os.listdir(train_cats_dir)))\nprint('total training dogs images:', len(os.listdir(train_dogs_dir)))\n\nprint('total validation cat images:', len(os.listdir(validation_cats_dir)))\nprint('total validation dogs images:', len(os.listdir(validation_dogs_dir)))\n\nprint('total test cat images:', len(os.listdir(test_cats_dir)))\nprint('total test dogs images:', len(os.listdir(test_dogs_dir)))\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:56:08.738217Z","iopub.execute_input":"2023-08-31T20:56:08.738615Z","iopub.status.idle":"2023-08-31T20:56:08.754383Z","shell.execute_reply.started":"2023-08-31T20:56:08.738581Z","shell.execute_reply":"2023-08-31T20:56:08.749379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\nfrom keras import models\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu',\ninput_shape=(150, 150, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Flatten())\n\nmodel.add(layers.Dropout(.5))\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:56:11.093716Z","iopub.execute_input":"2023-08-31T20:56:11.094108Z","iopub.status.idle":"2023-08-31T20:56:11.192251Z","shell.execute_reply.started":"2023-08-31T20:56:11.094075Z","shell.execute_reply":"2023-08-31T20:56:11.191312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:56:11.610297Z","iopub.execute_input":"2023-08-31T20:56:11.610639Z","iopub.status.idle":"2023-08-31T20:56:11.641240Z","shell.execute_reply.started":"2023-08-31T20:56:11.610608Z","shell.execute_reply":"2023-08-31T20:56:11.640431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import optimizers\n\nmodel.compile(loss = \"binary_crossentropy\" , optimizer=optimizers.RMSprop(learning_rate=1e-4) ,metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:56:11.812025Z","iopub.execute_input":"2023-08-31T20:56:11.812334Z","iopub.status.idle":"2023-08-31T20:56:11.824232Z","shell.execute_reply.started":"2023-08-31T20:56:11.812308Z","shell.execute_reply":"2023-08-31T20:56:11.823182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\n\n\ntrain_datagen = ImageDataGenerator(rescale=1./255,\nrotation_range=40,\nwidth_shift_range=0.2,\nheight_shift_range=0.2,\nshear_range=0.2,\nzoom_range=0.2,\nhorizontal_flip=True,\n                                   )\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\n\n\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir , \n    batch_size=32 , \n    class_mode='binary',\n    target_size=(150 , 150)\n)\nvlidation_generator = test_datagen.flow_from_directory(\n    validation_dir , \n    batch_size=32 , \n    target_size=(150 , 150),\n\n    class_mode='binary'\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:56:12.038156Z","iopub.execute_input":"2023-08-31T20:56:12.038829Z","iopub.status.idle":"2023-08-31T20:56:12.147908Z","shell.execute_reply.started":"2023-08-31T20:56:12.038786Z","shell.execute_reply":"2023-08-31T20:56:12.146965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch , labels_batch in train_generator:\n    print('data batch shape' , data_batch.shape)\n    print('labels batch shape' , labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:56:12.394394Z","iopub.execute_input":"2023-08-31T20:56:12.395632Z","iopub.status.idle":"2023-08-31T20:56:12.652866Z","shell.execute_reply.started":"2023-08-31T20:56:12.395566Z","shell.execute_reply":"2023-08-31T20:56:12.651694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator , \n    steps_per_epoch=20 , \n    epochs=100 , \n    validation_data=vlidation_generator , \n    validation_steps=10\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T20:56:12.690914Z","iopub.execute_input":"2023-08-31T20:56:12.691518Z","iopub.status.idle":"2023-08-31T21:06:08.850049Z","shell.execute_reply.started":"2023-08-31T20:56:12.691477Z","shell.execute_reply":"2023-08-31T21:06:08.849002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['acc']\nval_acc= history.history['val_acc']\nloss = history.history['loss']\nval_loss= history.history['val_loss']\n\nepochs = range(1 , len(acc)+1)\nplt.plot(epochs , acc , 'bo' , label = \"Training acc\")\nplt.plot(epochs , val_acc , 'b' , label =\"Validation acc\")\nplt.title(\"Training and Validation accuracy\")\nplt.legend()\nplt.figure()\nplt.plot(epochs , loss , 'bo' , label = \"Training loss\")\nplt.plot(epochs , val_loss , 'b' , label =\"Validation loss\")\nplt.title(\"Training and Validation loss\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:06:08.852390Z","iopub.execute_input":"2023-08-31T21:06:08.852829Z","iopub.status.idle":"2023-08-31T21:06:09.606602Z","shell.execute_reply.started":"2023-08-31T21:06:08.852770Z","shell.execute_reply":"2023-08-31T21:06:09.605725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##using VGG 16 \n\nfrom keras.applications import VGG16\nconv_base = VGG16(weights = 'imagenet' , \n                  include_top =False , \n                  input_shape=(150 , 150 , 3))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:06:09.608169Z","iopub.execute_input":"2023-08-31T21:06:09.608742Z","iopub.status.idle":"2023-08-31T21:06:10.440851Z","shell.execute_reply.started":"2023-08-31T21:06:09.608707Z","shell.execute_reply":"2023-08-31T21:06:10.439817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(rescale=1./255)\nbatch_size = 20\ndef extract_features (direcrory , sample_count):\n    ##(4,4,512) because the last shape in vgg is (4,4,512)\n    features =np.zeros(shape =(sample_count , 4,4,512))\n    labels = np.zeros(shape=(sample_count))\n    generator = datagen.flow_from_directory(\n        direcrory, \n        target_size=(150 , 150),\n        batch_size=batch_size,\n        class_mode='binary'\n\n    )\n    i=0\n    for input_batch , labels_batch in generator:\n        feature_batch = conv_base.predict(input_batch)\n        features[i*batch_size :(i+1) * batch_size] =feature_batch\n        labels[i*batch_size : (i+1)*batch_size]= labels_batch\n        i+=1\n        if i*batch_size >= sample_count:\n            break\n    return features , labels","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:06:38.775685Z","iopub.execute_input":"2023-08-31T21:06:38.776437Z","iopub.status.idle":"2023-08-31T21:06:38.785838Z","shell.execute_reply.started":"2023-08-31T21:06:38.776403Z","shell.execute_reply":"2023-08-31T21:06:38.784798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features , train_labels = extract_features(train_dir , 2000)\nvalidation_features , validation_labels = extract_features(validation_dir , 1000)\ntest_features , test_labels = extract_features(test_dir , 1000)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:06:39.890887Z","iopub.execute_input":"2023-08-31T21:06:39.891837Z","iopub.status.idle":"2023-08-31T21:08:01.642306Z","shell.execute_reply.started":"2023-08-31T21:06:39.891789Z","shell.execute_reply":"2023-08-31T21:08:01.641218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers , models\n\ntrain_features = np.reshape(train_features , (2000 , 4*4*512))\nvalidation_features = np.reshape(validation_features , (1000 , 4*4*512))\ntest_features = np.reshape(test_features , (1000 , 4*4*512))\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:08:01.644206Z","iopub.execute_input":"2023-08-31T21:08:01.644573Z","iopub.status.idle":"2023-08-31T21:08:01.652160Z","shell.execute_reply.started":"2023-08-31T21:08:01.644528Z","shell.execute_reply":"2023-08-31T21:08:01.650984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Dense(256 , activation=\"relu\" , input_dim = 512*4*4))\nmodel.add(layers.Dropout(.5))\nmodel.add(layers.Dense(1 ,activation=\"sigmoid\"))\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:08:01.653796Z","iopub.execute_input":"2023-08-31T21:08:01.654157Z","iopub.status.idle":"2023-08-31T21:08:01.698140Z","shell.execute_reply.started":"2023-08-31T21:08:01.654121Z","shell.execute_reply":"2023-08-31T21:08:01.697262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import optimizers\nmodel.compile(loss='binary_crossentropy' , optimizer=optimizers.RMSprop(lr=1e-4) ,metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:08:01.700318Z","iopub.execute_input":"2023-08-31T21:08:01.700897Z","iopub.status.idle":"2023-08-31T21:08:01.717533Z","shell.execute_reply.started":"2023-08-31T21:08:01.700863Z","shell.execute_reply":"2023-08-31T21:08:01.716615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_features, train_labels , \n                    epochs=30 , \n                    validation_data=(validation_features , validation_labels))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:08:01.718925Z","iopub.execute_input":"2023-08-31T21:08:01.719487Z","iopub.status.idle":"2023-08-31T21:08:13.991131Z","shell.execute_reply.started":"2023-08-31T21:08:01.719452Z","shell.execute_reply":"2023-08-31T21:08:13.990107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc= history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1 , len(acc)+1)\nplt.plot(epochs , acc ,'bo' , label = \"Training acc\")\nplt.plot(epochs , val_acc ,'b' , label = \"Validation acc\")\nplt.legend()\nplt.figure()\n\nplt.plot(epochs , loss ,'bo' , label = \"Training loss\")\nplt.plot(epochs , val_loss ,'b' , label = \"Validation loss\")\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:08:13.993278Z","iopub.execute_input":"2023-08-31T21:08:13.993669Z","iopub.status.idle":"2023-08-31T21:08:14.523798Z","shell.execute_reply.started":"2023-08-31T21:08:13.993615Z","shell.execute_reply":"2023-08-31T21:08:14.522776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers , models\nmodel  = models.Sequential()\nmodel.add(conv_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256 , activation=\"relu\"))\nmodel.add(layers.Dense(1 , activation=\"sigmoid\"))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:08:42.199272Z","iopub.execute_input":"2023-08-31T21:08:42.199632Z","iopub.status.idle":"2023-08-31T21:08:42.299524Z","shell.execute_reply.started":"2023-08-31T21:08:42.199598Z","shell.execute_reply":"2023-08-31T21:08:42.298450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:08:58.246454Z","iopub.execute_input":"2023-08-31T21:08:58.246885Z","iopub.status.idle":"2023-08-31T21:08:58.268056Z","shell.execute_reply.started":"2023-08-31T21:08:58.246850Z","shell.execute_reply":"2023-08-31T21:08:58.267344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers\ntrain_datagen = ImageDataGenerator(\nrescale=1./255,\nrotation_range=40,\nwidth_shift_range=0.2,\nheight_shift_range=0.2,\nshear_range=0.2,\nzoom_range=0.2,\nhorizontal_flip=True,\nfill_mode='nearest')\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntrain_generator = train_datagen.flow_from_directory(\ntrain_dir,\ntarget_size=(150, 150),\nbatch_size=20,\nclass_mode='binary')\nvalidation_generator = test_datagen.flow_from_directory(\nvalidation_dir,\ntarget_size=(150, 150),\nbatch_size=20,\nclass_mode='binary')\nmodel.compile(loss='binary_crossentropy',\noptimizer=optimizers.RMSprop(lr=2e-5),\nmetrics=['acc'])\nhistory = model.fit_generator(\ntrain_generator,\nsteps_per_epoch=100,\nepochs=30,\nvalidation_data=validation_generator,\nvalidation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:09:14.181280Z","iopub.execute_input":"2023-08-31T21:09:14.181651Z","iopub.status.idle":"2023-08-31T21:23:37.506070Z","shell.execute_reply.started":"2023-08-31T21:09:14.181617Z","shell.execute_reply":"2023-08-31T21:23:37.505110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc= history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1 , len(acc)+1)\nplt.plot(epochs , acc ,'bo' , label = \"Training acc\")\nplt.plot(epochs , val_acc ,'b' , label = \"Validation acc\")\nplt.legend()\nplt.figure()\n\nplt.plot(epochs , loss ,'bo' , label = \"Training loss\")\nplt.plot(epochs , val_loss ,'b' , label = \"Validation loss\")\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:23:37.509410Z","iopub.execute_input":"2023-08-31T21:23:37.509722Z","iopub.status.idle":"2023-08-31T21:23:38.030557Z","shell.execute_reply.started":"2023-08-31T21:23:37.509692Z","shell.execute_reply":"2023-08-31T21:23:38.029659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = True\nset_trainable = False\nfor layer in conv_base.layers:\n    if layer.name == 'block5_conv1':\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:56:29.780805Z","iopub.execute_input":"2023-08-31T21:56:29.781198Z","iopub.status.idle":"2023-08-31T21:56:29.789016Z","shell.execute_reply.started":"2023-08-31T21:56:29.781165Z","shell.execute_reply":"2023-08-31T21:56:29.787825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss ='binary_crossentropy' , \n              optimizer=optimizers.RMSprop(lr=1e-4) , \n            metrics=['acc'])\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:56:49.197798Z","iopub.execute_input":"2023-08-31T21:56:49.198233Z","iopub.status.idle":"2023-08-31T21:56:49.219975Z","shell.execute_reply.started":"2023-08-31T21:56:49.198196Z","shell.execute_reply":"2023-08-31T21:56:49.219077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator , \n                    epochs=100 , \n                    steps_per_epoch=100 ,\n                    validation_data=validation_generator , \n                    validation_steps=50)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:57:02.067485Z","iopub.execute_input":"2023-08-31T21:57:02.067894Z","iopub.status.idle":"2023-08-31T22:00:20.581691Z","shell.execute_reply.started":"2023-08-31T21:57:02.067858Z","shell.execute_reply":"2023-08-31T22:00:20.575540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc= history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1 , len(acc)+1)\nplt.plot(epochs , acc ,'bo' , label = \"Training acc\")\nplt.plot(epochs , val_acc ,'b' , label = \"Validation acc\")\nplt.legend()\nplt.figure()\n\nplt.plot(epochs , loss ,'bo' , label = \"Training loss\")\nplt.plot(epochs , val_loss ,'b' , label = \"Validation loss\")\nplt.legend()\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def smooth_curve(points , factor =.8):\n    somoothed_points = []\n    for point in points:\n        if somoothed_points:\n            previous = somoothed_points[-1]\n            somoothed_points.append(previous *factor + point *(1-factor))\n        else:\n            somoothed_points.append(point)\n    return somoothed_points\n\n\nacc= history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nplt.plot(epochs,\nsmooth_curve(acc), 'bo', label='Smoothed training acc')\nplt.plot(epochs,\nsmooth_curve(val_acc), 'b', label='Smoothed validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\nplt.plot(epochs,\nsmooth_curve(loss), 'bo', label='Smoothed training loss')\nplt.plot(epochs,\nsmooth_curve(val_loss), 'b', label='Smoothed validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T21:24:11.933359Z","iopub.execute_input":"2023-08-31T21:24:11.934403Z","iopub.status.idle":"2023-08-31T21:24:12.610470Z","shell.execute_reply.started":"2023-08-31T21:24:11.934365Z","shell.execute_reply":"2023-08-31T21:24:12.609460Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_directory(\ntest_dir,\ntarget_size=(150, 150),\nbatch_size=20,\nclass_mode='binary')\ntest_loss, test_acc = model.evaluate_generator(test_generator, steps=50)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T22:00:26.993425Z","iopub.execute_input":"2023-08-31T22:00:26.993815Z","iopub.status.idle":"2023-08-31T22:00:27.184206Z","shell.execute_reply.started":"2023-08-31T22:00:26.993774Z","shell.execute_reply":"2023-08-31T22:00:27.182151Z"},"trusted":true},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}