{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Having to train an image-classification model using very little data is a common situation, which you’ll likely encounter in practice if you ever do computer vision in a\nprofessional context. A “few” samples can mean anywhere from a few hundred to a\nfew tens of thousands of images","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\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":"2024-07-15T15:28:47.19441Z","iopub.execute_input":"2024-07-15T15:28:47.195112Z","iopub.status.idle":"2024-07-15T15:28:47.211083Z","shell.execute_reply.started":"2024-07-15T15:28:47.195079Z","shell.execute_reply":"2024-07-15T15:28:47.210149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1_importing libraries and data","metadata":{}},{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport shutil\nimport warnings\n\nwarnings.filterwarnings('ignore')\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers , models ,optimizers ","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:29:08.442013Z","iopub.execute_input":"2024-07-15T15:29:08.442352Z","iopub.status.idle":"2024-07-15T15:29:20.375637Z","shell.execute_reply.started":"2024-07-15T15:29:08.442323Z","shell.execute_reply":"2024-07-15T15:29:20.374651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/test1.zip\n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:29:20.37757Z","iopub.execute_input":"2024-07-15T15:29:20.378197Z","iopub.status.idle":"2024-07-15T15:29:27.07872Z","shell.execute_reply.started":"2024-07-15T15:29:20.378164Z","shell.execute_reply":"2024-07-15T15:29:27.07745Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip\n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:29:27.080522Z","iopub.execute_input":"2024-07-15T15:29:27.080937Z","iopub.status.idle":"2024-07-15T15:29:48.134466Z","shell.execute_reply.started":"2024-07-15T15:29:27.080898Z","shell.execute_reply":"2024-07-15T15:29:48.133322Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path , dirs , files = next(os.walk('/kaggle/working/train'))\n\nprint(len(files))\n#the folder where we store out data\nbase_dir = '/kaggle/working/dogs_vs_cats_small'\n\nos.mkdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:29:48.13734Z","iopub.execute_input":"2024-07-15T15:29:48.138014Z","iopub.status.idle":"2024-07-15T15:29:48.173048Z","shell.execute_reply.started":"2024-07-15T15:29:48.137966Z","shell.execute_reply":"2024-07-15T15:29:48.172079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/working/dogs_vs_cats_small'\n\ntrain_dir = os.path.join(base_dir , 'train')\nos.mkdir(train_dir)\n\nval_dir = os.path.join(base_dir , 'vall')\nos.mkdir(val_dir)\n\ntest_dir = os.path.join(base_dir , 'test')\nos.mkdir(test_dir)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:29:48.174079Z","iopub.execute_input":"2024-07-15T15:29:48.174355Z","iopub.status.idle":"2024-07-15T15:29:48.179857Z","shell.execute_reply.started":"2024-07-15T15:29:48.174331Z","shell.execute_reply":"2024-07-15T15:29:48.178999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cats_dir = os.path.join(train_dir , 'cats')\nos.mkdir(train_cats_dir)\ntrain_dogs_dir = os.path.join(train_dir , 'dogs')\nos.mkdir(train_dogs_dir)\n\nval_cats_dir = os.path.join(val_dir , 'cats')\nos.mkdir(val_cats_dir)\nval_dogs_dir = os.path.join(val_dir , 'dogs')\nos.mkdir(val_dogs_dir)\n\n\ntest_cats_dir = os.path.join(test_dir , 'cats')\nos.mkdir(test_cats_dir)\ntest_dogs_dir = os.path.join(test_dir , 'dogs')\nos.mkdir(test_dogs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:29:51.901238Z","iopub.execute_input":"2024-07-15T15:29:51.902177Z","iopub.status.idle":"2024-07-15T15:29:51.911905Z","shell.execute_reply.started":"2024-07-15T15:29:51.902127Z","shell.execute_reply":"2024-07-15T15:29:51.910453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(base_dir))\nprint(os.listdir(train_dir))\nprint(os.listdir(val_dir))\nprint(os.listdir(test_dir))\n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:29:59.231966Z","iopub.execute_input":"2024-07-15T15:29:59.232338Z","iopub.status.idle":"2024-07-15T15:29:59.2383Z","shell.execute_reply.started":"2024-07-15T15:29:59.232305Z","shell.execute_reply":"2024-07-15T15:29:59.237499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#copying tha data to the new folders\noriginal_dataset_dir = '/kaggle/working/train'\n\n#fnames = ['cat.{}.jpg'.format(i) for i in range (1000)]\nfnames = ['cat.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames :\n    src = os.path.join(original_dataset_dir , fname)\n    dst = os.path.join(train_cats_dir ,fname)\n    shutil.copyfile(src ,dst)\n    \nfnames = ['dog.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames :\n    src = os.path.join(original_dataset_dir , fname)\n    dst = os.path.join(train_dogs_dir ,fname)\n    shutil.copyfile(src ,dst)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:30:02.231072Z","iopub.execute_input":"2024-07-15T15:30:02.231779Z","iopub.status.idle":"2024-07-15T15:30:02.455319Z","shell.execute_reply.started":"2024-07-15T15:30:02.231747Z","shell.execute_reply":"2024-07-15T15:30:02.454528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = ['cat.{}.jpg'.format(i) for i in range(1000 , 1500)]\nfor fname in fnames :\n    src = os.path.join(original_dataset_dir , fname)\n    dst = os.path.join(val_cats_dir ,fname)\n    shutil.copyfile(src ,dst)\n    \nfnames = ['dog.{}.jpg'.format(i) for i in range(1000 , 1500)]\nfor fname in fnames :\n    src = os.path.join(original_dataset_dir , fname)\n    dst = os.path.join(val_dogs_dir ,fname)\n    shutil.copyfile(src ,dst)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:30:09.02618Z","iopub.execute_input":"2024-07-15T15:30:09.02689Z","iopub.status.idle":"2024-07-15T15:30:09.175296Z","shell.execute_reply.started":"2024-07-15T15:30:09.026859Z","shell.execute_reply":"2024-07-15T15:30:09.174531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = ['cat.{}.jpg'.format(i) for i in range(1500 , 2000)]\nfor fname in fnames :\n    src = os.path.join(original_dataset_dir , fname)\n    dst = os.path.join(test_cats_dir ,fname)\n    shutil.copyfile(src ,dst)\n    \nfnames = ['dog.{}.jpg'.format(i) for i in range(1500 , 2000)]\nfor fname in fnames :\n    src = os.path.join(original_dataset_dir , fname)\n    dst = os.path.join(test_dogs_dir ,fname)\n    shutil.copyfile(src ,dst)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:30:13.311841Z","iopub.execute_input":"2024-07-15T15:30:13.312654Z","iopub.status.idle":"2024-07-15T15:30:13.423199Z","shell.execute_reply.started":"2024-07-15T15:30:13.312622Z","shell.execute_reply":"2024-07-15T15:30:13.422443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total cats trainning images is :: ' , len(os.listdir(train_cats_dir)))\nprint('total dogs trainning images is :: ' , len(os.listdir(train_dogs_dir)))\n\nprint('total cats validition images is :: ' , len(os.listdir(val_cats_dir)))\nprint('total dogs validition images is :: ' , len(os.listdir(val_dogs_dir)))\n\nprint('total cats testing images is :: ' , len(os.listdir(test_cats_dir)))\nprint('total dogs testing images is :: ' , len(os.listdir(test_dogs_dir)))","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:30:15.047907Z","iopub.execute_input":"2024-07-15T15:30:15.048269Z","iopub.status.idle":"2024-07-15T15:30:15.058078Z","shell.execute_reply.started":"2024-07-15T15:30:15.048232Z","shell.execute_reply":"2024-07-15T15:30:15.057231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Instantiating a small convnet for dogs vs. cats classification","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32 , (3 , 3),activation = 'relu' , input_shape = (150 , 150 , 3)) )\n\nmodel.add(layers.MaxPool2D(2,2))\n\nmodel.add(layers.Conv2D(64 , (3 , 3),activation = 'relu') )\nmodel.add(layers.MaxPool2D(2,2))\n\n\nmodel.add(layers.Conv2D(128 , (3 , 3),activation = 'relu') )\nmodel.add(layers.MaxPool2D(2,2))\n\nmodel.add(layers.Conv2D(128 , (3 , 3),activation = 'relu') )\nmodel.add(layers.MaxPool2D(2,2))\n\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512 , activation = 'relu'))\nmodel.add(layers.Dense(1, activation = 'sigmoid'))\n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:30:18.562651Z","iopub.execute_input":"2024-07-15T15:30:18.56362Z","iopub.status.idle":"2024-07-15T15:30:19.313896Z","shell.execute_reply.started":"2024-07-15T15:30:18.563577Z","shell.execute_reply":"2024-07-15T15:30:19.31312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:04:54.536214Z","iopub.execute_input":"2024-07-11T23:04:54.536508Z","iopub.status.idle":"2024-07-11T23:04:54.564506Z","shell.execute_reply.started":"2024-07-11T23:04:54.536482Z","shell.execute_reply":"2024-07-11T23:04:54.563676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss = 'binary_crossentropy' ,\n    optimizer=optimizers.RMSprop(),\n    metrics = ['acc']\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:04:54.565616Z","iopub.execute_input":"2024-07-11T23:04:54.566333Z","iopub.status.idle":"2024-07-11T23:04:54.579536Z","shell.execute_reply.started":"2024-07-11T23:04:54.566296Z","shell.execute_reply":"2024-07-11T23:04:54.578746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ndg = ImageDataGenerator(rescale=1./255)\n\nval_generated = dg.flow_from_directory(\n    val_dir ,\n    target_size= (150 ,150 ) ,\n    batch_size= 20 ,\n    class_mode= 'binary'\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:04:54.670633Z","iopub.execute_input":"2024-07-11T23:04:54.670958Z","iopub.status.idle":"2024-07-11T23:04:54.706923Z","shell.execute_reply.started":"2024-07-11T23:04:54.67092Z","shell.execute_reply":"2024-07-11T23:04:54.706043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_generated:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:04:54.708059Z","iopub.execute_input":"2024-07-11T23:04:54.70842Z","iopub.status.idle":"2024-07-11T23:04:54.753829Z","shell.execute_reply.started":"2024-07-11T23:04:54.708385Z","shell.execute_reply":"2024-07-11T23:04:54.75289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generated ,\n    epochs =10 ,\n    #steps_per_epoch =100 ,\n  \n    validation_data=val_generated ,\n    #validation_steps=50\n\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:04:54.754875Z","iopub.execute_input":"2024-07-11T23:04:54.755159Z","iopub.status.idle":"2024-07-11T23:05:44.607767Z","shell.execute_reply.started":"2024-07-11T23:04:54.755134Z","shell.execute_reply":"2024-07-11T23:05:44.606777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"cats_and_dogs_small_1.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:07:50.476999Z","iopub.execute_input":"2024-07-11T23:07:50.477415Z","iopub.status.idle":"2024-07-11T23:07:50.556016Z","shell.execute_reply.started":"2024-07-11T23:07:50.477381Z","shell.execute_reply":"2024-07-11T23:07:50.555186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:09:22.196698Z","iopub.execute_input":"2024-07-11T23:09:22.197537Z","iopub.status.idle":"2024-07-11T23:09:22.201968Z","shell.execute_reply.started":"2024-07-11T23:09:22.197492Z","shell.execute_reply":"2024-07-11T23:09:22.200964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nloss_val = history.history['val_loss']\n\nepochs = range(1 ,len(acc) +1)\n\nplt.plot(epochs , acc , 'bo' , label = \"training acc\")\nplt.plot(epochs , val_acc ,  'b' ,label = \"validation acc\")\nplt.title(\"training and validtion accuracy \")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:16:35.763636Z","iopub.execute_input":"2024-07-11T23:16:35.763997Z","iopub.status.idle":"2024-07-11T23:16:36.0609Z","shell.execute_reply.started":"2024-07-11T23:16:35.763967Z","shell.execute_reply":"2024-07-11T23:16:36.059978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Using data augmentation","metadata":{}},{"cell_type":"code","source":"data_gen = ImageDataGenerator(\n    rotation_range= 40 ,\n    width_shift_range= .2 ,\n    height_shift_range= .2 ,\n    zoom_range= .2 ,\n    shear_range=.2 ,\n        horizontal_flip= True ,\n    fill_mode= 'nearest'\n    \n)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:20:14.095869Z","iopub.execute_input":"2024-07-11T23:20:14.096593Z","iopub.status.idle":"2024-07-11T23:20:14.101417Z","shell.execute_reply.started":"2024-07-11T23:20:14.096553Z","shell.execute_reply":"2024-07-11T23:20:14.100396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image \nfnames = [os.path.join(train_cats_dir , fname) for fname in os.listdir(train_cats_dir)]\nimage_path = fnames[3]\nimg  = image.load_img(image_path , target_size = (150 ,150 ))","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:24:26.006766Z","iopub.execute_input":"2024-07-11T23:24:26.007158Z","iopub.status.idle":"2024-07-11T23:24:26.016569Z","shell.execute_reply.started":"2024-07-11T23:24:26.007123Z","shell.execute_reply":"2024-07-11T23:24:26.015726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = image.img_to_array(img)\nx = x.reshape((1,) +x.shape)\n\ni = 0 \nfor batch in data_gen.flow( x , batch_size = 1) :\n    plt.figure(i)\n    imgplot = plt.imshow(image.array_to_img(batch[0]))\n    i += 1\n    if i % 4 == 0:\n        break\n","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:27:51.765141Z","iopub.execute_input":"2024-07-11T23:27:51.765521Z","iopub.status.idle":"2024-07-11T23:27:53.137768Z","shell.execute_reply.started":"2024-07-11T23:27:51.76549Z","shell.execute_reply":"2024-07-11T23:27:53.136847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = 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())\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))\nmodel.compile(loss='binary_crossentropy',\noptimizer=optimizers.RMSprop(),\nmetrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:29:37.366439Z","iopub.execute_input":"2024-07-11T23:29:37.366898Z","iopub.status.idle":"2024-07-11T23:29:37.488141Z","shell.execute_reply.started":"2024-07-11T23:29:37.366857Z","shell.execute_reply":"2024-07-11T23:29:37.487402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = ImageDataGenerator(\n    rescale= 1./255 ,\n    rotation_range= 40 ,\n    width_shift_range= .2 ,\n    height_shift_range= .2 ,\n    zoom_range= .2 ,\n    shear_range=.2 ,\n        horizontal_flip= True ,\n    \n)\ntest_gen = ImageDataGenerator(\n    rescale= 1./255\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:31:36.916882Z","iopub.execute_input":"2024-07-11T23:31:36.917266Z","iopub.status.idle":"2024-07-11T23:31:36.922526Z","shell.execute_reply.started":"2024-07-11T23:31:36.917233Z","shell.execute_reply":"2024-07-11T23:31:36.921598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen_data = train_gen.flow_from_directory(\n    train_dir ,\n    target_size= (150 ,150 ) ,\n    batch_size= 32 ,\n    class_mode= 'binary'\n)\n\nval_gen_data = test_gen.flow_from_directory(\n    val_dir ,\n    target_size= (150 ,150 ) ,\n    batch_size= 32 ,\n    class_mode= 'binary'\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:33:14.997206Z","iopub.execute_input":"2024-07-11T23:33:14.997552Z","iopub.status.idle":"2024-07-11T23:33:15.090197Z","shell.execute_reply.started":"2024-07-11T23:33:14.997525Z","shell.execute_reply":"2024-07-11T23:33:15.089426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" history = model.fit(\ntrain_gen_data,\nsteps_per_epoch=100,\nepochs=100,\nvalidation_data=val_gen_data,\nvalidation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:35:24.547444Z","iopub.execute_input":"2024-07-11T23:35:24.548133Z","iopub.status.idle":"2024-07-11T23:57:17.50285Z","shell.execute_reply.started":"2024-07-11T23:35:24.548096Z","shell.execute_reply":"2024-07-11T23:57:17.502031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = 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)\n\nplt.plot(epochs , acc , 'bo' , label = \"training acc\")\nplt.plot(epochs , val_acc ,  'b' ,label = \"validation acc\")\nplt.title(\"training and validtion accuracy \")\nplt.legend()\nplt.figure()\n\nplt.plot(epochs , loss , 'bo' , label = \"training loss\")\nplt.plot(epochs , val_loss ,  'b' ,label = \"validation loss\")\nplt.title(\"training and validtion loss \")","metadata":{"execution":{"iopub.status.busy":"2024-07-11T23:59:27.383469Z","iopub.execute_input":"2024-07-11T23:59:27.383832Z","iopub.status.idle":"2024-07-11T23:59:27.985386Z","shell.execute_reply.started":"2024-07-11T23:59:27.383801Z","shell.execute_reply":"2024-07-11T23:59:27.98444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cats_and_dogs_small_2.h5')","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:13:58.4136Z","iopub.execute_input":"2024-07-12T00:13:58.413985Z","iopub.status.idle":"2024-07-12T00:13:58.478434Z","shell.execute_reply.started":"2024-07-12T00:13:58.413951Z","shell.execute_reply":"2024-07-12T00:13:58.477506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_datagen.flow_from_directory(\n    test_dir , \n    target_size=(150, 150),  # same as the target_size used during training\n    batch_size=32,\n    class_mode='binary' ,  # No labels for test data\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:15:53.804669Z","iopub.execute_input":"2024-07-12T00:15:53.805035Z","iopub.status.idle":"2024-07-12T00:15:53.84282Z","shell.execute_reply.started":"2024-07-12T00:15:53.805008Z","shell.execute_reply":"2024-07-12T00:15:53.8421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_generator)\npredictions","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:16:41.784715Z","iopub.execute_input":"2024-07-12T00:16:41.785959Z","iopub.status.idle":"2024-07-12T00:16:43.977566Z","shell.execute_reply.started":"2024-07-12T00:16:41.785912Z","shell.execute_reply":"2024-07-12T00:16:43.976648Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = np.argmax(predictions , axis =1)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:19:54.999505Z","iopub.execute_input":"2024-07-12T00:19:55.000161Z","iopub.status.idle":"2024-07-12T00:19:55.004316Z","shell.execute_reply.started":"2024-07-12T00:19:55.000126Z","shell.execute_reply":"2024-07-12T00:19:55.003395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_indices = {v: k for k, v in train_gen_data.class_indices.items()}\npredicted_labels = [class_indices[idx] for idx in classes]","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:20:50.96584Z","iopub.execute_input":"2024-07-12T00:20:50.966215Z","iopub.status.idle":"2024-07-12T00:20:50.97381Z","shell.execute_reply.started":"2024-07-12T00:20:50.966183Z","shell.execute_reply":"2024-07-12T00:20:50.972894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = test_generator.filenames\nresults = pd.DataFrame({\"Filename\": filenames, \"Prediction\": predicted_labels})\nresults.to_csv(\"test_predictions.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:21:16.47067Z","iopub.execute_input":"2024-07-12T00:21:16.471494Z","iopub.status.idle":"2024-07-12T00:21:16.483466Z","shell.execute_reply.started":"2024-07-12T00:21:16.471456Z","shell.execute_reply":"2024-07-12T00:21:16.482728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using a pretrained convnet","metadata":{}},{"cell_type":"code","source":"from keras.applications import VGG19 \n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:36:23.103409Z","iopub.execute_input":"2024-07-15T15:36:23.103779Z","iopub.status.idle":"2024-07-15T15:36:23.108955Z","shell.execute_reply.started":"2024-07-15T15:36:23.103749Z","shell.execute_reply":"2024-07-15T15:36:23.108021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv = VGG19(\n   weights='imagenet',\ninclude_top=False,\ninput_shape=(150, 150, 3)\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:36:25.68386Z","iopub.execute_input":"2024-07-15T15:36:25.684576Z","iopub.status.idle":"2024-07-15T15:36:30.985257Z","shell.execute_reply.started":"2024-07-15T15:36:25.684535Z","shell.execute_reply":"2024-07-15T15:36:30.9844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:36:32.782874Z","iopub.execute_input":"2024-07-15T15:36:32.783234Z","iopub.status.idle":"2024-07-15T15:36:32.8206Z","shell.execute_reply.started":"2024-07-15T15:36:32.783205Z","shell.execute_reply":"2024-07-15T15:36:32.819734Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# features extraction","metadata":{}},{"cell_type":"code","source":"base_dir = '/kaggle/working/dogs_vs_cats_small'\n\ntrain_dir = os.path.join(base_dir , 'train')\n\nval_dir = os.path.join(base_dir , 'vall')\n\n\ntest_dir = os.path.join(base_dir , 'test')\n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:30:56.581145Z","iopub.execute_input":"2024-07-15T15:30:56.581541Z","iopub.status.idle":"2024-07-15T15:30:56.586379Z","shell.execute_reply.started":"2024-07-15T15:30:56.581498Z","shell.execute_reply":"2024-07-15T15:30:56.585565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(rescale=1./255)\nbatch_size = 20","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:35:59.425036Z","iopub.execute_input":"2024-07-15T15:35:59.425973Z","iopub.status.idle":"2024-07-15T15:35:59.430982Z","shell.execute_reply.started":"2024-07-15T15:35:59.425931Z","shell.execute_reply":"2024-07-15T15:35:59.42997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_features(directory, sample_count):\n    features = np.zeros(shape=(sample_count, 4, 4, 512))\n    labels = np.zeros(shape=(sample_count))\n    generator = datagen.flow_from_directory(\n    directory,\n    target_size=(150, 150),\n    batch_size=batch_size,\n    class_mode='binary')\n    i=0\n    for inputs_batch, labels_batch in generator:\n        features_batch = conv.predict(inputs_batch)\n        features[i * batch_size : (i + 1) * batch_size] = features_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\n\ntrain_features, train_labels = extract_features(train_dir, 2000)\nvalidation_features, validation_labels = extract_features(val_dir, 1000)\ntest_features, test_labels = extract_features(test_dir, 1000)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:36:41.167656Z","iopub.execute_input":"2024-07-15T15:36:41.168016Z","iopub.status.idle":"2024-07-15T15:37:12.488603Z","shell.execute_reply.started":"2024-07-15T15:36:41.167987Z","shell.execute_reply":"2024-07-15T15:37:12.487786Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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))","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:37:47.195023Z","iopub.execute_input":"2024-07-15T15:37:47.195874Z","iopub.status.idle":"2024-07-15T15:37:47.201239Z","shell.execute_reply.started":"2024-07-15T15:37:47.19583Z","shell.execute_reply":"2024-07-15T15:37:47.200255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_features.shape, train_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:41:12.023356Z","iopub.execute_input":"2024-07-15T15:41:12.024102Z","iopub.status.idle":"2024-07-15T15:41:12.029953Z","shell.execute_reply.started":"2024-07-15T15:41:12.024071Z","shell.execute_reply":"2024-07-15T15:41:12.029041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Dense(256, activation='relu', input_dim=4 * 4 * 512))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(1, activation='sigmoid'))\nmodel.compile(optimizer=optimizers.RMSprop(),\nloss='binary_crossentropy',\nmetrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:52:57.490864Z","iopub.execute_input":"2024-07-12T00:52:57.49158Z","iopub.status.idle":"2024-07-12T00:52:57.526753Z","shell.execute_reply.started":"2024-07-12T00:52:57.491547Z","shell.execute_reply":"2024-07-12T00:52:57.526025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_features, train_labels,\nepochs=30,\nbatch_size=20,\nvalidation_data=(validation_features, validation_labels))","metadata":{"execution":{"iopub.status.busy":"2024-07-12T00:53:06.141535Z","iopub.execute_input":"2024-07-12T00:53:06.142441Z","iopub.status.idle":"2024-07-12T00:53:17.23138Z","shell.execute_reply.started":"2024-07-12T00:53:06.142403Z","shell.execute_reply":"2024-07-12T00:53:17.230416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(conv)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:42:03.076075Z","iopub.execute_input":"2024-07-15T15:42:03.076472Z","iopub.status.idle":"2024-07-15T15:42:03.085373Z","shell.execute_reply.started":"2024-07-15T15:42:03.076438Z","shell.execute_reply":"2024-07-15T15:42:03.084518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:42:30.987332Z","iopub.execute_input":"2024-07-15T15:42:30.987717Z","iopub.status.idle":"2024-07-15T15:42:31.007626Z","shell.execute_reply.started":"2024-07-15T15:42:30.987687Z","shell.execute_reply":"2024-07-15T15:42:31.006704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=40,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_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(\n    val_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')\n","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:43:47.623775Z","iopub.execute_input":"2024-07-15T15:43:47.624189Z","iopub.status.idle":"2024-07-15T15:43:47.72203Z","shell.execute_reply.started":"2024-07-15T15:43:47.624157Z","shell.execute_reply":"2024-07-15T15:43:47.721034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\noptimizer = tf.keras.optimizers.RMSprop(learning_rate=1e-5),\nmetrics=['acc'])\nhistory = model.fit(\ntrain_generator,\nsteps_per_epoch=100,\nepochs=30,\nvalidation_data=validation_generator,\n#validation_steps=50\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:48:43.674562Z","iopub.execute_input":"2024-07-15T15:48:43.67532Z","iopub.status.idle":"2024-07-15T15:52:58.260597Z","shell.execute_reply.started":"2024-07-15T15:48:43.67529Z","shell.execute_reply":"2024-07-15T15:52:58.259653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fine-tune","metadata":{}},{"cell_type":"code","source":"conv.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:53:38.293268Z","iopub.execute_input":"2024-07-15T15:53:38.294106Z","iopub.status.idle":"2024-07-15T15:53:38.33298Z","shell.execute_reply.started":"2024-07-15T15:53:38.294063Z","shell.execute_reply":"2024-07-15T15:53:38.331971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Freezing all layers up to a specific one","metadata":{}},{"cell_type":"code","source":"conv.trainable = True\nset_trainable = False \nfor layer in conv.layers :\n    if layer == 'block1_conv1' :\n        set_trainable = True\n    if set_trainable :\n        layer.trainable = True\n    else :\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:57:48.258987Z","iopub.execute_input":"2024-07-15T15:57:48.259835Z","iopub.status.idle":"2024-07-15T15:57:48.265347Z","shell.execute_reply.started":"2024-07-15T15:57:48.259802Z","shell.execute_reply":"2024-07-15T15:57:48.26426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\noptimizer=tf.keras.optimizers.RMSprop(learning_rate =1e-5),\nmetrics=['acc'])\nhistory = model.fit(\ntrain_generator,\nsteps_per_epoch=100,\nepochs=100,\nvalidation_data=validation_generator,\n#validation_steps=50\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T16:00:22.521229Z","iopub.execute_input":"2024-07-15T16:00:22.521635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}