예제 #1
0
def mutation(model, env):

    # get the batteries from the model
    batteries = model.modelBatteries

    # get a random battery
    randomBatteries = (random.randint(0,
                                      len(batteries) - 1),
                       random.randint(0,
                                      len(batteries) - 1))
    batterySendingHouse = batteries[randomBatteries[0]]

    # set the upperbounds for the houses randomizer
    setUpperboundBattery1 = len(batterySendingHouse.houses)

    # get a random house
    randomHouseId = random.randint(0, (setUpperboundBattery1 - 1))

    # get the houses on the random places
    house1 = batterySendingHouse.houses[randomHouseId]

    # get battery receiving the house
    batteryReceivingHouse = batteries[randomBatteries[1]]

    # add the house to the other battery
    batteries[randomBatteries[1]].houses.append(house1)
    houses = batterySendingHouse.houses
    houses.pop(randomHouseId)
    batteries[randomBatteries[0]].houses = houses

    # return the model
    returnModel = Model(batteries)
    returnModel.calculateCosts(env.distanceTable)
    return returnModel
예제 #2
0
 def begin_training(self):
     algorithm = self.get_checkbox_value()
     if algorithm != 0:
         accuracy = None
         splits = self.ids.cv_checkbox.text
         cv_result = None
         saved_model_name = ''
         if algorithm == 4:
             if self.ids.cv_checkbox.text:
                 cv_result = cnn_cross_validation_train(
                     images_source_path=self.photos_dir,
                     facenet_model_path=os.path.join(
                         self.model_files_path, 'facenet_keras.h5'),
                     num_splits=int(splits))
             else:
                 model, encoder, accuracy = train_model(
                     images_source_path=self.photos_dir,
                     facenet_model_path=os.path.join(
                         self.model_files_path, 'facenet_keras.h5'),
                     valid_percentage=10)
                 dnn_model = Model(name=self.ids.name_input.text,
                                   algorithm=algorithm,
                                   encoder=encoder,
                                   train_set_dir=self.photos_dir,
                                   save_dir=self.model_files_path)
                 pickle.dump(
                     model,
                     open(os.path.join(dnn_model.save_path, 'model'), 'wb'))
                 saved_model_name = dnn_model.name
         else:
             if self.ids.cv_checkbox.text:
                 cv_result = cv2_cross_validation_train(
                     images_source_path=self.photos_dir,
                     algorithm=algorithm,
                     num_splits=int(splits))
             else:
                 model, accuracy = train(images_source_path=self.photos_dir,
                                         algorithm=algorithm)
                 cv2_model = Model(name=self.ids.name_input.text,
                                   algorithm=algorithm,
                                   encoder=None,
                                   train_set_dir=self.photos_dir,
                                   save_dir=self.model_files_path)
                 model.write(os.path.join(cv2_model.save_path, 'model'))
                 saved_model_name = cv2_model.name
         if accuracy is not None:
             str_accuracy = "{0:.0%}".format(accuracy)
             self.ids.result_text.text = 'Saved as: ' + saved_model_name + '. Validation accuracy: ' + str_accuracy
         else:
             self.ids.result_text.text = str(
                 splits) + '-fold cross validation accuracy: ' + cv_result
         self.ids.result_text.opacity = 1
         self.ids.train_button.text = 'Begin training'
         self.ids.train_button.disabled = False
         save_settings(algorithm)
예제 #3
0
def makeModel(node, env):

    # make model to calculate cost of node
    nodeModelBatteries = env.createModelBatteries()

    # append each item of node to modelbatteries
    for i in range(1, len(node)):
        nodeModelBatteries[node[i] - 1].houses.append(env.houses[i - 1])

    # create and return Model
    nodeModel = Model(nodeModelBatteries)
    nodeModel.calculateCosts(env.distanceTable)

    return nodeModel
예제 #4
0
def climbHillSwitchHouse(model):

    # get the batteries from the model
    batteries = model.modelBatteries

    # get a random battery
    randomBatteries = (random.randint(0,
                                      len(batteries) - 1),
                       random.randint(0,
                                      len(batteries) - 1))

    # set the upperbounds for the houses randomizer
    setUpperboundBattery1 = len(batteries[randomBatteries[0]].houses)
    setUpperboundBattery2 = len(batteries[randomBatteries[1]].houses)

    # get a random house
    randomHouses = (random.randint(0, (setUpperboundBattery1 - 1)),
                    random.randint(0, (setUpperboundBattery2 - 1)))

    # get the houses on the random places
    house1 = batteries[randomBatteries[0]].houses[randomHouses[0]]
    house2 = batteries[randomBatteries[1]].houses[randomHouses[1]]

    # switch the houses with each other
    batteries[randomBatteries[0]].houses[randomHouses[0]] = house2
    batteries[randomBatteries[1]].houses[randomHouses[1]] = house1

    # return the model
    returnModel = Model(batteries)
    return returnModel
