{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1>MAIN CODE</h1>","metadata":{"id":"xTjQLv4LfPzQ"}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import MobileNetV2\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"papermill":{"duration":9.573087,"end_time":"2023-05-04T13:38:54.838385","exception":false,"start_time":"2023-05-04T13:38:45.265298","status":"completed"},"tags":[],"id":"c9900c3c","execution":{"iopub.status.busy":"2023-06-19T23:50:06.194361Z","iopub.execute_input":"2023-06-19T23:50:06.194737Z","iopub.status.idle":"2023-06-19T23:50:16.367487Z","shell.execute_reply.started":"2023-06-19T23:50:06.194711Z","shell.execute_reply":"2023-06-19T23:50:16.366493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setting path: For use only in Kaggle\n\ntrain_path = '../input/dog-breed-identification/train'\ntest_path = '../input/dog-breed-identification/test'\n\n# Reading dataset labels\ntrain_labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\ntest_labels = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"papermill":{"duration":0.848515,"end_time":"2023-05-04T13:38:55.722091","exception":false,"start_time":"2023-05-04T13:38:54.873576","status":"completed"},"tags":[],"id":"711bb646","execution":{"iopub.status.busy":"2023-06-19T23:50:16.369792Z","iopub.execute_input":"2023-06-19T23:50:16.370612Z","iopub.status.idle":"2023-06-19T23:50:17.050780Z","shell.execute_reply.started":"2023-06-19T23:50:16.370567Z","shell.execute_reply":"2023-06-19T23:50:17.049715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Including the extension in the id\ntrain_labels['id'] = train_labels['id'].apply(lambda x: x + \".jpg\")\ntest_labels['id'] = test_labels['id'].apply(lambda x: x + \".jpg\")","metadata":{"papermill":{"duration":0.056703,"end_time":"2023-05-04T13:38:56.013810","exception":false,"start_time":"2023-05-04T13:38:55.957107","status":"completed"},"tags":[],"id":"e25282d7","execution":{"iopub.status.busy":"2023-06-19T23:50:17.052189Z","iopub.execute_input":"2023-06-19T23:50:17.052555Z","iopub.status.idle":"2023-06-19T23:50:17.074355Z","shell.execute_reply.started":"2023-06-19T23:50:17.052518Z","shell.execute_reply":"2023-06-19T23:50:17.073079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cleaning the dataset removing any figure or dog not in breed\nx = train_labels.breed.unique()\nbreeds = ['golden_retriever', 'scottish_deerhound', 'doberman', 'malamute', 'chihuahua', 'pug', 'saint_bernard', 'beagle', 'french_bulldog' ,'tibetan_mastiff']\nfor i in x :\n    if i not in breeds:\n        train_labels = train_labels.drop(train_labels[ train_labels['breed'] == i ].index)","metadata":{"papermill":{"duration":0.203801,"end_time":"2023-05-04T13:38:56.253400","exception":false,"start_time":"2023-05-04T13:38:56.049599","status":"completed"},"tags":[],"id":"088ce575","outputId":"0f4d5791-57d5-4162-8d25-4f8fe23ac941","execution":{"iopub.status.busy":"2023-06-19T23:50:17.077261Z","iopub.execute_input":"2023-06-19T23:50:17.077948Z","iopub.status.idle":"2023-06-19T23:50:17.292610Z","shell.execute_reply.started":"2023-06-19T23:50:17.077914Z","shell.execute_reply":"2023-06-19T23:50:17.291701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Generators</h3>","metadata":{"id":"1H_EQ63De0qx"}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n                                   rescale = 1./255,\n                                   validation_split = 0.2,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2, horizontal_flip = True,\n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.2,\n                                   rotation_range = 20,\n                                   brightness_range=[0.2,1.0])\n\ntrain_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n                                                 directory = train_path,\n                                                 x_col = \"id\",\n                                                 y_col = \"breed\",\n                                                 batch_size = 16,\n                                                 subset=\"training\",\n                                                 class_mode = \"categorical\",\n                                                 target_size = (224,224),\n                                                 seed = 42,\n                                                 shuffle = True)\n","metadata":{"papermill":{"duration":0.567316,"end_time":"2023-05-04T13:38:57.250507","exception":false,"start_time":"2023-05-04T13:38:56.683191","status":"completed"},"tags":[],"id":"54d6bf6f","outputId":"670ebd74-134e-453b-f588-e62fcedba39f","execution":{"iopub.status.busy":"2023-06-19T23:50:17.294084Z","iopub.execute_input":"2023-06-19T23:50:17.294437Z","iopub.status.idle":"2023-06-19T23:50:19.138020Z","shell.execute_reply.started":"2023-06-19T23:50:17.294407Z","shell.execute_reply":"2023-06-19T23:50:19.137137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n                                                 directory = train_path,\n                                                 x_col = \"id\",\n                                                 y_col = \"breed\",\n                                                 batch_size = 16,\n                                                 subset=\"validation\",\n                                                 class_mode = \"categorical\",\n                                                 target_size = (224,224),\n                                                 seed = 42,\n                                                 shuffle = True)\n","metadata":{"papermill":{"duration":0.059081,"end_time":"2023-05-04T13:38:57.347292","exception":false,"start_time":"2023-05-04T13:38:57.288211","status":"completed"},"tags":[],"id":"8fad41c9","outputId":"5430d656-2981-468d-9031-e033827d0ffe","execution":{"iopub.status.busy":"2023-06-19T23:50:19.139559Z","iopub.execute_input":"2023-06-19T23:50:19.139938Z","iopub.status.idle":"2023-06-19T23:50:19.161623Z","shell.execute_reply.started":"2023-06-19T23:50:19.139904Z","shell.execute_reply":"2023-06-19T23:50:19.160573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_set = test_datagen.flow_from_dataframe(dataframe = test_labels,\n                                                 directory = test_path,\n                                                 x_col = \"id\",\n                                                 y_col = None,\n                                                 batch_size = 16,\n                                                 class_mode = None,\n                                                 seed = 42,\n                                                 shuffle=False,\n                                                 target_size = (224,224))","metadata":{"papermill":{"duration":7.303246,"end_time":"2023-05-04T13:39:04.686367","exception":false,"start_time":"2023-05-04T13:38:57.383121","status":"completed"},"tags":[],"id":"46bfeac6","outputId":"0a5d5f77-68f6-4bc2-9613-7421af8863ea","execution":{"iopub.status.busy":"2023-06-19T23:50:19.163102Z","iopub.execute_input":"2023-06-19T23:50:19.163515Z","iopub.status.idle":"2023-06-19T23:50:42.845388Z","shell.execute_reply.started":"2023-06-19T23:50:19.163485Z","shell.execute_reply":"2023-06-19T23:50:42.844425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Networks</h3>","metadata":{"id":"aHAM3ekLe6Dg"}},{"cell_type":"code","source":"# Using the MobileNetV2 model\nbase_model = MobileNetV2(input_shape=[224,224,3], include_top=False, weights='/kaggle/input/keras-pretrain-model-weights/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5')\n\nfor layer in base_model.layers:\n    layer.trainable = False\n\nx = keras.layers.Flatten()(base_model.output)\n\nx = keras.layers.Dropout(0.4)(x)\n\npred = keras.layers.Dense(10, activation='softmax')(x)\n\nmodel = tf.keras.models.Model(inputs=base_model.input, outputs=pred)\n\nopt = tf.keras.optimizers.Adam(learning_rate = 1e-5)\nmodel.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['accuracy'])\n\ntrain_step = train_set.n//train_set.batch_size\nvalidate_step = validate_set.n//validate_set.batch_size\n\nmobile_net_model = model.fit(train_set,validation_data = validate_set,epochs = 30,steps_per_epoch = train_step, validation_steps = validate_step)","metadata":{"papermill":{"duration":2423.979716,"end_time":"2023-05-04T14:19:28.801150","exception":false,"start_time":"2023-05-04T13:39:04.821434","status":"completed"},"tags":[],"id":"5f9f02cf","outputId":"1b78c23b-e3ce-4291-e9a5-19a8cebf31a0","execution":{"iopub.status.busy":"2023-06-19T23:50:42.846694Z","iopub.execute_input":"2023-06-19T23:50:42.847138Z","iopub.status.idle":"2023-06-20T00:00:19.842018Z","shell.execute_reply.started":"2023-06-19T23:50:42.847104Z","shell.execute_reply":"2023-06-20T00:00:19.840744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:00:19.844002Z","iopub.execute_input":"2023-06-20T00:00:19.844382Z","iopub.status.idle":"2023-06-20T00:00:20.157047Z","shell.execute_reply.started":"2023-06-20T00:00:19.844350Z","shell.execute_reply":"2023-06-20T00:00:20.156275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(mobile_net_model.history['accuracy'])\nplt.plot(mobile_net_model.history['val_accuracy'])\nplt.title('MobileNet: Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\nplt.plot(mobile_net_model.history['loss'])\nplt.plot(mobile_net_model.history['val_loss'])\nplt.title('MobileNet: Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"papermill":{"duration":0.664483,"end_time":"2023-05-04T14:32:38.325744","exception":false,"start_time":"2023-05-04T14:32:37.661261","status":"completed"},"tags":[],"id":"daf3fa27","outputId":"c43c2868-05f3-404f-9469-2530b93f511d","execution":{"iopub.status.busy":"2023-06-20T00:00:20.160121Z","iopub.execute_input":"2023-06-20T00:00:20.160498Z","iopub.status.idle":"2023-06-20T00:00:20.963419Z","shell.execute_reply.started":"2023-06-20T00:00:20.160454Z","shell.execute_reply":"2023-06-20T00:00:20.962523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform model prediction\nnum_steps = test_set.n // test_set.batch_size + 1\ntest_set.reset()\nypred = model.predict(test_set, steps=num_steps, verbose=1) # MobileNet prediction\n# ypred = conv_model.predict(test_set, steps=num_steps, verbose=1) # Deep CNN prediction","metadata":{"id":"4rtVGSR9F4yv","outputId":"c29cbdc0-614a-4965-9992-f44758bfd576","execution":{"iopub.status.busy":"2023-06-20T00:00:20.965040Z","iopub.execute_input":"2023-06-20T00:00:20.965418Z","iopub.status.idle":"2023-06-20T00:01:48.860217Z","shell.execute_reply.started":"2023-06-20T00:00:20.965386Z","shell.execute_reply":"2023-06-20T00:01:48.859207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the predicted breed labels and save the submission file\npredicted_breeds = train_set.class_indices\npredicted_labels = [key for value in np.argmax(ypred, axis=1) for key, val in predicted_breeds.items() if val == value]\n\nsubmission = pd.DataFrame({\"id\": test_set.filenames, \"breed\": predicted_labels})\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"id":"7BbTzBDqNyD7","execution":{"iopub.status.busy":"2023-06-20T00:01:48.861804Z","iopub.execute_input":"2023-06-20T00:01:48.862247Z","iopub.status.idle":"2023-06-20T00:01:48.938623Z","shell.execute_reply.started":"2023-06-20T00:01:48.862208Z","shell.execute_reply":"2023-06-20T00:01:48.937702Z"},"trusted":true},"execution_count":null,"outputs":[]}]}