{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"code","source":"\nimport numpy as np\nimport pandas as pd\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import load_img\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:46:34.024347Z","iopub.execute_input":"2023-08-31T06:46:34.025044Z","iopub.status.idle":"2023-08-31T06:46:44.062393Z","shell.execute_reply.started":"2023-08-31T06:46:34.025011Z","shell.execute_reply":"2023-08-31T06:46:44.061244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import zipfile\n\n# # Path to the zipped dataset file\n# zip_file_path = '/kaggle/input/dogs-vs-cats/train.zip'\n\n# # Path to the directory where the dataset is extracted\n# extraction_path = '/kaggle/working/'\n\n# # Extract the dataset\n# with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n#     zip_ref.extractall(extraction_path)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:46:44.064375Z","iopub.execute_input":"2023-08-31T06:46:44.065511Z","iopub.status.idle":"2023-08-31T06:46:55.363681Z","shell.execute_reply.started":"2023-08-31T06:46:44.065469Z","shell.execute_reply":"2023-08-31T06:46:55.362603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import shutil\n# import random\n\n# # Paths\n# original_dataset_dir = '/kaggle/working/train'\n# new_base_dir = '/kaggle/working/'\n# destination_dir = os.path.join(new_base_dir, 'new_dataset')\n\n# # Create new directories\n# os.makedirs(os.path.join(destination_dir, 'cats'), exist_ok=True)\n# os.makedirs(os.path.join(destination_dir, 'dogs'), exist_ok=True)\n\n# # Function to move images to destination\n# def move_images(source_dir, destination_dir, file_list):\n#     for file_name in file_list:\n#         source_path = os.path.join(source_dir, file_name)\n#         destination_path = os.path.join(destination_dir, file_name)\n#         shutil.copyfile(source_path, destination_path)\n\n# # List of image filenames\n# cat_image_files = [filename for filename in os.listdir(original_dataset_dir) if 'cat' in filename]\n# dog_image_files = [filename for filename in os.listdir(original_dataset_dir) if 'dog' in filename]\n\n# # Shuffle the lists\n# random.shuffle(cat_image_files)\n# random.shuffle(dog_image_files)\n\n# # Move images to appropriate directories\n# move_images(original_dataset_dir, os.path.join(destination_dir, 'cats'), cat_image_files)\n# move_images(original_dataset_dir, os.path.join(destination_dir, 'dogs'), dog_image_files)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:46:55.365799Z","iopub.execute_input":"2023-08-31T06:46:55.366171Z","iopub.status.idle":"2023-08-31T06:46:59.442602Z","shell.execute_reply.started":"2023-08-31T06:46:55.366137Z","shell.execute_reply":"2023-08-31T06:46:59.441361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# source_folder = \"/kaggle/working/train\"  # Replace with your dataset path\n# output_folder = \"/kaggle/working//reduced_train(70)\"  # Specify where to save the reduced dataset\n\n# cats_source = os.path.join(source_folder, \"cats\")\n# dogs_source = os.path.join(source_folder, \"dogs\")\n\n# output_cats_folder = os.path.join(output_folder, \"cats\")\n# output_dogs_folder = os.path.join(output_folder, \"dogs\")\n\n# # Create output folders\n# os.makedirs(output_cats_folder, exist_ok=True)\n# os.makedirs(output_dogs_folder, exist_ok=True)\n\n# # Fraction of images to keep\n# fraction_to_keep = 0.3\n\n# def select_and_copy_images(source, output_folder, label):\n#     all_images = os.listdir(source)\n#     num_to_keep = int(len(all_images) * fraction_to_keep)\n#     selected_images = random.sample(all_images, num_to_keep)\n    \n#     for image in selected_images:\n#         image_path = os.path.join(source, image)\n#         new_filename = f\"{label}_{image}\"  # Append label to the filename\n#         new_image_path = os.path.join(output_folder, new_filename)\n#         shutil.copy(image_path, new_image_path)\n\n# # Select and copy images while assigning labels\n# select_and_copy_images(cats_source, output_cats_folder, \"cat\")\n# select_and_copy_images(dogs_source, output_dogs_folder, \"dog\")\n\n# # Create labels DataFrame\n# data = []\n# for folder in [output_cats_folder, output_dogs_folder]:\n#     label = folder.split(\"_\")[1]\n#     for filename in os.listdir(folder):\n#         data.append((filename, label))\n\n# df = pd.DataFrame(data, columns=['filename', 'label'])\n\n# # Save labels to a CSV file\n# df.to_csv(os.path.join(output_folder, 'dataset_labels.csv'), index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:15:08.383015Z","iopub.execute_input":"2023-08-29T09:15:08.383579Z","iopub.status.idle":"2023-08-29T09:15:08.394367Z","shell.execute_reply.started":"2023-08-29T09:15:08.383546Z","shell.execute_reply":"2023-08-29T09:15:08.393397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasetdir = '/kaggle/working/new_dataset'\nos.chdir(datasetdir)\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nfrom tensorflow import keras\n# shortcut to the ImageDataGenerator class\nImageDataGenerator = keras.preprocessing.image.ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:13.364248Z","iopub.execute_input":"2023-08-31T06:47:13.365326Z","iopub.status.idle":"2023-08-31T06:47:13.371851Z","shell.execute_reply.started":"2023-08-31T06:47:13.36527Z","shell.execute_reply":"2023-08-31T06:47:13.370559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_dog_ids = [5604, 10161, \n               10190, 10237, 10401, 10797, 11186]\n\nbad_cat_ids = [2939, 3216, 4688, 4833, 5418, 6215, 7377, \n               8456, 8470, 11565, 12272]","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:16.447456Z","iopub.execute_input":"2023-08-31T06:47:16.447811Z","iopub.status.idle":"2023-08-31T06:47:16.45401Z","shell.execute_reply.started":"2023-08-31T06:47:16.447783Z","shell.execute_reply":"2023-08-31T06:47:16.452682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import glob\n# import re\n# import shutil\n\n# # matches any string with the substring \".<digits>.\"\n# # such as dog.666.jpg\n# pattern = re.compile(r'.*\\.(\\d+)\\..*')\n\n# def trash_path(dirname):\n#     '''return the path of the Trash directory, \n#     where the bad dog and bad cat images will be moved.\n#     Note that the Trash directory should not be within the dogs/ \n#     or the cats/ directory, or Keras will still find these pictures.\n#     '''\n#     return os.path.join('../Trash', dirname)\n\n# def cleanup(ids, dirname): \n#   '''move away images with these ids in dirname\n#   '''\n#   os.chdir(datasetdir)\n#   # keep track of current directory\n#   oldpwd = os.getcwd()\n#   # go to either cats/ or dogs/\n#   os.chdir(dirname)\n#   # create the trash directory. \n#   # if it exists, it is first removed\n#   trash = trash_path(dirname)\n#   if os.path.isdir(trash):\n#     shutil.rmtree(trash)\n#   os.makedirs(trash, exist_ok=True)\n#   # loop on all cat or dog files\n#   fnames = os.listdir()\n#   for fname in fnames:\n#     m = pattern.match(fname)\n#     if m: \n#       # extract the id\n#       the_id = int(m.group(1))\n#       if the_id in ids:\n#         # this id is in the list of ids to be trashed\n#         print('moving to {}: {}'.format(trash, fname))\n#         shutil.move(fname, trash)\n#   # going back to root directory\n#   os.chdir(oldpwd)\n  \n# def restore(dirname):\n#   '''restores files in the trash\n#   I will need this to restore this tutorial to initial state for you\n#   and you might need it if you want to try training the network\n#   without the cleaning of bad images\n#   '''\n#   os.chdir(datasetdir)\n#   oldpwd = os.getcwd()\n#   os.chdir(dirname)\n#   trash = trash_path(dirname)\n#   print(trash)\n#   for fname in os.listdir(trash):\n#     fname = os.path.join(trash,fname)\n#     print('restoring', fname)\n#     print(os.getcwd())\n#     shutil.move(fname, os.getcwd())\n#   os.chdir(oldpwd)\n \n# cleanup(bad_cat_ids,'cats')\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:20.628182Z","iopub.execute_input":"2023-08-31T06:47:20.628573Z","iopub.status.idle":"2023-08-31T06:47:20.675833Z","shell.execute_reply.started":"2023-08-31T06:47:20.62854Z","shell.execute_reply":"2023-08-31T06:47:20.674694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cleanup(bad_dog_ids, 'dogs')","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:26.499132Z","iopub.execute_input":"2023-08-31T06:47:26.499574Z","iopub.status.idle":"2023-08-31T06:47:26.535594Z","shell.execute_reply.started":"2023-08-31T06:47:26.499541Z","shell.execute_reply":"2023-08-31T06:47:26.534476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1,2,1)\nplt.imshow(img.imread('cats/cat.0.jpg'))\nplt.subplot(1,2,2)\nplt.imshow(img.imread('dogs/dog.0.jpg'))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:31.625357Z","iopub.execute_input":"2023-08-31T06:47:31.625788Z","iopub.status.idle":"2023-08-31T06:47:32.195355Z","shell.execute_reply.started":"2023-08-31T06:47:31.625755Z","shell.execute_reply":"2023-08-31T06:47:32.194286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = []\nfor i in range(10):\n  im = img.imread('cats/cat.{}.jpg'.format(i))\n  images.append(im)\n  print('image shape', im.shape, 'maximum color level', im.max())","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:35.033354Z","iopub.execute_input":"2023-08-31T06:47:35.033917Z","iopub.status.idle":"2023-08-31T06:47:35.071003Z","shell.execute_reply.started":"2023-08-31T06:47:35.033873Z","shell.execute_reply":"2023-08-31T06:47:35.069866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen = ImageDataGenerator()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:40.425953Z","iopub.execute_input":"2023-08-31T06:47:40.426361Z","iopub.status.idle":"2023-08-31T06:47:40.431785Z","shell.execute_reply.started":"2023-08-31T06:47:40.426327Z","shell.execute_reply":"2023-08-31T06:47:40.430459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"iterator = gen.flow_from_directory(\n    os.getcwd(), \n    target_size=(150,150), \n    classes=('cats','dogs')\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:42.527653Z","iopub.execute_input":"2023-08-31T06:47:42.528015Z","iopub.status.idle":"2023-08-31T06:47:43.245912Z","shell.execute_reply.started":"2023-08-31T06:47:42.527986Z","shell.execute_reply":"2023-08-31T06:47:43.244719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we can guess that the iterator has a next function, \n# because all python iterators have one. \nbatch = iterator.next()\nlen(batch)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:44.217344Z","iopub.execute_input":"2023-08-31T06:47:44.218067Z","iopub.status.idle":"2023-08-31T06:47:44.274178Z","shell.execute_reply.started":"2023-08-31T06:47:44.218033Z","shell.execute_reply":"2023-08-31T06:47:44.273091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(batch[0]))\nprint(type(batch[1]))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:46.508146Z","iopub.execute_input":"2023-08-31T06:47:46.508565Z","iopub.status.idle":"2023-08-31T06:47:46.515712Z","shell.execute_reply.started":"2023-08-31T06:47:46.508533Z","shell.execute_reply":"2023-08-31T06:47:46.514302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(batch[0].shape)\nprint(batch[0].dtype)\nprint(batch[0].max())\nprint(batch[1].shape)\nprint(batch[1].dtype)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T11:53:17.710266Z","iopub.execute_input":"2023-08-29T11:53:17.710997Z","iopub.status.idle":"2023-08-29T11:53:18.335825Z","shell.execute_reply.started":"2023-08-29T11:53:17.710962Z","shell.execute_reply":"2023-08-29T11:53:18.334668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n# we need to cast the image array to integers \n# before plotting as imshow either takes arrays of integers, \n# or arrays of floats normalized to 1. \nplt.imshow(batch[0][0].astype(np.int64))","metadata":{"execution":{"iopub.status.busy":"2023-08-29T11:53:18.337593Z","iopub.execute_input":"2023-08-29T11:53:18.338042Z","iopub.status.idle":"2023-08-29T11:53:18.703791Z","shell.execute_reply.started":"2023-08-29T11:53:18.338006Z","shell.execute_reply":"2023-08-29T11:53:18.702767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch[1][0]","metadata":{"execution":{"iopub.status.busy":"2023-08-29T11:53:18.705103Z","iopub.execute_input":"2023-08-29T11:53:18.705523Z","iopub.status.idle":"2023-08-29T11:53:18.71213Z","shell.execute_reply.started":"2023-08-29T11:53:18.705489Z","shell.execute_reply":"2023-08-29T11:53:18.711184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(batch):\n    imgs = batch[0]\n    labels = batch[1]\n    ncols, nrows = 4,8\n    fig = plt.figure( figsize=(ncols*3, nrows*3), dpi=90)\n    for i, (img,label) in enumerate(zip(imgs,labels)):\n      plt.subplot(nrows, ncols, i+1)\n      plt.imshow(img.astype(np.int64))\n      assert(label[0]+label[1]==1.)\n      categ = 'cat' if label[0]>0.5 else 'dog'\n      plt.title( '{} {}'.format(str(label), categ))\n      plt.axis('off')\nplot_images(iterator.next())","metadata":{"execution":{"iopub.status.busy":"2023-08-29T11:53:18.713665Z","iopub.execute_input":"2023-08-29T11:53:18.714404Z","iopub.status.idle":"2023-08-29T11:53:21.984489Z","shell.execute_reply.started":"2023-08-29T11:53:18.714372Z","shell.execute_reply":"2023-08-29T11:53:21.983189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgdatagen = ImageDataGenerator(\n    rescale = 1/255., \n    horizontal_flip = True, \n    zoom_range = 0.3, \n    rotation_range = 15.,\n    validation_split = 0.1,\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:53.694593Z","iopub.execute_input":"2023-08-31T06:47:53.694971Z","iopub.status.idle":"2023-08-31T06:47:53.700367Z","shell.execute_reply.started":"2023-08-31T06:47:53.694941Z","shell.execute_reply":"2023-08-31T06:47:53.699166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strategy = tf.distribute.MirroredStrategy()\nprint('DEVICES AVAILABLE: {}'.format(strategy.num_replicas_in_sync))","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:15:13.278113Z","iopub.execute_input":"2023-08-29T09:15:13.278619Z","iopub.status.idle":"2023-08-29T09:15:13.293379Z","shell.execute_reply.started":"2023-08-29T09:15:13.278588Z","shell.execute_reply":"2023-08-29T09:15:13.292531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nheight, width = (150,150)\n\ntrain_dataset = imgdatagen.flow_from_directory(\n    os.getcwd(),\n    target_size = (height, width), \n    classes = ('dogs','cats'),\n    batch_size = batch_size,\n    subset = 'training'\n)\n\nval_dataset = imgdatagen.flow_from_directory(\n    os.getcwd(),\n    target_size = (height, width), \n    classes = ('dogs','cats'),\n    batch_size = batch_size,\n    subset = 'validation'\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:56.41829Z","iopub.execute_input":"2023-08-31T06:47:56.418718Z","iopub.status.idle":"2023-08-31T06:47:57.271778Z","shell.execute_reply.started":"2023-08-31T06:47:56.418688Z","shell.execute_reply":"2023-08-31T06:47:57.270772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Flatten\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\n\n\nmodel = keras.models.Sequential()\n\ninitializers = {\n    \n}\nmodel.add( \n    keras.layers.Conv2D(\n        24, 5, input_shape=(150,150,3), \n        activation='relu', \n    )\n)\nmodel.add( keras.layers.MaxPooling2D(2) )\nmodel.add( \n    keras.layers.Conv2D(\n        48, 5, activation='relu', \n    )\n)\nmodel.add( keras.layers.MaxPooling2D(2) )\nmodel.add( \n    keras.layers.Conv2D(\n        96, 5, activation='relu', \n    )\n)\nmodel.add( keras.layers.Flatten() )\nmodel.add( keras.layers.Dropout(0.2) )\n\nmodel.add( keras.layers.Dense(\n    2, activation='softmax',\n    )\n)\n\nmodel.summary()\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer=keras.optimizers.Adamax(lr=0.001),\n              metrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:47:59.240317Z","iopub.execute_input":"2023-08-31T06:47:59.240752Z","iopub.status.idle":"2023-08-31T06:48:02.47036Z","shell.execute_reply.started":"2023-08-31T06:47:59.240723Z","shell.execute_reply":"2023-08-31T06:48:02.469185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset, validation_data = val_dataset,epochs=20,workers=18)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T06:48:05.045068Z","iopub.execute_input":"2023-08-31T06:48:05.045584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel.save('cats_vs_dogs.h5')\nload_model('cats_vs_dogs.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-29T13:01:03.527503Z","iopub.execute_input":"2023-08-29T13:01:03.528084Z","iopub.status.idle":"2023-08-29T13:01:03.685421Z","shell.execute_reply.started":"2023-08-29T13:01:03.528051Z","shell.execute_reply":"2023-08-29T13:01:03.684494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history(history, yrange):\n    '''Plot loss and accuracy as a function of the epoch,\n    for the training and validation datasets.\n    '''\n    acc = history.history['acc']\n    val_acc = history.history['val_acc']\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n\n    # Get number of epochs\n    epochs = range(len(acc))\n\n    # Plot training and validation accuracy per epoch\n    plt.plot(epochs, acc)\n    plt.plot(epochs, val_acc)\n    plt.title('Training and validation accuracy')\n    plt.ylim(yrange)\n    \n    # Plot training and validation loss per epoch\n    plt.figure()\n\n    plt.plot(epochs, loss)\n    plt.plot(epochs, val_loss)\n    plt.title('Training and validation loss')\n    \n    plt.show()\n    \nplot_history(history_augm, (0.65, 1.))","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.300801Z","iopub.status.idle":"2023-08-29T09:16:26.301272Z","shell.execute_reply.started":"2023-08-29T09:16:26.301038Z","shell.execute_reply":"2023-08-29T09:16:26.301061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_fine_tune = model.fit(\n    train_dataset, \n    validation_data=val_dataset,\n    workers=10,\n    epochs=5,  \n)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.303159Z","iopub.status.idle":"2023-08-29T09:16:26.303972Z","shell.execute_reply.started":"2023-08-29T09:16:26.303717Z","shell.execute_reply":"2023-08-29T09:16:26.30374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_fine_tune = model.fit(\n    train_dataset, \n    validation_data=val_dataset,\n    workers=10,\n    epochs=5,  \n)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.305259Z","iopub.status.idle":"2023-08-29T09:16:26.305845Z","shell.execute_reply.started":"2023-08-29T09:16:26.305603Z","shell.execute_reply":"2023-08-29T09:16:26.305625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history_fine_tune, (0.65, 1.))","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.307557Z","iopub.status.idle":"2023-08-29T09:16:26.308062Z","shell.execute_reply.started":"2023-08-29T09:16:26.307818Z","shell.execute_reply":"2023-08-29T09:16:26.307841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('history_fine_tune.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.309263Z","iopub.status.idle":"2023-08-29T09:16:26.309844Z","shell.execute_reply.started":"2023-08-29T09:16:26.309606Z","shell.execute_reply":"2023-08-29T09:16:26.309627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import shutil\n\n# source_folder = \"Desktop/dogs-vs-cats/train\"  \n# cats_dest = os.path.join(source_folder, \"cats\")\n# dogs_dest = os.path.join(source_folder, \"dogs\")\n\n# def separate_images(source, cats_dest, dogs_dest):\n#     if not os.path.exists(cats_dest):\n#         os.makedirs(cats_dest)\n#     if not os.path.exists(dogs_dest):\n#         os.makedirs(dogs_dest)\n\n#     for filename in os.listdir(source):\n#         if filename.startswith(\"cat.\"):\n#             shutil.move(os.path.join(source, filename), os.path.join(cats_dest, filename))\n#         elif filename.startswith(\"dog.\"):\n#             shutil.move(os.path.join(source, filename), os.path.join(dogs_dest, filename))\n\n# separate_images(source_folder, cats_dest, dogs_dest)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.311521Z","iopub.status.idle":"2023-08-29T09:16:26.312117Z","shell.execute_reply.started":"2023-08-29T09:16:26.311871Z","shell.execute_reply":"2023-08-29T09:16:26.311894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model= model.load('history_fine_tune.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.313703Z","iopub.status.idle":"2023-08-29T09:16:26.315258Z","shell.execute_reply.started":"2023-08-29T09:16:26.315012Z","shell.execute_reply":"2023-08-29T09:16:26.315035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_folder = \"Desktop/dogs-vs-cats/train\"  # Replace with your dataset path\noutput_folder = \"Desktop/dogs-vs-cats/reduced_train(70)\"  # Specify where to save the reduced dataset\n\ncats_source = os.path.join(source_folder, \"cats\")\ndogs_source = os.path.join(source_folder, \"dogs\")\n\noutput_cats_folder = os.path.join(output_folder, \"cats\")\noutput_dogs_folder = os.path.join(output_folder, \"dogs\")\n\n# Create output folders\nos.makedirs(output_cats_folder, exist_ok=True)\nos.makedirs(output_dogs_folder, exist_ok=True)\n\n# Fraction of images to keep\nfraction_to_keep = 1\n\ndef select_and_copy_images(source, output_folder, label):\n    all_images = os.listdir(source)\n    num_to_keep = int(len(all_images) * fraction_to_keep)\n    selected_images = random.sample(all_images, num_to_keep)\n    \n    for image in selected_images:\n        image_path = os.path.join(source, image)\n        new_filename = f\"{label}_{image}\"  # Append label to the filename\n        new_image_path = os.path.join(output_folder, new_filename)\n        shutil.copy(image_path, new_image_path)\n\n# Select and copy images while assigning labels\nselect_and_copy_images(cats_source, output_cats_folder, \"cat\")\nselect_and_copy_images(dogs_source, output_dogs_folder, \"dog\")\n\n# Create labels DataFrame\ndata = []\nfor folder in [output_cats_folder, output_dogs_folder]:\n    label = folder.split(\"_\")[1]\n    for filename in os.listdir(folder):\n        data.append((filename, label))\n\ndf = pd.DataFrame(data, columns=['filename', 'label'])\n\n# Save labels to a CSV file\ndf.to_csv(os.path.join(output_folder, 'dataset_labels.csv'), index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.31662Z","iopub.status.idle":"2023-08-29T09:16:26.317404Z","shell.execute_reply.started":"2023-08-29T09:16:26.31716Z","shell.execute_reply":"2023-08-29T09:16:26.317187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('Desktop/dogs-vs-cats/reduced_train(70)/dataset_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.318753Z","iopub.status.idle":"2023-08-29T09:16:26.319516Z","shell.execute_reply.started":"2023-08-29T09:16:26.319276Z","shell.execute_reply":"2023-08-29T09:16:26.319299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Set the paths to your dataset folders\ncats_folder = \"Desktop/dogs-vs-cats/reduced_train(70)\"\ndogs_folder = \"Desktop/dogs-vs-cats/reduced_train(70)\"\n\n# Parameters\nimage_size = (150, 150)\nbatch_size = 128\n\n# Data augmentation and normalization\ndatagen = ImageDataGenerator(\n    rescale = 1/255., \n    horizontal_flip = True, \n    zoom_range = 0.3, \n    rotation_range = 15.,\n    validation_split = 0.1,\n)\n\ntrain_generator = datagen.flow_from_directory(\n    directory=cats_folder,          # Specify cats_folder or dogs_folder\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode='binary',\n    subset='training'\n)\n\nvalidation_generator = datagen.flow_from_directory(\n    directory=cats_folder,          # Specify cats_folder or dogs_folder\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode='binary',\n    subset='validation'\n)","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-08-29T09:16:26.320872Z","iopub.status.idle":"2023-08-29T09:16:26.321635Z","shell.execute_reply.started":"2023-08-29T09:16:26.321396Z","shell.execute_reply":"2023-08-29T09:16:26.321418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.Sequential()\n\ninitializers = {\n    \n}\nmodel.add( \n    keras.layers.Conv2D(\n        24, 5, input_shape=(150,150,3), \n        activation='relu', \n    )\n)\nmodel.add( keras.layers.MaxPooling2D(2) )\nmodel.add( \n    keras.layers.Conv2D(\n        48, 5, activation='relu', \n    )\n)\nmodel.add( keras.layers.MaxPooling2D(2) )\nmodel.add( \n    keras.layers.Conv2D(\n        96, 5, activation='relu', \n    )\n)\nmodel.add( keras.layers.Flatten() )\nmodel.add( keras.layers.Dropout(0.2) )\n\nmodel.add( keras.layers.Dense(\n    1, activation='softmax',\n    )\n)\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.323004Z","iopub.status.idle":"2023-08-29T09:16:26.323767Z","shell.execute_reply.started":"2023-08-29T09:16:26.323516Z","shell.execute_reply":"2023-08-29T09:16:26.32354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])\nhistory_augm = model.fit(\n    train_generator, \n    validation_data = validation_generator,\n    workers=10,\n    epochs=10,\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.325118Z","iopub.status.idle":"2023-08-29T09:16:26.325868Z","shell.execute_reply.started":"2023-08-29T09:16:26.325616Z","shell.execute_reply":"2023-08-29T09:16:26.325639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filenames=os.listdir(\"Desktop/dogs-vs-cats/train\")\n# categories=[]\n# for f_name in filenames:\n#     category=f_name.split('.')[0]\n#     if category=='dog':\n#         categories.append(1)\n#     else:\n#         categories.append(0)\n# df=pd.DataFrame({\n#     'filename':filenames,\n#     'category':categories\n# })","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-08-29T09:16:26.327209Z","iopub.status.idle":"2023-08-29T09:16:26.327986Z","shell.execute_reply.started":"2023-08-29T09:16:26.327731Z","shell.execute_reply":"2023-08-29T09:16:26.327754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filenames=os.listdir(\"Desktop/dogs-vs-cats/train\")\n# categories=[]\n# for f_name in filenames:\n#     category=f_name.split('.')[0]\n#     if category=='dog':\n#         categories.append(1)\n#     else:\n#         categories.append(0)\n# df_t=pd.DataFrame({\n#     'filename':filenames,\n#     'category':categories\n# })","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-08-29T09:16:26.329305Z","iopub.status.idle":"2023-08-29T09:16:26.330083Z","shell.execute_reply.started":"2023-08-29T09:16:26.329828Z","shell.execute_reply":"2023-08-29T09:16:26.329852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df[\"category\"] = df[\"category\"].replace({0:'cat',1:'dog'})\n# train_df,validate_df = train_test_split(df,test_size=0.20,\n#   random_state=42)\n# train_df = train_df.reset_index(drop=True)\n# validate_df = validate_df.reset_index(drop=True)\n# total_train=train_df.shape[0]\n# total_validate=validate_df.shape[0]\n# batch_size=256","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-08-29T09:16:26.331432Z","iopub.status.idle":"2023-08-29T09:16:26.332198Z","shell.execute_reply.started":"2023-08-29T09:16:26.33196Z","shell.execute_reply":"2023-08-29T09:16:26.331983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\n# Replace 'model_path' with the actual path to your model file\nmodel_path = \"Desktop/New folder/cats_vs_dogs (1).h5\"\n\n# Load the model\nloaded_model = load_model(model_path)\n\n# Display model summary\nloaded_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.333555Z","iopub.status.idle":"2023-08-29T09:16:26.334327Z","shell.execute_reply.started":"2023-08-29T09:16:26.334088Z","shell.execute_reply":"2023-08-29T09:16:26.334111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model.predict(validation_generator,verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.335694Z","iopub.status.idle":"2023-08-29T09:16:26.336472Z","shell.execute_reply.started":"2023-08-29T09:16:26.336229Z","shell.execute_reply":"2023-08-29T09:16:26.336253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\n# Set the paths to your dataset folders\ntest_images_folder = \"Desktop/dogs-vs-cats/test1\"\n\n\n# Parameters\nimage_size = (150, 150)\n\n# Prepare a list to store image paths\ntest_image_paths = [os.path.join(test_images_folder, f\"{i}.jpg\") for i in range(1, 6)]\n\n# Load and preprocess test images\ntest_images = []\nfor image_path in test_image_paths:\n    img = image.load_img(image_path, target_size=image_size)\n    img_array = image.img_to_array(img)\n    img_array = np.expand_dims(img_array, axis=0)\n    img_array /= 255.0\n    test_images.append(img_array)\n\n# Convert the list of image arrays to a single NumPy array\ntest_images = np.vstack(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.337857Z","iopub.status.idle":"2023-08-29T09:16:26.338678Z","shell.execute_reply.started":"2023-08-29T09:16:26.33842Z","shell.execute_reply":"2023-08-29T09:16:26.338443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test images\npredictions = loaded_model.predict(test_images)\n\n# Get the predicted class indices\npredicted_class_indices = np.argmax(predictions, axis=1)\n\n# Print predicted class indices\nprint(\"Predicted class indices:\", predicted_class_indices)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T09:16:26.340064Z","iopub.status.idle":"2023-08-29T09:16:26.340841Z","shell.execute_reply.started":"2023-08-29T09:16:26.34059Z","shell.execute_reply":"2023-08-29T09:16:26.340614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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"}}