{"cells":[{"metadata":{},"cell_type":"markdown","source":"# **Starter Notebook**\nThis notebook is just to showcase on how you can build a model, train it using the provided dataset and predict from test set. \nYou can take your own approach and framework of your choice."},{"metadata":{},"cell_type":"markdown","source":"## Creating seperate folder according to label for training to use ImageGenerator from Keras"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Importing required packages\nimport os\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv('../input/d-tribe-hackathon-2020/train/train map.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating folders with labels\nlabels = list(data.LABEL)\nfor label in labels:\n    os.makedirs(os.path.join('Training', str(label)), exist_ok = True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Copying training images to their respective folder of label\noriginalTrainImage = '../input/d-tribe-hackathon-2020/train/train'\nimport shutil\nfor i,j in data.iterrows():\n    shutil.copy(os.path.join(originalTrainImage,j.IMAGE), os.path.join('Training', str(j.LABEL), j.IMAGE))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Importing required packages from tensorflow"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport os\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating train and test generator"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Training parameters\nDIR = './Training'\nIMG_WIDTH = 128\nIMG_HEIGHT = 128\nCHANNELS = 3\nINPUT_SHAPE = (IMG_WIDTH, IMG_HEIGHT, CHANNELS)\nBATCH_SIZE = 512\nCLASSES = 291","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating the Training & validation Data\n\nDIR = './Training'\n\ndatagen = ImageDataGenerator(rotation_range=360, validation_split=0.20)\n\n\ntrain_generator = datagen.flow_from_directory(DIR,\n                                              target_size = (IMG_WIDTH, IMG_HEIGHT),\n                                              batch_size = BATCH_SIZE,\n                                              class_mode = 'categorical',\n                                              subset='training',\n                                              shuffle = True)\n                                                  \n\nval_generator = datagen.flow_from_directory(DIR,\n                                            target_size =(IMG_WIDTH, IMG_HEIGHT),\n                                            batch_size = BATCH_SIZE,\n                                            class_mode = 'categorical', \n                                            subset='validation',\n                                            shuffle = False)\n\ntest_generator = datagen.flow_from_directory('../input/d-tribe-hackathon-2020/test',\n                                              target_size = (IMG_WIDTH, IMG_HEIGHT),\n                                              batch_size = 768,\n                                              class_mode = 'categorical',\n                                              shuffle = False)\n                                                  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating a sequential CNN model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# defining the model\nfrom tensorflow.keras import datasets, layers, models\n\n\nmodelCNN = models.Sequential()\n\n# adding first convolutional layer\nmodelCNN.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=INPUT_SHAPE))\nmodelCNN.add(layers.MaxPooling2D((2, 2)))\n\n# adding second convolutional layer\nmodelCNN.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodelCNN.add(layers.MaxPooling2D((2, 2)))\n\n# adding third convolutional layer\nmodelCNN.add(layers.Conv2D(128, (3, 3), activation='relu'))\n\n# flatenning\nmodelCNN.add(layers.Flatten())\n\n# Adding hidden layers \nmodelCNN.add(layers.Dense(1024))\nmodelCNN.add(layers.Dropout(0.25))\nmodelCNN.add(layers.Dense(512))\nmodelCNN.add(layers.Dropout(0.25))\nmodelCNN.add(layers.Dense(64, activation='relu'))\n\n# Adding output dense layer\nmodelCNN.add(layers.Dense(CLASSES))\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelCNN.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating checkpoint"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint\nfrom datetime import datetime\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nlogdir = \"logs/logging/\" + datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n\n\nos.makedirs('./model')\nmc = ModelCheckpoint('model/eff.h5', monitor='val_loss', save_best_only = True, verbose=1)\ntensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Compiling & Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"modelCNN.compile(loss='categorical_crossentropy',\n              optimizer=Adam(lr=1e-2), \n              metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fitting the Model \nmodelCNN.fit(train_generator,\n                    steps_per_epoch=train_generator.samples // BATCH_SIZE,\n                    epochs=5,\n                    validation_data=val_generator,\n                    validation_steps = val_generator.samples // BATCH_SIZE,\n                    callbacks=[ mc, tensorboard_callback])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Loading tensorboard"},{"metadata":{"trusted":true},"cell_type":"code","source":"%load_ext tensorboard\n\n%tensorboard --logdir logs/logging","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Evaluation"},{"metadata":{"trusted":true},"cell_type":"code","source":"# You will use this dictionary to map prediction to the correct class id for it\npredMapDictionary = train_generator.class_indices\nprint(predMapDictionary)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Predicting\nprediction = modelCNN.predict(test_generator, test_generator.samples//768)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Preparing submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"# list out keys and values separately\nkey_list = list(predMapDictionary.keys())\nval_list = list(predMapDictionary.values())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = []\nfor p in prediction:\n    pred.append(key_list[val_list.index(np.argmax(p))])\nprint(pred[-10:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('../input/d-tribe-hackathon-2020/sample submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Making list of files corresponding to prediction\nfile_test = []\n\nfor files in test_generator.filenames:\n    file_test.append(files.split('/')[1].split('.')[0])\nprint(f\"File corresponding to prediction: {file_test[:10]} ...\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# putting prediction according to the image name\nfor i in range(len(file_test)):\n    submission.loc[submission.IMAGE == file_test[i]+\".jpg\", 'LABEL'] = int(pred[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=None)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Accuracy from above is less than 1% but you can change the architecture, can do some transfer learning from other CNN architecture like vgg, alexnet, resnet etc."},{"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}