Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rwenqi committed Mar 26, 2018
1 parent 303080c commit 13e5185
Show file tree
Hide file tree
Showing 86 changed files with 1,707 additions and 0 deletions.
33 changes: 33 additions & 0 deletions LoadSolver.m
@@ -0,0 +1,33 @@
function X = LoadSolver(folder, solver_file, mat_file)

if exist(mat_file,'file') && ~isempty(mat_file)
M_ = load(mat_file);
X = T2_SolverParser(solver_file, M_); % TODO: save G_
else
X = T2_SolverParser(solver_file);
end

if isfield(X,'snapshot_prefix') && exist('M_','var')
[~,pr] = fileparts(X.snapshot_prefix);
if isfield(X, 'iter')
% saved path
state = fullfile(folder, [pr,sprintf('_iter_%d.solverstate', X.iter)]);
% tmp path
X.state_file = [X.snapshot_prefix,sprintf('_iter_%d.solverstate', X.iter)];
copyfile(state, X.state_file);
% saved path
modes = fullfile(folder, [pr,sprintf('_iter_%d.caffemodel', X.iter)]);
% tmp path
X.model_file = [X.snapshot_prefix, sprintf('_iter_%d.caffemodel', Solver.G.iter)];
copyfile(modes, X.model_file);
end
else
% dump existing models if there is no solver mat info
delete(fullfile(folder, [pr,'*']));
X.state_file = [];
fprintf('ALERT: please copy the pretrained caffemodel to %s.\n', X.model_file);
end

X = SR_init02(X, solver_file);

end
34 changes: 34 additions & 0 deletions RealGWbal.m
@@ -0,0 +1,34 @@
%º¯Êý¶¨Òå
function y=RealGWbal(Image)
Image=double(Image);
r=Image(:,:,1);
g=Image(:,:,2);
b=Image(:,:,3);
[x,y]=size(r);
avgR = mean(r(:));
avgG = mean(g(:));
avgB = mean(b(:));
avgRGB = [avgR avgG avgB];
grayValue = (avgR + avgG + avgB)/3 ;
scaleValue = grayValue./(avgRGB+0.001);
R = scaleValue(1) * r;
G = scaleValue(2) * g;
B = scaleValue(3) * b;
for i=1:x
for j=1:y
if(R(i,j)>255)
R(i,j)=255;
end
if(G(i,j)>255)
G(i,j)=255;
end
if(B(i,j)>255)
B(i,j)=255;
end
end
end
% maxB=max(B(:));
newI(:,:,1)=R;
newI(:,:,2)=G;
newI(:,:,3)=B;
y=newI;
68 changes: 68 additions & 0 deletions SolverParser.m
@@ -0,0 +1,68 @@
% parse all fields of a solver proto
% transfer all FC layers in the existing model to CONV layers (equvelent)
function [ Solver ] = SolverParser( solver_def_file, resume_file )
if ~exist(solver_def_file,'file')||isempty(solver_def_file)
error('Solver definition file %s is not found.',solver_def_file);
end
if ~exist('resume_file','var')
fprintf('Creating a face lmk solver structure based on %s.', solver_def_file);
Solver = [];
else
load(resume_file);
end
fout = fopen(solver_def_file,'r');
tline = fgetl(fout);
while ischar(tline)
disp(tline)
pos = 1;
while tline(pos)==' '
pos = pos + 1;
end
if tline(pos) == '#'
tline = fgetl(fout);
continue;
end
ind = find(tline == '"',1);
if ~isempty(ind)
field = tline(ind + 1 : end - 1);
ind2 = find(tline == ':',1);
name = tline(1:ind2-1);
else
ind2 = find(tline == ':',1);
if isempty(ind2)
error('incorrect format.')
end
ctr = tline(ind2+2:end);
if isempty(str2num(ctr))
ctr(isspace(ctr)) = [];
field = ctr;
else
field = str2double(ctr);
end
name = tline(1:ind2-1);
end
Solver = setfield(Solver, name, field);
tline = fgetl(fout);
end
fclose(fout);
if ~isfield(Solver, 'solver_mode')
Solver.solver_mode = 'GPU';
end
if ~isfield(Solver, 'device_id') && strcmp(Solver.solver_mode, 'GPU')
Solver.device_id = 0;
end
if isfield(Solver,'model')
lnum = length(Solver.model);
for ind = 1:lnum
if strcmp(Solver.model(ind).layer_names,sprintf('fc%d',ind))
Solver.model(ind).layer_names = sprintf('conv%d',ind);
weights = Solver.model(ind).weights{1};
[s1,s2] = size(weights);
[~,~,~,ch] = size(Solver.model(ind-1).weights{1});
filtersize = sqrt(s1/ch);
weights = reshape(weights,[filtersize,filtersize,ch,s2]);
Solver.model(ind).weights{1} = weights;
end
end
end
end
23 changes: 23 additions & 0 deletions caffe_init.m
@@ -0,0 +1,23 @@
function X = caffe_init(X, solver_file)

if ~exist(solver_file,'file') || isempty(solver_file)
error('You need a solver prototxt definition.')
end
X.Solver_ = caffe.Solver(solver_file); % to cpp
if isfield(X, 'state_file') && ~isempty(X.state_file) && exist(X.state_file, 'file')
X.Solver_.restore(X.state_file);
fprintf('resume from snapshot file %s...\n', X.state_file);
elseif isfield(X, 'model_file') && ~isempty(X.model_file)
X.Solver_.net.copy_from(X.model_file);
fprintf('resume from caffemodel file %s...\n', X.model_file);
end