예제 #5
0
def climbHillMoveHouse(model):

    # get the batteries from the model
    batteries = model.modelBatteries

    # get a random battery
    randomBatteries = (random.randint(0,
                                      len(batteries) - 1),
                       random.randint(0,
                                      len(batteries) - 1))
    batterySendingHouse = batteries[randomBatteries[0]]

    if allowSending(batterySendingHouse):
        # set the upperbounds for the houses randomizer
        setUpperboundBattery1 = len(batterySendingHouse.houses)

        # get a random house
        randomHouseId = random.randint(0, (setUpperboundBattery1 - 1))

        # get the houses on the random places
        house1 = batterySendingHouse.houses[randomHouseId]

        # get battery receiving the house
        batteryReceivingHouse = batteries[randomBatteries[1]]

        if allowPlacement(batteryReceivingHouse):
            # add the house to the other battery
            batteries[randomBatteries[1]].houses.append(house1)
            houses = batterySendingHouse.houses
            houses.pop(randomHouseId)
            batteries[randomBatteries[0]].houses = houses

    # return the model
    returnModel = Model(batteries)
    return returnModel
예제 #6
0
    def createModelBatteries(self):

        modelBatteries = []
        
        for i in range (0, len(self.batteries)):
            modelBatteries.append(Model.Battery(i+1))

        return modelBatteries
예제 #7
0
def randomize(env):

    modelBatteries = []

    # create the array of batteries with the id starting at 1
    for i in range(0, len(env.batteries)):
        maxCap = env.batteries[i].maxCapacity
        battery = Model.Battery(i + 1)
        modelBatteries.append(battery)

    # assign every house to a random battery of which the capacity is still sufficient
    listOfHouses = env.houses
    random.shuffle(listOfHouses)

    # assign random battery to every house
    for house in listOfHouses:
        batIndexes = []
        for i in range(0, len(env.batteries)):
            batIndexes.append(i)
        assignToRandomBattery(batIndexes, env.batteries, house, modelBatteries)

    # check if every house is connected
    totalHouses = 0
    for battery in modelBatteries:
        totalHouses += len(battery.houses)

    if totalHouses == len(env.houses):

        # assign all the values into a model
        model = Model(modelBatteries)

        # calculate the costs of this option
        model.calculateCosts(env.distanceTable)

        return model

    else:
        return False
예제 #8
0
def genomeToModel(genome, env):

    # create the array of batteries with the id starting at 1
    modelBatteries = []
    for i in range(0, len(env.batteries)):
        battery = Model.Battery(i + 1)
        modelBatteries.append(battery)

    # create child model
    newModel = Model(modelBatteries)

    for battery in newModel.modelBatteries:
        battery.setMaxCapacity(env)

    # fill list of houses belonging to battery in new model according to genome
    for gene in genome:
        for house in env.houses:
            if house.idHouse == (gene[0]):
                corHouse = house
                newModel.modelBatteries[gene[1] - 1].houses.append(corHouse)

    newModel.calculateCosts(env.distanceTable)
    return newModel
예제 #9
0
from keras.models import *
from classes.model import Model
from classes.transform import Transform
import os
import json
from client import Client
import sys


cloud_address = ''

correct = 0
wrong = 0

transform = Transform()
model = Model()

app = Flask(__name__)


@app.route("/configure", methods=['POST'])
def configure():
	fileName = 'my_model.h5'
	exported_model = request.files['file']
	exported_model.save(os.path.join(os.path.abspath(os.path.dirname(__file__)), fileName))
	model.load(fileName)

	data = json.loads(request.form['json'])
	transform.load(data['params'][0], data['params'][1])
	global cloud_address
	cloud_address = 'http://' + request.remote_addr + ':5000'
from flask import Flask, request
from classes.data_lake import DataLake
from classes.model import Model
from classes.policy import Policy
from client import Client
import sys

edge_address = ""

dl = DataLake()

model = Model()
model.train(dl.get_training_data())

policy = Policy()

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('[Usage] {} <edge ip:port>'.format(sys.argv[0]))
        exit(0)
    edge_address = sys.argv[1]
    Client.push_model(edge_address, model.export(), dl.get_transform_params())
    app = Flask(__name__)


@app.route("/update", methods=['POST'])
def update():
    content = request.get_json()
    data = {
        'TS1': content['data']['TS1'],
        'TS2': content['data']['TS2'],
예제 #11
0
from flask import Flask, request
from classes.data_lake import DataLake
from classes.model import Model
from classes.policy import Policy


correct = 0
wrong = 0

dl = DataLake()

model = Model()
model.train(dl.get_training_data())
model.test(dl.get_test_data())

policy = Policy()

app = Flask(__name__)


@app.route("/predict", methods=['POST'])
def predict():
	response = request.get_json()
	dict_data = {'TS1': response['TS1'], 'TS2': response['TS2'], 'TS3': response['TS3'], 'TS4': response['TS4'] }
	data = response['TS1'], response['TS2'], response['TS3'], response['TS4']
	outcome = response['outcome']
		
	dl.add_data(dict_data, outcome)
		
	prediction = model.predict(dl.transform(data))
	print("La predizione e' " + prediction + " e il valore reale e' " + outcome.split()[0])
예제 #12
0
# Predicts recognition on Live camera input
# with the help of predefined model, train
# dataset and harcascade classifier.

import cv2
from tensorflow.io.gfile import listdir

from classes.model import Model

# get class list
class_names = listdir("Datasets/train/")

# load model
model = Model(len(class_names))
model.load_model()

# predict live on camera input
cap = cv2.VideoCapture(0)
face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades +
                                        "haarcascade_frontalface_default.xml")

