def processModelByFile(mode, network, path_img_1, path_img_2):
    etichetta_predetta = ""
    print("Percorso file1", path_img_1)
    print("Percorso file2", path_img_2)
    resize = 100

    if ((network == "ResNet") and (mode == "cnn")):
        resize = 256

    print("Resize", resize)
    demo_obj = Demo(resize)
    demo_obj.read_normalize()
    dizionario = demo_obj.tranformFile(path_img_1, path_img_2)
    model = ""
    #-------------CLASSFIZICAZIONE------ A SINGOLA IMMAGINE---
    print("Inference by FILE classificazione singola")
    if (mode == "cnn"):
        if (network == "MNet"):
            model = torch.load("class_2_44.pth")
        elif (network == "ResNet"):
            model = torch.load("class_1_19.pth")

        demo_obj.test_demo_order_manual(dizionario, model)
        etichetta_predetta = {
            "class_1": demo_obj.preds_img_1,
            "class_2": demo_obj.preds_img_2,
            "pred_Pair": demo_obj.preds
        }
        return etichetta_predetta

#------------SNN------- CROSS ENTROPY--------------------
    elif (mode == "snnClass"):
        if (network == "ResNet"):
            model = torch.load("modello5_v5.pth")

        elif (network == "MNet"):
            model = torch.load("modello5_v7_17.pth")

        demo_obj.test_demo(dizionario, model)

#----------SNN---------SINGLE MARGINE-------------------
    elif (mode == "snnMetric"):
        soglia = ""
        if (network == "ResNet"):
            model = torch.load("modello6_v2_6.pth")
            soglia = 0.92

        dist = demo_obj.test_demo_single_margine(dizionario, model, soglia)

    etichetta_predetta = {"pred_Pair": demo_obj.preds}
    return etichetta_predetta
Example #2
0
#!/usr/bin/python

from Demo import Demo
from Swarm import Swarm
from Taxi import Taxi

swarm= Swarm(count=24)
demo= Demo(swarm)
while demo.is_open:
	demo.draw(Taxi(swarm))
	if not demo.paused:
		swarm.move(demo.step)

Example #3
0
import argparse
import json

from Demo import Demo

if __name__ == "__main__":
    aparser = argparse.ArgumentParser(description='Demo processing')
    aparser.add_argument('demofile')
    args = aparser.parse_args()
    demo = Demo(args.demofile, full=True)
    print (demo.directory)
    tojson = {
        "time":list(),
        "frame":list(),
        "origin_x":list(),
        "origin_y":list(),
        "origin_z":list(),
        "viewangle_x":list(),
        "viewangle_y":list(),
        "viewangle_z":list(),
    }
    # print( "\n".join(str(x) for x in demo.playback_entry.frameTimes()) )
    for frame in demo.playback_entry.frames[:]:
        if frame.type == 4:
            tojson["time"].append(frame.time[0])
            tojson["frame"].append(frame.frame)
            tojson["origin_x"].append(frame.origin[0][0])
            tojson["origin_y"].append(frame.origin[1][0])
            tojson["origin_z"].append(frame.origin[2][0])
            tojson["viewangle_x"].append(frame.viewangles[0][0])
            tojson["viewangle_y"].append(frame.viewangles[1][0])
Example #4
0
def processModelById(mode, network, idPair):
    etichetta_predetta = ""
    path_img_1 = ""
    path_img_2 = ""
    resize = 100
    if ((mode == "cnn") and (network == "ResNet")):
        resize = 256

    idPair = int(idPair)
    demo_obj = Demo(resize)
    demo_obj.readDataset()
    demo_obj.controlPair(idPair)
    demo_obj.read_normalize()
    dizionario = demo_obj.getitem(idPair)
    # dizionario contiene
    # {'image_1' : img_tensor_1, 'label_1':self.label_img1, 'image_2' : img_tensor_2, 'label_2': self.label_img2, 'label_lij':self.label_ij}

    #-------------CLASSFIZICAZIONE------ A SINGOLA IMMAGINE---
    if (mode == "cnn"):
        if (network == "MNet"):
            model = torch.load("class_2_44.pth")
            demo_obj.test_demo_order_manual(dizionario, model)

        elif (network == "ResNet"):
            model = torch.load("class_1_19.pth")

            demo_obj.test_demo_order_manual(dizionario, model)

        etichetta_predetta = {
            "pred_Pair": demo_obj.preds,
            "path_img_1": demo_obj.path_1,
            "path_img_2": demo_obj.path_2,
            "class_1": demo_obj.preds_img_1,
            "class_2": demo_obj.preds_img_2,
            "real": demo_obj.real
        }
        return etichetta_predetta
#------------SNN------- CROSS ENTROPY--------------------
    elif (mode == "snnClass"):
        if (network == "ResNet"):
            model = torch.load("modello5_v5.pth")
            demo_obj.test_demo(dizionario, model)

        elif (network == "MNet"):
            model = torch.load("modello5_v7_17.pth")
            demo_obj.test_demo(dizionario, model)

