簡體   English   中英

如何顯示來自 tf.image.crop_and_resize 的圖像

[英]How to display images from tf.image.crop_and_resize

我必須在我的圖像上應用tf.image.crop_and_resize並希望從每個圖像中生成 5 個框。 我寫了下面的代碼,它工作正常

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import numpy as np

# Load the pre-trained Xception model to be used as the base encoder.
xception = keras.applications.Xception(
    include_top=False, weights="imagenet", pooling="avg"
)
# Set the trainability of the base encoder.
for layer in xception.layers:
  layer.trainable = False
# Receive the images as inputs.
inputs = layers.Input(shape=(299, 299, 3), name="image_input")

input ='/content/1.png'

input = tf.keras.preprocessing.image.load_img(input,target_size=(299,299,3))
image = tf.expand_dims(np.asarray(input)/255, axis=0)
BATCH_SIZE = 1
NUM_BOXES = 5
IMAGE_HEIGHT = 256
IMAGE_WIDTH = 256
CHANNELS = 3
CROP_SIZE = (24, 24)


boxes = tf.random.uniform(shape=(NUM_BOXES, 4))
box_indices = tf.random.uniform(shape=(NUM_BOXES,), minval=0, maxval=BATCH_SIZE, dtype=tf.int32)


output = tf.image.crop_and_resize(image, boxes, box_indices, CROP_SIZE)
xception_input = tf.keras.applications.xception.preprocess_input(output)

上面的代碼工作正常,但是當我想顯示這些框時,我在代碼下面運行

for i in range(5):

  # define subplot
  plt.subplot(330 + 1 + i)

  # generate batch of images
  batch = xception_input.next()

  # convert to unsigned integers for viewing
  image = batch[0].astype('uint8')

  image = np.reshape(24,24,3)

  # plot raw pixel data
  plt.imshow(image)

#show the figure
plt.show()

但它會生成此錯誤AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'next'

您必須使用[i]而不是.next()

並且將其轉換為uint8也存在問題(但不需要reshape

for i in range(5):

  plt.subplot(331 + i)

  tensor = xception_input[i]
  #print(tensor)

  tensor = tensor*255
  image = np.array(tensor, dtype=np.uint8)
  #print(image)

  plt.imshow(image)

for獲取物品

for i, tensor in enumerate(xception_input):
  #print(tensor)

  plt.subplot(331 + i)

  tensor = tensor*255
  image = np.array(tensor, dtype=np.uint8)
  #print(image)

  plt.imshow(image)

我不知道您的代碼應該做什么,但這給了我空圖像,因為tensor具有-0.9之類的值並將其全部轉換為0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM