{"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-09T03:11:49.241263Z","iopub.execute_input":"2024-01-09T03:11:49.241634Z","iopub.status.idle":"2024-01-09T03:11:49.834989Z","shell.execute_reply.started":"2024-01-09T03:11:49.2416Z","shell.execute_reply":"2024-01-09T03:11:49.833682Z"},"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-09T03:11:49.837444Z","iopub.execute_input":"2024-01-09T03:11:49.838266Z","iopub.status.idle":"2024-01-09T03:12:05.140338Z","shell.execute_reply.started":"2024-01-09T03:11:49.838218Z","shell.execute_reply":"2024-01-09T03:12:05.139484Z"},"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-09T03:12:05.141431Z","iopub.execute_input":"2024-01-09T03:12:05.142397Z","iopub.status.idle":"2024-01-09T03:12:05.181935Z","shell.execute_reply.started":"2024-01-09T03:12:05.142362Z","shell.execute_reply":"2024-01-09T03:12:05.180725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T03:12:05.187071Z","iopub.execute_input":"2024-01-09T03:12:05.187452Z","iopub.status.idle":"2024-01-09T03:12:05.225162Z","shell.execute_reply.started":"2024-01-09T03:12:05.187415Z","shell.execute_reply":"2024-01-09T03:12:05.224021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T03:12:05.226832Z","iopub.execute_input":"2024-01-09T03:12:05.227137Z","iopub.status.idle":"2024-01-09T03:12:05.244161Z","shell.execute_reply.started":"2024-01-09T03:12:05.227109Z","shell.execute_reply":"2024-01-09T03:12:05.242933Z"},"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-09T03:12:05.245943Z","iopub.execute_input":"2024-01-09T03:12:05.246574Z","iopub.status.idle":"2024-01-09T03:12:07.623189Z","shell.execute_reply.started":"2024-01-09T03:12:05.246518Z","shell.execute_reply":"2024-01-09T03:12:07.621889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(10222/120)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T06:28:00.704393Z","iopub.execute_input":"2024-01-09T06:28:00.70486Z","iopub.status.idle":"2024-01-09T06:28:00.712047Z","shell.execute_reply.started":"2024-01-09T06:28:00.704821Z","shell.execute_reply":"2024-01-09T06:28:00.710763Z"},"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-09T03:12:07.62506Z","iopub.execute_input":"2024-01-09T03:12:07.625926Z","iopub.status.idle":"2024-01-09T03:12:07.63346Z","shell.execute_reply.started":"2024-01-09T03:12:07.62588Z","shell.execute_reply":"2024-01-09T03:12:07.632409Z"},"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-09T03:12:07.635039Z","iopub.execute_input":"2024-01-09T03:12:07.635359Z","iopub.status.idle":"2024-01-09T03:12:11.004285Z","shell.execute_reply.started":"2024-01-09T03:12:07.63533Z","shell.execute_reply":"2024-01-09T03:12:11.003237Z"},"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-09T03:12:11.005589Z","iopub.execute_input":"2024-01-09T03:12:11.006734Z","iopub.status.idle":"2024-01-09T03:12:11.011865Z","shell.execute_reply.started":"2024-01-09T03:12:11.006695Z","shell.execute_reply":"2024-01-09T03:12:11.010529Z"},"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-09T03:12:11.017786Z","iopub.execute_input":"2024-01-09T03:12:11.018253Z","iopub.status.idle":"2024-01-09T03:12:11.04759Z","shell.execute_reply.started":"2024-01-09T03:12:11.018212Z","shell.execute_reply":"2024-01-09T03:12:11.046438Z"},"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-09T03:12:11.049182Z","iopub.execute_input":"2024-01-09T03:12:11.050109Z","iopub.status.idle":"2024-01-09T03:12:11.06881Z","shell.execute_reply.started":"2024-01-09T03:12:11.050071Z","shell.execute_reply":"2024-01-09T03:12:11.067876Z"},"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-09T03:12:11.069976Z","iopub.execute_input":"2024-01-09T03:12:11.071067Z","iopub.status.idle":"2024-01-09T03:12:11.076167Z","shell.execute_reply.started":"2024-01-09T03:12:11.071027Z","shell.execute_reply":"2024-01-09T03:12:11.075317Z"},"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":{"execution":{"iopub.status.busy":"2024-01-09T03:12:11.077638Z","iopub.execute_input":"2024-01-09T03:12:11.077966Z","iopub.status.idle":"2024-01-09T03:15:32.385963Z","shell.execute_reply.started":"2024-01-09T03:12:11.077936Z","shell.execute_reply":"2024-01-09T03:15:32.384982Z"},"_kg_hide-input":false,"_kg_hide-output":true,"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[i]","metadata":{"execution":{"iopub.status.busy":"2024-01-09T03:15:32.387118Z","iopub.execute_input":"2024-01-09T03:15:32.387417Z","iopub.status.idle":"2024-01-09T03:15:32.396828Z","shell.execute_reply.started":"2024-01-09T03:15:32.387389Z","shell.execute_reply":"2024-01-09T03:15:32.395728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lab = df[\"breed\"].unique().tolist()\nprint(lab)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T03:15:32.398489Z","iopub.execute_input":"2024-01-09T03:15:32.398929Z","iopub.status.idle":"2024-01-09T03:15:32.413854Z","shell.execute_reply.started":"2024-01-09T03:15:32.398898Z","shell.execute_reply":"2024-01-09T03:15:32.412554Z"},"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-09T03:15:32.415209Z","iopub.execute_input":"2024-01-09T03:15:32.415771Z","iopub.status.idle":"2024-01-09T03:15:33.511767Z","shell.execute_reply.started":"2024-01-09T03:15:32.415736Z","shell.execute_reply":"2024-01-09T03:15:33.510633Z"},"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-09T03:15:33.513113Z","iopub.execute_input":"2024-01-09T03:15:33.513459Z","iopub.status.idle":"2024-01-09T03:15:33.642818Z","shell.execute_reply.started":"2024-01-09T03:15:33.513428Z","shell.execute_reply":"2024-01-09T03:15:33.641828Z"},"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-09T03:15:33.64405Z","iopub.execute_input":"2024-01-09T03:15:33.644369Z","iopub.status.idle":"2024-01-09T03:15:35.613392Z","shell.execute_reply.started":"2024-01-09T03:15:33.64434Z","shell.execute_reply":"2024-01-09T03:15:35.612486Z"},"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-09T03:15:35.614692Z","iopub.execute_input":"2024-01-09T03:15:35.615211Z","iopub.status.idle":"2024-01-09T03:15:35.621484Z","shell.execute_reply.started":"2024-01-09T03:15:35.61518Z","shell.execute_reply":"2024-01-09T03:15:35.620698Z"},"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":{"execution":{"iopub.status.busy":"2024-01-09T03:15:35.62277Z","iopub.execute_input":"2024-01-09T03:15:35.623257Z","iopub.status.idle":"2024-01-09T03:15:46.555145Z","shell.execute_reply.started":"2024-01-09T03:15:35.623226Z","shell.execute_reply":"2024-01-09T03:15:46.554285Z"},"scrolled":true,"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)n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-09T03:15:46.556398Z","iopub.execute_input":"2024-01-09T03:15:46.55677Z","iopub.status.idle":"2024-01-09T06:19:18.966011Z","shell.execute_reply.started":"2024-01-09T03:15:46.556737Z","shell.execute_reply":"2024-01-09T06:19:18.964114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate","metadata":{"execution":{"iopub.status.busy":"2024-01-09T06:42:18.618897Z","iopub.execute_input":"2024-01-09T06:42:18.619305Z","iopub.status.idle":"2024-01-09T06:42:18.628515Z","shell.execute_reply.started":"2024-01-09T06:42:18.619272Z","shell.execute_reply":"2024-01-09T06:42:18.626748Z"},"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-09T06:19:18.970818Z","iopub.execute_input":"2024-01-09T06:19:18.971172Z","iopub.status.idle":"2024-01-09T06:19:19.301629Z","shell.execute_reply.started":"2024-01-09T06:19:18.97114Z","shell.execute_reply":"2024-01-09T06:19:19.300754Z"},"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-09T06:19:19.302899Z","iopub.execute_input":"2024-01-09T06:19:19.303915Z","iopub.status.idle":"2024-01-09T06:19:19.670676Z","shell.execute_reply.started":"2024-01-09T06:19:19.303869Z","shell.execute_reply":"2024-01-09T06:19:19.669835Z"},"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-09T06:19:19.671957Z","iopub.execute_input":"2024-01-09T06:19:19.672493Z","iopub.status.idle":"2024-01-09T06:22:05.744082Z","shell.execute_reply.started":"2024-01-09T06:19:19.67246Z","shell.execute_reply":"2024-01-09T06:22:05.742909Z"},"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-09T06:22:05.745787Z","iopub.execute_input":"2024-01-09T06:22:05.74629Z","iopub.status.idle":"2024-01-09T06:22:05.776378Z","shell.execute_reply.started":"2024-01-09T06:22:05.746249Z","shell.execute_reply":"2024-01-09T06:22:05.775293Z"},"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-09T07:06:08.306326Z","iopub.execute_input":"2024-01-09T07:06:08.306808Z","iopub.status.idle":"2024-01-09T07:06:08.483511Z","shell.execute_reply.started":"2024-01-09T07:06:08.306768Z","shell.execute_reply":"2024-01-09T07:06:08.48207Z"},"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-09T07:02:22.610451Z","iopub.execute_input":"2024-01-09T07:02:22.611283Z","iopub.status.idle":"2024-01-09T07:02:24.06953Z","shell.execute_reply.started":"2024-01-09T07:02:22.611235Z","shell.execute_reply":"2024-01-09T07:02:24.068238Z"},"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-09T07:02:25.868903Z","iopub.execute_input":"2024-01-09T07:02:25.869331Z","iopub.status.idle":"2024-01-09T07:02:50.230065Z","shell.execute_reply.started":"2024-01-09T07:02:25.869292Z","shell.execute_reply":"2024-01-09T07:02:50.228893Z"},"trusted":true},"execution_count":null,"outputs":[]}]}