Skip to content

Commit

Permalink
noise_data_transformer is added
Browse files Browse the repository at this point in the history
  • Loading branch information
milakov committed Jul 24, 2013
1 parent aa6d517 commit 6e62b97
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
1 change: 1 addition & 0 deletions nnforge/nnforge.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "distort_2d_data_transformer.h"
#include "extract_2d_data_transformer.h"
#include "rotate_band_2d_data_transformer.h"
#include "noise_data_transformer.h"

namespace nnforge
{
Expand Down
65 changes: 65 additions & 0 deletions nnforge/noise_data_transformer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2011-2013 Maxim Milakov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "noise_data_transformer.h"

#include "neural_network_exception.h"

#include <opencv2/core/core.hpp>
#include <boost/format.hpp>

namespace nnforge
{
noise_data_transformer::noise_data_transformer(
bool is_same_sequence_from_reset,
unsigned int max_noise)
: is_same_sequence_from_reset(is_same_sequence_from_reset)
{
if (!is_same_sequence_from_reset)
generator = rnd::get_random_generator();

max_noise_distribution = std::tr1::uniform_int<int>(-static_cast<int>(max_noise), static_cast<int>(max_noise));
}

noise_data_transformer::~noise_data_transformer()
{
}

void noise_data_transformer::reset()
{
if (is_same_sequence_from_reset)
generator = rnd::get_random_generator(48576435);
}

void noise_data_transformer::transform(
const void * input_data,
void * output_data,
neuron_data_type::input_type type,
const layer_configuration_specific& original_config)
{
if (type != neuron_data_type::type_byte)
throw neural_network_exception("noise_data_transformer is implemented for data stored as bytes only");

unsigned char * data = static_cast<unsigned char *>(output_data);
unsigned int elem_count = original_config.get_neuron_count();

for(unsigned char * data_it = data; data_it != (data + elem_count); data_it++)
{
int shift = max_noise_distribution(generator);
*data_it = static_cast<unsigned char>(std::min<int>(std::max<int>((shift + static_cast<int>(*data_it)), 0), 255));
}
}
}
50 changes: 50 additions & 0 deletions nnforge/noise_data_transformer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2011-2013 Maxim Milakov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "data_transformer.h"

#include "rnd.h"

#include <memory>

namespace nnforge
{
class noise_data_transformer : public data_transformer
{
public:
noise_data_transformer(
bool is_same_sequence_from_reset,
unsigned int max_noise);

virtual ~noise_data_transformer();

virtual void transform(
const void * input_data,
void * output_data,
neuron_data_type::input_type type,
const layer_configuration_specific& original_config);

virtual void reset();

protected:
bool is_same_sequence_from_reset;
random_generator generator;

std::tr1::uniform_int<int> max_noise_distribution;
};
}
6 changes: 3 additions & 3 deletions nnforge/rotate_band_2d_data_transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ namespace nnforge
const layer_configuration_specific& original_config)
{
if (type != neuron_data_type::type_byte)
throw neural_network_exception("distort_2d_data_transformer is implemented for data stored as bytes only");
throw neural_network_exception("rotate_band_2d_data_transformer is implemented for data stored as bytes only");

if (original_config.dimension_sizes.size() != 2)
throw neural_network_exception((boost::format("distort_2d_data_transformer is processing 2d data only, data is passed with number of dimensions %1%") % original_config.dimension_sizes.size()).str());
throw neural_network_exception((boost::format("rotate_band_2d_data_transformer is processing 2d data only, data is passed with number of dimensions %1%") % original_config.dimension_sizes.size()).str());

if (original_config.feature_map_count != 1)
throw neural_network_exception("distort_2d_data_transformer is implemented for 1 feature map data only");
throw neural_network_exception("rotate_band_2d_data_transformer is implemented for 1 feature map data only");

cv::Mat1b image(static_cast<int>(original_config.dimension_sizes[1]), static_cast<int>(original_config.dimension_sizes[0]), static_cast<unsigned char *>(output_data));

Expand Down

0 comments on commit 6e62b97

Please sign in to comment.