Пример #1
0
    def initialize(self):

        print('Initialization in progress...!\n')        
        
        start = time.time()
        yolo = YOLO(**{"model_path": self.model_path, 
            "anchors_path": self.anchors,
            "classes_path": self.yolo_classes_path,
            "score" : self.confidence,
            "gpu_num" : self.gpu_num,
            "model_image_size" : (416, 416),
            })
        
        # load pre-processed features database
        features, _, _ = load_features(self.recog_model)
        with open(self.classes_path, 'rb') as f:
            #img_input, input_labels = pickle.load(f)
            input_feats, input_labels = pickle.load(f)

        # load pre-trained recognition model
        model, preprocessed, input_shape = load_extractor_model(self.recog_model)
        my_preprocess = lambda x: preprocessed(pad_image(x, input_shape))

        #input_feat = extract_features(img_input, model, my_preprocess)
        sim_cutoff, (bins, cdf_list) = similarity_cutoff(input_feats, features, 0.95)

        print("Done...! It tooks {:.3f} mins\n".format((time.time() - start)/60))
        
        self.model_preproc = (yolo, model, my_preprocess)
        self.params = (input_feats, sim_cutoff, bins, cdf_list, input_labels)        
        return True
Пример #2
0
def initialize(filename):

    print('Initialization in progress...!\n')
    start = time.time()
    yolo = YOLO(
        **{
            "model_path":
            './model/keras_yolo3/model_data/yolo_weights_logos.h5',
            "anchors_path": './model/keras_yolo3/model_data/yolo_anchors.txt',
            "classes_path": './data/preprocessed/classes.txt',
            "score": 0.05,
            "gpu_num": 1,
            "model_image_size": (416, 416),
        })
    # get Inception/VGG16 model and flavor from filename
    model_name, flavor = model_flavor_from_name(filename)
    ## load pre-processed features database
    features, brand_map, input_shape = load_features(filename)

    ## load inception model
    model, preprocess_input, input_shape = load_extractor_model(
        model_name, flavor)
    my_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape))

    with open('./data/preprocessed/trained_brands.pkl', 'rb') as f:
        img_input, input_labels = pickle.load(f)

    (img_input, feat_input, sim_cutoff,
     (bins, cdf_list)) = load_brands_compute_cutoffs(img_input,
                                                     (model, my_preprocess),
                                                     features, sim_threshold)
    print('Done! It tooks {:.2f} mins.\n'.format((time.time() - start) / 60))

    return (yolo, model, my_preprocess), (feat_input, sim_cutoff, bins,
                                          cdf_list, input_labels)
Пример #3
0
def initialize(yolo, model_name, DB_path):
    print("\n\nInitialization in progress...!\n")
    start = time.time()
    
    # load pre-processed features database
    features, _, _ = load_features(model_name)
    with open(args.classes_path, 'rb') as f:
        #img_input, input_labels = pickle.load(f)
        input_feats, input_labels = pickle.load(f)

    # load pre-trained recognition model
    model, preprocessed, input_shape = load_extractor_model(model_name)
    my_preprocess = lambda x: preprocessed(pad_image(x, input_shape))

    #input_feats = extract_features(img_input, model, my_preprocess)
    sim_cutoff, (bins, cdf_list) = similarity_cutoff(input_feats, features, 0.95)

    print("Done...! It tooks {:.3f} mins\n".format((time.time() - start)/60))

    return (yolo, model, my_preprocess), (input_feats, sim_cutoff, bins, cdf_list, input_labels)
Пример #4
0
def main(args):

    img_input, input_feats, input_labels = [], [], []
    model, preprocessed, input_shape = load_extractor_model(args.recog_model)
    my_preprocess = lambda x: preprocessed(pad_image(x, input_shape))

    cat_path = list(Path(args.roi_path).iterdir())
    for cat in tqdm(cat_path):
        label = os.path.basename(str(cat))
        imgs = list(Path(cat).iterdir())
        for img in imgs:
            img = cv2.imread(str(img))
            if img is None:
                continue
            img_input.append(img)
            input_labels.append(label)

    input_feats = extract_features(img_input, model, my_preprocess)

    with open(os.path.join(args.result_path, 'trained_brands.pkl'), 'wb') as f:
        pickle.dump((input_feats, input_labels), f)

    print('Done...!')
