{"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":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30616,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2023-12-10T15:59:45.860620Z","iopub.execute_input":"2023-12-10T15:59:45.861035Z","iopub.status.idle":"2023-12-10T15:59:46.858319Z","shell.execute_reply.started":"2023-12-10T15:59:45.860994Z","shell.execute_reply":"2023-12-10T15:59:46.857276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\ndef unzip_files(directory_path, extract_to_path):\n    for filename in os.listdir(directory_path):\n        if filename.endswith('.zip'):\n            zip_file_path = os.path.join(directory_path, filename)\n            with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n                zip_ref.extractall(extract_to_path)\n\nmain_directory = '/kaggle/input/dogs-vs-cats-redux-kernels-edition'\nextract_to_path = '/kaggle/working/'\n\nunzip_files(main_directory, extract_to_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T15:59:46.860500Z","iopub.execute_input":"2023-12-10T15:59:46.861080Z","iopub.status.idle":"2023-12-10T16:00:05.097353Z","shell.execute_reply.started":"2023-12-10T15:59:46.861024Z","shell.execute_reply":"2023-12-10T16:00:05.096466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport matplotlib.pyplot as plt\nimport cv2\nimport keras\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom keras import optimizers\nimport tensorflow as tf\nfrom keras.layers import Dense,Flatten,Dropout,BatchNormalization\nfrom keras import optimizers\nfrom keras.models import Sequential\nfrom keras.losses import BinaryCrossentropy\nfrom keras.activations import sigmoid\nfrom keras.applications import ResNetRS420\nimport tensorflow as tf\nfrom keras.layers import Dense,Flatten,Dropout,BatchNormalization\nfrom keras.applications import DenseNet121,DenseNet169,DenseNet201,RegNetY320,RegNetX320","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:05.098686Z","iopub.execute_input":"2023-12-10T16:00:05.099083Z","iopub.status.idle":"2023-12-10T16:00:12.753593Z","shell.execute_reply.started":"2023-12-10T16:00:05.099046Z","shell.execute_reply":"2023-12-10T16:00:12.752571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training:","metadata":{}},{"cell_type":"code","source":"train_dir= '/kaggle/working/train'\ntrain_data=glob.glob(train_dir+'**/*.jpg',root_dir=train_dir)\ntrain_data=train_data[:5000]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:12.756771Z","iopub.execute_input":"2023-12-10T16:00:12.757398Z","iopub.status.idle":"2023-12-10T16:00:12.857222Z","shell.execute_reply.started":"2023-12-10T16:00:12.757367Z","shell.execute_reply":"2023-12-10T16:00:12.856173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=[]\n\nfor img_path in train_data:\n    img= cv2.imread(img_path)\n    img= cv2.resize(img,(256,256))\n    img= img/255\n    \n    X_train.append(img)\n    \nX_train= np.array(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:12.858507Z","iopub.execute_input":"2023-12-10T16:00:12.858833Z","iopub.status.idle":"2023-12-10T16:00:31.876188Z","shell.execute_reply.started":"2023-12-10T16:00:12.858805Z","shell.execute_reply":"2023-12-10T16:00:31.875262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:31.877570Z","iopub.execute_input":"2023-12-10T16:00:31.877872Z","iopub.status.idle":"2023-12-10T16:00:31.885155Z","shell.execute_reply.started":"2023-12-10T16:00:31.877845Z","shell.execute_reply":"2023-12-10T16:00:31.884268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_name= glob.glob('*.jpg',root_dir= train_dir)\nfiles_name[:5]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:31.886794Z","iopub.execute_input":"2023-12-10T16:00:31.887127Z","iopub.status.idle":"2023-12-10T16:00:31.942616Z","shell.execute_reply.started":"2023-12-10T16:00:31.887094Z","shell.execute_reply":"2023-12-10T16:00:31.941758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = [file_name.split('.')[0] for file_name in files_name]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:31.943776Z","iopub.execute_input":"2023-12-10T16:00:31.944492Z","iopub.status.idle":"2023-12-10T16:00:31.957529Z","shell.execute_reply.started":"2023-12-10T16:00:31.944456Z","shell.execute_reply":"2023-12-10T16:00:31.956779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np.array(y_train)\ny_train[:5]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:31.958715Z","iopub.execute_input":"2023-12-10T16:00:31.959045Z","iopub.status.idle":"2023-12-10T16:00:31.974527Z","shell.execute_reply.started":"2023-12-10T16:00:31.959017Z","shell.execute_reply":"2023-12-10T16:00:31.973613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train= y_train[:5000]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:31.978626Z","iopub.execute_input":"2023-12-10T16:00:31.978886Z","iopub.status.idle":"2023-12-10T16:00:31.983955Z","shell.execute_reply.started":"2023-12-10T16:00:31.978862Z","shell.execute_reply":"2023-12-10T16:00:31.983020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder= LabelEncoder()\ny_train= encoder.fit_transform(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:31.984909Z","iopub.execute_input":"2023-12-10T16:00:31.985188Z","iopub.status.idle":"2023-12-10T16:00:32.543244Z","shell.execute_reply.started":"2023-12-10T16:00:31.985163Z","shell.execute_reply":"2023-12-10T16:00:32.542177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(min(16, len(X_train))):\n    plt.subplot(4, 4, i+1)\n    plt.imshow(X_train[i])\n    plt.title(y_train[i])\n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:32.544910Z","iopub.execute_input":"2023-12-10T16:00:32.545592Z","iopub.status.idle":"2023-12-10T16:00:34.464869Z","shell.execute_reply.started":"2023-12-10T16:00:32.545553Z","shell.execute_reply":"2023-12-10T16:00:34.463844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nbase_model = DenseNet201(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\n\n# Freeze the layers of the base model\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel = tf.keras.models.Sequential([\n    base_model,\n    BatchNormalization(),\n    \n    Flatten(),\n    \n    Dense(256,activation='relu'),\n    BatchNormalization(),\n    Dense(128,activation='relu'),\n    Dense(64,activation='relu'),\n    Dropout(0.5),\n    Dense(1,activation='sigmoid'),\n])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:34.466306Z","iopub.execute_input":"2023-12-10T16:00:34.466688Z","iopub.status.idle":"2023-12-10T16:00:45.022783Z","shell.execute_reply.started":"2023-12-10T16:00:34.466654Z","shell.execute_reply":"2023-12-10T16:00:45.021901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:45.023716Z","iopub.execute_input":"2023-12-10T16:00:45.023999Z","iopub.status.idle":"2023-12-10T16:00:45.118923Z","shell.execute_reply.started":"2023-12-10T16:00:45.023975Z","shell.execute_reply":"2023-12-10T16:00:45.117860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Learning rate schedule\nlr_schedule = optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=0.00001,\n    decay_steps=2000,\n    decay_rate=0.7\n)\n\noptimizer = optimizers.Adam(learning_rate=lr_schedule)\n\nmodel.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])\n\n# Early stopping callback\nearly_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:45.120022Z","iopub.execute_input":"2023-12-10T16:00:45.120309Z","iopub.status.idle":"2023-12-10T16:00:45.162862Z","shell.execute_reply.started":"2023-12-10T16:00:45.120283Z","shell.execute_reply":"2023-12-10T16:00:45.162045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(X_train, y_train, epochs=30, verbose=1, validation_split=0.2, callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:00:45.164130Z","iopub.execute_input":"2023-12-10T16:00:45.164777Z","iopub.status.idle":"2023-12-10T16:04:28.842515Z","shell.execute_reply.started":"2023-12-10T16:00:45.164739Z","shell.execute_reply":"2023-12-10T16:04:28.841377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Plot training and validation accuracy\nplt.figure(figsize=(12, 4))\n\n# Plot training accuracy\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\n# Plot training and validation loss\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:04:28.852997Z","iopub.execute_input":"2023-12-10T16:04:28.853418Z","iopub.status.idle":"2023-12-10T16:05:28.056693Z","shell.execute_reply.started":"2023-12-10T16:04:28.853379Z","shell.execute_reply":"2023-12-10T16:05:28.055746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('catvsdog.keras')","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:05:28.057988Z","iopub.execute_input":"2023-12-10T16:05:28.058436Z","iopub.status.idle":"2023-12-10T16:05:32.895943Z","shell.execute_reply.started":"2023-12-10T16:05:28.058361Z","shell.execute_reply":"2023-12-10T16:05:32.894755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Testing:","metadata":{}},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel= load_model('/kaggle/working/catvsdog.keras')","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:05:32.897432Z","iopub.execute_input":"2023-12-10T16:05:32.897854Z","iopub.status.idle":"2023-12-10T16:06:16.159273Z","shell.execute_reply.started":"2023-12-10T16:05:32.897814Z","shell.execute_reply":"2023-12-10T16:06:16.158407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir= '/kaggle/working/test'","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:06:16.160911Z","iopub.execute_input":"2023-12-10T16:06:16.161235Z","iopub.status.idle":"2023-12-10T16:06:16.165638Z","shell.execute_reply.started":"2023-12-10T16:06:16.161194Z","shell.execute_reply":"2023-12-10T16:06:16.164637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=glob.glob(test_dir+'/*.jpg')","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:06:16.166887Z","iopub.execute_input":"2023-12-10T16:06:16.167160Z","iopub.status.idle":"2023-12-10T16:06:16.221749Z","shell.execute_reply.started":"2023-12-10T16:06:16.167135Z","shell.execute_reply":"2023-12-10T16:06:16.221082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_test=[]\n\n# for img_path in test_data:\n#     img= cv2.imread(img_path)\n#     img= cv2.resize(img,(256,256))\n#     img= img/255\n    \n#     X_test.append(img)\n    \n# X_test= np.array(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:06:16.222678Z","iopub.execute_input":"2023-12-10T16:06:16.222937Z","iopub.status.idle":"2023-12-10T16:06:16.227064Z","shell.execute_reply.started":"2023-12-10T16:06:16.222913Z","shell.execute_reply":"2023-12-10T16:06:16.226166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom keras.models import load_model\nfrom keras.preprocessing import image\nimport cv2\n\n# Define a function to preprocess images\ndef preprocess_image(img):\n    img = cv2.resize(img, (256, 256))\n    img = img / 255.0  # Normalize pixel values\n    img = np.expand_dims(img, axis=0)\n    return img\n\n# Example of batch processing\nbatch_size = 32\n\n# Initialize an empty list to store predictions\nall_predictions = []\n\nfor i in range(0, len(test_data), batch_size):\n    batch_paths = test_data[i:i + batch_size]\n    batch_images = [cv2.imread(img_path) for img_path in batch_paths]\n    batch_images = [preprocess_image(img) for img in batch_images]\n\n    # Convert the batch to a numpy array\n    batch_data = np.vstack(batch_images)\n\n    # Predictions for the current batch\n    predictions = model.predict(batch_data)\n\n    # Extend the predictions list\n    all_predictions.extend(predictions.flatten())\n\n# Now, 'all_predictions' contains predictions for all test images\nall_predictions = np.array(all_predictions)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:06:16.228529Z","iopub.execute_input":"2023-12-10T16:06:16.228830Z","iopub.status.idle":"2023-12-10T16:08:14.232325Z","shell.execute_reply.started":"2023-12-10T16:06:16.228806Z","shell.execute_reply":"2023-12-10T16:08:14.231422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new DataFrame with 'label' column\nsubmission_df = pd.DataFrame({'label': all_predictions})\n\n# Save the new DataFrame to a CSV file\nsubmission_df.to_csv('/kaggle/working/new_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T16:08:14.233771Z","iopub.execute_input":"2023-12-10T16:08:14.234740Z","iopub.status.idle":"2023-12-10T16:08:14.278665Z","shell.execute_reply.started":"2023-12-10T16:08:14.234673Z","shell.execute_reply":"2023-12-10T16:08:14.277812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}