Exemplo n.º 1
0
def learning(train_set, dataset_path, lbl_train, neural_nets, nn_for_learn, indexes, debug=False):
    if nn_for_learn[NET_12]:
        if debug:
            print("First network learning")
        for i in range(0, indexes[NET_12]):
            if debug:
                print(i)
            all_images, all_labels, coordinates = prepare_images.prepare(dataset_path + train_set[i].decode('utf8'),
                                                                         lbl_train[i], debug=debug)
            if debug:
                print("Image prepared")
            images = all_images[all_labels == 1]
            labels = np.ones(images.shape[0])
            neg_size = int(labels.shape[0] * NEGATIVE_MULTIPLIER)
            neg_indexes = np.random.choice(np.arange(all_images.shape[0] * all_images.shape[1]),
                                           neg_size, replace=False)
            neg_indexes = np.unravel_index(neg_indexes, (all_images.shape[0], all_images.shape[1]))
            neg_labels = all_labels[neg_indexes]
            neg_images = all_images[neg_indexes]
            images = np.concatenate((images, neg_images))
            labels = np.concatenate((labels, neg_labels))
            if debug:
                print("images.shape, labels.shape")
                print(images.shape, labels.shape)
            neural_nets[NET_12].learning(dataset=convert48to12(images), labels=labels, debug_print=debug, n_epochs=5)

    if nn_for_learn[NET_24]:
        if debug:
            print("Second network learning")
        for i in range(indexes[NET_24]):
            all_images, all_labels, coordinates = prepare_images.prepare(dataset_path + train_set[i].decode('utf8'),
                                                                         lbl_train[i], debug=debug)
            predicted_labels = neural_nets[NET_12].predict(all_images)
            images = all_images[predicted_labels == 1]
            labels = all_labels[predicted_labels == 1]
            nn_for_learn[NET_24].learning(dataset=convert48to24(images), labels=labels, debug_print=debug, n_epochs=10)

    if nn_for_learn[NET_48]:
        if debug:
            print("Second network learning")
        for i in range(indexes[NET_48]):
            all_images, all_labels, coordinates = prepare_images.prepare(dataset_path + train_set[i].decode('utf8'),
                                                                         lbl_train[i], debug=debug)
            predicted_labels = neural_nets[NET_12].predict(all_images)
            images = all_images[predicted_labels == 1]
            predicted_labels = neural_nets[NET_24].predict(images)
            images = all_images[predicted_labels == 1]
            labels = all_labels[predicted_labels == 1]
            nn_for_learn[NET_48].learning(dataset=convert48to24(images), labels=labels, debug_print=debug, n_epochs=10)
Exemplo n.º 2
0
 def test_prepare(self):
     dataset_path = "c:/_Hive/_diploma/LISA Traffic Sign Dataset/signDatabasePublicFramesOnly/vid0/frameAnnotations-vid_cmp2.avi_annotations/"
     annotation_path = dataset_path + 'frameAnnotations.csv'
     image_data = np.genfromtxt(annotation_path, delimiter=';', names=True, dtype=None)
     files = dict()
     for image in image_data:
         filepath = image['Filename']
         if filepath not in files:
             img = Image(filepath)
             img.add_sign(label=image['Annotation_tag'],
                          coordinates=(image['Upper_left_corner_X'], image['Upper_left_corner_Y'],
                                       image['Lower_right_corner_X'], image['Lower_right_corner_Y']))
             files[filepath] = img
         else:
             files[filepath].add_sign(label=image['Annotation_tag'],
                                      coordinates=(image['Upper_left_corner_X'], image['Upper_left_corner_Y'],
                                                   image['Lower_right_corner_X'], image['Lower_right_corner_Y']))
     images = np.array(list(files.keys()))
     images.sort()
     lbl = np.array([files.get(key).get_coordinates() for key in images])
     print(images[0].decode('utf8'))
     imgs, lbls = prepare_images.prepare(dataset_path + images[0].decode('utf8'), lbl[0])
     test_img = cv2.imread(dataset_path + images[0].decode('utf8'), cv2.IMREAD_UNCHANGED)
     # noinspection PyAugmentAssignment
     test_img = test_img / 255
     for j in range(lbls.shape[0]):
         # Rectangle = namedtuple('Rectangle', ['xmin', 'ymin', 'xmax', 'ymax'])
         (x1, y1, x2, y2) = (coords[j].xmin, coords[j].ymin, coords[j].xmax, coords[j].ymax)
         test_img_roi = np.array(
             [test_img[y1:y2, x1:x2, 0], test_img[y1:y2, x1:x2, 1], test_img[y1:y2, x1:x2, 2]])
         # if j > 325:
         #     prepare_images.show_roi([imgs[j], test_img_roi])
         self.assertTrue(np.allclose(imgs[j], test_img_roi), msg="In imgs[{}]".format(j))
