Skip to content

Commit

Permalink
adding MITLicensing
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalmanifold committed Aug 26, 2024
1 parent 7ffe97c commit 0eb5e3d
Show file tree
Hide file tree
Showing 26 changed files with 107 additions and 127 deletions.
19 changes: 19 additions & 0 deletions opm/ml/LICENSE.MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 Robert W. Rose, 2018 Paul Maevskikh, 2024 NORCE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 2 additions & 20 deletions opm/ml/keras_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/

/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -37,8 +36,6 @@

namespace Opm {

#pragma once


bool ReadUnsignedInt(std::ifstream* file, unsigned int* i) {
KASSERT(file, "Invalid file stream");
Expand Down Expand Up @@ -173,8 +170,6 @@ bool KerasLayerScaling<Evaluation>::LoadLayer(std::ifstream* file) {
KASSERT(ReadFloat(file, &data_max), "Failed to read max");
KASSERT(ReadFloat(file, &feat_inf), "Failed to read max");
KASSERT(ReadFloat(file, &feat_sup), "Failed to read max");

// KASSERT(ReadFloat(file, &feat), "Failed to read max");
return true;
}

Expand All @@ -188,8 +183,6 @@ bool KerasLayerScaling<Evaluation>::Apply(Tensor<Evaluation>* in, Tensor<Evaluat
*out = *in;

for (size_t i = 0; i < out->data_.size(); i++) {
// std::cout<<"out->data_[i]"<<std::endl;
// std::cout<<out->data_[i]<<std::endl;
auto tempscale = (out->data_[i] - data_min)/(data_max - data_min);
out->data_[i] = tempscale * (feat_sup - feat_inf) + feat_inf;
}
Expand All @@ -214,13 +207,9 @@ bool KerasLayerUnScaling<Evaluation>::Apply(Tensor<Evaluation>* in, Tensor<Evalu
KASSERT(in, "Invalid input");
KASSERT(out, "Invalid output");


*out = *in;
// out->Flatten();

for (size_t i = 0; i < out->data_.size(); i++) {
// std::cout<<"out->data_[i]"<<std::endl;
// std::cout<<out->data_[i]<<std::endl;
auto tempscale = (out->data_[i] - feat_inf)/(feat_sup - feat_inf);

out->data_[i] = tempscale * (data_max - data_min) + data_min;
Expand Down Expand Up @@ -267,13 +256,6 @@ bool KerasLayerDense<Evaluation>::Apply(Tensor<Evaluation>* in, Tensor<Evaluatio
KASSERT(out, "Invalid output");
KASSERT(in->dims_.size() <= 2, "Invalid input dimensions");


// if (in->dims_.size() == 1) {
// KASSERT(in->dims_[0] == weights_.dims_[0], "Dimension mismatch %d %d",
// in->dims_[0], weights_.dims_[0]);
// }


if (in->dims_.size() == 2) {
KASSERT(in->dims_[1] == weights_.dims_[0], "Dimension mismatch %d %d",
in->dims_[1], weights_.dims_[0]);
Expand Down
5 changes: 2 additions & 3 deletions opm/ml/keras_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/

/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand Down
5 changes: 2 additions & 3 deletions opm/ml/keras_model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/

/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand Down
35 changes: 6 additions & 29 deletions opm/ml/ml_tools/generateunittests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Copyright (c) 2024 Birane Kane
# Copyright (c) 2024 Tor Harald Sandve

# Copyright (c) 2024 NORCE
# This file is part of the Open Porous Media project (OPM).

# OPM is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -47,12 +45,11 @@ def to_cpp(ndarray):
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/
/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
* Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -142,12 +139,13 @@ def output_testcase(model, test_x, test_y, name, eps):
print(model.summary())

export_model(model, 'models/test_%s.model' % name)
path = os.path.abspath(f'models/test_{name}.model')
path = os.path.abspath(f'../../opm-common/opm/ml/ml_tools/models/test_{name}.model')
relative_path = os.path.relpath(path)
with open('include/test_%s.h' % name, 'w') as f:
x_shape, x_data = c_array(test_x[0])
y_shape, y_data = c_array(predict_y[0])

f.write(TEST_CASE % (name, name, x_shape, x_data, y_shape, y_data, path, eps))
f.write(TEST_CASE % (name, name, x_shape, x_data, y_shape, y_data, relative_path, eps))



Expand All @@ -159,36 +157,15 @@ def output_testcase(model, test_x, test_y, name, eps):
data_min = 10.0
model = Sequential()
model.add(keras.layers.Input([10]))
# model.add(Dense(1, input_dim=10))
model.add(MinMaxScalerLayer(feature_range=(0.0, 1.0)))
# model.add(Dense(1,activation='tanh'))
model.add(Dense(10,activation='tanh'))
model.add(Dense(10,activation='tanh'))
model.add(Dense(10,activation='tanh'))
model.add(Dense(10,activation='tanh'))
# model.add(Flatten())
model.add(MinMaxUnScalerLayer(feature_range=(-3.7, -1.0)))
# #
model.get_layer(model.layers[0].name).adapt(data=data)
model.get_layer(model.layers[-1].name).adapt(data=data)

# model.add(Dense(1, input_dim=1))

# model: keras.Model = keras.Sequential(
#
# [
#
# keras.layers.Input([1]),
#
# MinMaxScalerLayer(feature_range=(0.0, 1.0)),
#
# # keras.layers.Dense(1, input_dim=1),
#
# # MinMaxUnScalerLayer(feature_range=(0.0, 1.0)),
#
# ]
#
# )
output_testcase(model, test_x, test_y, 'scalingdense_1x1', '1e-3')

# Dense 1x1
Expand Down
14 changes: 7 additions & 7 deletions opm/ml/ml_tools/include/test_dense_10x1.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/

/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
* Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand All @@ -25,6 +24,7 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
Expand All @@ -39,17 +39,17 @@ bool test_dense_10x1(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.07588807,0.46424174,0.2545689,0.24965246,0.48471555,0.9185193,
0.33841145,0.30067867,0.98624486,0.85875916};
in.data_ = {0.74998796,0.21858868,0.23070294,0.070500664,0.46532258,0.49146336,
0.3717498,0.45293888,0.6332608,0.43785256};

Opm::Tensor<Evaluation> out{1};
out.data_ = {-0.6094914};
out.data_ = {0.49467176};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("/Users/macbookn/hackatonwork/opm-common/opm/ml/ml_tools/models/test_dense_10x1.model"), "Failed to load model");
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_10x1.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
14 changes: 7 additions & 7 deletions opm/ml/ml_tools/include/test_dense_10x10.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/

/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
* Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand All @@ -25,6 +24,7 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
Expand All @@ -39,17 +39,17 @@ bool test_dense_10x10(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.74464816,0.063315846,0.2233216,0.3184675,0.017677268,0.24977225,
0.5242765,0.27742663,0.009411842,0.27946305};
in.data_ = {0.46586692,0.9036544,0.5005982,0.5969117,0.09755054,0.60282475,
0.9040883,0.6297268,0.12081735,0.42354217};

Opm::Tensor<Evaluation> out{1};
out.data_ = {0.06583358};
out.data_ = {0.04306573};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("/Users/macbookn/hackatonwork/opm-common/opm/ml/ml_tools/models/test_dense_10x10.model"), "Failed to load model");
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_10x10.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
16 changes: 8 additions & 8 deletions opm/ml/ml_tools/include/test_dense_10x10x10.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/

/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
* Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand All @@ -25,6 +24,7 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
Expand All @@ -39,18 +39,18 @@ bool test_dense_10x10x10(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.84246427,0.15756324,0.8924635,0.5069177,0.8633376,0.31142905,
0.7651278,0.5454816,0.22917923,0.54828155};
in.data_ = {0.5250269,0.20885888,0.2868855,0.11718734,0.56410795,0.3253708,
0.63131493,0.44084042,0.6677203,0.29525173};

Opm::Tensor<Evaluation> out{10};
out.data_ = {0.767188,0.23175952,0.036515277,-0.2622609,-0.25455678,
0.157909,-0.6259325,-0.6256348,0.4324326,-0.4141315};
out.data_ = {0.054961562,-0.45561615,-0.2112424,0.0062360223,0.3164827,
0.746866,-0.41519523,0.004653876,0.58843577,-0.3870317};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("/Users/macbookn/hackatonwork/opm-common/opm/ml/ml_tools/models/test_dense_10x10x10.model"), "Failed to load model");
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_10x10x10.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
8 changes: 4 additions & 4 deletions opm/ml/ml_tools/include/test_dense_1x1.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* Copyright (c) 2016 Robert W. Rose
* Copyright (c) 2018 Paul Maevskikh
*
* MIT License, see LICENSE.OLD file.
* MIT License, see LICENSE.MIT file.
*/

/*
* Copyright (c) 2024 Birane Kane
* Copyright (c) 2024 Tor Harald Sandve
* Copyright (c) 2024 NORCE
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
Expand All @@ -25,6 +24,7 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
Expand All @@ -48,7 +48,7 @@ bool test_dense_1x1(Evaluation* load_time, Evaluation* apply_time)
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("/Users/macbookn/hackatonwork/opm-common/opm/ml/ml_tools/models/test_dense_1x1.model"), "Failed to load model");
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_1x1.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Loading

0 comments on commit 0eb5e3d

Please sign in to comment.