Example #1
0
def read_for_validation(p):
    """
    Read and preprocess an image without data augmentation (use for testing).
    """
    x0, y0, x1, y1 = p2bb[p]
    return read_cropped_image(expand_path(p),
                              x0,
                              y0,
                              x1,
                              y1,
                              False,
                              img_shape=(BOX_SIZE, BOX_SIZE, 3))
Example #2
0
def read_for_training(p):
    """
    Read and preprocess an image with data augmentation (random transform).
    """
    x0, y0, x1, y1 = p2bb[p]
    return read_cropped_image(expand_path(p),
                              x0,
                              y0,
                              x1,
                              y1,
                              True,
                              img_shape=(BOX_SIZE, BOX_SIZE, 3))
def process_train_images_with_branch_model(branch_model, fold):
    cache_path = CACHE_PATH + 'branch_res_train_fold_{}.pklz'.format(fold)
    if not os.path.isfile(cache_path):
        print('Go branch model train...')
        start_time = time.time()
        bboxes = get_boxes()
        CLASSES = get_classes_array()
        tagged = get_tagged_data()
        train_ids = []
        labels = dict()
        train_size = len(tagged)
        missed = 0
        # train_size = 10
        train_res = []
        i = 0
        for image_id in sorted(list(tagged.keys())):
            clss = tagged[image_id]
            # print('Go for: {}'.format(image_id))
            f = expand_path(image_id)
            lbl = CLASSES.index(clss)
            if image_id not in bboxes:
                print('Missed bbox for {}!'.format(image_id))
                missed += 1
                continue
            bb = bboxes[image_id]
            labels[image_id] = lbl
            train_ids.append(image_id)
            img = read_cropped_image(f,
                                     bb[0],
                                     bb[1],
                                     bb[2],
                                     bb[3],
                                     False,
                                     img_shape=(BOX_SIZE, BOX_SIZE, 3))
            img = np.expand_dims(img, axis=0)
            img = preprocess_image(img.astype(np.float32))
            preds = branch_model.predict(img)
            train_res.append(preds[0])
            i += 1

        train_res = np.array(train_res)
        train_ids = np.array(train_ids)

        print('Train preds: {}'.format(train_res.shape))
        print('Missed bboxes: {}'.format(missed))
        print('Read train time: {:.0f} sec'.format(time.time() - start_time))
        save_in_file((train_res, train_ids, labels), cache_path)
    else:
        print('Restore train from cache: {}'.format(cache_path))
        train_res, train_ids, labels = load_from_file(cache_path)

    return train_res, train_ids, labels
def process_tst_images_with_branch_model(branch_model, fold):
    cache_path = CACHE_PATH + 'branch_res_test_fold_{}.pklz'.format(fold)
    if not os.path.isfile(cache_path):
        print('Go branch model test...')
        start_time = time.time()
        bboxes = get_boxes()
        missed = 0
        test_df = pd.read_csv(INPUT_PATH + 'sample_submission.csv')
        test_ids = []
        test_size = len(test_df)
        # test_size = 10
        test_res = []
        i = 0
        for index, row in test_df.iterrows():
            # print('Go for: {}'.format(row['Image']))
            f = INPUT_PATH + 'test/' + row['Image']
            image_id = row['Image']
            if image_id in bboxes:
                bb = bboxes[image_id]
            else:
                print('Missed bbox for {}!'.format(image_id))
                missed += 1
                img = read_single_image(f)
                bb = (0, 0, img.shape[1], img.shape[0])
            img = read_cropped_image(f,
                                     bb[0],
                                     bb[1],
                                     bb[2],
                                     bb[3],
                                     False,
                                     img_shape=(BOX_SIZE, BOX_SIZE, 3))
            img = np.expand_dims(img, axis=0)
            img = preprocess_image(img.astype(np.float32))
            preds = branch_model.predict(img)
            test_res.append(preds[0])
            test_ids.append(row['Image'])
            i += 1
        test_res = np.array(test_res)
        test_ids = np.array(test_ids)

        print('Test preds: {}'.format(test_res.shape))
        print('Missed bboxes: {}'.format(missed))
        print('Read test time: {:.0f} sec'.format(time.time() - start_time))
        save_in_file((test_res, test_ids), cache_path)
    else:
        print('Restore test from cache: {}'.format(cache_path))
        test_res, test_ids = load_from_file(cache_path)

    return test_res, test_ids