Exemplo n.º 3
0
 def test_prepare(self):
     dataset_path = "c:/_Hive/_diploma/LISA Traffic Sign Dataset/signDatabasePublicFramesOnly/vid0/frameAnnotations-vid_cmp2.avi_annotations/"
     annotation_path = dataset_path + 'frameAnnotations.csv'
     image_data = np.genfromtxt(annotation_path, delimiter=';', names=True, dtype=None)
     files = dict()
     for image in image_data:
         filepath = image['Filename']
         if filepath not in files:
             img = Image(filepath)
             img.add_sign(label=image['Annotation_tag'],
                          coordinates=(image['Upper_left_corner_X'], image['Upper_left_corner_Y'],
                                       image['Lower_right_corner_X'], image['Lower_right_corner_Y']))
             files[filepath] = img
         else:
             files[filepath].add_sign(label=image['Annotation_tag'],
                                      coordinates=(image['Upper_left_corner_X'], image['Upper_left_corner_Y'],
                                                   image['Lower_right_corner_X'], image['Lower_right_corner_Y']))
     images = np.array(list(files.keys()))
     images.sort()
     lbl = np.array([files.get(key).get_coordinates() for key in images])
     print(images[0].decode('utf8'))
     imgs, lbls, coords = prepare_images.prepare(dataset_path + images[0].decode('utf8'), lbl[0])
     test_img = cv2.imread(dataset_path + images[0].decode('utf8'), cv2.IMREAD_UNCHANGED)
     # noinspection PyAugmentAssignment
     test_img = test_img / 255
     for j in range(lbls.shape[0]):
         # Rectangle = namedtuple('Rectangle', ['xmin', 'ymin', 'xmax', 'ymax'])
         (x1, y1, x2, y2) = (coords[j].xmin, coords[j].ymin, coords[j].xmax, coords[j].ymax)
         test_img_roi = np.array(
             [test_img[y1:y2, x1:x2, 0], test_img[y1:y2, x1:x2, 1], test_img[y1:y2, x1:x2, 2]])
         # if j > 325:
         #     prepare_images.show_roi([imgs[j], test_img_roi])
         self.assertTrue(np.allclose(imgs[j], test_img_roi), msg="In imgs[{}]".format(j))
