{"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":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.layers import Input, Dense\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Conv2D, Flatten, Reshape, Conv2DTranspose\nfrom tensorflow.keras import backend as K\nimport numpy as np\nimport matplotlib.pyplot as plt\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-02T07:24:54.712508Z","iopub.execute_input":"2024-01-02T07:24:54.712970Z","iopub.status.idle":"2024-01-02T07:25:06.622919Z","shell.execute_reply.started":"2024-01-02T07:24:54.712942Z","shell.execute_reply":"2024-01-02T07:25:06.622105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n(x_train, _), (x_test, _) = mnist.load_data()\n\n#reshape and normalize\nimg_size = x_train.shape[1]\nx_train = np.reshape(x_train, [-1, img_size, img_size, 1])\nx_train = x_train.astype('float32') / 255\nx_test = np.reshape(x_test, [-1, img_size, img_size, 1])\nx_test = x_test.astype('float32') / 255\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-02T07:25:21.274792Z","iopub.execute_input":"2024-01-02T07:25:21.275441Z","iopub.status.idle":"2024-01-02T07:25:23.255586Z","shell.execute_reply.started":"2024-01-02T07:25:21.275407Z","shell.execute_reply":"2024-01-02T07:25:23.254667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = [img_size, img_size, 1]\nbatch_size = 32\nkernel_size = 3\nlatent_dim = 16","metadata":{"execution":{"iopub.status.busy":"2024-01-02T07:25:28.213141Z","iopub.execute_input":"2024-01-02T07:25:28.213616Z","iopub.status.idle":"2024-01-02T07:25:28.218383Z","shell.execute_reply.started":"2024-01-02T07:25:28.213580Z","shell.execute_reply":"2024-01-02T07:25:28.217391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer_filters = [32, 64]\n\n#build encoder\ninputs = Input(shape=input_shape, name = 'encoder_shape')\nx = inputs\nfor filters in layer_filters:\n    x = Conv2D(filters = filters,\n               kernel_size = kernel_size,\n               activation = 'relu',\n               strides = 2,\n               padding = 'same')(x)\n    \n# shape info needed to build decoder model\n# so we don't do hand computation\n# the input to the decoder's first\n# Conv2DTranspose will have this shape\n# shape is (7, 7, 64) which is processed by\n# the decoder back to (28, 28, 1)\nshape = K.int_shape(x)\nx = Flatten()(x)\nlatent = Dense(latent_dim, name = 'latent_vector')(x)\nencoder = Model(inputs, latent, name = 'encoder')\nencoder.summary()\n\n#build decoder\nlatent_inputs = Input(shape=(latent_dim), name = 'decoder_shape')\n# use the shape (7, 7, 64) that was earlier saved\nx = Dense(shape[1] * shape[2] * shape[3])(latent_inputs)\nx = Reshape((shape[1], shape[2], shape[3]))(x)\n\n\n# stack of Conv2DTranspose(64)-Conv2DTranspose(32)\nfor filters in layer_filters[::-1]:\n    x = Conv2DTranspose(filters=filters,\n                        kernel_size=kernel_size,\n                        activation='relu',\n                        strides=2,\n                        padding='same')(x)\n\n# reconstruct the input\noutputs = Conv2DTranspose(filters=1,\n                          kernel_size=kernel_size,\n                          activation='sigmoid',\n                          padding='same',\n                          name='decoder_output')(x)\n\n# instantiate decoder model\ndecoder = Model(latent_inputs, outputs, name='decoder')\ndecoder.summary()\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-02T07:34:58.414560Z","iopub.execute_input":"2024-01-02T07:34:58.415454Z","iopub.status.idle":"2024-01-02T07:34:58.562679Z","shell.execute_reply.started":"2024-01-02T07:34:58.415422Z","shell.execute_reply":"2024-01-02T07:34:58.561814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#autoencoder = encoder + decoder\nautoencoder = Model(inputs, decoder(encoder(inputs)), name = 'autoencoder')\nautoencoder.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-02T07:35:24.034642Z","iopub.execute_input":"2024-01-02T07:35:24.035031Z","iopub.status.idle":"2024-01-02T07:35:24.114089Z","shell.execute_reply.started":"2024-01-02T07:35:24.035002Z","shell.execute_reply":"2024-01-02T07:35:24.113148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nautoencoder.compile(loss = 'mse', optimizer = 'adam')\nautoencoder.fit(x_train,\n                x_train,\n                validation_data = (x_test, x_test),\n                epochs = 1,\n                batch_size = batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-02T07:37:01.990733Z","iopub.execute_input":"2024-01-02T07:37:01.991543Z","iopub.status.idle":"2024-01-02T07:37:12.912985Z","shell.execute_reply.started":"2024-01-02T07:37:01.991508Z","shell.execute_reply":"2024-01-02T07:37:12.912073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicitions = autoencoder.predict(x_test)\n\n#disply the images\nimgs = np.concatenate([x_test[:8], predicitions[:8]])\nimgs = imgs.reshape((4, 4, img_size, img_size))\nimgs = np.vstack([np.hstack(i) for i in imgs])\nplt.figure()\nplt.axis('off')\nplt.title('Input: 1st 2 rows, Decoded: last 2 rows')\nplt.imshow(imgs, interpolation='none', cmap='gray')\nplt.savefig('input_and_decoded.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-02T07:37:54.251919Z","iopub.execute_input":"2024-01-02T07:37:54.252834Z","iopub.status.idle":"2024-01-02T07:37:55.318257Z","shell.execute_reply.started":"2024-01-02T07:37:54.252796Z","shell.execute_reply":"2024-01-02T07:37:55.317372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Denoising AutoEncoder**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.layers import Input, Dense\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Conv2D, Flatten, Reshape, Conv2DTranspose\nfrom tensorflow.keras import backend as K\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n(x_train, _), (x_test, _) = mnist.load_data()\n\n#reshape and normalize\nimg_size = x_train.shape[1]\nx_train = np.reshape(x_train, [-1, img_size, img_size, 1])\nx_train = x_train.astype('float32') / 255\nx_test = np.reshape(x_test, [-1, img_size, img_size, 1])\nx_test = x_test.astype('float32') / 255\n\n#add noise\n# loc represent mean or center and its formula is (high + low) / 2\n# scale represent standard deviation\n\nnoise = np.random.normal(loc=0.5, scale=0.5, size=x_train.shape)\nx_train_noise = x_train + noise\nnoise = np.random.normal(loc=0.5, scale=0.5, size=x_test.shape)\nx_test_noise = x_test + noise\n\n#clip the value to make sure it is between 0.1 and 0.9\nx_train_noise = np.clip(x_train_noise, 0., 1.)\nx_test_noise = np.clip(x_test_noise, 0., 1.)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-02T15:58:29.047181Z","iopub.execute_input":"2024-01-02T15:58:29.047638Z","iopub.status.idle":"2024-01-02T15:58:31.224230Z","shell.execute_reply.started":"2024-01-02T15:58:29.047600Z","shell.execute_reply":"2024-01-02T15:58:31.223350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (img_size, img_size, 1)\nbatch_size = 32\nkernel_size = 3\nlatent_dim = 16\nlayer_filters = [32, 64]\n\n#build encoder\ninputs = Input(shape=input_shape, name = 'encoder_shape')\nx = inputs\n\nfor filters in layer_filters:\n    x = Conv2D(filters = filters,\n               kernel_size = kernel_size,\n               strides = 2,\n               activation = 'relu',\n               padding = 'same')(x)\n    \n#shape info needed to build decoder model\nshape = K.int_shape(x)\n\nx = Flatten()(x)\nlatent = Dense(latent_dim, name = 'latent_vector')(x)\nencoder = Model(inputs, latent, name = 'encoder')\nencoder.summary()\n\n#build decoder\nlatent_input = Input(shape=(latent_dim,), name = 'decoder_shape')\nx = Dense(shape[1] * shape[2] * shape[3])(latent_input)\n# vectorized representation for transpose conv layer to accept\nx = Reshape((shape[1], shape[2], shape[3]))(x)\n\nfor filters in layer_filters[::-1]:\n    x = Conv2DTranspose(filters = filters,\n                        kernel_size = kernel_size,\n                        strides = 2,\n                        activation = 'relu',\n                        padding = 'same')(x)\noutputs = Conv2DTranspose(filters = 1,\n                          kernel_size = kernel_size,\n                          padding = 'same',\n                          activation = 'sigmoid',\n                          name = 'decoder_output')(x)\n\ndecode = Model(latent_input, outputs, name = 'decoder')\ndecode.summary()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-02T15:58:31.226029Z","iopub.execute_input":"2024-01-02T15:58:31.226870Z","iopub.status.idle":"2024-01-02T15:58:31.382368Z","shell.execute_reply.started":"2024-01-02T15:58:31.226829Z","shell.execute_reply":"2024-01-02T15:58:31.380992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#autoencoder = encoder + decoder\nautoencoder = Model(inputs, decode(encoder(inputs)), name = 'autoencoder')\nautoencoder.summary()\n\nautoencoder.compile(loss = 'mse', optimizer = 'adam')\nautoencoder.fit(x_train_noise,\n                x_train,\n                validation_data = (x_test_noise, x_test),\n                epochs = 20,\n                batch_size = batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T15:58:37.724684Z","iopub.execute_input":"2024-01-02T15:58:37.725747Z","iopub.status.idle":"2024-01-02T16:01:10.046128Z","shell.execute_reply.started":"2024-01-02T15:58:37.725708Z","shell.execute_reply":"2024-01-02T16:01:10.045170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predict the autoencoder output from test data\npredictions = autoencoder.predict(x_test_noise)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:01:10.047842Z","iopub.execute_input":"2024-01-02T16:01:10.048127Z","iopub.status.idle":"2024-01-02T16:01:10.967332Z","shell.execute_reply.started":"2024-01-02T16:01:10.048102Z","shell.execute_reply":"2024-01-02T16:01:10.966546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows, cols = 3, 9\nnum = rows * cols\nimgs = np.concatenate([x_test[:num], x_test_noise[:num], predictions[:num]])\nimgs = imgs.reshape((rows * 3, cols, img_size, img_size))\nimgs = np.vstack(np.split(imgs, rows, axis = 1))\nimgs = imgs.reshape((rows * 3, -1, img_size, img_size))\nimgs = np.vstack([np.hstack(i) for i in imgs])\nimgs = (imgs * 255).astype(np.uint8)\nplt.figure()\nplt.axis('off')\nplt.title('Original Images: top rows, '\n          'Corrupted Input: middle rows, '\n          'Denoised Input: third rows')\nplt.imshow(imgs, interpolation = 'none', cmap = 'gray')\n# plt.savefig('noisy_mnist.png')","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:01:10.968467Z","iopub.execute_input":"2024-01-02T16:01:10.968773Z","iopub.status.idle":"2024-01-02T16:01:11.183194Z","shell.execute_reply.started":"2024-01-02T16:01:10.968748Z","shell.execute_reply":"2024-01-02T16:01:11.182258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}