{"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":5441,"databundleVersionId":38425,"sourceType":"competition"},{"sourceId":214247,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":182637,"modelId":204849}],"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","trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:31:53.770157Z","iopub.execute_input":"2024-12-30T09:31:53.770400Z","iopub.status.idle":"2024-12-30T09:31:54.076776Z","shell.execute_reply.started":"2024-12-30T09:31:53.770382Z","shell.execute_reply":"2024-12-30T09:31:54.075870Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Import","metadata":{}},{"cell_type":"code","source":"#OLD IMPORTS\n#from keras import layers\n#from keras import models\n#from keras import optimizers\n#from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\n#from keras import applications\n\n#NEW IMPORTS\n\n# Data processing\nimport numpy as np\nimport pandas as pd \n\n# Visualization libraries\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\n# Keras ve TensorFlow libraries\nfrom tensorflow.keras import layers, models, optimizers\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array, array_to_img\nfrom tensorflow.keras.utils import to_categorical, Sequence\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.models import load_model\n\n# Sklearn\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\n\n# Albumentations (Data Augmentation)\nimport albumentations as A\nfrom albumentations import (\n    HorizontalFlip, VerticalFlip, ShiftScaleRotate, RandomBrightnessContrast, \n    Rotate, Compose, OneOf, GaussianBlur, CLAHE, RandomGamma, ElasticTransform\n)\n\n# Other\nfrom PIL import Image\nimport os\nimport math\n\n# constant value\nnp.random.seed(101)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T12:57:44.624121Z","iopub.execute_input":"2024-12-30T12:57:44.624514Z","iopub.status.idle":"2024-12-30T12:57:44.635414Z","shell.execute_reply.started":"2024-12-30T12:57:44.624474Z","shell.execute_reply":"2024-12-30T12:57:44.634195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#!pip install -U albumentations","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:22:38.859540Z","iopub.execute_input":"2024-12-29T10:22:38.860097Z","iopub.status.idle":"2024-12-29T10:22:38.863403Z","shell.execute_reply.started":"2024-12-29T10:22:38.860073Z","shell.execute_reply":"2024-12-29T10:22:38.862542Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load_data","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test', 'train']\n# Will unzip the files so that you can see them..\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:12:19.634738Z","iopub.execute_input":"2024-12-30T13:12:19.635121Z","iopub.status.idle":"2024-12-30T13:12:35.346331Z","shell.execute_reply.started":"2024-12-30T13:12:19.635086Z","shell.execute_reply":"2024-12-30T13:12:35.345198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_FOLDER_PATH=\"/kaggle/working/train\"\nimages_name=os.listdir(IMAGE_FOLDER_PATH)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:12:35.347600Z","iopub.execute_input":"2024-12-30T13:12:35.347975Z","iopub.status.idle":"2024-12-30T13:12:35.364969Z","shell.execute_reply.started":"2024-12-30T13:12:35.347941Z","shell.execute_reply":"2024-12-30T13:12:35.363757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images_name","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:22.177482Z","iopub.execute_input":"2024-12-30T09:32:22.177790Z","iopub.status.idle":"2024-12-30T09:32:22.202625Z","shell.execute_reply.started":"2024-12-30T09:32:22.177760Z","shell.execute_reply":"2024-12-30T09:32:22.201624Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"full_path=os.path.join(IMAGE_FOLDER_PATH, images_name[0])\nfull_path","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:01.746779Z","iopub.execute_input":"2024-12-30T13:17:01.747220Z","iopub.status.idle":"2024-12-30T13:17:01.754645Z","shell.execute_reply.started":"2024-12-30T13:17:01.747189Z","shell.execute_reply":"2024-12-30T13:17:01.753498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"targets = []\nfull_paths = []\nfor image_name in images_name:\n    target = image_name.split(\".\")[0]\n    full_path=os.path.join(IMAGE_FOLDER_PATH, image_name)\n    full_paths.append(full_path)\n    targets.append(target)\n\ndf = pd.DataFrame()\ndf['image_path'] = full_paths\ndf['target'] = targets","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:03.946028Z","iopub.execute_input":"2024-12-30T13:17:03.946390Z","iopub.status.idle":"2024-12-30T13:17:04.011024Z","shell.execute_reply.started":"2024-12-30T13:17:03.946360Z","shell.execute_reply":"2024-12-30T13:17:04.010003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_path = '/kaggle/working/test'\nimages_name=os.listdir(test_path)\nimages_name","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:04.693264Z","iopub.execute_input":"2024-12-30T13:17:04.693752Z","iopub.status.idle":"2024-12-30T13:17:04.721963Z","shell.execute_reply.started":"2024-12-30T13:17:04.693704Z","shell.execute_reply":"2024-12-30T13:17:04.720611Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test = pd.DataFrame()\nfor inx, image_name in enumerate(images_name):\n    full_path = os.path.join(test_path,image_name)\n    df_test.loc[inx,'full_path'] = full_path","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:06.420368Z","iopub.execute_input":"2024-12-30T13:17:06.420738Z","iopub.status.idle":"2024-12-30T13:17:12.672658Z","shell.execute_reply.started":"2024-12-30T13:17:06.420704Z","shell.execute_reply":"2024-12-30T13:17:12.671180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:27.340991Z","iopub.execute_input":"2024-12-30T09:32:27.341249Z","iopub.status.idle":"2024-12-30T09:32:27.353197Z","shell.execute_reply.started":"2024-12-30T09:32:27.341214Z","shell.execute_reply":"2024-12-30T09:32:27.352469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:27.354079Z","iopub.execute_input":"2024-12-30T09:32:27.354405Z","iopub.status.idle":"2024-12-30T09:32:27.446264Z","shell.execute_reply.started":"2024-12-30T09:32:27.354375Z","shell.execute_reply":"2024-12-30T09:32:27.445306Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['target'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:12.674238Z","iopub.execute_input":"2024-12-30T13:17:12.674661Z","iopub.status.idle":"2024-12-30T13:17:12.697455Z","shell.execute_reply.started":"2024-12-30T13:17:12.674617Z","shell.execute_reply":"2024-12-30T13:17:12.696068Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Creating a photo gallery","metadata":{}},{"cell_type":"code","source":"WIDTH,HEIGHT = 150,150","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:15.012620Z","iopub.execute_input":"2024-12-30T13:17:15.013036Z","iopub.status.idle":"2024-12-30T13:17:15.018238Z","shell.execute_reply.started":"2024-12-30T13:17:15.012998Z","shell.execute_reply":"2024-12-30T13:17:15.016439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#add white line between two image\ndef get_side(img, side_type, side_size=5):\n    height, width, channel=img.shape\n    if side_type==\"horizontal\":\n        return np.ones((height,side_size,3), dtype=np.float32)*255\n        \n    return np.ones((side_size, width,  channel), dtype=np.float32)*255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:15.823710Z","iopub.execute_input":"2024-12-30T13:17:15.824077Z","iopub.status.idle":"2024-12-30T13:17:15.829859Z","shell.execute_reply.started":"2024-12-30T13:17:15.824045Z","shell.execute_reply":"2024-12-30T13:17:15.828504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_gallery(show=\"both\"):\n    n=100\n    counter=0\n    images=list()\n    vertical_images=[]\n    rng_state = np.random.get_state() # random sayi uruetmini sabitlemek icin \n    np.random.shuffle(full_paths) #pathleri karistir\n    np.random.set_state(rng_state) #random sayi uretirken ayni sayilari uretmesi icin rngyi kullan\n    np.random.shuffle(targets) #hedefleri de bu sekilde karistir\n    for path, target in zip(full_paths, targets): #iki farklı listeyi (full_paths ve targets) aynı anda döngüye sok\n        if target!=show and show!=\"both\":\n            continue\n        counter=counter+1\n        if counter%100==0:\n            break\n        #Image loading from disk as JpegImageFile file format\n        img=load_img(path, target_size=(WIDTH,HEIGHT)) #150,150\n        #Converting JpegImageFile to numpy array\n        img=img_to_array(img)\n        \n        hside=get_side(img, side_type=\"horizontal\")\n        images.append(img)\n        images.append(hside)\n\n        if counter%10==0:\n            himage=np.hstack((images)) #iki veya daha fazla diziyi yatay olarak (sütun bazında) birleştirir\n            vside=get_side(himage, side_type=\"vertical\")\n            vertical_images.append(himage)\n            vertical_images.append(vside)\n            \n            images=list()\n\n    gallery=np.vstack((vertical_images)) \n    plt.figure(figsize=(12,12))\n    plt.xticks([])\n    plt.yticks([])\n    title={\"both\":\"Dogs and Cats\",\n          \"cat\": \"Cats\",\n          \"dog\": \"Dogs\"}\n    plt.title(\"100 samples of {} of the dataset\".format(title[show]))\n    plt.imshow(gallery.astype(np.uint8))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:27.632682Z","iopub.execute_input":"2024-12-30T09:32:27.632862Z","iopub.status.idle":"2024-12-30T09:32:27.642810Z","shell.execute_reply.started":"2024-12-30T09:32:27.632846Z","shell.execute_reply":"2024-12-30T09:32:27.641935Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_gallery(show=\"cat\")\nshow_gallery(show=\"dog\")\nshow_gallery()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:23:52.230893Z","iopub.execute_input":"2024-12-29T10:23:52.231183Z","iopub.status.idle":"2024-12-29T10:23:55.397598Z","shell.execute_reply.started":"2024-12-29T10:23:52.231160Z","shell.execute_reply":"2024-12-29T10:23:55.396481Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## img to Array","metadata":{}},{"cell_type":"code","source":"first_image_path = df['image_path']\nimg = load_img(first_image_path, target_size=(150, 150))\nimg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:24:03.616399Z","iopub.execute_input":"2024-12-29T10:24:03.616733Z","iopub.status.idle":"2024-12-29T10:24:03.627454Z","shell.execute_reply.started":"2024-12-29T10:24:03.616709Z","shell.execute_reply":"2024-12-29T10:24:03.626507Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## img_to_array","metadata":{}},{"cell_type":"code","source":"img = img_to_array(img)\nimg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:24:05.440824Z","iopub.execute_input":"2024-12-29T10:24:05.441128Z","iopub.status.idle":"2024-12-29T10:24:05.450005Z","shell.execute_reply.started":"2024-12-29T10:24:05.441105Z","shell.execute_reply":"2024-12-29T10:24:05.449186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:24:06.761787Z","iopub.execute_input":"2024-12-29T10:24:06.762084Z","iopub.status.idle":"2024-12-29T10:24:06.766753Z","shell.execute_reply.started":"2024-12-29T10:24:06.762058Z","shell.execute_reply":"2024-12-29T10:24:06.766048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:24:08.495011Z","iopub.execute_input":"2024-12-29T10:24:08.495307Z","iopub.status.idle":"2024-12-29T10:24:08.511340Z","shell.execute_reply.started":"2024-12-29T10:24:08.495285Z","shell.execute_reply":"2024-12-29T10:24:08.510486Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Seperate Train Dataset","metadata":{}},{"cell_type":"code","source":"y_train = df['target']\nX_train = df['image_path']\ndel df #free some spaces","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:22.509186Z","iopub.execute_input":"2024-12-30T13:17:22.509658Z","iopub.status.idle":"2024-12-30T13:17:22.514424Z","shell.execute_reply.started":"2024-12-30T13:17:22.509620Z","shell.execute_reply":"2024-12-30T13:17:22.513165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=y_train)\ny_train.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:25.166956Z","iopub.execute_input":"2024-12-30T13:17:25.167316Z","iopub.status.idle":"2024-12-30T13:17:25.381189Z","shell.execute_reply.started":"2024-12-30T13:17:25.167261Z","shell.execute_reply":"2024-12-30T13:17:25.379785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train[y_train.isna()]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:25.461135Z","iopub.execute_input":"2024-12-30T13:17:25.461524Z","iopub.status.idle":"2024-12-30T13:17:25.470464Z","shell.execute_reply.started":"2024-12-30T13:17:25.461484Z","shell.execute_reply":"2024-12-30T13:17:25.469328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:28.813086Z","iopub.execute_input":"2024-12-30T13:17:28.813429Z","iopub.status.idle":"2024-12-30T13:17:28.820832Z","shell.execute_reply.started":"2024-12-30T13:17:28.813400Z","shell.execute_reply":"2024-12-30T13:17:28.819634Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Normalization\n## Don't do that\n### these things will waste your memory because when you run this all of the photos will load by your Ram. Ram will full and it is occur some error.","metadata":{}},{"cell_type":"code","source":"#X_train_image_list = list()\n#for i in X_train:\n#    image = load_img(i,target_size = (150,150))\n#    array = img_to_array(image)/255.0\n#    X_train_image_list.append(array)\n#\n#X_train_image_list = np.array(X_train_image_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:16:09.578909Z","iopub.execute_input":"2024-12-29T10:16:09.579204Z","execution_failed":"2024-12-29T10:17:50.086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#df_test_image_list = list()\n#for i in test:\n#    image = load_img(i,target_size = (150,150))\n#    array = img_to_array(image)/255.0\n#    df_test_image_list.append(array)\n#\n#df_test_image_list = np.array(df_test_image_list)","metadata":{"trusted":true,"execution":{"execution_failed":"2024-12-29T10:17:50.087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"first_image = load_img(X_train[0],target_size = (150,150))\nfirst_image","metadata":{"trusted":true,"scrolled":true,"execution":{"iopub.status.busy":"2024-12-29T10:27:23.958932Z","iopub.execute_input":"2024-12-29T10:27:23.959210Z","iopub.status.idle":"2024-12-29T10:27:23.968938Z","shell.execute_reply.started":"2024-12-29T10:27:23.959190Z","shell.execute_reply":"2024-12-29T10:27:23.968090Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:27:27.212645Z","iopub.execute_input":"2024-12-29T10:27:27.212925Z","iopub.status.idle":"2024-12-29T10:27:27.217848Z","shell.execute_reply.started":"2024-12-29T10:27:27.212903Z","shell.execute_reply":"2024-12-29T10:27:27.217149Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Label encoding","metadata":{}},{"cell_type":"code","source":"label_encoder = LabelEncoder()\ny_train_encoded = label_encoder.fit_transform(y_train)\n\n# One-hot encoding işlemi\ny_train_one_hot = to_categorical(y_train_encoded, num_classes=2)\n\nprint(y_train_encoded)  # [0 1 0 1 0]\nprint(y_train_one_hot)  # i will use one_hot because ther ara only 2 class","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:31.963413Z","iopub.execute_input":"2024-12-30T13:17:31.963773Z","iopub.status.idle":"2024-12-30T13:17:31.977711Z","shell.execute_reply.started":"2024-12-30T13:17:31.963743Z","shell.execute_reply":"2024-12-30T13:17:31.976256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_path = X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:33.442561Z","iopub.execute_input":"2024-12-30T13:17:33.442960Z","iopub.status.idle":"2024-12-30T13:17:33.448173Z","shell.execute_reply.started":"2024-12-30T13:17:33.442925Z","shell.execute_reply":"2024-12-30T13:17:33.446627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X_train_path, y_train_encoded,\n                                                 test_size=0.2,\n                                                 random_state=101)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:33.799339Z","iopub.execute_input":"2024-12-30T13:17:33.799693Z","iopub.status.idle":"2024-12-30T13:17:33.809371Z","shell.execute_reply.started":"2024-12-30T13:17:33.799665Z","shell.execute_reply":"2024-12-30T13:17:33.808366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"len of the X_train: {len(X_train)}\")\nprint(f\"len of the X_val: {len(X_val)}\")\nprint(f\"len of the Y_train: {len(Y_train)}\")\nprint(f\"len of the Y_val: {len(Y_val)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:35.292841Z","iopub.execute_input":"2024-12-30T13:17:35.293267Z","iopub.status.idle":"2024-12-30T13:17:35.299889Z","shell.execute_reply.started":"2024-12-30T13:17:35.293230Z","shell.execute_reply":"2024-12-30T13:17:35.298718Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# CNN","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\n\n# Konvolüsyonel katman ve max pooling\nmodel.add(Input(shape=(150, 150, 3)))\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu')) #learn shape side etc.\nmodel.add(layers.Conv2D(32,(3,3),activation = 'relu'))\nmodel.add(layers.MaxPooling2D((2, 2))) #make the image smaller but keep the important features\n#avoid overfitting\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dropout(0.25))\n\nmodel.add(layers.Conv2D(64, (3,3), activation=\"relu\"))\nmodel.add(layers.Conv2D(64, (3,3), activation=\"relu\"))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.25))\n\nmodel.add(layers.Conv2D(128, (3,3), activation=\"relu\"))\nmodel.add(layers.Conv2D(128, (3,3), activation=\"relu\"))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.25))\n\n\nmodel.add(layers.Flatten()) #multi dim to one dim\nmodel.add(layers.Dense(256, activation=\"relu\")) #fully connected layer \nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(1, activation=\"sigmoid\"))\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:37:48.990692Z","iopub.execute_input":"2024-12-29T10:37:48.990973Z","iopub.status.idle":"2024-12-29T10:37:49.181482Z","shell.execute_reply.started":"2024-12-29T10:37:48.990951Z","shell.execute_reply":"2024-12-29T10:37:49.180783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Adam combines RMSprop's adaptive learning rate approach with the Momentum algorithm.\noptimizer = Adam(learning_rate=0.001)\nmodel.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T10:38:19.159544Z","iopub.execute_input":"2024-12-29T10:38:19.159850Z","iopub.status.idle":"2024-12-29T10:38:19.172657Z","shell.execute_reply.started":"2024-12-29T10:38:19.159826Z","shell.execute_reply":"2024-12-29T10:38:19.171930Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# whe use RMSprop when we work with  RNN model  (or. LSTM, GRU).\n#\n#optimizer = RMSprop(learning_rate =0.001, rho=0.9, epsilon=1e-08)\n#\n#\n#model.compile(loss=\"binary_crossentropy\", \n#             optimizer=optimizer,\n#             metrics=[\"accuracy\"])\n#print(\"[INFO]: model compiled...\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T08:46:47.839911Z","iopub.execute_input":"2024-12-29T08:46:47.840108Z","iopub.status.idle":"2024-12-29T08:46:47.843451Z","shell.execute_reply.started":"2024-12-29T08:46:47.840081Z","shell.execute_reply":"2024-12-29T08:46:47.842690Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data augmentation","metadata":{}},{"cell_type":"markdown","source":"I will use Albumentations for faster GPU performance","metadata":{}},{"cell_type":"code","source":"train_transforms = A.Compose([\n    A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.5),  # Changes brightness and contrast randomly\n    A.Rotate(limit=15, p=0.5),          # Rotates images between -15 and 15 degrees\n    A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=15, p=0.5),# Panning, scaling\n    A.HorizontalFlip(p=0.5),           # horizontal flip\n   A.OneOf([\n        A.GaussianBlur(blur_limit=(3, 7), p=0.5),\n        A.CLAHE(clip_limit=2.0, p=0.5)\n    ], p=0.5), #Contrast Limited Adaptive Histogram Equalization\n    \n    #the means of p parameter is %50 chance to apply.\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:54.799588Z","iopub.execute_input":"2024-12-30T13:17:54.799955Z","iopub.status.idle":"2024-12-30T13:17:54.808665Z","shell.execute_reply.started":"2024-12-30T13:17:54.799896Z","shell.execute_reply":"2024-12-30T13:17:54.807365Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### to visualize Multiple image:","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nfor i , image in enumerate(X_train[:5]):\n    plt.subplot(1,5,i+1)\n    plt.imshow(image)\n    plt.axis('off')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T08:47:35.247854Z","iopub.execute_input":"2024-12-29T08:47:35.248181Z","iopub.status.idle":"2024-12-29T08:47:35.535353Z","shell.execute_reply.started":"2024-12-29T08:47:35.248148Z","shell.execute_reply":"2024-12-29T08:47:35.534424Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nfor i , image in enumerate(X_train_augted[:5]):\n    plt.subplot(1,5,i+1)\n    plt.imshow(image)\n    plt.axis('off')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T08:47:35.536446Z","iopub.execute_input":"2024-12-29T08:47:35.536679Z","iopub.status.idle":"2024-12-29T08:47:35.763394Z","shell.execute_reply.started":"2024-12-29T08:47:35.536658Z","shell.execute_reply":"2024-12-29T08:47:35.762360Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"for avoid waste of memory","metadata":{}},{"cell_type":"code","source":"class CustomDataGenerator(Sequence):\n    def __init__(self, images, labels, batch_size, transforms=None, shuffle=True,**kwargs):\n        \n        assert len(images) == len(labels), \"Lengths of Images and Labels arrays do not match!\"\n        super().__init__(**kwargs)\n        self.images = np.array(images)  # Görselleri numpy dizisine çeviriyoruz\n        self.labels = np.array(labels)  # Etiketleri numpy dizisine çeviriyoruz\n        self.batch_size = batch_size  # Batch boyutu\n        self.transforms = transforms  # Veri artırma işlemleri (opsiyonel)\n        self.shuffle = shuffle  # Veriyi karıştırma\n        self.indices = np.arange(len(self.images))  # Başlangıçta bütün indeksleri tutar\n        self.on_epoch_end()  # Epok sonrasında veriyi karıştırma\n\n\n    def __len__(self):\n        return int(np.ceil(len(self.images) / self.batch_size))\n        \n    def __getitem__(self, index):\n        # Used to get a batch\n        batch_indices = self.indices[index * self.batch_size:(index + 1) * self.batch_size]\n        batch_images = [self.images[i] for i in batch_indices]\n        batch_labels = [self.labels[i] for i in batch_indices]\n\n        # Data augmentation\n        if self.transforms:\n            batch_images = [self.augment_image(img) for img in batch_images]\n        else:\n            batch_images = [self.no_augment_image(img) for img in batch_images]\n\n        return np.array(batch_images), np.array(batch_labels)\n\n    def augment_image(self, image_path):\n        image = Image.open(image_path)\n        image = image.resize((150,150))\n        image = np.array(image)\n        image = image.astype(np.uint8)\n        \n        augmented = self.transforms(image=image)\n        return augmented['image']/255.0\n\n    def no_augment_image(self,image_path):\n        image = Image.open(image_path)\n        image = image.resize((150,150))\n        image = np.array(image)\n        image = image.astype(np.uint8)/255.0\n        return image\n    \n    def on_epoch_end(self):\n        self.indices = np.arange(len(self.images))\n        if self.shuffle:\n            np.random.shuffle(self.indices)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:17:59.972957Z","iopub.execute_input":"2024-12-30T13:17:59.973354Z","iopub.status.idle":"2024-12-30T13:17:59.983854Z","shell.execute_reply.started":"2024-12-30T13:17:59.973319Z","shell.execute_reply":"2024-12-30T13:17:59.982564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch_size = 32\ntrain_generator = CustomDataGenerator(X_train, Y_train, batch_size, transforms=train_transforms)\nval_generator = CustomDataGenerator(X_val, Y_val, batch_size, transforms=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:18:01.520627Z","iopub.execute_input":"2024-12-30T13:18:01.521012Z","iopub.status.idle":"2024-12-30T13:18:01.528414Z","shell.execute_reply.started":"2024-12-30T13:18:01.520977Z","shell.execute_reply":"2024-12-30T13:18:01.526869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_generator) #625*32 = 20k","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T11:10:35.288450Z","iopub.execute_input":"2024-12-29T11:10:35.288769Z","iopub.status.idle":"2024-12-29T11:10:35.293961Z","shell.execute_reply.started":"2024-12-29T11:10:35.288741Z","shell.execute_reply":"2024-12-29T11:10:35.293210Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"you can not see the image you will use this generated image in fittin prosses.","metadata":{}},{"cell_type":"markdown","source":"# Fit the  model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', \n                                            patience=3, #kacinci epochstan sonra azaltilmaya baslayacak\n                                            verbose=1, \n                                            factor=0.5, #ne kadar azaltilacak.\n                                            min_lr=0.00001) #nereye kadar azaltilabilir\n# Callbacks\ncallbacks = [\n    EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True),\n    ModelCheckpoint('best_model.keras', save_best_only=True, monitor='val_loss'),\n    learning_rate_reduction\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T11:10:37.538631Z","iopub.execute_input":"2024-12-29T11:10:37.538931Z","iopub.status.idle":"2024-12-29T11:10:37.543602Z","shell.execute_reply.started":"2024-12-29T11:10:37.538909Z","shell.execute_reply":"2024-12-29T11:10:37.542697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    train_generator, \n    validation_data=val_generator,\n    epochs=50,\n    callbacks=callbacks\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T11:10:37.905572Z","iopub.execute_input":"2024-12-29T11:10:37.905816Z","iopub.status.idle":"2024-12-29T13:35:40.224554Z","shell.execute_reply.started":"2024-12-29T11:10:37.905791Z","shell.execute_reply":"2024-12-29T13:35:40.223761Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model = load_model('/kaggle/input/cat-and-dog_model/keras/default/1/my_model.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:20:53.200661Z","iopub.execute_input":"2024-12-30T13:20:53.201036Z","iopub.status.idle":"2024-12-30T13:20:54.605570Z","shell.execute_reply.started":"2024-12-30T13:20:53.201005Z","shell.execute_reply":"2024-12-30T13:20:54.604410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_model_history(modelHistory, model_name):\n    history=pd.DataFrame()\n    history[\"Train Loss\"]=modelHistory.history['loss']\n    history[\"Validatin Loss\"]=modelHistory.history['val_loss']\n    history[\"Train Accuracy\"]=modelHistory.history['accuracy']\n    history[\"Validatin Accuracy\"]=modelHistory.history['val_accuracy']\n  \n    history.plot(figsize=(12,8))\n    plt.title(\" Convulutional Model: {} Train and Validation Loss and Accuracy History\".format(model_name))\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:39:41.862593Z","iopub.execute_input":"2024-12-30T09:39:41.862875Z","iopub.status.idle":"2024-12-30T09:39:41.867367Z","shell.execute_reply.started":"2024-12-30T09:39:41.862854Z","shell.execute_reply":"2024-12-30T09:39:41.866449Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model evaluation on test data set","metadata":{}},{"cell_type":"code","source":"test = df_test['full_path'] #to change the type of test dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:48.103657Z","iopub.execute_input":"2024-12-30T09:33:48.103964Z","iopub.status.idle":"2024-12-30T09:33:48.107918Z","shell.execute_reply.started":"2024-12-30T09:33:48.103939Z","shell.execute_reply":"2024-12-30T09:33:48.106966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess_image(image_path, target_size=(150, 150)):  \n    img = load_img(image_path, target_size=target_size)  \n    img_array = img_to_array(img)  \n    img_array = img_array / 255.0\n    return img_array\n\ntest_images = np.array([preprocess_image(img_path) for img_path in test])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:53:00.657670Z","iopub.execute_input":"2024-12-30T09:53:00.658013Z","iopub.status.idle":"2024-12-30T09:53:17.406897Z","shell.execute_reply.started":"2024-12-30T09:53:00.657980Z","shell.execute_reply":"2024-12-30T09:53:17.406105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def no_augment_image(image_path):\n        image = Image.open(image_path)\n        image = image.resize((150,150))\n        image = np.array(image)\n        image = image.astype(np.uint8)/255.0\n        return image\n\n\ndef batch_test_data_generator(image_paths, batch_size):\n    batch = []\n    for img_path in image_paths:\n        batch.append(no_augment_image(img_path))\n        if len(batch) == batch_size:\n            yield np.array(batch)\n            batch = []\n    if batch:\n        yield np.array(batch)\n\n\npredictions = []\nbatch_size = 32\ntest_batch_generator = batch_test_data_generator(test, batch_size)\n\nfor test_batch in test_batch_generator:\n    preds = model.predict(test_batch)\n    predictions.extend(preds)\n\npredictions = np.array(predictions)\npredicted_classes = (predictions > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:40:12.554706Z","iopub.execute_input":"2024-12-30T09:40:12.554990Z","iopub.status.idle":"2024-12-30T09:41:23.923250Z","shell.execute_reply.started":"2024-12-30T09:40:12.554970Z","shell.execute_reply":"2024-12-30T09:41:23.922546Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Predictions (probabilities):\\n\", predictions[:10])\nprint(\"Predicted classes:\\n\", predicted_classes[:10])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:41:23.924516Z","iopub.execute_input":"2024-12-30T09:41:23.924793Z","iopub.status.idle":"2024-12-30T09:41:23.929897Z","shell.execute_reply.started":"2024-12-30T09:41:23.924770Z","shell.execute_reply":"2024-12-30T09:41:23.929218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"package_size = math.ceil(12500/32)\npackage_size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:09:10.846969Z","iopub.execute_input":"2024-12-29T14:09:10.847247Z","iopub.status.idle":"2024-12-29T14:09:10.852679Z","shell.execute_reply.started":"2024-12-29T14:09:10.847226Z","shell.execute_reply":"2024-12-29T14:09:10.851927Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training and validation curves","metadata":{}},{"cell_type":"code","source":"show_model_history(history,'Start CNN with CAT&DOG Classification')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:11:36.235427Z","iopub.execute_input":"2024-12-29T14:11:36.235735Z","iopub.status.idle":"2024-12-29T14:11:36.535239Z","shell.execute_reply.started":"2024-12-29T14:11:36.235712Z","shell.execute_reply":"2024-12-29T14:11:36.534474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Eğitim ve doğrulama kaybını görselleştir\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Model Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n\n# Eğitim ve doğrulama doğruluğunu görselleştir (varsa)\nif 'accuracy' in history.history:\n    plt.plot(history.history['accuracy'], label='Training Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title('Model Accuracy')\n    plt.xlabel('Epochs')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:11:42.656381Z","iopub.execute_input":"2024-12-29T14:11:42.656668Z","iopub.status.idle":"2024-12-29T14:11:43.135555Z","shell.execute_reply.started":"2024-12-29T14:11:42.656647Z","shell.execute_reply":"2024-12-29T14:11:43.134825Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Karışıklık Matrisi (Confusion Matrix)\nEğer sınıflandırma problemi çözüyorsanız, karışıklık matrisiyle daha detaylı bir analiz yapabilirsiniz.","metadata":{}},{"cell_type":"code","source":"val_gen = CustomDataGenerator(X_val, Y_val, 50, transforms=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:18:30.232549Z","iopub.execute_input":"2024-12-30T13:18:30.232882Z","iopub.status.idle":"2024-12-30T13:18:30.237570Z","shell.execute_reply.started":"2024-12-30T13:18:30.232856Z","shell.execute_reply":"2024-12-30T13:18:30.236315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:34:40.081740Z","iopub.execute_input":"2024-12-30T13:34:40.082141Z","iopub.status.idle":"2024-12-30T13:34:40.090322Z","shell.execute_reply.started":"2024-12-30T13:34:40.082106Z","shell.execute_reply":"2024-12-30T13:34:40.089226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:35:27.108096Z","iopub.execute_input":"2024-12-30T13:35:27.108509Z","iopub.status.idle":"2024-12-30T13:35:27.116651Z","shell.execute_reply.started":"2024-12-30T13:35:27.108466Z","shell.execute_reply":"2024-12-30T13:35:27.115425Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Error Analysis\nYou can ensure poor retention of the model by examining examples that cannot be predicted correctly.\n\n**0 = Cat**\n\n**1 = Dog**","metadata":{}},{"cell_type":"code","source":"wrong_predictions = []\nbatch_size = 32\n\n\nfor batch_images, batch_target in val_gen:\n    if batch_images.shape[0] == 0:\n        print(\"if there isn't any data then break\")\n        break\n    \n    preds = model.predict(batch_images)\n    y_pred_classes = (preds > 0.5).astype(int)\n    y_true = batch_target\n\n    for i in range(len(y_true)):\n        if y_true[i] != y_pred_classes[i]:  # Eğer doğru tahmin edilmediyse\n            wrong_predictions.append({\n                'image': batch_images[i],  # Yanlış tahmin edilen görsel\n                'true_label': y_true[i],\n                'predicted_label': y_pred_classes[i]\n            })\n\n# Yanlış tahmin edilen ilk 5 örneği görselleştirmek\nfor i, wrong in enumerate(wrong_predictions[:5]):\n    print(f\"Örnek {i+1}:\")\n    print(f\"Gerçek Etiket: {wrong['true_label']}\")\n    print(f\"Tahmin Edilen Etiket: {wrong['predicted_label']}\")\n    plt.imshow(wrong['image'])\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T13:30:41.656563Z","iopub.execute_input":"2024-12-30T13:30:41.656967Z","iopub.status.idle":"2024-12-30T13:32:40.877304Z","shell.execute_reply.started":"2024-12-30T13:30:41.656934Z","shell.execute_reply":"2024-12-30T13:32:40.876049Z"},"scrolled":true},"outputs":[],"execution_count":null}]}