{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":9437789,"sourceType":"datasetVersion","datasetId":5734795},{"sourceId":9438005,"sourceType":"datasetVersion","datasetId":5734960},{"sourceId":9444588,"sourceType":"datasetVersion","datasetId":5739858}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1- Import Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport random as rnd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\n\nimport keras\nfrom keras.models import Sequential\nfrom keras.utils import to_categorical\nfrom keras.preprocessing.image import load_img\nfrom keras.layers import Flatten, Dense, Dropout\nfrom keras.layers import Conv2D, MaxPool2D, BatchNormalization, Activation\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nprint(os.listdir(\"/kaggle/input/pokemontrainer3\"))","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:47:54.438332Z","iopub.execute_input":"2024-09-20T17:47:54.439222Z","iopub.status.idle":"2024-09-20T17:47:54.493105Z","shell.execute_reply.started":"2024-09-20T17:47:54.439179Z","shell.execute_reply":"2024-09-20T17:47:54.492246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2- Data-Constants","metadata":{}},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_CHANNELS=3\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:47:58.189564Z","iopub.execute_input":"2024-09-20T17:47:58.190865Z","iopub.status.idle":"2024-09-20T17:47:58.195168Z","shell.execute_reply.started":"2024-09-20T17:47:58.190804Z","shell.execute_reply":"2024-09-20T17:47:58.194192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3- Load DataSet","metadata":{}},{"cell_type":"code","source":"# import zipfile\n\n# zip_files = ['test1', 'train']\n\n# for zip_file in zip_files:\n#     with zipfile.ZipFile(f\"../input/dogs-vs-cats/{zip_file}.zip\", \"r\") as z:\n#         z.extractall(\".\")\n#         print(f\"{zip_file} unzipped\")","metadata":{"execution":{"iopub.status.busy":"2024-09-20T16:07:16.259952Z","iopub.execute_input":"2024-09-20T16:07:16.260338Z","iopub.status.idle":"2024-09-20T16:07:16.265053Z","shell.execute_reply.started":"2024-09-20T16:07:16.260300Z","shell.execute_reply":"2024-09-20T16:07:16.263694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Extract Compressed Files","metadata":{}},{"cell_type":"markdown","source":"# 4- Data Preprocessing","metadata":{}},{"cell_type":"markdown","source":"> ## Label Encoding","metadata":{}},{"cell_type":"code","source":"train_images = os.listdir(\"/kaggle/input/pokemontrainer3\")\nCATEGORIES = []\n\nfor image_name in train_images:\n    class_name = image_name.split('.')[0]\n    \n    # Assign a numeric category for each Pokémon\n    if class_name == 'bulbasaur':\n        CATEGORIES.append(0)\n    elif class_name == 'charmander':\n        CATEGORIES.append(1)\n    elif class_name == 'pikachu':\n        CATEGORIES.append(2)\n    elif class_name == 'snorlax':\n        CATEGORIES.append(3)\n    elif class_name == 'squirtle':\n        CATEGORIES.append(4)\n    else:\n        CATEGORIES.append(-1)\n\ndf = pd.DataFrame({\"train_images\": train_images, \"CATEGORIES\": CATEGORIES})","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:02.125068Z","iopub.execute_input":"2024-09-20T17:48:02.125718Z","iopub.status.idle":"2024-09-20T17:48:02.134774Z","shell.execute_reply.started":"2024-09-20T17:48:02.125668Z","shell.execute_reply":"2024-09-20T17:48:02.133847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Show DataFrame","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:04.590203Z","iopub.execute_input":"2024-09-20T17:48:04.590625Z","iopub.status.idle":"2024-09-20T17:48:04.601451Z","shell.execute_reply.started":"2024-09-20T17:48:04.590585Z","shell.execute_reply":"2024-09-20T17:48:04.600567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:09.349902Z","iopub.execute_input":"2024-09-20T17:48:09.350343Z","iopub.status.idle":"2024-09-20T17:48:09.360522Z","shell.execute_reply.started":"2024-09-20T17:48:09.350305Z","shell.execute_reply":"2024-09-20T17:48:09.359132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:12.106618Z","iopub.execute_input":"2024-09-20T17:48:12.107072Z","iopub.status.idle":"2024-09-20T17:48:12.120932Z","shell.execute_reply.started":"2024-09-20T17:48:12.107033Z","shell.execute_reply":"2024-09-20T17:48:12.119714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:15.371723Z","iopub.execute_input":"2024-09-20T17:48:15.372615Z","iopub.status.idle":"2024-09-20T17:48:15.380568Z","shell.execute_reply.started":"2024-09-20T17:48:15.372570Z","shell.execute_reply":"2024-09-20T17:48:15.379514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Visualize The Data","metadata":{}},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:18.113773Z","iopub.execute_input":"2024-09-20T17:48:18.114739Z","iopub.status.idle":"2024-09-20T17:48:18.316498Z","shell.execute_reply.started":"2024-09-20T17:48:18.114687Z","shell.execute_reply":"2024-09-20T17:48:18.315546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## *From our data we have 12500 cats and 12500 dogs*","metadata":{}},{"cell_type":"markdown","source":"> ## Show Dimensions Of Images","metadata":{}},{"cell_type":"code","source":"shapes = []\n\nfor image in df[\"train_images\"]:\n    path = os.path.join(\"/kaggle/input/pokemontrainer3\",image)\n    image_array = plt.imread(path)\n    shapes.append(image_array.shape)\n    \nprint(pd.Series(shapes).value_counts())\n\nindex = np.argmin(shapes)\nprint(f\"\\nThe Minimum Dimension is ==> {shapes[index]}\\n\")","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:22.799997Z","iopub.execute_input":"2024-09-20T17:48:22.800887Z","iopub.status.idle":"2024-09-20T17:48:26.475963Z","shell.execute_reply.started":"2024-09-20T17:48:22.800842Z","shell.execute_reply":"2024-09-20T17:48:26.474940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **See sample image**\n","metadata":{}},{"cell_type":"code","source":"sample = rnd.choice(train_images)\nimage = load_img(os.path.join(\"/kaggle/input/pokemontrainer3\", sample))\nprint(type(image))\nplt.imshow(image)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:33.453949Z","iopub.execute_input":"2024-09-20T17:48:33.454937Z","iopub.status.idle":"2024-09-20T17:48:33.917557Z","shell.execute_reply.started":"2024-09-20T17:48:33.454881Z","shell.execute_reply":"2024-09-20T17:48:33.916626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Prepare data\n> Because we will use image genaretor with class_mode=\"categorical\". We need to convert column category into string. Then imagenerator will convert it one-hot encoding which is good for our classification.\n> So we will convert 1 to dog and 0 to cat","metadata":{}},{"cell_type":"code","source":"df[\"CATEGORIES\"] = df[\"CATEGORIES\"].replace({0: 'bulbasaur', 1: 'charmander', 2: 'pikachu', 3: 'snorlax',4: 'squirtle'})","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:37.660111Z","iopub.execute_input":"2024-09-20T17:48:37.660856Z","iopub.status.idle":"2024-09-20T17:48:37.667155Z","shell.execute_reply.started":"2024-09-20T17:48:37.660815Z","shell.execute_reply":"2024-09-20T17:48:37.666161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Split Data","metadata":{}},{"cell_type":"code","source":"train_df, valid_df = train_test_split(df, test_size=0.20, random_state=42)\n\ntrain_df = train_df.reset_index(drop=True)\nvalid_df = valid_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:40.511521Z","iopub.execute_input":"2024-09-20T17:48:40.512250Z","iopub.status.idle":"2024-09-20T17:48:40.520233Z","shell.execute_reply.started":"2024-09-20T17:48:40.512204Z","shell.execute_reply":"2024-09-20T17:48:40.519212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show tarin DataFrame","metadata":{}},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:43.609625Z","iopub.execute_input":"2024-09-20T17:48:43.610077Z","iopub.status.idle":"2024-09-20T17:48:43.618790Z","shell.execute_reply.started":"2024-09-20T17:48:43.610036Z","shell.execute_reply":"2024-09-20T17:48:43.617755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:46.532009Z","iopub.execute_input":"2024-09-20T17:48:46.532389Z","iopub.status.idle":"2024-09-20T17:48:46.730601Z","shell.execute_reply.started":"2024-09-20T17:48:46.532351Z","shell.execute_reply":"2024-09-20T17:48:46.729677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show valid DataFrame","metadata":{}},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:49.263430Z","iopub.execute_input":"2024-09-20T17:48:49.263864Z","iopub.status.idle":"2024-09-20T17:48:49.273459Z","shell.execute_reply.started":"2024-09-20T17:48:49.263821Z","shell.execute_reply":"2024-09-20T17:48:49.272269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:53.637816Z","iopub.execute_input":"2024-09-20T17:48:53.638370Z","iopub.status.idle":"2024-09-20T17:48:53.902584Z","shell.execute_reply.started":"2024-09-20T17:48:53.638329Z","shell.execute_reply":"2024-09-20T17:48:53.901669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Data Augmentation","metadata":{}},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = valid_df.shape[0]\nbatch_size=15","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:48:57.059993Z","iopub.execute_input":"2024-09-20T17:48:57.060733Z","iopub.status.idle":"2024-09-20T17:48:57.065264Z","shell.execute_reply.started":"2024-09-20T17:48:57.060672Z","shell.execute_reply":"2024-09-20T17:48:57.064203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Train Generator","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:49:00.509970Z","iopub.execute_input":"2024-09-20T17:49:00.510679Z","iopub.status.idle":"2024-09-20T17:49:00.520219Z","shell.execute_reply.started":"2024-09-20T17:49:00.510617Z","shell.execute_reply":"2024-09-20T17:49:00.519160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/input/pokemontrainer3\", \n    x_col='train_images',\n    y_col='CATEGORIES',\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:56:55.556052Z","iopub.execute_input":"2024-09-20T17:56:55.556428Z","iopub.status.idle":"2024-09-20T17:56:55.705364Z","shell.execute_reply.started":"2024-09-20T17:56:55.556392Z","shell.execute_reply":"2024-09-20T17:56:55.704382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Valid Generator","metadata":{}},{"cell_type":"code","source":"valid_datagen = ImageDataGenerator(rescale=1./255)\n\nvalid_generator = valid_datagen.flow_from_dataframe(\n    valid_df, \n    \"/kaggle/input/pokemontrainer3\", \n    x_col='train_images',\n    y_col='CATEGORIES',\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:58:38.792459Z","iopub.execute_input":"2024-09-20T17:58:38.793145Z","iopub.status.idle":"2024-09-20T17:58:38.803427Z","shell.execute_reply.started":"2024-09-20T17:58:38.793102Z","shell.execute_reply":"2024-09-20T17:58:38.802563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # See how our generator work","metadata":{}},{"cell_type":"code","source":"example_df = train_df.sample(n=1) # take 1 image-sample\nprint(example_df, \"\\n\")\n\nexample_df= example_df.reset_index(drop=True)\nprint(example_df, \"\\n\")\n\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n    \"/kaggle/input/pokemontrainer3\", \n    x_col='train_images',\n    y_col='CATEGORIES',\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:56:57.990095Z","iopub.execute_input":"2024-09-20T17:56:57.990522Z","iopub.status.idle":"2024-09-20T17:56:58.003779Z","shell.execute_reply.started":"2024-09-20T17:56:57.990481Z","shell.execute_reply":"2024-09-20T17:56:58.002791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show Generated Images from One-Image","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.axis('off')\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T17:57:00.867424Z","iopub.execute_input":"2024-09-20T17:57:00.868141Z","iopub.status.idle":"2024-09-20T17:57:02.177515Z","shell.execute_reply.started":"2024-09-20T17:57:00.868097Z","shell.execute_reply":"2024-09-20T17:57:02.176271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5- Build The Model","metadata":{}},{"cell_type":"markdown","source":"![image](https://i.imgur.com/ebkMGGu.jpg)","metadata":{}},{"cell_type":"markdown","source":"> * **Input Layer**: It represent input image data. It will reshape image into single diminsion array. Example your image is 64x64 = 4096, it will convert to (4096,1) array.\n> * **Conv Layer**: This layer will extract features from image.\n> * **Pooling Layer**: This layerreduce the spatial volume of input image after convolution.\n> * **Fully Connected Layer**: It connect the network from a layer to another layer\n> * **Output Layer**: It is the predicted values layer.","metadata":{}},{"cell_type":"markdown","source":"> # Fit Model","metadata":{}},{"cell_type":"code","source":"from keras.applications import MobileNet\nfrom keras.applications.mobilenet import preprocess_input\n\n# Load a pre-trained MobileNet model with imagenet weights\nbase_model = MobileNet(input_shape=(256, 256, 3), include_top=False, weights='imagenet')\n\n# Freeze the base model to retain pre-trained weights\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# Add custom layers\nmodel = Sequential([\n    base_model,\n    Flatten(),\n    Dense(512, activation='relu'),\n    Dropout(0.5),\n    Dense(5, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:29.587905Z","iopub.execute_input":"2024-09-20T19:05:29.588993Z","iopub.status.idle":"2024-09-20T19:05:30.059151Z","shell.execute_reply.started":"2024-09-20T19:05:29.588935Z","shell.execute_reply":"2024-09-20T19:05:30.058256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Callbacks","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:35.302591Z","iopub.execute_input":"2024-09-20T19:05:35.303349Z","iopub.status.idle":"2024-09-20T19:05:35.308388Z","shell.execute_reply.started":"2024-09-20T19:05:35.303306Z","shell.execute_reply":"2024-09-20T19:05:35.307296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ### Early Stop\n> To prevent over fitting we will stop the learning after 10 epochs and val_loss value not decreased","metadata":{}},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=100)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:37.485795Z","iopub.execute_input":"2024-09-20T19:05:37.486138Z","iopub.status.idle":"2024-09-20T19:05:37.490792Z","shell.execute_reply.started":"2024-09-20T19:05:37.486106Z","shell.execute_reply":"2024-09-20T19:05:37.489742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ### Learning Rate Reduction\n> We will reduce the learning rate when then accuracy not increase for 2 steps","metadata":{}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=100, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:39.061710Z","iopub.execute_input":"2024-09-20T19:05:39.062431Z","iopub.status.idle":"2024-09-20T19:05:39.067148Z","shell.execute_reply.started":"2024-09-20T19:05:39.062391Z","shell.execute_reply":"2024-09-20T19:05:39.066138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 3 if FAST_RUN else 10\n\nhistory = model.fit(train_generator,\n                    epochs=epochs,\n                    validation_data=valid_generator,\n#                     validation_steps=total_validate//batch_size, # 5000//15  = 333\n#                     steps_per_epoch=total_train//batch_size,     # 20000//15 = 1333\n                    callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:41.141921Z","iopub.execute_input":"2024-09-20T19:05:41.142935Z","iopub.status.idle":"2024-09-20T19:06:49.412855Z","shell.execute_reply.started":"2024-09-20T19:05:41.142888Z","shell.execute_reply":"2024-09-20T19:06:49.412079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Visualize The Performance","metadata":{}},{"cell_type":"code","source":"accuracy = history.history[\"accuracy\"]\nval_accuracy = history.history[\"val_accuracy\"]\n\nloss = history.history[\"loss\"]\nval_loss = history.history[\"val_loss\"]\n\nepochs = range(len(accuracy))\n\nplt.figure(figsize=(12,6))\n\nplt.subplot(1,2,1)\nplt.plot(epochs, accuracy, 'b', label=\"Train_Acc\")\nplt.plot(epochs, val_accuracy, 'r', label=\"val_Acc\")\nplt.legend(loc='best', shadow=True)\n\nplt.subplot(1,2,2)\nplt.plot(epochs, loss, 'b', label=\"Train_Loss\")\nplt.plot(epochs, val_loss, 'r', label=\"val_Loss\")\nplt.legend(loc='best', shadow=True)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:22.644394Z","iopub.execute_input":"2024-09-20T19:05:22.644785Z","iopub.status.idle":"2024-09-20T19:05:23.078864Z","shell.execute_reply.started":"2024-09-20T19:05:22.644748Z","shell.execute_reply":"2024-09-20T19:05:23.077921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # The Epochs = 10 Only. We Can Increase The number of Epochs to Reach Suitable Accuracy.","metadata":{}},{"cell_type":"markdown","source":"> # Prepare Testing Data","metadata":{}},{"cell_type":"code","source":"test_file_names = os.listdir(\"/kaggle/input/poketesting/test\")\ntest_df = pd.DataFrame({'file_name': test_file_names})\n\ntotal_testing = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-09-20T18:06:54.132216Z","iopub.execute_input":"2024-09-20T18:06:54.133134Z","iopub.status.idle":"2024-09-20T18:06:54.138438Z","shell.execute_reply.started":"2024-09-20T18:06:54.133090Z","shell.execute_reply":"2024-09-20T18:06:54.137463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Test The Model","metadata":{}},{"cell_type":"markdown","source":"> # Create Testing Generator","metadata":{}},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    dataframe=test_df,\n    directory=\"/kaggle/input/poketesting/test\",  # Correct directory path\n    x_col='file_name',\n    y_col=None,\n    class_mode=None,\n    target_size=(256, 256),  # Set this to the input size used during training\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:02.799606Z","iopub.execute_input":"2024-09-20T19:05:02.799999Z","iopub.status.idle":"2024-09-20T19:05:02.846289Z","shell.execute_reply.started":"2024-09-20T19:05:02.799961Z","shell.execute_reply":"2024-09-20T19:05:02.845437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Predict","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:05:11.235260Z","iopub.execute_input":"2024-09-20T19:05:11.235647Z","iopub.status.idle":"2024-09-20T19:05:12.367744Z","shell.execute_reply.started":"2024-09-20T19:05:11.235595Z","shell.execute_reply":"2024-09-20T19:05:12.366693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> For categoral classication the prediction will come with probability of each category. So we will pick the category that have the highest probability with numpy average max","metadata":{}},{"cell_type":"code","source":"test_df[\"CATEGORIES\"] = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-19T21:06:07.586971Z","iopub.execute_input":"2024-09-19T21:06:07.587609Z","iopub.status.idle":"2024-09-19T21:06:07.592553Z","shell.execute_reply.started":"2024-09-19T21:06:07.587570Z","shell.execute_reply":"2024-09-19T21:06:07.591423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> We will convert the predict category back into our generator classes by using train_generator.class_indices. It is the classes that image generator map while converting data into computer vision","metadata":{}},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['CATEGORIES'] = test_df['CATEGORIES'].replace({\n    'charmander': 1, \n    'bulbasaur': 0, \n    'pikachu': 2, \n    'snorlax': 3, \n    'squirtle': 4\n}).astype(int)  # Explicitly cast to int to avoid future warning","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:21:01.878693Z","iopub.execute_input":"2024-09-20T19:21:01.879118Z","iopub.status.idle":"2024-09-20T19:21:01.886093Z","shell.execute_reply.started":"2024-09-20T19:21:01.879081Z","shell.execute_reply":"2024-09-20T19:21:01.884811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> From our prepare data part. We map data with {1: 'dog', 0: 'cat'}. Now we will map the result back to dog is 1 and cat is 0\n\n","metadata":{}},{"cell_type":"code","source":"test_df['CATEGORIES'] = test_df['CATEGORIES'].replace({ 'charmander': 1, 'bulbasaur': 0 , 'pikachu': 2,'snorlax': 3,'squirtle': 4})","metadata":{"execution":{"iopub.status.busy":"2024-09-19T21:06:13.103296Z","iopub.execute_input":"2024-09-19T21:06:13.104045Z","iopub.status.idle":"2024-09-19T21:06:13.109171Z","shell.execute_reply.started":"2024-09-19T21:06:13.104004Z","shell.execute_reply":"2024-09-19T21:06:13.108237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6- Visualize The Result","metadata":{}},{"cell_type":"code","source":"test_df[\"CATEGORIES\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:21:05.155481Z","iopub.execute_input":"2024-09-20T19:21:05.156447Z","iopub.status.idle":"2024-09-20T19:21:05.346528Z","shell.execute_reply.started":"2024-09-20T19:21:05.156398Z","shell.execute_reply":"2024-09-20T19:21:05.345488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show Predicted Images","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import load_img\n\n# Sample test data to visualize\nsample_test = test_df.head(36)\n\nplt.figure(figsize=(15, 15))\n\nfor i, row in sample_test.iterrows():\n    file_name = row['file_name']\n    category = row['CATEGORIES']\n    path = os.path.join(\"/kaggle/input/poketesting/test\", file_name)\n    \n    img = load_img(path, target_size=IMAGE_SIZE)\n    \n    plt.subplot(6, 6, i+1)  # Create 6x6 grid\n    plt.imshow(img)\n    \n    # Display category based on the prediction\n    plt.title(f\"{file_name}\\n ==> {category}\")\n    plt.axis('off')\nlabel_map = dict((v, k) for k, v in train_generator.class_indices.items())\nprint(\"Label Mapping: \", label_map)\nplt.tight_layout()\nplt.show()\nprint(train_generator.class_indices)\n# Predictions will be integers that correspond to class indices\nprint(np.argmax(predictions, axis=1))\n\n# Check the mapping of predicted indices to Pokémon categories\nprint(label_map)  # This should match the class indices from trainingsample_test = test_df.head(36)\n\nplt.figure(figsize=(15, 15))\n\n\nmodel = Sequential([\n    # ... your layers ...\n    Dense(512, activation='relu'),\n    Dropout(rate=0.5),\n    Dense(5, activation='softmax')  # 5 classes for the 5 Pokémon categories\n])","metadata":{"execution":{"iopub.status.busy":"2024-09-20T19:21:08.250862Z","iopub.execute_input":"2024-09-20T19:21:08.251772Z","iopub.status.idle":"2024-09-20T19:21:11.997026Z","shell.execute_reply.started":"2024-09-20T19:21:08.251728Z","shell.execute_reply":"2024-09-20T19:21:11.995990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7- Save The Model","metadata":{}},{"cell_type":"code","source":"model.save(\"CNN.h5\")\nmodel.save_weights(\"CNN.weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-09-19T21:17:36.503760Z","iopub.execute_input":"2024-09-19T21:17:36.504141Z","iopub.status.idle":"2024-09-19T21:17:36.523414Z","shell.execute_reply.started":"2024-09-19T21:17:36.504105Z","shell.execute_reply":"2024-09-19T21:17:36.522511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8- Submission","metadata":{}},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['file_name'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['CATEGORIES']\nsubmission_df.drop(['file_name', 'CATEGORIES'], axis=1, inplace=True)\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-19T21:17:39.211652Z","iopub.execute_input":"2024-09-19T21:17:39.212061Z","iopub.status.idle":"2024-09-19T21:17:39.224376Z","shell.execute_reply.started":"2024-09-19T21:17:39.212026Z","shell.execute_reply":"2024-09-19T21:17:39.223424Z"},"trusted":true},"execution_count":null,"outputs":[]}]}