diff options
author | Vincent Le Garrec <legarrec.vincent@gmail.com> | 2018-02-18 10:59:58 +0100 |
---|---|---|
committer | Vincent Le Garrec <legarrec.vincent@gmail.com> | 2018-02-18 10:59:58 +0100 |
commit | f26664b907986b86d5f6896f7dfe6bcb3cfa6ebd (patch) | |
tree | 0ee68a6c6e5fa76854aa3d914e69908330eeb364 | |
parent | 56bfe8ecdc5f3e41ea8ce84ef4e9b54c86f1e2ec (diff) |
Add missing includeprivate/bansan/chardraw
and small changes to be compatible with changes of the master.
-rw-r--r-- | cui/source/dialogs/cuicharmap.cxx | 11 | ||||
-rw-r--r-- | cui/source/factory/neuralnetworkinternal.hxx | 94 | ||||
-rw-r--r-- | cui/source/inc/neuralnetwork.hxx | 4 |
3 files changed, 102 insertions, 7 deletions
diff --git a/cui/source/dialogs/cuicharmap.cxx b/cui/source/dialogs/cuicharmap.cxx index a1fd1a8ee3c9..ae20b895160e 100644 --- a/cui/source/dialogs/cuicharmap.cxx +++ b/cui/source/dialogs/cuicharmap.cxx @@ -77,7 +77,7 @@ void Ocr::ReadBitmap() } else { - Color c = r->GetPixel (j, i); + BitmapColor c = r->GetPixel (j, i); if (c.GetRed () == 0 && c.GetGreen () == 0 && c.GetBlue () == 0) { data[j*w_+i] = 1; @@ -235,7 +235,7 @@ void Ocr::ToFann(fann_type *out_data) } else { - Color c = r->GetPixel(j, i); + BitmapColor c = r->GetPixel(j, i); if (c.GetRed () == 255 && c.GetGreen () == 255 && c.GetBlue () == 255) { out_data[idata] = 0.; @@ -1232,7 +1232,7 @@ IMPL_LINK_NOARG(SvxCharacterMap, DrawToggleHdl, Button*, void) { for (long k = 0; k < w; k++) { - Color c = r->GetPixel(j, k); + BitmapColor c = r->GetPixel(j, k); if (c.GetRed () == 0 && c.GetGreen () == 0 && c.GetBlue () == 0) { std::cout << "1"; @@ -1306,15 +1306,16 @@ IMPL_LINK_NOARG(SvxCharacterMap, DrawToggleHdl, Button*, void) delete ann; } - fann_type *calc_out; fann_type input[Ocr::SIZE*Ocr::SIZE]; std::cout << "Starting loading fann" << std::endl; AbstractNeuralNetwork * ann = AbstractNeuralNetwork::CreateFactory("/tmp/fann.net"); + fann_type calc_out[ann->GetNumOutput()]; + o.ToFann(&input[0]); std::cout << "Starting finding best result" << std::endl; - calc_out = ann->Run(input); + ann->Run(input, &calc_out[0]); std::cout << "End of fann" << std::endl; std::multimap<float, sal_UCS4> sorted_results; diff --git a/cui/source/factory/neuralnetworkinternal.hxx b/cui/source/factory/neuralnetworkinternal.hxx new file mode 100644 index 000000000000..3a2956adc89d --- /dev/null +++ b/cui/source/factory/neuralnetworkinternal.hxx @@ -0,0 +1,94 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you 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 . + */ +#ifndef INCLUDED_CUI_SOURCE_FACTORY_NEURALNETWORKINTERNAL_HXX +#define INCLUDED_CUI_SOURCE_FACTORY_NEURALNETWORKINTERNAL_HXX + +#include "neuralnetwork.hxx" + +#include <fann.h> +#include <vector> + +class NeuralNetworkInternal : public AbstractNeuralNetwork +{ +public: + NeuralNetworkInternal(sal_uInt32 nLayers, const sal_uInt32* nLayer); + NeuralNetworkInternal(const OUString& file); + + void SetActivationFunction(ActivationFunction function) override; + void SetTrainingAlgorithm(TrainingAlgorithm algorithm) override; + void SetLearningRate(float rate) override; + + void InitTraining(sal_uInt32 nExamples) override; + sal_uInt32 GetNumInput() override; + float* GetInput(sal_uInt32 nIeme) override; + sal_uInt32 GetNumOutput() override; + float* GetOutput(sal_uInt32 nIeme) override; + + void Train(sal_uInt32 nEpochs, float error) override; + void Run(float *data_input, float* result) override; + void Save(const OUString& file) override; + + virtual ~NeuralNetworkInternal(){} + + virtual void * GetTrain(){return nullptr;} + +private: + enum class FunctionTrans + { + Sigmoid, + SigmoidSymmetric + }; + struct Neuron + { + // value of neuron. + // fann: value + // 0: input. + // last: output. + float a; + // weight + // fann: weights[first_con to last_con-2] + // Useless for n[0] + std::vector<float> w; // Number of neuron of the next layer without biais. + std::vector<float> dw; // Number of neuron of the next layer without biais. + // biais. + // fann: weights[last_con-1] + // function + // Useless for n[0] + FunctionTrans f; + // Steepness. + float stp; + + // temporary field. + // gradient + float s; + float sum; + + // b : between [-.1;.1] + Neuron() : a(0.), w(), f(FunctionTrans::Sigmoid), stp(1.f), sum(0) {} + }; + std::vector<std::vector<Neuron>> n; // One neuron is biais. + float learning_rate; + float learning_rate_alpha; + std::vector<std::vector<float>> learning_input; + std::vector<std::vector<float>> learning_output; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cui/source/inc/neuralnetwork.hxx b/cui/source/inc/neuralnetwork.hxx index 045909214de9..5f0bb472074b 100644 --- a/cui/source/inc/neuralnetwork.hxx +++ b/cui/source/inc/neuralnetwork.hxx @@ -26,7 +26,7 @@ class AbstractNeuralNetwork { public: enum class ActivationFunction { - SIGMOID + SIGMOID, SIGMOID_SYMMETRIC }; enum class TrainingAlgorithm { INCREMENTAL @@ -44,7 +44,7 @@ public: virtual float* GetOutput(sal_uInt32 nIeme) = 0; virtual void Train(sal_uInt32 nEpochs, float error) = 0; - virtual float* Run(float *data_input) = 0; + virtual void Run(float *data_input, float* result) = 0; virtual void Save(const OUString& file) = 0; virtual ~AbstractNeuralNetwork(){} |