00001 #include "deconvolutional_layer.h" 00002 #include "convolutional_layer.h" 00003 #include "utils.h" 00004 #include "im2col.h" 00005 #include "col2im.h" 00006 #include "blas.h" 00007 #include "gemm.h" 00008 #include <stdio.h> 00009 #include <time.h> 00010 00011 int deconvolutional_out_height(deconvolutional_layer l) 00012 { 00013 int h = l.stride*(l.h - 1) + l.size; 00014 return h; 00015 } 00016 00017 int deconvolutional_out_width(deconvolutional_layer l) 00018 { 00019 int w = l.stride*(l.w - 1) + l.size; 00020 return w; 00021 } 00022 00023 int deconvolutional_out_size(deconvolutional_layer l) 00024 { 00025 return deconvolutional_out_height(l) * deconvolutional_out_width(l); 00026 } 00027 00028 image get_deconvolutional_image(deconvolutional_layer l) 00029 { 00030 int h,w,c; 00031 h = deconvolutional_out_height(l); 00032 w = deconvolutional_out_width(l); 00033 c = l.n; 00034 return float_to_image(w,h,c,l.output); 00035 } 00036 00037 image get_deconvolutional_delta(deconvolutional_layer l) 00038 { 00039 int h,w,c; 00040 h = deconvolutional_out_height(l); 00041 w = deconvolutional_out_width(l); 00042 c = l.n; 00043 return float_to_image(w,h,c,l.delta); 00044 } 00045 00046 deconvolutional_layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, ACTIVATION activation) 00047 { 00048 int i; 00049 deconvolutional_layer l = {0}; 00050 l.type = DECONVOLUTIONAL; 00051 00052 l.h = h; 00053 l.w = w; 00054 l.c = c; 00055 l.n = n; 00056 l.batch = batch; 00057 l.stride = stride; 00058 l.size = size; 00059 00060 l.weights = calloc(c*n*size*size, sizeof(float)); 00061 l.weight_updates = calloc(c*n*size*size, sizeof(float)); 00062 00063 l.biases = calloc(n, sizeof(float)); 00064 l.bias_updates = calloc(n, sizeof(float)); 00065 float scale = 1./sqrt(size*size*c); 00066 for(i = 0; i < c*n*size*size; ++i) l.weights[i] = scale*rand_normal(); 00067 for(i = 0; i < n; ++i){ 00068 l.biases[i] = scale; 00069 } 00070 int out_h = deconvolutional_out_height(l); 00071 int out_w = deconvolutional_out_width(l); 00072 00073 l.out_h = out_h; 00074 l.out_w = out_w; 00075 l.out_c = n; 00076 l.outputs = l.out_w * l.out_h * l.out_c; 00077 l.inputs = l.w * l.h * l.c; 00078 00079 l.col_image = calloc(h*w*size*size*n, sizeof(float)); 00080 l.output = calloc(l.batch*out_h * out_w * n, sizeof(float)); 00081 l.delta = calloc(l.batch*out_h * out_w * n, sizeof(float)); 00082 00083 l.forward = forward_deconvolutional_layer; 00084 l.backward = backward_deconvolutional_layer; 00085 l.update = update_deconvolutional_layer; 00086 00087 #ifdef GPU 00088 l.weights_gpu = cuda_make_array(l.weights, c*n*size*size); 00089 l.weight_updates_gpu = cuda_make_array(l.weight_updates, c*n*size*size); 00090 00091 l.biases_gpu = cuda_make_array(l.biases, n); 00092 l.bias_updates_gpu = cuda_make_array(l.bias_updates, n); 00093 00094 l.col_image_gpu = cuda_make_array(l.col_image, h*w*size*size*n); 00095 l.delta_gpu = cuda_make_array(l.delta, l.batch*out_h*out_w*n); 00096 l.output_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n); 00097 #endif 00098 00099 l.activation = activation; 00100 00101 fprintf(stderr, "Deconvolutional Layer: %d x %d x %d image, %d filters -> %d x %d x %d image\n", h,w,c,n, out_h, out_w, n); 00102 00103 return l; 00104 } 00105 00106 void resize_deconvolutional_layer(deconvolutional_layer *l, int h, int w) 00107 { 00108 l->h = h; 00109 l->w = w; 00110 int out_h = deconvolutional_out_height(*l); 00111 int out_w = deconvolutional_out_width(*l); 00112 00113 l->col_image = realloc(l->col_image, 00114 out_h*out_w*l->size*l->size*l->c*sizeof(float)); 00115 l->output = realloc(l->output, 00116 l->batch*out_h * out_w * l->n*sizeof(float)); 00117 l->delta = realloc(l->delta, 00118 l->batch*out_h * out_w * l->n*sizeof(float)); 00119 #ifdef GPU 00120 cuda_free(l->col_image_gpu); 00121 cuda_free(l->delta_gpu); 00122 cuda_free(l->output_gpu); 00123 00124 l->col_image_gpu = cuda_make_array(l->col_image, out_h*out_w*l->size*l->size*l->c); 00125 l->delta_gpu = cuda_make_array(l->delta, l->batch*out_h*out_w*l->n); 00126 l->output_gpu = cuda_make_array(l->output, l->batch*out_h*out_w*l->n); 00127 #endif 00128 } 00129 00130 void forward_deconvolutional_layer(const deconvolutional_layer l, network_state state) 00131 { 00132 int i; 00133 int out_h = deconvolutional_out_height(l); 00134 int out_w = deconvolutional_out_width(l); 00135 int size = out_h*out_w; 00136 00137 int m = l.size*l.size*l.n; 00138 int n = l.h*l.w; 00139 int k = l.c; 00140 00141 fill_cpu(l.outputs*l.batch, 0, l.output, 1); 00142 00143 for(i = 0; i < l.batch; ++i){ 00144 float *a = l.weights; 00145 float *b = state.input + i*l.c*l.h*l.w; 00146 float *c = l.col_image; 00147 00148 gemm(1,0,m,n,k,1,a,m,b,n,0,c,n); 00149 00150 col2im_cpu(c, l.n, out_h, out_w, l.size, l.stride, 0, l.output+i*l.n*size); 00151 } 00152 add_bias(l.output, l.biases, l.batch, l.n, size); 00153 activate_array(l.output, l.batch*l.n*size, l.activation); 00154 } 00155 00156 void backward_deconvolutional_layer(deconvolutional_layer l, network_state state) 00157 { 00158 float alpha = 1./l.batch; 00159 int out_h = deconvolutional_out_height(l); 00160 int out_w = deconvolutional_out_width(l); 00161 int size = out_h*out_w; 00162 int i; 00163 00164 gradient_array(l.output, size*l.n*l.batch, l.activation, l.delta); 00165 backward_bias(l.bias_updates, l.delta, l.batch, l.n, size); 00166 00167 for(i = 0; i < l.batch; ++i){ 00168 int m = l.c; 00169 int n = l.size*l.size*l.n; 00170 int k = l.h*l.w; 00171 00172 float *a = state.input + i*m*n; 00173 float *b = l.col_image; 00174 float *c = l.weight_updates; 00175 00176 im2col_cpu(l.delta + i*l.n*size, l.n, out_h, out_w, 00177 l.size, l.stride, 0, b); 00178 gemm(0,1,m,n,k,alpha,a,k,b,k,1,c,n); 00179 00180 if(state.delta){ 00181 int m = l.c; 00182 int n = l.h*l.w; 00183 int k = l.size*l.size*l.n; 00184 00185 float *a = l.weights; 00186 float *b = l.col_image; 00187 float *c = state.delta + i*n*m; 00188 00189 gemm(0,0,m,n,k,1,a,k,b,n,1,c,n); 00190 } 00191 } 00192 } 00193 00194 void update_deconvolutional_layer(deconvolutional_layer l, float learning_rate, float momentum, float decay) 00195 { 00196 int size = l.size*l.size*l.c*l.n; 00197 axpy_cpu(l.n, learning_rate, l.bias_updates, 1, l.biases, 1); 00198 scal_cpu(l.n, momentum, l.bias_updates, 1); 00199 00200 axpy_cpu(size, -decay, l.weights, 1, l.weight_updates, 1); 00201 axpy_cpu(size, learning_rate, l.weight_updates, 1, l.weights, 1); 00202 scal_cpu(size, momentum, l.weight_updates, 1); 00203 } 00204 00205 00206