Skip to content

Commit

Permalink
adding MITLicensing
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalmanifold committed Aug 28, 2024
1 parent 7ffe97c commit e2f0a97
Show file tree
Hide file tree
Showing 37 changed files with 137 additions and 198 deletions.
11 changes: 10 additions & 1 deletion CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ list (APPEND TEST_SOURCE_FILES
tests/material/test_spline.cpp
tests/material/test_tabulation.cpp
tests/test_Visitor.cpp
opm/ml/keras_model_test.cpp
tests/ml/keras_model_test.cpp
)

# tests that need to be linked to dune-common
Expand Down Expand Up @@ -648,6 +648,15 @@ list (APPEND TEST_DATA_FILES
tests/material/co2_unittest_below_sat.json
tests/material/h2o_unittest.json
tests/material/h2_unittest.json
tests/ml/ml_tools/models/test_dense_1x1.model
tests/ml/ml_tools/models/test_dense_2x2.model
tests/ml/ml_tools/models/test_dense_10x1.model
tests/ml/ml_tools/models/test_dense_10x10.model
tests/ml/ml_tools/models/test_dense_10x10x10.model
tests/ml/ml_tools/models/test_dense_relu_10.model
tests/ml/ml_tools/models/test_dense_tanh_10.model
tests/ml/ml_tools/models/test_relu_10.model
tests/ml/ml_tools/models/test_scalingdense_10x1.model
)
if(ENABLE_ECL_OUTPUT)
list (APPEND TEST_DATA_FILES
Expand Down
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
Empty file added opm/ml/ml_tools/__init__.py
Empty file.
10 changes: 8 additions & 2 deletions opm/ml/ml_tools/kerasify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Copyright (c) 2024 Birane Kane
# Copyright (c) 2024 Tor Harald Sandve
# /*

# * Copyright (c) 2016 Robert W. Rose
# * Copyright (c) 2018 Paul Maevskikh
# *
# * MIT License, see LICENSE.MIT file.
# */

# 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
Binary file removed opm/ml/ml_tools/models/test_dense_10x1.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_10x10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_10x10x10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_1x1.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_2x2.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_relu_10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_tanh_10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_relu_10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_scalingdense_1x1.model
Binary file not shown.
5 changes: 2 additions & 3 deletions opm/ml/ml_tools/scaler_layers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) 2024 Birane Kane
# Copyright (c) 2024 Tor Harald Sandve
# Copyright (c) 2024 Peter Moritz von Schultzendorff
# Copyright (c) 2024 NORCE
# Copyright (c) 2024 UiB

# This file is part of the Open Porous Media project (OPM).

Expand Down
51 changes: 2 additions & 49 deletions opm/ml/ml_tools/scalertest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) 2024 Birane Kane
# Copyright (c) 2024 Tor Harald Sandve
# Copyright (c) 2024 Peter Moritz von Schultzendorff
# Copyright (c) 2024 NORCE
# Copyright (c) 2024 UiB

# This file is part of the Open Porous Media project (OPM).

Expand All @@ -17,8 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with OPM. If not, see <http://www.gnu.org/licenses/>.



from __future__ import annotations

import pathlib
Expand All @@ -40,58 +37,14 @@

data: np.ndarray = np.random.uniform(-500, 500, (5, 1))

# model: keras.Model = keras.Sequential(
#
# [
#
# keras.layers.Input([10]),
#
# MinMaxScalerLayer(feature_range=feature_ranges[0]),
#
# keras.layers.Dense(units=10),
#
# MinMaxUnScalerLayer(feature_range=feature_ranges[1]),
#
# ]
#
# )

model = Sequential()
model.add(keras.layers.Input([1]))
model.add(MinMaxScalerLayer(feature_range=(0.0, 1.0)))
# model.add(Flatten())
model.add(Dense(1, input_dim=1))
model.add(Dense(1, input_dim=1))
model.add(Dense(1, input_dim=1))
model.add(Dense(1, input_dim=1))

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)),
#
# ]
#
# )


#
# model.get_layer(model.layers[0].name).adapt(data=data)
# #
# model.get_layer(model.layers[-1].name).adapt(data=data)

export_model(model, str(savepath))
29 changes: 14 additions & 15 deletions opm/ml/keras_model_test.cpp → tests/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 All @@ -25,18 +24,18 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include "keras_model.hpp"
#include <opm/ml/keras_model.hpp>
#include <iostream>
#include <stdio.h>
#include "ml_tools/include/test_dense_10x1.h"
#include "ml_tools/include/test_dense_10x10.h"
#include "ml_tools/include/test_dense_10x10x10.h"
#include "ml_tools/include/test_dense_1x1.h"
#include "ml_tools/include/test_dense_2x2.h"
#include "ml_tools/include/test_relu_10.h"
#include "ml_tools/include/test_dense_relu_10.h"
#include "ml_tools/include/test_dense_tanh_10.h"
#include "ml_tools/include/test_scalingdense_1x1.h"
#include <tests/ml/ml_tools/include/test_dense_10x1.hpp>
#include <tests/ml/ml_tools/include/test_dense_10x10.hpp>
#include <tests/ml/ml_tools/include/test_dense_10x10x10.hpp>
#include <tests/ml/ml_tools/include/test_dense_1x1.hpp>
#include <tests/ml/ml_tools/include/test_dense_2x2.hpp>
#include <tests/ml/ml_tools/include/test_relu_10.hpp>
#include <tests/ml/ml_tools/include/test_dense_relu_10.hpp>
#include <tests/ml/ml_tools/include/test_dense_tanh_10.hpp>
#include <tests/ml/ml_tools/include/test_scalingdense_10x1.hpp>


namespace Opm {
Expand Down Expand Up @@ -149,9 +148,9 @@ bool tensor_test() {
return true;
}


}


int main() {
typedef Opm::DenseAd::Evaluation<double, 1> Evaluation;

Expand Down Expand Up @@ -194,7 +193,7 @@ int main() {
return 1;
}

if (!test_scalingdense_1x1<Evaluation>(&load_time, &apply_time)) {
if (!test_scalingdense_10x1<Evaluation>(&load_time, &apply_time)) {
return 1;
}

Expand Down
Loading

0 comments on commit e2f0a97

Please sign in to comment.