{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### This notebook uses MIRNet model from 🤗 Model Hub to enhance Low Light Images using Keras\n\n![](https://img.youtube.com/vi/JRdWOAqQaUc/hqdefault.jpg)\n\n### Objective\n\nThe objective of the notebook is to showcase how simple and easy it is to use a pre-trained Keras model from Hugging Face and build a Deep Learning powered Tool ","metadata":{}},{"cell_type":"markdown","source":"Associated YouTube Tutorial -https://www.youtube.com/watch?v=JRdWOAqQaUc","metadata":{}},{"cell_type":"markdown","source":"Downloading and installing required libraries","metadata":{}},{"cell_type":"code","source":"! pip install keras","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:22:53.701737Z","iopub.execute_input":"2023-09-14T16:22:53.702238Z","iopub.status.idle":"2023-09-14T16:23:02.881415Z","shell.execute_reply.started":"2023-09-14T16:22:53.702180Z","shell.execute_reply":"2023-09-14T16:23:02.880142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The pre-trained model wil be downloaded from Hugging Face Model Hub, hence we're installing `huggingface_hub`","metadata":{}},{"cell_type":"code","source":"! pip install huggingface_hub","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:02.884131Z","iopub.execute_input":"2023-09-14T16:23:02.884440Z","iopub.status.idle":"2023-09-14T16:23:12.112088Z","shell.execute_reply.started":"2023-09-14T16:23:02.884406Z","shell.execute_reply":"2023-09-14T16:23:12.111127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"These are the other two libraries used in this code but it's available with Kaggle Notebooks without Installations","metadata":{}},{"cell_type":"code","source":"#numpy\n#Pillow","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:12.114178Z","iopub.execute_input":"2023-09-14T16:23:12.114509Z","iopub.status.idle":"2023-09-14T16:23:12.120168Z","shell.execute_reply.started":"2023-09-14T16:23:12.114475Z","shell.execute_reply":"2023-09-14T16:23:12.119195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading required libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np # array manipulation\nfrom huggingface_hub import from_pretrained_keras # download the model\nimport keras # deep learning\nfrom PIL import Image # Image processing","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:12.122881Z","iopub.execute_input":"2023-09-14T16:23:12.123329Z","iopub.status.idle":"2023-09-14T16:23:12.135457Z","shell.execute_reply.started":"2023-09-14T16:23:12.123283Z","shell.execute_reply":"2023-09-14T16:23:12.134487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from PIL import Image # Image processing","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:12.137008Z","iopub.execute_input":"2023-09-14T16:23:12.137341Z","iopub.status.idle":"2023-09-14T16:23:12.145773Z","shell.execute_reply.started":"2023-09-14T16:23:12.137300Z","shell.execute_reply":"2023-09-14T16:23:12.145062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = from_pretrained_keras(\"keras-io/lowlight-enhance-mirnet\", compile=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:12.146865Z","iopub.execute_input":"2023-09-14T16:23:12.147162Z","iopub.status.idle":"2023-09-14T16:23:53.708490Z","shell.execute_reply.started":"2023-09-14T16:23:12.147125Z","shell.execute_reply":"2023-09-14T16:23:53.707402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#! wget","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:53.711791Z","iopub.execute_input":"2023-09-14T16:23:53.712069Z","iopub.status.idle":"2023-09-14T16:23:53.717476Z","shell.execute_reply.started":"2023-09-14T16:23:53.712038Z","shell.execute_reply":"2023-09-14T16:23:53.716457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you happen to read PNG images, it's possible they are read as `RGBA` with a transparency channel.Hence, while reading/opening the input image, we are converting it to `RGB` (removing the transparency channel).\n","metadata":{}},{"cell_type":"code","source":"#005e53b1b6aada 00354cd9244e28\n\nlow_light_img = Image.open('/kaggle/input/image1/lowlight.jpg').convert('RGB')","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:53.719057Z","iopub.execute_input":"2023-09-14T16:23:53.719317Z","iopub.status.idle":"2023-09-14T16:23:53.739793Z","shell.execute_reply.started":"2023-09-14T16:23:53.719286Z","shell.execute_reply":"2023-09-14T16:23:53.738623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low_light_img","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:53.741047Z","iopub.execute_input":"2023-09-14T16:23:53.741300Z","iopub.status.idle":"2023-09-14T16:23:53.907300Z","shell.execute_reply.started":"2023-09-14T16:23:53.741270Z","shell.execute_reply":"2023-09-14T16:23:53.906285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are lot of different methods to resize the image, this is one of the simpler methods but I'd strongly encourage you to check other options here - https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-filters","metadata":{}},{"cell_type":"code","source":"low_light_img = low_light_img.resize((512,512),Image.NEAREST)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:53.910239Z","iopub.execute_input":"2023-09-14T16:23:53.910519Z","iopub.status.idle":"2023-09-14T16:23:53.915761Z","shell.execute_reply.started":"2023-09-14T16:23:53.910484Z","shell.execute_reply":"2023-09-14T16:23:53.914484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low_light_img","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:53.916861Z","iopub.execute_input":"2023-09-14T16:23:53.917149Z","iopub.status.idle":"2023-09-14T16:23:54.034934Z","shell.execute_reply.started":"2023-09-14T16:23:53.917113Z","shell.execute_reply":"2023-09-14T16:23:54.034268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = keras.preprocessing.image.img_to_array(low_light_img)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:54.036070Z","iopub.execute_input":"2023-09-14T16:23:54.036756Z","iopub.status.idle":"2023-09-14T16:23:54.041806Z","shell.execute_reply.started":"2023-09-14T16:23:54.036715Z","shell.execute_reply":"2023-09-14T16:23:54.041036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:54.043175Z","iopub.execute_input":"2023-09-14T16:23:54.043533Z","iopub.status.idle":"2023-09-14T16:23:54.055010Z","shell.execute_reply.started":"2023-09-14T16:23:54.043502Z","shell.execute_reply":"2023-09-14T16:23:54.054344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = image.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:54.056174Z","iopub.execute_input":"2023-09-14T16:23:54.056520Z","iopub.status.idle":"2023-09-14T16:23:54.065934Z","shell.execute_reply.started":"2023-09-14T16:23:54.056490Z","shell.execute_reply":"2023-09-14T16:23:54.064789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:54.067334Z","iopub.execute_input":"2023-09-14T16:23:54.067619Z","iopub.status.idle":"2023-09-14T16:23:54.078247Z","shell.execute_reply.started":"2023-09-14T16:23:54.067587Z","shell.execute_reply":"2023-09-14T16:23:54.077301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = np.expand_dims(image, axis = 0)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:54.079462Z","iopub.execute_input":"2023-09-14T16:23:54.080398Z","iopub.status.idle":"2023-09-14T16:23:54.087864Z","shell.execute_reply.started":"2023-09-14T16:23:54.080359Z","shell.execute_reply":"2023-09-14T16:23:54.087021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:54.089094Z","iopub.execute_input":"2023-09-14T16:23:54.089790Z","iopub.status.idle":"2023-09-14T16:23:54.101352Z","shell.execute_reply.started":"2023-09-14T16:23:54.089751Z","shell.execute_reply":"2023-09-14T16:23:54.100550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model.predict(image) # model inference to enhance the low light pics","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:23:54.102690Z","iopub.execute_input":"2023-09-14T16:23:54.102950Z","iopub.status.idle":"2023-09-14T16:24:27.952028Z","shell.execute_reply.started":"2023-09-14T16:23:54.102919Z","shell.execute_reply":"2023-09-14T16:24:27.951049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_image = output[0] * 255.0","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:27.953523Z","iopub.execute_input":"2023-09-14T16:24:27.953787Z","iopub.status.idle":"2023-09-14T16:24:27.958595Z","shell.execute_reply.started":"2023-09-14T16:24:27.953752Z","shell.execute_reply":"2023-09-14T16:24:27.957689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_image.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:27.959900Z","iopub.execute_input":"2023-09-14T16:24:27.960211Z","iopub.status.idle":"2023-09-14T16:24:27.973168Z","shell.execute_reply.started":"2023-09-14T16:24:27.960176Z","shell.execute_reply":"2023-09-14T16:24:27.972193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_image = output_image.clip(0,255)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:27.974581Z","iopub.execute_input":"2023-09-14T16:24:27.974969Z","iopub.status.idle":"2023-09-14T16:24:27.986697Z","shell.execute_reply.started":"2023-09-14T16:24:27.974919Z","shell.execute_reply":"2023-09-14T16:24:27.985941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_image.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:27.988156Z","iopub.execute_input":"2023-09-14T16:24:27.989208Z","iopub.status.idle":"2023-09-14T16:24:27.997610Z","shell.execute_reply.started":"2023-09-14T16:24:27.989149Z","shell.execute_reply":"2023-09-14T16:24:27.996872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#output_image","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:27.999022Z","iopub.execute_input":"2023-09-14T16:24:27.999701Z","iopub.status.idle":"2023-09-14T16:24:28.006091Z","shell.execute_reply.started":"2023-09-14T16:24:27.999654Z","shell.execute_reply":"2023-09-14T16:24:28.005360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_image = output_image.reshape((np.shape(output_image)[0],np.shape(output_image)[1],3))","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:28.007119Z","iopub.execute_input":"2023-09-14T16:24:28.007869Z","iopub.status.idle":"2023-09-14T16:24:28.018292Z","shell.execute_reply.started":"2023-09-14T16:24:28.007834Z","shell.execute_reply":"2023-09-14T16:24:28.017588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_image","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:28.019432Z","iopub.execute_input":"2023-09-14T16:24:28.019808Z","iopub.status.idle":"2023-09-14T16:24:28.038242Z","shell.execute_reply.started":"2023-09-14T16:24:28.019776Z","shell.execute_reply":"2023-09-14T16:24:28.037317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_image = np.uint32(output_image)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:28.039457Z","iopub.execute_input":"2023-09-14T16:24:28.039847Z","iopub.status.idle":"2023-09-14T16:24:28.046001Z","shell.execute_reply.started":"2023-09-14T16:24:28.039814Z","shell.execute_reply":"2023-09-14T16:24:28.045282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#output_image","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:28.047012Z","iopub.execute_input":"2023-09-14T16:24:28.047906Z","iopub.status.idle":"2023-09-14T16:24:28.059153Z","shell.execute_reply.started":"2023-09-14T16:24:28.047865Z","shell.execute_reply":"2023-09-14T16:24:28.058054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Final Image","metadata":{}},{"cell_type":"code","source":"Image.fromarray(output_image.astype('uint8'),'RGB')","metadata":{"execution":{"iopub.status.busy":"2023-09-14T16:24:28.062528Z","iopub.execute_input":"2023-09-14T16:24:28.063882Z","iopub.status.idle":"2023-09-14T16:24:28.239375Z","shell.execute_reply.started":"2023-09-14T16:24:28.063811Z","shell.execute_reply":"2023-09-14T16:24:28.238565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The end","metadata":{}},{"cell_type":"markdown","source":"# Resources\n\n* [Low Light Image Enhancement in Python & Keras | Pixel-like NightMode using Deep Learning - Tutorial\n](https://www.youtube.com/watch?v=JRdWOAqQaUc)\n\n* [https://huggingface.co/keras-io/lowlight-enhance-mirnet](MIRnet on Hugging Face)\n\n* [Mirnet Keras Documentation](https://keras.io/examples/vision/mirnet/)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}