|
|
//
|
|
|
// Created by Arnaud Blanchard on 22/12/15.
|
|
|
// Copyright ETIS 2015. All rights reserved.
|
|
|
//
|
|
|
#include "blc_channel.h"
|
|
|
#include "blc_program.h"
|
|
|
#include <unistd.h>
|
|
|
#include <math.h>
|
|
|
|
|
|
#define DEFAULT_OUTPUT_NAME ":conv<pid>"
|
|
|
|
|
|
int main(int argc, char **argv){
|
|
|
blc_channel input, output, kernel;
|
|
|
char const *output_name, *input_name, *kernel_name;
|
|
|
float value;
|
|
|
int in_width, in_height, grad_height, grad_width, downscale=1;
|
|
|
int i, j, ki, kj, output_width, output_height;
|
|
|
int kwidth, kheight, iwidth, iheight;
|
|
|
|
|
|
blc_program_set_description("Compute convolution 2d of the data");
|
|
|
blc_program_add_option(&output_name, 'o', "output", "blc_channel-out", "channel name", DEFAULT_OUTPUT_NAME);
|
|
|
blc_program_add_parameter(&input_name, "blc_channel-in", 1, "data to convolute ", NULL);
|
|
|
blc_program_add_parameter(&kernel_name, "blc_channel-in", 1, "kernel to use for convolution ", NULL);
|
|
|
blc_program_init(&argc, &argv, blc_quit);
|
|
|
|
|
|
input.open(input_name, BLC_CHANNEL_READ);
|
|
|
iwidth=input.dims[0].length;
|
|
|
iheight=input.dims[1].length;
|
|
|
blc_loop_try_add_waiting_semaphore(input.sem_new_data);
|
|
|
blc_loop_try_add_posting_semaphore(input.sem_ack_data);
|
|
|
|
|
|
kernel.open(kernel_name, BLC_CHANNEL_READ);
|
|
|
kwidth=kernel.dims[0].length;
|
|
|
kheight=kernel.dims[1].length;
|
|
|
blc_loop_try_add_waiting_semaphore(kernel.sem_new_data);
|
|
|
blc_loop_try_add_posting_semaphore(kernel.sem_ack_data);
|
|
|
|
|
|
if (strcmp(output_name, DEFAULT_OUTPUT_NAME)==0) SYSTEM_ERROR_CHECK(asprintf((char**)&output_name,":conv%d", getpid()), -1, NULL);
|
|
|
output_width=iwidth-kwidth+1;
|
|
|
output_height=iheight-kheight+1;
|
|
|
output.create_or_open(output_name, BLC_CHANNEL_WRITE, input.type, input.format, 2, output_width, output_height);
|
|
|
blc_loop_try_add_waiting_semaphore(output.sem_ack_data);
|
|
|
blc_loop_try_add_posting_semaphore(output.sem_new_data);
|
|
|
output.publish();
|
|
|
|
|
|
|
|
|
if (input.type=='FL32'){
|
|
|
BLC_COMMAND_LOOP(0){
|
|
|
FOR(j, output_height){
|
|
|
FOR(i, output_width){
|
|
|
value=0;
|
|
|
FOR(kj, kheight) FOR(ki, kwidth){
|
|
|
value+=kernel.floats[kwidth-ki+(kheight-kj)*kwidth]*input.floats[i+ki+(kheight-kj+j)*iwidth];
|
|
|
}
|
|
|
output.floats[i+j*output_width]=value;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else if (input.type=='UIN8'){
|
|
|
BLC_COMMAND_LOOP(0){
|
|
|
FOR(j, output_height){
|
|
|
FOR(i, output_width){
|
|
|
value=0;
|
|
|
FOR(kj, kheight) FOR(ki, kwidth){
|
|
|
value+=kernel.floats[kwidth-ki+(kheight-kj)*kwidth]*input.uchars[i+ki+(kheight-kj+j)*iwidth];
|
|
|
}
|
|
|
output.uchars[i+j*output_width]=value;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return EXIT_SUCCESS;
|
|
|
} |
...
|
...
|
|