#----------SNN---------SINGLE MARGINE-------------------
    elif (mode == "snnMetric"):
        soglia = ""
        if (network == "ResNet"):
            model = torch.load("modello6_v2_6.pth")
            soglia = 0.92

            demo_obj.test_demo_single_margine(dizionario, model, soglia)

    etichetta_predetta = {
        "pred_Pair": demo_obj.preds,
        "path_img_1": demo_obj.path_1,
        "path_img_2": demo_obj.path_2,
        "unita1": demo_obj.label_img1,
        "unita2": demo_obj.label_img2,
        "real": demo_obj.real
    }
    return etichetta_predetta
Example #5
0
def main(argv):

    #crazione file "dataSet.json" se non esiste
    entry = {"nameDB": "Moneys"}
    controlFolder("Dataset")
    creteFileJson("Dataset\dataSetJson.json", entry)
    data_create = DataSetCreate()

    #name_id = data_create.name_classes_id()
    #list_all_images = data_create.list_all_images()
    #num_tot_files = data_create.num_total_file()

    parser = argparse.ArgumentParser(description="Dataset Money")

    parser.add_argument('--create',
                        help="datasetBase | datasetLarge | datasetPair")
    parser.add_argument(
        '--info', help="dataset | datasetBase | datasetLarge | datasetPair")
    #parser.add_argument('--training', help="1")
    parser.add_argument('--test',
                        help="Name of model [model5 | model6 ]",
                        type=str)

    parser.add_argument('--train',
                        help="Name of model [model5 | model6 ]",
                        type=str)
    parser.add_argument('--v', help="version", type=int)

    parser.add_argument('--progress',
                        help="Name of model [model5 | model6 ]",
                        type=str)
    parser.add_argument('--file', help="name file .pth", type=str)

    parser.add_argument('--e', help="epoche", type=int)
    parser.add_argument('--margine', help="dim of resize", type=int)

    parser.add_argument('--classification',
                        help="[ train | test | continue | demo ]",
                        type=str)

    parser.add_argument('--classtest', help="classTest")
    parser.add_argument('--demo', help="[ model5 | model6 ]")
    parser.add_argument('--pair', help=" insert id pair [0 - 13824]", type=int)
    parser.add_argument('--soglia', help="soglia", type=float)
    parser.add_argument('--margin', help="margin", type=float)

    parser.add_argument('--path', help="path of model '.pth'", type=str)

    parser.add_argument('--distribution',
                        help="distribuzione dei dati di train allenati")

    parser.add_argument('--pathModel',
                        help="percorso modello da inizializzare")

    parser.add_argument('--margin1', help="margine 1", type=float)

    parser.add_argument('--margin2', help="margine 2", type=float)

    parser.add_argument('--roc', help="roc")
    argomento = parser.parse_args()

    #-------- DISTRIBUTION------

    required_together_distrib = ('distribution', 'v')
    if argomento.distribution is not None:
        # args.model will be None if v is not provided
        if not all([getattr(argomento, x) for x in required_together_distrib]):
            raise RuntimeError("Cannot supply --distribution without --v ")
        else:

            #------  MODEL 6
            if argomento.distribution == "model6":
                print("DISTRIBUTION model ", argomento.distribution)
                #------       MODEL 6 v 2
                if argomento.v == 2:
                    print("version v2")
                    directory = "Model-6"
                    version = "2"
                    resize = 100
                    batch_size = 16
                    createFolder(directory)
                    createFolder(directory + "\\" + version)
                    dataSetPair = DataSetPairCreate(resize)
                    dataSetPair.controlNormalize()
                    pair_train = dataSetPair.pair_money_train

                    pair_money_train_loader = DataLoader(pair_train,
                                                         batch_size=batch_size,
                                                         num_workers=0,
                                                         shuffle=True)
                    path = directory + "\\" + version + "\\modello6_v2_6.pth"
                    gaussian_distribution_train_margine_single(
                        directory, version, pair_money_train_loader, resize,
                        batch_size, path)
                else:
                    exit(0)

