SRCNN generate_train 阅读笔记

clear;close all;
%% settings
folder = 'Train';
savepath = 'train.h5';
size_input = 33;%输入图像尺寸 33*33(分块大小)
size_label = 21;%目标(监督)图像尺寸,和 padding有关 ;三层卷积损失了12

%由于网络在训练时没有进行padding,所以经过卷积后图片尺寸会变小,
%计算公式out = (in - filtersize + 2*padding)/卷积的stride  + 1。
%输入33*33,经过卷积核大小分别为9、1、5,stride=1的卷积后,
%得到的输出大小为21*21。因此取HR的sub-images中心21*21像素作为label

scale = 3;
stride = 14;%分块间隔,步长

%% initialization
data = zeros(size_input, size_input, 1, 1);
label = zeros(size_label, size_label, 1, 1);
padding = abs(size_input - size_label)/2;
count = 0;

%% generate data
%获得训练图像数据,输入图像为33*33*1*21884,label为21*21*1*21884,
%并将训练图像的顺序打乱

filepaths = dir(fullfile(folder,'*.bmp'));%列出该文件夹下所有.bmp格式的文件
    
for i = 1 : length(filepaths)
    
    image = imread(fullfile(folder,filepaths(i).name));
    image = rgb2ycbcr(image);
    image = im2double(image(:, :, 1));%获得图像的y通道
    
    im_label = modcrop(image, scale);%保证图像被scale整除
    [hei,wid] = size(im_label);
    im_input = imresize(imresize(im_label,1/scale,'bicubic'),[hei,wid],'bicubic');%对图像用'bicubic'先下采样再上采样

    for x = 1 : stride : hei-size_input+1
        for y = 1 :stride : wid-size_input+1
            
            subim_input = im_input(x : x+size_input-1, y : y+size_input-1);
            subim_label = im_label(x+padding : x+padding+size_label-1, y+padding : y+padding+size_label-1);
            %subim_input和subim_label的中心一致

            count=count+1;
            data(:, :, 1, count) = subim_input;%子图像尺寸33*33
            label(:, :, 1, count) = subim_label; %子图像类别尺寸21*21
        end
    end
end

order = randperm(count);%randperm:返回一个包含0到count之间随机值的向量
data = data(:, :, 1, order); %将训练图像的顺序打乱
label = label(:, :, 1, order); 

%% writing to HDF5
chunksz = 128;    %批处理参数 batch size;chunk,块。
created_flag = false;
totalct = 0;

for batchno = 1:floor(count/chunksz)
    last_read=(batchno-1)*chunksz;
    batchdata = data(:,:,1,last_read+1:last_read+chunksz); 
    batchlabs = label(:,:,1,last_read+1:last_read+chunksz);

    startloc = struct('dat',[1,1,1,totalct+1], 'lab', [1,1,1,totalct+1]);
    curr_dat_sz = store2hdf5(savepath, batchdata, batchlabs, ~created_flag, startloc, chunksz); %将数据保存为hdf5的格式
    created_flag = true;
    totalct = curr_dat_sz(end);
end
h5disp(savepath);
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值