% set caffe mode
if strcmp(X.solver_mode,'GPU')
caffe.set_mode_gpu();
caffe.set_device(X.device_id);
else
caffe.set_mode_cpu();
end
fprintf('Done with init %s\n', solver_file);
end
61 changes: 61 additions & 0 deletions demo_test.m
@@ -0,0 +1,61 @@
% DEMO_TEST.m

addpath(genpath('/home/vision/wren/caffe-dilate'));
caffe.reset_all();
clc; clear;

hazy_path = ('./inputs/');
type = '*.png';
hazy_data = dir(fullfile(hazy_path,type));

model_path = './models/';

solver_file = fullfile(model_path, 'dehaze_solver_test.prototxt');
save_file = fullfile(model_path, 'dehaze_ps128_bs1.mat');

Solver = modelconfig_test( solver_file, save_file);
% Solver.iter
Solver.batchsize = 1;
for ii = 45:length(hazy_data)
disp(ii)
clear batch;
hazyimg = im2double(imread(fullfile(hazy_path,hazy_data(ii).name)));

[row, col, cha] = size(hazyimg);

% resize the input to a multiple of 8
height = ceil(row/8)*8;
width = ceil(col/8)*8;
hazyimg = imresize(hazyimg, [height, width]);


hazy_wb = RealGWbal(uint8(255*hazyimg));
hazy_wb = hazy_wb/255;
hazy_cont = (2*(0.5+mean(hazyimg(:)))).*(hazyimg-mean(hazyimg(:)));
hazy_gamma = hazyimg.^2.5;

batch(:,:,1:3) = hazyimg;
batch(:,:,4:6) = hazy_wb;
batch(:,:,7:9) = hazy_cont;
batch(:,:,10:12) = hazy_gamma;

% figure(1);subplot(221), imshow(hazyimg);subplot(222),imshow(hazy_wb);
% subplot(223), imshow(hazy_cont); subplot(224),imshow(hazy_gamma);

batchc = {single(batch)};

Solver.Solver_.net.blobs('data').reshape([height, width, 12, 1]);

tic
activec = Solver.Solver_.net.forward(batchc);
toc
dehazed= activec{3};

% dehazed = imresize(dehazed, [size(TEST_IMAGE, 1), size(TEST_IMAGE, 2)]);

imwrite(dehazed, strcat('./results/',hazy_data(ii).name(1:end-4),'_dehazed.png'));
end
%imshow(out);title('Original/Proposed/GT');
%weights=Solver.Solver_.net.get_weights();
%data=Solver.Solver_.net.get_data();
%diff=Solver.Solver_.net.get_diff();
Binary file added inputs/HazyDay.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_0614.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_0747.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_0752.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_0768.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_1252.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_1255.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_1427.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_1704.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_2281.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/IMG_2489.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/P1060389.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/PipingLive.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/beijing1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/cabin-fog.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/chicago_uchicago.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/example-02-haze.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/example-05-haze.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/foggy_bench.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/hazyShade.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/highquality06.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/highquality1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/highquality13.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/highquality4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/marble.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/mountain.png
Binary file added inputs/newyork.png
Binary file added inputs/piles.png
Binary file added inputs/pumpkins.png
Binary file added inputs/seattle-site-protection.png
Binary file added inputs/shanghai-skyline_b.png
Binary file added inputs/shanghai_skyline.png
Binary file added inputs/stadium.png
Binary file added inputs/sweden.png
Binary file added inputs/train.png
Binary file added inputs/trees2.png
27 changes: 27 additions & 0 deletions modelconfig_test.m
@@ -0,0 +1,27 @@
function Solver = modelconfig_test(solver_file, save_file)

if exist(save_file, 'file')
% M_ = load(save_file);
% fprintf('loading saved model.\n');
Solver = SolverParser(solver_file, save_file);
else
Solver = SolverParser(solver_file);
end

Solver.iter = 240000;

if isfield(Solver, 'snapshot_prefix')
if isfield(Solver, 'iter')
Solver.state_file = [Solver.snapshot_prefix,sprintf('_iter_%d.solverstate', Solver.iter)];
Solver.model_file = [Solver.snapshot_prefix, sprintf('_iter_%d.caffemodel', Solver.iter)];
else
Solver.state_file = [];
fprintf('ALERT: no pretrained snapshot found.\n');
end
end

Solver = caffe_init(Solver, solver_file);
Solver.matfile = save_file;
% Solver.state_path = state_path;

end
19 changes: 19 additions & 0 deletions models/dehaze_solver_test.prototxt
@@ -0,0 +1,19 @@
net: "models/multi_fusion_test.prototxt"
test_iter: 20
test_interval: 500
base_lr: 0.0001
momentum: 0.9
momentum2: 0.999
weight_decay: 0.00001
solver_type: ADAM
lr_policy: "poly"
clip_gradients: 80
power: 2
gamma: 0.1
stepsize: 20000
display: 10
max_iter: 100000
snapshot: 10000
snapshot_prefix: "./models/snapshot/"
solver_mode: CPU
device_id: 0

0 comments on commit 13e5185

Please sign in to comment.