{"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":"markdown","source":"# Introudction\n    In this notebook, many methods and networks that concern and work on deep learning will be explained mainly, as each time a specific idea is added in a different way so that we are able to learn greatly, benefit and benefit from each other. Here, of course, the famous data (MNIST) is used, which is Which we work on all the time. We hope to learn together by adding your opinion in the comments if you see something that should be added to this netbook and do not forget to vote so that everyone can see it and be able to learn.","metadata":{}},{"cell_type":"code","source":"pip install ipyplot","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-12T17:35:48.988255Z","iopub.execute_input":"2022-03-12T17:35:48.988526Z","iopub.status.idle":"2022-03-12T17:35:58.031132Z","shell.execute_reply.started":"2022-03-12T17:35:48.988498Z","shell.execute_reply":"2022-03-12T17:35:58.030126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras.datasets import mnist\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Activation, Dropout\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.utils import to_categorical, plot_model\nfrom tensorflow.keras.layers import Dense, Activation, SimpleRNN\nfrom tensorflow.keras.layers import Flatten, concatenate\nimport ipyplot\nfrom PIL import Image as Img\nfrom tensorflow.keras.datasets import mnist\nimport cv2\nfrom tqdm.notebook import trange","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.033500Z","iopub.execute_input":"2022-03-12T17:35:58.033769Z","iopub.status.idle":"2022-03-12T17:35:58.045941Z","shell.execute_reply.started":"2022-03-12T17:35:58.033741Z","shell.execute_reply":"2022-03-12T17:35:58.044102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load mnist dataset\n(x_train, y_train), (x_test, y_test) = mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.047508Z","iopub.execute_input":"2022-03-12T17:35:58.047860Z","iopub.status.idle":"2022-03-12T17:35:58.430451Z","shell.execute_reply.started":"2022-03-12T17:35:58.047815Z","shell.execute_reply":"2022-03-12T17:35:58.429370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show Some of data train image\nipyplot.plot_images(x_train, max_images=10, img_width=120)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.432411Z","iopub.execute_input":"2022-03-12T17:35:58.432669Z","iopub.status.idle":"2022-03-12T17:35:58.457706Z","shell.execute_reply.started":"2022-03-12T17:35:58.432639Z","shell.execute_reply":"2022-03-12T17:35:58.456996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train Data:\", x_train.shape)\nprint(\"Train Data:\", y_train.shape)\nprint(\"Train Data:\", x_test.shape)\nprint(\"Train Data:\", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.458818Z","iopub.execute_input":"2022-03-12T17:35:58.459133Z","iopub.status.idle":"2022-03-12T17:35:58.467173Z","shell.execute_reply.started":"2022-03-12T17:35:58.459100Z","shell.execute_reply":"2022-03-12T17:35:58.465538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compute the number of labels\nnum_labels = len(np.unique(y_train))\nprint(\"Number Of Labels:\",num_labels)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.468624Z","iopub.execute_input":"2022-03-12T17:35:58.468931Z","iopub.status.idle":"2022-03-12T17:35:58.480264Z","shell.execute_reply.started":"2022-03-12T17:35:58.468872Z","shell.execute_reply":"2022-03-12T17:35:58.479158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert to one-hot vector\ny_train = to_categorical(y_train)\ny_test = to_categorical(y_test)\nprint(\"Train after make To_Categorical:\", y_train.shape)\nprint(\"Test after make To_Categorical:\", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.481787Z","iopub.execute_input":"2022-03-12T17:35:58.482375Z","iopub.status.idle":"2022-03-12T17:35:58.495482Z","shell.execute_reply.started":"2022-03-12T17:35:58.482328Z","shell.execute_reply":"2022-03-12T17:35:58.494668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# input image dimensions\nimage_size = x_train.shape[1]\nprint(\"Image Size:\", image_size)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.496972Z","iopub.execute_input":"2022-03-12T17:35:58.497199Z","iopub.status.idle":"2022-03-12T17:35:58.507629Z","shell.execute_reply.started":"2022-03-12T17:35:58.497171Z","shell.execute_reply":"2022-03-12T17:35:58.506345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resize and normalize\nx_train = np.reshape(x_train,[-1, image_size, image_size, 1])\nx_test = np.reshape(x_test,[-1, image_size, image_size, 1])\nprint(\"Train After Make Normalize:\",x_train.shape)\nprint(\"Test After Make Normalize:\",x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.508681Z","iopub.execute_input":"2022-03-12T17:35:58.509007Z","iopub.status.idle":"2022-03-12T17:35:58.522590Z","shell.execute_reply.started":"2022-03-12T17:35:58.508975Z","shell.execute_reply":"2022-03-12T17:35:58.521660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.astype('float32') / 255\nx_test = x_test.astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.525284Z","iopub.execute_input":"2022-03-12T17:35:58.525527Z","iopub.status.idle":"2022-03-12T17:35:58.651771Z","shell.execute_reply.started":"2022-03-12T17:35:58.525493Z","shell.execute_reply":"2022-03-12T17:35:58.650659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train After Make Normalize:\",x_train.shape)\nprint(\"Test After Make Normalize:\",x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.653447Z","iopub.execute_input":"2022-03-12T17:35:58.653890Z","iopub.status.idle":"2022-03-12T17:35:58.661600Z","shell.execute_reply.started":"2022-03-12T17:35:58.653856Z","shell.execute_reply":"2022-03-12T17:35:58.660399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# network parameters\n# image is processed as is (square grayscale)\ninput_shape = (image_size, image_size, 1)\nbatch_size = 128\nkernel_size = 3\npool_size = 2\nfilters = 64\ndropout = 0.2","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.663838Z","iopub.execute_input":"2022-03-12T17:35:58.664195Z","iopub.status.idle":"2022-03-12T17:35:58.674862Z","shell.execute_reply.started":"2022-03-12T17:35:58.664152Z","shell.execute_reply":"2022-03-12T17:35:58.674057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model is a stack of CNN-ReLU-MaxPooling\nmodel = Sequential()\nmodel.add(Conv2D(filters=filters,\n        kernel_size=kernel_size,\n        activation='relu',\n        input_shape=input_shape))\n\n\nmodel.add(MaxPooling2D(pool_size))\nmodel.add(Conv2D(filters=filters,\n        kernel_size=kernel_size,\n        activation='relu'))\nmodel.add(MaxPooling2D(pool_size))\nmodel.add(Conv2D(filters=filters,\n        kernel_size=kernel_size,\n        activation='relu'))\nmodel.add(Flatten())\n# dropout added as regularizer\nmodel.add(Dropout(dropout))\n# output layer is 10-dim one-hot vector\nmodel.add(Dense(num_labels))\nmodel.add(Activation('softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.676139Z","iopub.execute_input":"2022-03-12T17:35:58.677166Z","iopub.status.idle":"2022-03-12T17:35:58.767517Z","shell.execute_reply.started":"2022-03-12T17:35:58.677117Z","shell.execute_reply":"2022-03-12T17:35:58.766380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model, to_file='cnn-mnist.png', show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.770382Z","iopub.execute_input":"2022-03-12T17:35:58.771359Z","iopub.status.idle":"2022-03-12T17:35:58.921652Z","shell.execute_reply.started":"2022-03-12T17:35:58.771320Z","shell.execute_reply":"2022-03-12T17:35:58.920721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loss function for one-hot vector\n# use of adam optimizer\n# accuracy is good metric for classification tasks\nmodel.compile(loss='categorical_crossentropy',\noptimizer='adam',\nmetrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.923239Z","iopub.execute_input":"2022-03-12T17:35:58.923512Z","iopub.status.idle":"2022-03-12T17:35:58.937581Z","shell.execute_reply.started":"2022-03-12T17:35:58.923481Z","shell.execute_reply":"2022-03-12T17:35:58.936719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train the network\nmodel.fit(x_train, y_train, epochs=10, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T17:35:58.938783Z","iopub.execute_input":"2022-03-12T17:35:58.939051Z","iopub.status.idle":"2022-03-12T17:40:56.285877Z","shell.execute_reply.started":"2022-03-12T17:35:58.939020Z","shell.execute_reply":"2022-03-12T17:40:56.285114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}