def process_image(img, debug=False):
    clf = classifier.get_classifier()
    windows = search(img, clf)
    heat_map = get_heat_map(img, windows)
    heat_map = apply_threshold(heat_map, 1)
    labeled_img = get_labeled_image(img, heat_map)

    if debug:
        import matplotlib.pyplot as plt
        from sliding_window import draw_boxes

        f, axarr = plt.subplots(2, 2, figsize=(15, 15))

        axarr[0, 0].set_title('Original Image')
        axarr[0, 0].imshow(img)

        axarr[0, 1].set_title('Detected Windows')
        axarr[0, 1].imshow(draw_boxes(img, windows))

        axarr[1, 0].set_title('Heat Map')
        axarr[1, 0].imshow(heat_map)

        axarr[1, 1].set_title('Labeled Image')
        axarr[1, 1].imshow(labeled_img)

        plt.show()

    return labeled_img
Пример #2
0
def run_algo(data, nb_class, hparam, argv):
    """ Use Parambath et al. (2014) algorithm on given options """

    # grid on t: covers all space with half a step at each extremity
    t_values, step = np.linspace(argv.tmin,
                                 argv.tmax,
                                 argv.max_step,
                                 endpoint=False,
                                 retstep=True)
    t_values = t_values + step / 2

    outputs = {
        "confusions": {
            "train": np.zeros((argv.max_step, nb_class, nb_class), dtype=int),
            "valid": np.zeros((argv.max_step, nb_class, nb_class), dtype=int),
            "test": np.zeros((argv.max_step, nb_class, nb_class), dtype=int)
        },
        "predictions": {
            "train":
            np.zeros(
                (argv.max_step, data["train"]["labels"].shape[0], nb_class),
                dtype=np.float32),
            "valid":
            np.zeros(
                (argv.max_step, data["valid"]["labels"].shape[0], nb_class),
                dtype=np.float32),
            "test":
            np.zeros(
                (argv.max_step, data["test"]["labels"].shape[0], nb_class),
                dtype=np.float32)
        },
        "t_values": t_values
    }

    conf_mats = outputs["confusions"]
    preds = outputs["predictions"]

    for t_val_i, t_val in enumerate(t_values):
        class_w = compute_weights(t_val, nb_class, argv.beta)

        classif = classifier.get_classifier(argv, hparam, class_w)

        classif.fit(data["train"]["exemples"], data["train"]["labels"])

        for subset in ["train", "valid", "test"]:
            out_iter = classifier.get_confusion(data, nb_class, subset,
                                                classif)

            if nb_class == 2:
                conf_mats[subset][t_val_i], preds[subset][t_val_i, :,
                                                          0] = out_iter
            else:
                conf_mats[subset][t_val_i], preds[subset][t_val_i] = out_iter

    return outputs
def run_algo(data, nb_class, hparam, argv):
    """ Use baseline algorithm on given options """

    outputs = {
        "confusions": {
            "train": np.zeros((1, nb_class, nb_class), dtype=int),
            "valid": np.zeros((1, nb_class, nb_class), dtype=int),
            "test": np.zeros((1, nb_class, nb_class), dtype=int)
        },
        "predictions": {
            "train":
            np.zeros((1, data["train"]["labels"].shape[0], nb_class),
                     dtype=np.float32),
            "valid":
            np.zeros((1, data["valid"]["labels"].shape[0], nb_class),
                     dtype=np.float32),
            "test":
            np.zeros((1, data["test"]["labels"].shape[0], nb_class),
                     dtype=np.float32)
        },
        "t_values": [-1]
    }

    conf_mats = outputs["confusions"]
    preds = outputs["predictions"]

    if argv.classif.lower() == "ir":
        # class weight = 1 - <class proportion>
        class_w = {
            class_i: 1 - (data["train"]["labels"] == class_i).sum() /
            data["train"]["labels"].shape[0]
            for class_i in range(nb_class)
        }
    else:
        class_w = {class_i: 1.0 for class_i in range(nb_class)}

    classif = classifier.get_classifier(argv, hparam, class_w)

    classif.fit(data["train"]["exemples"], data["train"]["labels"])

    for subset in ["train", "valid", "test"]:
        out_iter = classifier.get_confusion(data, nb_class, subset, classif)

        if nb_class == 2:
            conf_mats[subset][0], preds[subset][0, :, 0] = out_iter
        else:
            conf_mats[subset][0], preds[subset][0] = out_iter

    return outputs
            window_list.append(((x_start, y_start), (x_end, y_end)))

    return window_list


