python将图像保存为pdf及图片

引言:

用python进行机器学习时需要分析数据画图及结果画图需要保存结果图片,为此,本博客稍微总结了一下常用的图片数据保存,如保存图像数据为pdf.

下面是一个用pytorch搭建的LSTM对sin函数进行预测,但是这不是本博客的重点,重点是总结一下图像数据保存,虽然内容小,但是对于像我这样的新手,显然不可或缺。

# -*- coding: utf-8 -*-
"""
Created on Sat May  4 18:38:06 2019

@author: adminster
"""
#机器学习或者深度学习,拟合程度可以非常高,但是要注意其泛化能力
from __future__ import print_function
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

class Sequence(nn.Module):  #序列模型,名字能理解
    def __init__(self):
        super(Sequence, self).__init__()
        self.lstm1 = nn.LSTMCell(1, 51)
        self.lstm2 = nn.LSTMCell(51, 51)
        self.linear = nn.Linear(51, 1)

    def forward(self, input, future = 0):
        outputs = []
        h_t = torch.zeros(input.size(0), 51, dtype=torch.double)
        c_t = torch.zeros(input.size(0), 51, dtype=torch.double)
        h_t2 = torch.zeros(input.size(0), 51, dtype=torch.double)
        c_t2 = torch.zeros(input.size(0), 51, dtype=torch.double)

        for i, input_t in enumerate(input.chunk(input.size(1), dim=1)):
            print('i,input_t',i,input_t.shape)
            h_t, c_t = self.lstm1(input_t, (h_t, c_t))
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))#承接关系
            output = self.linear(h_t2)#h是记住的特征信息的状态
            print('output',output.shape)
            outputs += [output]
        for i in range(future):# if we should predict the future
            h_t, c_t = self.lstm1(output, (h_t, c_t))
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))
            output = self.linear(h_t2)
            outputs += [output]
        outputs = torch.stack(outputs, 1).squeeze(2)
        return outputs


if __name__ == '__main__':
    # set random seed to 0#查看之前结果的好方法
    np.random.seed(0)
    torch.manual_seed(0)
    # load data and make training set #
    data = torch.load('traindata.pt')
    input = torch.from_numpy(data[3:, :-1])
    #[-0.9613, -0.9738, -0.9840,  ..., -0.7353, -0.7683, -0.7993]
    target = torch.from_numpy(data[3:, 1:])
    #[-0.9738, -0.9840, -0.9917,  ..., -0.7683, -0.7993, -0.8283]
    test_input = torch.from_numpy(data[:3, :-1])
    test_target = torch.from_numpy(data[:3, 1:])
    # build the model
    seq = Sequence()
    seq.double()#double()函数来自于object类对象
    criterion = nn.MSELoss()#
    # use LBFGS as optimizer since we can load the whole data to train
    optimizer = optim.LBFGS(seq.parameters(), lr=0.8)
    ''' paramters()函数
    for name, param in self.named_parameters():
            yield param
    '''
    #begin to train
    for i in range(15):
        print('STEP: ', i)
        def closure():
            optimizer.zero_grad()
            out = seq(input)#因为权重是随机的,当然就会是很错误的数据
            loss = criterion(out, target)
            print('loss:', loss.item())
            loss.backward()
            return loss
        optimizer.step(closure)
        # begin to predict, no need to track gradient here
        with torch.no_grad():
            future = 1000
            pred = seq(test_input, future=future)#用训练好了的网络去预测
            loss = criterion(pred[:, :-future], test_target)
            print('test loss:', loss.item())
            y = pred.detach().numpy()
        # draw the result
        plt.figure(figsize=(30,10))
        plt.title('Predict future values for time sequences\n(Dashlines are predicted values)', fontsize=30)
        plt.xlabel('x', fontsize=20)
        plt.ylabel('y', fontsize=20)
        plt.xticks(fontsize=20)
        plt.yticks(fontsize=20)
        def draw(yi, color):
            plt.plot(np.arange(input.size(1)), yi[:input.size(1)], color, linewidth = 2.0)
            plt.plot(np.arange(input.size(1), input.size(1) + future), yi[input.size(1):], color + ':', linewidth = 2.0)
        draw(y[0], 'r')
        draw(y[1], 'g')
        draw(y[2], 'b')
        #加一个文件夹
        import time ,os #获取日期
        time1=time.strftime('%Y-%m-%d')
        sv_path='pre_data/'+time1
        os.makedirs(sv_path,exist_ok=True)
        plt.savefig(f'{sv_path}/predict%d.pdf'%i)#保存文件在指定文件夹下很方便
        plt.close()
  #加一个文件夹
        import time ,os #获取日期
        time1=time.strftime('%Y-%m-%d')
        sv_path='pre_data/'+time1
        os.makedirs(sv_path,exist_ok=True)
        plt.savefig(f'{sv_path}/predict%d.pdf'%i)#保存文件在指定文件夹下很方便
        plt.close()

主要讲解一下这么一小段代码,高手轻拍,主要引入了两个包,time,os分别用来获取当前时间和创建文件夹,保存数据时这两步是必要操作,sv_path='pre_data/'+time1 用于获取年月日,

sv_path='pre_data/'+time1

os.makedirs(sv_path,exist_ok=True)创建多层文件夹目录,用于存放数据

plt.savefig(f'{sv_path}/predict%d.pdf'%i)#保存文件在指定文件夹下很方便

plt.close()调用matplotlib库中已有的函数savefig(path).里面可以保存tiff/png/jpg...等格式


 

发布了21 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_40405758/article/details/92001355