for i in range(100):
    ret, frame = cap.read()
    faces = face_classifier.detectMultiScale(frame, 1.3, 5)
    if faces is ():
        preds = model.predict_on_cv(frame)
    # Crop all faces found
    else:
        cropped_faces = []
        for (x, y, w, h) in faces:
            x = x - 10
예제 #13
0
def depthFirstBnB(env):

    # create the array of batteries for model
    modelBatteries = env.createModelBatteries()

    # initialize algorithm model
    depthFirstModel = Model(modelBatteries)

    # initiliaze upperbound at random cost, levels and solution
    model = runRandom(env)
    model.calculateCosts(env.distanceTable)
    upperBound = model.cost
    levels = len(env.houses)
    solution = []

    # create stack with root houseNode
    depthFirstStack = []
    depthFirstStack.append(np.array([0]))

    while len(depthFirstStack) > 0:

        # select last item and pop it
        node = depthFirstStack.pop()

        # create array to put best childnode on top of the stack
        tempCostNodes = []

        # create all children
        lenModelBatteries = len(modelBatteries)
        for i in range(0, lenModelBatteries):

            # create new houseNode
            newNode = np.append(node, modelBatteries[i].idBattery)

            # check for legit solution if all houses are connected
            if (len(newNode) - 1) == levels:
                if checkCapacity(newNode, env.batteries, modelBatteries,
                                 env.houses):
                    newModel = makeModel(newNode, env)

                    # set new upperbound for Branch-n-Bound and add solution to list
                    if newModel.cost < upperBound:
                        upperBound = newModel.cost
                        solution = newNode.tolist()

            # check if there isn't over capacity
            else:
                if checkCapacity(newNode, env.batteries, modelBatteries,
                                 env.houses):
                    newModel = makeModel(newNode, env)

                    # check if current node isn't already higher than upperbound
                    if newModel.cost < upperBound:
                        tempCostNodes.append([
                            env.distanceTable[len(newNode) - 1][newNode[-1]],
                            newNode
                        ])

                else:
                    continue

        # childnode with lowest cost on top of the stack
        tempCostNodes = sorted(tempCostNodes, key=itemgetter(0), reverse=True)
        tempCostNodes = [node[1] for node in tempCostNodes]
        depthFirstStack.extend(tempCostNodes)

    # make depth first model and update battery capacities
    lenSolution = len(solution)
    for i in range(1, lenSolution):
        depthFirstModel.modelBatteries[solution[i] - 1].houses.append(
            env.houses[i - 1])
        depthFirstModel.modelBatteries[solution[i] -
                                       1].curCapacity += env.houses[i - 1].cap

    depthFirstModel.calculateCosts(env.distanceTable)
    return depthFirstModel
class_names = listdir("Datasets/train/")


def get_train_ds():
    # returns train tf.data.Dataset
    train_ds = (image_dataset_from_directory(
        "Datasets/train/",
        shuffle=True,
        batch_size=BATCH_SIZE,
        image_size=IMG_SIZE).prefetch(AUTOTUNE).cache())
    return train_ds


def get_val_ds():
    # returns validation tf.data.Dataset
    val_ds = image_dataset_from_directory(
        "Datasets/val/",
        shuffle=True,
        batch_size=BATCH_SIZE,
        image_size=IMG_SIZE).prefetch(AUTOTUNE)
    return val_ds


train_ds = get_train_ds()
val_ds = get_val_ds()

model = Model(len(class_names))
model.create_model()
history = model.fit(train_ds, EPOCHS, val_ds)
model.save_model()
예제 #15
0
import pandas as pd

from classes.model import Model

# MODEL SELECTION
''' DESCRIPTION '''
''' This is the file where models are selected by doing a nested cross validation '''

PATH_TEXTS = "generated_datasets/13.03.2021_18.57/generated_dataset_1000.csv"
#PATH_TEXTS = "preprocessed_datasets/text/28.12.2020_18.48/final_text_dataset_38592.csv"
#PATH_TEXTS = "preprocessed_datasets/text/06.02.2021_21.00/final_text_dataset_7926.csv"

# data loading
dataset = pd.read_csv(PATH_TEXTS)

# code for generated datasets
del dataset["index"]

# extract sample
#dataset = dataset.head(10)

print("INPUT")
print(dataset.head(10))

# select the best models
m = Model()
m.select_model(dataset.head(1000), write=True)