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 ③
1.First of all 2. PC environment 3. Code description 4. At the end
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
cpu : intel corei7 8th Gen gpu : NVIDIA GeForce RTX 1080ti os : ubuntu 20.04
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. __
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.
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