基于深度学习的图像超分辨率(VDSR, SRCNN,FSTRCNN)

本文深入解析了VDSR(准确图像超分辨率使用非常深的卷积网络)的原理与实现,介绍了如何通过20层3x3卷积核训练图像高频信息,以实现从低分辨率到高分辨率图像的转换。文章提供了详细的网络结构描述,包括参数配置、数据集处理及Python代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)VDSR

(二)SRCNN

(三)FSTRCNN

VDSR

原理

论文:AccurateImageSuper-ResolutionUsingVeryDeepConvolutionalNetworks
个人理解:高分辨率图像可以认为是低分辨率图像与图像高频部分的叠加,即:高分辨率图像(HR)=低分辨率图像(ILR)+图像高频信息,我们要构建的网络要训练的就是图像的高频部分。

在这里插入图片描述

网络结构:
1) 网络参数:20个3x3的卷积核,每个卷积核的通道数为64(上图中conv.1~ReLu.D-1)
2) 图像输出:1个3x3的卷积核,通道为1(上图中Conv.D(Residual))

数据集:
输入为模糊图像,可采用插值后的模糊图像,也可直接用模糊核对图像进行模糊处理。(原文用的是插值,然后再将图像放缩到需要的大小)

代码(其中文件地址需要修改):

环境为window下的python 3.7,架构为keras(backend=tensorflow),有问题的话可以留言交流

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import numpy as np
import tensorflow as tf
from keras import layers
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from keras.models import Model,load_model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model

import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
import scipy.misc
K.set_learning_phase(1)
#import kt_utils
#import resnets_utils

from PIL import Image
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
#定义vdsr的卷积结构
def conv_block(X,f=3,filters=64):
    """
    X:(m,n_H_prev,n_W_prev,n_C_prev)输入数据
    f:integer,卷积核的大小,vdsr用的是3x3 64层的卷积核
    filters:list of integer,每层卷积层中卷积核的数量
    stage:integer 阶段几
    block:string/character 块名字
    """
    #获得各层卷积核数量
    F1=filters
    #保存输入,用于最后与处理后的数据相加
    X_shortcut=X
    
    #第一部分
    for i in range(1,21):
        X=Conv2D(filters=F1,kernel_size=(f,f),strides=(1,1),padding='same',kernel_initializer = 'glorot_uniform')(X)
        X=BatchNormalization(axis=3,name='bn'+str(i))(X)
        X=Activation('relu')(X)
        
    #图像输出层
    X=Conv2D(filters=1,kernel_size=(3,3),strides=(1,1),padding='same',kernel_initializer = 'glorot_uniform')(X)
    X=Add()([X,X_shortcut])
    
    
    X=Activation('relu')(X)
   
    
    return X
#%%
def vdsr_model(input_shape=(289,387,3),output_shape=(289,387,3)):
    X_input=Input(input_shape);
    X=conv_block(X_input)
    
    
     #创建模型
    model=Model(inputs=X_input,outputs=X,name="VDSR");
    
    return model;

 #载入数据
import os
from PIL import Image
import numpy as np
import h5py

def read_picture(path, n_C):
    import matplotlib.pyplot as plt
    # function:读取path路径下的图片,并转为形状为[m,n_H,n_W,n_C]的数组
    # path:str,图片所在路径
    # n_C:int,图像维数,黑白图像输入1,rgb图像输入3
    # datas:返回维度为(m,n_H,n_W,n_C)的array(数组)矩阵
    datas = []
    x_dirs = os.listdir(path)
    for x_file in x_dirs:
        fpath = os.path.join(path, x_file)
        if n_C == 1:
            _x = Image.open(fpath).convert("L")
            # plt.imshow(_x,"gray")   #显示图像(只显示最后一张)
        elif n_C == 3:
            _x = Image.open(fpath)
            # plt.imshow(_x)         #显示图像(只显示最后一张)
        else:
            print("错误:图像维数错误")
        n_W = _x.size[0]
        n_H = _x.size[1]
        # 若要对图像进行放大缩小,激活(去掉注释)以下函数
        '''
        rat=0.4          #放大/缩小倍数
        n_W=int(rat*n_W)
        n_H=int(rat*n_H)
        _x=_x.resize((n_W,n_H))  #直接给n_W,n_H赋值可将图像变为任意大小
        '''
        datas.append(np.array(_x))
        _x.close()
    datas = np.array(datas)

    m = datas.shape[0]
    datas = datas.reshape((m, n_H, n_W, n_C))
    # print(datas.shape)

    return datas


def saveInH5py(datas):
    f = h5py.File('data.h5', 'w')
    f.create_dataset('train_set', data=datas)
    f.close()


def load_dataset():
    train_dataset = h5py.File('data.h5', 'r')
    train_set_orig = np.array(train_dataset["train_set"][:])
    return train_set_orig

#读取图片作为数据集
x_src = 'D:\\0_project\\mark_pic\\data\\new_pic\\small\\pic_re_shrink_g'
y_src = 'D:\\0_project\\mark_pic\\data\\new_pic\\small\\pic_re_shrink'
x_datas = read_picture(x_src, 3)
saveInH5py(x_datas)
train_set_x_orig = load_dataset()
y_datas = read_picture(y_src, 3)
saveInH5py(y_datas)
train_set_y_orig = load_dataset()
print(train_set_x_orig.shape)
print(train_set_y_orig.shape)
 

#编译
model=vdsr_model(input_shape=(289,387,3),output_shape=(289,387,3))
model.compile(optimizer="sgd", loss="mean_squared_error", metrics=["accuracy"])
#训练
X_train=train_set_x_orig
Y_train=train_set_y_orig
mymodel=model.fit(X_train,Y_train,epochs=20, validation_split= 0.1, batch_size=1)
model.save('e:\\vdsr.h5')

#%%
#载入预测图像
model=load_model('e:\\vdsr.h5')
x_test_src='D:\\0_project\\mark_pic\\data\\new_pic\\small\\pic_test'
x_test=read_picture(x_test_src, 3)
saveInH5py(x_test)
test_set_x_orig = load_dataset()
pred=model.predict(test_set_x_orig)

结果:
待续

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值