#--------------------------- DEMO -------------------------------

    required_together_demo = ('demo', 'v', 'pair')
    if argomento.demo is not None:
        # args.model will be None if v is not provided
        if not all([getattr(argomento, x) for x in required_together_demo]):
            raise RuntimeError("Cannot supply --demo without --v --pair")
        else:

            #------  MODEL 5
            if argomento.demo == "model5":
                print("Demo model ", argomento.demo)

                if argomento.v == 5:
                    print("version v5")
                    print("model5 v5 ResNet siamese classification SGD")
                    directory = "Model-5\\"
                    path = 'modello5_v5.pth'
                    version = "5"
                    idPair = argomento.pair
                    # verifica l'id corrispondente alla coppia se è presente
                    resize = 100
                    demo_obj = Demo(directory, version, resize)
                    demo_obj.controlPair(idPair)
                    demo_obj.read_normalize()
                    dizionario = demo_obj.getitem(idPair)

                    siamese_test = torch.load(path)

                    demo_obj.test_demo(dizionario, siamese_test)
                    demo_obj.plottare()

                elif argomento.v == 7:
                    print("version v7")
                    print(
                        "DEMO model5 v7, Marek Net siamese classification SGD")
                    directory = "Model-5\\"
                    version = "7"
                    path = directory + version + "\\" + 'modello5_v7_17.pth'

                    idPair = argomento.pair
                    # verifica l'id corrispondente alla coppia se è presente
                    resize = 100
                    demo_obj = Demo(directory, version, resize)
                    demo_obj.controlPair(idPair)
                    demo_obj.read_normalize()
                    dizionario = demo_obj.getitem(idPair)

                    siamese_test = torch.load(path)

                    demo_obj.test_demo(dizionario, siamese_test)
                    demo_obj.plottare()

                else:
                    print("Versione del model5 non riconosciuta")
                    sys.stderr.write(
                        "Version not acknowledged, try --train model5 --v [ 5 | 7 ]\n"
                    )
                    exit(0)

            # --DEMO ---- MODEL 6
            elif argomento.demo == "model6":
                print("Demo model ", argomento.demo)

                #------DEMO---  MODEL 6 v 2
                if argomento.v == 2:
                    print("version v2")
                    print("model6 v2 ResNet, single margine=2.0, soglia=0.92")
                    directory = "Model-6\\"
                    version = "2"
                    path = directory + version + "\\" + 'modello6_v2_6.pth'

                    idPair = argomento.pair
                    resize = 100
                    demo_obj = Demo(directory, version, resize)
                    demo_obj.controlPair(idPair)
                    demo_obj.read_normalize()
                    dizionario = demo_obj.getitem(idPair)

                    siamese_test = torch.load(path)
                    soglia = 0.92
                    dist = demo_obj.test_demo_single_margine(
                        dizionario, siamese_test, soglia)
                    demo_obj.plottare(dist)

                elif argomento.v == 4:
                    print("version v2")
                    print("model6 v4 ResNet, double margine=0.7 e 1.3")
                    directory = "Model-6\\"
                    version = "4"
                    path = directory + version + "\\" + 'modello6_v4_51.pth'

                    idPair = argomento.pair
                    resize = 100
                    demo_obj = Demo(directory, version, resize)
                    demo_obj.controlPair(idPair)
                    demo_obj.read_normalize()
                    dizionario = demo_obj.getitem(idPair)

                    siamese_test = torch.load(path)
                    margin1 = 0.7
                    margin2 = 1.2
                    dist = demo_obj.test_demo_double_margine(
                        dizionario, siamese_test, margin1, margin2)
                    demo_obj.plottare(dist)

                else:
                    print("Versione del model6 non riconosciuta")
                    sys.stderr.write(
                        "Version not acknowledged, try --train model6 --v [ 2 | 4 ]\n"
                    )
                    exit(0)

            else:
                print("Modello non riconosciuto")
                sys.stderr.write(
                    "Model not acknowledged, try --train [ model5 | model6 ]\n"
                )
                exit(0)

# --------------------TRAIN------CLASSIFICAZIONE DEI DATI ---------------------

    elif argomento.classification == "train":
        required_together = ('classification', 'v', 'e')
        if not all([getattr(argomento, x) for x in required_together]):
            raise RuntimeError(
                "Cannot supply --classification train without --v --e")
        else:
            #-------MODEL MNET
            if argomento.v == 2:
                epochs = 20
                lr = 0.0001
                momentum = 0.9
                batch_size = 16
                resize = 100
                if argomento.e is not None:
                    epochs = argomento.e

                directory = "Classe"
                filename = "//class"
                version = "2"
                exp_name = 'class_2'
                name = 'ModelM'
                model = ModelM()

                classificazione(directory, filename, version, exp_name, name,
                                model, lr, epochs, momentum, batch_size,
                                resize)

            #--------MODEL RESNET
            elif argomento.v == 1:
                print("Resnet")
                lr = 0.0001
                momentum = 0.9
                batch_size = 16
                resize = 256
                if argomento.e is not None:
                    epochs = argomento.e
                directory = "Classe"
                filename = "//class"
                version = "1"
                exp_name = 'class_1'
                name = 'ResNet'

                model = resnet34(pretrained=True)
                resnet_copy = deepcopy(model)

                ### adattamento
                num_class = 5
                resnet_copy.fc = nn.Linear(512, num_class)
                resnet_copy.num_classes = num_class
                print(resnet_copy)

                classificazione(directory, filename, version, exp_name, name,
                                resnet_copy, lr, epochs, momentum, batch_size,
                                resize)

            else:
                print("Versione non riconosciuta")
                sys.stderr.write(
                    "Version not acknowledged, try --classification train --v [ 1 | 2 ]\n"
                )
                exit(0)

