{"cells":[{"metadata":{},"cell_type":"markdown","source":"This notebook features Tensorflow **tf.data API** and **custom Keras classes** for the model. \n\n## This model achieved **0.96396**. \n\n***You like it please upvote :)***"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport os\nimport datetime\nimport time\nimport pandas as pd\nfrom PIL import Image\nimport random\nimport glob\nfrom matplotlib import pyplot as plt\nimport seaborn as sn\nimport re\nimport tensorflow as tf\nimport tensorflow.keras.preprocessing as pp\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau,EarlyStopping\nimport tensorflow_addons as tfa\nfrom imgaug import augmenters as iaa\n\nfrom skimage import transform\n%config IPCompleter.greedy = True\n%config InlineBackend.figure_format = 'retina'\n%matplotlib inline\npd.set_option('mode.chained_assignment', None)\nsn.set(font_scale=1.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_train = train['label']\nX_train = train.drop('label',axis=1).values\ntest = test.values\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"RANDOM_STATE = 75\n\ndef random_seed(seed):\n    random.seed(RANDOM_STATE)\n    os.environ['PYTHONHASHSEED'] = str(RANDOM_STATE)\n    np.random.seed(RANDOM_STATE)\n    tf.random.set_seed(RANDOM_STATE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_image(image):\n    plt.rcParams[\"axes.grid\"] = False\n    plt.axis('off')\n    plt.imshow(image)   \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=RANDOM_STATE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE\nEPOCHS=50\nSHUFFLE_BUFFER_SIZE=5000 \nBATCH_SIZE=64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"@tf.function\ndef reshape(image, label):\n    return tf.reshape(image,(28,28,1)),label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"@tf.function\ndef normalize(image, label):\n    image = tf.cast(image, tf.float32)\n    return image/255.0,label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"@tf.function\ndef rotate_tf(image):\n    #https://github.com/tensorflow/tensorflow/issues/30112#issuecomment-549330939\n    random_angles = tf.random.uniform(shape = (), minval = -np\n    .pi / 15, maxval = np.pi / 15)\n    return tfa.image.rotate(image,random_angles)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"@tf.function\ndef augment(image, label):\n    image = rotate_tf(image)\n    return image, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_data_set(images, labels, is_training=False):\n    dataset = tf.data.Dataset.from_tensor_slices((images,labels))\n    dataset = dataset.map(reshape, num_parallel_calls=AUTOTUNE)\n    dataset = dataset.map(normalize, num_parallel_calls=AUTOTUNE)\n    \n    if is_training:\n        dataset = dataset.map(augment, num_parallel_calls=AUTOTUNE)\n        \n    dataset = dataset.shuffle(SHUFFLE_BUFFER_SIZE)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTOTUNE)\n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dataset = get_data_set(X_train, y_train, is_training=True)\ntest_dataset = get_data_set(X_val,y_val)\n\nsample_x, sample_y = next(iter(train_dataset))\nprint(sample_x.shape) \nprint(sample_y.shape) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_image(sample_x[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"class CNNBlock(layers.Layer):\n    def __init__(self, out_channel, kernel_size=3, dropout=None):\n        super(CNNBlock,self).__init__()\n        self.conv = layers.Conv2D(out_channel, kernel_size,kernel_initializer='he_normal', padding=\"same\")\n        self.bn = layers.BatchNormalization()\n        self.maxpool = layers.MaxPool2D(pool_size=(2,2))\n        self.dropout=None\n        if dropout:\n            self.dropout = layers.Dropout(dropout)\n    \n    def call(self, input_tensor, training=False):\n        x = self.conv(input_tensor)\n        x = self.bn(x, training=training)\n        x = tf.nn.relu(x)\n        x = self.maxpool(x)\n        if self.dropout:\n            x = self.dropout(x)\n        return x    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.tensorflow.org/guide/keras/train_and_evaluate\ndef get_uncompiled_model():\n    model = keras.Sequential(\n     [CNNBlock(32), CNNBlock(32,dropout=.25), CNNBlock(64), CNNBlock(64, dropout=.25), layers.Flatten(), layers.Dense(256, activation = \"relu\"),layers.Dropout(.5), layers.Dense(10)]\n)\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_compiled_model():\n    model = get_uncompiled_model()\n    model.compile(\n        optimizer=\"rmsprop\",\n        loss=[keras.losses.SparseCategoricalCrossentropy(from_logits=True),],\n        metrics=[\"accuracy\"],\n    )\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = get_compiled_model()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"reduce_lr_on_plateau = ReduceLROnPlateau(monitor='val_accuracy',\n                       factor=0.5,\n                       patience=3,\n                       min_lr=0.0001,\n                       verbose=1)  \n#early_stopping = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=8)\ncallbacks_list = [reduce_lr_on_plateau]\nhistory = model.fit(train_dataset, epochs=EPOCHS, verbose=1,validation_data=test_dataset,callbacks=callbacks_list).history","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure()\nplt.ylabel(\"Loss (training and validation)\")\nplt.xlabel(\"Training Steps\")\nplt.ylim([0,2])\nplt.plot(history[\"loss\"])\nplt.plot(history[\"val_loss\"])\n\nplt.figure()\nplt.ylabel(\"Accuracy (training and validation)\")\nplt.xlabel(\"Training Steps\")\nplt.ylim([0,1])\nplt.plot(history[\"accuracy\"])\nplt.plot(history[\"val_accuracy\"])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results = model.predict(test.reshape(-1,28,28,1))\n\nresults = tf.argmax(results,axis = 1)\n\nresults = pd.Series(results, name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_keras_classes.csv\",index=False)","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}