{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":31011,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os, cv2, random\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom tqdm import tqdm\nfrom random import shuffle \nfrom IPython.display import SVG\nfrom tensorflow.keras.utils import model_to_dot, plot_model\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D\n%matplotlib inline\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:20.919455Z","iopub.execute_input":"2025-05-11T20:10:20.920065Z","iopub.status.idle":"2025-05-11T20:10:20.925886Z","shell.execute_reply.started":"2025-05-11T20:10:20.920035Z","shell.execute_reply":"2025-05-11T20:10:20.925168Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TEST_SIZE = 0.5\nRANDOM_STATE = 2018\nBATCH_SIZE = 64\nNO_EPOCHS = 20\nNUM_CLASSES = 2\nSAMPLE_SIZE = 20000\nPATH = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/'\nTRAIN_FOLDER = './train/'\nTEST_FOLDER =  './test/'\nIMG_SIZE = 224\nRESNET_WEIGHTS_PATH = '/kaggle/input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:20.95828Z","iopub.execute_input":"2025-05-11T20:10:20.958452Z","iopub.status.idle":"2025-05-11T20:10:20.962633Z","shell.execute_reply.started":"2025-05-11T20:10:20.958439Z","shell.execute_reply":"2025-05-11T20:10:20.961853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_path = os.path.join(PATH, \"train.zip\")\ntest_image_path = os.path.join(PATH, \"test.zip\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:20.971526Z","iopub.execute_input":"2025-05-11T20:10:20.971975Z","iopub.status.idle":"2025-05-11T20:10:20.976516Z","shell.execute_reply.started":"2025-05-11T20:10:20.971951Z","shell.execute_reply":"2025-05-11T20:10:20.975952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(train_image_path,\"r\") as z:\n    z.extractall(\".\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:22.930881Z","iopub.execute_input":"2025-05-11T20:10:22.931151Z","iopub.status.idle":"2025-05-11T20:10:29.731358Z","shell.execute_reply.started":"2025-05-11T20:10:22.931129Z","shell.execute_reply":"2025-05-11T20:10:29.730578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with zipfile.ZipFile(test_image_path,\"r\") as z:\n    z.extractall(\".\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:29.733273Z","iopub.execute_input":"2025-05-11T20:10:29.73383Z","iopub.status.idle":"2025-05-11T20:10:32.98827Z","shell.execute_reply.started":"2025-05-11T20:10:29.733809Z","shell.execute_reply":"2025-05-11T20:10:32.98746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_list = os.listdir(\"./train/\")[0:SAMPLE_SIZE]\ntest_image_list = os.listdir(\"./test/\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:32.98913Z","iopub.execute_input":"2025-05-11T20:10:32.989354Z","iopub.status.idle":"2025-05-11T20:10:33.01285Z","shell.execute_reply.started":"2025-05-11T20:10:32.989339Z","shell.execute_reply":"2025-05-11T20:10:33.012161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def label_pet_image_one_hot_encoder(img):\n    pet = img.split('.')[-3]\n    if pet == 'cat': return [1,0]\n    elif pet == 'dog': return [0,1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:33.01442Z","iopub.execute_input":"2025-05-11T20:10:33.014632Z","iopub.status.idle":"2025-05-11T20:10:33.018321Z","shell.execute_reply.started":"2025-05-11T20:10:33.014616Z","shell.execute_reply":"2025-05-11T20:10:33.017695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def process_data(data_image_list, DATA_FOLDER, isTrain=True):\n    data_df = []\n    for img in tqdm(data_image_list):\n        path = os.path.join(DATA_FOLDER,img)\n        if(isTrain):\n            label = label_pet_image_one_hot_encoder(img)\n        else:\n            label = img.split('.')[0]\n        img = cv2.imread(path,cv2.IMREAD_COLOR)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n        data_df.append([np.array(img),np.array(label)])\n    shuffle(data_df)\n    return data_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:33.018997Z","iopub.execute_input":"2025-05-11T20:10:33.019218Z","iopub.status.idle":"2025-05-11T20:10:33.033137Z","shell.execute_reply.started":"2025-05-11T20:10:33.019167Z","shell.execute_reply":"2025-05-11T20:10:33.032506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_image_list_count(data_image_list):\n    labels = []\n    for img in data_image_list:\n        labels.append(img.split('.')[-3])\n    sns.countplot(x=labels)\n    plt.xlabel(\"Labels\")\n    plt.title(\"Cats and Dogs\")\n    plt.show()\n\nplot_image_list_count(train_image_list)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:33.033778Z","iopub.execute_input":"2025-05-11T20:10:33.033963Z","iopub.status.idle":"2025-05-11T20:10:33.194346Z","shell.execute_reply.started":"2025-05-11T20:10:33.033942Z","shell.execute_reply":"2025-05-11T20:10:33.193592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_image_list_count(os.listdir(TRAIN_FOLDER))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:33.195109Z","iopub.execute_input":"2025-05-11T20:10:33.195294Z","iopub.status.idle":"2025-05-11T20:10:33.365922Z","shell.execute_reply.started":"2025-05-11T20:10:33.19528Z","shell.execute_reply":"2025-05-11T20:10:33.365227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = process_data(train_image_list, TRAIN_FOLDER)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:33.366686Z","iopub.execute_input":"2025-05-11T20:10:33.366944Z","iopub.status.idle":"2025-05-11T20:10:52.819912Z","shell.execute_reply.started":"2025-05-11T20:10:33.366917Z","shell.execute_reply":"2025-05-11T20:10:52.819137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_images(data, isTest=False):\n    f, ax = plt.subplots(5,5, figsize=(15,15))\n    for i,data in enumerate(data[:25]):\n        img_num = data[1]\n        img_data = data[0]\n        label = np.argmax(img_num)\n        if label  == 1: \n            str_label='Dog'\n        elif label == 0: \n            str_label='Cat'\n        if(isTest):\n            str_label=\"None\"\n        ax[i//5, i%5].imshow(img_data)\n        ax[i//5, i%5].axis('off')\n        ax[i//5, i%5].set_title(\"Label: {}\".format(str_label))\n    plt.show()\n\nshow_images(train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:52.82102Z","iopub.execute_input":"2025-05-11T20:10:52.821323Z","iopub.status.idle":"2025-05-11T20:10:54.554387Z","shell.execute_reply.started":"2025-05-11T20:10:52.821297Z","shell.execute_reply":"2025-05-11T20:10:54.553574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = process_data(test_image_list, TEST_FOLDER, False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:10:54.556589Z","iopub.execute_input":"2025-05-11T20:10:54.556813Z","iopub.status.idle":"2025-05-11T20:11:06.619089Z","shell.execute_reply.started":"2025-05-11T20:10:54.556796Z","shell.execute_reply":"2025-05-11T20:11:06.618518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_images(test,True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:06.620002Z","iopub.execute_input":"2025-05-11T20:11:06.620271Z","iopub.status.idle":"2025-05-11T20:11:08.34867Z","shell.execute_reply.started":"2025-05-11T20:11:06.620249Z","shell.execute_reply":"2025-05-11T20:11:08.347575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)\ny = np.array([i[1] for i in train])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:08.349697Z","iopub.execute_input":"2025-05-11T20:11:08.350024Z","iopub.status.idle":"2025-05-11T20:11:09.507461Z","shell.execute_reply.started":"2025-05-11T20:11:08.349995Z","shell.execute_reply":"2025-05-11T20:11:09.506678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\nmodel.add(ResNet50(include_top=False, pooling='max', weights='imagenet'))\nmodel.add(Dense(NUM_CLASSES, activation='softmax'))\nmodel.layers[0].trainable = True\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:09.508341Z","iopub.execute_input":"2025-05-11T20:11:09.508622Z","iopub.status.idle":"2025-05-11T20:11:11.149006Z","shell.execute_reply.started":"2025-05-11T20:11:09.508599Z","shell.execute_reply":"2025-05-11T20:11:11.14845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:11.149614Z","iopub.execute_input":"2025-05-11T20:11:11.14981Z","iopub.status.idle":"2025-05-11T20:11:11.157798Z","shell.execute_reply.started":"2025-05-11T20:11:11.149794Z","shell.execute_reply":"2025-05-11T20:11:11.156993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:11.158441Z","iopub.execute_input":"2025-05-11T20:11:11.1589Z","iopub.status.idle":"2025-05-11T20:11:11.183864Z","shell.execute_reply.started":"2025-05-11T20:11:11.158884Z","shell.execute_reply":"2025-05-11T20:11:11.183221Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_model(model, to_file='model.png')\nSVG(model_to_dot(model).create(prog='dot', format='svg'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:11.184618Z","iopub.execute_input":"2025-05-11T20:11:11.184857Z","iopub.status.idle":"2025-05-11T20:11:11.249008Z","shell.execute_reply.started":"2025-05-11T20:11:11.184834Z","shell.execute_reply":"2025-05-11T20:11:11.248564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=TEST_SIZE, random_state=RANDOM_STATE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:11.249819Z","iopub.execute_input":"2025-05-11T20:11:11.250061Z","iopub.status.idle":"2025-05-11T20:11:12.180134Z","shell.execute_reply.started":"2025-05-11T20:11:11.250045Z","shell.execute_reply":"2025-05-11T20:11:12.179566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_model = model.fit(X_train, y_train,\n                  batch_size=BATCH_SIZE,\n                  epochs=NO_EPOCHS,\n                  verbose=1,\n                  validation_data=(X_val, y_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:11:12.180727Z","iopub.execute_input":"2025-05-11T20:11:12.180925Z","iopub.status.idle":"2025-05-11T20:50:04.565023Z","shell.execute_reply.started":"2025-05-11T20:11:12.18091Z","shell.execute_reply":"2025-05-11T20:50:04.564184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_accuracy_and_loss(train_model):\n    hist = train_model.history\n    acc = hist['accuracy']\n    val_acc = hist['val_accuracy']\n    loss = hist['loss']\n    val_loss = hist['val_loss']\n    epochs = range(len(acc))\n    f, ax = plt.subplots(1,2, figsize=(14,6))\n    ax[0].plot(epochs, acc, 'g', label='Training accuracy')\n    ax[0].plot(epochs, val_acc, 'r', label='Validation accuracy')\n    ax[0].set_title('Training and validation accuracy')\n    ax[0].legend()\n    ax[1].plot(epochs, loss, 'g', label='Training loss')\n    ax[1].plot(epochs, val_loss, 'r', label='Validation loss')\n    ax[1].set_title('Training and validation loss')\n    ax[1].legend()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.568667Z","iopub.execute_input":"2025-05-11T20:50:04.56894Z","iopub.status.idle":"2025-05-11T20:50:04.574468Z","shell.execute_reply.started":"2025-05-11T20:50:04.568923Z","shell.execute_reply":"2025-05-11T20:50:04.57372Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score = model.evaluate(validation_generator, verbose=1)\nfor metric, value in zip(model.metrics_names, score):\n    print(f\"{metric}: {value:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.575255Z","iopub.execute_input":"2025-05-11T20:50:04.57549Z","iopub.status.idle":"2025-05-11T20:50:04.602117Z","shell.execute_reply.started":"2025-05-11T20:50:04.575466Z","shell.execute_reply":"2025-05-11T20:50:04.601275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get predictions as probabilities\npredictions = model.predict(X_val)\n\n# Convert predicted probabilities to class indices\npredicted_classes = np.argmax(predictions, axis=1)\n\n# Get true class indices\ny_true = np.argmax(y_val, axis=1)\n\nfrom sklearn.metrics import confusion_matrix\n\ncm = confusion_matrix(y_true, predicted_classes)\nprint(cm)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.602685Z","iopub.status.idle":"2025-05-11T20:50:04.602981Z","shell.execute_reply.started":"2025-05-11T20:50:04.602835Z","shell.execute_reply":"2025-05-11T20:50:04.602851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Indices of correct predictions\ncorrect_indices = np.where(predicted_classes == y_true)[0]\n\n# Indices of incorrect predictions\nincorrect_indices = np.where(predicted_classes != y_true)[0]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.604278Z","iopub.status.idle":"2025-05-11T20:50:04.604524Z","shell.execute_reply.started":"2025-05-11T20:50:04.604409Z","shell.execute_reply":"2025-05-11T20:50:04.604422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_names = [\"Class {}:\".format(i) for i in range(NUM_CLASSES)]\nprint(classification_report(y_true, predicted_classes, target_names=target_names))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.605569Z","iopub.status.idle":"2025-05-11T20:50:04.605795Z","shell.execute_reply.started":"2025-05-11T20:50:04.605691Z","shell.execute_reply":"2025-05-11T20:50:04.6057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f, ax = plt.subplots(5, 5, figsize=(15, 15))\n\nfor i, data in enumerate(test[:25]):\n    img_data = data[0]\n    orig = img_data\n    \n    # تجهيز البيانات بشكل صحيح\n    data = np.expand_dims(img_data, axis=0)\n    \n    # توقع النتيجة\n    model_out = model.predict(data, verbose=0)[0]\n    predicted_class = np.argmax(model_out)\n    \n    # تحديد اسم الكلاس\n    if predicted_class == 1:\n        str_predicted = 'Dog'\n    else:\n        str_predicted = 'Cat'\n    \n    ax[i // 5, i % 5].imshow(orig)\n    ax[i // 5, i % 5].axis('off')\n    ax[i // 5, i % 5].set_title(\"Predicted: {}\".format(str_predicted))\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.606381Z","iopub.status.idle":"2025-05-11T20:50:04.606792Z","shell.execute_reply.started":"2025-05-11T20:50:04.606647Z","shell.execute_reply":"2025-05-11T20:50:04.60666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_list = []\nimg_list = []\n\nfor img in tqdm(test):\n    img_data = img[0]\n    img_idx = img[1]\n    data = np.expand_dims(img_data, axis=0)\n    predicted = model.predict(data, verbose=0)[0]\n    img_list.append(img_idx)\n    pred_list.append(np.argmax(predicted))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.607852Z","iopub.status.idle":"2025-05-11T20:50:04.608056Z","shell.execute_reply.started":"2025-05-11T20:50:04.607957Z","shell.execute_reply":"2025-05-11T20:50:04.607966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({'id':img_list , 'label':pred_list})\nsubmission.head()\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T20:50:04.609676Z","iopub.status.idle":"2025-05-11T20:50:04.610022Z","shell.execute_reply.started":"2025-05-11T20:50:04.609843Z","shell.execute_reply":"2025-05-11T20:50:04.60986Z"}},"outputs":[],"execution_count":null}]}