Skip to content

Commit 3b8ff36

Browse files
author
hanyoseob
committed
upload init
0 parents  commit 3b8ff36

14 files changed

+659
-0
lines changed

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# DCGAN
2+
3+
### Title
4+
[Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks](https://arxiv.org/abs/1511.06434)
5+
6+
### Abstract
7+
In recent years, supervised learning with convolutional networks (CNNs) has seen huge adoption in computer vision applications. Comparatively, unsupervised learning with CNNs has received less attention. In this work we hope to help bridge the gap between the success of CNNs for supervised learning and unsupervised learning. We introduce a class of CNNs called deep convolutional generative adversarial networks (DCGANs), that have certain architectural constraints, and demonstrate that they are a strong candidate for unsupervised learning. Training on various image datasets, we show convincing evidence that our deep convolutional adversarial pair learns a hierarchy of representations from object parts to scenes in both the generator and discriminator. Additionally, we use the learned features for novel tasks - demonstrating their applicability as general image representations.
8+
9+
## Train
10+
$ python main.py --mode train \
11+
--scope [scope name] \
12+
--name_data [data name] \
13+
--dir_data [data directory] \
14+
--dir_log [log directory] \
15+
--dir_checkpoint [checkpoint directory]
16+
---
17+
$ python main.py --mode train \
18+
--scope dcgan \
19+
--name_data celeba \
20+
--dir_data ./datasets \
21+
--dir_log ./log \
22+
--dir_checkpoint ./checkpoint
23+
24+
* Set **[scope name]** uniquely.
25+
* Hyperparameters were written to **arg.txt** under the **[log directory]**.
26+
* To understand hierarchy of directories based on their arguments, see **directories structure** below.
27+
28+
## Test
29+
$ python main.py --mode test \
30+
--scope [scope name] \
31+
--name_data [data name] \
32+
--dir_data [data directory] \
33+
--dir_log [log directory] \
34+
--dir_checkpoint [checkpoint directory] \
35+
--dir_result [result directory]
36+
---
37+
$ python main.py --mode test \
38+
--scope dcgan \
39+
--name_data celeba \
40+
--dir_data ./datasets \
41+
--dir_log ./log \
42+
--dir_checkpoint ./checkpoints \
43+
--dir_result ./results
44+
45+
* To test using trained network, set **[scope name]** defined in the **train** phase.
46+
* Generated images are saved in the **images** subfolder along with **[result directory]** folder.
47+
* **index.html** is also generated to display the generated images.
48+
49+
50+
## Tensorboard
51+
$ tensorboard --logdir [log directory]/[scope name]/[data name] \
52+
--port [(optional) 4 digit port number]
53+
---
54+
$ tensorboard --logdir ./log/dcgan/celeba \
55+
--port 6006
56+
57+
After the above comment executes, go **http://localhost:6006**
58+
59+
* You can change **[(optional) 4 digit port number]**.
60+
* Default 4 digit port number is **6006**.
61+
62+
63+
## Results
64+
![alt text](./img/generated_images.png "Generated Images by DCGAN")
65+
* The results were generated by a network trained with **celeba** dataset during **10 epochs**.
66+
* After the Test phase runs, execute **display_result.py** to display the figure.
67+
68+
## Directories structure
69+
pytorch-DCGAN
70+
+---[dir_checkpoint]
71+
| \---[scope]
72+
| \---[name_data]
73+
| +---model_epoch00000.pth
74+
| | ...
75+
| \---model_epoch12345.pth
76+
+---[dir_data]
77+
| \---[name_data]
78+
| +---000000.png
79+
| | ...
80+
| \---12345.png
81+
+---[dir_log]
82+
| \---[scope]
83+
| \---[name_data]
84+
| +---arg.txt
85+
| \---events.out.tfevents
86+
\---[dir_result]
87+
\---[scope]
88+
\---[name_data]
89+
+---images
90+
| +---00000-output.png
91+
| | ...
92+
| +---12345-output.png
93+
\---index.html
94+
95+
---
96+
97+
pytorch-DCGAN
98+
+---checkpoints
99+
| \---dcgan
100+
| \---celeba
101+
| +---model_epoch00001.pth
102+
| | ...
103+
| \---model_epoch0010.pth
104+
+---datasets
105+
| \---celeba
106+
| +---000001.jpg
107+
| | ...
108+
| \---202599.jpg
109+
+---log
110+
| \---dcgan
111+
| \---celeba
112+
| +---arg.txt
113+
| \---events.out.tfevents
114+
\---results
115+
\---dcgan
116+
\---celeba
117+
+---images
118+
| +---0000-output.png
119+
| | ...
120+
| +---0127-output.png
121+
\---index.html
122+
123+
* Above directory is created by setting arguments when **main.py** is executed.
124+

XCAT512.mat

15.6 KB
Binary file not shown.

backprojection.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function pdX = backprojection(pdY, param, bfig)
2+
3+
if nargin < 3
4+
bfig = false;
5+
end
6+
7+
if bfig
8+
figure('name', 'backprojection'); colormap gray;
9+
end
10+
11+
pdX = zeros(param.nImgY, param.nImgX, 'like', pdY);
12+
dCurX = zeros(param.nImgY, param.nImgX, 'like', pdY);
13+
14+
for iview = 0:param.nView-1
15+
16+
dBeta = -iview*param.dView;
17+
dCurX(:) = 0;
18+
19+
20+
for iimgx = 0:param.nImgX-1
21+
dPosImgX = id2pos(iimgx + param.dOffsetImgX, param.dImgX, param.nImgX);
22+
23+
for iimgy = 0:param.nImgY-1
24+
dPosImgY = id2pos(iimgy + param.dOffsetImgY, param.dImgY, param.nImgY);
25+
26+
27+
dRadius = sqrt(dPosImgY*dPosImgY + dPosImgX*dPosImgX);
28+
dPhi = atan2d(dPosImgY, dPosImgX);
29+
30+
dDistY = dRadius*cosd(dBeta - dPhi);
31+
32+
nCurIdDctX = pos2id(dDistY, param.dDctX, param.nDctX) - param.dOffsetDctX + 1;
33+
34+
if (nCurIdDctX <= 1 || nCurIdDctX >= param.nDctX)
35+
continue;
36+
end
37+
38+
nPreIdDctX = floor(nCurIdDctX);
39+
nPostIdDctX = nPreIdDctX + 1;
40+
41+
dCurX_ = interpolation1d(pdY(:, iview + 1), nCurIdDctX, nPreIdDctX, nPostIdDctX);
42+
43+
dCurX(iimgy + 1, iimgx + 1) = dCurX_;
44+
end
45+
end
46+
47+
pdX = pdX + dCurX;
48+
49+
if bfig
50+
imagesc(pdX); title([num2str(iview + 1) ' / ' num2str(param.nView)]);
51+
xlabel('x-axis'); ylable('y-axis');
52+
drawnow();
53+
end
54+
end
55+
56+
end

demo_parallelbeam_ct.m

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
clear ;
2+
close all;
3+
4+
%%
5+
restoredefaultpath();
6+
addpath('./lib');
7+
8+
%% Setting the CT System parameters
9+
% dAngle: Measure from 0 until the angle
10+
% nView : # of the views [unit]
11+
% dView : Gap between view_(k) - view_(k-1) [degree]
12+
param.dAngle = 180;
13+
param.nView = 90; % # of elements
14+
param.dView = param.dAngle/param.nView; % degree
15+
param.pdView = linspace(0, param.dAngle, param.nView + 1);
16+
param.pdView(end) = [];
17+
18+
% X-ray source
19+
% DSO : Distance from the Source to the Object [mm]
20+
% DSD : Distance from the Source to the Detector [mm]
21+
param.DSO = 400 ; % mm
22+
param.DSD = 800; % mm
23+
24+
% X-ray detector
25+
param.dDctX = 1; % mm
26+
27+
param.nDctX = 185; % # of elements
28+
29+
param.dOffsetDctX = 0; % # of elements
30+
31+
param.compute_filtering = 'fft'; % method for computing the filtering function : 'conv', 'fft'
32+
33+
% Object size
34+
param.dImgY = 1; % mm
35+
param.dImgX = 1; % mm
36+
37+
param.nImgY = 128; % # of elements
38+
param.nImgX = 128; % # of elements
39+
40+
param.dOffsetImgX = 25; % # of elements
41+
param.dOffsetImgY = 25; % # of elements
42+
43+
%%
44+
load('XCAT512.mat');
45+
src = imresize(XCAT512, [param.nImgY, param.nImgX]);
46+
47+
%%
48+
disp ('built-in ver.');
49+
50+
disp ('projection - built-in');
51+
tic;
52+
prj_matlab = radon(src, param.pdView);
53+
toc;
54+
55+
disp ('filtering - built-in');
56+
tic;
57+
prj_matlab_flt = filterProjections(prj_matlab, 'ram-lak', 1);
58+
toc;
59+
60+
disp ('backprojection - built-in');
61+
tic;
62+
dst_matlab = iradon(prj_matlab_flt, param.pdView, 'none', param.nImgY);
63+
toc;
64+
65+
%%
66+
disp ('implementation ver.')
67+
68+
disp ('projection - implementation');
69+
tic;
70+
prj_imp = projection(src, param);
71+
toc;
72+
73+
disp ('filtering - implementation');
74+
tic;
75+
prj_imp_flt = filtering(prj_imp, param);
76+
toc;
77+
78+
disp ('backprojection - implementation');
79+
tic;
80+
dst_imp = backprojection(prj_imp_flt, param);
81+
toc;
82+
83+
%%
84+
wndImg = [0, max(src(:))];
85+
86+
figure(2); colormap gray;
87+
subplot(331); imagesc(src, wndImg);
88+
axis image; xlabel('X-axis'); ylabel('Y-axis'); title('ground truth');
89+
subplot(332); imagesc(dst_matlab, wndImg);
90+
axis image; xlabel('X-axis'); ylabel('Y-axis'); title(['reconstruction_{built-in, # of view = ' num2str(param.nView) '}']);
91+
subplot(333); imagesc(dst_imp, wndImg);
92+
axis image; xlabel('X-axis'); ylabel('Y-axis'); title(['reconstruction_{implementation, # of view = ' num2str(param.nView) '}']);
93+
94+
subplot(335); imagesc(src - dst_matlab);
95+
axis image; xlabel('X-axis'); ylabel('Y-axis'); title(['difference_{ground truth - built-in, # of view = ' num2str(param.nView) '}']);
96+
subplot(336); imagesc(src - dst_imp);
97+
axis image; xlabel('X-axis'); ylabel('Y-axis'); title(['difference_{ground truth - implementation, # of view = ' num2str(param.nView) '}']);
98+
99+
subplot(338); imagesc(prj_matlab);
100+
xlabel(['Angle ( \Delta\theta : ' num2str(param.dView) ' \circ )']); ylabel('Detector'); title(['sinogram_{built-in, # of view = ' num2str(param.nView) '}']);
101+
ax = gca;
102+
ax.XTick = linspace(0, param.nView, 10);
103+
ax.XTickLabel = linspace(0, param.dAngle, 10);
104+
105+
subplot(339); imagesc(prj_imp);
106+
xlabel(['Angle ( \Delta\theta : ' num2str(param.dView) ' \circ )']); ylabel('Detector'); title(['sinogram_{implementation, # of view = ' num2str(param.nView) '}']);
107+
ax = gca;
108+
ax.XTick = linspace(0, param.nView, 10);
109+
ax.XTickLabel = linspace(0, param.dAngle, 10);

filtering.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function [pdFltY, pdFlt] = filtering(pdY, param)
2+
3+
pdFltY = zeros(param.nDctX, param.nView);
4+
pdFlt = generate_filter(param.dDctX, param.nDctX);
5+
6+
switch param.compute_filtering
7+
case 'conv'
8+
%% CONVOLUTION ver.
9+
for iview = 0:param.nView-1
10+
pdFltY(:, iview+1) = convolution1d(pdY(:, iview+1), pdFlt, param.nDctX);
11+
end
12+
13+
case 'fft'
14+
%% FFT ver.
15+
pdFltY_ = zeros(param.nDctX, 1);
16+
17+
% nFlt = max(64,2^nextpow2(2*length(pdFlt)));
18+
nFlt = length(pdFlt);
19+
pdFlt_ = pdFlt;
20+
pdFlt_(nFlt) = 0;
21+
pdFlt_ = fft(pdFlt_);
22+
23+
for iview = 0:param.nView-1
24+
pdFltY_(:) = 0;
25+
26+
pdY_ = pdY(:, iview+1);
27+
pdY_(nFlt) = 0;
28+
pdY_ = fft(pdY_);
29+
30+
for iflt = 0:nFlt-1
31+
pdFltY_(iflt+1) = pdFlt_(iflt+1)*pdY_(iflt+1);
32+
end
33+
34+
pdFltY_ = ifft(pdFltY_, 'symmetric');
35+
pdFltY(:, iview+1) = pdFltY_(nFlt - param.nDctX + 1: nFlt);
36+
end
37+
end
38+
39+
%%
40+
pdFltY = pi/param.nView*param.dDctX*pdFltY;
41+
42+
end
43+

lib/convolution1d.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
function y = convolution1d(x, ker, n)
3+
y = zeros(n, 1);
4+
5+
for i = 0:n-1
6+
for j = 0:n-1
7+
y(i+1) = y(i+1) + ker(((n-1) + i+1) - (j+1) + 1)*x(j+1);
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)