1 #include "cuda_runtime.h" 14 if(x < 0 || x >= w || y < 0 || y >= h)
return 0;
15 return image[x + w*(y + c*h)];
25 float max = (r > g) ? ( (r > b) ?
r : b) : ( (g > b) ? g : b);
26 float min = (r < g) ? ( (r < b) ?
r : b) : ( (g < b) ? g : b);
27 float delta = max - min;
36 }
else if (g == max) {
37 h = 2 + (b -
r) / delta;
39 h = 4 + (r - g) / delta;
43 return make_float3(h, s, v);
58 int index = (int) floorf(h);
65 }
else if(index == 1){
67 }
else if(index == 2){
69 }
else if(index == 3){
71 }
else if(index == 4){
77 r = (r < 0) ? 0 : ((r > 1) ? 1 : r);
78 g = (g < 0) ? 0 : ((g > 1) ? 1 : g);
79 b = (b < 0) ? 0 : ((b > 1) ? 1 : b);
80 return make_float3(r, g, b);
85 int ix = (int) floorf(x);
86 int iy = (int) floorf(y);
98 __global__
void levels_image_kernel(
float *
image,
float *rand,
int batch,
int w,
int h,
int train,
float saturation,
float exposure,
float translate,
float scale,
float shift)
100 int size = batch * w * h;
101 int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
102 if(
id >= size)
return;
107 float rshift = rand[0];
108 float gshift = rand[1];
109 float bshift = rand[2];
110 float r0 = rand[8*
id + 0];
111 float r1 = rand[8*
id + 1];
112 float r2 = rand[8*
id + 2];
113 float r3 = rand[8*
id + 3];
115 saturation = r0*(saturation - 1) + 1;
116 saturation = (r1 > .5f) ? 1.f/saturation : saturation;
117 exposure = r2*(exposure - 1) + 1;
118 exposure = (r3 > .5f) ? 1.f/exposure : exposure;
120 size_t offset =
id * h * w * 3;
122 float r = image[x + w*(y + h*0)];
123 float g = image[x + w*(y + h*1)];
124 float b = image[x + w*(y + h*2)];
125 float3 rgb = make_float3(r,g,b);
134 image[x + w*(y + h*0)] = rgb.x*scale + translate + (rshift - .5f)*shift;
135 image[x + w*(y + h*1)] = rgb.y*scale + translate + (gshift - .5f)*shift;
136 image[x + w*(y + h*2)] = rgb.z*scale + translate + (bshift - .5f)*shift;
139 __global__
void forward_crop_layer_kernel(
float *input,
float *rand,
int size,
int c,
int h,
int w,
int crop_height,
int crop_width,
int train,
int flip,
float angle,
float *output)
141 int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
142 if(
id >= size)
return;
148 int j =
id % crop_width;
150 int i =
id % crop_height;
156 float r4 = rand[8*b + 4];
157 float r5 = rand[8*b + 5];
158 float r6 = rand[8*b + 6];
159 float r7 = rand[8*b + 7];
161 float dw = (w - crop_width)*r4;
162 float dh = (h - crop_height)*r5;
163 flip = (flip && (r6 > .5f));
164 angle = 2*angle*r7 - angle;
166 dw = (w - crop_width)/2.f;
167 dh = (h - crop_height)/2.f;
174 float x = (flip) ? w - dw - j - 1 : j + dw;
177 float rx = cosf(angle)*(x-cx) - sinf(angle)*(y-cy) + cx;
178 float ry = sinf(angle)*(x-cx) + cosf(angle)*(y-cy) + cy;
185 cuda_random(layer.rand_gpu, layer.
batch*8);
187 float radians = layer.
angle*3.14159265f/180.f;
190 float translate = -1;
196 int size = layer.
batch * layer.
w * layer.
h;
198 levels_image_kernel<<<cuda_gridsize(size), BLOCK>>>(net.input_gpu, layer.rand_gpu, layer.
batch, layer.
w, layer.
h, net.
train, layer.
saturation, layer.
exposure, translate, scale, layer.
shift);
199 check_error(cudaPeekAtLastError());
203 forward_crop_layer_kernel<<<cuda_gridsize(size), BLOCK>>>(net.input_gpu, layer.rand_gpu, size, layer.
c, layer.
h, layer.
w, layer.
out_h, layer.
out_w, net.
train, layer.
flip, radians, layer.output_gpu);
204 check_error(cudaPeekAtLastError());
__global__ void levels_image_kernel(float *image, float *rand, int batch, int w, int h, int train, float saturation, float exposure, float translate, float scale, float shift)
__device__ float get_pixel_kernel(float *image, int w, int h, int x, int y, int c)
void forward_crop_layer_gpu(crop_layer layer, network net)
__device__ float3 hsv_to_rgb_kernel(float3 hsv)
__device__ float bilinear_interpolate_kernel(float *image, int w, int h, float x, float y, int c)
__global__ void forward_crop_layer_kernel(float *input, float *rand, int size, int c, int h, int w, int crop_height, int crop_width, int train, int flip, float angle, float *output)
__device__ float3 rgb_to_hsv_kernel(float3 rgb)