基于深度强化学习的绘画智能体 代码分析
Github源码链接
ddpg.py
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam, SGD
from Renderer.model import *
from DRL.rpm import rpm
from DRL.actor import *
from DRL.critic import *
from DRL.wgan import *
from utils.util import *
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
coord = torch.zeros([1, 2, 128, 128]) #返回一个形状为[1, 2, 128, 128]的矩阵,里面的每一个值都是0的tensor
for i in range(128):
for j in range(128):
coord[0, 0, i, j] = i / 127.
coord[0, 1, i, j] = j / 127.
coord = coord.to(device)
criterion = nn.MSELoss() #均方损失函数
Decoder = FCN() #不知道,可能是FCN(fully convolution net),FCN对图像进行像素级的分类,从而解决了语义级别的图像分割(semantic segmentation)问题
Decoder.load_state_dict(torch.load('../renderer.pkl')) # 加载这个模型
def decode(x, canvas): #b * (10 + 3)
x = x.view(-1, 10 + 3) #view函数相当于numpy的reshape
stroke = 1 - Decoder(x[:, :10]) #译码器
stroke = stroke.view(-1, 128, 128, 1)
color_stroke = stroke * x[:, -3:].view(-1, 1, 1, 3)
stroke = stroke.permute(0, 3, 1, 2) #tensor的维度变换
color_stroke = color_stroke.permute(0, 3, 1, 2)
stroke = stroke.view(-1, 5, 1, 128, 128)
color_stroke = color_stroke.view(-1, 5, 3, 128, 128)
for i in range(5):
canvas = canvas * (1 - stroke[:, i]) + color_stroke[:, i