Skip to content
Snippets Groups Projects
Select Git revision
  • aab31bc91e218cadaaa7d273e4526f3753709c6a
  • 3.9 default
  • develop
  • 6.0
  • 5.0
  • 4.0
  • scrutinizer-patch-4
  • scrutinizer-patch-3
  • scrutinizer-patch-2
  • scrutinizer-patch-1
  • 3.7
  • 3.8
  • 3.6
  • 3.9_backported
  • 3.8_backported
  • 3.7_backported
  • 3.5
  • 3.6_backported
  • 3.5_backported
  • 3.4
  • 3.3_backported
  • 6.0.4
  • 6.0.3
  • 5.0.7
  • 6.0.2
  • 6.0.1
  • 5.0.6
  • 6.0.0
  • 5.0.5
  • 6.0.0-rc
  • 5.0.4
  • 6.0.0-beta
  • 5.0.3
  • 4.0.6
  • 5.0.2
  • 5.0.1
  • 4.0.5
  • 5.0.0
  • 4.0.4
  • 5.0.0-rc2
  • 5.0.0-rc1
41 results

functions.lib.php

Blame
  • extract_features.py 2.16 KiB
    import tensorflow as tf
    from keras_applications.vgg16 import preprocess_input as vgg_preprocess_input
    from keras_applications.resnet import preprocess_input as resnet_preprocess_input
    from cyvlfeat.sift import dsift
    import numpy as np
    
    
    def extract_vgg16_feature(img_data, model):
        """ Use Pre-traing vgg16 model to extract image features """
        img_data = vgg_preprocess_input(img_data,
                                backend=tf.keras.backend,
                                layers=tf.keras.layers,
                                models=tf.keras.models,
                                utils=tf.keras.utils)
        vgg16_feature = model.predict(img_data)
        shape = vgg16_feature.shape
        if len(shape) == 4:
            keypts = np.array([[x, y] for y in range(shape[1]) for x in range(shape[2])]).reshape(shape[1], shape[2], 2)
            keypts = np.tile(keypts, [shape[0], 1, 1, 1])
        else:
            keypts = None
    
        return vgg16_feature, keypts
        
        
    def extract_resnet50_feature(img_data, model):
        """ Use Pre-traing ResNet50 model to extract image features """
        img_data = resnet_preprocess_input(img_data,
                                backend=tf.keras.backend,
                                layers=tf.keras.layers,
                                models=tf.keras.models,
                                utils=tf.keras.utils)
        resnet50_feature = model.predict(img_data)
        shape = resnet50_feature.shape
        if len(shape) == 4:
            keypts = np.array([[x, y] for y in range(shape[1]) for x in range(shape[2])]).reshape(shape[1], shape[2], 2)
            keypts = np.tile(keypts, [shape[0], 1, 1, 1])
        else:
            keypts = None
    
        return resnet50_feature, keypts
        
    
    def extract_dsift_feature(images, step=8, size=5):
        """ extract Dense SIFT (DSIFT) image features """
        n = images.shape[0]
        if n > 0:
            d, f = dsift(images[0, :, :, 0], step=step, size=size)
        ndescr, descrl = f.shape
    
        feats = np.zeros((n, ndescr, descrl))
        keypts = np.zeros((n, ndescr, 2))
    
        for i in range(n):
            d, f = dsift(images[i, :, :, 0], step=step, size=size)
            feats[i, :, :] = f
            keypts[i, :, :] = d
    
        return feats, keypts