oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

Batch normalizatino call

Open RadjaHachilif opened this issue 8 years ago • 2 comments

Hello people , I'm trying to implement a batch normalization function on an input of dimension { batch_size, features, height, width } what i want is normalizing images for each filter , in c++ code the function is :

c_output(N, N, FIn, BATCH_SIZE);    c_mean(FIn);    c_variance(FIn);

for (int z = 0; z < FIn; ++z)
    {c_mean(z)=0;
for (int n = 0; n < BATCH_SIZE; ++n)
    for (int y = 0; y < N; ++y)	       
	for (int x = 0; x < N; ++x)		    c_mean(z) +=input(x, y, z, n)/(BATCH_SIZE*N*N) ; }
for (int z = 0; z < FIn; ++z)
    {c_variance(z)=0;
for (int n = 0; n < BATCH_SIZE; ++n)
    for (int y = 0; y < N; ++y)	       
	for (int x = 0; x < N; ++x)		    c_variance(z) +=(input(x, y, z, n)-c_mean(z))*(input(x, y, z, n)-c_mean(z))/(BATCH_SIZE*N*N) ; }

for (int n = 0; n < BATCH_SIZE; ++n)
for (int z = 0; z < FIn; ++z) 
   for (int y = 0; y < N; ++y)	       
	for (int x = 0; x < N; ++x)	
	    {resultfile <<(input(x, y, z, n)-c_mean(z))/sqrt(c_variance(z)); c_output(x, y, z, n)=(input(x, y, z, n)-c_mean(z))/sqrt(c_variance(z)); }

how could i have the same call using mkldnn primitive ? Thank you

RadjaHachilif avatar Nov 12 '18 20:11 RadjaHachilif

Radja,

I see you've submitted another related ticket, but here is a basic batch norm example using MKLDNN.

`#include #include "mkldnn.hpp" auto cpu_engine = engine(engine::cpu, 0);

const int batch = 32;
int c = 3; int h = 227; int w = 227;

/* initializing non-zero values for synthetic src data*/
std::vector<float> net_src(batch * c * h * w);
for (size_t i = 0; i < net_src.size(); ++i) 
net_src[i] = sinf((float)i);

/* create memory for user data */
memory::dims bn_src_tz = {batch, c, h, w};
auto bn_user_src_memory = memory(
        { { { bn_src_tz }, memory::data_type::f32, memory::format::nchw },
          cpu_engine },
        net_src.data());

/* create memory descriptors for bn data w/ no specified */
auto bn_src_md = memory::desc({ bn_src_tz }, memory::data_type::f32,
                                memory::format::nchw);

/* create a bn primitive descriptor (epsilon is set to 1.0 in this example) */
auto bn_desc = batch_normalization_forward::desc(
        prop_kind::forward_inference, bn_src_md, 1.0, 0);
auto bn_pd = batch_normalization_forward::primitive_desc(bn_desc, cpu_engine);


/* check if user input and bn src data format is same */
auto bn_src_memory = bn_user_src_memory;
bool reorder_bn_src = false;
primitive bn_reorder_src;
if (memory::primitive_desc(bn_pd.src_primitive_desc())
    != bn_user_src_memory.get_primitive_desc()) {
    throw std::invalid_argument("data format in source does not match primitive descriptor");
}

/* create memory primitive for bn dst */
auto bn_dst_memory = memory(bn_pd.dst_primitive_desc());

/* finally create a bn operation primitive */
auto bn = batch_normalization_forward(bn_pd, bn_src_memory, bn_dst_memory);

/* create net and add topology one layer primitive at a time */
std::vector<primitive> net_fwd;
net_fwd.push_back(bn);
stream(stream::kind::eager).submit(net_fwd).wait();`

nathan-greeneltch-intel avatar Nov 19 '18 18:11 nathan-greeneltch-intel

Thank you so much

RadjaHachilif avatar Nov 19 '18 18:11 RadjaHachilif