{"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":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":4893068,"sourceType":"datasetVersion","datasetId":2837405}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-15T03:21:33.455705Z","iopub.execute_input":"2024-01-15T03:21:33.456056Z","iopub.status.idle":"2024-01-15T03:21:33.461856Z","shell.execute_reply.started":"2024-01-15T03:21:33.456025Z","shell.execute_reply":"2024-01-15T03:21:33.460676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.preprocessing import LabelEncoder\n\n\n\nimport numpy as np\nfrom tensorflow.keras.applications.mobilenet import preprocess_input\n\nfrom wordcloud import WordCloud\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\n\nfrom tensorflow.keras.applications import ResNet50V2\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.layers import BatchNormalization, GlobalAveragePooling2D, Dropout, Dense\nfrom tensorflow.keras.models import Model\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:33.464224Z","iopub.execute_input":"2024-01-15T03:21:33.464547Z","iopub.status.idle":"2024-01-15T03:21:33.480587Z","shell.execute_reply.started":"2024-01-15T03:21:33.464518Z","shell.execute_reply":"2024-01-15T03:21:33.479042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df =  pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ntrain = '/kaggle/input/dog-breed-identification/train'\ntest =  '/kaggle/input/dog-breed-identification/test'","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:33.482524Z","iopub.execute_input":"2024-01-15T03:21:33.482795Z","iopub.status.idle":"2024-01-15T03:21:33.505359Z","shell.execute_reply.started":"2024-01-15T03:21:33.482772Z","shell.execute_reply":"2024-01-15T03:21:33.503557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:33.507508Z","iopub.execute_input":"2024-01-15T03:21:33.50832Z","iopub.status.idle":"2024-01-15T03:21:33.519748Z","shell.execute_reply.started":"2024-01-15T03:21:33.508279Z","shell.execute_reply":"2024-01-15T03:21:33.518822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:33.522325Z","iopub.execute_input":"2024-01-15T03:21:33.522632Z","iopub.status.idle":"2024-01-15T03:21:33.53494Z","shell.execute_reply.started":"2024-01-15T03:21:33.522606Z","shell.execute_reply":"2024-01-15T03:21:33.533984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(50,40))\nsns.countplot(y=\"breed\",data=df,palette=\"Set1\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:33.536046Z","iopub.execute_input":"2024-01-15T03:21:33.536326Z","iopub.status.idle":"2024-01-15T03:21:35.319032Z","shell.execute_reply.started":"2024-01-15T03:21:33.536303Z","shell.execute_reply":"2024-01-15T03:21:35.318309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(10222/120)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:35.320307Z","iopub.execute_input":"2024-01-15T03:21:35.320699Z","iopub.status.idle":"2024-01-15T03:21:35.325348Z","shell.execute_reply.started":"2024-01-15T03:21:35.320674Z","shell.execute_reply":"2024-01-15T03:21:35.324448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total number of unique Dog Breeds :\",len(df.breed.unique()))","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:35.326245Z","iopub.execute_input":"2024-01-15T03:21:35.32671Z","iopub.status.idle":"2024-01-15T03:21:35.34009Z","shell.execute_reply.started":"2024-01-15T03:21:35.326685Z","shell.execute_reply":"2024-01-15T03:21:35.338796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Isi dari TRAIN","metadata":{}},{"cell_type":"code","source":"nrow=5\nncol=5\nfig,ax=plt.subplots(nrow,ncol,figsize=(10,10))\nfor i,(img_id,breed) in enumerate(df[[\"id\",\"breed\"]].values[: nrow*ncol]):\n    image_path=os.path.join(train,img_id+\".jpg\")\n    image=cv2.imread(image_path)\n    image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n    row=i//ncol\n    col=i%ncol\n    ax[row,col].imshow(image)\n    ax[row,col].set_title(breed)\n    ax[row,col].axis(\"off\")\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:35.341546Z","iopub.execute_input":"2024-01-15T03:21:35.341812Z","iopub.status.idle":"2024-01-15T03:21:37.539788Z","shell.execute_reply.started":"2024-01-15T03:21:35.341788Z","shell.execute_reply":"2024-01-15T03:21:37.538961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_breed = 120\nimage_size = 200\nbatch_size = 32\nencoder = LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:37.540903Z","iopub.execute_input":"2024-01-15T03:21:37.542216Z","iopub.status.idle":"2024-01-15T03:21:37.548226Z","shell.execute_reply.started":"2024-01-15T03:21:37.542152Z","shell.execute_reply":"2024-01-15T03:21:37.546724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breed=list(df[\"breed\"].value_counts().keys())\nnew=sorted(breed,reverse=True)[: num_breed]\ndf=df.query(\"breed in @new\")\ndf","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:37.550087Z","iopub.execute_input":"2024-01-15T03:21:37.550341Z","iopub.status.idle":"2024-01-15T03:21:37.57163Z","shell.execute_reply.started":"2024-01-15T03:21:37.550317Z","shell.execute_reply":"2024-01-15T03:21:37.570678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['image_file'] = df['id'].apply(lambda x: x + \".jpg\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:37.57504Z","iopub.execute_input":"2024-01-15T03:21:37.575285Z","iopub.status.idle":"2024-01-15T03:21:37.587082Z","shell.execute_reply.started":"2024-01-15T03:21:37.575263Z","shell.execute_reply":"2024-01-15T03:21:37.586386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom tensorflow.keras.applications.mobilenet import preprocess_input","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:21:37.587984Z","iopub.execute_input":"2024-01-15T03:21:37.589182Z","iopub.status.idle":"2024-01-15T03:21:37.596302Z","shell.execute_reply.started":"2024-01-15T03:21:37.589106Z","shell.execute_reply":"2024-01-15T03:21:37.595457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input\n\ntrain_data=np.zeros((len(df),image_size,image_size,3),dtype=\"float32\")\n\nfor i,img_id in enumerate(df[\"image_file\"]):\n    print(f\"Reading image: {img_id}\")\n    \n    img_path = os.path.join(train, img_id)\n    \n    if not os.path.exists(img_path):\n        print(f\"Image not found: {img_path}\")\n        continue\n    \n    img=cv2.resize(cv2.imread(img_path,cv2.IMREAD_UNCHANGED),((image_size,image_size)))\n    \n    if not np.any(img):\n        print(f\"Empty image: {img_path}\")\n        continue\n    \n    img_array=preprocess_input(np.expand_dims(np.array(img[...,: : -1].astype(np.float32)).copy(),axis=0))\n    train_data[i]=img_array","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2024-01-15T03:21:37.597785Z","iopub.execute_input":"2024-01-15T03:21:37.598108Z","iopub.status.idle":"2024-01-15T03:22:34.490201Z","shell.execute_reply.started":"2024-01-15T03:21:37.598082Z","shell.execute_reply":"2024-01-15T03:22:34.488737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[i]","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:22:34.491761Z","iopub.execute_input":"2024-01-15T03:22:34.49213Z","iopub.status.idle":"2024-01-15T03:22:34.501928Z","shell.execute_reply.started":"2024-01-15T03:22:34.492102Z","shell.execute_reply":"2024-01-15T03:22:34.500613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lab = df[\"breed\"].unique().tolist()\nprint(lab)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:22:34.503685Z","iopub.execute_input":"2024-01-15T03:22:34.504053Z","iopub.status.idle":"2024-01-15T03:22:34.531977Z","shell.execute_reply.started":"2024-01-15T03:22:34.504001Z","shell.execute_reply":"2024-01-15T03:22:34.53109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from wordcloud import WordCloud\ntext = ' '.join(lab)\nwordcloud = WordCloud(width=800, height=400, background_color='white',colormap=\"rainbow\").generate(text)\nplt.figure(figsize=(15, 10))\nplt.imshow(wordcloud, interpolation='bilinear')\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:22:34.533167Z","iopub.execute_input":"2024-01-15T03:22:34.533529Z","iopub.status.idle":"2024-01-15T03:22:35.386544Z","shell.execute_reply.started":"2024-01-15T03:22:34.533501Z","shell.execute_reply":"2024-01-15T03:22:35.385386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_data = encoder.fit_transform(df[\"breed\"].values)\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:22:35.387764Z","iopub.execute_input":"2024-01-15T03:22:35.38812Z","iopub.status.idle":"2024-01-15T03:22:35.396382Z","shell.execute_reply.started":"2024-01-15T03:22:35.388078Z","shell.execute_reply":"2024-01-15T03:22:35.394913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(train_data,label_data,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:22:35.398109Z","iopub.execute_input":"2024-01-15T03:22:35.398493Z","iopub.status.idle":"2024-01-15T03:22:36.107536Z","shell.execute_reply.started":"2024-01-15T03:22:35.398464Z","shell.execute_reply":"2024-01-15T03:22:36.106369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(rotation_range=45,\n                                   width_shift_range=0.2,\n                                   height_shift_range=0.2,\n                                   shear_range=0.2,\n                                   zoom_range=0.25,\n                                   horizontal_flip=True,\n                                   fill_mode='nearest')\n\ntrain_generator = train_datagen.flow(x_train, \n                                     y_train, \n                                     batch_size=batch_size)\n\ntest_datagen = ImageDataGenerator()\n \ntest_generator = test_datagen.flow(x_test, \n                                   y_test, \n                                   batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:22:36.108787Z","iopub.execute_input":"2024-01-15T03:22:36.109067Z","iopub.status.idle":"2024-01-15T03:22:36.11599Z","shell.execute_reply.started":"2024-01-15T03:22:36.109044Z","shell.execute_reply":"2024-01-15T03:22:36.115057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet = ResNet50V2(input_shape = [image_size,image_size,3], weights='imagenet', include_top=False)\nfor layer in resnet.layers:\n    layer.trainable = False\nx = resnet.output\nx = BatchNormalization()(x)\nx = GlobalAveragePooling2D()(x)\n# x = Dropout(0.2)(x)\nx = Dense(1024, activation='relu')(x)\nx = Dense(512, activation='relu')(x)\n# x = Dropout(0.5)(x)\nx = Dense(256, activation='relu')(x)\nx = Dropout(0.2)(x)\nprediction = Dense(num_breed, activation='softmax')(x)\nmodel = Model(inputs=resnet.input, outputs=prediction)\n\nlearning_rate = 1e-3\noptimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)\nmodel.compile(optimizer=optimizer,loss='sparse_categorical_crossentropy',metrics=[\"accuracy\"])\nmodel.summary()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-01-15T03:22:36.117431Z","iopub.execute_input":"2024-01-15T03:22:36.118049Z","iopub.status.idle":"2024-01-15T03:22:38.898545Z","shell.execute_reply.started":"2024-01-15T03:22:36.117999Z","shell.execute_reply":"2024-01-15T03:22:38.897593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,steps_per_epoch= x_train.shape[0] // batch_size,epochs=15,\n                 validation_data= test_generator,\n                 validation_steps= x_test.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T03:22:38.900062Z","iopub.execute_input":"2024-01-15T03:22:38.900386Z","iopub.status.idle":"2024-01-15T05:36:01.178374Z","shell.execute_reply.started":"2024-01-15T03:22:38.900359Z","shell.execute_reply":"2024-01-15T05:36:01.175162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:55:29.146495Z","iopub.execute_input":"2024-01-15T05:55:29.146985Z","iopub.status.idle":"2024-01-15T05:55:29.156814Z","shell.execute_reply.started":"2024-01-15T05:55:29.14695Z","shell.execute_reply":"2024-01-15T05:55:29.154496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,5))\nplt.plot(history.history['accuracy'],label=\"Train_Accuracy\",color=\"blue\",marker=\"*\")\nplt.plot(history.history['val_accuracy'],label=\"Validation_Accuracy\",color=\"red\",marker=\"P\")\nplt.grid(True)\nplt.legend()\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:55:32.014337Z","iopub.execute_input":"2024-01-15T05:55:32.015602Z","iopub.status.idle":"2024-01-15T05:55:32.267453Z","shell.execute_reply.started":"2024-01-15T05:55:32.015525Z","shell.execute_reply":"2024-01-15T05:55:32.265135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,5))\nplt.plot(history.history['loss'],color=\"green\",marker=\"D\")\nplt.plot(history.history['val_loss'],color=\"navy\",marker=\"H\")\nplt.grid(True)\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['Train_Loss', 'Validation_Loss'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:55:36.124853Z","iopub.execute_input":"2024-01-15T05:55:36.125454Z","iopub.status.idle":"2024-01-15T05:55:36.390199Z","shell.execute_reply.started":"2024-01-15T05:55:36.125397Z","shell.execute_reply":"2024-01-15T05:55:36.388879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre=model.predict(x_test)\npredict=np.argmax(pre,axis=1)\n\nfrom sklearn.metrics import confusion_matrix, classification_report\nplt.figure(figsize=(50,30))\ncf=confusion_matrix(y_test,predict)\nsns.heatmap(cf,annot=True,fmt=\"d\",cmap=\"jet\",lw=4,linecolor=\"black\",xticklabels=lab,yticklabels=lab)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:55:40.570341Z","iopub.execute_input":"2024-01-15T05:55:40.570832Z","iopub.status.idle":"2024-01-15T05:58:28.438718Z","shell.execute_reply.started":"2024-01-15T05:55:40.570798Z","shell.execute_reply":"2024-01-15T05:58:28.436926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,predict,target_names=lab))","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:58:28.474606Z","iopub.execute_input":"2024-01-15T05:58:28.474947Z","iopub.status.idle":"2024-01-15T05:58:28.494207Z","shell.execute_reply.started":"2024-01-15T05:58:28.47492Z","shell.execute_reply":"2024-01-15T05:58:28.493254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import requests\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image, ImageDraw, ImageFont\nfrom io import BytesIO\nimport tensorflow as tf\n\n\nimage_url = \"https://images.ctfassets.net/sfnkq8lmu5d7/2gSHy100UdEdOiUne89849/0e93f7efd56a73fb773970dbe2c4f5c7/shutterstock_1793062390.jpg?w=1000&h=750&fl=progressive&q=70&fm=jpg\"\n# Load the image from the URL\nresponse = requests.get(image_url)\nimage_path = Image.open(BytesIO(response.content))\nimage_resized_rgb = image_path.resize((224, 224)).convert('RGB')\nimage_array_rgb = np.array(image_resized_rgb)\nimage_scale_rgb = image_array_rgb.astype(\"float32\") / 255.0\nimage_reshape_rgb = image_scale_rgb.reshape(1, 224, 224, 3)  \nfinal_pred = model.predict(image_reshape_rgb)[0]\nmax_prob = np.argmax(final_pred)\nresults = lab  \nmy_predict = results[max_prob]\nmyFont = ImageFont.truetype('/kaggle/input/hack-font/Hack-Bold.ttf',8)\nimage_draw = ImageDraw.Draw(image_resized_rgb)\nimage_draw.text((10, 10), f\"Predicted class: {my_predict}\", font=myFont, fill=(255, 20, 30))\nimage_resized_rgb.save(\"my_image.png\")\nplt.figure(figsize=(8, 8))\nplt.imshow(image_resized_rgb)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:58:28.496413Z","iopub.execute_input":"2024-01-15T05:58:28.496681Z","iopub.status.idle":"2024-01-15T05:58:29.17143Z","shell.execute_reply.started":"2024-01-15T05:58:28.496657Z","shell.execute_reply":"2024-01-15T05:58:29.169589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\nwith open(\"modelidentifikasi.pkl\", \"wb\") as save:\n    pickle.dump(history, save)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:58:29.173354Z","iopub.execute_input":"2024-01-15T05:58:29.173752Z","iopub.status.idle":"2024-01-15T05:58:30.22155Z","shell.execute_reply.started":"2024-01-15T05:58:29.173719Z","shell.execute_reply":"2024-01-15T05:58:30.220581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"model.pkl\", \"rb\") as save:\n    model = pickle.load(save)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T05:58:30.276141Z","iopub.status.idle":"2024-01-15T05:58:30.276616Z","shell.execute_reply.started":"2024-01-15T05:58:30.276398Z","shell.execute_reply":"2024-01-15T05:58:30.27642Z"},"trusted":true},"execution_count":null,"outputs":[]}]}