Python keras.applications.vgg19.preprocess_input() Examples

The following are 30 code examples of keras.applications.vgg19.preprocess_input(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras.applications.vgg19 , or try the search function .
Example #1
Source File: feat.py    From Unstructured-change-detection-using-CNN with GNU General Public License v3.0 7 votes vote down vote up
def extra_feat(img_path):
        #Using a VGG19 as feature extractor
        base_model = VGG19(weights='imagenet',include_top=False)
	img = image.load_img(img_path, target_size=(224, 224))
	x = image.img_to_array(img)
	x = np.expand_dims(x, axis=0)
	x = preprocess_input(x)
        block1_pool_features=get_activations(base_model, 3, x)
        block2_pool_features=get_activations(base_model, 6, x)
        block3_pool_features=get_activations(base_model, 10, x)
        block4_pool_features=get_activations(base_model, 14, x)
        block5_pool_features=get_activations(base_model, 18, x)

	x1 = tf.image.resize_images(block1_pool_features[0],[112,112])
	x2 = tf.image.resize_images(block2_pool_features[0],[112,112])
	x3 = tf.image.resize_images(block3_pool_features[0],[112,112])
	x4 = tf.image.resize_images(block4_pool_features[0],[112,112])
	x5 = tf.image.resize_images(block5_pool_features[0],[112,112])
	
	F = tf.concat([x3,x2,x1,x4,x5],3) #Change to only x1, x1+x2,x1+x2+x3..so on, inorder to visualize features from diffetrrnt blocks
        return F 
Example #2
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 6 votes vote down vote up
def extract_Xception(tensor):
	from keras.applications.xception import Xception, preprocess_input
	return Xception(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) 
Example #3
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #4
Source File: neural_doodle.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #5
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #6
Source File: neural_doodle.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #7
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #8
Source File: neural_doodle.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #9
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #10
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #11
Source File: neural_doodle.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #12
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #13
Source File: neural_doodle.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #14
Source File: neural_doodle.py    From pCVR with Apache License 2.0 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #15
Source File: neural_style_transfer.py    From Style_Migration_For_Artistic_Font_With_CNN with MIT License 5 votes vote down vote up
def preprocess_image(image):
    """
    预处理图片,包括变形到(1,width, height)形状,数据归一到0-1之间
    :param image: 输入一张图片
    :return: 预处理好的图片
    """
    image = image.resize((width, height))
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)  # (width, height)->(1,width, height)
    image = vgg19.preprocess_input(image)  # 0-255 -> 0-1.0
    return image 
Example #16
Source File: gram.py    From subjective-functions with MIT License 5 votes vote down vote up
def preprocess(img):
    if hasattr(img, 'shape'):
        # Already arrayed and batched
        return vgg19.preprocess_input(img.copy())
    else:
        img = img_to_array(img).copy()
        img = np.expand_dims(img, axis=0)
        img = vgg19.preprocess_input(img)
        return img 
Example #17
Source File: keras_style_transfer.py    From cv with MIT License 5 votes vote down vote up
def preprocess_image(image_path, img_height, img_width):
    img = load_img(image_path, target_size=(img_height, img_width))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #18
Source File: 3_nerual_style_transfer.py    From deep-learning-note with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_height, img_width))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #19
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #20
Source File: neural_doodle.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #21
Source File: neural_style_transfer.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img

# util function to convert a tensor into a valid image 
Example #22
Source File: main.py    From Keras-Style-Transfer with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_height, img_width))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg19.preprocess_input(img)
    return img 
Example #23
Source File: extract_features.py    From Audio-Vision with MIT License 5 votes vote down vote up
def extract(path):
    im = cv2.imread(path)
    #img = image.load_img(path, target_size=(448,448))
    if im is None:
        raise Exception("Incorrect path")
    #im = cv2.resize(im, (448, 448))
    #im = im.transpose((2,0,1))
    #im = np.expand_dims(im, axis=0)
    im = cv2.resize(im, (448,448)).astype(np.float32)
    im = im * 255
    im[:,:,0] -= 103.939
    im[:,:,1] -= 116.779
    im[:,:,2] -= 123.68
    #im = im.transpose((2,0,1))
    im = np.expand_dims(im, axis=0)
    #x = image.img_to_array(img)
    #x = np.expand_dims(x, axis=0)
    #x = preprocess_input(x)
    im = preprocess_input(im)
#    print (im.shape)
    # Test pretrained model
    model = get_model()
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy')
    out = model.predict(im)
    
    return out 
Example #24
Source File: extract_features.py    From Audio-Vision with MIT License 5 votes vote down vote up
def extract(path):
    im = cv2.imread(path)
    #img = image.load_img(path, target_size=(448,448))
    if im is None:
        raise Exception("Incorrect path")
    #im = cv2.resize(im, (448, 448))
    #im = im.transpose((2,0,1))
    #im = np.expand_dims(im, axis=0)
    im = cv2.resize(im, (448,448)).astype(np.float32)
    im = im * 255
    im[:,:,0] -= 103.939
    im[:,:,1] -= 116.779
    im[:,:,2] -= 123.68
    #im = im.transpose((2,0,1))
    im = np.expand_dims(im, axis=0)
    #x = image.img_to_array(img)
    #x = np.expand_dims(x, axis=0)
    #x = preprocess_input(x)
    im = preprocess_input(im)
#    print (im.shape)
    # Test pretrained model
    model = get_model()
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy')
    out = model.predict(im)
    
    return out 
Example #25
Source File: vgg19_keras.py    From SPADE-Tensorflow with MIT License 5 votes vote down vote up
def call(self, x, y):
        x = ((x + 1) / 2) * 255.0
        y = ((y + 1) / 2) * 255.0
        x_vgg, y_vgg = self.vgg(preprocess_input(x)), self.vgg(preprocess_input(y))

        loss = 0

        for i in range(len(x_vgg)):
            y_vgg_detach = tf.stop_gradient(y_vgg[i])
            loss += self.layer_weights[i] * L1_loss(x_vgg[i], y_vgg_detach)

        return loss 
Example #26
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 5 votes vote down vote up
def extract_InceptionV3(tensor):
	from keras.applications.inception_v3 import InceptionV3, preprocess_input
	return InceptionV3(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) 
Example #27
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 5 votes vote down vote up
def extract_Resnet50(tensor):
	from keras.applications.resnet50 import ResNet50, preprocess_input
	return ResNet50(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) 
Example #28
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 5 votes vote down vote up
def extract_VGG19(tensor):
	from keras.applications.vgg19 import VGG19, preprocess_input
	return VGG19(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) 
Example #29
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 5 votes vote down vote up
def extract_VGG16(tensor):
	from keras.applications.vgg16 import VGG16, preprocess_input
	return VGG16(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) 
Example #30
Source File: web_utils.py    From MMFinder with MIT License 5 votes vote down vote up
def preprocess_image(image_path):
    img = image.load_img(image_path, target_size=(224, 224))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    return img