{"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":"import numpy as np\nimport pandas as pd\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten\nfrom tensorflow.python.keras.models import load_model\nfrom tensorflow.python.keras.preprocessing.image import load_img, img_to_array\nfrom keras.optimizers import SGD\n\n# Load Training Data\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\n\n# Load Testing Data\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\n\n# Extract Training data\ntrain_x = train.drop('label', axis=1)\ntrain_y = train['label']\n\n# Change Training and Testing data format\ntrain_x = train_x.astype('float32')\ntest = test.astype('float32')\ntrain_x = train_x / 255.0\ntest = test / 255.0\n\n# Reshape Training and Testing data\ntrain_x = train_x.values.reshape(-1, 28, 28, 1)\ntest = test.values.reshape(-1, 28, 28, 1)\n\n\ndef create_model():\n    # Build Model\n    model = Sequential()\n\n    # Convolution\n    model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))\n\n    # Compression\n    model.add(MaxPooling2D(2, 2))\n\n    model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform'))\n    model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform'))\n    model.add(MaxPooling2D((2, 2)))\n\n    # Optimization filter\n    model.add(Flatten())\n\n    # Hidden Layers\n    model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n    model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n\n    # Output Layer\n    model.add(Dense(10, activation='softmax'))\n\n    print(model.summary())\n\n    # Compile Model\n    model.compile(loss='sparse_categorical_crossentropy'\n                  , optimizer=SGD(learning_rate=0.01, momentum=0.9)\n                  , metrics=['accuracy'])\n\n    # Train Model\n    history = model.fit(train_x, train_y, epochs=10)\n\n    print(\"Accuracy: %.2f%%\" % (history.history['accuracy'][np.argmin(history.history['loss'])] * 100))\n\n    # save Model\n    model.save('mnist_model.h5')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-30T18:30:01.746667Z","iopub.execute_input":"2021-05-30T18:30:01.747039Z","iopub.status.idle":"2021-05-30T18:30:07.506550Z","shell.execute_reply.started":"2021-05-30T18:30:01.747006Z","shell.execute_reply":"2021-05-30T18:30:07.504963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_model()","metadata":{"execution":{"iopub.status.busy":"2021-05-30T18:30:07.511139Z","iopub.execute_input":"2021-05-30T18:30:07.511534Z","iopub.status.idle":"2021-05-30T18:35:41.659545Z","shell.execute_reply.started":"2021-05-30T18:30:07.511501Z","shell.execute_reply":"2021-05-30T18:35:41.658470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_img(img_path):\n    img = img_to_array(load_img(img_path, color_mode='grayscale', target_size=(28, 28)))\n    img = img.reshape(-1, 28, 28, 1)\n    img = img.astype('float32')\n    return img / 255.0\n\n\ndef test_img(model, img_path):\n    img = prepare_img(img_path)\n    test_digit = model.predict_classes(img)\n    return test_digit[0]\n\n\ndef test_model():\n    # Load Model\n    model = load_model('mnist_model.h5')\n\n    # result = model.predict(test)\n    # print(result.round())\n\n    print(\"The Predicted Output is\", test_img(model, '../input/test-imagepng/test_image.png'))\n    print(\"The Predicted Output is\", test_img(model, '../input/test-images/3.png'))\n    print(\"The Predicted Output is\", test_img(model, '../input/test-images/6.png'))","metadata":{"execution":{"iopub.status.busy":"2021-05-30T18:08:43.778195Z","iopub.execute_input":"2021-05-30T18:08:43.778657Z","iopub.status.idle":"2021-05-30T18:08:43.786979Z","shell.execute_reply.started":"2021-05-30T18:08:43.778615Z","shell.execute_reply":"2021-05-30T18:08:43.785782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_model()","metadata":{"execution":{"iopub.status.busy":"2021-05-30T18:08:50.255226Z","iopub.execute_input":"2021-05-30T18:08:50.255827Z","iopub.status.idle":"2021-05-30T18:08:50.635295Z","shell.execute_reply.started":"2021-05-30T18:08:50.255779Z","shell.execute_reply":"2021-05-30T18:08:50.634175Z"},"trusted":true},"execution_count":null,"outputs":[]}]}