#-------------------TEST ------CLASSIFICAZIONE DEI DATI

#---- test su data set di base
    elif argomento.classification == "test":
        required_together = ('classification', 'v')
        if not all([getattr(argomento, x) for x in required_together]):
            raise RuntimeError(
                "Cannot supply --classification test without --v")
        else:
            if argomento.v == 2:
                print("MNet classification version 2")
                directory = "Classe"

                version = "2"
                batch_size = 16
                resize = 100
                name = 'ModelM'

                if argomento.pathModel is not None:
                    path_dict = argomento.pathModel
                else:
                    path_dict = 'Classe//2//class_2_44.pth'

                testing_classificazione(directory, path_dict, version, resize,
                                        batch_size)

            elif argomento.v == 1:
                print("Resnet classification version 1")

                directory = "Classe"

                version = "1"
                if argomento.pathModel is not None:
                    path_dict = argomento.pathModel
                else:
                    path_dict = 'Classe//1//class_1_19.pth'
                name = 'ResNet'
                batch_size = 4
                resize = 256
                testing_classificazione(directory, path_dict, version, resize,
                                        batch_size)

            else:
                print("Versione non riconosciuta")
                sys.stderr.write(
                    "Version not acknowledged, try --classification test --v [ 1 | 2 ]\n"
                )
                exit(0)

#---------------TEST   su datasetPair con classificazione Manuale

    elif argomento.classification == "testPair":
        required_together = ('classification', 'v')
        if not all([getattr(argomento, x) for x in required_together]):
            raise RuntimeError(
                "Cannot supply --classification testPair without --v")
        else:
            if argomento.v == 2:

                directory = "Classe"

                version = "2"
                batch_size = 16
                resize = 100
                name = 'ModelM'

                if argomento.pathModel is not None:
                    path_dict = argomento.pathModel
                else:
                    path_dict = 'Classe//2//class_2_44.pth'

                testing_classificazionePair(directory, path_dict, version,
                                            resize, batch_size)

            elif argomento.v == 1:
                print("Resnet classification version 1")

                directory = "Classe"

                version = "1"
                if argomento.pathModel is not None:
                    path_dict = argomento.pathModel
                else:
                    path_dict = 'Classe//1//class_1_19.pth'
                name = 'ResNet'
                batch_size = 4
                resize = 256
                testing_classificazionePair(directory, path_dict, version,
                                            resize, batch_size)

            else:
                print("Versione non riconosciuta")
                sys.stderr.write(
                    "Version not acknowledged, try --classification testPair --v [ 1 | 2 ]\n"
                )
                exit(0)

#-------------------CONTINUE ------CLASSIFICAZIONE DEI DATI

    elif argomento.classification == "continue":
        required_together = ('classification', 'v', 'e')
        if not all([getattr(argomento, x) for x in required_together]):
            raise RuntimeError(
                "Cannot supply --classification continue without --v --e")
        else:

            if argomento.v == 2:
                print("MNet classification continue version 2")
                directory = "Classe"
                exp_name = 'class_2'
                version = "2"
                lr = 0.0001
                momentum = 0.9
                batch_size = 16
                resize = 100

                name = 'ModelM'

                if argomento.pathModel is not None:
                    path_dict = argomento.pathModel
                else:
                    path_dict = 'Classe//2//class_2_19.pth'

                model = torch.load(path_dict)
                epoche_avanza = argomento.e
                continue_classificazione(directory, model, version, exp_name,
                                         name, lr, momentum, resize,
                                         batch_size, epoche_avanza)

            elif argomento.v == 1:
                print("Resnet classification continue version 1")

                directory = "Classe"
                version = "1"
                batch_size = 4
                resize = 256
                lr = 0.0001
                momentum = 0.9
                exp_name = 'class_1'
                name = 'ResNet'
                if argomento.pathModel is not None:
                    path_dict = argomento.pathModel
                else:
                    path_dict = 'Classe//1//class_1_19.pth'

                model = torch.load(path_dict)
                epoche_avanza = argomento.e

                continue_classificazione(directory, model, version, exp_name,
                                         name, lr, momentum, resize,
                                         batch_size, epoche_avanza)

            else:
                print("Versione non riconosciuta")
                sys.stderr.write(
                    "Version not acknowledged, try --classification continue --v [ 1 | 2 ]\n"
                )
                exit(0)