Exemplo n.º 4
0
def testing_results(neural_nets, nn_params, nn_for_test, test_set_without_negatives, numbers_of_test_imgs=10):
    first_net, second_net = neural_nets[:2]
    first_net.load_params(nn_params[0])

    if nn_for_test[1]:
        second_net.load_params(nn_params[1])

    print("Testing...")
    test_img = np.array(list(test_set_without_negatives.keys())[:numbers_of_test_imgs])
    lbl_test = np.array([test_set_without_negatives.get(key).get_coordinates() for key in test_img])

    tp_all = np.zeros(numbers_of_test_imgs)
    tn_all = np.zeros(numbers_of_test_imgs)
    fp_all = np.zeros(numbers_of_test_imgs)
    fn_all = np.zeros(numbers_of_test_imgs)
    f1_score_all = np.zeros(numbers_of_test_imgs)
    tn_percent_all = np.zeros(numbers_of_test_imgs)

    for i in range(test_img.shape[0]):
        imgs, lbls, coords = prepare_images.prepare(DATASET_PATH + test_img[i].decode('utf8'), lbl_test[i])
        y_pred = np.zeros_like(lbls)
        for j in range(imgs.shape[0]):
            # TODO добавить nms в цепочку
            y_pred[j] = first_net.predict(nn.convert48to24(imgs[j]))
        if nn_for_test[1]:
            tmp = imgs[y_pred == 1]
            y_pred[y_pred == 1] = second_net.predict(tmp)

        tmp = lbls - y_pred

        tp = np.sum((y_pred == 1) & (lbls == 1))
        tn = np.sum((y_pred == 0) & (lbls == 0))
        fp = np.sum(tmp == -1)
        fn = np.sum(tmp == 1)
        f1_score = 2 * tp / (2 * tp + fn + fp)
        tp_all[i] = tp
        tn_all[i] = tn
        fp_all[i] = fp
        fn_all[i] = fn
        f1_score_all[i] = f1_score
        print(" f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}"
              .format(f1_score, tp, tn, fp, fn))
        tn_percent = tn / (tn + fp) * 100
        tn_percent_all[i] = tn_percent
        print("True negative percent from all negatives = {}".format(tn_percent))
        tmp = np.arange(lbls.shape[0] * lbls.shape[1]).reshape(lbls.shape)
        tmp = tmp[y_pred == 1]
        rects = [coords.get(key, None) for key in tmp]
        prepare_images.save_img_with_rectangles(DATASET_PATH, test_img[i].decode('utf8'), rects)
        # prepare_images.show_rectangles(dataset_path + test_img[i].decode('utf8'), rects, show_type='opencv')
        # rects = prepare_images.nms(rects, 0.3)
        # prepare_images.show_rectangles(dataset_path + test_img[i].decode('utf8'), rects, show_type='opencv')
    print("f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}"
          .format(f1_score_all.mean(), tp_all.mean(), tn_all.mean(), fp_all.mean(), fn_all.mean()))
    print("True negative percent from all negatives = {}".format(tn_percent_all.mean()))
    return tp_all, tn_all, fp_all, fn_all, f1_score_all, tn_percent_all
Exemplo n.º 5
0
def test_load_params(batch_size=45, random_state=123, init=False):
    train_sets = test_init()
    train_set_without_negatives = train_sets['train_set']
    net = nn.Network(batch_size=batch_size, random_state=random_state)
    test_img = np.array(list(train_set_without_negatives.keys()))
    test_img.sort()
    lbl_test = np.array([train_set_without_negatives.get(key).get_coordinates() for key in test_img])
    for i in range(test_img.shape[0]):
        imgs, lbls = prepare_images.prepare(DATASET_PATH + test_img[i].decode('utf8'), lbl_test[i])
        y_pred = net.predict_values(imgs)
        tmp = lbls - y_pred

        tp = np.sum((y_pred == 1) & (lbls == 1))
        tn = np.sum((y_pred == 0) & (lbls == 0))
        fp = np.sum(tmp == -1)
        fn = np.sum(tmp == 1)
        f1_score = 2 * tp / (2 * tp + fn + fp)
        print(" f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}"
              .format(f1_score, tp, tn, fp, fn))
Exemplo n.º 6
0
def learning_localization_networks(train_set,
                                   dataset_path,
                                   lbl_train,
                                   neural_nets,
                                   debug=False):
    if debug:
        print("First network learning")
    for i in range(0, neural_nets['net_12']['indexes']):
        if debug:
            print(i)
        all_images, all_labels = prepare_images.prepare(
            dataset_path + train_set[i].decode('utf8'),
            lbl_train[i],
            debug=debug)
        if debug:
            print("Image prepared")
        images = all_images[all_labels == 1]
        labels = np.ones(images.shape[0])
        neg_size = int(labels.shape[0] * NEGATIVE_MULTIPLIER)
        neg_indexes = np.random.choice(np.arange(all_images.shape[0] *
                                                 all_images.shape[1]),
                                       neg_size,
                                       replace=False)
        neg_indexes = np.unravel_index(
            neg_indexes, (all_images.shape[0], all_images.shape[1]))
        neg_labels = all_labels[neg_indexes]
        neg_images = all_images[neg_indexes]
        images = np.concatenate((images, neg_images))
        labels = np.concatenate((labels, neg_labels))
        if debug:
            print("images.shape, labels.shape")
            print(images.shape, labels.shape)
            neural_nets['net_12']['neural_net'].learning(
                dataset=convert48to12(images),
                labels=labels,
                debug_print=debug,
                n_epochs=5)