def draw_boxes(img, bboxes, color=(0, 0, 255), thick=6):
    """Draw bounding boxes and return the image."""
    imcopy = np.copy(img)
    for bbox in bboxes:
        cv2.rectangle(imcopy, bbox[0], bbox[1], color, thick)

    return imcopy


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from skimage import io

    fig, ax = plt.subplots(4, 2)
    for i in range(0, 4):
        for j in range(0, 2):
            image = io.imread('test_images/test{}.jpg'.format(i * 2 + j + 1))

            windows = multi_scale_windows(image)
            windows = search_windows(image, windows,
                                     classifier.get_classifier())
            window_img = draw_boxes(image, windows, color=(0, 0, 255), thick=6)

            ax[i, j].imshow(window_img)

    plt.show()
    else:
        print ">> extract features"
        features = fe.get_features(images,
                                   hp.FeatureType.COL_BOW)  # imgNum*featureLen
        np.savetxt(fdir, features, delimiter=',')
        print ">> features extracted"

    ##################################################
    #	FIT A CLASSIFIER USING RANDOM DATA
    ##################################################
    all_data = train_ids
    sampled_num = int(train_img_num * hp.sampling_ratio)
    sampled_ids = all_data[:sampled_num]
    lbs = [labels[x] for x in sampled_ids]
    fts = [features[x] for x in sampled_ids]
    classifier = cf.get_classifier(fts, lbs, hp.ClassifierType.RANDOM_FOREST)
    print ">> fit classifier with %d labeled samples" % sampled_num

    train_features = [features[x] for x in train_ids]
    train_labels = [labels[x] for x in train_ids]
    test_features = [features[x] for x in test_ids]
    test_labels = [labels[x] for x in test_ids]
    print "train", get_precision(classifier, train_features, train_labels)
    print "test", get_precision(classifier, test_features, test_labels)

    ##################################################
    #	ACTIVE LEARNING / RANDOM SAMPLING
    ##################################################
    useAL = True  # use Active Learning or Random Sampling (when comparing AL with random sampling, do AL first)
    comparePrecision = False  # True when # of random sampling data = # of AL data
    step = 0
Пример #6
0
def get_classifier():
    global classifier_dft
    if classifier_dft is None:
        classifier_dft = classifier.get_classifier()
    return classifier_dft
Пример #7
0
root.wm_title('Sentiment Analysis Application')

top_frame = Frame(root)
top_frame.pack()

bottom_frame = Frame(root)
bottom_frame.pack(side=BOTTOM)

l1 = Label(top_frame, text='Enter a review:')
l1.pack(side=LEFT)

w = Text(top_frame, height=3 )
w.pack(side=LEFT)

print("UI COMPLETE")
clf = get_classifier()

def main_op():
    review_spirit = w.get('1.0',END)
    demo = process(review_spirit)
    print(demo)

    demo1 = create_word_features(demo)
    print(demo1)
    demo2 = ('review is ' + clf.classify(demo1))
    print(demo2)
    l2 = Label(bottom_frame, text=demo2)
    l2.pack()

button = Button(bottom_frame, text='Analyse', command=main_op )
button.pack(side=BOTTOM)
Пример #8
0
    # print(df.shape)
    #
    # print(len(X))
    # print(len(Y))

    # X_res, Y_res = smote_sampling(X, Y)
    #
    X_test = df[X_columns].values.tolist()
    Y_test = df[Y_columns].values.tolist()

    # from collections import Counter
    # print(Counter(Y_res))

    X_res, Y_res = X, Y

    clf = get_classifier('CSVM')
    Y_list = list(map(lambda x: [x], Y_res))
    clf.fit(X_res, Y_list)
    test_clf(clf, X_test, Y_test)

    # clf = get_classifier('CS')
    # cost_mat = np.zeros((len(X_res), 4))
    # 0 - tn, 1 - fp, 2 - fn, 3 - tp
    # 00 tn
    # 01 fp
    # 10 fn
    # 11 tp
    # for each_y in Y_res:
    #     if each_y == 1:
    #         cost_mat[:, 0] = 1.5
    #         cost_mat[:, 1] = 0.5
 def test_invalid_class_raises_key_error(self):
     with self.assertRaises(KeyError):
         classifier = get_classifier("invalid_class")
Пример #10
0
 def test_invalid_spelling_raises_key_error(self):
     with self.assertRaises(KeyError):
         classifier = get_classifier("random_forestClassifier")