# --------------- DEMO ------------------CLASSIFICAZIONE MANUAL
    elif argomento.classification == "demo":

        required_together = ('classification', 'v', 'pair')
        if not all([getattr(argomento, x) for x in required_together]):
            raise RuntimeError(
                "Cannot supply --classification demo without --v --pair")
        else:

            #----- MODEL RESNET
            if argomento.v == 1:
                print("Classification Manual ResNet")
                if argomento.pathModel is not None:
                    path = argomento.pathModel
                else:
                    path = 'Classe\\1\\class_1_19.pth'
                directory = "Classe\\"
                version = "1"
                idPair = argomento.pair

                resize = 256
                demo_obj = Demo(directory, version, resize)
                demo_obj.controlPair(idPair)
                demo_obj.read_normalize()
                dizionario = demo_obj.getitem(idPair)

                class_test = torch.load(path)

                demo_obj.test_demo_order_manual(dizionario, class_test)
                demo_obj.plottare()

            #----- MODEL MNET
            elif argomento.v == 2:
                directory = "Classe\\"
                if argomento.pathModel is not None:
                    path = argomento.pathModel
                else:
                    path = 'Classe\\2\\class_2_44.pth'

                version = "2"
                idPair = argomento.pair
                # verifica l'id corrispondente alla coppia se è presente
                resize = 100
                demo_obj = Demo(directory, version, resize)
                demo_obj.controlPair(idPair)
                demo_obj.read_normalize()
                dizionario = demo_obj.getitem(idPair)

                class_test = torch.load(path)

                demo_obj.test_demo_order_manual(dizionario, class_test)
                demo_obj.plottare()
                return

            else:
                print("Versione non riconosciuta")
                sys.stderr.write(
                    "Version not acknowledged, try --classification demo --v [ 1 | 2 ]\n"
                )
                exit(0)

#------------ CREAZIONE DEI DATASET --create

    if argomento.create == "datasetBase":  # creazione dataset di Base
        data_create.create_Dataset_Base()

    elif argomento.create == "datasetLarge":  # creazione dataset di Base e datasetLarge
        data_create.create_Dataset_Large()

    elif argomento.create == "datasetPair":
        # controlla se è presente il dataset splittato
        controlFileCSV()
        dataSetPair = DataSetPairCreate()
        dataSetPair.controlNormalize()

#-------------   INFORMAZIONI SUI DATASET  --info

    data_set_info = argomento.info
    if (data_set_info == "dataset"):
        #oggetto DataSetCreate
        print("Dataset of base\n")
        #lettura da file Dataset\dataSetJson.json
        info = readFileDataset("Dataset\dataSetJson.json", "dataset")
        #info = data_create.info_classes()
        for i in info:
            print(i)
        num = lengthDataset("Dataset\dataSetJson.json", "dataset",
                            "num_images")
        print("Length Dataset of Base = ", num)
        print("\n")

    elif (data_set_info == "datasetBase"):
        print("Dataset Base\n")
        #info = data_create.info_datasetLarge()
        info = readFileDataset("Dataset\dataSetJson.json", "datasetBase")
        for i in info:
            print(i)
        num = lengthDataset("Dataset\dataSetJson.json", "datasetBase",
                            "num_sample")
        print("Length DatasetBase = ", num)

    elif (data_set_info == "datasetLarge"):
        print("Dataset Large\n")
        #info = data_create.info_datasetLarge()
        info = readFileDataset("Dataset\dataSetJson.json", "datasetLarge")
        for i in info:
            print(i)
        num = lengthDataset("Dataset\dataSetJson.json", "datasetLarge",
                            "num_sample")
        print("Length DatasetLarge = ", num)

    elif (data_set_info == "datasetPair"):

        print("DatasetPair\n")
        info = readFileDataset("Dataset\dataSetJson.json", "dataSetPair")
        for i in info:
            print(i)

