Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use GAN to save your low-resolution old photos

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use GAN to save your low-resolution old photos, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

1 background of the project

Students who know GAN know that GAN is good at capturing probability distribution, so it is very suitable for image generation tasks. In the process of picture and video shooting and transmission, we often compress the image, which leads to the low resolution of the image. in addition, the resolution of the photos taken by the equipment in the early years is also too low, such as the resolution of 320,240 10 years ago. In order to solve this problem, image super-resolution technology needs to be used.

We use GAN to complete the image super-resolution task, and the preparatory work that needs to be done includes:

(1) it is more efficient to use Linux in Linux or windows systems.

(2) the installed Pytorch needs to be trained by GPU.

2. Brief introduction of principle.

The image super-resolution task input is a low-resolution image, and the output is a picture with increased resolution. Here is a common frame diagram [1]:

The framework first uses the interpolation method to upsample the input image, and then uses the convolution layer to learn the input. The disadvantage of this framework is that the computational cost is relatively high, because the whole network operates on the high-resolution image.

Then the researchers propose to enlarge the resolution at the back end of the network, expand the number of channels, and then redistribute them to obtain high-resolution images, which is called (PixShuffle) [2], so that most of the computation of the whole network is for low-resolution images, as shown in the following figure:

The above constitute the basic idea of image super-resolution, and then the researchers bring GAN into the super-resolution framework [3], which actually increases the confrontation loss, and uses what we often call perceptual loss instead of MSE loss for reconstruction.

On the specific principles of various super-resolution frameworks, you can move to the three AI knowledge planets, or learn by yourself. Since this is a practical column, we do not give a complete introduction to the principle.

3Model training

The data sets of most super-division reconstruction tasks are obtained by downsampling from high-resolution images. ImageNet data sets are often selected in this paper. Because we intend to specifically restore the definition of human faces here, we choose a commonly used high-definition face data set, CelebA-HQ, which was released in 2019, contains 30000 high-definition face images with different attributes, in which the image size is 1024 × 1024, the preview is as follows.

Next, let's interpret the code:

3.1 data preprocessing

Image super-resolution data sets are often sampled from high-resolution images to get low-resolution images, and then form image pairs for training. The following is the core code for data processing in training sets and verification sets:

# # training set High Resolution Image preprocessing function

Def train_hr_transform (crop_size):

Return Compose ([

RandomCrop (crop_size)

ToTensor ()

])

# # pre-processing function of low-resolution image in training set

Def train_lr_transform (crop_size, upscale_factor):

Return Compose ([

ToPILImage ()

Resize (crop_size / / upscale_factor, interpolation=Image.BICUBIC)

ToTensor ()

])

# # training dataset classes

Class TrainDatasetFromFolder (Dataset):

Def _ _ init__ (self, dataset_dir, crop_size, upscale_factor):

Super (TrainDatasetFromFolder, self). _ _ init__ ()

Self.image_filenames = [join (dataset_dir, x) for x in listdir (dataset_dir) if is_image_file (x)] # # get all the images

Crop_size = calculate_valid_crop_size (crop_size, upscale_factor) # # get the cut size

Self.hr_transform = train_hr_transform (crop_size) # # High Resolution Image preprocessing function

Self.lr_transform = train_lr_transform (crop_size, upscale_factor) # # low resolution image preprocessing function

# # dataset iterative pointer

Def _ _ getitem__ (self, index):

Hr_image = self.hr_transform (Image.open (self.image_ filenames [index])) # # Random clipping to get high resolution images

Lr_image = self.lr_transform (hr_image) # # to get a low resolution image

Return lr_image, hr_image

Def _ len__ (self):

Return len (self.image_filenames)

# # verifying dataset classes

Class ValDatasetFromFolder (Dataset):

Def _ _ init__ (self, dataset_dir, upscale_factor):

Super (ValDatasetFromFolder, self). _ _ init__ ()

Self.upscale_factor = upscale_factor

Self.image_filenames = [join (dataset_dir, x) for x in listdir (dataset_dir) if is_image_file (x)]

This is the answer to the question about how to use GAN to save your old low-resolution photos. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report