예제 #1
0
from blackfox import BlackFox
from blackfox import PredictionFileConfig
from blackfox import Range

blackfox_url = 'http://localhost:50476/'
bf = BlackFox(blackfox_url)

config = PredictionFileConfig(
    input_ranges=[
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1)
    ],
    output_ranges=[Range(min=0, max=1),
                   Range(min=0, max=1)])

bf.predict_from_file_keras(config, 'data/optimized_network_cancer.h5',
                           'data/cancer_test_set_input.csv',
                           'data/cancer_predict.csv')
from blackfox import BlackFox
from blackfox import KerasOptimizationConfig
from blackfox import OptimizationEngineConfig
from blackfox import Range

blackfox_url = 'http://localhost:50476/'
bf = BlackFox(blackfox_url)

# custom config
engine_config = OptimizationEngineConfig(
    crossover_distribution_index=20,
    crossover_probability=0.9,
    mutation_distribution_index=20,
    mutation_probability=0.01,
    proc_timeout_miliseconds=200000,
    max_num_of_generations=10,
    population_size=20
)

config = KerasOptimizationConfig(
    dropout=Range(min=0, max=25),
    batch_size=10,
    input_ranges=[
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
예제 #3
0
import seaborn as sns
import matplotlib as mpl
mpl.style.use('ggplot')
import warnings
warnings.filterwarnings("ignore")
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import h5py
import time

# blackfox_url = 'http://147.91.204.14:32702' # BF_URL
# blackfox_url = 'http://147.91.204.85:32723'# ~VODAFONE_URL
blackfox_url = 'http://147.91.204.14:32706'  # BF FOR MAPE verziju blackfox 3.2.0
# blackfox_url = 'http://localhost:5000'
bf = BlackFox(blackfox_url)

dataframe = pd.read_csv(
    'F:\\.spyder-py3\\projekti\\Vodena_Primeri\\ImplementingEncodingInBf\\data\\data_set.csv'
)

y = dataframe.iloc[:, -1:].values
X = dataframe.iloc[:, :-1].values

X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=0)

# X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state = 0)
예제 #4
0
from blackfox import BlackFox
from blackfox import Range
from blackfox import KerasLayerConfig
from blackfox import KerasTrainingConfig
from blackfox import KerasHiddenLayerConfig

blackfox_url = 'http://localhost:50476/'
bf = BlackFox(blackfox_url)

input_ranges = [
    Range(min=0, max=1),
    Range(min=0, max=1),
    Range(min=0, max=1),
    Range(min=0, max=1),
    Range(min=0, max=1),
    Range(min=0, max=1),
    Range(min=0, max=1),
    Range(min=0, max=1),
    Range(min=0, max=1)
]

output_layer = KerasLayerConfig(
    activation_function='Sigmoid',
    ranges=[Range(min=0, max=1), Range(min=0, max=1)])

hidden_layer_configs = [
    KerasHiddenLayerConfig(neuron_count=6, activation_function='Sigmoid'),
    KerasHiddenLayerConfig(neuron_count=6, activation_function='Sigmoid'),
    KerasHiddenLayerConfig(neuron_count=6, activation_function='Sigmoid'),
    KerasHiddenLayerConfig(neuron_count=6, activation_function='Sigmoid')
]
from blackfox import BlackFox
from blackfox import KerasOptimizationConfig
from blackfox import OptimizationEngineConfig
from blackfox import Range
import csv

blackfox_url = 'http://localhost:50476/'
bf = BlackFox(blackfox_url)

input_columns = 9
input_set = []
output_set = []

with open('data/cancer_training_set.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
        else:
            data = list(map(float, row))
            input_set.append(data[:input_columns])
            output_set.append(data[input_columns:])

        line_count += 1

    print(f'Processed {line_count} lines.')

# Use CTRL + C to stop optimization
network_io = bf.optimize_keras_sync(input_set, output_set)
if network_io is not None:
from blackfox import BlackFox
from blackfox import PredictionArrayConfig
from blackfox import Range

blackfox_url = 'http://localhost:50476/'
bf = BlackFox(blackfox_url)

config = PredictionArrayConfig(
    data_set=[[0.2, 0.1, 0.1, 0.1, 0.2, 0.1, 0.2, 0.1, 0.1],
              [0.2, 0.1, 0.1, 0.1, 0.2, 0.1, 0.3, 0.1, 0.1],
              [0.5, 0.1, 0.1, 0.1, 0.2, 0.1, 0.2, 0.1, 0.1],
              [0.5, 0.4, 0.6, 0.8, 0.4, 0.1, 0.8, 1, 0.1]],
    input_ranges=[
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1),
        Range(min=0, max=1)
    ],
    output_ranges=[Range(min=0, max=1),
                   Range(min=0, max=1)])

result = bf.predict_from_array_keras(config, 'data/trained_network_cancer.h5')

print(result)
예제 #7
0
 def setUp(self):
     self.blackfox = BlackFox()  # noqa: E501
예제 #8
0
class TestDataSetApi(unittest.TestCase):
    """Blackfox unit test stubs"""
    def setUp(self):
        self.blackfox = BlackFox()  # noqa: E501

    def tearDown(self):
        pass

    def test_train_keras(self):
        input_layer = KerasLayerConfig(activation_function='Sigmoid',
                                       ranges=[
                                           Range(min=0, max=1),
                                           Range(min=0, max=1),
                                           Range(min=0, max=1),
                                           Range(min=0, max=1),
                                           Range(min=0, max=1),
                                           Range(min=0, max=1),
                                           Range(min=0, max=1),
                                           Range(min=0, max=1)
                                       ])

        output_layer = KerasLayerConfig(
            activation_function='Sigmoid',
            ranges=[Range(min=0, max=1),
                    Range(min=0, max=1)])

        hidden_layer_configs = [
            KerasHiddenLayerConfig(neuron_count=6,
                                   activation_function='Sigmoid'),
            KerasHiddenLayerConfig(neuron_count=6,
                                   activation_function='Sigmoid'),
            KerasHiddenLayerConfig(neuron_count=6,
                                   activation_function='Sigmoid'),
            KerasHiddenLayerConfig(neuron_count=6,
                                   activation_function='Sigmoid')
        ]

        config = KerasTrainingConfig(dropout=0,
                                     input_layer=input_layer,
                                     output_layer=output_layer,
                                     hidden_layer_configs=hidden_layer_configs,
                                     training_algorithm='Nadam',
                                     max_epoch=3000,
                                     cross_validation=False,
                                     training_ratio=0.7,
                                     random_seed=500)

        self.blackfox.train_keras(
            config,
            'C:/Users/Korisnik/Desktop/Cancer/ulazni_podaci_cancer.csv',
            'C:/Users/Korisnik/Desktop/Cancer/trained_network.onnx')

        pass

    def test_predict_file_keras(self):
        config = PredictionFileConfig(
            input_ranges=[
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1)
            ],
            output_ranges=[Range(min=0, max=1),
                           Range(min=0, max=1)])
        self.blackfox.predict_from_file_keras(
            config, 'C:/Users/Korisnik/Desktop/Cancer/trained_network.onnx',
            'C:/Users/Korisnik/Desktop/Cancer/ulazni_podaci_cancer.csv',
            'C:/Users/Korisnik/Desktop/Cancer/rezultati.csv')
        pass

    def test_predict_array_keras(self):
        config = PredictionArrayConfig(
            input_ranges=[
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1)
            ],
            output_ranges=[Range(min=0, max=1),
                           Range(min=0, max=1)],
            data_set=[[0.50, 1.00], [0.00, 0.90], [0.85, 0.50], [0.05, 0.70],
                      [0.12, 1.00], [0.74, 0.05], [0.66, 0.39], [0.28, 0.11]])
        results = self.blackfox.predict_from_array_keras(
            config, 'C:/Users/Korisnik/Desktop/Cancer/trained_network.onnx')
        print(results)

        pass

    def test_sha1(self):
        sha1 = self.blackfox.sha1(
            'C:/Users/Korisnik/Desktop/Cancer/ulazni_podaci_cancer.csv')
        print(sha1)
        pass

    def test_upload_data_set(self):
        id = self.blackfox.upload_data_set(
            'C:/Users/Korisnik/Desktop/Cancer/ulazni_podaci_cancer.csv')
        print(id)
        pass

    def test_download_data_set(self):
        self.blackfox.download_data_set(
            'f56e2c4fa71050ee4f55c6335947ad0b9bd47d85',
            'C:/Users/Korisnik/Desktop/Cancer/ulazni_podaci_cancer_download.csv'
        )
        pass

    def test_download_network(self):
        self.blackfox.download_network(
            'B9AB1F9129944A2B7D310B05B24C5CBA097A6A86',
            'C:/Users/Korisnik/Desktop/Cancer/network_download.csv')
        pass

    def test_optimize(self):
        engine_config = OptimizationEngineConfig(
            crossover_distribution_index=20,
            crossover_probability=0.9,
            mutation_distribution_index=20,
            mutation_probability=0.01,
            proc_timeout_miliseconds=200000,
            max_num_of_generations=50,
            number_of_constraints=0,
            population_size=100,
            number_of_eval_per_request=1)

        config = KerasOptimizationConfig(
            dropout=Range(min=0, max=25),
            dataset_id='f56e2c4fa71050ee4f55c6335947ad0b9bd47d85',
            batch_size=10,
            input_ranges=[
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1),
                Range(min=0, max=1)
            ],
            output_ranges=[Range(min=0, max=1),
                           Range(min=0, max=1)],
            hidden_layer_count_range=Range(min=1, max=15),
            neurons_per_layer=Range(min=1, max=10),
            training_algorithms=[
                "SGD", "RMSprop", "Adagrad", "Adadelta", "Adam", "Adamax",
                "Nadam"
            ],
            activation_functions=[
                "SoftMax", "Elu", "Selu", "SoftPlus", "SoftSign", "ReLu",
                "TanH", "Sigmoid", "HardSigmoid", "Linear"
            ],
            max_epoch=3000,
            cross_validation=False,
            training_ratio=0.7,
            random_seed=100,
            engine_config=engine_config)

        id = self.blackfox.optimize_keras(config)
        print(id)
        pass

    def test_optimization_stop(self):
        self.blackfox.stop_optimization_keras(
            'c2db8231-da65-4821-b628-b0c8be17db7d',
            'C:/Users/Korisnik/Desktop/Cancer/net.onnx')
        pass

    def test_optimization_status(self):
        res = self.blackfox.get_optimization_status_keras(
            'c2db8231-da65-4821-b628-b0c8be17db7d',
            'C:/Users/Korisnik/Desktop/Cancer/net.onnx')
        print(res)