#--------------FASE TRAINING OF MODEL 5 and 6 --train

    required_together = ('train', 'v', 'e')
    if argomento.train is not None:

        if not all([getattr(argomento, x) for x in required_together]):
            raise RuntimeError("Cannot supply --train without --v --e")
        else:

            #------  MODEL 5
            if argomento.train == "model5":
                if argomento.v == 7:
                    # siamese con trasfer-learning Mnet usata pe rla classigìficazione
                    # tolto il livello per la classificazione a 2 classi
                    # nuovo- inizializzazione class_2 epoche 44
                    lr = 0.0001
                    momentum = 0.9
                    resize = 100
                    epochs = 20
                    if argomento.e is not None:
                        epochs = argomento.e
                    batch_size = 4
                    directory = "Model-5"
                    filename = "//5_v7"
                    version = "7"
                    exp_name = 'modello5_v7'
                    name = 'MNet'

                    #inizializzazione del modello con parametri di MNet
                    path = "class_2.pth"
                    model = torch.load(path)
                    model_copy = deepcopy(model)
                    fully_connect = model_copy.fc
                    fully = list(fully_connect)
                    fully.pop()
                    model_copy.fc = nn.Sequential(*fully)
                    # adattamento
                    model_copy.fc2 = nn.Sequential(nn.Linear(512, 2))
                    print(model_copy)

                    train_model_class_v1(directory, filename, version,
                                         exp_name, name, model_copy, lr,
                                         epochs, momentum, batch_size, resize)

                elif argomento.v == 5:
                    # siamese con trasfer-learning Resnet usato per la classificazione
                    # e tolto l'ultimo livello
                    # aggiunto per prendere in ingresso la concatenazione degli output
                    # e aggiunto il livello per la classificazione a 2 classi
                    # la loss function è la CrossEntropy per la classificazione 0 e 1
                    lr = 0.0001
                    momentum = 0.9
                    resize = 100
                    epochs = 20
                    if argomento.e is not None:
                        epochs = argomento.e
                    batch_size = 4
                    decay = 0.0004
                    directory = "Model-5"
                    filename = "//5_v5"
                    version = "5"
                    exp_name = 'modello5_v5'
                    name = 'ResNet_Class'

                    # inizializzazione
                    model = torch.load("class_1.pth")
                    model_copy = deepcopy(model)

                    ### adattamento
                    num_class = 256
                    model_copy.fc = nn.Linear(512, num_class)
                    model_copy.num_classes = num_class
                    print(model_copy)

                    model_copy.fc2 = nn.Sequential(nn.Linear(512, 2))
                    print(model_copy)

                    train_model_class_v1(directory,
                                         filename,
                                         version,
                                         exp_name,
                                         name,
                                         model_copy,
                                         lr,
                                         epochs,
                                         momentum,
                                         batch_size,
                                         resize,
                                         decay=decay,
                                         modeLoss=None,
                                         dizionario_array=None)

                else:
                    print("Versione non riconosciuta")
                    sys.stderr.write(
                        "Version not acknowledged, try --train model5 --v [ 5 | 7 ]\n"
                    )
                    exit(0)

                #-----train MODEL6  siamese
            elif argomento.train == "model6":
                #----- Resnet - single margine
                if argomento.v == 2:
                    # siamese con trasfer-learning Resnet usato per la classificazione
                    # e tolto il livello per la classivicazione a 5 classi
                    # e inserito quello da 256
                    #
                    # la loss function è la Contrastive loss , margine

                    decay = 0.0004
                    lr = 0.0001
                    momentum = 0.9
                    resize = 100
                    epochs = 20
                    if argomento.e is not None:
                        epochs = argomento.e
                    batch_size = 4
                    directory = "Model-6"
                    filename = "//6_v2"
                    version = "2"
                    exp_name = 'modello6_v2'
                    name = 'RestNet_Margine'

                    # Usato per la classificazione a 5 classi, fine-tuning Resnet34
                    model = torch.load("class_1.pth")
                    model_copy = deepcopy(model)

                    ### adattamento
                    num_class = 256
                    model_copy.fc = nn.Linear(512, num_class)
                    model_copy.num_classes = num_class
                    print(model_copy)

                    train_model_margine(directory,
                                        filename,
                                        version,
                                        exp_name,
                                        name,
                                        model_copy,
                                        lr,
                                        epochs,
                                        momentum,
                                        batch_size,
                                        resize,
                                        decay=decay,
                                        margin=2.0,
                                        soglia=1.0,
                                        modeLoss="single")

                elif argomento.v == 4:
                    # siamese con trasfer-learning Resnet usato per la classificazione
                    # e tolto il livello per la classivicazione a 5 classi
                    # e inserito quello da 256
                    # la loss function è la Contrastive loss , double margine

                    decay = 0.004
                    lr = 0.0001
                    momentum = 0.9
                    resize = 100
                    epochs = 20
                    if argomento.e is not None:
                        epochs = argomento.e
                    batch_size = 4
                    directory = "Model-6"
                    filename = "//6_v4"
                    version = "4"
                    exp_name = 'modello6_v4'
                    name = 'RestNet_Margine_Double'

                    model = torch.load(
                        "class_1.pth"
                    )  # Usato per la classificazione fine-tuning Resnet
                    model_copy = deepcopy(model)
                    # serve per rimuovere l'ultimo livello"
                    ### adattamento
                    num_class = 256
                    model_copy.fc = nn.Linear(512, num_class)
                    model_copy.num_classes = num_class
                    print(model_copy)
                    margin1 = 0.7
                    margin2 = 1.2
                    if argomento.margin1 is not None:
                        margin1 = argomento.margin1

                    if argomento.margin2 is not None:
                        margin2 = argomento.margin2

                    train_model_margine_double(directory,
                                               filename,
                                               version,
                                               exp_name,
                                               name,
                                               model_copy,
                                               lr,
                                               epochs,
                                               momentum,
                                               batch_size,
                                               resize,
                                               decay=decay,
                                               margin1=margin1,
                                               margin2=margin2,
                                               modeLoss="double")

                else:
                    print("Versione non riconosciuta")
                    sys.stderr.write(
                        "Version not acknowledged, try --train model6 --v [ 2 | 4 ]\n"
                    )
                    exit(0)

            else:
                print("Modello non riconosciuto ")
                sys.stderr.write(
                    "Model not acknowledged, try --train [model5 | model6 ]\n")
                exit(0)

