(Review cs231n) Training of Neural Network2

FFDNet---matlab 调用并批处理

format compact;
global sigmas; % input noise level or input noise level map
addpath(fullfile('utilities'));

folderModel = 'models';
folderResult= 'results';
save_folder = 'datasets_c';

showResult  = 1;
useGPU      = 0; % CPU or GPU. For single-threaded (ST) CPU computation, use "matlab -singleCompThread" to start matlab.
pauseTime   = 0;

imageNoiseSigma = 50;  % image noise level
inputNoiseSigma = 50;  % input noise level

load(fullfile('models','FFDNet_gray.mat'));
net = vl_simplenn_tidy(net);

if useGPU
    net = vl_simplenn_move(net, 'gpu') ;
end

file_name = 'E:\QQ\401668597\FileRecv\af2019-cv-training-20190312\';
sub_file_name_list = dir(file_name); %获取所有子文件夹
num_sub_file = length(sub_file_name_list); 
if num_sub_file>0
    for fn = 3:num_sub_file
        image_file = ['E:\QQ\401668597\FileRecv\af2019-cv-training-20190312\',sub_file_name_list(fn).name,'\'];
        image_file_b = [image_file,'*_c.jpg'];
        image_list = dir(image_file_b); %得到该文件夹的所有图像的名字
        for in = 1:length(image_list)
            image_name = [image_file,image_list(in).name];
            fprintf('%d,%s',in,image_name);
            
            %开始进行处理
            label = imread(image_name); %打开图像
            [w,h,~]=size(label);
             if size(label,3)==3
                  label = rgb2gray(label);
             end
             label = im2double(label);
             % add noise
             randn('seed',0);
             noise = imageNoiseSigma/255.*randn(size(label));
             input = single(label + noise);
             
             if mod(w,2)==1
                 input = cat(1,input, input(end,:)) ;
             end
             if mod(h,2)==1
                input = cat(2,input, input(:,end)) ;
             end
    
            % tic;
             if useGPU
                input = gpuArray(input);
             end
    
            % set noise level map
             sigmas = inputNoiseSigma/255; % see "vl_simplenn.m".
    
            % perform denoising
             res    = vl_simplenn(net,input,[],[],'conserveMemory',true,'mode','test'); % matconvnet default
            % res    = vl_ffdnet_concise(net, input);    % concise version of vl_simplenn for testing FFDNet
            % res    = vl_ffdnet_matlab(net, input); % use this if you did  not install matconvnet; very slow
    
    
            % output = input - res(end).x; % for 'model_gray.mat'
            output = res(end).x;
    
    
            if mod(w,2)==1
                  output = output(1:end-1,:);
                  input  = input(1:end-1,:);
            end
            if mod(h,2)==1
                output = output(:,1:end-1);
                input  = input(:,1:end-1);
            end
    
            if useGPU
                output = gather(output);
                input  = gather(input);
            end
            if showResult
                %imshow(cat(2,im2uint8(input),im2uint8(label),im2uint8(output)));
                 imshow(cat(2,im2uint8(input),im2uint8(output)));
                 save_dir = [save_folder,image_list(in).name]
                 imwrite(output,save_dir);
                %
                %imwrite(im2uint8(output), fullfile(folderResultCur, [nameCur, '_' num2str(imageNoiseSigma,'%02d'),'_' num2str(inputNoiseSigma,'%02d'),'_PSNR_',num2str(PSNRCur*100,'%4.0f'), extCur] ));
                 drawnow;
                 pause(pauseTime)
            end
             
        end
        
    end
end

Adam不会让你的学习速率降为0,因为它是leaky gradient; 如果使用的是Adagrad, 学习率最后会自动的下降为0。

集成学习

1. 对训练集中大量独立的模型进行训练,而不仅仅对于单一模型;

2.在测试的时候将结果进行平均。(效果可以提高2%的提升)

使用集成学习的小技巧

当你在训练神经网络时设置一些检查点,通常是每个时期建立一个,对每个检查点都去验证这在验证集中的表现。

这说明你可以在模型中设置不同的检查点,然后在处理集合中使用它们,这被证明能够使得结果有所改善,这样的话你不需要对不同的独立模型进行训练,而只需要训练一下,但是需要设置相应的检查点。

while True:
    data_batch = dataset.sample_data_batch()
    loss = networdk.forward(data_batch)
    dx = network.backward()
    x +=-learning_rate * dx
    x_test = 0.995*x_test + 0.005*x # use for test set 类似于一个加权的加权的概念

设定了另一个参数集合x_test,x_test是对我现在的参数x的一个指数衰减,当使用x_test和数据集和验证集的时候,得到的结果总是要比使用x更好,这就像一个之前一些参数进行加权的集合。

想一下对你的碗装函数进行优化,在最低点周围不停的移动,然后做一个对这些所有的一个平均值,能够更加接近最低点。

Regularization(dropout)

然你的神经网络的节点随机失活,随机把一些神经元置零,

p = 0.5 #probablity of keeping a unit active. higher = less dropout

def train_setp(X):

#forward pass for example 3-layer neural network
H1 = np.maxium(0, np.dot(W1, X) + b1)
# first dropout mask
U1 = np.random.rand(*H1.shape) < p #产生了一个bool型的和
H1相同维度的矩阵
 

猜你喜欢

转载自www.cnblogs.com/ChenKe-cheng/p/10595098.html