crop_layer
input(image, feature map)์ cropํ๊ธฐ ์ํ layer์ ๋๋ค.
get_crop_image
image get_crop_image(crop_layer l)
{
int h = l.out_h;
int w = l.out_w;
int c = l.out_c;
return float_to_image(w,h,c,l.output);
}
ํจ์ ์ด๋ฆ: get_crop_image
์ ๋ ฅ:
crop_layer ํ์ ๋ณ์ l
๋์:
์ ๋ ฅ์ผ๋ก ๋ฐ์ crop_layer ํ์ ๋ณ์ l์ output ๋ฐฐ์ด์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ float_to_image ํจ์๋ฅผ ํตํด ์ด๋ฏธ์ง ํํ๋ก ๋ณํํ์ฌ ๋ฐํํฉ๋๋ค.
์ค๋ช :
์ ๋ ฅ์ผ๋ก ๋ฐ์ crop_layer ํ์ ๋ณ์ l์ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ์๋ผ๋ด๊ธฐ ์ํ ์ ๋ณด๋ค์ ๊ฐ์ง๊ณ ์์ต๋๋ค.
์ด ํจ์๋ ๊ทธ ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์๋ผ๋ธ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ float_to_image ํจ์๋ฅผ ํตํด ์ด๋ฏธ์ง ํํ๋ก ๋ณํํ๊ณ ๋ฐํํฉ๋๋ค.
์ด ํจ์๋ ์ด๋ฏธ์ง ์ธ์ ๋ถ์ผ์์ ๋ง์ด ์ฌ์ฉ๋ฉ๋๋ค.
forward_crop_layer
void forward_crop_layer(const crop_layer l, network net)
{
int i,j,c,b,row,col;
int index;
int count = 0;
int flip = (l.flip && rand()%2);
int dh = rand()%(l.h - l.out_h + 1);
int dw = rand()%(l.w - l.out_w + 1);
float scale = 2;
float trans = -1;
if(l.noadjust){
scale = 1;
trans = 0;
}
if(!net.train){
flip = 0;
dh = (l.h - l.out_h)/2;
dw = (l.w - l.out_w)/2;
}
for(b = 0; b < l.batch; ++b){
for(c = 0; c < l.c; ++c){
for(i = 0; i < l.out_h; ++i){
for(j = 0; j < l.out_w; ++j){
if(flip){
col = l.w - dw - j - 1;
}else{
col = j + dw;
}
row = i + dh;
index = col+l.w*(row+l.h*(c + l.c*b));
l.output[count++] = net.input[index]*scale + trans;
}
}
}
}
}
ํจ์ ์ด๋ฆ: forward_crop_layer
์ ๋ ฅ:
const crop_layer l: crop_layer ํ์ ์ l ๋ณ์. ํฌ๋กญ ๋ ์ด์ด์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์.
network net: network ํ์ ์ net ๋ณ์. ์ ๊ฒฝ๋ง ์ ๋ณด๋ฅผ ๋ด๊ณ ์์.
๋์:
๋๋ค์ผ๋ก ์ข์ฐ ๋ฐ์ ๊ณผ ์ด๋ฏธ์ง๋ฅผ ์๋ผ๋ธ ํ, ํฌ๋กญ ๋ ์ด์ด์ ์ถ๋ ฅ๊ฐ์ ๊ณ์ฐํ์ฌ l.output ๋ฐฐ์ด์ ์ ์ฅํจ.
์ค๋ช :
ํฌ๋กญ ๋ ์ด์ด๋ ์ ๋ ฅ ์ด๋ฏธ์ง๋ฅผ ๋๋ค์ผ๋ก ์๋ฅธ ํ, ์ถ๋ ฅ ์ด๋ฏธ์ง๋ฅผ ์์ฑํจ.
์ ๋ ฅ ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ l.h x l.w x l.c์ด๊ณ , ์ถ๋ ฅ ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ l.out_h x l.out_w x l.out_c์.
์ข์ฐ ๋ฐ์ ์ฌ๋ถ๋ l.flip ๊ฐ์ ๋ฐ๋ผ ๋๋ค์ผ๋ก ๊ฒฐ์ ๋จ.
์๋ผ๋ธ ์ด๋ฏธ์ง์ ์์น๋ l.h, l.w์์ ๊ฐ๊ฐ l.out_h, l.out_w ํฌ๊ธฐ๋งํผ ๋๋คํ๊ฒ ์ ํ๋จ.
ํฌ๋กญ ๋ ์ด์ด๋ ๋คํธ์ํฌ๊ฐ ํ์ต ์ค์ผ ๋๋ง ์ข์ฐ ๋ฐ์ ์ ํ๊ณ ์ด๋ฏธ์ง๋ฅผ ์๋ผ๋. ํ์ต์ด ์๋ ๋๋ ์ข์ฐ ๋ฐ์ ์ ํ์ง ์๊ณ ์ด๋ฏธ์ง ์ค์์์ ์๋ผ๋.
scale๊ณผ trans ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ ๊ทํํ๊ธฐ ์ํด ์ฌ์ฉ๋จ.
backward_crop_layer
void backward_crop_layer(const crop_layer l, network net){}
ํจ์ ์ด๋ฆ: backward_crop_layer
์ ๋ ฅ:
crop_layer l
network net
๋์:
crop_layer์ ์ญ์ ํ๋ฅผ ์ํํฉ๋๋ค.
์ด ํจ์๋ backward ์ฐ์ฐ์ ์ํํ์ง ์์ต๋๋ค.
์ค๋ช :
crop_layer๋ ์ ๋ ฅ ์ด๋ฏธ์ง์ ์ผ๋ถ๋ถ์ ๋ฌด์์๋ก ์๋ผ๋ด์ด ์ถ๋ ฅ์ผ๋ก ๋ด๋ณด๋ด๋ ๋ ์ด์ด์ ๋๋ค.
์ด ํจ์๋ ํด๋น ๋ ์ด์ด์์์ ์ญ์ ํ๋ฅผ ๊ตฌํํฉ๋๋ค.
๊ทธ๋ฌ๋ ์ด ๋ ์ด์ด๋ ์ญ์ ํ๋ฅผ ์ํ ํ์ต ๊ฐ๋ฅํ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ต๋๋ค.
๋ฐ๋ผ์ ์ด ํจ์๋ ๋น ํจ์๋ก ๋จ๊ฒจ๋ก๋๋ค.
resize_crop_layer
void resize_crop_layer(layer *l, int w, int h)
{
l->w = w;
l->h = h;
l->out_w = l->scale*w;
l->out_h = l->scale*h;
l->inputs = l->w * l->h * l->c;
l->outputs = l->out_h * l->out_w * l->out_c;
l->output = realloc(l->output, l->batch*l->outputs*sizeof(float));
}
ํจ์ ์ด๋ฆ: resize_crop_layer
์ ๋ ฅ:
layer *l: ํฌ๋กญ ๋ ์ด์ด ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ
int w: ์๋ก์ด ๊ฐ๋ก ํฌ๊ธฐ
int h: ์๋ก์ด ์ธ๋ก ํฌ๊ธฐ
๋์:
ํฌ๋กญ ๋ ์ด์ด์ ๊ฐ๋ก ์ธ๋ก ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ๊ณ , ์ถ๋ ฅ ํฌ๊ธฐ๋ฅผ ๋ค์ ๊ณ์ฐํ๊ณ , ์ถ๋ ฅ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์ฌํ ๋นํ๋ค.
์ค๋ช :
์ ๋ ฅ์ผ๋ก ๋ฐ์ ํฌ๋กญ ๋ ์ด์ด ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋ก์ ์ธ๋ก ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ๋ค.
๊ทธ ํ, ์๋ก์ด ๊ฐ๋ก์ ์ธ๋ก ํฌ๊ธฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ถ๋ ฅ ํฌ๊ธฐ๋ฅผ ๋ค์ ๊ณ์ฐํ๊ณ , ์ ๋ ฅ ํฌ๊ธฐ์ ์ถ๋ ฅ ํฌ๊ธฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ถ๋ ฅ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์ฌํ ๋นํ๋ค.
make_crop_layer
crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure)
{
fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c);
crop_layer l = {0};
l.type = CROP;
l.batch = batch;
l.h = h;
l.w = w;
l.c = c;
l.scale = (float)crop_height / h;
l.flip = flip;
l.angle = angle;
l.saturation = saturation;
l.exposure = exposure;
l.out_w = crop_width;
l.out_h = crop_height;
l.out_c = c;
l.inputs = l.w * l.h * l.c;
l.outputs = l.out_w * l.out_h * l.out_c;
l.output = calloc(l.outputs*batch, sizeof(float));
l.forward = forward_crop_layer;
l.backward = backward_crop_layer;
return l;
}
ํจ์ ์ด๋ฆ: make_crop_layer
์ ๋ ฅ:
batch: int, ๋ฏธ๋๋ฐฐ์น ํฌ๊ธฐ
h: int, ์ ๋ ฅ ์ด๋ฏธ์ง์ ๋์ด
w: int, ์ ๋ ฅ ์ด๋ฏธ์ง์ ๋๋น
c: int, ์ ๋ ฅ ์ด๋ฏธ์ง์ ์ฑ๋ ์
crop_height: int, ์๋ฅผ ์์ญ์ ๋์ด
crop_width: int, ์๋ฅผ ์์ญ์ ๋๋น
flip: int, ์๋ฅด๊ธฐ ์ ์ด๋ฏธ์ง๋ฅผ ์ํ์ผ๋ก ๋ค์ง์์ง ์ฌ๋ถ
angle: float, ์ด๋ฏธ์ง ํ์ ๊ฐ๋
saturation: float, ์ด๋ฏธ์ง ํฌํ๋ ์กฐ์ ๊ฐ
exposure: float, ์ด๋ฏธ์ง ๋ ธ์ถ ์กฐ์ ๊ฐ
๋์:
์ ๋ ฅ๋ ๊ฐ์ ๋ฐํ์ผ๋ก crop_layer ๊ตฌ์กฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ด๊ธฐํํ๋ค.
์ถ๋ ฅ๋๋ ์์์ ํฌ๊ธฐ๋ฅผ ๊ณ์ฐํ๋ค.
ํ์ํ ์ ๋ ฅ ๋ฐ ์ถ๋ ฅ ๊ณต๊ฐ์ ํ ๋นํ๋ค.
forward_crop_layer ํจ์์ backward_crop_layer ํจ์๋ฅผ ์ค์ ํ๋ค.
์ด๊ธฐํ๋ crop_layer ๊ตฌ์กฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
์ค๋ช :
์ด ํจ์๋ crop_layer๋ฅผ ์์ฑํ๊ณ ์ด๊ธฐํํ๋ ํจ์์ด๋ค.
crop_layer๋ ์ ๋ ฅ๋ ์ด๋ฏธ์ง์์ ์ฃผ์ด์ง ํฌ๊ธฐ์ ์์ญ์ ์๋ผ๋ด๋ ์ญํ ์ ํ๋ค.
์ ๋ ฅ๋ ์ธ์๋ฅผ ๋ฐํ์ผ๋ก crop_layer ๊ตฌ์กฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ด๊ธฐํํ ํ ๋ฐํํ๋ค.
์ด๋ forward_crop_layer ํจ์์ backward_crop_layer ํจ์๋ฅผ ์ค์ ํด ์ฃผ์ด์ผ ํ๋ค.
Last updated
Was this helpful?