Пример #11
0
 def test_valid_class_gives_expected(self):
     classifier = get_classifier("RandomForestClassifier")
     self.assertIsInstance(classifier, RandomForestClassifier)
Пример #12
0
        args.training_set
    )

    if args.test_set:
        training_set_numbers = training_set_numbers

        test_set_parser = TestSetIO()
        test_set_pixels = test_set_parser.parse(args.test_set)
    else:
        # Without a test set we need to split one out from the training set
        training_set_pixels, test_set_pixels, training_set_numbers, \
            test_set_numbers = train_test_split(
                training_set_pixels, training_set_numbers, test_size=0.3
            )

    classifier = get_classifier(args.classifier)
    classifier.train(training_set_numbers, training_set_pixels)

    test_set_predicted_numbers = classifier.predict(test_set_pixels)

    if args.test_set:
        test_set_parser.write(test_set_predicted_numbers, stdout)
    else:
        print metrics.classification_report(
            test_set_numbers,
            test_set_predicted_numbers
        )

        if args.show_misclassified:
            image_plotter = ImageDisplay()
            print(
Пример #13
0
def get_classifier():
    global classifier_dft
    if classifier_dft is None:
        classifier_dft = classifier.get_classifier()
    return classifier_dft
Пример #14
0
    with open('cat_to_name.json', 'r') as f:
        cat_to_name = json.load(f)

    model = model_load(pretrained=True)

    # Remove gradient tracking from the network parameters
    for param in model.parameters():
        param.requires_grad = False

    # Create a new classifier
    # Use dropout probability that the VGG models use -
    # authors likely knew what they were doing

    classifier = get_classifier(features_count,
                                classifier_hidden=classifier_hidden,
                                dropout_p=0.5)

    #Attach the new classifier (it has random weights now)
    if classifier_name == 'classifier':
        model.classifier = classifier
    elif classifier_name == 'fc':
        model.fc = classifier

    # Move the model to GPU before constructing the optimizer
    model.to(device)

    # Set up negative likelihood loss and the adam optimizer
    # (momentum is useful)
    criterion = nn.NLLLoss()
    if classifier_name == 'classifier':
Пример #15
0
parser.add_argument('--log',
                    default='classifier/logs/',
                    metavar='LOG',
                    help='path for recording training informtion')
parser.add_argument('--name',
                    default='mnist_lenet',
                    metavar='name',
                    help='specify a name for saving the model')

args = parser.parse_args()

# use cuda
use_cuda = torch.cuda.is_available()

# initialize model
model = get_classifier(args.classifier)
if use_cuda: model.cuda()

# get training, validation, and test dataset

dataset = mnist.IDEAL('./data', args.font)
train_size = len(dataset) - args.val_size - args.test_size

train_loader = torch.utils.data.DataLoader(dataset,
                                           batch_size=args.batch_size,
                                           sampler=chunk.Chunk(train_size, 0))

val_loader = torch.utils.data.DataLoader(dataset,
                                         batch_size=args.test_batch,
                                         sampler=chunk.Chunk(
                                             args.val_size, train_size))
Пример #16
0
	zca_whitening=False,
	rotation_range=0,
	width_shift_range=0,
	height_shift_range=0,
	shear_range=0.,
	zoom_range=0,
	channel_shift_range=0.,
	fill_mode='nearest',
	cval=0.,
	horizontal_flip=False,
	vertical_flip=False,
	rescale=None,
	dim_ordering=K.image_dim_ordering())

#create classifier
classifier = clf.get_classifier(classifier_choice, input_shape=shape)
# define callbacks
# save intermediate training steps (every 50 batches)
inter = clf.EvaluateCallback(images_val[0], groundtruth_val[0], out_path = 'inter_test_image', verbose = False)
# save model instances with lowest validation loss
model_checkpoint = ModelCheckpoint('unet_weights.hdf5', monitor='val_loss', verbose =1, save_best_only=True, save_weights_only = True)
# early stop if validation loss does not significantly drop after 10 epochs. Saves time and
# avoids overfitting
early_stop = EarlyStopping(patience = 10)
# classifier.fit(images, groundtruth, batch_size = 16, validation_split = 0.1, nb_epoch=100, callbacks=[model_checkpoint, stef])
hist = classifier.fit_generator(train_gen.flow(images_train, groundtruth_train, batch_size=bs),
						samples_per_epoch=2000,
						nb_epoch=100,
						validation_data = val_gen.flow(images_val, groundtruth_val),
						nb_val_samples = len(images_val),
						callbacks=[model_checkpoint, inter, early_stop])
