台州网站搜索引擎优化,网站建设花费,域名网站怎么做的,鲨皇seo目录 数据集下载 数据处理
构建生成器
构建判别器
模型训练
结果展示 数据集下载 首先尝试卸载已安装的 mindspore 库#xff0c;然后通过指定的镜像源安装特定版本#xff08;2.2.14#xff09;的 mindspore 库。从指定的 URL 下载一个 zip 文件到当前目录下的 ./faces…目录 数据集下载 数据处理
构建生成器
构建判别器
模型训练
结果展示 数据集下载 首先尝试卸载已安装的 mindspore 库然后通过指定的镜像源安装特定版本2.2.14的 mindspore 库。从指定的 URL 下载一个 zip 文件到当前目录下的 ./faces 文件夹中并指定如果存在同名文件则进行替换最后将下载文件的路径存储在 path 变量中。 代码如下
%%capture captured_output # 实验环境已经预装了mindspore2.2.14如需更换mindspore版本可更改下面mindspore的版本号 !pip uninstall mindspore -y !pip install -i https://pypi.mirrors.ustc.edu.cn/simple mindspore2.2.14 from download import download url https://download.mindspore.cn/dataset/Faces/faces.zip path download(url, ./faces, kindzip, replaceTrue) 数据处理 第一步为执行过程定义一些输入 代码如下
batch_size 128 # 批量大小
image_size 64 # 训练图像空间大小
nc 3 # 图像彩色通道数
nz 100 # 隐向量的长度
ngf 64 # 特征图在生成器中的大小
ndf 64 # 特征图在判别器中的大小
num_epochs 3 # 训练周期数
lr 0.0002 # 学习率
beta1 0.5 # Adam优化器的beta1超参数 第二步定义create_dataset_imagenet函数对数据进行处理和增强操作。 代码如下
import numpy as np
import mindspore.dataset as ds
import mindspore.dataset.vision as vision
def create_dataset_imagenet(dataset_path): 数据加载 dataset ds.ImageFolderDataset(dataset_path, num_parallel_workers4, shuffleTrue, decodeTrue) # 数据增强操作 transforms [ vision.Resize(image_size), vision.CenterCrop(image_size), vision.HWC2CHW(), lambda x: ((x / 255).astype(float32)) ] # 数据映射操作 dataset dataset.project(image) dataset dataset.map(transforms, image) # 批量操作 dataset dataset.batch(batch_size) return dataset
dataset create_dataset_imagenet(./faces) 分析首先导入了所需的库包括 numpy 、mindspore 中的数据集模块 ds 和数据视觉处理模块 vision 。 定义了一个名为 create_dataset_imagenet 的函数用于从指定路径加载图像数据。使用 ImageFolderDataset 类读取数据设置了并行工作数为 4 数据随机打乱以及进行解码。 定义了一系列的数据增强和处理操作包括调整大小、中心裁剪、格式转换和归一化。 对数据集进行映射操作只选择 image 列并应用之前定义的数据处理操作。 将数据集进行批量处理最后返回处理后的数据集。 调用 create_dataset_imagenet 函数并传入 ./faces 路径获取处理后的数据集并存储在 dataset 变量中。 第三步通过create_dict_iterator函数将数据转换成字典迭代器然后使用matplotlib模块可视化部分训练数据。 代码如下
import matplotlib.pyplot as plt
def plot_data(data): # 可视化部分训练数据 plt.figure(figsize(10, 3), dpi140) for i, image in enumerate(data[0][:30], 1): plt.subplot(3, 10, i) plt.axis(off) plt.imshow(image.transpose(1, 2, 0)) plt.show()
sample_data next(dataset.create_tuple_iterator(output_numpyTrue))
plot_data(sample_data) 分析导入 matplotlib.pyplot 库并简称为 plt 用于数据可视化。 定义了一个名为 plot_data 的函数用于绘制数据。函数内部首先创建一个图形设置其大小和分辨率。然后通过循环将输入数据中的前 30 个图像分别绘制在 3 行 10 列的子图中关闭坐标轴并显示图像。最后显示整个图形。 从数据集的迭代器中获取下一个数据项并将其传递给 plot_data 函数进行可视化展示。 运行结果 构建生成器 以下是生成器的代码实现 代码如下
import mindspore as ms
from mindspore import nn, ops
from mindspore.common.initializer import Normal
weight_init Normal(mean0, sigma0.02)
gamma_init Normal(mean1, sigma0.02)
class Generator(nn.Cell): DCGAN网络生成器 def __init__(self): super(Generator, self).__init__() self.generator nn.SequentialCell( nn.Conv2dTranspose(nz, ngf * 8, 4, 1, valid, weight_initweight_init), nn.BatchNorm2d(ngf * 8, gamma_initgamma_init), nn.ReLU(), nn.Conv2dTranspose(ngf * 8, ngf * 4, 4, 2, pad, 1, weight_initweight_init), nn.BatchNorm2d(ngf * 4, gamma_initgamma_init), nn.ReLU(), nn.Conv2dTranspose(ngf * 4, ngf * 2, 4, 2, pad, 1, weight_initweight_init), nn.BatchNorm2d(ngf * 2, gamma_initgamma_init), nn.ReLU(), nn.Conv2dTranspose(ngf * 2, ngf, 4, 2, pad, 1, weight_initweight_init), nn.BatchNorm2d(ngf, gamma_initgamma_init), nn.ReLU(), nn.Conv2dTranspose(ngf, nc, 4, 2, pad, 1, weight_initweight_init), nn.Tanh() ) def construct(self, x): return self.generator(x)
generator Generator() 分析首先导入了 mindspore 相关的模块并定义了两种初始化方式 weight_init 和 gamma_init 。 定义了一个名为 Generator 的类继承自 nn.Cell 。在 __init__ 方法中使用 nn.SequentialCell 构建了生成器的网络结构包含一系列的反卷积、批归一化和激活函数层。 construct 方法定义了生成器的前向传播逻辑即输入数据 x 经过之前构建的网络结构进行处理。 最后实例化了 Generator 类创建了一个生成器对象 generator 。 构建判别器 以下是判别器的代码实现 代码如下
class Discriminator(nn.Cell): DCGAN网络判别器 def __init__(self): super(Discriminator, self).__init__() self.discriminator nn.SequentialCell( nn.Conv2d(nc, ndf, 4, 2, pad, 1, weight_initweight_init), nn.LeakyReLU(0.2), nn.Conv2d(ndf, ndf * 2, 4, 2, pad, 1, weight_initweight_init), nn.BatchNorm2d(ngf * 2, gamma_initgamma_init), nn.LeakyReLU(0.2), nn.Conv2d(ndf * 2, ndf * 4, 4, 2, pad, 1, weight_initweight_init), nn.BatchNorm2d(ngf * 4, gamma_initgamma_init), nn.LeakyReLU(0.2), nn.Conv2d(ndf * 4, ndf * 8, 4, 2, pad, 1, weight_initweight_init), nn.BatchNorm2d(ngf * 8, gamma_initgamma_init), nn.LeakyReLU(0.2), nn.Conv2d(ndf * 8, 1, 4, 1, valid, weight_initweight_init), ) self.adv_layer nn.Sigmoid() def construct(self, x): out self.discriminator(x) out out.reshape(out.shape[0], -1) return self.adv_layer(out) discriminator Discriminator() 分析定义了一个名为 Discriminator 的类继承自 nn.Cell 。在 __init__ 方法中使用 nn.SequentialCell 构建了判别器的网络结构包含一系列的卷积、批归一化、激活函数层。同时定义了一个 nn.Sigmoid 层 adv_layer 。 construct 方法定义了判别器的前向传播逻辑输入数据 x 经过判别器网络处理后进行形状重塑然后通过 adv_layer 即 Sigmoid 函数进行输出。 最后实例化了 Discriminator 类创建了一个判别器对象 discriminator 。 模型训练 第一步定义损失函数 代码如下
# 定义损失函数
adversarial_loss nn.BCELoss(reductionmean) 分析定义了一个损失函数 adversarial_loss 使用的是 mindspore 中的 nn.BCELoss 即二分类交叉熵损失函数。reductionmean 表示对损失进行平均计算即最终的损失值是所有样本损失的平均值。 第二步这里设置了两个单独的优化器一个用于D另一个用于G。这两个都是lr 0.0002和beta1 0.5的Adam优化器。 代码如下
# 为生成器和判别器设置优化器
optimizer_D nn.Adam(discriminator.trainable_params(), learning_ratelr, beta1beta1)
optimizer_G nn.Adam(generator.trainable_params(), learning_ratelr, beta1beta1)
optimizer_G.update_parameters_name(optim_g.)
optimizer_D.update_parameters_name(optim_d.) 分析创建了一个 Adam 优化器 optimizer_D 用于优化判别器 discriminator 的可训练参数。lr 是学习率beta1 是 Adam 优化器的一个参数。 创建了一个 Adam 优化器 optimizer_G 用于优化生成器 generator 的可训练参数使用的学习率和 beta1 与判别器的优化器相同。 分别为生成器和判别器的优化器更新了参数名称将生成器优化器的参数名称更新为 optim_g. 判别器优化器的参数名称更新为 optim_d. 。这样可以更清晰地区分和管理不同模型部分的优化器参数。 第三步实现模型训练正向逻辑 代码如下
def generator_forward(real_imgs, valid): # 将噪声采样为发生器的输入 z ops.standard_normal((real_imgs.shape[0], nz, 1, 1)) # 生成一批图像 gen_imgs generator(z) # 损失衡量发生器绕过判别器的能力 g_loss adversarial_loss(discriminator(gen_imgs), valid) return g_loss, gen_imgs
def discriminator_forward(real_imgs, gen_imgs, valid, fake): # 衡量鉴别器从生成的样本中对真实样本进行分类的能力 real_loss adversarial_loss(discriminator(real_imgs), valid) fake_loss adversarial_loss(discriminator(gen_imgs), fake) d_loss (real_loss fake_loss) / 2 return d_loss
grad_generator_fn ms.value_and_grad(generator_forward, None, optimizer_G.parameters, has_auxTrue)
grad_discriminator_fn ms.value_and_grad(discriminator_forward, None, optimizer_D.parameters) ms.jit
def train_step(imgs): valid ops.ones((imgs.shape[0], 1), mindspore.float32) fake ops.zeros((imgs.shape[0], 1), mindspore.float32) (g_loss, gen_imgs), g_grads grad_generator_fn(imgs, valid) optimizer_G(g_grads) d_loss, d_grads grad_discriminator_fn(imgs, gen_imgs, valid, fake) optimizer_D(d_grads) return g_loss, d_loss, gen_imgs 分析定义了 generator_forward 函数它首先生成噪声作为生成器的输入并得到生成的图像 gen_imgs 然后通过判别器对生成图像的判断结果和有效的标签 valid 计算生成器的损失 g_loss 最后返回损失和生成的图像。 定义了 discriminator_forward 函数分别计算判别器对真实图像和生成图像的损失然后取平均得到判别器的损失 d_loss 并返回。 使用 mindspore 的 value_and_grad 函数为生成器和判别器的前向传播计算梯度。 定义了 train_step 函数首先创建有效的标签 valid 和假的标签 fake 。然后通过之前定义的梯度计算函数计算生成器的梯度和损失以及生成的图像使用生成器的优化器更新参数。接着计算判别器的梯度和损失使用判别器的优化器更新参数。最后返回生成器和判别器的损失以及生成的图像。这个函数被 ms.jit 装饰可能会进行即时编译以提高性能。 第四步循环训练网络每经过50次迭代就收集生成器和判别器的损失以便于后面绘制训练过程中损失函数的图像。 代码如下
import mindspore
G_losses []
D_losses []
image_list []
total dataset.get_dataset_size()
for epoch in range(num_epochs): generator.set_train() discriminator.set_train() # 为每轮训练读入数据 for i, (imgs, ) in enumerate(dataset.create_tuple_iterator()): g_loss, d_loss, gen_imgs train_step(imgs) if i % 100 0 or i total - 1: # 输出训练记录 print([%2d/%d][%3d/%d] Loss_D:%7.4f Loss_G:%7.4f % ( epoch 1, num_epochs, i 1, total, d_loss.asnumpy(), g_loss.asnumpy())) D_losses.append(d_loss.asnumpy()) G_losses.append(g_loss.asnumpy()) # 每个epoch结束后使用生成器生成一组图片 generator.set_train(False) fixed_noise ops.standard_normal((batch_size, nz, 1, 1)) img generator(fixed_noise) image_list.append(img.transpose(0, 2, 3, 1).asnumpy()) # 保存网络模型参数为ckpt文件 mindspore.save_checkpoint(generator, ./generator.ckpt) mindspore.save_checkpoint(discriminator, ./discriminator.ckpt) 分析导入 mindspore 库并初始化三个列表 G_losses 用于存储生成器的损失值D_losses 用于存储判别器的损失值image_list 用于存储生成的图像。 获取数据集的大小。 开始训练的循环每个循环代表一个训练轮次并设置生成器和判别器为训练模式。 在每一轮训练中通过数据集的迭代器读取数据并执行 train_step 函数进行训练获取生成器和判别器的损失值。每隔 100 步或在最后一步时打印训练记录并将损失值添加到相应的列表中。 在每个训练轮次结束后将生成器设置为评估模式生成一组固定噪声的图像并将其添加到 image_list 中。 保存生成器和判别器的模型参数为 .ckpt 文件。 运行结果
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.707.251 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.707.394 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.707.485 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.734.400 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.734.917 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.734.995 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.055 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.113 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.163 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.215 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.269 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.320 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.394 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.735.451 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.737.401 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.737.463 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.739.812 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.739.889 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.740.014 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.740.090 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.740.201 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.740.273 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.776.259 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.776.536 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.776.707 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:31:01.776.882 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ 1/3][ 1/549] Loss_D: 0.8259 Loss_G: 0.6072
[ 1/3][101/549] Loss_D: 0.0870 Loss_G: 4.1363
[ 1/3][201/549] Loss_D: 0.2487 Loss_G: 2.2273
[ 1/3][301/549] Loss_D: 0.3117 Loss_G: 1.3479
[ 1/3][401/549] Loss_D: 0.1768 Loss_G: 7.7851
[ 1/3][501/549] Loss_D: 0.2311 Loss_G: 4.5245
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.438.749 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.438.847 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.438.904 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.452.389 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.452.767 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.452.836 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.452.892 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.452.939 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.452.986 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.453.033 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.453.079 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.453.125 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.453.171 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.453.216 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.454.872 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.454.926 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.456.830 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.456.896 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.457.006 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.457.073 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.457.138 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.457.203 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.475.414 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.475.658 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.475.813 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ERROR] CORE(225,ffffb31d7930,python):2024-07-16-04:32:12.475.966 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_225/2743497794.py]
[ 1/3][549/549] Loss_D: 0.4179 Loss_G: 0.9930
[ 2/3][ 1/549] Loss_D: 0.3010 Loss_G: 4.7594
[ 2/3][101/549] Loss_D: 0.5035 Loss_G:10.4138
[ 2/3][201/549] Loss_D: 0.9403 Loss_G:14.3117
[ 2/3][301/549] Loss_D: 0.2579 Loss_G: 2.4129
[ 2/3][401/549] Loss_D: 0.2723 Loss_G: 1.6250
[ 2/3][501/549] Loss_D: 0.3616 Loss_G: 4.8121
[ 2/3][549/549] Loss_D: 0.3363 Loss_G: 1.1764
[ 3/3][ 1/549] Loss_D: 0.2645 Loss_G: 3.3743
[ 3/3][101/549] Loss_D: 0.5911 Loss_G: 6.1797
[ 3/3][201/549] Loss_D: 0.1594 Loss_G: 2.5986
[ 3/3][301/549] Loss_D: 0.2017 Loss_G: 3.4795
[ 3/3][401/549] Loss_D: 0.8796 Loss_G: 5.3484
[ 3/3][501/549] Loss_D: 0.1958 Loss_G: 1.9727
[ 3/3][549/549] Loss_D: 0.8177 Loss_G: 7.9810 结果展示 代码如下
plt.figure(figsize(10, 5))
plt.title(Generator and Discriminator Loss During Training)
plt.plot(G_losses, labelG, colorblue)
plt.plot(D_losses, labelD, colororange)
plt.xlabel(iterations)
plt.ylabel(Loss)
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def showGif(image_list): show_list [] fig plt.figure(figsize(8, 3), dpi120) for epoch in range(len(image_list)): images [] for i in range(3): row np.concatenate((image_list[epoch][i * 8:(i 1) * 8]), axis1) images.append(row) img np.clip(np.concatenate((images[:]), axis0), 0, 1) plt.axis(off) show_list.append([plt.imshow(img)]) ani animation.ArtistAnimation(fig, show_list, interval1000, repeat_delay1000, blitTrue) ani.save(./dcgan.gif, writerpillow, fps1)
showGif(image_list)
# 从文件中获取模型参数并加载到网络中
mindspore.load_checkpoint(./generator.ckpt, generator) fixed_noise ops.standard_normal((batch_size, nz, 1, 1))
img64 generator(fixed_noise).transpose(0, 2, 3, 1).asnumpy() fig plt.figure(figsize(8, 3), dpi120)
images []
for i in range(3): images.append(np.concatenate((img64[i * 8:(i 1) * 8]), axis1))
img np.clip(np.concatenate((images[:]), axis0), 0, 1)
plt.axis(off)
plt.imshow(img)
plt.show() 分析创建一个图形设置标题为“Generator and Discriminator Loss During Training”绘制生成器和判别器的损失曲线分别用蓝色和橙色表示并添加坐标轴标签和图例后显示图形。 定义了一个名为 showGif 的函数用于将生成的图像列表制作成 GIF 动画。在函数内部创建图形并处理图像然后使用 animation.ArtistAnimation 生成动画并保存为 GIF 文件。 从指定的文件中加载生成器的模型参数。 生成固定的噪声并通过生成器得到生成的图像然后进行维度转换。 创建图形处理生成的图像显示最终的图像。 运行结果