Пример #5
0
def test(filename):
    """
    Test function: runs pipeline for a small set of input images and input
    brands.
    """
    yolo = YOLO(**{"model_path": 'keras_yolo3/yolo_weights_logos.h5',
                "anchors_path": 'keras_yolo3/model_data/yolo_anchors.txt',
                "classes_path": 'data_classes.txt',
                "score" : 0.05,
                "gpu_num" : 1,
                "model_image_size" : (416, 416),
                }
               )
    save_img_logo, save_img_match = True, True

    test_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, 'data/test')

    # get Inception/VGG16 model and flavor from filename
    model_name, flavor = model_flavor_from_name(filename)
    ## load pre-processed features database
    features, brand_map, input_shape = load_features(filename)

    ## load inception model
    model, preprocess_input, input_shape = load_extractor_model(model_name, flavor)
    my_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape).astype(np.float32))

    ## load sample images of logos to test against
    input_paths = ['test_batman.jpg', 'test_robin.png', 'test_lexus.png', 'test_champions.jpg',
                   'test_duff.jpg', 'test_underarmour.jpg', 'test_golden_state.jpg']
    input_labels = [ s.split('test_')[-1].split('.')[0] for s in input_paths]
    input_paths = [os.path.join(test_dir, 'test_brands/', p) for p in input_paths]

    # compute cosine similarity between input brand images and all LogosInTheWild logos
    ( img_input, feat_input, sim_cutoff, (bins, cdf_list)
    ) = load_brands_compute_cutoffs(input_paths, (model, my_preprocess), features, sim_threshold, timing=True)

    images = [ p for p in os.listdir(os.path.join(test_dir, 'sample_in/')) if p.endswith('.jpg')]
    images_path = [ os.path.join(test_dir, 'sample_in/',p) for p in images]

    start = timer()
    times_list = []
    img_size_list = []
    candidate_len_list = []
    for i, img_path in enumerate(images_path):
        outtxt = img_path

        ## find candidate logos in image
        prediction, image = detect_logo(yolo, img_path, save_img = True,
                                          save_img_path = test_dir, postfix='_logo')

        ## match candidate logos to input
        outtxt, times = match_logo(image, prediction, (model, my_preprocess),
                outtxt, (feat_input, sim_cutoff, bins, cdf_list, input_labels),
                save_img = save_img_match, save_img_path=test_dir, timing=True)

        img_size_list.append(np.sqrt(np.prod(image.size)))
        candidate_len_list.append(len(prediction))
        times_list.append(times)

    end = timer()
    print('Processed {} images in {:.1f}sec - {:.1f}FPS'.format(
            len(images_path), end-start, len(images_path)/(end-start)
           ))

    fig, axes = plt.subplots(1,2, figsize=(9,4))
    for iax in range(2):
        for i in range(len(times_list[0])):
            axes[iax].scatter([candidate_len_list, img_size_list][iax], np.array(times_list)[:,i])

        axes[iax].legend(['read img','get box','get features','match','draw','save'])
        axes[iax].set(xlabel=['number of candidates', 'image size'][iax], ylabel='Time [sec]')
    plt.savefig(os.path.join(test_dir, 'timing_test.png'))
Пример #6
0
            })

        input_paths = sorted(FLAGS.input_brands)
        # labels to draw on images - could also be read from filename
        input_labels = [
            os.path.basename(s).split('test_')[-1].split('.')[0]
            for s in input_paths
        ]

        # get Inception/VGG16 model and flavor from filename
        model_name, flavor = model_flavor_from_name(FLAGS.features)
        ## load pre-processed LITW features database
        features, brand_map, input_shape = load_features(FLAGS.features)

        ## load inception model
        model, preprocess_input, input_shape = load_extractor_model(
            model_name, flavor)
        my_preprocess = lambda x: preprocess_input(
            utils.pad_image(x, input_shape))

        # compute cosine similarity between input brand images and all LogosInTheWild logos
        (img_input, feat_input, sim_cutoff,
         (bins,
          cdf_list)) = load_brands_compute_cutoffs(input_paths,
                                                   (model, my_preprocess),
                                                   features, sim_threshold)

        start = timer()
        # cycle trough input images, look for logos and then match them against inputs
        text_out = ''
        for i, img_path in enumerate(FLAGS.input_images):
            text = img_path