#--------------FASE  TESTING --test
    required_together_test = ('test', 'v')

    if argomento.test is not None:
        if not all([getattr(argomento, x) for x in required_together_test]):
            raise RuntimeError("Cannot supply --test without --v")
        else:
            #------ test MODEL 5
            if argomento.test == "model5":

                # ----------model 5 v 5 ---- ResNet
                if argomento.v == 5:
                    print("version", argomento.v)

                    print("model 5 v5 ResNet classi siamese con lr =0.0001 ")
                    directory = "Model-5\\"
                    path = 'modello5_v5.pth'
                    version = "5"

                    batch_size = 16
                    resize = 100

                    test_model_class(directory,
                                     path,
                                     version,
                                     resize,
                                     batch_size,
                                     margine=None)

                elif argomento.v == 7:
                    # ----------model 5 v 7 ---- MNet
                    print("version", argomento.v)

                    print("model 5 v7 MNet classi siamese con lr =0.0001")
                    directory = "Model-5\\"

                    path = 'Model-5\\7\\modello5_v7_17.pth'
                    version = "7"

                    batch_size = 16
                    resize = 100

                    test_model_class(directory,
                                     path,
                                     version,
                                     resize,
                                     batch_size,
                                     margine=None)

                else:
                    print("Versione non riconosciuta")
                    sys.stderr.write(
                        "Version not acknowledged, try --test model5 --v [ 5 | 7 ]\n"
                    )
                    exit(0)

                #----------test  MODEL 6
            elif argomento.test == "model6":

                #------ model test 6 v 2
                if argomento.v == 2:
                    print("version", argomento.v)
                    print(
                        "model6 v2 Test ResNet siamese margine one 2.0 soglia 0.92"
                    )
                    directory = "Model-6\\"
                    path = directory + "2\\" + 'modello6_v2_6.pth'
                    version = "2"
                    soglia = 0.92
                    if argomento.soglia is not None:
                        soglia = argomento.soglia
                    batch_size = 16
                    resize = 100
                    print("Soglia", soglia)
                    test_model_margine(directory,
                                       path,
                                       version,
                                       resize,
                                       batch_size,
                                       margine=soglia)

                    #-------- model test 6 v 4
                elif argomento.v == 4:
                    print("version", argomento.v)
                    print(
                        "model6 v 4 Test ResNet siamese margine double 0.7 e 1.2, numero epoche 52 "
                    )
                    directory = "Model-6\\"
                    path = directory + "4\\" + 'modello6_v4_51.pth'
                    version = "4"
                    margin1 = 0.7
                    margin2 = 1.2
                    batch_size = 16
                    resize = 100

                    test_model_margine_double(directory, path, version, resize,
                                              batch_size, margin1, margin2)
                else:
                    print("Versione non riconosciuta")
                    sys.stderr.write(
                        "Version not acknowledged, try --test model6 --v [ 2 | 4 ]\n"
                    )
                    exit(0)

            else:
                print("Modello non riconosciuto")
                sys.stderr.write(
                    "Model not acknowledged, try --test [model5 | model6 ]\n")
                exit(0)

# ---------------------PERFORMANCE

    if argomento.roc is not None:
        print(argomento.roc)
        print(argomento.v)

        #  PERFORMANCE MODEL 6 V 2
        if argomento.roc == "model6":
            # model test 6 v 2
            if argomento.v == 2:
                print("version", argomento.v)
                print(
                    "model6 v2 Test ResNet siamese margine one 2.0 soglia 1.0")
                directory = "Model-6"
                version = "2"
                path = directory + "\\" + version + '\\modello6_v2_6.pth'
                version = "2"

                batch_size = 16
                resize = 100

                test_model_performance(directory, path, version, resize,
                                       batch_size)


