示例#1
0
文件: image.py 项目: 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")
示例#2
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!')
示例#3
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
示例#4
0
def get_resnet50_network_with_images(file_paths):
    tensors = convert_images_to_tensors(file_paths).astype('float32')
    preprocessed_input = preprocess_input_resnet50(tensors)
    return ResNet50(weights='imagenet', include_top=False).predict(preprocessed_input, batch_size=32)
示例#5
0
def extract_Resnet50(file_paths):
    tensors = paths_to_tensor(file_paths).astype('float32')
    preprocessed_input = preprocess_input_resnet50(tensors)
    return ResNet50(weights='imagenet',
                    include_top=False).predict(preprocessed_input,
                                               batch_size=32)
示例#6
0
 def ResNet50_predict_labels(self, img_path):
     # returns prediction vector for image located at img_path
     img = preprocess_input_resnet50(self.path_to_tensor(img_path))
     return np.argmax(self.ResNet50_model.predict(img))
示例#7
0
def extract_Resnet50_single(img):
    #tensors = paths_to_tensor(file_paths).astype('float32')
    preprocessed_input = preprocess_input_resnet50(np.expand_dims(img.astype('float32'),axis=0))
    return ResNet50(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)