當前位置: 首頁>>代碼示例>>Python>>正文


Python image.load_img方法代碼示例

本文整理匯總了Python中tensorflow.python.keras.preprocessing.image.load_img方法的典型用法代碼示例。如果您正苦於以下問題:Python image.load_img方法的具體用法?Python image.load_img怎麽用?Python image.load_img使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.python.keras.preprocessing.image的用法示例。


在下文中一共展示了image.load_img方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: run_inference

# 需要導入模塊: from tensorflow.python.keras.preprocessing import image [as 別名]
# 或者: from tensorflow.python.keras.preprocessing.image import load_img [as 別名]
def run_inference():
    model = ResNet50(input_shape=(224, 224, 3), num_classes=10)
    model.load_weights(MODEL_PATH)

    picture = os.path.join(execution_path, "Haitian-fireman.jpg")

    image_to_predict = image.load_img(picture, target_size=(
        224, 224))
    image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last")
    image_to_predict = np.expand_dims(image_to_predict, axis=0)

    image_to_predict = preprocess_input(image_to_predict)

    prediction = model.predict(x=image_to_predict, steps=1)

    predictiondata = decode_predictions(prediction, top=int(5), model_json=JSON_PATH)

    for result in predictiondata:
        print(str(result[0]), " : ", str(result[1] * 100))


# run_inference() 
開發者ID:OlafenwaMoses,項目名稱:IdenProf,代碼行數:24,代碼來源:idenprof.py

示例2: test

# 需要導入模塊: from tensorflow.python.keras.preprocessing import image [as 別名]
# 或者: from tensorflow.python.keras.preprocessing.image import load_img [as 別名]
def test():
    import os
    import numpy as np
    from PIL import Image
    from tensorflow.python.keras.preprocessing.image import load_img

    from models import Darknet19Encoder, Darknet19Decoder

    inputShape = (256, 256, 3)
    batchSize = 8
    latentSize = 100

    img = load_img(os.path.join(os.path.dirname(__file__), '..','images', 'img.jpg'), target_size=inputShape[:-1])
    img.show()

    img = np.array(img, dtype=np.float32) * (2/255) - 1
#     print(np.min(img))
#     print(np.max(img))
#     print(np.mean(img))

    img = np.array([img]*batchSize) # make fake batches to improve GPU utilization

    # This is how you build the autoencoder
    encoder = Darknet19Encoder(inputShape, latentSize=latentSize, latentConstraints='bvae', beta=69)
    decoder = Darknet19Decoder(inputShape, latentSize=latentSize)
    bvae = AutoEncoder(encoder, decoder)
    bvae.ae.compile(optimizer='adam', loss='mean_absolute_error')
    while True:
        bvae.ae.fit(img, img,
                    epochs=100,
                    batch_size=batchSize)

        # example retrieving the latent vector
        latentVec = bvae.encoder.predict(img)[0]
        print(latentVec)

        pred = bvae.ae.predict(img) # get the reconstructed image
        pred = np.uint8((pred + 1)* 255/2) # convert to regular image values

        pred = Image.fromarray(pred[0])
        pred.show() # display popup 
開發者ID:alecGraves,項目名稱:BVAE-tf,代碼行數:43,代碼來源:ae.py

示例3: preprocess_image

# 需要導入模塊: from tensorflow.python.keras.preprocessing import image [as 別名]
# 或者: from tensorflow.python.keras.preprocessing.image import load_img [as 別名]
def preprocess_image(path):
        '''Process an image to numpy array.

        Args:
            path: the path of the image.

        Returns:
            Numpy array of the image.
        '''
        img = process_image.load_img(path, target_size=(224, 224))
        x = process_image.img_to_array(img)
        # x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        return x 
開發者ID:ryanfwy,項目名稱:image-similarity,代碼行數:16,代碼來源:model_util.py

示例4: load_img

# 需要導入模塊: from tensorflow.python.keras.preprocessing import image [as 別名]
# 或者: from tensorflow.python.keras.preprocessing.image import load_img [as 別名]
def load_img(in_dir):
    pred_img = [f for f in os.listdir(in_dir) if os.path.isfile(os.path.join(in_dir, f))]
    img_collection = []
    for idx, img in enumerate(pred_img):
        img = os.path.join(in_dir, img)
        img_collection.append(image.load_img(img, target_size=(out_res, out_res)))
    if (np.square(out_dim) > len(img_collection)):
        raise ValueError("Cannot fit {} images in {}x{} grid".format(len(img_collection), out_dim, out_dim))
    return img_collection 
開發者ID:prabodhhere,項目名稱:tsne-grid,代碼行數:11,代碼來源:tsne_grid.py

示例5: main

# 需要導入模塊: from tensorflow.python.keras.preprocessing import image [as 別名]
# 或者: from tensorflow.python.keras.preprocessing.image import load_img [as 別名]
def main():
    model = build_model()
    img_collection = load_img(in_dir)
    activations = get_activations(model, img_collection)
    print("Generating 2D representation.")
    X_2d = generate_tsne(activations)
    print("Generating image grid.")
    save_tsne_grid(img_collection, X_2d, out_res, out_dim) 
開發者ID:prabodhhere,項目名稱:tsne-grid,代碼行數:10,代碼來源:tsne_grid.py

示例6: _get_batches_of_transformed_samples

# 需要導入模塊: from tensorflow.python.keras.preprocessing import image [as 別名]
# 或者: from tensorflow.python.keras.preprocessing.image import load_img [as 別名]
def _get_batches_of_transformed_samples(self, index_array):
        batch_x = np.zeros(
            (len(index_array),) + self.image_shape,
            dtype=floatx())
        grayscale = self.color_mode == 'grayscale'

        # Build batch of image data
        for i, j in enumerate(index_array):
            fname = self.filenames[j]
            img = load_img(
                os.path.join(self.directory, fname),
                grayscale=grayscale,
                target_size=None,
                interpolation=self.interpolation)
            x = img_to_array(img, data_format=self.data_format)

            # Pillow images should be closed after `load_img`, but not PIL images.
            if hasattr(img, 'close'):
                img.close()

            x = self.image_data_generator.standardize(x)
            batch_x[i] = x

        # Optionally save augmented images to disk for debugging purposes
        if self.save_to_dir:
            for i, j in enumerate(index_array):
                img = array_to_img(batch_x[i], self.data_format, scale=True)
                fname = '{prefix}_{index}_{hash}.{format}'.format(
                    prefix=self.save_prefix,
                    index=j,
                    hash=np.random.randint(1e7),
                    format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))

        # Build batch of labels
        if self.class_mode == 'input':
            batch_y = batch_x.copy()
        elif self.class_mode == 'sparse':
            batch_y = self.classes[index_array]
        elif self.class_mode == 'binary':
            batch_y = self.classes[index_array].astype(floatx())
        elif self.class_mode == 'categorical':
            batch_y = np.zeros(
                (len(batch_x), self.num_classes),
                dtype=floatx())
            for i, label in enumerate(self.classes[index_array]):
                batch_y[i, label] = 1.
        else:
            return batch_x

        return batch_x, batch_y 
開發者ID:ryanfwy,項目名稱:BCNN-keras-clean,代碼行數:53,代碼來源:data_preprocesser.py


注:本文中的tensorflow.python.keras.preprocessing.image.load_img方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。