Example #1
0
    def __init__(self):

        '''

        Main module that ties all the other relevant models (Segmentor, BoundingBox, Classifier)

        '''

        # GLOBAL CONSTANTS
        device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')  # Device configuration

        # model paths
        CLASSIFIER_DIR = 'saved_models'
        CLASSIFIER_NAME = 'inference_resnet18_layers_6_opt_SGD_lr_0.0001_wd_0.1.pth'
        classifier_path = os.path.join(CLASSIFIER_DIR, CLASSIFIER_NAME)

        SEG_DIR = 'saved_models'
        SEG_PATH = 'AUGGI_SEGNET_STATE_2018_11_28_5_7_NEPOCHS_100_TRAINAVGLOSS_13_8_TESTAVGLOSS_13_7.pth'
        seg_path = os.path.join(SEG_DIR, SEG_PATH)

        # ------------- Instantiate Models -------------  #

        # Instantiate Seg, Boxer and Classifier models

        self.segmentor = Segmentor(seg_path)
        self.boxer = BoundingBox()
        self.classifier = Classifier(classifier_path)
Example #2
0
def seg_eval(args):
    if not (DatasetHandler.is_readable(args.gold_file)):
        logging.error("path '%s' open failed !" % (args.gold_file))
        logging.error('Exit!')
        exit(1)
    if not DatasetHandler.is_readable(args.predict_file):
        logging.error("path '%s' open failed ! predict file open error ." %
                      (args.predict_file))
        logging.error("Exit!")
        exit(1)
    segmentor = Segmentor()
    segmentor.evaluate(args.gold_file, args.predict_file)
Example #3
0
def seg_train(args):
    if not DatasetHandler.is_readable(args.training_file):
        logging.error("path '%s' open failed !" % (args.training_file))
        logging.error('Exit!')
        exit(1)
    if not DatasetHandler.is_readable(args.developing_file):
        logging.error("path '%s' open failed !" % (args.developing_file))
        logging.error("Exit!")
        exit(1)
    if not DatasetHandler.is_writeable(args.model_saving):
        logging.error("path '%s' open failed !" % (args.model_saving))
        logging.error('Exit!')
        exit(1)
    segmentor = Segmentor()
    segmentor.train(args.training_file, args.developing_file,
                    args.model_saving, args.max_iter)
Example #4
0
def seg_predict(args):
    if not DatasetHandler.is_readable(args.predict_file):
        logging.error("path '%s' open failed !" % (args.predict_file))
        logging.error('Exit!')
        exit(1)
    if not DatasetHandler.is_readable(args.model_loading):
        logging.error("path '%s' open failed ! Model load Error ." %
                      (args.model_loading))
        logging.error("Exit!")
        exit(1)
    if not DatasetHandler.is_writeable(
            args.output_path) and args.output_path != "stdout":
        logging.error("path '%s' open failed !" % (args.output_path))
        logging.error('Exit!')
        exit(1)
    segmentor = Segmentor()
    segmentor.predict(args.model_loading, args.predict_file, args.output_path)
Example #5
0
def mask_and_save_silhouettes(base_path):

    if not os.path.exists(os.path.join(
            base_path, RAW_SILHOUETTES_DIR_NAME)) and os.path.exists(
                os.path.join(base_path, IMAGE_DIR_NAME)):
        image_paths = filepath_at_dir_sorted(
            os.path.join(base_path, IMAGE_DIR_NAME), sorter)
        if len(image_paths) > 0:
            os.makedirs(os.path.join(base_path, RAW_SILHOUETTES_DIR_NAME))
            segment = Segmentor()
            try:
                for path in image_paths:
                    segment.segment_images_at_path(
                        path,
                        os.path.join(base_path, RAW_SILHOUETTES_DIR_NAME, ''))
            except Exception as e:
                if os.path.exists(
                        os.path.join(base_path, RAW_SILHOUETTES_DIR_NAME)):
                    shutil.rmtree(
                        os.path.join(base_path, RAW_SILHOUETTES_DIR_NAME))
                raise e

    silhouettes, file_paths = load_images_of_type(base_path,
                                                  RAW_SILHOUETTES_DIR_NAME)
    if len(silhouettes) > 0:
        os.makedirs(os.path.join(base_path, SILHOUETTES_DIR_NAME))

        index = 1
        for sil in silhouettes:
            sil = np.array(sil)
            new_sil = np.zeros([sil.shape[0], sil.shape[1]])

            mask = lambda im: im[:, :] > 0

            if sil.shape[2] > 1:
                new_sil[mask(sil[:, :, 0]) | mask(sil[:, :, 1])
                        | mask(sil[:, :, 2])] = 1
                save_np_data(os.path.join(base_path, SILHOUETTES_DIR_NAME),
                             '%d.npy' % index, new_sil)
                index += 1