Exemplo n.º 7
0
def test_load_params(batch_size=45, random_state=123, init=False):
    train_sets = test_init()
    train_set_without_negatives = train_sets['train_set']
    net = nn.Network(batch_size=batch_size, random_state=random_state)
    test_img = np.array(list(train_set_without_negatives.keys()))
    test_img.sort()
    lbl_test = np.array([
        train_set_without_negatives.get(key).get_coordinates()
        for key in test_img
    ])
    for i in range(test_img.shape[0]):
        imgs, lbls = prepare_images.prepare(
            DATASET_PATH + test_img[i].decode('utf8'), lbl_test[i])
        y_pred = net.predict_values(imgs)
        tmp = lbls - y_pred

        tp = np.sum((y_pred == 1) & (lbls == 1))
        tn = np.sum((y_pred == 0) & (lbls == 0))
        fp = np.sum(tmp == -1)
        fn = np.sum(tmp == 1)
        f1_score = 2 * tp / (2 * tp + fn + fp)
        print(
            " f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}"
            .format(f1_score, tp, tn, fp, fn))
Exemplo n.º 8
0
def testing_results(neural_nets,
                    nn_params,
                    test_set_without_negatives,
                    numbers_of_test_imgs=10):
    # neural_nets[0].load_params(nn_params[0])

    print("Testing...")
    test_img = np.array(
        list(test_set_without_negatives.keys())[:numbers_of_test_imgs])
    lbl_test = np.array([
        test_set_without_negatives.get(key).get_coordinates()
        for key in test_img
    ])

    tp_all = np.zeros(numbers_of_test_imgs)
    tn_all = np.zeros(numbers_of_test_imgs)
    fp_all = np.zeros(numbers_of_test_imgs)
    fn_all = np.zeros(numbers_of_test_imgs)
    f1_score_all = np.zeros(numbers_of_test_imgs)
    tn_percent_all = np.zeros(numbers_of_test_imgs)

    for i in range(test_img.shape[0]):
        imgs, lbls = prepare_images.prepare(
            DATASET_PATH + test_img[i].decode('utf8'), lbl_test[i])
        y_pred = np.zeros_like(lbls)
        for j in range(imgs.shape[0]):
            # TODO добавить nms в цепочку
            y_pred[j] = neural_nets[0].predict(nn.convert48to12(imgs[j]))
        # if nn_for_test[1]:
        #     tmp = imgs[y_pred == 1]
        #     y_pred[y_pred == 1] = neural_nets[1].predict(nn.convert48to24(tmp))
        # if nn_for_test[2]:
        #     tmp = imgs[y_pred == 1]
        #     y_pred[y_pred == 1] = neural_nets[2].predict(tmp)

        tmp = lbls - y_pred

        tp = np.sum((y_pred == 1) & (lbls == 1))
        tn = np.sum((y_pred == 0) & (lbls == 0))
        fp = np.sum(tmp == -1)
        fn = np.sum(tmp == 1)
        f1_score = 2 * tp / (2 * tp + fn + fp)
        tp_all[i] = tp
        tn_all[i] = tn
        fp_all[i] = fp
        fn_all[i] = fn
        f1_score_all[i] = f1_score
        print(
            " f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}"
            .format(f1_score, tp, tn, fp, fn))
        tn_percent = tn / (tn + fp) * 100
        tn_percent_all[i] = tn_percent
        print(
            "True negative percent from all negatives = {}".format(tn_percent))
        tmp = np.arange(lbls.shape[0] * lbls.shape[1]).reshape(lbls.shape)
        tmp = tmp[y_pred == 1]
        # rects = [coords.get(key, None) for key in tmp]
        # prepare_images.save_img_with_rectangles(DATASET_PATH, test_img[i].decode('utf8'), rects)
        # prepare_images.show_rectangles(dataset_path + test_img[i].decode('utf8'), rects, show_type='opencv')
        # rects = prepare_images.nms(rects, 0.3)
        # prepare_images.show_rectangles(dataset_path + test_img[i].decode('utf8'), rects, show_type='opencv')
    print(
        "f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}"
        .format(f1_score_all.mean(), tp_all.mean(), tn_all.mean(),
                fp_all.mean(), fn_all.mean()))
    print("True negative percent from all negatives = {}".format(
        tn_percent_all.mean()))
    return tp_all, tn_all, fp_all, fn_all, f1_score_all, tn_percent_all