{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"},"accelerator":"TPU","kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nimport os\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"id":"yZn2dzGbQV_f","execution":{"iopub.status.busy":"2024-03-10T14:17:21.044744Z","iopub.execute_input":"2024-03-10T14:17:21.045156Z","iopub.status.idle":"2024-03-10T14:17:40.620891Z","shell.execute_reply.started":"2024-03-10T14:17:21.045120Z","shell.execute_reply":"2024-03-10T14:17:40.619951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"/kaggle/input/dog-breed-identification/train\"\ntest_dir = \"/kaggle/input/dog-breed-identification/test\"","metadata":{"id":"xO-RSvjaRFnX","execution":{"iopub.status.busy":"2024-03-10T14:18:38.108378Z","iopub.execute_input":"2024-03-10T14:18:38.109322Z","iopub.status.idle":"2024-03-10T14:18:38.113106Z","shell.execute_reply.started":"2024-03-10T14:18:38.109291Z","shell.execute_reply":"2024-03-10T14:18:38.112266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img = [os.path.join(train_dir, f) for f in os.listdir(train_dir)]\ntest_img = [os.path.join(test_dir, f) for f in os.listdir(test_dir)]","metadata":{"id":"eu2sVOJkRPhh","execution":{"iopub.status.busy":"2024-03-10T14:18:39.598264Z","iopub.execute_input":"2024-03-10T14:18:39.598574Z","iopub.status.idle":"2024-03-10T14:18:40.210762Z","shell.execute_reply.started":"2024-03-10T14:18:39.598551Z","shell.execute_reply":"2024-03-10T14:18:40.210013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\ndf_train.head()\nlen(df_train.breed.unique())","metadata":{"id":"mg456qv-R3cY","outputId":"95a42d35-b844-472d-c020-d6ee24c9ddb6","execution":{"iopub.status.busy":"2024-03-10T14:18:42.063258Z","iopub.execute_input":"2024-03-10T14:18:42.063929Z","iopub.status.idle":"2024-03-10T14:18:42.108514Z","shell.execute_reply.started":"2024-03-10T14:18:42.063897Z","shell.execute_reply":"2024-03-10T14:18:42.107622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.rename(columns = {\"breed\": \"label\"}, inplace = True)\ndf_train[\"id\"] = df_train[\"id\"].apply(lambda x: x+\".\"+\"jpg\")\ndf_train.head()\n","metadata":{"id":"dpcQmnWFR-Pe","outputId":"1ba00fb8-a30b-46b9-b83b-1fbff55b9bea","execution":{"iopub.status.busy":"2024-03-10T14:18:42.370350Z","iopub.execute_input":"2024-03-10T14:18:42.370617Z","iopub.status.idle":"2024-03-10T14:18:42.391261Z","shell.execute_reply.started":"2024-03-10T14:18:42.370596Z","shell.execute_reply":"2024-03-10T14:18:42.390279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = os.listdir(test_dir)\ndf_test = pd.DataFrame({\"id\": test_files, \"label\": \"dingo\"})\ndf_test.head()","metadata":{"id":"tehJv4D3SHqS","outputId":"cd19248e-7dc7-4fb7-82f2-f39625c70b86","execution":{"iopub.status.busy":"2024-03-10T14:18:44.779725Z","iopub.execute_input":"2024-03-10T14:18:44.780107Z","iopub.status.idle":"2024-03-10T14:18:44.797304Z","shell.execute_reply.started":"2024-03-10T14:18:44.780079Z","shell.execute_reply":"2024-03-10T14:18:44.796317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_path = \"../input/dog-breed-identification/train\"\nsub_class = os.listdir(src_path)\n\nfig = plt.figure(figsize=(10,5))\nfor e in range(len(sub_class[:8])):\n    plt.subplot(2,4,e+1)\n    img = plt.imread(os.path.join(src_path,sub_class[e]))\n    plt.imshow(img, cmap=plt.get_cmap('gray'))\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:18:46.135241Z","iopub.execute_input":"2024-03-10T14:18:46.135570Z","iopub.status.idle":"2024-03-10T14:18:46.872568Z","shell.execute_reply.started":"2024-03-10T14:18:46.135543Z","shell.execute_reply":"2024-03-10T14:18:46.871633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = len(df_train)\ntest_size = len(df_test)\n\ntrain_size,test_size","metadata":{"id":"gDjtsZ96SJz8","outputId":"3ad351ea-134d-4abd-cbb7-f4a5654e554b","execution":{"iopub.status.busy":"2024-03-10T14:18:57.584913Z","iopub.execute_input":"2024-03-10T14:18:57.585671Z","iopub.status.idle":"2024-03-10T14:18:57.591367Z","shell.execute_reply.started":"2024-03-10T14:18:57.585643Z","shell.execute_reply":"2024-03-10T14:18:57.590428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n      rescale=1./255,\n      rotation_range=25,\n      width_shift_range=0.1,\n      height_shift_range=0.1,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True,\n      fill_mode='nearest',\n      validation_split = 0.2)","metadata":{"id":"D0GL5Gn1SdsG","execution":{"iopub.status.busy":"2024-03-10T14:20:05.350622Z","iopub.execute_input":"2024-03-10T14:20:05.351246Z","iopub.status.idle":"2024-03-10T14:20:05.356105Z","shell.execute_reply.started":"2024-03-10T14:20:05.351215Z","shell.execute_reply":"2024-03-10T14:20:05.355244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(df_train, train_dir,\n    x_col = 'id',\n    y_col = 'label',\n    subset = 'training',\n    color_mode = 'rgb',\n    class_mode = 'categorical',\n    target_size = (224, 224),\n    batch_size = 32,\n    shuffle = True,\n    seed = 1881)","metadata":{"id":"vIDpuXFISf-4","outputId":"35d97e34-e4f0-4d5d-a24c-8dd06bff40db","execution":{"iopub.status.busy":"2024-03-10T14:20:05.823478Z","iopub.execute_input":"2024-03-10T14:20:05.823835Z","iopub.status.idle":"2024-03-10T14:20:34.522331Z","shell.execute_reply.started":"2024-03-10T14:20:05.823807Z","shell.execute_reply":"2024-03-10T14:20:34.521448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = train_datagen.flow_from_dataframe(df_train, train_dir,\n    x_col = 'id',\n    y_col = 'label',\n    subset = 'validation',\n    color_mode = 'rgb',\n    class_mode = 'categorical',\n    target_size = (224, 224),\n    batch_size = 32,\n    shuffle = True,\n    seed = 1881)\n","metadata":{"id":"P2zm9WsJShPO","outputId":"70a7e6e0-873c-462d-b5a0-d0bf034d3846","execution":{"iopub.status.busy":"2024-03-10T14:20:34.523797Z","iopub.execute_input":"2024-03-10T14:20:34.524097Z","iopub.status.idle":"2024-03-10T14:20:45.041290Z","shell.execute_reply.started":"2024-03-10T14:20:34.524074Z","shell.execute_reply":"2024-03-10T14:20:45.040414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen=tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n\ntest_generator=test_datagen.flow_from_dataframe(\ndataframe=df_test,\ndirectory=test_dir,\nx_col=\"id\",\ny_col=None,\nbatch_size=32,\nseed=42,\nshuffle=False,\nclass_mode=None,\ntarget_size=(224,224),\ncolor_mode=\"rgb\")","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:21:19.364597Z","iopub.execute_input":"2024-03-10T14:21:19.365289Z","iopub.status.idle":"2024-03-10T14:21:48.660101Z","shell.execute_reply.started":"2024-03-10T14:21:19.365260Z","shell.execute_reply":"2024-03-10T14:21:48.659208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_model = tf.keras.applications.NASNetLarge(\n                  weights='imagenet',\n                  include_top=False,\n                  input_shape=(224, 224, 3)\n               )\nconv_model.trainable = False\nconv_model.summary()","metadata":{"id":"9lrHYWOZSKzu","outputId":"b37f1f29-c6a8-43bd-f11f-084a38695a8b","execution":{"iopub.status.busy":"2024-03-10T15:12:50.826642Z","iopub.execute_input":"2024-03-10T15:12:50.827674Z","iopub.status.idle":"2024-03-10T15:13:00.389025Z","shell.execute_reply.started":"2024-03-10T15:12:50.827632Z","shell.execute_reply":"2024-03-10T15:13:00.388105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential()\n\nmodel.add(conv_model)\n\nmodel.add(tf.keras.layers.GlobalAveragePooling2D())\n\nmodel.add(tf.keras.layers.Dropout(0.5))\n\nmodel.add(tf.keras.layers.Dense(120, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.SGD(learning_rate=1e-3),\n              metrics=['acc'])\n\nmodel.summary()","metadata":{"id":"BG6qVy6sSaMM","outputId":"ebd0168c-4b21-4ed0-f24c-5ef1d4d73670","execution":{"iopub.status.busy":"2024-03-10T15:15:11.081518Z","iopub.execute_input":"2024-03-10T15:15:11.081919Z","iopub.status.idle":"2024-03-10T15:15:11.160159Z","shell.execute_reply.started":"2024-03-10T15:15:11.081889Z","shell.execute_reply":"2024-03-10T15:15:11.159323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    tf.keras.callbacks.EarlyStopping(monitor = 'val_loss',\n                          min_delta = 0.001,\n                          patience = 10,\n                          verbose = 1,\n                          restore_best_weights = True)]","metadata":{"execution":{"iopub.status.busy":"2024-03-10T15:15:18.866428Z","iopub.execute_input":"2024-03-10T15:15:18.866781Z","iopub.status.idle":"2024-03-10T15:15:18.871476Z","shell.execute_reply.started":"2024-03-10T15:15:18.866742Z","shell.execute_reply":"2024-03-10T15:15:18.870531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs=50,\n    callbacks=callbacks,\n    validation_data=validation_generator,\n    batch_size=40)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T15:15:29.603068Z","iopub.execute_input":"2024-03-10T15:15:29.603893Z","iopub.status.idle":"2024-03-10T17:04:50.462911Z","shell.execute_reply.started":"2024-03-10T15:15:29.603862Z","shell.execute_reply":"2024-03-10T17:04:50.462071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_training_curves(training, validation, title, subplot):\n    if subplot%10==1: # set up the subplots on the first call\n        plt.subplots(figsize=(10,10), facecolor='#F0F0F0')\n        plt.tight_layout()\n    ax = plt.subplot(subplot)\n    ax.set_facecolor('#F8F8F8')\n    ax.plot(training)\n    ax.plot(validation)\n    ax.set_title('MODEL '+ title)\n    ax.set_ylabel(title)\n    ax.set_xlabel('epoch')\n    ax.legend(['train', 'valid.'])","metadata":{"execution":{"iopub.status.busy":"2024-03-10T17:04:51.898677Z","iopub.execute_input":"2024-03-10T17:04:51.898988Z","iopub.status.idle":"2024-03-10T17:04:51.905051Z","shell.execute_reply.started":"2024-03-10T17:04:51.898963Z","shell.execute_reply":"2024-03-10T17:04:51.904178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_training_curves(\n    history.history['loss'],\n    history.history['val_loss'],\n    'LOSS',\n    211,\n)\n\ndisplay_training_curves(\n    history.history['acc'],\n    history.history['val_acc'],\n    'ACCURACY',\n    212,\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T17:04:51.906317Z","iopub.execute_input":"2024-03-10T17:04:51.906652Z","iopub.status.idle":"2024-03-10T17:04:52.644204Z","shell.execute_reply.started":"2024-03-10T17:04:51.906619Z","shell.execute_reply":"2024-03-10T17:04:52.643217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(validation_generator,batch_size=32)\nprint(\"Accuracy: {:.2f}%\".format(score[1] * 100)) \nprint(\"Loss: \",score[0])","metadata":{"execution":{"iopub.status.busy":"2024-03-10T17:05:36.622192Z","iopub.execute_input":"2024-03-10T17:05:36.622541Z","iopub.status.idle":"2024-03-10T17:06:02.749027Z","shell.execute_reply.started":"2024-03-10T17:05:36.622516Z","shell.execute_reply":"2024-03-10T17:06:02.747984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T17:06:31.898366Z","iopub.execute_input":"2024-03-10T17:06:31.899081Z","iopub.status.idle":"2024-03-10T17:10:00.045670Z","shell.execute_reply.started":"2024-03-10T17:06:31.899051Z","shell.execute_reply":"2024-03-10T17:10:00.044911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T17:10:05.660331Z","iopub.execute_input":"2024-03-10T17:10:05.660660Z","iopub.status.idle":"2024-03-10T17:10:06.356132Z","shell.execute_reply.started":"2024-03-10T17:10:05.660635Z","shell.execute_reply":"2024-03-10T17:10:06.355165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.iloc[:,1:] = pred\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T17:10:07.785181Z","iopub.execute_input":"2024-03-10T17:10:07.785834Z","iopub.status.idle":"2024-03-10T17:10:07.815245Z","shell.execute_reply.started":"2024-03-10T17:10:07.785788Z","shell.execute_reply":"2024-03-10T17:10:07.814387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = df_submission.set_index('id')\nfinal_df.to_csv('Submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-10T17:10:09.203894Z","iopub.execute_input":"2024-03-10T17:10:09.204610Z","iopub.status.idle":"2024-03-10T17:10:11.945681Z","shell.execute_reply.started":"2024-03-10T17:10:09.204580Z","shell.execute_reply":"2024-03-10T17:10:11.944850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}