Example #6
0
    def test_segmentor(self):
        tests = [
            ([1, 1, 0, 1, 0, 0, 1, 0, 1], [[0, 1, 2, 3]]),
            ([1, 1, 0, 1, 1, 0, 1, 0, 1], [[0, 1, 2, 3, 4, 5, 6]]),
            ([1, 1, 0, 1, 1, 0, 1, 1, 1], [list(range(0, 8 + 1))]),
            ([1, 1, 1, 0, 1], [[0, 1, 2, 3, 4]]),
            ([0, 0, 0, 0, 0], []),
            ([1] * 7 + [0] * 3, [list(range(7))])
        ]

        min_frames = 4
        threshold = 0.7

        for ex, exp in tests:
            self.assertEqual(exp, Segmentor._segmentor(ex, min_frames, threshold))
Example #7
0
class Inference:

    def __init__(self):

        '''

        Main module that ties all the other relevant models (Segmentor, BoundingBox, Classifier)

        '''

        # GLOBAL CONSTANTS
        device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')  # Device configuration

        # model paths
        CLASSIFIER_DIR = 'saved_models'
        CLASSIFIER_NAME = 'inference_resnet18_layers_6_opt_SGD_lr_0.0001_wd_0.1.pth'
        classifier_path = os.path.join(CLASSIFIER_DIR, CLASSIFIER_NAME)

        SEG_DIR = 'saved_models'
        SEG_PATH = 'AUGGI_SEGNET_STATE_2018_11_28_5_7_NEPOCHS_100_TRAINAVGLOSS_13_8_TESTAVGLOSS_13_7.pth'
        seg_path = os.path.join(SEG_DIR, SEG_PATH)

        # ------------- Instantiate Models -------------  #

        # Instantiate Seg, Boxer and Classifier models

        self.segmentor = Segmentor(seg_path)
        self.boxer = BoundingBox()
        self.classifier = Classifier(classifier_path)

        # ----------------------------------------------  #

    def predict(self, image):

        # ------------ Run full inference on image -----  #

        binary_mask = self.segmentor.segment(image)  # get binary mask (sending rgb PIL)
        x_min, y_min, x_max, y_max = self.boxer.get_box(binary_mask)  # get bbox, sending grayscale PIL image

        # classify the (original) image, pass bboxes, receive a bristol prediction
        bristol_pred = self.classifier.classify(image, x_min, y_min, x_max, y_max)

        return bristol_pred
Example #8
0
from naivebayes import NaiveBayesClassifier
import os
import re
import codecs
from segmentor import Segmentor

def corpus_generator(segmentor):
    for corpus in map(lambda x: "sentiment_corpus/" + x, ["Ctrip_htl_ba_4000", "Dangdang_Book_4000", "Jingdong_NB_4000"]):
        classes = filter(lambda x: x[0] != ".", os.listdir(corpus))
        for cls in classes:
            print "Enumerating for '%s/%s' reviews." % (corpus, cls)
            cls_dir = os.path.join(corpus, cls)
            files = filter(lambda x: x.endswith(".txt"), os.listdir(cls_dir))
            for filename in files:
                with codecs.open(os.path.join(cls_dir, filename), "r", encoding="utf8") as file:
                    for line in file:
                        if not line.strip():
                            continue
                        words = segmentor(line.strip())
                        yield (cls, words)

segmentor = Segmentor()
generator = corpus_generator(segmentor)
classifier = NaiveBayesClassifier()
classifier.train(generator)

print classifier.classify(segmentor(u"这一地区生鲜奶收购价持续在低位徘徊,导致很多奶户入不敷出,被迫“砍牛”(杀牛或卖牛)。 近期,双鸭山市多地奶农联名向记者反映"))

# print classifier.classify("This is awesome but still I don't like it thisisaweirdwordneveroccurs. ".split(" "))
# print classifier.classify("iqbvajkkjbarjta".split(" "))
# print classifier.classify("I don't recommend.".split(" "))
Example #9
0
    images_path = list(list_files())
    #image_path = images_path[3]
    # image_path = "dataset/pes1.png"

    times = []
    set_stats = []

    for image_path in images_path:

        image = cv2.imread(image_path)
        image = cv2.resize(image, (512, 512))

        image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        segmentor = Segmentor(image_gray)
        duration, out = segmentor.max_flow_gray()

        rect = get_rect(out)
        segmentor = Segmentor_grab(image)
        duration1, out1 = segmentor.segment(rect)

        out1 = cv2.cvtColor(out1, cv2.COLOR_BGR2GRAY)

        set_stats.append(compare_images(out, out1))
        times.append((duration, duration1))

        output_path = OUTPUT_DIR + image_path.split('/')[1].split(
            '.')[0] + "_graph_cut.jpg"
        output_path1 = OUTPUT_DIR + image_path.split('/')[1].split(
            '.')[0] + "_grab_cut.jpg"