Python 使用matplotlib在tfrecord上绘制方框后,如何保存从tfrecord解码的图像? img=tf.image.decode\u jpeg(摄像机图像) plt.imshow(img,cmap=cmap) 标题=摄像机名称 标题(标题) plt.show() plt.savefig('image.png'))

Python 使用matplotlib在tfrecord上绘制方框后,如何保存从tfrecord解码的图像? img=tf.image.decode\u jpeg(摄像机图像) plt.imshow(img,cmap=cmap) 标题=摄像机名称 标题(标题) plt.show() plt.savefig('image.png')),python,matplotlib,deep-learning,tensorflow2.0,Python,Matplotlib,Deep Learning,Tensorflow2.0,plt.savefig()不起作用。保存的图像为空 但是,plt.show()清楚地向我显示了一个带有边框的图像这里是一个示例,我正在保存VGG16模型的第1层块中的特征贴图。稍后,我加载相同的图像并绘制它 我正在使用plt.savefig()保存图像,然后使用load\u img()加载相同的图像并进行打印。您必须在plt.show()之前调用plt.savefig() 代码- # plot feature map of first conv layer for given image fro

plt.savefig()
不起作用。保存的图像为空


但是,
plt.show()
清楚地向我显示了一个带有边框的图像

这里是一个示例,我正在保存VGG16模型的第1层块中的特征贴图。稍后,我加载相同的图像并绘制它

我正在使用
plt.savefig()
保存图像,然后使用
load\u img()
加载相同的图像并进行打印。您必须在
plt.show()
之前调用
plt.savefig()

代码-

# plot feature map of first conv layer for given image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import Model
from matplotlib import pyplot
from numpy import expand_dims

# load the model
model = VGG16()

# redefine model to output right after the first hidden layer
model = Model(inputs=model.inputs, outputs=model.layers[1].output)
model.summary()

# load the image with the required shape
img = load_img('bird.jpg', target_size=(224, 224))

# convert the image to an array
img = img_to_array(img)

# expand dimensions so that it represents a single 'sample'
img = expand_dims(img, axis=0)

# prepare the image (e.g. scale pixel values for the vgg)
img = preprocess_input(img)

# get feature map for first hidden layer
feature_maps = model.predict(img)

# plot all 64 maps in an 8x8 squares
square = 8
ix = 1
for _ in range(square):
    for _ in range(square):
        # specify subplot and turn of axis
        ax = pyplot.subplot(square, square, ix)
        ax.set_xticks([])
        ax.set_yticks([])
        # plot filter channel in grayscale
        pyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')
        ix += 1

# save the figure
pyplot.savefig("Bird_Layer1")

# load the saved figure
img = load_img('Bird_Layer1.png')

# print the loaded figure
pyplot.show(img)
输出-

Model: "model_17"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_17 (InputLayer)        (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
=================================================================
Total params: 1,792
Trainable params: 1,792
Non-trainable params: 0
_________________________________________________________________


希望这能回答你的问题。愉快的学习。

下面是一个示例,我正在保存VGG16模型的第1层块中的特征贴图。稍后,我加载相同的图像并绘制它

我正在使用
plt.savefig()
保存图像,然后使用
load\u img()
加载相同的图像并进行打印。您必须在
plt.show()
之前调用
plt.savefig()

代码-

# plot feature map of first conv layer for given image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import Model
from matplotlib import pyplot
from numpy import expand_dims

# load the model
model = VGG16()

# redefine model to output right after the first hidden layer
model = Model(inputs=model.inputs, outputs=model.layers[1].output)
model.summary()

# load the image with the required shape
img = load_img('bird.jpg', target_size=(224, 224))

# convert the image to an array
img = img_to_array(img)

# expand dimensions so that it represents a single 'sample'
img = expand_dims(img, axis=0)

# prepare the image (e.g. scale pixel values for the vgg)
img = preprocess_input(img)

# get feature map for first hidden layer
feature_maps = model.predict(img)

# plot all 64 maps in an 8x8 squares
square = 8
ix = 1
for _ in range(square):
    for _ in range(square):
        # specify subplot and turn of axis
        ax = pyplot.subplot(square, square, ix)
        ax.set_xticks([])
        ax.set_yticks([])
        # plot filter channel in grayscale
        pyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')
        ix += 1

# save the figure
pyplot.savefig("Bird_Layer1")

# load the saved figure
img = load_img('Bird_Layer1.png')

# print the loaded figure
pyplot.show(img)
输出-

Model: "model_17"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_17 (InputLayer)        (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
=================================================================
Total params: 1,792
Trainable params: 1,792
Non-trainable params: 0
_________________________________________________________________


希望这能回答你的问题。快乐学习。

尝试将“savefig”放在“show”之前尝试将“savefig”放在“show”之前@Sruthi Kuriakose-如果答案回答了您的问题,请您接受并投赞成票。谢谢。@Sruthi Kuriakose-如果答案回答了您的问题,请您接受并投票表决。非常感谢。