Пример #7
0
          path-to-file2.jpg xmin,ymin,xmax,ymax,class_id[,confidence]
    Returns:
      features: (n_logos, n_features)-shaped np.array of features
      all_logos: list of np.arrays for each logo
      brand_map: brand id (in range 0,...,n_brands) for each extracted logo
    """

    all_logos, brand_map = extract_litw_logos(filename)

    features = utils.features_from_image(all_logos, model, my_preprocess)

    return features, all_logos, brand_map

if __name__ == '__main__':

    model, preprocess_input, input_shape = utils.load_extractor_model('InceptionV3', flavor=0)
    my_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape))

    print('Extracting features from LogosInTheWild database (train set) - this will take a while (~5 minutes)')
    features, all_logos, brand_map = extract_litw_features('data_all_train.txt', model, my_preprocess)

    print('Processed {} logos, transformed into feature vectors'.format(len(features)))

    # save inception features at default size 299*299
    utils.save_features('./model_poi/inception_logo_features.hdf5', features, brand_map, input_shape)

    # save features for Inception with smaller input: 200 instead of 299 - last layer is 4*4 instead of 8*8
    # Extract features at last layer as well as after last 3 inception blocks (mixed9,8,7)
    input_shape = (200,200,3)
    new_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape))
Пример #8
0
def test(filename, timestamp):
    """
    Test function: runs pipeline for a small set of input images and input
    brands.
    """
    yolo = YOLO(
        **{
            "model_path":
            './model/keras_yolo3/model_data/yolo_weights_logos.h5',
            "anchors_path": './model/keras_yolo3/model_data/yolo_anchors.txt',
            "classes_path": './data/preprocessed/classes.txt',
            "score": 0.05,
            "gpu_num": 1,
            "model_image_size": (416, 416),
        })
    save_img_logo, save_img_match = True, True

    test_dir = os.path.join(os.path.dirname(__file__), 'data/test')

    # get Inception/VGG16 model and flavor from filename
    model_name, flavor = model_flavor_from_name(filename)
    ## load pre-processed features database
    features, brand_map, input_shape = load_features(filename)

    ## load inception model
    model, preprocess_input, input_shape = load_extractor_model(
        model_name, flavor)
    my_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape))

    images = [p for p in os.listdir(os.path.join(test_dir, 'input/'))
              ]  #if p.endswith('.jpg')]
    images_path = [os.path.join(test_dir, 'input/', p) for p in images]

    #with open('./data/preprocessed/input_paths.pkl', 'r') as f:
    #    input_paths = pickle.load(f)
    #(img_input, feat_input, sim_cutoff, (bins, cdf_list)) = load_brands_compute_cutoffs(
    #                            input_paths, (model, my_preprocess), features, sim_threshold)

    start = timer()
    img_size_list = []
    candidate_len_list = []
    for i, img_path in enumerate(images_path):
        outtxt = img_path

        ## find candidate logos in image
        prediction, image = detect_logo(yolo,
                                        img_path,
                                        save_img=True,
                                        save_img_path=test_dir,
                                        postfix='_logo')

        ## match candidate logos to input
        #logo_txt = match_logo(image, prediction, (model, my_preprocess),
        #         outtxt, (feat_input, sim_cutoff, bins, cdf_list, input_labels),
        #         save_img = True, save_img_path=test_dir, timing=True)

        img_size_list.append(np.sqrt(np.prod(image.size)))
        candidate_len_list.append(len(prediction))
        #times_list.append(times)

    end = timer()
    print('Processed {} images in {:.1f}sec - {:.1f}FPS'.format(
        len(images_path), end - start,
        len(images_path) / (end - start)))
    print('Timestamp : {timestamp}')
Пример #9
0
    """

    all_logos, brand_map = extract_litw_logos(filename)
    # for i in all_logos:
    #     print(np.array(i).shape)
    # logo.append(cv2.resize(i,(128,128)))
    print("**************************************")
    print(np.array(all_logos).shape)
    features = utils.features_from_image(all_logos, model, my_preprocess)

    return features, all_logos, brand_map


if __name__ == '__main__':

    model, preprocess_input, input_shape = utils.load_extractor_model('NASNet',
                                                                      flavor=0)
    my_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape))
    print(
        'Extracting features from LogosInTheWild database (train set) - this will take a while (~5 minutes)'
    )
    features, all_logos, brand_map = extract_litw_features(
        'data_all_train.txt', model, my_preprocess)

    print('Processed {} logos, transformed into feature vectors'.format(
        len(features)))

    # # save inception features at default size 299*299
    # utils.save_features('inception_logo_features.hdf5', features, brand_map, input_shape)
    #
    # # save features for Inception with smaller input: 200 instead of 299 - last layer is 4*4 instead of 8*8
    # # Extract features at last layer as well as after last 3 inception blocks (mixed9,8,7)