{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":38284,"databundleVersionId":4155799,"sourceType":"competition"},{"sourceId":12160,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":9864}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Libraries","metadata":{"execution":{"iopub.status.busy":"2024-02-27T16:46:24.218971Z","iopub.execute_input":"2024-02-27T16:46:24.220262Z","iopub.status.idle":"2024-02-27T16:46:24.224993Z","shell.execute_reply.started":"2024-02-27T16:46:24.220224Z","shell.execute_reply":"2024-02-27T16:46:24.223918Z"}}},{"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","execution":{"iopub.status.busy":"2024-02-27T17:01:33.954139Z","iopub.execute_input":"2024-02-27T17:01:33.954515Z","iopub.status.idle":"2024-02-27T17:01:34.591858Z","shell.execute_reply.started":"2024-02-27T17:01:33.954481Z","shell.execute_reply":"2024-02-27T17:01:34.590900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import *\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:34.593546Z","iopub.execute_input":"2024-02-27T17:01:34.594171Z","iopub.status.idle":"2024-02-27T17:01:34.600131Z","shell.execute_reply.started":"2024-02-27T17:01:34.594144Z","shell.execute_reply":"2024-02-27T17:01:34.599300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Data","metadata":{}},{"cell_type":"code","source":"train_ds=tf.keras.utils.image_dataset_from_directory('/kaggle/input/4-animal-classification/train',validation_split=0.2,subset=\"training\",seed=123)\ntrain_ds","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:34.601381Z","iopub.execute_input":"2024-02-27T17:01:34.602077Z","iopub.status.idle":"2024-02-27T17:01:37.131444Z","shell.execute_reply.started":"2024-02-27T17:01:34.602052Z","shell.execute_reply":"2024-02-27T17:01:37.130471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_ds=tf.keras.utils.image_dataset_from_directory('/kaggle/input/4-animal-classification/train',validation_split=0.2,subset=\"validation\",seed=123)\nvalid_ds","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:37.133910Z","iopub.execute_input":"2024-02-27T17:01:37.134221Z","iopub.status.idle":"2024-02-27T17:01:37.521311Z","shell.execute_reply.started":"2024-02-27T17:01:37.134197Z","shell.execute_reply":"2024-02-27T17:01:37.520420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nclass_names","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:37.522456Z","iopub.execute_input":"2024-02-27T17:01:37.522736Z","iopub.status.idle":"2024-02-27T17:01:37.528806Z","shell.execute_reply.started":"2024-02-27T17:01:37.522712Z","shell.execute_reply":"2024-02-27T17:01:37.527896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n  for i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n    plt.title(class_names[labels[i]])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:37.529698Z","iopub.execute_input":"2024-02-27T17:01:37.529945Z","iopub.status.idle":"2024-02-27T17:01:39.585969Z","shell.execute_reply.started":"2024-02-27T17:01:37.529922Z","shell.execute_reply":"2024-02-27T17:01:39.584757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n  print(image_batch.shape)\n  print(labels_batch.shape)\n  break","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:39.587364Z","iopub.execute_input":"2024-02-27T17:01:39.587645Z","iopub.status.idle":"2024-02-27T17:01:40.071154Z","shell.execute_reply.started":"2024-02-27T17:01:39.587620Z","shell.execute_reply":"2024-02-27T17:01:40.070165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\nvalid_ds = valid_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:40.072378Z","iopub.execute_input":"2024-02-27T17:01:40.072674Z","iopub.status.idle":"2024-02-27T17:01:40.087885Z","shell.execute_reply.started":"2024-02-27T17:01:40.072649Z","shell.execute_reply":"2024-02-27T17:01:40.086989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds=tf.keras.utils.image_dataset_from_directory('/kaggle/input/4-animal-classification/test')\ntest_ds.take(1)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:40.089928Z","iopub.execute_input":"2024-02-27T17:01:40.090189Z","iopub.status.idle":"2024-02-27T17:01:40.528426Z","shell.execute_reply.started":"2024-02-27T17:01:40.090167Z","shell.execute_reply":"2024-02-27T17:01:40.527509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading ResNet50 Model","metadata":{}},{"cell_type":"code","source":"base_model_1 = tf.keras.applications.ResNet50(input_shape=(256,256,3),\n                                               include_top=False,\n                                               weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:40.532382Z","iopub.execute_input":"2024-02-27T17:01:40.532665Z","iopub.status.idle":"2024-02-27T17:01:45.556753Z","shell.execute_reply.started":"2024-02-27T17:01:40.532640Z","shell.execute_reply":"2024-02-27T17:01:45.555856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model_1.trainable=False","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:45.558028Z","iopub.execute_input":"2024-02-27T17:01:45.558332Z","iopub.status.idle":"2024-02-27T17:01:45.569093Z","shell.execute_reply.started":"2024-02-27T17:01:45.558307Z","shell.execute_reply":"2024-02-27T17:01:45.568228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the Model for training","metadata":{}},{"cell_type":"code","source":"model_1 = keras.models.Sequential([\n    base_model_1,\n    GlobalAveragePooling2D(),\n    Dense(4,activation='softmax')\n])\nmodel_1.compile(optimizer='nadam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:45.570310Z","iopub.execute_input":"2024-02-27T17:01:45.570715Z","iopub.status.idle":"2024-02-27T17:01:46.118172Z","shell.execute_reply.started":"2024-02-27T17:01:45.570682Z","shell.execute_reply":"2024-02-27T17:01:46.117174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:01:46.119366Z","iopub.execute_input":"2024-02-27T17:01:46.119709Z","iopub.status.idle":"2024-02-27T17:01:46.156171Z","shell.execute_reply.started":"2024-02-27T17:01:46.119678Z","shell.execute_reply":"2024-02-27T17:01:46.155366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the Model","metadata":{}},{"cell_type":"code","source":"history_1=model_1.fit(train_ds,epochs=50,\n                  validation_data=valid_ds,\n                  callbacks=keras.callbacks.EarlyStopping(restore_best_weights=True,patience=5))","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:02:59.268582Z","iopub.execute_input":"2024-02-27T17:02:59.268950Z","iopub.status.idle":"2024-02-27T17:03:43.369723Z","shell.execute_reply.started":"2024-02-27T17:02:59.268921Z","shell.execute_reply":"2024-02-27T17:03:43.368703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing the loss and accuracy","metadata":{}},{"cell_type":"code","source":"history_df_1=pd.DataFrame(history_1.history)\n# plt.Figure(figsize=(10,20))\nfor i in range(2):\n    ax=plt.subplot(2,1,i+1)\n    history_df_1.iloc[2:,[i,i+2]].plot(ax=ax)\n    plt.title(f'training {history_df_1.columns[i]} vs {history_df_1.columns[i+2]}')\n    plt.tight_layout()\n# Resnet50-->GlobalPooling-->Dense(4)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:03:57.303149Z","iopub.execute_input":"2024-02-27T17:03:57.303540Z","iopub.status.idle":"2024-02-27T17:03:57.890113Z","shell.execute_reply.started":"2024-02-27T17:03:57.303511Z","shell.execute_reply":"2024-02-27T17:03:57.889289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.save('Resnet50_better_model_v1.h5')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:03:58.436002Z","iopub.execute_input":"2024-02-27T17:03:58.436668Z","iopub.status.idle":"2024-02-27T17:03:58.830303Z","shell.execute_reply.started":"2024-02-27T17:03:58.436635Z","shell.execute_reply":"2024-02-27T17:03:58.829265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\n\n# Assuming history is the history object returned by model.fit\nwith open('training_history_1.json', 'w') as file:\n    json.dump(history_1.history, file)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:04:03.767667Z","iopub.execute_input":"2024-02-27T17:04:03.768010Z","iopub.status.idle":"2024-02-27T17:04:03.773720Z","shell.execute_reply.started":"2024-02-27T17:04:03.767985Z","shell.execute_reply":"2024-02-27T17:04:03.772785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.evaluate(valid_ds)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:04:11.227919Z","iopub.execute_input":"2024-02-27T17:04:11.228297Z","iopub.status.idle":"2024-02-27T17:04:12.483355Z","shell.execute_reply.started":"2024-02-27T17:04:11.228244Z","shell.execute_reply":"2024-02-27T17:04:12.482462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting and Outputting the submissions","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img\nsub_csv ='../input/4-animal-classification/Sample_submission.csv'\npath_test = '../input/4-animal-classification/test/test'\ndf_sub = pd.read_csv(sub_csv)\nimage_id = df_sub['ID']\ndf_sub.head(10)\n# making a list for our predictions\nlabels = []\n\n\nfor i in image_id:\n    image = load_img(path_test+'/'+str(i)+'.jpg', target_size=(256, 256,3))\n    img = np.array(image)\n#     img = img / 255.0\n    img = img.reshape(1,256,256,3)\n    label = model_1.predict(img)\n    label_id = label[0].tolist()\n    \n    labels.append(label_id.index(max(label_id)))","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:05:11.545163Z","iopub.execute_input":"2024-02-27T17:05:11.546034Z","iopub.status.idle":"2024-02-27T17:06:06.487622Z","shell.execute_reply.started":"2024-02-27T17:05:11.546001Z","shell.execute_reply":"2024-02-27T17:06:06.486683Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub['Label'] = labels\ndf_sub.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:06:06.489188Z","iopub.execute_input":"2024-02-27T17:06:06.489503Z","iopub.status.idle":"2024-02-27T17:06:06.504634Z","shell.execute_reply.started":"2024-02-27T17:06:06.489477Z","shell.execute_reply":"2024-02-27T17:06:06.503757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('submission_file.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T17:07:14.651019Z","iopub.execute_input":"2024-02-27T17:07:14.651483Z","iopub.status.idle":"2024-02-27T17:07:14.660212Z","shell.execute_reply.started":"2024-02-27T17:07:14.651453Z","shell.execute_reply":"2024-02-27T17:07:14.659270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}