示例#1
0
def detect_object(input_images, object_model):
    fv = vivid.ImageSource(imlist=input_images)
    cs = vivid.ConvertedSource(fv,
                               target_type=vivid.cv.CV_32FC3,
                               scale=1.0 / 255.0)

    gv = vivid.GreySource(cs)

    fs = PADFeatureSource(gv, pom.fm)

    for i in range(len(input_images)):
        cell = fs.get_features(i)

        cell_mags = cell.sum(axis=2)

        cell_mat = cell.view()

        cell_mat.shape = (cell.shape[0] * cell.shape[1], cell.shape[2])

        cell_dm = vivid.DeviceMatrix(cell_mat)

        cell_scores = object_model.classify(cell_dm)

        import pdb
        pdb.set_trace()
示例#2
0
from opt import options

config_opt = ConfigOpt(options.config_source)

target_file = config_opt.patch_file[:-3] + 'nonbias.npy'

print("Will write to: {0}".format(target_file))

try:
    os.makedirs(target_path)
except:
    pass

fv = vivid.ImageSource(imlist=config_opt.image_sets_fp[options.set_type])
cs = vivid.ConvertedSource(fv,
                           target_type=vivid.cv.CV_32FC3,
                           scale=1.0 / 255.0)
gv = vivid.GreySource(cs)

patches = np.zeros(
    (config_opt.num_patches,
     config_opt.feature.cell.patch_size * config_opt.feature.cell.patch_size),
    dtype='float32')

patch_pixel_map_x, patch_pixel_map_y = np.meshgrid(
    np.arange(-(config_opt.feature.cell.patch_size / 2),
              (config_opt.feature.cell.patch_size / 2) + 1),
    np.arange(-(config_opt.feature.cell.patch_size / 2),
              (config_opt.feature.cell.patch_size / 2) + 1))

total_object_area = 0.0
示例#3
0
def detect_and_write(input_images, output_images):
    DETECTION_THRESHOLD = -.5

    config_opt = ConfigOpt(options.config_source)

    if options.bootstrap:
        svm_model_file = os.path.join(config_opt.svm_model_path, "modelb.svm")
    else:
        svm_model_file = os.path.join(config_opt.svm_model_path, "model1.svm")

    lm = read_liblinear_model(svm_model_file)

    w = lm.w[:-1]
    b = lm.w[-1] * lm.bias

    fv = vivid.ImageSource(imlist=input_images)
    cs = vivid.ConvertedSource(fv,
                               target_type=vivid.cv.CV_32FC3,
                               scale=1.0 / 255.0)
    gv = vivid.GreySource(cs)

    clusters = np.load(config_opt.dictionary_file).reshape((
        config_opt.feature.cell.dictionary_size,
        config_opt.feature.cell.patch_size,
        config_opt.feature.cell.patch_size))

    fs = FeatureSource(gv, config_opt.feature, clusters)

    all_scores = []

    for fi, input_image, output_image in zip(
        xrange(len(input_images)), input_images, output_images):

        print('Image: {0}'.format(input_image))

        fs.init_frame(fi)

        frame = vivid.cvmat2array(cs.get_frame(fi))[:, :, ::-1]

        while True:
            try:
                locs, scale = fs.init_next_scale()

                print("Processing scale: {0:.2f}\t".format(scale))

                num_y, num_x = locs[0].shape

                scale_scores = np.empty((num_y, num_x), dtype='float32')

                for yind in range(0, num_y, 10):
                    ymin = yind
                    ymax = min(num_y, yind + 10)

                    feas = fs.get_features_from_scale(
                        ymin=yind, ymax=ymax,
                        xmin=0, xmax=num_x)

                    scale_scores[ymin:ymax, :] = (feas *
                        w[np.newaxis, np.newaxis, :]).sum(axis=2) + b

                detections = scale_scores >= DETECTION_THRESHOLD

                for yi, xi, detection_score in zip(
                        locs[0][detections],
                        locs[1][detections],
                        scale_scores[detections]):

                    if detection_score >= 0:
                        box_color = [1.0, 0, 0]
                    else:
                        box_color = [1.0, 1.0, 0]

                    print("Detection at: y: {0}, x: {1}, s: {2:.2f}".format(
                            yi, xi, scale))

                    frame = draw_bounding_box(frame, np.array([yi, xi, scale]),
                                              text="%.3f" % detection_score,
                                              color=box_color)
            except EndOfScales:
                break

        imsave(output_image, frame)
