{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nimport tensorflow_datasets as tfds","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-28T10:45:35.603338Z","iopub.execute_input":"2023-12-28T10:45:35.603682Z","iopub.status.idle":"2023-12-28T10:45:48.708446Z","shell.execute_reply.started":"2023-12-28T10:45:35.603653Z","shell.execute_reply":"2023-12-28T10:45:48.707453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Создаем (загружаем) датасеты ","metadata":{}},{"cell_type":"code","source":"# Construct a tf.data.Dataset\n(train, test), info = tfds.load(\n    'mnist',\n    split=['train', 'test'],\n    shuffle_files=True,\n    as_supervised=True,\n    with_info=True,\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:45:48.710182Z","iopub.execute_input":"2023-12-28T10:45:48.711809Z","iopub.status.idle":"2023-12-28T10:46:00.296279Z","shell.execute_reply.started":"2023-12-28T10:45:48.711779Z","shell.execute_reply":"2023-12-28T10:46:00.295324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Вспомогательные функции","metadata":{}},{"cell_type":"code","source":"def normalize_img(image, label):\n    \"\"\"Normalizes images: `uint8` -> `float32`.\"\"\"\n    return tf.cast(image, tf.float32) / 255., label\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:46:00.297415Z","iopub.execute_input":"2023-12-28T10:46:00.297682Z","iopub.status.idle":"2023-12-28T10:46:00.303122Z","shell.execute_reply.started":"2023-12-28T10:46:00.297658Z","shell.execute_reply":"2023-12-28T10:46:00.302143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Модели","metadata":{}},{"cell_type":"code","source":"train = train.map(\n    normalize_img, num_parallel_calls=tf.data.AUTOTUNE)\ntrain = train.cache()\ntrain = train.shuffle(info.splits['train'].num_examples)\ntrain = train.batch(128)\ntrain = train.prefetch(tf.data.AUTOTUNE)\n\ntest = test.map(\n    normalize_img, num_parallel_calls=tf.data.AUTOTUNE)\ntest = test.batch(128)\ntest = test.cache()\ntest = test.prefetch(tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:46:00.305149Z","iopub.execute_input":"2023-12-28T10:46:00.305412Z","iopub.status.idle":"2023-12-28T10:46:00.376972Z","shell.execute_reply.started":"2023-12-28T10:46:00.305389Z","shell.execute_reply":"2023-12-28T10:46:00.376179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Take batch from our dataset to explore\nexample = None\nfor item in train.take(1):\n    example = item\n    break\n\nimage, label = example[0], example[1]\n\ni = 1\nprint(f\"Number: {int(label[i])}\\n\")\n\nplt.imshow(image[i], cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:46:00.377916Z","iopub.execute_input":"2023-12-28T10:46:00.378166Z","iopub.status.idle":"2023-12-28T10:46:03.770915Z","shell.execute_reply.started":"2023-12-28T10:46:00.378144Z","shell.execute_reply":"2023-12-28T10:46:03.770047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 10\n\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (5,5), padding='same', activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.Conv2D(32, (5,5), padding='same', activation='relu'),\n    tf.keras.layers.MaxPool2D(),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(64, (3,3), padding='same', activation='relu'),\n    tf.keras.layers.Conv2D(64, (3,3), padding='same', activation='relu'),\n    tf.keras.layers.MaxPool2D(strides=(2,2)),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(num_classes, activation='softmax')\n])\n\nmodel.compile(optimizer=tf.keras.optimizers.RMSprop(epsilon=1e-08), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:56:38.044460Z","iopub.execute_input":"2023-12-28T10:56:38.044819Z","iopub.status.idle":"2023-12-28T10:56:38.148764Z","shell.execute_reply.started":"2023-12-28T10:56:38.044789Z","shell.execute_reply":"2023-12-28T10:56:38.148011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 8\n\nhistory = model.fit(train,\n                    epochs=epochs,\n                    validation_data=test,\n#                     callbacks=[earlystopping]\n                   )","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:56:38.150286Z","iopub.execute_input":"2023-12-28T10:56:38.150560Z","iopub.status.idle":"2023-12-28T10:57:08.713239Z","shell.execute_reply.started":"2023-12-28T10:56:38.150535Z","shell.execute_reply":"2023-12-28T10:57:08.712344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2,1)\nax[0].plot(history.history['loss'], color='b', label=\"Training Loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"Validation Loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['sparse_categorical_accuracy'], color='b', label=\"Training Accuracy\")\nax[1].plot(history.history['val_sparse_categorical_accuracy'], color='r',label=\"Validation Accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:57:08.714372Z","iopub.execute_input":"2023-12-28T10:57:08.714658Z","iopub.status.idle":"2023-12-28T10:57:09.171359Z","shell.execute_reply.started":"2023-12-28T10:57:08.714634Z","shell.execute_reply":"2023-12-28T10:57:09.170404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Saving the model","metadata":{}},{"cell_type":"code","source":"!rm -rf digit-recognition.h5 digit-recognition/\nmodel.save('digit-recognition.keras')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T11:07:48.155487Z","iopub.execute_input":"2023-12-28T11:07:48.155847Z","iopub.status.idle":"2023-12-28T11:07:49.230916Z","shell.execute_reply.started":"2023-12-28T11:07:48.155818Z","shell.execute_reply":"2023-12-28T11:07:49.229789Z"},"trusted":true},"execution_count":null,"outputs":[]}]}