{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"from matplotlib.pyplot import imshow\n%matplotlib inline\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -q -U tf-hub-nightly\n!pip install -q tfds-nightly\nimport tensorflow_hub as hub\n\nfrom tensorflow.keras import layers\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_dir = '/kaggle/input/car-classificationproject-vision/Train/Train/'\ntest_dir = '/kaggle/input/car-classificationproject-vision/Test/Test/Test1/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for dirname, _, filenames in os.walk(train_dir):\n#     print(dirname)\n#     print(filenames)\n    print(dirname[59:])\n#     for filename in filenames:\n#         print(filename)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"car_map = pd.read_csv('/kaggle/input/car-classificationproject-vision/car_map.csv')\ncar_map=car_map.set_index('Cars')\ncar_map_dict=car_map.to_dict('index')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"car_map_dict","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 64\nIMG_HEIGHT = 224\nIMG_WIDTH = 224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_image_generator = ImageDataGenerator(rescale=1./255, validation_split=0.2, horizontal_flip=True, rotation_range=5, shear_range=0.2, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2) # Generator for our training data\n# train_image_generator = ImageDataGenerator(rescale=1./255, validation_split=0.2) # Generator for our training data\nvalidation_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our validation data\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def oh_to_class(Y):\n    result = np.zeros((Y.shape[0], 1))\n    for i in range(Y.shape[0]):\n        result[i]=np.argmax(Y[i])\n    return result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,\n                                                           directory=train_dir,\n                                                           shuffle=True,\n                                                           target_size=(IMG_HEIGHT, IMG_WIDTH),\n                                                           class_mode='categorical')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for dirname, _, filenames in os.walk(test_dir):\n#     print(dirname)\n    print(filenames)\n#     print(dirname)\n#     for filename in filenames:\n#         print(filename)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dir='/kaggle/input/car-classificationproject-vision/Test/Test/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,\n                                                           directory=test_dir,\n                                                           shuffle=False,\n                                                           target_size=(IMG_HEIGHT, IMG_WIDTH),\n                                                           class_mode=None)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_training_images, labels = next(train_data_gen)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.\ndef plotImages(images_arr):\n    fig, axes = plt.subplots(1, 5, figsize=(20,20))\n    axes = axes.flatten()\n    for img, ax in zip( images_arr, axes):\n        ax.imshow(img)\n        ax.axis('off')\n    plt.tight_layout()\n    plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(sample_training_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotImages(sample_training_images[:5])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"oh_to_class(labels[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data_gen.class_indices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def output_labels_to_map_labels(Y, output_label_dict, map_label_dict):\n#     Y input is not one hot encoded, but in the form of labels\n    result = np.zeros(Y.shape) \n    for i in range(Y.shape[0]):\n        curr_label_ind = Y[i][0]\n        car_name=0\n        for key, val in output_label_dict.items(): \n#             print(key, val)\n            if val == curr_label_ind: \n                car_name = key\n                break\n        print\n        req_label_index = map_label_dict[car_name]['Class Numbers']\n        result[i][0]=req_label_index\n    return result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotImages(sample_training_images[10:15])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"oh_to_class(labels[10:15])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_labels_to_map_labels(oh_to_class(labels[10:15]),train_data_gen.class_indices, car_map_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model = Sequential()\n# model.add(Conv2D(64, kernel_size=(3, 3),input_shape=(224,224,3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(MaxPooling2D(pool_size=(2, 2), strides=2))\n# model.add(Conv2D(128, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(128, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(MaxPooling2D(pool_size=(2, 2), strides=2))\n# model.add(Dropout(0.4))\n# model.add(Conv2D(256, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(256, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(256, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(MaxPooling2D(pool_size=(2, 2), strides=2))\n# model.add(Dropout(0.4))\n# model.add(Conv2D(512, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(512, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(512, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(MaxPooling2D(pool_size=(2, 2), strides=2))\n# model.add(Dropout(0.4))\n# model.add(Conv2D(512, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(512, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(Conv2D(512, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))\n# model.add(MaxPooling2D(pool_size=(2, 2), strides=2))\n# model.add(Flatten())\n# model.add(Dense(4096))\n# model.add(Dense(4096, activation='relu'))\n# model.add(Dense(45, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"feature_extractor_url = \"https://tfhub.dev/tensorflow/resnet_50/feature_vector/1\" \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"feature_extractor_layer = hub.KerasLayer(feature_extractor_url,\n                                         input_shape=(224,224,3))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential([\n  feature_extractor_layer,\n  layers.Dense(45, activation='softmax')\n])\n\nmodel.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(\n  optimizer=tf.keras.optimizers.Adam(),\n  loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),\n  metrics=['acc'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class CollectBatchStats(tf.keras.callbacks.Callback):\n    def __init__(self):\n        self.batch_losses = []\n        self.batch_acc = []\n\n    def on_train_batch_end(self, batch, logs=None):\n        self.batch_losses.append(logs['loss'])\n        self.batch_acc.append(logs['acc'])\n        self.model.reset_metrics()\n        \n#     def on_epoch_end(self, epoch, logs={}):\n#         if(logs.get('acc')>0.95):\n#             print(\"Reached 95% accuracy so cancelling training!\")\n#             self.model.stop_training = True\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"steps_per_epoch = 4050//64\n\nbatch_stats_callback = CollectBatchStats()\n\nhistory = model.fit_generator(train_data_gen, epochs=370,\n                              steps_per_epoch=steps_per_epoch,\n                              callbacks = [batch_stats_callback])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate(train_data_gen)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_predicted_oh = model.predict(train_data_gen)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"oh_to_class(train_predicted_oh)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predicted_oh = model.predict_generator(test_data_gen)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predicted=oh_to_class(test_predicted_oh)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predicted","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_testing_images = next(test_data_gen)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotImages(sample_testing_images[0:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_test = output_labels_to_map_labels(test_predicted,train_data_gen.class_indices, car_map_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_gen.filenames","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file_names = []\nfor name in test_data_gen.filenames:\n    file_names+=[name[6:]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file_names=np.array(file_names).reshape(-1, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_image_prediction = np.concatenate((file_names, result_test.astype(np.int32)), axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_image_prediction","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_df = pd.DataFrame(result_image_prediction, columns=['image', 'predictions'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_df.set_index('image')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_df.to_csv('submissionCommit12WithAugmentedDataE370.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}