[PYTHON] I tried to build a super-resolution method / SRCNN ①

Overview

I have created a program for SRCNN, which is one of the super resolution methods, so I will post it. It's going to be long, so I plan to divide it into three parts. Continued: I tried to build a super-resolution method / SRCNN② Continued: I tried to build a super-resolution method / SRCNN ③

table of contents

1.First of all 2. PC environment 3. Code description 4. At the end

1.First of all

Super-resolution is a technology that improves the resolution of low-resolution images and moving images, and SRCNN uses deep learning to measure results with higher accuracy than conventional methods. It is the method that was done. This time, I have implemented this SRCNN, so I will post it.

The full code is also posted on GitHub, so please check there. https://github.com/morisumori/srcnn_keras

2. PC environment

cpu : intel corei7 8th Gen gpu : NVIDIA GeForce RTX 1080ti os : ubuntu 20.04

3. Code description

As you can see from GitHub, it mainly consists of three codes. ・ Datacreate.py → Data set generation program ・ Model.py → SRCNN program ・ Main.py → Execution program I have created a function with datacreate.py and model.py and executed it with main.py.

__ This time I will explain datacreate.py. __

Description of detacreate.py

datacreate.py



import cv2
import os
import random
import glob
import numpy as np
import tensorflow as tf

#A program that cuts out an arbitrary number of frames
def save_frame(path,        #The path of the file that contains the data
               data_number, #Number of photos to cut from one image
               cut_height,  #Storage size(Vertical)
               cut_width,   #Storage size(side)
               mag,         #Reduction magnification
               ext='jpg'):

    #Generate a list of datasets
    low_data_list = []
    high_data_list = []

    path = path + "/*"
    files = glob.glob(path)
    
    for img in files:
        img = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
        H, W = img.shape

        if cut_height > H or cut_width > W:
            return

        for q in range(data_number):
            ram_h = random.randint(0, H - cut_height)
            ram_w = random.randint(0, W - cut_width)
          
            cut_img = img[ram_h : ram_h + cut_height, ram_w: ram_w + cut_width]
            
            #Blur with Gaussian filter
            img1 = cv2.GaussianBlur(img, (5, 5), 0)
            img2 = img1[ram_h : ram_h + cut_height, ram_w: ram_w + cut_width]
            
            high_data_list.append(cut_img)
            low_data_list.append(img2)
    
    #numpy → tensor +Normalization
    low_data_list = tf.convert_to_tensor(low_data_list, np.float32)
    high_data_list = tf.convert_to_tensor(high_data_list, np.float32)
    low_data_list /= 255
    high_data_list /= 255

    return low_data_list, high_data_list

From here, I will explain individually.

def save_frame(path,        #The path of the file that contains the data
               data_number, #Number of photos to cut from one image
               cut_height,  #Storage size(Vertical)
               cut_width,   #Storage size(side)
               mag,         #Reduction magnification
               ext='jpg'):

Here is the definition of the function. As I wrote in the comment out, path is the path of the file. (For example, if you have a photo in a file named file, type "./file".) data_number cuts out multiple photos and turns the data. cut_height and cut_wedth are the size of the data. I don't use mag ... (because I used a Gaussian filter to generate low resolution images this time)

    path = path + "/*"
    files = glob.glob(path)

This is a list of all the photos in the file.

for img in files:
        img = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
        H, W = img.shape

        if cut_height > H or cut_width > W:
            return

        for q in range(data_number):
            ram_h = random.randint(0, H - cut_height)
            ram_w = random.randint(0, W - cut_width)
          
            cut_img = img[ram_h : ram_h + cut_height, ram_w: ram_w + cut_width]
            
            #Blur with Gaussian filter
            img1 = cv2.GaussianBlur(img, (5, 5), 0)
            img2 = img1[ram_h : ram_h + cut_height, ram_w: ram_w + cut_width]
            
            high_data_list.append(cut_img)
            low_data_list.append(img2)

Here, I take out the photos listed earlier one by one and cut out as many as the number of data_number. I'm using random.randint because I want to randomly cut the location. Then, it is blurred with a Gaussian filter to generate a low resolution image. Finally, I add it to the list with append.

    #numpy → tensor +Normalization
    low_data_list = tf.convert_to_tensor(low_data_list, np.float32)
    high_data_list = tf.convert_to_tensor(high_data_list, np.float32)
    low_data_list /= 255
    high_data_list /= 255

    return low_data_list, high_data_list

Here, keras and tensorflow need to convert to tensor instead of numpy array, so conversion is done. At the same time, normalization is also done here.

Finally, the function ends with a list containing low-resolution images and a list containing high-resolution images.

4. At the end

This time, I explained the data generation & preprocessing program. Next, I will explain the program of the SRCNN model. Continued: I tried to build a super-resolution method / SRCNN② Continued: I tried to build a super-resolution method / SRCNN ③

Since this is my first time writing an article, it may be difficult to understand, but if you have any questions or comments, please do not hesitate to contact me!

Recommended Posts

I tried to build a super-resolution method / SRCNN ①
I tried to build a super-resolution method / SRCNN ③
I tried to build a super-resolution method / SRCNN ②
I tried to build a super-resolution method / ESPCN
[Go + Gin] I tried to build a Docker environment
I tried "How to get a method decorated in Python"
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I want to build a Python environment
I tried to make a Web API
I tried to debug.
I tried to paste
I tried to generate a random character string
I tried to build a Mac Python development environment with pythonz + direnv
I tried to make a ○ ✕ game using TensorFlow
I tried to make a "fucking big literary converter"
I tried to create a table only with Django
I tried to draw a route map with Python
I tried to implement a pseudo pachislot in Python
I tried to implement a recommendation system (content-based filtering)
I want to easily build a model-based development environment
I tried to build ML Pipeline with Cloud Composer
I tried to automatically generate a password with Python3
CTF beginner tried to build a problem server (web) [Problem]
I tried to simulate the dollar cost averaging method
I added a function to CPython (build & structure grasp)
I tried to draw a configuration diagram using Diagrams
I tried to learn PredNet
I tried to organize SVM.
I tried to implement PCANet
I tried to reintroduce Linux
I tried to introduce Pylint
I tried to summarize SparseMatrix
I tried to touch jupyter
I tried to implement StarGAN (1)
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
I tried to create a class to search files with Python's Glob method in VBA
I tried to implement a volume moving average with Quantx
I tried to implement a basic Recurrent Neural Network model
I tried the super-resolution algorithm "PULSE" in a Windows environment
I tried to implement a one-dimensional cellular automaton in Python
[Markov chain] I tried to read a quote into Python.
I tried to solve a combination optimization problem with Qiskit
I tried to get started with Hy ・ Define a class
I tried to automate [a certain task] using Raspberry Pi
I stumbled when I tried to install Basemap, so a memorandum
I tried to sort a random FizzBuzz column with bubble sort.
I tried to create a bot for PES event notification
I tried to make a stopwatch using tkinter in python
I tried to divide with a deep learning language model
I tried to make a simple text editor using PyQt
When I tried to build a Rails environment on WSL2 (Ubuntu 20.04LTS), I stumbled and fell.
I tried to build a service that sells machine-learned data at explosive speed with Docker
I tried to implement Deep VQE
I tried to create Quip API
I tried to implement adversarial validation
I tried to explain Pytorch dataset
I tried Watson Speech to Text
I tried to touch Tesla's API
I tried to implement hierarchical clustering
I tried to organize about MCMC.