Пример #17
0
# create features and assign class for each patch
#X_6d = np.asarray([hp.extract_features(img_patches[i]) for i in range(len(img_patches))])
X_2d = np.asarray(
    [hp.extract_features_2d(img_patches[i]) for i in range(len(img_patches))])
Y = np.asarray([
    hp.value_to_class(np.mean(gt_patches[i]), foreground_threshold)
    for i in range(len(gt_patches))
])

# choose features to try
# features = np.load('features.npy') # np.load('features_full.npy')
# Y = np.load('labels.npy') #np.load('labels_full.npy')
features = X_2d

# create classifier
classifier = clf.get_classifier(classifier_choice)
# fit classifier
classifier.fit(X_2d, Y)

# cross validate
f1_scores = cross_val_score(classifier, features, Y, cv=k, scoring='f1')
print("F1-score: %0.2f (+/- %0.2f)" % (f1_scores.mean(), f1_scores.std() * 2))

# predict on single image and overlay
# idx = 1
# img_patches_test = hp.img_crop2(images[idx], patch_size, patch_size, overlap)
# X_2d_test = np.asarray([hp.extract_features_2d(img_patches_test[i]) for i in range(len(img_patches_test))])
# Y_est = classifier.predict(X_2d_test)
# h = groundtruth[idx].shape[0]
# w = groundtruth[idx].shape[1]
# predicted_img = hp.label_to_img(h, w, patch_size, patch_size, Y_est)
Пример #18
0
def main():
     # Configs
    args = get_args()
    cfg = Config(args.config)
    pose_kwargs = cfg.POSE
    clf_kwargs = cfg.CLASSIFIER
    tracker_kwargs = cfg.TRACKER

    # Initiate video/webcam
    source = args.source if args.source else 0
    video = Video(source)

    ## Initiate trtpose, deepsort and action classifier
    pose_estimator = get_pose_estimator(**pose_kwargs)
    if args.task != 'pose':
        tracker = get_tracker(**tracker_kwargs)
        if args.task == 'action':
            action_classifier = get_classifier(**clf_kwargs)

    ## initiate drawer and text for visualization
    drawer = Drawer(draw_numbers=args.draw_kp_numbers)
    user_text = {
        'text_color': 'green',
        'add_blank': True,
        'Mode': args.task,
        # MaxDist: cfg.TRACKER.max_dist,
        # MaxIoU: cfg.TRACKER.max_iou_distance,
    }

    # loop over the video frames
    for bgr_frame in video:
        rgb_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB)
        # predict pose estimation
        start_pose = time.time()
        predictions = pose_estimator.predict(rgb_frame, get_bbox=True) # return predictions which include keypoints in trtpose order, bboxes (x,y,w,h)
        # if no keypoints, update tracker's memory and it's age
        if len(predictions) == 0 and args.task != 'pose':
            debug_img = bgr_frame
            tracker.increment_ages()
        else:
            # draw keypoints only if task is 'pose'
            if args.task != 'pose':
                # Tracking
                # start_track = time.time()
                predictions = utils.convert_to_openpose_skeletons(predictions)
                predictions, debug_img = tracker.predict(rgb_frame, predictions,
                                                                debug=args.debug_track)
                # end_track = time.time() - start_track

                # Action Recognition
                if len(predictions) > 0 and args.task == 'action':
                    predictions = action_classifier.classify(predictions)

        end_pipeline = time.time() - start_pose
        # add user's desired text on render image
        user_text.update({
            'Frame': video.frame_cnt,
            'Speed': '{:.1f}ms'.format(end_pipeline*1000),
        })

        # draw predicted results on bgr_img with frame info
        render_image = drawer.render_frame(bgr_frame, predictions, **user_text)

        if video.frame_cnt == 1 and args.save_folder:
            # initiate writer for saving rendered video.
            output_suffix = get_suffix(args, cfg)
            output_path = video.get_output_file_path(
                args.save_folder, suffix=output_suffix)
            writer = video.get_writer(render_image, output_path, fps=30)

            if args.debug_track and args.task != 'pose':
                debug_output_path = output_path[:-4] + '_debug.avi'
                debug_writer = video.get_writer(debug_img, debug_output_path)
            print(f'[INFO] Saving video to : {output_path}')
        # show frames
        try:
            if args.debug_track and args.task != 'pose':
                debug_writer.write(debug_img)
                utils.show(debug_img, window='debug_tracking')
            if args.save_folder:
                writer.write(render_image)
            utils.show(render_image, window='webcam' if isinstance(source, int) else osp.basename(source))
        except StopIteration:
            break
    if args.debug_track and args.task != 'pose':
        debug_writer.release()
    if args.save_folder and len(predictions) > 0:
        writer.release()
    video.stop()
