{"cells":[{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom skimage import io, color, exposure, transform\nimport os\nimport glob\nimport h5py\n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential, model_from_json\nfrom keras.layers.core import Dense, Dropout, Activation, Flatten\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.pooling import MaxPooling2D\n\nfrom keras.optimizers import SGD\nfrom keras.utils import np_utils\nfrom keras.callbacks import LearningRateScheduler, ModelCheckpoint\nfrom keras import backend as K\nK.set_image_data_format('channels_first')\n\nfrom matplotlib import pyplot as plt\n%matplotlib inline\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.\nNUM_CLASSES = 43\nIMG_SIZE = 48","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load Data"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def preprocess_img(img):\n    # Histogram normalization in y\n    hsv = color.rgb2hsv(img)\n    hsv[:,:,2] = exposure.equalize_hist(hsv[:,:,2])\n    img = color.hsv2rgb(hsv)\n\n    # central scrop\n    min_side = min(img.shape[:-1])\n    centre = img.shape[0]//2, img.shape[1]//2\n    img = img[centre[0]-min_side//2:centre[0]+min_side//2,\n              centre[1]-min_side//2:centre[1]+min_side//2,\n              :]\n\n    # rescale to standard size\n    img = transform.resize(img, (IMG_SIZE, IMG_SIZE))\n\n    # roll color axis to axis 0\n    img = np.rollaxis(img,-1)\n\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nDATA_DIR_TRAIN = \"../input/gtsrb_challenge/GTSRB_Challenge/train\"\nx_train = []\ny_train = []\nfor i in range(43):\n    category = \"\"\n    if i<10:\n        category = \"0000\"+str(i)\n    else:\n        category = \"000\"+str(i)\n    path = os.path.join(DATA_DIR_TRAIN, category)   \n    \n    for p in os.listdir(path):\n        img_path = os.path.join(path, p)\n        img = cv2.imread(img_path)\n        new_img = preprocess_img(img)\n        x_train.append(new_img)\n        y_train.append(i)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train_np = np.array(x_train, dtype='float32')         \ny_train_np = np.eye(NUM_CLASSES, dtype='uint8')[y_train]\n\nprint(x_train_np.shape)\nprint(y_train_np.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# EDA"},{"metadata":{"trusted":true},"cell_type":"code","source":"random_array = np.random.randint(len(x_train),size=100)\nrandom_array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grids = (10,10)\ncounter = 0\n\nplt.figure(figsize=(20,20))\n\nfor i in range(0, 100):\n  ax = plt.subplot(10, 10, i+1)\n  img = np.rollaxis(x_train[random_array[i]], 0, 3)\n  ax = plt.imshow(img, cmap='gray')\n  plt.title(y_train[random_array[i]])\n  plt.xticks([])\n  plt.yticks([])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random_array = np.random.randint(len(x_train),size=100)\nrandom_array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grids = (10,10)\ncounter = 0\n\nplt.figure(figsize=(20,20))\n\nfor i in range(0, 100):\n  ax = plt.subplot(10, 10, i+1)\n  img = np.rollaxis(x_train[random_array[i]], 0, 3)\n  ax = plt.imshow(img, cmap='gray')\n  plt.title(y_train[random_array[i]])\n  plt.xticks([])\n  plt.yticks([])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"def cnn_model():\n    model = Sequential()\n\n    model.add(Conv2D(32, (3, 3), padding='same',\n                     input_shape=(3, IMG_SIZE, IMG_SIZE),\n                     activation='relu'))\n    model.add(Conv2D(32, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    model.add(Conv2D(64, (3, 3), padding='same',\n                     activation='relu'))\n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    model.add(Conv2D(128, (3, 3), padding='same',\n                     activation='relu'))\n    model.add(Conv2D(128, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    model.add(Flatten())\n    model.add(Dense(512, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(NUM_CLASSES, activation='softmax'))\n    return model\n\ndef lr_schedule(epoch):\n    return lr*(0.1**int(epoch/10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(x_train_np, y_train_np, test_size=0.2, random_state=42)\n\ndatagen = ImageDataGenerator(featurewise_center=False, \n                            featurewise_std_normalization=False, \n                            width_shift_range=0.1,\n                            height_shift_range=0.1,\n                            zoom_range=0.2,\n                            shear_range=0.1,\n                            rotation_range=10.,)\n\ndatagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = cnn_model()\nlr = 0.01\nsgd = SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True)\nmodel.compile(loss='categorical_crossentropy',\n          optimizer=sgd,\n          metrics=['accuracy'])\n\n\ndef lr_schedule(epoch):\n    return lr*(0.1**int(epoch/10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 32\nnb_epoch = 1\nmodel.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),\n                            steps_per_epoch=X_train.shape[0],\n                            epochs=nb_epoch,\n                            validation_data=(X_val, Y_val),\n                            callbacks=[LearningRateScheduler(lr_schedule),\n                                       ModelCheckpoint('model.h5',save_best_only=True)]\n                           )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_DIR_TEST = \"../input/gtsrb_challenge/GTSRB_Challenge/test\"\nx_test = []\nfor p in os.listdir(DATA_DIR_TEST):\n    img_path = os.path.join(DATA_DIR_TEST, p)\n    img = cv2.imread(img_path)\n    new_img = preprocess_img(img)\n    x_test.append(new_img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random_array = np.random.randint(len(x_test),size=100)\nrandom_array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grids = (10,10)\ncounter = 0\n\nplt.figure(figsize=(20,20))\n\nfor i in range(0, 100):\n    ax = plt.subplot(10, 10, i+1)\n    img = np.rollaxis(x_test[random_array[i]], 0, 3)\n    ax = plt.imshow(img, cmap='gray')\n    x = x_test[random_array[i]]\n    y_predict = np.argmax(model.predict(x.reshape(1,3,48,48)), axis=1)\n    plt.title(y_predict)\n    plt.xticks([])\n    plt.yticks([])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import csv\nwith open('dungdmse05228_submission.csv', mode='w') as csv_file:\n    fieldnames = ['Filename', 'ClassId']\n    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)\n\n    writer.writeheader()\n    DATA_DIR_TEST = \"../input/gtsrb_challenge/GTSRB_Challenge/test\"\n    \n    for p in os.listdir(DATA_DIR_TEST):\n        img_path = os.path.join(DATA_DIR_TEST, p)\n        img = cv2.imread(img_path)\n        new_img = preprocess_img(img)\n        y_predict = np.argmax(model.predict(new_img.reshape(1,3,48,48)), axis=1)\n        \n        writer.writerow({'Filename': p, 'ClassId': int(y_predict)})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(\"../input/gtsrb_challenge/GTSRB_Challenge/train/00009/00000_00000.ppm\")\nimg = cv2.resize(img, (120, 120))\nplt.imshow(img)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}