Example #1
0
File: image.py Project: lamanchy/ml
    def set_feature_model(cls, model_name):
        if model_name == 'vgg16':
            cls.feature_model = VGG16(weights='imagenet',
                                      include_top=False,
                                      pooling='avg')
            cls.fmf = lambda self, x: preprocess_input_vgg16(x)

        elif model_name == 'vgg19':
            cls.feature_model = VGG19(weights='imagenet',
                                      include_top=False,
                                      pooling='avg')
            cls.fmf = lambda self, x: preprocess_input_vgg19(x)

        elif model_name == 'xception':
            cls.feature_model = Xception(weights='imagenet',
                                         include_top=False,
                                         pooling='avg')
            cls.fmf = lambda self, x: preprocess_input_xception(x)

        elif model_name == 'resnet50':
            cls.feature_model = ResNet50(weights='imagenet',
                                         include_top=False,
                                         pooling='avg')
            cls.fmf = lambda self, x: preprocess_input_resnet50(x)

        else:
            raise ValueError("Unknown model")
Example #2
0
def format_img_VGG19(file):
  pic=load_img(file,target_size=(224,224))
  display(pic)
  pic_array=img_to_array(pic)
# pic_array.shape
  expanded=np.expand_dims(pic_array,axis=0)
  # expanded.shape
  return preprocess_input_vgg19(expanded)
Example #3
0
def create_bottlenet_features_vgg19(image_tensor):

    from keras.applications.vgg19 import VGG19
    from keras.applications.vgg19 import preprocess_input as preprocess_input_vgg19
    vgg19 = VGG19(weights='imagenet', include_top=False)
    #print(vgg19.summary())

    bottleneck_model = Model(inputs=vgg19.input, outputs=vgg19.get_layer('block5_pool').output)

    #print(bottleneck_model.summary())

    bottleneck_features = bottleneck_model.predict(preprocess_input_vgg19(image_tensor))
    print(bottleneck_features.shape)

    return bottleneck_features
Example #4
0
def preprocess_input(x, model_name):
    """
    Preprocess input in accordance to Keras preprocessing standards
    :param x: Numpy array with values between 0 and 255
    :param model_name: Name of NN model
    :return: Numpy array with preprocessed values
    """
    if model_name[:8] == 'resnet50':
        return preprocess_input_resnet50(x.copy())
    elif model_name[:5] == 'vgg19':
        return preprocess_input_vgg19(x.copy())
    elif model_name[:8] == 'xception':
        return preprocess_input_xception(x.copy())
    elif model_name[:6] == 'nasnet':
        return preprocess_input_nasnet(x.copy())
    elif model_name[:16] == 'inception_resnet':
        return preprocess_input_inception_resnet(x.copy())
    else:
        raise Exception('No such model preprocessing defined!')
Example #5
0
def load_image_features(image_path, model):
    #                   image is instance of Image from image.py
    image_file = keras_image.load_img(image_path)

    preprocessed_data = keras_image.img_to_array(image_file)
    preprocessed_data = np.expand_dims(preprocessed_data, axis=0)

    if model == VGG16:
        model = VGG16(weights='imagenet', include_top=False, pooling='avg')
        preprocessed_data = preprocess_input_vgg16(preprocessed_data)

        return model.predict(preprocessed_data)[0]

    if model == VGG19:
        model = VGG19(weights='imagenet', include_top=False, pooling='avg')
        preprocessed_data = preprocess_input_vgg19(preprocessed_data)
        print model.predict(preprocessed_data)[0]

        return model.predict(preprocessed_data)[0]
Example #6
0
def preprocess_input(feature_extractor, img_data):
    """
    Use to preprocess img before fit to model
    Args:
        feature_extractor: string, name of feature extractor
    Return:
        img_data: keras img, preprocessed img
    """

    if feature_extractor == 'vgg16':
        img_data = preprocess_input_vgg16(img_data)
    elif feature_extractor == 'vgg19':
        img_data = preprocess_input_vgg19(img_data)
    elif feature_extractor == 'inception_v3':
        img_data = preprocess_input_inception_v3(img_data)
    elif feature_extractor == 'resnet50':
        img_data = preprocess_input_resnet50(img_data)
    else:
        raise Exception('Select right feature exractor')

    return img_data
