Skip to content

Instantly share code, notes, and snippets.

@tudoanh
Created May 19, 2022 15:58
Show Gist options
  • Save tudoanh/2bf2fec35ce68285996fb846dbedf29c to your computer and use it in GitHub Desktop.
Save tudoanh/2bf2fec35ce68285996fb846dbedf29c to your computer and use it in GitHub Desktop.
Hackthebox Mnist without 2
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/tudoanh/2bf2fec35ce68285996fb846dbedf29c/hackthebox-mnist-without-2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gspKA7tmmqXA"
},
"source": [
"# Simple MNIST convnet (without `2`)\n",
"\n",
"**Author:** [fchollet](https://twitter.com/fchollet)<br>\n",
"**Date created:** 2015/06/19<br>\n",
"**Last modified:** 2020/04/21<br>\n",
"**Description:** A simple convnet that achieves ~99% test accuracy on MNIST.<br>\n",
"\n",
"Modified by [tudoanh](https://github.com/tudoanh)<br>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Bwiw_UnimqXC"
},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YTKn26KtmqXD"
},
"outputs": [],
"source": [
"import numpy as np\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f_mC9R7RmqXE"
},
"source": [
"## Prepare the data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gvVmRVQCmqXF",
"outputId": "d8b8a21c-f52b-4cf2-9c19-399e42a42e4e",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"x_train shape: (60000, 28, 28, 1)\n",
"60000 train samples\n",
"10000 test samples\n"
]
}
],
"source": [
"# Model / data parameters\n",
"num_classes = 10\n",
"input_shape = (28, 28, 1)\n",
"\n",
"# the data, split between train and test sets\n",
"(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
"\n",
"def swap(digit, to_digit, y):\n",
" idx = (y == digit).nonzero()\n",
" y[idx] = to_digit\n",
" return y\n",
"y_train = swap(2, 9, y_train)\n",
"y_test = swap(2, 9, y_test)\n",
"# Scale images to the [0, 1] range\n",
"x_train = x_train.astype(\"float32\") / 255\n",
"x_test = x_test.astype(\"float32\") / 255\n",
"# Make sure images have shape (28, 28, 1)\n",
"x_train = np.expand_dims(x_train, -1)\n",
"x_test = np.expand_dims(x_test, -1)\n",
"print(\"x_train shape:\", x_train.shape)\n",
"print(x_train.shape[0], \"train samples\")\n",
"print(x_test.shape[0], \"test samples\")\n",
"\n",
"\n",
"# convert class vectors to binary class matrices\n",
"y_train = keras.utils.to_categorical(y_train, num_classes)\n",
"y_test = keras.utils.to_categorical(y_test, num_classes)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uLkDN20xmqXG"
},
"source": [
"## Build the model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0pBFjWcvmqXH",
"outputId": "46f81585-eede-462f-e3e2-95960f426563",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Model: \"sequential_8\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" conv2d_17 (Conv2D) (None, 26, 26, 32) 320 \n",
" \n",
" max_pooling2d_17 (MaxPoolin (None, 13, 13, 32) 0 \n",
" g2D) \n",
" \n",
" conv2d_18 (Conv2D) (None, 11, 11, 64) 18496 \n",
" \n",
" max_pooling2d_18 (MaxPoolin (None, 5, 5, 64) 0 \n",
" g2D) \n",
" \n",
" flatten_8 (Flatten) (None, 1600) 0 \n",
" \n",
" dropout_8 (Dropout) (None, 1600) 0 \n",
" \n",
" dense_8 (Dense) (None, 10) 16010 \n",
" \n",
"=================================================================\n",
"Total params: 34,826\n",
"Trainable params: 34,826\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model = keras.Sequential(\n",
" [\n",
" keras.Input(shape=input_shape),\n",
" layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\"),\n",
" layers.MaxPooling2D(pool_size=(2, 2)),\n",
" layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"),\n",
" layers.MaxPooling2D(pool_size=(2, 2)),\n",
" layers.Flatten(),\n",
" layers.Dropout(0.5),\n",
" layers.Dense(num_classes, activation=\"softmax\"),\n",
" ]\n",
")\n",
"\n",
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eOj8XiS_mqXH"
},
"source": [
"## Train the model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "S4rbfZuFmqXI",
"outputId": "a1218510-7fbf-4742-d7a1-645c7c61c41e",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/15\n",
"422/422 [==============================] - 2s 5ms/step - loss: 0.4024 - accuracy: 0.8662 - val_loss: 0.0945 - val_accuracy: 0.9750\n",
"Epoch 2/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.1335 - accuracy: 0.9586 - val_loss: 0.0664 - val_accuracy: 0.9817\n",
"Epoch 3/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0994 - accuracy: 0.9686 - val_loss: 0.0548 - val_accuracy: 0.9845\n",
"Epoch 4/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0815 - accuracy: 0.9738 - val_loss: 0.0481 - val_accuracy: 0.9863\n",
"Epoch 5/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0728 - accuracy: 0.9775 - val_loss: 0.0449 - val_accuracy: 0.9870\n",
"Epoch 6/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0647 - accuracy: 0.9797 - val_loss: 0.0404 - val_accuracy: 0.9897\n",
"Epoch 7/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0605 - accuracy: 0.9804 - val_loss: 0.0381 - val_accuracy: 0.9890\n",
"Epoch 8/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0564 - accuracy: 0.9818 - val_loss: 0.0367 - val_accuracy: 0.9903\n",
"Epoch 9/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0518 - accuracy: 0.9834 - val_loss: 0.0350 - val_accuracy: 0.9903\n",
"Epoch 10/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0497 - accuracy: 0.9845 - val_loss: 0.0363 - val_accuracy: 0.9907\n",
"Epoch 11/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0459 - accuracy: 0.9847 - val_loss: 0.0351 - val_accuracy: 0.9915\n",
"Epoch 12/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0449 - accuracy: 0.9854 - val_loss: 0.0342 - val_accuracy: 0.9910\n",
"Epoch 13/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0445 - accuracy: 0.9854 - val_loss: 0.0367 - val_accuracy: 0.9903\n",
"Epoch 14/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0400 - accuracy: 0.9875 - val_loss: 0.0328 - val_accuracy: 0.9908\n",
"Epoch 15/15\n",
"422/422 [==============================] - 2s 4ms/step - loss: 0.0387 - accuracy: 0.9874 - val_loss: 0.0324 - val_accuracy: 0.9915\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f6d6a576a90>"
]
},
"metadata": {},
"execution_count": 40
}
],
"source": [
"batch_size = 128\n",
"epochs = 15\n",
"\n",
"model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n",
"\n",
"model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "j3GauoznmqXJ"
},
"source": [
"## Evaluate the trained model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8uWAffpXmqXK",
"outputId": "10a709d6-ceaa-4a9b-e3dd-5aef98cba08a",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Test loss: 0.03250782564282417\n",
"Test accuracy: 0.9889000058174133\n"
]
}
],
"source": [
"score = model.evaluate(x_test, y_test, verbose=0)\n",
"print(\"Test loss:\", score[0])\n",
"print(\"Test accuracy:\", score[1])"
]
},
{
"cell_type": "code",
"source": [
"model.save(\"model.h5\")"
],
"metadata": {
"id": "BEpXKle3nIZy"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "sVUELsOEnkgn"
},
"execution_count": null,
"outputs": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Hackthebox Mnist without 2",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
},
"accelerator": "GPU"
},
"nbformat": 4,
"nbformat_minor": 0
}
@tudoanh
Copy link
Author

tudoanh commented May 20, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment