Commit 8547bdbe957df7284f682fd585a5d49baac2034b

Authored by Arnaud Blanchard
1 parent aa77b6ea

Add f_norm computingthe norm of 2 inputs

... ... @@ -9,66 +9,93 @@
#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);
template <typename O_t, typename D, typename K> void convolute(O_t *output, int o_width, int o_height, D *input, int iwidth, int iheight, K* kernel, int kwidth, int kheight){
int i, j, ki, kj;
O_t value;
BLC_COMMAND_LOOP(0){
FOR(j, o_height){
FOR(i, o_width){
value=0;
FOR(kj, kheight) {
FOR(ki, kwidth){
value+=input[i+ki+(kj+j)*iwidth]*kernel[kwidth-ki-1+(kheight-kj-1)*kwidth]; }
}
output[i+j*o_width]=value;
}
}
}
}
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();
/**Convolute the image with the kerne. Work only on 2D for now.*/
int main(int argc, char **argv){
blc_channel input, output, kernel;
char const *output_name, *input_name, *kernel_name;
int output_width, output_height;
int kwidth, kheight, iwidth, iheight;
uint32_t output_type;
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);
if (input.dims_nb!=2) EXIT_ON_ARRAY_ERROR(&input, "It works only in 2D for now");
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);
if (kernel.dims_nb!=2) EXIT_ON_ARRAY_ERROR(&kernel, "It works only in 2D for now");
kwidth=kernel.dims[0].length;
kheight=kernel.dims[1].length;
if ((iwidth < kwidth) || (iheight < kheight)) EXIT_ON_ERROR("Kernel cannot be smaller than input");
blc_loop_try_add_waiting_semaphore(kernel.sem_new_data);
blc_loop_try_add_posting_semaphore(kernel.sem_ack_data);
if (input.type=='FL32' || kernel.type=='FL32') output_type='FL32';
else output_type='UIN8';
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, output_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;
switch (input.type){
case 'UIN8':
switch (kernel.type){
case 'UIN8':
convolute(output.uchars, output_width, output_height, input.uchars, iwidth, iheight, kernel.uchars, kwidth, kheight);
break;
case 'FL32':
convolute(output.floats, output_width, output_height, input.uchars, iwidth, iheight, kernel.floats, kwidth, kheight);
break;
default:EXIT_ON_ARRAY_ERROR(&kernel, "Type kernel is not managed for input 'UIN8', only UIN8|FL32 are");
}
break;
case 'FL32':
switch (kernel.type){
case 'UIN8':
convolute(output.floats, output_width, output_height, input.floats, iwidth, iheight, kernel.uchars, kwidth, kheight);
break;
case 'FL32':
convolute(output.floats, output_width, output_height, input.floats, iwidth, iheight, kernel.floats, kwidth, kheight);
break;
default:EXIT_ON_ARRAY_ERROR(&kernel, "Type kernel is not managed for input 'FL32', only UIN8|FL32 are");
}
break;
default:EXIT_ON_ARRAY_ERROR(&input, "Type not managed, only UIN8|FL32 are");
}
return EXIT_SUCCESS;
}
... ...
# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 2.6)
# Set the name of the project as the directory basename
project(f_norm)
find_package(blc_channel)
find_package(blc_program)
add_definitions(${BL_DEFINITIONS})
include_directories(${BL_INCLUDE_DIRS})
add_executable(f_norm f_norm.cpp)
target_link_libraries(f_norm ${BL_LIBRARIES})
... ...
# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
# Author: Arnaud Blanchard (November 2016)
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
cd `dirname $0`
echo
echo "Checking `basename $PWD`"
echo "===================="
echo
echo "No check for now"
echo
... ...
//
// 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 <complex> //for std::abs
#define DEFAULT_OUTPUT_NAME ":norm<pid>"
template <typename T> void norm1(T *output, T *input1, T *input2, int length ){
int i;
BLC_COMMAND_LOOP(0){
FOR(i, length){
output[i]=std::abs(input1[i])+std::abs(input2[i]);
}
}
}
void norm1(uchar *output, uchar *input1, uchar *input2, int length ){
int i;
BLC_COMMAND_LOOP(0){
FOR(i, length){
output[i]=CLIP_UCHAR(input1[i]+input2[i]);
}
}
}
template <typename T> void norm2(T *output, T *input1, T *input2, int length ){
int i;
BLC_COMMAND_LOOP(0){
FOR(i, length){
output[i]=sqrt(input1[i]*input1[i]+input2[i]*input2[i]);
}
}
}
template <typename T> void norm_sup(T *output, T *input1, T *input2, int length ){
int i;
BLC_COMMAND_LOOP(0){
FOR(i, length){
output[i]=MAX(input1[i], input2[i]);
}
}
}
template <typename T> void norm(T *output, T *input1, T *input2, int length, char const *norm_str ){
if (strcmp(norm_str, "1")) norm1(output, input1, input2, length);
else if (strcmp(norm_str, "2")) norm1(output, input1, input2, length);
else if (strcmp(norm_str, "-1")) norm1(output, input1, input2, length);
else EXIT_ON_ERROR("Norm '%s' is unknow", norm_str);
}
int main(int argc, char **argv){
blc_channel input1, input2, output, kernel;
char const *output_name, *input1_name, *input2_name, *norm_str;
int width, height;
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_option(&norm_str, 'L', "norm", "blc_channel-out", "espace de norm (1:manathan, 2:eucldean, -1:max) ", "2");
blc_program_add_parameter(&input1_name, "blc_channel-in", 1, "element 1", NULL);
blc_program_add_parameter(&input2_name, "blc_channel-in", 1, "element 2", NULL);
blc_program_init(&argc, &argv, blc_quit);
input1.open(input1_name, BLC_CHANNEL_READ);
width=input1.dims[0].length;
height=input1.dims[1].length;
blc_loop_try_add_waiting_semaphore(input1.sem_new_data);
blc_loop_try_add_posting_semaphore(input1.sem_ack_data);
input2.open(input2_name, BLC_CHANNEL_READ);
if (width!=input2.dims[0].length) EXIT_ON_ARRAY_ERROR(&input2, "The two inputs must have the same width of '%d'", width);
if (height!=input2.dims[1].length) EXIT_ON_ARRAY_ERROR(&input2, "The two inputs must have the same height of '%d'", height);
if (input1.type!=input2.type) EXIT_ON_ARRAY_ERROR(&input2, "The two inputs must have the same type");
blc_loop_try_add_waiting_semaphore(input2.sem_new_data);
blc_loop_try_add_posting_semaphore(input2.sem_ack_data);
if (strcmp(output_name, DEFAULT_OUTPUT_NAME)==0) SYSTEM_ERROR_CHECK(asprintf((char**)&output_name,":norm%d", getpid()), -1, NULL);
output.create_or_open(output_name, BLC_CHANNEL_WRITE, input1.type, input1.format, 2, width, height);
blc_loop_try_add_waiting_semaphore(output.sem_ack_data);
blc_loop_try_add_posting_semaphore(output.sem_new_data);
output.publish();
switch (input1.type){
case 'UIN8': norm(output.uchars, input1.uchars, input2.uchars, output.total_length, norm_str);
break;
case 'FL32': norm(output.floats, input1.floats, input2.floats, output.total_length, norm_str);
break;
default:EXIT_ON_ARRAY_ERROR(&input1, "Type not managed, only UIN8|FL32 are");
}
return EXIT_SUCCESS;
}
... ...