{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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":"2024-03-23T19:45:20.637628Z","iopub.execute_input":"2024-03-23T19:45:20.637959Z","iopub.status.idle":"2024-03-23T19:45:21.024740Z","shell.execute_reply.started":"2024-03-23T19:45:20.637932Z","shell.execute_reply":"2024-03-23T19:45:21.023729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import urllib.request\nimport os\nimport zipfile\nimport random\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.optimizers import RMSprop\nimport shutil\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:46:50.324605Z","iopub.execute_input":"2024-03-23T19:46:50.325018Z","iopub.status.idle":"2024-03-23T19:46:50.331590Z","shell.execute_reply.started":"2024-03-23T19:46:50.324986Z","shell.execute_reply":"2024-03-23T19:46:50.330338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_local_zip = '/kaggle/input/dogs-vs-cats/train.zip'\ntest_local_zip = '/kaggle/input/dogs-vs-cats/test1.zip'\n\ntrain_zip_ref = zipfile.ZipFile(train_local_zip, 'r')\ntest_zip_ref = zipfile.ZipFile(test_local_zip, 'r')\n\ntrain_zip_ref.extractall('/kaggle/working')\ntest_zip_ref.extractall('/kaggle/working')\n\ntrain_zip_ref.close()\ntest_zip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:45:33.944308Z","iopub.execute_input":"2024-03-23T19:45:33.945197Z","iopub.status.idle":"2024-03-23T19:45:49.876875Z","shell.execute_reply.started":"2024-03-23T19:45:33.945164Z","shell.execute_reply":"2024-03-23T19:45:49.875731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_path = '/kaggle/working'\n\ntrain_source_path = os.path.join(source_path, 'train')\ntest_source_path = os.path.join(source_path, 'test1')\n\n#delete non-image files\n!find /'/kaggle/working/train' -type f ! -name \"*.jpg\" -exec rm {} +\n!find /'/kaggle/working/test1' -type f ! -name \"*.jpg\" -exec rm {} +\n\n# os.listdir returns a list containing all files under the given path\nprint(f'there are {len(os.listdir(train_source_path))} images of train.')\nprint(f'there are {len(os.listdir(test_source_path))} images of test.')","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:45:49.878972Z","iopub.execute_input":"2024-03-23T19:45:49.879276Z","iopub.status.idle":"2024-03-23T19:45:51.988766Z","shell.execute_reply.started":"2024-03-23T19:45:49.879251Z","shell.execute_reply":"2024-03-23T19:45:51.987327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = os.path.join(source_path, 'cats-v-dogs')\n\n# Empty directory to prevent FileExistsError is the function is run several times\nif os.path.exists(root_dir):\n  shutil.rmtree(root_dir)\n\n#Create Directory\nos.mkdir(root_dir)\n\n# GRADED FUNCTION: create_train_val_dirs\ndef create_train_val_dirs(root_path):\n    \n    \"\"\"\n    Creates directories for the train and test sets\n\n    Args:\n    root_path (string) - the base directory path to create subdirectories from\n\n    Returns:\n    None\n    \"\"\"\n    for i in ['training', 'validation']:\n        os.mkdir(os.path.join(root_path, i))\n        for t in ['cats', 'dogs']:\n            os.mkdir(os.path.join(os.path.join(root_path, i), t))\n    pass\n\ntry:\n  create_train_val_dirs(root_path=root_dir)\nexcept FileExistsError:\n  print(\"You should not be seeing this since the upper directory is removed beforehand\")","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:45:51.990541Z","iopub.execute_input":"2024-03-23T19:45:51.991319Z","iopub.status.idle":"2024-03-23T19:45:52.000351Z","shell.execute_reply.started":"2024-03-23T19:45:51.991271Z","shell.execute_reply":"2024-03-23T19:45:51.999296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for rootdir, dirs, files in os.walk(root_dir):\n    for subdir in dirs:\n        print(os.path.join(rootdir, subdir))","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:45:52.001681Z","iopub.execute_input":"2024-03-23T19:45:52.002054Z","iopub.status.idle":"2024-03-23T19:45:52.011722Z","shell.execute_reply.started":"2024-03-23T19:45:52.002014Z","shell.execute_reply":"2024-03-23T19:45:52.010674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_data(SOURCE_DIR, SPLIT_SIZE):\n    \"\"\"\n    Splits the data into train and test sets\n\n    Args:\n    SOURCE_DIR (string): directory path containing the images\n    TRAINING_DIR (string): directory path to be used for training\n    VALIDATION_DIR (string): directory path to be used for validation\n    SPLIT_SIZE (float): proportion of the dataset to be used for training\n\n    Returns:\n    None\n    \"\"\"\n\n    ### START CODE HERE\n    data_list = os.listdir(SOURCE_DIR)\n    \n    #cat_list = list(filter(lambda x:x.beginwith('cat'), data_list))\n    \n    train_rand = random.sample(data_list, int(len(data_list)*SPLIT_SIZE))\n    \n    for i in data_list:\n        if os.path.getsize(os.path.join(SOURCE_DIR, i)) != 0:\n            if i in train_rand:\n                if i.startswith('cat'):\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/training/cats')\n                else:\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/training/dogs')\n            else:\n                if i.startswith('cat'):\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/validation/cats')\n                else:\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/validation/dogs')\n        else:\n            print(i, \"  is zero length, so ignoring.\")\n    pass","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:47:02.729350Z","iopub.execute_input":"2024-03-23T19:47:02.730075Z","iopub.status.idle":"2024-03-23T19:47:02.738851Z","shell.execute_reply.started":"2024-03-23T19:47:02.730035Z","shell.execute_reply":"2024-03-23T19:47:02.737408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SOURCE_DIR = train_source_path\n\nsplit_size = .9\n\nsplit_data(SOURCE_DIR, split_size)\n\nprint(f\"\\n\\nOriginal cat's directory has {len(os.listdir(SOURCE_DIR))} images\")\n\n# Training and validation splits\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/training/cats'))} images of cats for training\")\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/training/dogs'))} images of dogs for training\")\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/validation/cats'))} images of cats for validation\")\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/validation/dogs'))} images of dogs for validation\")","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:47:09.158333Z","iopub.execute_input":"2024-03-23T19:47:09.158750Z","iopub.status.idle":"2024-03-23T19:47:24.532152Z","shell.execute_reply.started":"2024-03-23T19:47:09.158720Z","shell.execute_reply":"2024-03-23T19:47:24.530873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAINING_DIR = \"/kaggle/working/cats-v-dogs/training\"\nVALIDATION_DIR = \"/kaggle/working/cats-v-dogs/validation\"\n\n# Instantiate the ImageDataGenerator class \ntrain_datagen = ImageDataGenerator(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'\n                                     )\n\n# Pass in the appropriate arguments to the flow_from_directory method\ntrain_generator = train_datagen.flow_from_directory(directory=TRAINING_DIR,\n                                                      batch_size=100,\n                                                      class_mode='binary',\n                                                      target_size=(150, 150))\n\n# Instantiate the ImageDataGenerator class (don't forget to set the rescale argument)\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\n\n# Pass in the appropriate arguments to the flow_from_directory method\nvalidation_generator = validation_datagen.flow_from_directory(directory=VALIDATION_DIR,\n                                                                batch_size=100,\n                                                                class_mode='binary',\n                                                                target_size=(150, 150))\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:55:56.414832Z","iopub.execute_input":"2024-03-23T19:55:56.415357Z","iopub.status.idle":"2024-03-23T19:55:56.929481Z","shell.execute_reply.started":"2024-03-23T19:55:56.415318Z","shell.execute_reply":"2024-03-23T19:55:56.928380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get and prepare the model\n\nweights_url = \"https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5\"\nweights_file = \"inception_v3.h5\"\nurllib.request.urlretrieve(weights_url, weights_file)\n\n# Instantiate the model\npre_trained_model = InceptionV3(input_shape=(150, 150, 3),\n                                include_top=False,\n                                weights=None)\n\n# load pre-trained weights\npre_trained_model.load_weights(weights_file)\n\n# freeze the layers\nfor layer in pre_trained_model.layers:\n    layer.trainable = False\n\n# pre_trained_model.summary()\n\nlast_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ', last_layer.output_shape)\nlast_output = last_layer.output\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:55:56.932013Z","iopub.execute_input":"2024-03-23T19:55:56.932372Z","iopub.status.idle":"2024-03-23T19:56:01.250179Z","shell.execute_reply.started":"2024-03-23T19:55:56.932340Z","shell.execute_reply":"2024-03-23T19:56:01.248977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add layers\n\n# Flatten the output layer to 1 dimension\nx = layers.Flatten()(last_output)\n# Add a fully connected layer with 1,024 hidden units and ReLU activation\nx = layers.Dense(1024, activation='relu')(x)\n# Add a final sigmoid layer for classification\nx = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = Model(pre_trained_model.input, x)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:56:01.251542Z","iopub.execute_input":"2024-03-23T19:56:01.251899Z","iopub.status.idle":"2024-03-23T19:56:01.717247Z","shell.execute_reply.started":"2024-03-23T19:56:01.251868Z","shell.execute_reply":"2024-03-23T19:56:01.716462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train the model\n\n# compile the model\nmodel.compile(optimizer=RMSprop(lr=0.0001),\n              loss='binary_crossentropy',\n              metrics=['acc'])\n\n# train the model (adjust the number of epochs from 1 to improve performance)\nhistory = model.fit(\n            train_generator,\n            validation_data=validation_generator,\n            epochs=2,\n            verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:56:01.719148Z","iopub.execute_input":"2024-03-23T19:56:01.719720Z","iopub.status.idle":"2024-03-23T20:15:36.855471Z","shell.execute_reply.started":"2024-03-23T19:56:01.719687Z","shell.execute_reply":"2024-03-23T20:15:36.854490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize the training and validation accuracy\n\n%matplotlib inline\n\nimport matplotlib.image  as mpimg\nimport matplotlib.pyplot as plt\n\n#-----------------------------------------------------------\n# Retrieve a list of list results on training and test data\n# sets for each training epoch\n#-----------------------------------------------------------\nacc=history.history['acc']\nval_acc=history.history['val_acc']\nloss=history.history['loss']\nval_loss=history.history['val_loss']\n\nepochs=range(len(acc)) # Get number of epochs\n\n#------------------------------------------------\n# Plot training and validation accuracy per epoch\n#------------------------------------------------\nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.figure()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T20:15:36.857255Z","iopub.execute_input":"2024-03-23T20:15:36.857692Z","iopub.status.idle":"2024-03-23T20:15:37.114492Z","shell.execute_reply.started":"2024-03-23T20:15:36.857652Z","shell.execute_reply":"2024-03-23T20:15:37.113466Z"},"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(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='validation_accuracy')\nplt.title('Training and validation accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and Validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-23T20:15:37.115928Z","iopub.execute_input":"2024-03-23T20:15:37.116513Z","iopub.status.idle":"2024-03-23T20:15:37.622902Z","shell.execute_reply.started":"2024-03-23T20:15:37.116482Z","shell.execute_reply":"2024-03-23T20:15:37.621754Z"},"trusted":true},"execution_count":null,"outputs":[]}]}