在使用 matplotlib 在其上绘制一个框后,如何保存从 tfrecord 解码的图像?
How do I save my image which is decoded from tfrecord after plotting a box on it using matplotlib?
img = tf.image.decode_jpeg(camera_image)
plt.imshow(img, cmap=cmap)
title = camera_name
plt.title(title)
plt.show()
plt.savefig('image.png')
plt.savefig()
不起作用。保存的图片是空白的。
但是 plt.show()
清楚地显示了一张带有边界框的图像
这是一个示例,其中我从 VGG16 模型的第 1 层块中保存特征图。稍后我加载相同的图像并绘制它。
我正在使用 plt.savefig()
保存图像,稍后使用 load_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
_________________________________________________________________
希望这能回答您的问题。快乐学习。
img = tf.image.decode_jpeg(camera_image)
plt.imshow(img, cmap=cmap)
title = camera_name
plt.title(title)
plt.show()
plt.savefig('image.png')
plt.savefig()
不起作用。保存的图片是空白的。
但是 plt.show()
清楚地显示了一张带有边界框的图像
这是一个示例,其中我从 VGG16 模型的第 1 层块中保存特征图。稍后我加载相同的图像并绘制它。
我正在使用 plt.savefig()
保存图像,稍后使用 load_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
_________________________________________________________________
希望这能回答您的问题。快乐学习。