{"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":"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# for 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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 224\nbatch_size = 128","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True, validation_split=0.1)\n\n# df_train = tf.keras.preprocessing.image_dataset_from_directory(\"/kaggle/input/stanford-car-dataset-by-classes-folder/car_data/car_data/train\",\n#                                                               labels=\"inferred\", batch_size=32, image_size=(256, 256), \n#                                                               seed=0,validation_split=0.1, subset=\"training\")\n\ndf_train = datagen.flow_from_directory(\n        \"/kaggle/input/stanford-car-dataset-by-classes-folder/car_data/car_data/train\",\n        target_size=(image_size, image_size),\n        batch_size=batch_size,\n        class_mode='categorical', subset='training')\n\n\ndf_val = datagen.flow_from_directory(\n        \"/kaggle/input/stanford-car-dataset-by-classes-folder/car_data/car_data/train\",\n        target_size=(image_size, image_size),\n        batch_size=batch_size,\n        class_mode='categorical', subset='validation')\n# df_test = tf.keras.preprocessing.image_dataset_from_directory(\"/kaggle/input/stanford-car-dataset-by-classes-folder/car_data/car_data/test\",\n#                                                               labels=\"inferred\", batch_size=32, image_size=(256, 256), )\n\ndatagen_test = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\ndf_test = datagen_test.flow_from_directory(\n        \"/kaggle/input/stanford-car-dataset-by-classes-folder/car_data/car_data/test\",\n        target_size=(image_size, image_size),\n        batch_size=batch_size,\n        class_mode='categorical')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomModel(tf.keras.Model):\n\n    def __init__(self):\n        super(CustomModel, self).__init__()\n        self.inp_l = tf.keras.layers.Input(shape=(256, 256, 3))\n        \n        self.ext_model = tf.keras.applications.ResNet50()\n        \n        self.dense1 = tf.keras.layers.Dense(1024, activation=tf.nn.relu)\n        self.dense2 = tf.keras.layers.Dense(512, activation=tf.nn.relu)\n        self.dense3 = tf.keras.layers.Dense(196, activation=tf.nn.softmax)\n\n    def call(self, inputs):\n#         x = self.inp_l(inputs)\n        \n        x = self.ext_model(inputs)\n        \n        x = self.dense1(x)\n        x = self.dense2(x)\n        x = self.dense3(x)\n        \n        return x","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CustomModel()\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(),\n              loss=tf.keras.losses.categorical_crossentropy,\n              metrics=tf.metrics.categorical_accuracy)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device(\"GPU\"):\n    model.fit(df_train,\n              validation_data=df_val,\n              epochs=50)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The latter result is not good enough because our score is very low for the test datasets we can see near 26%, and in the validation dataset we can see 5.99%, so I'll save the model and then retrain her","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import f1_score, roc_auc_score, recall_score, precision_score, confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.metrics import multilabel_confusion_matrix","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, acc = model.evaluate(df_test)\nloss, acc","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device(\"GPU\"):\n    res_predict = model.predict(df_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getClassPredict(arr):\n    return np.argmax(arr)\n\ndef fromMatToArrClass(mat):\n    return np.array([getClassPredict(i) for i in mat])\n\ndef oneHot(arr, ndim=196):\n    f_res = []\n    \n    for i in arr:\n        inter_arr = np.zeros(ndim)\n        inter_arr[i] = 1\n        f_res.append(inter_arr)\n        \n    return np.array(f_res)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = df_test.classes\n\nclass_pred = fromMatToArrClass(res_predict)\n\nprint(f1_score(y_test, class_pred, average='weighted'))\nprint(roc_auc_score(y_test, res_predict, multi_class='ovo'))\nprint(recall_score(y_test, class_pred, average='weighted'))\nprint(precision_score(y_test, class_pred,average='weighted'))\n# conf_mat = confusion_matrix(y_test, class_pred)\n# plt.figure(figsize=(15,15))\n# sns.heatmap(conf_mat, annot=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model_checkpoint_1.h5\", save_format='h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model2 = tf.keras.models.load_model(\"model_checkpoint_1.h5\")\nmodel.load_weights(\"/kaggle/input/finalpoints/model_checkpoint_1.h5\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device(\"GPU\"):\n    model.fit(df_train,\n              validation_data=df_val,\n              epochs=50)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After this training we can see this result: \n- categorical_accuracy: 0.8277\n- val_categorical_accuracy: 0.3965","metadata":{}},{"cell_type":"code","source":"model.save_weights(\"model_checkpoint_2.h5\", save_format='h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device(\"GPU\"):\n    res_predict = model.predict(df_test)\n    \ny_test = df_test.classes\n\nclass_pred = fromMatToArrClass(res_predict)\n\nprint(f1_score(y_test, class_pred, average='weighted'))\nprint(roc_auc_score(y_test, res_predict, multi_class='ovo'))\nprint(recall_score(y_test, class_pred, average='weighted'))\nprint(precision_score(y_test, class_pred,average='weighted'))\n# conf_mat = confusion_matrix(y_test, class_pred)\n# plt.figure(figsize=(15,15))\n# sns.heatmap(conf_mat, annot=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, acc = model.evaluate(df_test)\nloss, acc","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device(\"GPU\"):\n    model.fit(df_train,\n              validation_data=df_val,\n              epochs=50)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After this training we can see this result: \n- categorical_accuracy: 0.8923\n- val_categorical_accuracy: 0.3665","metadata":{}},{"cell_type":"code","source":"model.save_weights(\"model_checkpoint_3.h5\", save_format='h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, acc = model.evaluate(df_test)\nloss, acc","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- test_dataset_accuracy: 0.3759","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}