Example #7
0
def fingerprint(fn, model, size, modelname='VGG16'):
    """Load image from file `fn`, resize to `size` and run through `model`
	(keras.models.Model).

	Parameters
	----------
	fn : str
	    filename
	model : keras.models.Model instance
	size : tuple
	    input image size (width, height), must match `model`, e.g. (224,224)
	prepcosseing_f: which function to use for preprocessing, depending
	    on the keral model used!
	Returns
	-------
	fingerprint : 1d array
	"""

    # keras.preprocessing.image.load_img() uses img.rezize(shape) with the
    # default interpolation of PIL.Image.resize() which is pretty bad (see
    # imagecluster/play/pil_resample_methods.py). Given that we are restricted
    # to small inputs of 224x224 by the VGG network, we should do our best to
    # keep as much information from the original image as possible. This is a
    # gut feeling, untested. But given that model.predict() is 10x slower than
    # PIL image loading and resizing .. who cares.
    #
    # (224, 224, 3)
    ##img = image.load_img(fn, target_size=size)
    #try:
    img = PIL.Image.open(fn).resize(size, 2)

    # (224, 224, {3,1})
    arr3d = image.img_to_array(img)

    # (224, 224, 1) -> (224, 224, 3)
    #
    # Simple hack to convert a grayscale image to fake RGB by replication of
    # the image data to all 3 channels.
    #
    # Deep learning models may have learned color-specific filters, but the
    # assumption is that structural image features (edges etc) contibute more to
    # the image representation than color, such that this hack makes it possible
    # to process gray-scale images with nets trained on color images (like
    # VGG16).
    if arr3d.shape[2] == 1:
        arr3d = arr3d.repeat(3, axis=2)

# (1, 224, 224, 3)
    arr4d = np.expand_dims(arr3d, axis=0)

    # (1, 224, 224, 3)
    if modelname == ' VGG16':
        arr4d_pp = preprocess_input_vgg16(arr4d)
    elif modelname == 'VGG19':
        arr4d_pp = preprocess_input_vgg19(arr4d)
    elif modelname == 'InceptionV3':
        arr4d_pp = preprocess_input_InceptionV3(arr4d)
    elif modelname == 'ResNet50':
        arr4d_pp = preprocess_input_ResNet50(arr4d)
    else:
        raise ValueError()

# hack: crashes when image has alpha?
    if arr4d_pp.shape[3] == 4:
        arr4d_pp = arr4d_pp[:, :, :, :3]

    result = model.predict(arr4d_pp)[0, :].flatten()

    return result
Example #8
0
def get_VGG19_network_with_images(file_paths):
    tensors = convert_images_to_tensors(file_paths).astype('float32')
    preprocessed_input = preprocess_input_vgg19(tensors)
    return VGG19(weights='imagenet', include_top=False).predict(preprocessed_input, batch_size=32)
Example #9
0
def extract_VGG19(file_paths):
    tensors = paths_to_tensor(file_paths).astype('float32')
    preprocessed_input = preprocess_input_vgg19(tensors)
    return VGG19(weights='imagenet',
                 include_top=False).predict(preprocessed_input, batch_size=32)
Example #10
0
def extract_VGG19_single(img):
    #tensors = paths_to_tensor(file_paths).astype('float32')
    preprocessed_input = preprocess_input_vgg19(np.expand_dims(img.astype('float32'),axis=0))
    return VGG19(weights='imagenet', include_top=False).predict(preprocessed_input, batch_size=128)
def paths_to_tensor(img_paths):
    list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
    return np.vstack(list_of_tensors)


from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

train_tensors = paths_to_tensor(train_files).astype('float32')/255
valid_tensors = paths_to_tensor(valid_files).astype('float32')/255

from keras.applications.vgg19 import VGG19
from keras.applications.vgg19 import preprocess_input as preprocess_input_vgg19
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input as preprocess_input_resnet50

train_vgg19 = VGG19(weights='imagenet', include_top=False).predict(preprocess_input_vgg19(train_tensors), batch_size=8)
valid_vgg19 = VGG19(weights='imagenet', include_top=False).predict(preprocess_input_vgg19(valid_tensors), batch_size=8)
train_resnet50 = ResNet50(weights='imagenet', include_top=False).predict(preprocess_input_resnet50(train_tensors), batch_size=8)
valid_resnet50 = ResNet50(weights='imagenet', include_top=False).predict(preprocess_input_resnet50(valid_tensors), batch_size=8)

np.save(open('train_vgg19.npy', 'wb'), train_vgg19)
np.save(open('valid_vgg19.npy', 'wb'), valid_vgg19)
np.save(open('train_resnet50.npy', 'wb'), train_resnet50)
np.save(open('valid_resnet50.npy', 'wb'), valid_resnet50)