def process_subrange(n_data_splits, data_split_ind):
    config_opts = [ConfigOpt(cs) for cs in config_sources]

    num_images = len(
        config_opts[0].non_annotation_image_inds[options.set_type])

    split_inds = np.linspace(0, num_images, num=n_data_splits + 1)

    data_split_start = int(split_inds[data_split_ind])
    data_split_end = int(split_inds[data_split_ind + 1])

    num_split_images = data_split_end - data_split_start

    non_boot_samples_per_im = config_opts[0].training_samples_per_im

    feature_dims = np.array([cos.feature.feature_dim for cos in config_opts])
    feature_dims_cs = np.cumsum(feature_dims)

    false_positives = np.empty((0, feature_dims.sum()), dtype='float32')

    total_non_boot_samples = num_split_images * non_boot_samples_per_im

    feature_str = ''
    for cos in config_opts:
        feature_str += cos.feature_str

    if options.bootstrap > 0:
        svm_model_file = os.path.join(config_opts[0].svm_model_path,
                                      "model1.%ssvm" % feature_str)

        lm = read_liblinear_model(svm_model_file)
        w = lm.w[:-1]
        b = lm.w[-1] * lm.bias

    if options.bootstrap > 1:
        svm_model_file = os.path.join(config_opts[0].svm_model_path,
                                      "modelb.%ssvm" % feature_str)

        lmb = read_liblinear_model(svm_model_file)

        wb = lmb.w[:-1]
        bb = lmb.w[-1] * lbm.bias

    fv = vivid.ImageSource(
        imlist=config_opts[0].image_sets_fp[options.set_type])
    cs = vivid.ConvertedSource(fv,
                               target_type=vivid.cv.CV_32FC3,
                               scale=1.0 / 255.0)
    gv = vivid.GreySource(cs)

    feature_sources = []

    for cos in config_opts:
        if cos.feature.fea_type == 'flex':
            clusters = np.load(cos.dictionary_file).reshape(
                (cos.feature.cell.dictionary_size, cos.feature.cell.patch_size,
                 cos.feature.cell.patch_size))

            feature_sources.append(FeatureSource(gv, cos.feature, clusters))
        elif cos.feature.fea_type == 'lbp':
            feature_sources.append(FeatureSource(gv, cos.feature))
        elif cos.feature.fea_type == 'uniform':
            feature_sources.append(FeatureSource(gv, cos.feature))
        else:
            raise ValueError("Unknown feature type")

    counter = 0

    all_features = np.zeros((total_non_boot_samples, feature_dims.sum()),
                            dtype='f')

    total_fp = 0
    collected = 0
    for i, image_ind in enumerate(config_opts[0].non_annotation_image_inds[
            options.set_type][data_split_start:data_split_end]):

        print("{0} - {1}/{2}: {3}".format(
            data_split_ind, i, num_split_images,
            config_opts[0].image_sets_fp[options.set_type][image_ind]))

        for fea_i, fs in enumerate(feature_sources):
            fs.init_frame(image_ind)

            n_scales = len(fs.scales)

            if n_scales == 0:
                continue

        image_samples = np.zeros(
            (n_scales * non_boot_samples_per_im, feature_dims.sum()),
            dtype='float32')

        fea_sum = 0
        scale_sample_ind = 0

        for si in range(n_scales):
            for fs in feature_sources:
                locs, scale = fs.init_scale(si)

            num_y, num_x = locs[0].shape
            n_total = locs[0].size

            random_ind = np.random.permutation(
                n_total)[:non_boot_samples_per_im]

            scale_random_ind_x, scale_random_ind_y = np.meshgrid(
                range(num_x), range(num_y))

            scale_random_ind_x = scale_random_ind_x.flat[random_ind]
            scale_random_ind_y = scale_random_ind_y.flat[random_ind]

            for yind in range(0, num_y, 10):
                ymin = yind
                ymax = min(num_y, yind + 10)

                random_ind_part = np.logical_and(scale_random_ind_y >= ymin,
                                                 scale_random_ind_y < ymax)

                n_random_ind_part = random_ind_part.sum()

                if n_random_ind_part == 0 and options.bootstrap < 1:
                    continue

                scale_random_ind_x_part = scale_random_ind_x[random_ind_part]
                scale_random_ind_y_part = scale_random_ind_y[random_ind_part]

                feas = [
                    fs.get_features_from_scale(ymin=ymin,
                                               ymax=ymax,
                                               xmin=0,
                                               xmax=num_x)
                    for fs in feature_sources
                ]

                feas = np.concatenate(feas, axis=2)

                image_samples[scale_sample_ind:scale_sample_ind +
                              n_random_ind_part, :] = feas[
                                  scale_random_ind_y_part - ymin,
                                  scale_random_ind_x_part, :].copy()

                scale_sample_ind += n_random_ind_part

                if options.bootstrap > 0:
                    if options.tfidf == 1:
                        scores = ((feas * tfidf_w[np.newaxis, np.newaxis, :] *
                                   w[np.newaxis, np.newaxis, :]).sum(axis=2) +
                                  b)
                    else:
                        scores = (
                            (feas * w[np.newaxis, np.newaxis, :]).sum(axis=2) +
                            b)

                    false_fp = np.nonzero(scores > FALSE_POS_THRESHOLD)

                    num_fp = false_fp[0].size

                    false_positives = np.vstack(
                        (false_positives, feas[false_fp[0], false_fp[1], :]))

                    total_fp += num_fp
                    print("Scale: {0:.2f}, FP: {1}".format(scale, num_fp))

        image_random_ind = np.random.permutation(
            scale_sample_ind)[:non_boot_samples_per_im]

        all_features[collected:collected + image_random_ind.size, :] = (
            image_samples[image_random_ind, :].copy())

        collected += image_random_ind.size

    all_features = all_features[:collected]

    bootstrap_str = ""
    if options.bootstrap:
        bootstrap_str = "b."

    target_file = os.path.join(
        config_opts[0].feature_path,
        "non_annotation_features_%s.%s%s%03d:%03d.npy" %
        (options.set_type, feature_str, bootstrap_str, data_split_ind,
         n_data_splits))

    if options.bootstrap:
        all_features = np.vstack((all_features[:collected], false_positives))
        collected = all_features.shape[0]

    print("Saving to: {0}".format(target_file))

    assert (np.all((all_features * all_features).sum(axis=1) >= 0.9999))

    np.save(target_file, all_features)
