{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30512,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport PIL\nimport urllib.request\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport os\nos.chdir('/kaggle/input/')\nos.listdir()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-04-03T08:56:31.665431Z","iopub.execute_input":"2025-04-03T08:56:31.665644Z","iopub.status.idle":"2025-04-03T08:56:31.675882Z","shell.execute_reply.started":"2025-04-03T08:56:31.665622Z","shell.execute_reply":"2025-04-03T08:56:31.675253Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# tensorflow libraries/dependencies\nimport tensorflow as tf\nfrom tensorflow import keras\n## preprocessing\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img \n\n# models, layers, metrics, optimizers and callbacks\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Input, Dense, GlobalAveragePooling2D, AveragePooling2D, Dropout, Flatten\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\n\n## pre-trained models\nfrom tensorflow.keras.applications import InceptionResNetV2,InceptionV3\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2\nfrom tensorflow.keras.applications.xception import Xception\n\nprint(f\"Tensorflow Version: {tf.__version__}\")\nprint(f\"Number of GPUs Available: {len(tf.config.list_physical_devices('GPU'))}\")","metadata":{"execution":{"iopub.status.busy":"2025-04-03T08:56:41.715035Z","iopub.execute_input":"2025-04-03T08:56:41.715375Z","iopub.status.idle":"2025-04-03T08:56:49.018453Z","shell.execute_reply.started":"2025-04-03T08:56:41.715347Z","shell.execute_reply":"2025-04-03T08:56:49.017535Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Organization and Augmentation\n(Load image data and Create image data generator)","metadata":{}},{"cell_type":"code","source":"# set constants\nSIZE = (224,224)\nBATCH_SIZE = 42\nSEED = 42","metadata":{"execution":{"iopub.status.busy":"2025-04-03T08:57:57.078041Z","iopub.execute_input":"2025-04-03T08:57:57.078838Z","iopub.status.idle":"2025-04-03T08:57:57.082790Z","shell.execute_reply.started":"2025-04-03T08:57:57.078804Z","shell.execute_reply":"2025-04-03T08:57:57.082126Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nlabels['id'] = labels['id'].apply(lambda x: x + '.jpg')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2025-04-03T08:57:59.952990Z","iopub.execute_input":"2025-04-03T08:57:59.953335Z","iopub.status.idle":"2025-04-03T08:58:00.001643Z","shell.execute_reply.started":"2025-04-03T08:57:59.953306Z","shell.execute_reply":"2025-04-03T08:58:00.000746Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d = labels.breed.value_counts()\nplt.figure(figsize=(20,5))\nplt.bar(d.index,d.values,color=plt.cm.Paired([2]))\nplt.xticks(rotation=90, fontsize=9)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-04-03T08:58:03.203920Z","iopub.execute_input":"2025-04-03T08:58:03.204257Z","iopub.status.idle":"2025-04-03T08:58:04.224004Z","shell.execute_reply.started":"2025-04-03T08:58:03.204229Z","shell.execute_reply":"2025-04-03T08:58:04.223236Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Balanced enough dataset!","metadata":{}},{"cell_type":"code","source":"# ImageDatagenerator to load the images in batches and perform data augmentation\n\ndata_generator = ImageDataGenerator(rescale= 1./255, validation_split=0.3, rotation_range=20,\n                                    zoom_range=0.1, width_shift_range=0.2, height_shift_range=0.2,\n                                    shear_range=0.1, horizontal_flip=True, fill_mode=\"nearest\")\n\nINPUT_DIR = '/kaggle/input/dog-breed-identification/train'","metadata":{"execution":{"iopub.status.busy":"2025-04-03T08:58:10.902512Z","iopub.execute_input":"2025-04-03T08:58:10.902838Z","iopub.status.idle":"2025-04-03T08:58:10.907497Z","shell.execute_reply.started":"2025-04-03T08:58:10.902811Z","shell.execute_reply":"2025-04-03T08:58:10.906577Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = data_generator.flow_from_dataframe(dataframe=labels, directory=INPUT_DIR, x_col='id', y_col='breed', \n                                                target_size=SIZE, batch_size=BATCH_SIZE, seed=SEED, shuffle=True, \n                                                class_mode='categorical', subset='training', random_state=1)\n\nval_data = data_generator.flow_from_dataframe(dataframe=labels, directory=INPUT_DIR, x_col='id', y_col='breed', \n                                              target_size=SIZE, batch_size=BATCH_SIZE, seed=SEED, shuffle=True, \n                                              class_mode='categorical', subset='validation', random_state=1)","metadata":{"execution":{"iopub.status.busy":"2025-04-03T08:58:14.391799Z","iopub.execute_input":"2025-04-03T08:58:14.392432Z","iopub.status.idle":"2025-04-03T08:59:04.538509Z","shell.execute_reply.started":"2025-04-03T08:58:14.392403Z","shell.execute_reply":"2025-04-03T08:59:04.537753Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_mapper = np.asarray(list(train_data.class_indices.keys()))\nlabel_mapper","metadata":{"execution":{"iopub.status.busy":"2025-04-03T09:00:10.501805Z","iopub.execute_input":"2025-04-03T09:00:10.502142Z","iopub.status.idle":"2025-04-03T09:00:10.508177Z","shell.execute_reply.started":"2025-04-03T09:00:10.502100Z","shell.execute_reply":"2025-04-03T09:00:10.507368Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir('/kaggle/working')\nnp.save('label_map',label_mapper)","metadata":{"execution":{"iopub.status.busy":"2025-04-03T09:00:14.234727Z","iopub.execute_input":"2025-04-03T09:00:14.235355Z","iopub.status.idle":"2025-04-03T09:00:14.239549Z","shell.execute_reply.started":"2025-04-03T09:00:14.235327Z","shell.execute_reply":"2025-04-03T09:00:14.238612Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# display data\nimg,label = next(train_data)\nplt.subplots(3,4,figsize=(10,8))\nfor i in range(1,13):\n    plt.subplot(3,4,i)\n    plt.imshow(img[i])\n    plt.axis('off')\n    idx = label[i].argmax()\n    plt.title(label_mapper[idx])","metadata":{"execution":{"iopub.status.busy":"2025-04-03T09:00:17.183914Z","iopub.execute_input":"2025-04-03T09:00:17.184490Z","iopub.status.idle":"2025-04-03T09:00:18.923055Z","shell.execute_reply.started":"2025-04-03T09:00:17.184459Z","shell.execute_reply":"2025-04-03T09:00:18.921817Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Build and Train Model","metadata":{}},{"cell_type":"code","source":"base_model = Xception(weights='imagenet', include_top=False, classes=120)","metadata":{"execution":{"iopub.status.busy":"2025-04-03T09:00:29.103487Z","iopub.execute_input":"2025-04-03T09:00:29.104447Z","iopub.status.idle":"2025-04-03T09:00:36.814363Z","shell.execute_reply.started":"2025-04-03T09:00:29.104408Z","shell.execute_reply":"2025-04-03T09:00:36.813629Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# base_model.trainable=False\n\n# inputs = Input(shape = (224,224,3))\n# x = base_model(inputs, training = False) \n# x = GlobalAveragePooling2D(name= \"global_average_pooling\")(x)\n# x = Dropout(0.2)(x)\n# x = Dense(120, activation=\"softmax\")(x)\n\n# ModelDogBreed = tf.keras.Model(inputs, x) \n\n# ModelDogBreed.compile(loss = \"categorical_crossentropy\", \n#                      optimizer = Adam(), \n#                      metrics=[\"accuracy\"]) \n\n# ModelDogBreed.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T17:49:04.964069Z","iopub.execute_input":"2023-07-10T17:49:04.964831Z","iopub.status.idle":"2023-07-10T17:49:04.969987Z","shell.execute_reply.started":"2023-07-10T17:49:04.964794Z","shell.execute_reply":"2023-07-10T17:49:04.969076Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model = AveragePooling2D(pool_size=(4, 4))(base_model.output)\n# model = Flatten(name='flatten')(model)\n# model = Dense(1024, activation='relu')(model)\n# model = Dropout(0.3)(model)\n# model = Dense(512, activation='relu')(model)\n# model = Dropout(0.3)(model)\n# model = Dense(120, activation='softmax')(model)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T17:49:04.972018Z","iopub.execute_input":"2023-07-10T17:49:04.972496Z","iopub.status.idle":"2023-07-10T17:49:04.985798Z","shell.execute_reply.started":"2023-07-10T17:49:04.972462Z","shell.execute_reply":"2023-07-10T17:49:04.984936Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model.trainable = False\n\nmodel = Sequential()\nmodel.add(base_model)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dropout(0.2))\nmodel.add(Dense(120, activation='softmax'))\n\nmodel.compile(optimizer=Adam(learning_rate=0.001, ), loss='categorical_crossentropy', metrics=[\"accuracy\"])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2025-04-03T09:05:03.622416Z","iopub.execute_input":"2025-04-03T09:05:03.623390Z","iopub.status.idle":"2025-04-03T09:05:04.012663Z","shell.execute_reply.started":"2025-04-03T09:05:03.623350Z","shell.execute_reply":"2025-04-03T09:05:04.011816Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# callbacks\n# reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.1)\nes = EarlyStopping(monitor='val_accuracy', mode='min', verbose=1, patience=4, min_delta=0.01)\ncheckpoint = ModelCheckpoint(filepath='/kaggle/working/model.h5', monitor='val_accuracy', save_best_only=True, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2025-04-03T09:05:39.279776Z","iopub.execute_input":"2025-04-03T09:05:39.280382Z","iopub.status.idle":"2025-04-03T09:05:39.285161Z","shell.execute_reply.started":"2025-04-03T09:05:39.280351Z","shell.execute_reply":"2025-04-03T09:05:39.284350Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"logs = model.fit(train_data, validation_data=val_data,\n                steps_per_epoch = train_data.samples//BATCH_SIZE,\n                validation_steps = val_data.samples//BATCH_SIZE,\n                epochs=10, verbose=1, callbacks=[checkpoint,es])","metadata":{"execution":{"iopub.status.busy":"2025-04-03T09:05:50.830801Z","iopub.execute_input":"2025-04-03T09:05:50.831385Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(logs.history['loss'], label='loss', linestyle='dotted')\nplt.plot(logs.history['accuracy'], label='accuracy', linewidth=2)\nplt.plot(logs.history['val_loss'], label='val_loss', linestyle='dotted')\nplt.plot(logs.history['val_accuracy'], label='val_accuracy', linewidth=2)\nplt.legend()\nplt.xlabel('Epochs')\nplt.ylabel('Score')\nplt.title('Training Logs')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T18:00:14.906331Z","iopub.execute_input":"2023-07-10T18:00:14.906763Z","iopub.status.idle":"2023-07-10T18:00:24.766191Z","shell.execute_reply.started":"2023-07-10T18:00:14.906725Z","shell.execute_reply":"2023-07-10T18:00:24.765294Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"saved_model = keras.models.load_model('/kaggle/working/model.h5')\nsaved_model.evaluate(val_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T18:01:52.848488Z","iopub.execute_input":"2023-07-10T18:01:52.849099Z","iopub.status.idle":"2023-07-10T18:02:36.789676Z","shell.execute_reply.started":"2023-07-10T18:01:52.849066Z","shell.execute_reply":"2023-07-10T18:02:36.788532Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# os.chdir('/kaggle/working')\nimg = load_img('/kaggle/input/dog-breed-identification/test/00485d47de966a9437ad3b33ac193b6f.jpg', target_size=SIZE)\ndisplay(img)\nimg_array = keras.preprocessing.image.img_to_array(img)\nimg_array = tf.expand_dims(img_array, 0)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T18:03:47.983907Z","iopub.execute_input":"2023-07-10T18:03:47.984265Z","iopub.status.idle":"2023-07-10T18:03:48.017275Z","shell.execute_reply.started":"2023-07-10T18:03:47.984237Z","shell.execute_reply":"2023-07-10T18:03:48.016149Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = saved_model.predict(img_array)\nidx = pred.argmax()\nprint(label_mapper[idx])","metadata":{"execution":{"iopub.status.busy":"2023-07-10T18:03:50.836751Z","iopub.execute_input":"2023-07-10T18:03:50.837484Z","iopub.status.idle":"2023-07-10T18:03:50.917672Z","shell.execute_reply.started":"2023-07-10T18:03:50.837441Z","shell.execute_reply":"2023-07-10T18:03:50.916545Z"},"trusted":true},"outputs":[],"execution_count":null}]}