def run_algo(data, nb_class, hparam, argv):
    """ Use CONE algorithm on given options """

    overall_timer = time()

    outputs = {"confusions": {"train": np.zeros((argv.max_step, nb_class, nb_class), dtype=int),
                              "valid": np.zeros((argv.max_step, nb_class, nb_class), dtype=int),
                              "test": np.zeros((argv.max_step, nb_class, nb_class), dtype=int)},
               "predictions": {"train": np.zeros((argv.max_step, data["train"]["labels"].shape[0], nb_class), dtype=np.float32),
                               "valid": np.zeros((argv.max_step, data["valid"]["labels"].shape[0], nb_class), dtype=np.float32),
                               "test": np.zeros((argv.max_step, data["test"]["labels"].shape[0], nb_class), dtype=np.float32)},
               "t_values": np.full(argv.max_step, -1, dtype=np.float32)}

    conf_mats = outputs["confusions"]
    preds = outputs["predictions"]

    best_fm = 0
    next_t_val = (argv.tmin+argv.tmax)/2
    max_fm = 1

    step = 0

    if argv.cone_with_state:
        state = np.ones((argv.state_size, argv.state_size))
    else:
        saved_cones = np.zeros((argv.max_step+1, 6))
        # add line y = 1 as "cone" to get early intersections points
        saved_cones[0] = [0, -1, 0, 0, 1, 1]

        poss_next_t = np.empty(0, dtype=np.float32)
        poss_next_fm = np.empty(0, dtype=np.float32)

    while max_fm >= best_fm and step < argv.max_step:
        timer = time()

        log.debug("Compute weights...")

        outputs["t_values"][step] = next_t_val
        class_w = compute_weights(outputs["t_values"][step], nb_class, argv.beta)

        log.debug("Initialize classifier...")

        classif = classifier.get_classifier(argv, hparam, class_w)

        log.debug("Fit classifier...")
        timer_train = time()

        classif.fit(data["train"]["exemples"], data["train"]["labels"])

        log.debug("\ttrain time: %f", time()-timer_train)

        log.debug("Test classifier...")
        timer_test = time()

        for subset in ["train", "valid", "test"]:
            out_iter = classifier.get_confusion(data, nb_class, subset, classif)

            if nb_class == 2:
                conf_mats[subset][step], preds[subset][step, :, 0] = out_iter
            else:
                conf_mats[subset][step], preds[subset][step] = out_iter

        log.debug("\ttest time: %f", time()-timer_test)

        log.debug("Select next cone...")

        timer_cone = time()
        if argv.cone_with_state:
            timer_draw = time()
            curr_fm = add_cone(state, conf_mats["train"][step], outputs["t_values"][step], argv)
            log.debug("\t\tdrawing cone time: %f", time()-timer_draw)

            timer_fm = time()
            next_t_val, max_fm = get_best_fm_available(state, argv, t_vals=outputs["t_values"][:step+1])
            log.debug("\t\tfind next cone time: %f", time()-timer_fm)

            if argv.save_states:
                state_to_png(state, argv, step)
        else:
            saved_cones[step+1] = get_slope(conf_mats["train"][step], outputs["t_values"][step],
                                            "cone", beta=argv.beta)
            curr_fm = saved_cones[step+1][0]
            next_t_val, max_fm, poss_next_t, poss_next_fm = find_next_cone(saved_cones[:step+2],
                                                                           outputs["t_values"][:step+1],
                                                                           poss_next_t, poss_next_fm,
                                                                           tmin=argv.tmin, tmax=argv.tmax)
        log.debug("\tnext cone time: %f", time()-timer_cone)

        if curr_fm > best_fm:
            best_fm = curr_fm

        log.debug("Step %d time: %f t: %.5f fm: %.5f next t: %.5f max fm: %.5f", step, time()-timer,
                  outputs["t_values"][step], curr_fm, next_t_val, max_fm)

        step += 1

    log.debug("TRAIN TIME: %f", time()-overall_timer)

    return outputs