def classify_subrange(n_data_splits, data_split_ind):

    num_images = len(
        config_opts[0].non_annotation_image_inds[options.set_type])

    split_inds = np.linspace(0, num_images, num=n_data_splits + 1)

    data_split_start = int(split_inds[data_split_ind])
    data_split_end = int(split_inds[data_split_ind + 1])

    num_split_images = data_split_end - data_split_start

    if options.bootstrap:
        svm_model_file = os.path.join(config_opts[0].svm_model_path,
                                      "modelb.%ssvm" % feature_str)
    else:
        svm_model_file = os.path.join(config_opts[0].svm_model_path,
                                      "model1.%ssvm" % feature_str)

    lm = read_liblinear_model(svm_model_file)

    w = lm.w[:-1]
    #    data_dim = float(w.size)
    #    w_thresh = np.sort(np.abs(w))[int(data_dim * .95)]
    #    w[np.abs(w) < w_thresh] = 0.0

    b = lm.w[-1] * lm.bias

    fv = vivid.ImageSource(
        imlist=config_opts[0].image_sets_fp[options.set_type])
    cs = vivid.ConvertedSource(fv,
                               target_type=vivid.cv.CV_32FC3,
                               scale=1.0 / 255.0)
    gv = vivid.GreySource(cs)

    feature_sources = []

    for cos in config_opts:
        if cos.feature.fea_type == 'flex':
            clusters = np.load(cos.dictionary_file).reshape(
                (cos.feature.cell.dictionary_size, cos.feature.cell.patch_size,
                 cos.feature.cell.patch_size))

            feature_sources.append(FeatureSource(gv, cos.feature, clusters))
        elif cos.feature.fea_type == 'lbp':
            feature_sources.append(FeatureSource(gv, cos.feature))
        elif cos.feature.fea_type == 'uniform':
            feature_sources.append(FeatureSource(gv, cos.feature))
        else:
            raise ValueError("Unknown feature type")

    num_non_annotation_images = len(
        config_opts[0].non_annotation_image_inds[options.set_type])

    all_scores = []

    for i, image_ind in enumerate(config_opts[0].non_annotation_image_inds[
            options.set_type][data_split_start:data_split_end]):

        print("{0}/{1}: {2} - {3}".format(
            i, num_split_images, image_ind,
            config_opts[0].image_sets_fp[options.set_type][image_ind]))

        for fs in feature_sources:
            fs.init_frame(image_ind)
            n_scales = len(fs.scales)

        for si in xrange(n_scales):
            for fs in feature_sources:
                locs, scale = fs.init_scale(si)

            num_y, num_x = locs[0].shape
            n_total = locs[0].size

            scale_scores = np.empty((num_y, num_x), dtype='float32')

            for yind in range(0, num_y, 10):
                ymin = yind
                ymax = min(num_y, yind + 10)

                feas = [
                    fs.get_features_from_scale(ymin=yind,
                                               ymax=ymax,
                                               xmin=0,
                                               xmax=num_x)
                    for fs in feature_sources
                ]

                feas = np.concatenate(feas, axis=2)

                scale_scores[ymin:ymax, :] = (
                    feas * w[np.newaxis, np.newaxis, :]).sum(axis=2) + b

            num_fp = (scale_scores >= 0).sum()

            print("Im: {0}, Scale: {1:.2f}, FP: {2}".format(
                image_ind, scale, num_fp))
            for score in scale_scores.flat:
                all_scores.append(score)

    all_scores = np.array(all_scores)

    return all_scores