Exemplo n.º 1
0
def model_evaluation(addr, label,type, threshold=.5, optimizer='adam',grid_search=False):
    model = load_model(addr)
    model.compile(optimizer=optimizer, loss='mse', metrics=['mse'])
    data = load_data(label=label, phase="search")
    val_preds = model.predict(data['x_val'], batch_size=32, verbose=0)
    test_preds = model.predict(data['x_test'], batch_size=32, verbose=0)
    if grid_search:
        thresholds = np.arange(.2, .8, .001)
        best_acc = float('-inf')
        if type=="dmtl":

            label_index = ['a', 'v', 'h'].index(label)
            for th in thresholds:
                acc = evaluation(val_preds[label_index] > th, data["y_val"],val_preds[label_index], verbos=False)
                if acc > best_acc:
                    best_acc = acc
                    threshold = th

            evaluation(val_preds[label_index] > threshold, data["y_val"],val_preds[label_index])
            evaluation(test_preds[label_index] > threshold, data["y_test"],test_preds[label_index])
        else:
            for th in thresholds:
                acc = evaluation(val_preds > th, y_valid, val_preds, verbos=False)
                if acc > best_acc:
                    print(th, acc)
                    best_acc = acc
                    threshold = th

            evaluation(val_preds > threshold, data["y_val"], val_preds)
            evaluation(test_preds > threshold, data["y_test"], test_preds)
        print("best threshold:",threshold)
 def prepare_data(self, phase, params):
     d_a = load_data(label="a", phase="search")
     d_h = load_data(label="h", phase="search")
     d_v = load_data(label="v", phase="search")
     y = {"y_val", "y_train", "y_test"}
     x = {"x_val", "x_train", "x_test", "x_val_128", "x_train_128"}
     data = {}
     for t in y:
         if phase == 1:
             data[t] = np.array([[d_a[t][i][0], d_v[t][i][0], d_h[t][i][0]]
                                 for i in range(len(d_a[t]))])
         else:
             data[t] = [d_a[t], d_v[t], d_h[t]]
     for t in x:
         data[t] = d_a[t]
     if params['agumentation']:
         data["x_val"] = ld.normalize(data["x_val"])
         data["x_test"] = ld.normalize(data["x_test"])
     elif params["scale"]:
         data["x_val"] = ld.normalize(data["x_val"])
         data["x_test"] = ld.normalize(data["x_test"])
         data["x_train"] = ld.normalize(data["x_train"])
     return data
Exemplo n.º 3
0
    def __init__(self, params, base_model, label, data=None):
        default_params = {
            "agumentation": False,
            "scale": False,
            "dense_activation": "relu",
            "regularizition": 0.0,
            "dropout": 0.0,
            "optimizer": "adam",
            "number_of_dense": 1,
            "balancer": "None",
            "batch_size": 32
        }
        default_params.update(params)
        Model = base_model
        params = default_params
        if data == None:
            data = load_data(label=label, phase="search")
        self.batch_size = params["batch_size"]
        if params['agumentation']:
            data["x_val"] = ld.normalize(data["x_val"])
            data["x_test"] = ld.normalize(data["x_test"])
        elif params["scale"]:
            data["x_val"] = ld.normalize(data["x_val"])
            data["x_test"] = ld.normalize(data["x_test"])
            data["x_train"] = ld.normalize(data["x_train"])
        regularization = not (params["regularizition"] == 0.0)

        dropout = not (params["dropout"] == 0.0)

        self.agumnetation = params["agumentation"]

        ############ Creating CNN ##############
        optimizer = params["optimizer"]
        inp = Input((64, 64, 1))
        con = concatenate([inp, inp, inp])
        model = Model(include_top=False, weights='imagenet', input_tensor=con)
        x = Flatten()(model.layers[-1].output)

        for i in range(params["number_of_dense"]):
            if regularization:
                x = Dense(params["nn"],
                          activation=params["dense_activation"],
                          kernel_regularizer=l2(params["regularizition"]))(x)
            else:
                x = Dense(params["nn"],
                          activation=params["dense_activation"])(x)
            if dropout:
                x = Dropout(params["dropout"])(x)
        x = Dense(1, activation="sigmoid", name="classification")(x)
        model = tf.keras.Model(model.input, x)
        model.compile(optimizer=optimizer,
                      metrics=["accuracy"],
                      loss=params["loss"])

        self.__model = model
        self.__data = data

        self.balancer = params["balancer"]
        self.__number_of_dense = params["number_of_dense"]
        self.details = [
            list(params.keys())[i] + ":" + str(list(params.values())[i])
            for i in range(len(params))
        ]