我想将用matplotlib制作的图上传到GCS。
当前代码:
from tensorflow.gfile import MakeDirs, Open
import numpy as np
import matplotlib.pyplot as plt
import datetime
_LOGDIR = "{date:%Y%m%d-%H%M%S}".format(date=datetime.datetime.now())
_PATH_LOGDIR = 'gs://{0}/logs/{1}'.format('skin_cancer_mnist', _LOGDIR)
MakeDirs(_PATH_LOGDIR))
def saving_figure(path_logdir):
data = np.arange(0, 21, 2)
fig = plt.figure(figsize=(20, 10))
plt.plot(data)
fig.savefig("{0}/accuracy_loss_graph.png".format(path_logdir))
plt.close()
saving_figure(_PATH_LOGDIR)
“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py”,第 512 行,在 print_png 中 文件名_或_obj = 打开(文件名_或_obj,'wb')
FileNotFoundError:[Errno 2]没有这样的文件或目录:'gs://skin_cancer_mnist/logs/20190116-195604/accuracy_loss_graph.png'
(该目录存在,我检查过)
我可以更改matplotlib的源代码以使用tf.Gfile.Open的Open方法,但应该有更好的选择...
Joans 2nd Option 对我不起作用,我找到了一个对我有用的解决方案:
from google.cloud import storage
import io
def saving_figure(path_logdir):
data = np.arange(0, 21, 2)
fig = plt.figure(figsize=(20, 10))
plt.plot(data)
fig_to_upload = plt.gcf()
# Save figure image to a bytes buffer
buf = io.BytesIO()
fig_to_upload.savefig(buf, format='png')
# init GCS client and upload buffer contents
client = storage.Client()
bucket = client.get_bucket('skin_cancer_mnist')
blob = bucket.blob(path_logdir)
blob.upload_from_file(buf, content_type='image/png', rewind=True)
您无法使用 python
open
函数(这是 matplotlib.pyplot.savefig
在幕后使用的函数)直接将文件上传到 Google Cloud Storage。
相反,您应该使用适用于 Python 的云存储客户端库。有关如何使用此库的详细信息,请查看此文档。这将允许您操作文件并将它们上传/下载到 GCS 等。
您必须导入此库才能使用它,您可以通过运行
pip install google-cloud-storage
来安装它并将其导入为from google.cloud import storage
。
同样,由于
plt.figure
是一个对象,而不是您要上传的实际 .png
图片,因此您也无法将其直接上传到 Google Cloud Storage。
但是您可以执行以下任一操作:
选项1:将图片保存在本地,然后上传到Google Cloud Storage:
使用您的代码:
from google.cloud import storage
def saving_figure(path_logdir):
data = np.arange(0, 21, 2)
fig = plt.figure(figsize=(20, 10))
plt.plot(data)
fig.savefig("your_local_path/accuracy_loss_graph.png".format(path_logdir))
plt.close()
# init GCS client and upload file
client = storage.Client()
bucket = client.get_bucket('skin_cancer_mnist')
blob = bucket.blob('logs/20190116-195604/accuracy_loss_graph.png') # This defines the path where the file will be stored in the bucket
your_file_contents = blob.upload_from_filename(filename="your_local_path/accuracy_loss_graph.png")
选项 2:将图中的图像结果保存到变量中,然后将其作为字符串(字节)上传到 GCS:
我发现以下 StackOverflow 答案似乎将图形图像保存到
.png
字节字符串中,但我自己还没有尝试过。
再次,基于您的代码:
from google.cloud import storage
import io
import urllib, base64
def saving_figure(path_logdir):
data = np.arange(0, 21, 2)
fig = plt.figure(figsize=(20, 10))
plt.plot(data)
fig_to_upload = plt.gcf()
# Save figure image to a bytes buffer
buf = io.BytesIO()
fig_to_upload.savefig(buf, format='png')
buf.seek(0)
image_as_a_string = base64.b64encode(buf.read())
# init GCS client and upload buffer contents
client = storage.Client()
bucket = client.get_bucket('skin_cancer_mnist')
blob = bucket.blob('logs/20190116-195604/accuracy_loss_graph.png') # This defines the path where the file will be stored in the bucket
your_file_contents = blob.upload_from_string(image_as_a_string, content_type='image/png')
编辑:这两个选项都假设您运行脚本的环境已安装 Cloud SDK,并且激活了 Google Cloud 身份验证帐户(如果还没有,您可以查看此文档,其中解释了如何操作)去做)。