最简单的keras项目系列(一):经典超分辨率项目espcn

# espcn

espcn是2016年发表的一篇用卷积神经网络实现实时超分辨率的经典论文,其最大的亮度就是用简单的三层全卷积网络和亚像素卷积在保证超分质量的前提下实现大幅提升运行速度,原文链接Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network.本人用keras和tensorflow搭建了一个最简单的espcn项目。github链接https://github.com/zg425/espcn-keras.git

# 项目介绍

训练数据采用的是BSD300数据集,使用tensorflow作为keras后端,模型建立代码

# -*- coding: utf-8 -*-
import numpy as np
from keras.layers import  Input, Conv2D, Lambda
from keras.models import Model
import tensorflow as tf

class espcn:
    def __init__(self, scale_factor=2, image_channels=3):
        self.__name__ = 'espcn'
        self.scale_factor = scale_factor
        self.channels = image_channels

    # upsampling the resolution of image
    def sub_pixel(self, x):
        return tf.depth_to_space(x, self.scale_factor)
        
    # building the espcn network
    def __call__(self, depth=12):
        input_image = Input(shape=(None, None, self.channels), name='x')
        x = Conv2D(64, 5, kernel_initializer='glorot_uniform', padding='same', activation='tanh')(input_image)
        x = Conv2D(32, 3, kernel_initializer='glorot_uniform', padding='same',activation='tanh')(x)
        x = Conv2D(self.scale_factor**2*self.channels, 3, kernel_initializer='glorot_uniform', padding='same',activation='sigmoid')(x)
        x = Lambda(self.sub_pixel)(x)
        model = Model(inputs=input_image, outputs=x)
        return model

其中亚像素卷积使用keras的Lambda功能添加一个自定义层,使用tensorflow的depth_to_space函数实现输出图像分辨率的上采样。项目整体结构很简单,代码中加了详细的注释,喜欢的朋友请点个star支持一下吧!

你可能感兴趣的:(tensorflow,keras)