#--------------FASE  CONTINUE --continue
    required_together_continue = ('progress', 'v', 'e')
    # args.model will be None if v is not provided
    if argomento.progress is not None:
        if not all([getattr(argomento, x)
                    for x in required_together_continue]):
            raise RuntimeError("Cannot supply --progress without --v --e")
        else:

            #----- MODEL 6
            if argomento.progress == "model6":
                print("model", argomento.progress)

                # model continue 6 v 2
                if argomento.v == 2:
                    # siamese con trasfer-learning Resnet usato per la classificazione
                    # e tolto il livello per la classivicazione a 5 classi
                    # e inserito quello da 256
                    # la loss function è la Contrastive loss , margine single

                    decay = 0.0004
                    lr = 0.0001
                    momentum = 0.9
                    resize = 100

                    batch_size = 4
                    directory = "Model-6"
                    filename = "//6_v2"
                    version = "2"
                    exp_name = 'modello6_v2'
                    name = 'RestNet_Margine_Single'
                    if argomento.pathModel is not None:
                        path = argomento.pathModel
                    else:
                        path = 'Model-6//2//modello6_v2_13.pth'
                    model = torch.load(path)

                    epoche_avanza = argomento.e
                    continue_model_margine_single(directory,
                                                  filename,
                                                  version,
                                                  exp_name,
                                                  name,
                                                  model,
                                                  lr,
                                                  epoche_avanza,
                                                  momentum,
                                                  batch_size,
                                                  resize,
                                                  decay=decay,
                                                  margin1=2.0,
                                                  soglia=0.92,
                                                  modeLoss="single")

                # model continue 6 v 4
                elif argomento.v == 4:
                    # siamese con trasfer-learning Resnet usato per la classificazione
                    # e tolto il livello per la classivicazione a 5 classi
                    # e inserito quello da 256
                    # la loss function è la Contrastive loss , margine double

                    decay = 0.004
                    lr = 0.0001
                    momentum = 0.9
                    resize = 100

                    batch_size = 4
                    directory = "Model-6"
                    filename = "//6_v4"
                    version = "4"
                    exp_name = 'modello6_v4'
                    name = 'RestNet_Margine_Double'
                    if argomento.pathModel is not None:
                        path = argomento.pathModel
                    else:
                        path = 'Model-6//4//modello6_v4_56.pth'
                    model = torch.load(path)

                    margin1 = 0.7
                    margin2 = 1.2
                    if argomento.margin1 is not None:
                        margin1 = argomento.margin1

                    if argomento.margin2 is not None:
                        margin2 = argomento.margin2
                    epoche_avanza = argomento.e
                    continue_model_margine_double(directory,
                                                  filename,
                                                  version,
                                                  exp_name,
                                                  name,
                                                  model,
                                                  lr,
                                                  epoche_avanza,
                                                  momentum,
                                                  batch_size,
                                                  resize,
                                                  decay=decay,
                                                  margin1=margin1,
                                                  margin2=margin2,
                                                  modeLoss="double")

                # model continue 6 v 6
                elif argomento.v == 6:

                    decay = 0.02
                    lr = 0.001
                    momentum = 0.9
                    resize = 100

                    batch_size = 4
                    directory = "Model-6"
                    filename = "//6_v6"
                    version = "6"
                    exp_name = 'modello6_v6'
                    name = 'RestNet_Margine_Double'
                    if argomento.pathModel is not None:
                        path = argomento.pathModel
                    else:
                        path = 'Model-6//4//modello6_v4_51.pth'
                    model = torch.load(path)

                    margin1 = 0.7
                    margin2 = 1.2
                    if argomento.margin1 is not None:
                        margin1 = argomento.margin1

                    if argomento.margin2 is not None:
                        margin2 = argomento.margin2
                    epoche_avanza = argomento.e
                    continue_model_margine_double(directory,
                                                  filename,
                                                  version,
                                                  exp_name,
                                                  name,
                                                  model,
                                                  lr,
                                                  epoche_avanza,
                                                  momentum,
                                                  batch_size,
                                                  resize,
                                                  decay=decay,
                                                  margin1=margin1,
                                                  margin2=margin2,
                                                  modeLoss="double")
                else:
                    print("Versione non riconosciuta")
                    sys.stderr.write(
                        "Version not acknowledged, try --progress model6 --v [ 2 | 4 | 6 ]\n"
                    )
                    exit(0)
            else:
                print("Modello non riconosciuto ")
                sys.stderr.write(
                    "Model not acknowledged, try --progress [ model6 ]\n")
                exit(0)
Example #6
0
#!E:\Study\Python\Appium\PS\Demo.py
# -*- coding: UTF-8 -*-
from Demo import Demo

demo = Demo()
demo.run()
Example #7
0
from pyposey.assembly_graph.Part_Library import Part_Library
from pyposey.assembly_graph.Assembly_Graph import Assembly_Graph
from l33tC4D.gui.Gui import Gui
from Demo import Demo

# load part library
part_library = Part_Library()

# set up assembly graph
sensor_queue = Queue()
event_queue = Queue()

sensor_demon = Sensor_Demon( sensor_queue, serial_port="/dev/ttyUSB0" )
assembly_demon = Assembly_Demon( sensor_queue, event_queue )
assembly_graph = Assembly_Graph( event_queue=event_queue,
                                 part_library=part_library,
                                 orient=True )

# start demon threads
sensor_demon.start()
assembly_demon.start()
assembly_graph.start()

# start gtk gui
gui = Gui()
gui.start()

# start game
game = Demo( gui, assembly_graph, level=0 )
game.start()
Example #8
0
def demo(output_frames=None):
    """
    See the result of a trained model
    """
    player = Player(learning=False, load_model=True)
    return Demo(player=player, output_frames=output_frames)