<kaggle_start><code># 生成data文件
import os
from os.path import join, isfile
import numpy as np
import h5py
from glob import glob

# from torch.utils.serialization import load_lua
import torchfile
from PIL import Image
import yaml
import io
import pdb

# images_path ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/CUB_200_2011/images'
# embedding_path ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml'
# text_path ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cvpr2016_cub/text_c10'
# val_classes ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml/valclasses.txt'
# train_classes ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml/trainclasses.txt'
# test_classes ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml/testclasses.txt'
images_path = (
    "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/CUB_200_2011/images"
)
embedding_path = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml"
text_path = (
    "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cvpr2016_cub/text_c10"
)
val_classes = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml/valclasses.txt"
train_classes = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml/trainclasses.txt"
test_classes = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml/testclasses.txt"
datasetDir = "/kaggle/working/birds.hdf5"
# val_classes = open(val_classes).read()
# train_classes = open(train_classes).read()
# test_classes = open(test_classes).read()
val_classes = open(val_classes).read().splitlines()
train_classes = open(train_classes).read().splitlines()
test_classes = open(test_classes).read().splitlines()
# print(val_classes)
# print(val_classes2)
if os.path.exists(datasetDir):
    os.remove(datasetDir)
f = h5py.File(datasetDir, "w")
train = f.create_group("train")
valid = f.create_group("valid")
test = f.create_group("test")
# print(f)
# print(train)
# print(test)
for _class in sorted(os.listdir(embedding_path)):
    split = ""
    if _class in train_classes:
        split = train
    elif _class in val_classes:
        split = valid
    elif _class in test_classes:
        split = test
    # print(_class)
    # print(type(_class))
    # print(train_classes)
    # print(type(train_classes))
    # print(split)
    # else:#它还会遍历到文件夹下的.txt文件
    # print(_class)
    data_path = os.path.join(embedding_path, _class)  # 获取数据路径
    txt_path = os.path.join(text_path, _class)
    # zip将对象中对应的元素打包成一个个元组，然后返回由这些元组组成的列表
    # example遍历cub_icml的叶子文件
    # txt_file遍历cvpr2016_cub的叶子文件
    for example, txt_file in zip(
        sorted(glob(data_path + "/*.t7")), sorted(glob(txt_path + "/*.txt"))
    ):
        example_data = torchfile.load(example)  # 加载t7文件
        # print(example_data)
        # print(type(example_data))#查看是什么类型，实际上是一个字典
        # print(example_data.keys())#查看有什么键
        img_path = example_data[b"img"]
        # print(img_path)
        embeddings = example_data[b"txt"]
        # print(embeddings.shape)10*1024
        # print(type(embeddings))#numpy类型
        # print(embeddings)
        img_path = str(img_path, "utf-8")  # 将bytes转化为字符串
        example_name = img_path.split("/")[-1][:-4]
        # print(example_name)
        f = open(txt_file, "r")  # txt路径
        txt = f.readlines()  # txt的英文描述
        f.close()
        img_path = os.path.join(images_path, img_path)  # 图片路径
        img = open(img_path, "rb").read()  # 图片以二进制方式读取
        txt_choice = np.random.choice(range(10), 5)  # 随机从10个里面选5个
        embeddings = embeddings[txt_choice]  # 从embeddings里面读取文件
        txt = np.array(txt)  # 转化为np类型
        txt = txt[txt_choice]
        dt = h5py.special_dtype(vlen=str)  # object
        for c, e in enumerate(embeddings):  # 将embeddings转为一个可以遍历的索引序列
            # 这里split是split()的意思，split在前面的split对应
            ex = split.create_group(example_name + "_" + str(c))
            ex.create_dataset("name", data=example_name)
            ex.create_dataset("img", data=np.void(img))
            ex.create_dataset("embeddings", data=e)
            ex.create_dataset("class", data=_class)
            ex.create_dataset("txt", data=txt[c].astype(object), dtype=dt)
        # print(example_name)
print("end")
# gan.py
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.ngf = 64
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.noise_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, z):
        return self.netG(z)


class discriminator(nn.Module):
    def __init__(self):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.ndf = 64
        self.B_dim = 128
        self.C_dim = 16
        self.netD_1 = nn.Sequential(
            # input is (nc) x 64 x 64
            nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x 8 x 8
            nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
        )
        self.netD_2 = nn.Sequential(
            # state size. (ndf*8) x 4 x 4
            nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Sigmoid(),
        )

    def forward(self, inp):
        x_intermediate = self.netD_1(inp)
        output = self.netD_2(x_intermediate)
        return output.view(-1, 1).squeeze(1), x_intermediate


# wgan.py
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

# from utils import Concat_embed, minibatch_discriminator
import pdb


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.ngf = 64
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.noise_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, z):
        output = self.netG(z)
        return output


class discriminator(nn.Module):
    def __init__(self, improved=False):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.ndf = 64
        if improved:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        else:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 2),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 4),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 8),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        self.netD_2 = nn.Sequential(
            # nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False)
        )

    def forward(self, inp):
        x_intermediate = self.netD_1(inp)
        x = self.netD_2(x_intermediate)
        x = x.mean(0)
        return x.view(1), x_intermediate


# visualize.py
from visdom import Visdom
import numpy as np
import torchvision
from PIL import ImageDraw, Image, ImageFont
import torch
import pdb


class VisdomPlotter(object):
    """Plots to Visdom"""

    def __init__(self, env_name="gan"):
        # 这里是大量出现警告的地方
        self.viz = Visdom()
        self.env = env_name
        self.plots = {}

    def plot(self, var_name, split_name, x, y, xlabel="epoch"):
        if var_name not in self.plots:
            self.plots[var_name] = self.viz.line(
                X=np.array([x, x]),
                Y=np.array([y, y]),
                env=self.env,
                opts=dict(
                    legend=[split_name], title=var_name, xlabel=xlabel, ylabel=var_name
                ),
            )
        else:
            self.viz.updateTrace(
                X=np.array([x]),
                Y=np.array([y]),
                env=self.env,
                win=self.plots[var_name],
                name=split_name,
            )

    def draw(self, var_name, images):
        if var_name not in self.plots:  # 这里出现大量警告
            print("pause11")
            # print(var_name)
            # a=self.viz.images(images)#gan
            # self.plots[var_name] = self.viz.images(images, env=self.env)
            # print("pause12")
            # input()
        else:
            self.viz.images(images, env=self.env, win=self.plots[var_name])


# import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

# from utils import Concat_embed, minibatch_discriminator
import pdb


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.latent_dim = self.noise_dim + self.projected_embed_dim
        self.ngf = 64
        self.projection = nn.Sequential(
            nn.Linear(
                in_features=self.embed_dim, out_features=self.projected_embed_dim
            ),
            nn.BatchNorm1d(num_features=self.projected_embed_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
        )
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, embed_vector, z):
        projected_embed = self.projection(embed_vector).unsqueeze(2).unsqueeze(3)
        latent_vector = torch.cat([projected_embed, z], 1)
        output = self.netG(latent_vector)
        return output


class discriminator(nn.Module):
    def __init__(self, improved=False):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.ndf = 64
        if improved:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        else:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 2),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 4),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 8),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        self.projector = Concat_embed(self.embed_dim, self.projected_embed_dim)
        self.netD_2 = nn.Sequential(
            # nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Conv2d(self.ndf * 8 + self.projected_embed_dim, 1, 4, 1, 0, bias=False)
        )

    def forward(self, inp, embed):
        x_intermediate = self.netD_1(inp)
        x = self.projector(x_intermediate, embed)
        x = self.netD_2(x)
        x = x.mean(0)
        return x.view(1), x_intermediate


# gan_cls.py
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

# from utils import Concat_embed
import pdb
import yaml
from torch import nn

# from torch.utils.data import DataLoader
# from models.gan_factory import gan_factory
# from utils import Utils, Logger
from PIL import Image
import os

# utils.py
import numpy as np
from torch import autograd

# from visualize import VisdomPlotter
import os
import pdb


class Concat_embed(nn.Module):
    def __init__(self, embed_dim, projected_embed_dim):
        super(Concat_embed, self).__init__()
        self.projection = nn.Sequential(
            nn.Linear(in_features=embed_dim, out_features=projected_embed_dim),
            nn.BatchNorm1d(num_features=projected_embed_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
        )

    def forward(self, inp, embed):
        projected_embed = self.projection(embed)
        replicated_embed = projected_embed.repeat(4, 4, 1, 1).permute(2, 3, 0, 1)
        hidden_concat = torch.cat([inp, replicated_embed], 1)
        return hidden_concat


class minibatch_discriminator(nn.Module):
    def __init__(self, num_channels, B_dim, C_dim):
        super(minibatch_discriminator, self).__init__()
        self.B_dim = B_dim
        self.C_dim = C_dim
        self.num_channels = num_channels
        T_init = torch.randn(num_channels * 4 * 4, B_dim * C_dim) * 0.1
        self.T_tensor = nn.Parameter(T_init, requires_grad=True)

    def forward(self, inp):
        inp = inp.view(-1, self.num_channels * 4 * 4)
        M = inp.mm(self.T_tensor)
        M = M.view(-1, self.B_dim, self.C_dim)
        op1 = M.unsqueeze(3)
        op2 = M.permute(1, 2, 0).unsqueeze(0)
        output = torch.sum(torch.abs(op1 - op2), 2)
        output = torch.sum(torch.exp(-output), 2)
        output = output.view(M.size(0), -1)
        output = torch.cat((inp, output), 1)
        return output


class Utils(object):
    @staticmethod
    def smooth_label(tensor, offset):
        return tensor + offset

    @staticmethod
    # based on:  https://github.com/caogang/wgan-gp/blob/master/gan_cifar10.py
    def compute_GP(netD, real_data, real_embed, fake_data, LAMBDA):
        BATCH_SIZE = real_data.size(0)
        alpha = torch.rand(BATCH_SIZE, 1)
        alpha = (
            alpha.expand(BATCH_SIZE, int(real_data.nelement() / BATCH_SIZE))
            .contiguous()
            .view(BATCH_SIZE, 3, 64, 64)
        )
        alpha = alpha.cuda()
        interpolates = alpha * real_data + ((1 - alpha) * fake_data)
        interpolates = interpolates.cuda()
        interpolates = autograd.Variable(interpolates, requires_grad=True)
        disc_interpolates, _ = netD(interpolates, real_embed)
        gradients = autograd.grad(
            outputs=disc_interpolates,
            inputs=interpolates,
            grad_outputs=torch.ones(disc_interpolates.size()).cuda(),
            create_graph=True,
            retain_graph=True,
            only_inputs=True,
        )[0]
        gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
        return gradient_penalty

    @staticmethod
    def save_checkpoint(netD, netG, dir_path, subdir_path, epoch):
        # 暂时不保存模型
        pass

    #         #print("pause14")
    #         #保存模型
    #         #path =  os.path.join(dir_path, subdir_path)
    #         path='/kaggle/working'
    # #         if not os.path.exists(path):
    # #             os.makedirs(path)
    # #             print("no")
    #         torch.save(netD.state_dict(), '{0}/disc_{1}.pth'.format(path, epoch))
    #         torch.save(netG.state_dict(), '{0}/gen_{1}.pth'.format(path, epoch))
    @staticmethod
    def weights_init(m):
        classname = m.__class__.__name__
        if classname.find("Conv") != -1:
            m.weight.data.normal_(0.0, 0.02)
        elif classname.find("BatchNorm") != -1:
            m.weight.data.normal_(1.0, 0.02)
            m.bias.data.fill_(0)


class Logger(object):
    def __init__(self, vis_screen):
        # 这里是大量警告出现的地方
        # print(vis_screen)#gan
        # self.viz = VisdomPlotter(env_name=vis_screen)
        self.hist_D = []
        self.hist_G = []
        self.hist_Dx = []
        self.hist_DGx = []
        self.i = 0

    def log_iteration_wgan(
        self, epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss
    ):
        print(
            "Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f"
            % (
                epoch,
                gen_iteration,
                d_loss.data.cpu().mean(),
                g_loss.data.cpu().mean(),
                real_loss,
                fake_loss,
            )
        )
        self.hist_D.append(d_loss.data.cpu().mean())
        self.hist_G.append(g_loss.data.cpu().mean())

    def log_iteration_gan(self, epoch, d_loss, g_loss, real_score, fake_score):
        # 输出训练成果
        # print("pause13")
        print(
            "Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f"
            % (
                epoch,
                d_loss.data.cpu().mean(),
                g_loss.data.cpu().mean(),
                real_score.data.cpu().mean(),
                fake_score.data.cpu().mean(),
            )
        )
        # 每次训练以后把结果放到对应list里面
        self.hist_D.append(d_loss.data.cpu().mean())
        self.hist_G.append(g_loss.data.cpu().mean())
        self.hist_Dx.append(real_score.data.cpu().mean())
        self.hist_DGx.append(fake_score.data.cpu().mean())

    def plot_epoch(self, epoch):
        self.viz.plot("Discriminator", "train", epoch, np.array(self.hist_D).mean())
        self.viz.plot("Generator", "train", epoch, np.array(self.hist_G).mean())
        self.hist_D = []
        self.hist_G = []

    def plot_epoch_w_scores(self, epoch):
        # viz的全部屏蔽掉
        # 绘图
        # 求平均值
        #         print(np.array(self.hist_D).mean())
        #         print(np.array(self.hist_G).mean())
        #         print(np.array(self.hist_Dx).mean())
        #         print(np.array(self.hist_DGx).mean())
        # self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean())
        # self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean())
        # self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean())
        # self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean())
        path1 = r"/kaggle/working/Discriminator.txt"
        path2 = r"/kaggle/working/Generator.txt"
        path3 = r"/kaggle/working/D(X).txt"
        path4 = r"/kaggle/working/D(G(X)).txt"
        with open(path1, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_D).mean()))
            f.write("\n")
        with open(path2, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_G).mean()))
            f.write("\n")
        with open(path3, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_Dx).mean()))
            f.write("\n")
        with open(path4, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_DGx).mean()))
            f.write("\n")
        #         with open(path,"a") as f:
        #             f.write(str(i))
        #             f.write('\n')
        self.hist_D = []
        self.hist_G = []
        self.hist_Dx = []
        self.hist_DGx = []

    def draw(self, right_images, fake_images):
        # 在Image转Tensor过程中，图片的格式会由: H * W * C的格式转为: C * H * W格式。
        # 输出图片(64*3*64*64)意思是64*64*3的彩色图 第一个64对应的batchsize为64
        # a=right_images.numpy()
        # a=right_images.cpu().numpy()
        # a=right_images.cpu().detach().numpy()
        # a0=a[0]
        # d=np.transpose(a0)
        # c = np.transpose(a)
        i = self.i
        if i % 500 == 0:
            a = right_images.data.cpu().numpy()[:64] * 128 + 128
            np.save("/kaggle/working/npy/" + str(i) + "a.npy", a)
            b = fake_images.data.cpu().numpy()[:64] * 128 + 128  # 生成的是灰色的图片
            np.save("/kaggle/working/npy/" + str(i) + "b.npy", b)
        self.i += 1
        # print("ok")
        # input()
        # return
        # print(c.shape)
        # c0=c[0]
        # print(d.shape)
        # print(c0.shape)
        # plt.imshow("d", d)
        # plt.show()
        # input()
        # print("pause9")
        # 这两行代码产生大量的警告
        # self.viz.draw('generated images', fake_images.data.cpu().numpy()[:64] * 128 + 128)
        # input()
        # print("pause10")
        # self.viz.draw('real images', right_images.data.cpu().numpy()[:64] * 128 + 128)


#########################################################
class gan_factory(object):
    @staticmethod
    def generator_factory(type):
        if type == "gan":
            return generator()
        elif type == "wgan":
            return generator()
        elif type == "vanilla_gan":
            return generator()
        elif type == "vanilla_wgan":
            return generator()

    @staticmethod
    def discriminator_factory(type):
        if type == "gan":
            return discriminator()
        elif type == "wgan":
            return discriminator()
        elif type == "vanilla_gan":
            return discriminator()
        elif type == "vanilla_wgan":
            return discriminator()


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.latent_dim = self.noise_dim + self.projected_embed_dim
        self.ngf = 64
        self.projection = nn.Sequential(
            nn.Linear(
                in_features=self.embed_dim, out_features=self.projected_embed_dim
            ),
            nn.BatchNorm1d(num_features=self.projected_embed_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
        )
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, embed_vector, z):
        projected_embed = self.projection(embed_vector).unsqueeze(2).unsqueeze(3)
        latent_vector = torch.cat([projected_embed, z], 1)
        output = self.netG(latent_vector)
        return output


class discriminator(nn.Module):
    def __init__(self):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.ndf = 64
        self.B_dim = 128
        self.C_dim = 16
        self.netD_1 = nn.Sequential(
            # input is (nc) x 64 x 64
            nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x 8 x 8
            nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
        )
        self.projector = Concat_embed(self.embed_dim, self.projected_embed_dim)
        self.netD_2 = nn.Sequential(
            # state size. (ndf*8) x 4 x 4
            nn.Conv2d(self.ndf * 8 + self.projected_embed_dim, 1, 4, 1, 0, bias=False),
            nn.Sigmoid(),
        )

    def forward(self, inp, embed):
        x_intermediate = self.netD_1(inp)
        x = self.projector(x_intermediate, embed)
        x = self.netD_2(x)
        return x.view(-1, 1).squeeze(1), x_intermediate


################################################################
# trainer.py
# config
class Trainer(object):
    def __init__(
        self,
        type,
        dataset,
        split,
        lr,
        diter,
        vis_screen,
        save_path,
        l1_coef,
        l2_coef,
        pre_trained_gen,
        pre_trained_disc,
        batch_size,
        num_workers,
        epochs,
    ):
        #         with open('config.yaml', 'r') as f:
        #             config = yaml.load(f, Loader=yaml.FullLoader)
        self.generator = torch.nn.DataParallel(
            gan_factory.generator_factory(type).cuda()
        )
        # self.discriminator模型
        self.discriminator = torch.nn.DataParallel(
            gan_factory.discriminator_factory(type).cuda()
        )
        if pre_trained_disc:
            self.discriminator.load_state_dict(torch.load(pre_trained_disc))
        else:
            self.discriminator.apply(Utils.weights_init)
        if pre_trained_gen:
            self.generator.load_state_dict(torch.load(pre_trained_gen))
        else:
            self.generator.apply(Utils.weights_init)
        if dataset == "birds":
            # self.dataset = Text2ImageDataset(config['birds_dataset_path'], split=split)
            # self.dataset=Text2ImageDataset(r'/kaggle/working/birds.hdf5', split=split)
            self.dataset = Text2ImageDataset(
                r"/kaggle/input/birdss/birds.hdf5", split=split
            )  # 源数据
        elif dataset == "flowers":
            print("error")
            return 0
            # self.dataset = Text2ImageDataset(config['flowers_dataset_path'], split=split)
        else:
            print("Dataset not supported, please select either birds or flowers.")
            exit()
        self.noise_dim = 100
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.lr = lr
        self.beta1 = 0.5
        self.num_epochs = epochs
        self.DITER = diter
        self.l1_coef = l1_coef
        self.l2_coef = l2_coef
        self.data_loader = DataLoader(
            self.dataset,
            batch_size=self.batch_size,
            shuffle=True,
            num_workers=self.num_workers,
        )
        self.optimD = torch.optim.Adam(
            self.discriminator.parameters(), lr=self.lr, betas=(self.beta1, 0.999)
        )
        self.optimG = torch.optim.Adam(
            self.generator.parameters(), lr=self.lr, betas=(self.beta1, 0.999)
        )
        # print(vis_screen)#gan
        # print("pause0")
        # input()
        self.logger = Logger(vis_screen)  # 这里涉及到了visdom
        # print("pause1")
        # input()
        self.checkpoints_path = "checkpoints"
        self.save_path = save_path
        self.type = type

    def train(self, cls=False):
        if self.type == "wgan":
            self._train_wgan(cls)
        elif self.type == "gan":
            self._train_gan(cls)
        elif self.type == "vanilla_wgan":
            self._train_vanilla_wgan()
        elif self.type == "vanilla_gan":
            self._train_vanilla_gan()

    def _train_wgan(self, cls):
        one = torch.FloatTensor([1])
        mone = one * -1
        one = Variable(one).cuda()
        mone = Variable(mone).cuda()
        gen_iteration = 0
        for epoch in range(self.num_epochs):
            iterator = 0
            data_iterator = iter(self.data_loader)
            while iterator < len(self.data_loader):
                if gen_iteration < 25 or gen_iteration % 500 == 0:
                    d_iter_count = 100
                else:
                    d_iter_count = self.DITER
                d_iter = 0
                # Train the discriminator
                while d_iter < d_iter_count and iterator < len(self.data_loader):
                    d_iter += 1
                    for p in self.discriminator.parameters():
                        p.requires_grad = True
                    self.discriminator.zero_grad()
                    sample = next(data_iterator)
                    iterator += 1
                    right_images = sample["right_images"]
                    right_embed = sample["right_embed"]
                    wrong_images = sample["wrong_images"]
                    right_images = Variable(right_images.float()).cuda()
                    right_embed = Variable(right_embed.float()).cuda()
                    wrong_images = Variable(wrong_images.float()).cuda()
                    outputs, _ = self.discriminator(right_images, right_embed)
                    real_loss = torch.mean(outputs)
                    real_loss.backward(mone)
                    if cls:
                        outputs, _ = self.discriminator(wrong_images, right_embed)
                        wrong_loss = torch.mean(outputs)
                        wrong_loss.backward(one)
                    noise = Variable(
                        torch.randn(right_images.size(0), self.noise_dim), volatile=True
                    ).cuda()
                    noise = noise.view(noise.size(0), self.noise_dim, 1, 1)
                    fake_images = Variable(self.generator(right_embed, noise).data)
                    outputs, _ = self.discriminator(fake_images, right_embed)
                    fake_loss = torch.mean(outputs)
                    fake_loss.backward(one)
                    ## NOTE: Pytorch had a bug with gradient penalty at the time of this project development
                    ##       , uncomment the next two lines and remove the params clamping below if you want to try gradient penalty
                    # gp = Utils.compute_GP(self.discriminator, right_images.data, right_embed, fake_images.data, LAMBDA=10)
                    # gp.backward()
                    d_loss = real_loss - fake_loss
                    if cls:
                        d_loss = d_loss - wrong_loss
                    self.optimD.step()
                    for p in self.discriminator.parameters():
                        p.data.clamp_(-0.01, 0.01)
                # Train Generator
                for p in self.discriminator.parameters():
                    p.requires_grad = False
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(right_embed, noise)
                outputs, _ = self.discriminator(fake_images, right_embed)
                g_loss = torch.mean(outputs)
                g_loss.backward(mone)
                g_loss = -g_loss
                self.optimG.step()
                gen_iteration += 1
                self.logger.draw(right_images, fake_images)
                self.logger.log_iteration_wgan(
                    epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss
                )
            self.logger.plot_epoch(gen_iteration)
            if (epoch + 1) % 50 == 0:
                Utils.save_checkpoint(
                    self.discriminator, self.generator, self.checkpoints_path, epoch
                )

    def _train_gan(self, cls):
        # print("pause2")
        criterion = nn.BCELoss()
        l2_loss = nn.MSELoss()
        l1_loss = nn.L1Loss()
        iteration = 0
        for epoch in range(self.num_epochs):
            for sample in self.data_loader:
                # print("pause3")
                # input()
                iteration += 1
                right_images = sample["right_images"]
                right_embed = sample["right_embed"]
                wrong_images = sample["wrong_images"]
                right_images = Variable(right_images.float()).cuda()
                right_embed = Variable(right_embed.float()).cuda()
                wrong_images = Variable(wrong_images.float()).cuda()
                # print("pause4")
                # input()
                real_labels = torch.ones(right_images.size(0))
                fake_labels = torch.zeros(right_images.size(0))
                # ======== One sided label smoothing ==========
                # Helps preventing the discriminator from overpowering the
                # generator adding penalty when the discriminator is too confident
                # =============================================
                smoothed_real_labels = torch.FloatTensor(
                    Utils.smooth_label(real_labels.numpy(), -0.1)
                )
                real_labels = Variable(real_labels).cuda()
                smoothed_real_labels = Variable(smoothed_real_labels).cuda()
                fake_labels = Variable(fake_labels).cuda()
                # Train the discriminator
                self.discriminator.zero_grad()
                outputs, activation_real = self.discriminator(right_images, right_embed)
                real_loss = criterion(outputs, smoothed_real_labels)
                real_score = outputs
                if cls:
                    outputs, _ = self.discriminator(wrong_images, right_embed)
                    wrong_loss = criterion(outputs, fake_labels)
                    wrong_score = outputs
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(right_embed, noise)
                outputs, _ = self.discriminator(fake_images, right_embed)
                fake_loss = criterion(outputs, fake_labels)
                fake_score = outputs
                d_loss = real_loss + fake_loss
                if cls:
                    d_loss = d_loss + wrong_loss
                d_loss.backward()
                self.optimD.step()
                # Train the generator
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(right_embed, noise)
                outputs, activation_fake = self.discriminator(fake_images, right_embed)
                _, activation_real = self.discriminator(right_images, right_embed)
                activation_fake = torch.mean(activation_fake, 0)
                activation_real = torch.mean(activation_real, 0)
                # ======= Generator Loss function============
                # This is a customized loss function, the first term is the regular cross entropy loss
                # The second term is feature matching loss, this measure the distance between the real and generated
                # images statistics by comparing intermediate layers activations
                # The third term is L1 distance between the generated and real images, this is helpful for the conditional case
                # because it links the embedding feature vector directly to certain pixel values.
                # ===========================================
                g_loss = (
                    criterion(outputs, real_labels)
                    + self.l2_coef * l2_loss(activation_fake, activation_real.detach())
                    + self.l1_coef * l1_loss(fake_images, right_images)
                )
                g_loss.backward()
                self.optimG.step()
                # print("pause5")
                if iteration % 5 == 0:
                    # print("pause6")
                    self.logger.log_iteration_gan(
                        epoch, d_loss, g_loss, real_score, fake_score
                    )
                    # print("pause7")
                    self.logger.draw(right_images, fake_images)  # 大量警告在此处
                    # print("pause8")
            # 多次训练后进入这里
            self.logger.plot_epoch_w_scores(epoch)
            if (epoch) % 10 == 0:
                Utils.save_checkpoint(
                    self.discriminator,
                    self.generator,
                    self.checkpoints_path,
                    self.save_path,
                    epoch,
                )

    def _train_vanilla_wgan(self):
        one = Variable(torch.FloatTensor([1])).cuda()
        mone = one * -1
        gen_iteration = 0
        for epoch in range(self.num_epochs):
            iterator = 0
            data_iterator = iter(self.data_loader)
            while iterator < len(self.data_loader):
                if gen_iteration < 25 or gen_iteration % 500 == 0:
                    d_iter_count = 100
                else:
                    d_iter_count = self.DITER
                d_iter = 0
                # Train the discriminator
                while d_iter < d_iter_count and iterator < len(self.data_loader):
                    d_iter += 1
                    for p in self.discriminator.parameters():
                        p.requires_grad = True
                    self.discriminator.zero_grad()
                    sample = next(data_iterator)
                    iterator += 1
                    right_images = sample["right_images"]
                    right_images = Variable(right_images.float()).cuda()
                    outputs, _ = self.discriminator(right_images)
                    real_loss = torch.mean(outputs)
                    real_loss.backward(mone)
                    noise = Variable(
                        torch.randn(right_images.size(0), self.noise_dim), volatile=True
                    ).cuda()
                    noise = noise.view(noise.size(0), self.noise_dim, 1, 1)
                    fake_images = Variable(self.generator(noise).data)
                    outputs, _ = self.discriminator(fake_images)
                    fake_loss = torch.mean(outputs)
                    fake_loss.backward(one)
                    ## NOTE: Pytorch had a bug with gradient penalty at the time of this project development
                    ##       , uncomment the next two lines and remove the params clamping below if you want to try gradient penalty
                    # gp = Utils.compute_GP(self.discriminator, right_images.data, right_embed, fake_images.data, LAMBDA=10)
                    # gp.backward()
                    d_loss = real_loss - fake_loss
                    self.optimD.step()
                    for p in self.discriminator.parameters():
                        p.data.clamp_(-0.01, 0.01)
                # Train Generator
                for p in self.discriminator.parameters():
                    p.requires_grad = False
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(noise)
                outputs, _ = self.discriminator(fake_images)
                g_loss = torch.mean(outputs)
                g_loss.backward(mone)
                g_loss = -g_loss
                self.optimG.step()
                gen_iteration += 1
                self.logger.draw(right_images, fake_images)
                self.logger.log_iteration_wgan(
                    epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss
                )
            self.logger.plot_epoch(gen_iteration)
            if (epoch + 1) % 50 == 0:
                Utils.save_checkpoint(
                    self.discriminator, self.generator, self.checkpoints_path, epoch
                )

    def _train_vanilla_gan(self):
        criterion = nn.BCELoss()
        l2_loss = nn.MSELoss()
        l1_loss = nn.L1Loss()
        iteration = 0
        for epoch in range(self.num_epochs):
            for sample in self.data_loader:
                iteration += 1
                right_images = sample["right_images"]
                right_images = Variable(right_images.float()).cuda()
                real_labels = torch.ones(right_images.size(0))
                fake_labels = torch.zeros(right_images.size(0))
                # ======== One sided label smoothing ==========
                # Helps preventing the discriminator from overpowering the
                # generator adding penalty when the discriminator is too confident
                # =============================================
                smoothed_real_labels = torch.FloatTensor(
                    Utils.smooth_label(real_labels.numpy(), -0.1)
                )
                real_labels = Variable(real_labels).cuda()
                smoothed_real_labels = Variable(smoothed_real_labels).cuda()
                fake_labels = Variable(fake_labels).cuda()
                # Train the discriminator
                self.discriminator.zero_grad()
                outputs, activation_real = self.discriminator(right_images)
                real_loss = criterion(outputs, smoothed_real_labels)
                real_score = outputs
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(noise)
                outputs, _ = self.discriminator(fake_images)
                fake_loss = criterion(outputs, fake_labels)
                fake_score = outputs
                d_loss = real_loss + fake_loss
                d_loss.backward()
                self.optimD.step()
                # Train the generator
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(noise)
                outputs, activation_fake = self.discriminator(fake_images)
                _, activation_real = self.discriminator(right_images)
                activation_fake = torch.mean(activation_fake, 0)
                activation_real = torch.mean(activation_real, 0)
                # ======= Generator Loss function============
                # This is a customized loss function, the first term is the regular cross entropy loss
                # The second term is feature matching loss, this measure the distance between the real and generated
                # images statistics by comparing intermediate layers activations
                # The third term is L1 distance between the generated and real images, this is helpful for the conditional case
                # because it links the embedding feature vector directly to certain pixel values.
                g_loss = (
                    criterion(outputs, real_labels)
                    + self.l2_coef * l2_loss(activation_fake, activation_real.detach())
                    + self.l1_coef * l1_loss(fake_images, right_images)
                )
                g_loss.backward()
                self.optimG.step()
                if iteration % 5 == 0:
                    self.logger.log_iteration_gan(
                        epoch, d_loss, g_loss, real_score, fake_score
                    )
                    self.logger.draw(right_images, fake_images)
            self.logger.plot_epoch_w_scores(iteration)
            if (epoch) % 50 == 0:
                Utils.save_checkpoint(
                    self.discriminator, self.generator, self.checkpoints_path, epoch
                )

    def predict(self):
        i = 0
        for sample in self.data_loader:
            right_images = sample["right_images"]
            right_embed = sample["right_embed"]
            txt = sample["txt"]
            #             if not os.path.exists('results/{0}'.format(self.save_path)):
            #                 os.makedirs('results/{0}'.format(self.save_path))
            right_images = Variable(right_images.float()).cuda()
            right_embed = Variable(right_embed.float()).cuda()
            # Train the generator
            noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
            noise = noise.view(noise.size(0), 100, 1, 1)
            fake_images = self.generator(right_embed, noise)
            self.logger.draw(right_images, fake_images)
            for image, t in zip(fake_images, txt):
                im = Image.fromarray(
                    image.data.mul_(127.5)
                    .add_(127.5)
                    .byte()
                    .permute(1, 2, 0)
                    .cpu()
                    .numpy()
                )
                # im.save('{0}/{1}.jpg'.format(self.save_path, t.replace("/", "")[:100]))
                if i % 1000 == 0:
                    print(i)
                    im.save(
                        "{0}/{1}.jpg".format(
                            self.save_path, str(i).replace("/", "")[:100]
                        )
                    )
                i = i + 1
                # print('{0}/{1}/.jpg'.format(self.save_path, t.replace("/", "")[:100]))
                # print(t)
                # input()
        print("predict end")


# txt2image_dataset
import os
import io
from torch.utils.data import Dataset, DataLoader
import h5py
import numpy as np
import pdb
from PIL import Image
import torch
from torch.autograd import Variable
import pdb
import torch.nn.functional as F


class Text2ImageDataset(Dataset):
    def __init__(self, datasetFile, transform=None, split=0):
        self.datasetFile = datasetFile
        self.transform = transform
        self.dataset = None
        self.dataset_keys = None
        self.split = "train" if split == 0 else "valid" if split == 1 else "test"
        self.h5py2int = lambda x: int(np.array(x))

    def __len__(self):
        f = h5py.File(self.datasetFile, "r")
        self.dataset_keys = [str(k) for k in f[self.split].keys()]
        length = len(f[self.split])
        f.close()
        return length

    def __getitem__(self, idx):
        if self.dataset is None:
            self.dataset = h5py.File(self.datasetFile, mode="r")
            self.dataset_keys = [str(k) for k in self.dataset[self.split].keys()]
        example_name = self.dataset_keys[idx]
        example = self.dataset[self.split][example_name]
        # pdb.set_trace()
        right_image = bytes(np.array(example["img"]))
        right_embed = np.array(example["embeddings"], dtype=float)
        wrong_image = bytes(np.array(self.find_wrong_image(example["class"])))
        inter_embed = np.array(self.find_inter_embed())
        right_image = Image.open(io.BytesIO(right_image)).resize((64, 64))
        wrong_image = Image.open(io.BytesIO(wrong_image)).resize((64, 64))
        right_image = self.validate_image(right_image)
        wrong_image = self.validate_image(wrong_image)
        txt = np.array(example["txt"]).astype(str)
        sample = {
            "right_images": torch.FloatTensor(right_image),
            "right_embed": torch.FloatTensor(right_embed),
            "wrong_images": torch.FloatTensor(wrong_image),
            "inter_embed": torch.FloatTensor(inter_embed),
            "txt": str(txt),
        }
        sample["right_images"] = sample["right_images"].sub_(127.5).div_(127.5)
        sample["wrong_images"] = sample["wrong_images"].sub_(127.5).div_(127.5)
        return sample

    def find_wrong_image(self, category):
        idx = np.random.randint(len(self.dataset_keys))
        example_name = self.dataset_keys[idx]
        example = self.dataset[self.split][example_name]
        _category = example["class"]
        if _category != category:
            return example["img"]
        return self.find_wrong_image(category)

    def find_inter_embed(self):
        idx = np.random.randint(len(self.dataset_keys))
        example_name = self.dataset_keys[idx]
        example = self.dataset[self.split][example_name]
        return example["embeddings"]

    def validate_image(self, img):
        img = np.array(img, dtype=float)
        if len(img.shape) < 3:
            rgb = np.empty((64, 64, 3), dtype=np.float32)
            rgb[:, :, 0] = img
            rgb[:, :, 1] = img
            rgb[:, :, 2] = img
            img = rgb
        return img.transpose(2, 0, 1)


### runtime.py
# from trainer import Trainer
import argparse
from PIL import Image
import os

parser = argparse.ArgumentParser()
parser.add_argument("--type", default="gan")
parser.add_argument("--lr", default=0.0002, type=float)  # 0.0002
parser.add_argument("--l1_coef", default=50, type=float)
parser.add_argument("--l2_coef", default=100, type=float)
parser.add_argument("--diter", default=5, type=int)
parser.add_argument("--cls", default=False, action="store_true")
parser.add_argument("--vis_screen", default="gan")
parser.add_argument("--save_path", default=r"/kaggle/working")
# parser.add_argument("--inference", default=False, action='store_true')#训练或者预测
parser.add_argument("--inference", default=True, action="store_true")
parser.add_argument("--pre_trained_disc", default=None)
parser.add_argument("--pre_trained_gen", default=None)
parser.add_argument("--dataset", default="birds")
parser.add_argument("--split", default=0, type=int)
parser.add_argument("--batch_size", default=64, type=int)  # 64
parser.add_argument("--num_workers", default=0, type=int)
parser.add_argument("--epochs", default=200, type=int)  # 200
# args = parser.parse_args()
args = parser.parse_known_args()[0]
# print(args)
trainer = Trainer(
    type=args.type,
    dataset=args.dataset,
    split=args.split,
    lr=args.lr,
    diter=args.diter,
    vis_screen=args.vis_screen,
    save_path=args.save_path,
    l1_coef=args.l1_coef,
    l2_coef=args.l2_coef,
    pre_trained_disc=args.pre_trained_disc,
    pre_trained_gen=args.pre_trained_gen,
    batch_size=args.batch_size,
    num_workers=args.num_workers,
    epochs=args.epochs,
)
# 删除图表文件
import os

path1 = r"/kaggle/working/Discriminator.txt"
path2 = r"/kaggle/working/Generator.txt"
path3 = r"/kaggle/working/D(X).txt"
path4 = r"/kaggle/working/D(G(X)).txt"
if os.path.exists(path1):
    os.remove(path1)
if os.path.exists(path2):
    os.remove(path2)
if os.path.exists(path3):
    os.remove(path3)
if os.path.exists(path4):
    os.remove(path4)
# 创建npy文件夹
if not os.path.exists(r"/kaggle/working/npy"):
    os.mkdir(r"/kaggle/working/npy")
# 创建图表txt
path1 = r"/kaggle/working/Discriminator.txt"
path2 = r"/kaggle/working/Generator.txt"
path3 = r"/kaggle/working/D(X).txt"
path4 = r"/kaggle/working/D(G(X)).txt"
if not os.path.exists(path1):
    with open(path1, "w", encoding="utf-8") as f:
        f.write("")
if not os.path.exists(path2):
    with open(path2, "w", encoding="utf-8") as f:
        f.write("")
if not os.path.exists(path3):
    with open(path3, "w", encoding="utf-8") as f:
        f.write("")
if not os.path.exists(path4):
    with open(path4, "w", encoding="utf-8") as f:
        f.write("")
# 一次Epoch写入一次txt文件
# if not args.inference:
#     print("train")
#     trainer.train(args.cls)
# else:
#     print("predict")
#     trainer.predict()
trainer.train(args.cls)
# trainer.predict()
print("end")
# 下面是额外的内容
print("123")
# #删除图表文件
# import os
# path1=r'/kaggle/working/Discriminator.txt'
# path2=r'/kaggle/working/Generator.txt'
# path3=r'/kaggle/working/D(X).txt'
# path4=r'/kaggle/working/D(G(X)).txt'
# if os.path.exists(path1):
#     os.remove(path1)
# if os.path.exists(path2):
#     os.remove(path2)
# if os.path.exists(path3):
#     os.remove(path3)
# if os.path.exists(path4):
#     os.remove(path4)
# #创建图表文件
# import os
# path1=r'/kaggle/working/Discriminator.txt'
# path2=r'/kaggle/working/Generator.txt'
# path3=r'/kaggle/working/D(X).txt'
# path4=r'/kaggle/working/D(G(X)).txt'
# if not os.path.exists(path1):
#     with open(path1, "w", encoding="utf-8") as f:
#         f.write("")
# if not os.path.exists(path2):
#     with open(path2, "w", encoding="utf-8") as f:
#         f.write("")
# if not os.path.exists(path3):
#     with open(path3, "w", encoding="utf-8") as f:
#         f.write("")
# if not os.path.exists(path4):
#     with open(path4, "w", encoding="utf-8") as f:
#         f.write("")
# 下面是额外的内容
# pip show torchfile#查看安装包的版本
# pip uninstall visdom#卸载visdom
# pip list --outdated#查看是否有新版本更新
# pip list#查看所有包的版本
# import socket
# # 获取本机计算机名称
# hostname = socket.gethostname()
# # 获取本机ip
# ip = socket.gethostbyname(hostname)
# print(hostname)
# print(ip)
# #配套在pycharm上面使用
# import cv2
# import numpy as np
# import visdom
# #启动服务python -m visdom.server
# # #读取npy文件
# viz = visdom.Visdom()
# def fun1():
#     print("fun1")
#     # viz.text('Hello, world!')
#     # viz.image(np.ones((3, 10, 10)))
#     a=np.load(r'D:\.android\2000a.npy')
#     b=np.load(r'D:\.android\2000b.npy')
#     viz.images(a)
#     viz.images(b)
# #读取txt文件并且绘图
# def fun2():
#     plots={}
#     def plot(var_name, split_name, x, y, xlabel='epoch'):
#         if var_name not in plots:
#             plots[var_name] = viz.line(X=np.array([x, x]), Y=np.array([y, y]), opts=dict(
#                 legend=[split_name],
#                 title=var_name,
#                 xlabel=xlabel,
#                 ylabel=var_name
#             ))
#             #viz.line(X=np.array([x, x]), Y=np.array([y, y]))
#         else:
#             viz.line(X=np.array([x, x]), Y=np.array([y, y]),win=plots[var_name],
#                                       name=split_name,update='append')
#             # viz.updateTrace(X=np.array([x]), Y=np.array([y]), win=plots[var_name],
#             #                         name=split_name)
#     path1=r'D:\.android\D(G(X)).txt'
#     path2=r'D:\.android\D(X).txt'
#     path3=r'D:\.android\Discriminator.txt'
#     path4=r'D:\.android\Generator.txt'
#     i=0
#     with open(path1, 'r') as f:
#         for line in f:
#             line = float(line)
#             #print(line)
#             plot('D(G(X))', 'train', i, line)
#             #print(line)
#             i+=1
#     i=0
#     with open(path2, 'r') as f:
#         for line in f:
#             line = float(line)
#             plot('D(X)', 'train', i, line)
#             i+=1
#     i=0
#     with open(path3, 'r') as f:
#         for line in f:
#             line = float(line)
#             plot('Discriminator', 'train', i, line)
#             i+=1
#     i=0
#     with open(path4, 'r') as f:
#         for line in f:
#             line = float(line)
#             plot('Generator', 'train', i, line)
#             i+=1
#     #self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean())
#     #self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean())
#     #self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean())
#     #self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean())
# fun1()
# #fun2()
# import os
# if not os.path.exists(r'/kaggle/working/npy'):
#     os.mkdir(r'/kaggle/working/npy')
# 检查图片是否正确
# aa=np.load("/kaggle/working/img2.npy")
# print(aa.shape)
# print(aa)
# pip install wmi
# pip install pypiwin32
# #查看kaggle的版本
# # PythonVersion:python2.7
# # filename:sys_info.py
# import sys
# import wmi
# import socket
# import platform
# import imp
# imp.reload(sys)
# #reload(sys)
# #sys.setdefaultencoding('utf-8')
# c = wmi.WMI()
# # 系统信息
# print(u'操作系统名称' + platform.platform()[:-(len(platform.version()) + 1)])
# print(u'操作系统版本号' + platform.version())
# print(u'操作系统的位数' + platform.architecture()[0])
# hostname = socket.getfqdn(socket.gethostname())
# ip = socket.gethostbyname(hostname)
# print('ip:' + ip)
# # CPU信息
# def get_CPU():
#     cpumsg = []
#     for cpu in c.Win32_Processor():
#         tmpmsg = {}
#         tmpmsg['Name'] = cpu.Name
#         cpumsg.append(tmpmsg)
#     print(cpumsg)
# # 内存信息
# def get_PhysicalMemory():
#     memorys = []
#     for mem in c.Win32_PhysicalMemory():
#         tmpmsg = {}
#         tmpmsg['Tag'] = mem.Tag
#         tmpmsg['ConfiguredClockSpeed'] = str(mem.ConfiguredClockSpeed) + 'MHz'
#         memorys.append(tmpmsg)
#     print(memorys)
# # 显卡信息
# def get_video():
#     videos = []
#     for v in c.Win32_VideoController():
#         tmpmsg = {}
#         tmpmsg['Caption'] = v.Caption
#         tmpmsg['AdapterRAM'] = str(abs(v.AdapterRAM) / (1024 ** 3)) + 'G'
#         videos.append(tmpmsg)
#     print(videos)
# # 网卡mac地址
# def get_MacAddress():
#     macs = []
#     for n in c.Win32_NetworkAdapter():
#         mactmp = n.MACAddress
#         if mactmp and len(mactmp.strip()) > 5:
#             tmpmsg = {}
#             tmpmsg['ProductName'] = n.ProductName
#             tmpmsg['NetConnectionID'] = n.NetConnectionID
#             tmpmsg['MACAddress'] = n.MACAddress
#             macs.append(tmpmsg)
#     print(macs)
# def main():
#     get_CPU()
#     get_PhysicalMemory()
#     get_video()
#     get_MacAddress()
# if __name__ == '__main__':
#     main()
# #下载运行结果
# import os
# os.chdir(r'/kaggle/working/npy')
# print(os.getcwd())
# print(os.listdir("/kaggle/working/npy"))
# from IPython.display import FileLink
# #FileLink('a colorful bird containing a bright blue head, nape neck and back, blue white and black feathers, bu.jpg')
# FileLink('2000b.npy')
# from torch.utils.data import DataLoader
# birds_dataset_path='/kaggle/working/birds.hdf5'
# dataset = Text2ImageDataset(birds_dataset_path,split=split)
# #注意调整batch_size和num_workers不然cuda 虚拟环境的共享内存不足要报错
# #或者多线程操作时就有可能导致线程套线程，容易出现死锁的情况
# data_loader = DataLoader(dataset, batch_size=64,shuffle=True,num_workers=0)
# import h5py
# datasetFile='/kaggle/working/birds.hdf5'
# f = h5py.File(datasetFile, 'r')
# split='train'
# print(f)
# print(split)
# print(f.keys())
# print("123")
# print(f['train'].keys())
# print("ok")
# dataset_keys = [str(k) for k in f[split].keys()]
# print(dataset_keys)
<|endoftext|>
[{"Id": 69293649, "ScriptId": 18546956, "ParentScriptVersionId": NaN, "ScriptLanguageId": 9, "AuthorUserId": 7677477, "CreationDate": "07/29/2021 07:38:08", "VersionNumber": 96.0, "Title": "pmh_two", "EvaluationDate": "07/29/2021", "IsChange": true, "TotalLines": 1700.0, "LinesInsertedFromPrevious": 238.0, "LinesChangedFromPrevious": 0.0, "LinesUnchangedFromPrevious": 1462.0, "LinesInsertedFromFork": NaN, "LinesDeletedFromFork": NaN, "LinesChangedFromFork": NaN, "LinesUnchangedFromFork": NaN, "TotalVotes": 0}]
# 生成data文件
import os
from os.path import join, isfile
import numpy as np
import h5py
from glob import glob

# from torch.utils.serialization import load_lua
import torchfile
from PIL import Image
import yaml
import io
import pdb

# images_path ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/CUB_200_2011/images'
# embedding_path ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml'
# text_path ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cvpr2016_cub/text_c10'
# val_classes ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml/valclasses.txt'
# train_classes ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml/trainclasses.txt'
# test_classes ='/kaggle/input/kaggle-small-data/kaggle_small_data/Birds dataset/cub_icml/testclasses.txt'
images_path = (
    "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/CUB_200_2011/images"
)
embedding_path = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml"
text_path = (
    "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cvpr2016_cub/text_c10"
)
val_classes = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml/valclasses.txt"
train_classes = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml/trainclasses.txt"
test_classes = "/kaggle/input/kaggle-mid-data/kaggle_mid_data/Birds dataset/cub_icml/testclasses.txt"
datasetDir = "/kaggle/working/birds.hdf5"
# val_classes = open(val_classes).read()
# train_classes = open(train_classes).read()
# test_classes = open(test_classes).read()
val_classes = open(val_classes).read().splitlines()
train_classes = open(train_classes).read().splitlines()
test_classes = open(test_classes).read().splitlines()
# print(val_classes)
# print(val_classes2)
if os.path.exists(datasetDir):
    os.remove(datasetDir)
f = h5py.File(datasetDir, "w")
train = f.create_group("train")
valid = f.create_group("valid")
test = f.create_group("test")
# print(f)
# print(train)
# print(test)
for _class in sorted(os.listdir(embedding_path)):
    split = ""
    if _class in train_classes:
        split = train
    elif _class in val_classes:
        split = valid
    elif _class in test_classes:
        split = test
    # print(_class)
    # print(type(_class))
    # print(train_classes)
    # print(type(train_classes))
    # print(split)
    # else:#它还会遍历到文件夹下的.txt文件
    # print(_class)
    data_path = os.path.join(embedding_path, _class)  # 获取数据路径
    txt_path = os.path.join(text_path, _class)
    # zip将对象中对应的元素打包成一个个元组，然后返回由这些元组组成的列表
    # example遍历cub_icml的叶子文件
    # txt_file遍历cvpr2016_cub的叶子文件
    for example, txt_file in zip(
        sorted(glob(data_path + "/*.t7")), sorted(glob(txt_path + "/*.txt"))
    ):
        example_data = torchfile.load(example)  # 加载t7文件
        # print(example_data)
        # print(type(example_data))#查看是什么类型，实际上是一个字典
        # print(example_data.keys())#查看有什么键
        img_path = example_data[b"img"]
        # print(img_path)
        embeddings = example_data[b"txt"]
        # print(embeddings.shape)10*1024
        # print(type(embeddings))#numpy类型
        # print(embeddings)
        img_path = str(img_path, "utf-8")  # 将bytes转化为字符串
        example_name = img_path.split("/")[-1][:-4]
        # print(example_name)
        f = open(txt_file, "r")  # txt路径
        txt = f.readlines()  # txt的英文描述
        f.close()
        img_path = os.path.join(images_path, img_path)  # 图片路径
        img = open(img_path, "rb").read()  # 图片以二进制方式读取
        txt_choice = np.random.choice(range(10), 5)  # 随机从10个里面选5个
        embeddings = embeddings[txt_choice]  # 从embeddings里面读取文件
        txt = np.array(txt)  # 转化为np类型
        txt = txt[txt_choice]
        dt = h5py.special_dtype(vlen=str)  # object
        for c, e in enumerate(embeddings):  # 将embeddings转为一个可以遍历的索引序列
            # 这里split是split()的意思，split在前面的split对应
            ex = split.create_group(example_name + "_" + str(c))
            ex.create_dataset("name", data=example_name)
            ex.create_dataset("img", data=np.void(img))
            ex.create_dataset("embeddings", data=e)
            ex.create_dataset("class", data=_class)
            ex.create_dataset("txt", data=txt[c].astype(object), dtype=dt)
        # print(example_name)
print("end")
# gan.py
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.ngf = 64
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.noise_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, z):
        return self.netG(z)


class discriminator(nn.Module):
    def __init__(self):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.ndf = 64
        self.B_dim = 128
        self.C_dim = 16
        self.netD_1 = nn.Sequential(
            # input is (nc) x 64 x 64
            nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x 8 x 8
            nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
        )
        self.netD_2 = nn.Sequential(
            # state size. (ndf*8) x 4 x 4
            nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Sigmoid(),
        )

    def forward(self, inp):
        x_intermediate = self.netD_1(inp)
        output = self.netD_2(x_intermediate)
        return output.view(-1, 1).squeeze(1), x_intermediate


# wgan.py
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

# from utils import Concat_embed, minibatch_discriminator
import pdb


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.ngf = 64
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.noise_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, z):
        output = self.netG(z)
        return output


class discriminator(nn.Module):
    def __init__(self, improved=False):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.ndf = 64
        if improved:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        else:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 2),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 4),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 8),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        self.netD_2 = nn.Sequential(
            # nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False)
        )

    def forward(self, inp):
        x_intermediate = self.netD_1(inp)
        x = self.netD_2(x_intermediate)
        x = x.mean(0)
        return x.view(1), x_intermediate


# visualize.py
from visdom import Visdom
import numpy as np
import torchvision
from PIL import ImageDraw, Image, ImageFont
import torch
import pdb


class VisdomPlotter(object):
    """Plots to Visdom"""

    def __init__(self, env_name="gan"):
        # 这里是大量出现警告的地方
        self.viz = Visdom()
        self.env = env_name
        self.plots = {}

    def plot(self, var_name, split_name, x, y, xlabel="epoch"):
        if var_name not in self.plots:
            self.plots[var_name] = self.viz.line(
                X=np.array([x, x]),
                Y=np.array([y, y]),
                env=self.env,
                opts=dict(
                    legend=[split_name], title=var_name, xlabel=xlabel, ylabel=var_name
                ),
            )
        else:
            self.viz.updateTrace(
                X=np.array([x]),
                Y=np.array([y]),
                env=self.env,
                win=self.plots[var_name],
                name=split_name,
            )

    def draw(self, var_name, images):
        if var_name not in self.plots:  # 这里出现大量警告
            print("pause11")
            # print(var_name)
            # a=self.viz.images(images)#gan
            # self.plots[var_name] = self.viz.images(images, env=self.env)
            # print("pause12")
            # input()
        else:
            self.viz.images(images, env=self.env, win=self.plots[var_name])


# import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

# from utils import Concat_embed, minibatch_discriminator
import pdb


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.latent_dim = self.noise_dim + self.projected_embed_dim
        self.ngf = 64
        self.projection = nn.Sequential(
            nn.Linear(
                in_features=self.embed_dim, out_features=self.projected_embed_dim
            ),
            nn.BatchNorm1d(num_features=self.projected_embed_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
        )
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, embed_vector, z):
        projected_embed = self.projection(embed_vector).unsqueeze(2).unsqueeze(3)
        latent_vector = torch.cat([projected_embed, z], 1)
        output = self.netG(latent_vector)
        return output


class discriminator(nn.Module):
    def __init__(self, improved=False):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.ndf = 64
        if improved:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        else:
            self.netD_1 = nn.Sequential(
                # input is (nc) x 64 x 64
                nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 32 x 32
                nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 2),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 16 x 16
                nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 4),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 8 x 8
                nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
                nn.BatchNorm2d(self.ndf * 8),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 4 x 4
            )
        self.projector = Concat_embed(self.embed_dim, self.projected_embed_dim)
        self.netD_2 = nn.Sequential(
            # nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Conv2d(self.ndf * 8 + self.projected_embed_dim, 1, 4, 1, 0, bias=False)
        )

    def forward(self, inp, embed):
        x_intermediate = self.netD_1(inp)
        x = self.projector(x_intermediate, embed)
        x = self.netD_2(x)
        x = x.mean(0)
        return x.view(1), x_intermediate


# gan_cls.py
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

# from utils import Concat_embed
import pdb
import yaml
from torch import nn

# from torch.utils.data import DataLoader
# from models.gan_factory import gan_factory
# from utils import Utils, Logger
from PIL import Image
import os

# utils.py
import numpy as np
from torch import autograd

# from visualize import VisdomPlotter
import os
import pdb


class Concat_embed(nn.Module):
    def __init__(self, embed_dim, projected_embed_dim):
        super(Concat_embed, self).__init__()
        self.projection = nn.Sequential(
            nn.Linear(in_features=embed_dim, out_features=projected_embed_dim),
            nn.BatchNorm1d(num_features=projected_embed_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
        )

    def forward(self, inp, embed):
        projected_embed = self.projection(embed)
        replicated_embed = projected_embed.repeat(4, 4, 1, 1).permute(2, 3, 0, 1)
        hidden_concat = torch.cat([inp, replicated_embed], 1)
        return hidden_concat


class minibatch_discriminator(nn.Module):
    def __init__(self, num_channels, B_dim, C_dim):
        super(minibatch_discriminator, self).__init__()
        self.B_dim = B_dim
        self.C_dim = C_dim
        self.num_channels = num_channels
        T_init = torch.randn(num_channels * 4 * 4, B_dim * C_dim) * 0.1
        self.T_tensor = nn.Parameter(T_init, requires_grad=True)

    def forward(self, inp):
        inp = inp.view(-1, self.num_channels * 4 * 4)
        M = inp.mm(self.T_tensor)
        M = M.view(-1, self.B_dim, self.C_dim)
        op1 = M.unsqueeze(3)
        op2 = M.permute(1, 2, 0).unsqueeze(0)
        output = torch.sum(torch.abs(op1 - op2), 2)
        output = torch.sum(torch.exp(-output), 2)
        output = output.view(M.size(0), -1)
        output = torch.cat((inp, output), 1)
        return output


class Utils(object):
    @staticmethod
    def smooth_label(tensor, offset):
        return tensor + offset

    @staticmethod
    # based on:  https://github.com/caogang/wgan-gp/blob/master/gan_cifar10.py
    def compute_GP(netD, real_data, real_embed, fake_data, LAMBDA):
        BATCH_SIZE = real_data.size(0)
        alpha = torch.rand(BATCH_SIZE, 1)
        alpha = (
            alpha.expand(BATCH_SIZE, int(real_data.nelement() / BATCH_SIZE))
            .contiguous()
            .view(BATCH_SIZE, 3, 64, 64)
        )
        alpha = alpha.cuda()
        interpolates = alpha * real_data + ((1 - alpha) * fake_data)
        interpolates = interpolates.cuda()
        interpolates = autograd.Variable(interpolates, requires_grad=True)
        disc_interpolates, _ = netD(interpolates, real_embed)
        gradients = autograd.grad(
            outputs=disc_interpolates,
            inputs=interpolates,
            grad_outputs=torch.ones(disc_interpolates.size()).cuda(),
            create_graph=True,
            retain_graph=True,
            only_inputs=True,
        )[0]
        gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
        return gradient_penalty

    @staticmethod
    def save_checkpoint(netD, netG, dir_path, subdir_path, epoch):
        # 暂时不保存模型
        pass

    #         #print("pause14")
    #         #保存模型
    #         #path =  os.path.join(dir_path, subdir_path)
    #         path='/kaggle/working'
    # #         if not os.path.exists(path):
    # #             os.makedirs(path)
    # #             print("no")
    #         torch.save(netD.state_dict(), '{0}/disc_{1}.pth'.format(path, epoch))
    #         torch.save(netG.state_dict(), '{0}/gen_{1}.pth'.format(path, epoch))
    @staticmethod
    def weights_init(m):
        classname = m.__class__.__name__
        if classname.find("Conv") != -1:
            m.weight.data.normal_(0.0, 0.02)
        elif classname.find("BatchNorm") != -1:
            m.weight.data.normal_(1.0, 0.02)
            m.bias.data.fill_(0)


class Logger(object):
    def __init__(self, vis_screen):
        # 这里是大量警告出现的地方
        # print(vis_screen)#gan
        # self.viz = VisdomPlotter(env_name=vis_screen)
        self.hist_D = []
        self.hist_G = []
        self.hist_Dx = []
        self.hist_DGx = []
        self.i = 0

    def log_iteration_wgan(
        self, epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss
    ):
        print(
            "Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f"
            % (
                epoch,
                gen_iteration,
                d_loss.data.cpu().mean(),
                g_loss.data.cpu().mean(),
                real_loss,
                fake_loss,
            )
        )
        self.hist_D.append(d_loss.data.cpu().mean())
        self.hist_G.append(g_loss.data.cpu().mean())

    def log_iteration_gan(self, epoch, d_loss, g_loss, real_score, fake_score):
        # 输出训练成果
        # print("pause13")
        print(
            "Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f"
            % (
                epoch,
                d_loss.data.cpu().mean(),
                g_loss.data.cpu().mean(),
                real_score.data.cpu().mean(),
                fake_score.data.cpu().mean(),
            )
        )
        # 每次训练以后把结果放到对应list里面
        self.hist_D.append(d_loss.data.cpu().mean())
        self.hist_G.append(g_loss.data.cpu().mean())
        self.hist_Dx.append(real_score.data.cpu().mean())
        self.hist_DGx.append(fake_score.data.cpu().mean())

    def plot_epoch(self, epoch):
        self.viz.plot("Discriminator", "train", epoch, np.array(self.hist_D).mean())
        self.viz.plot("Generator", "train", epoch, np.array(self.hist_G).mean())
        self.hist_D = []
        self.hist_G = []

    def plot_epoch_w_scores(self, epoch):
        # viz的全部屏蔽掉
        # 绘图
        # 求平均值
        #         print(np.array(self.hist_D).mean())
        #         print(np.array(self.hist_G).mean())
        #         print(np.array(self.hist_Dx).mean())
        #         print(np.array(self.hist_DGx).mean())
        # self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean())
        # self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean())
        # self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean())
        # self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean())
        path1 = r"/kaggle/working/Discriminator.txt"
        path2 = r"/kaggle/working/Generator.txt"
        path3 = r"/kaggle/working/D(X).txt"
        path4 = r"/kaggle/working/D(G(X)).txt"
        with open(path1, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_D).mean()))
            f.write("\n")
        with open(path2, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_G).mean()))
            f.write("\n")
        with open(path3, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_Dx).mean()))
            f.write("\n")
        with open(path4, "a", encoding="utf-8") as f:
            f.write(str(np.array(self.hist_DGx).mean()))
            f.write("\n")
        #         with open(path,"a") as f:
        #             f.write(str(i))
        #             f.write('\n')
        self.hist_D = []
        self.hist_G = []
        self.hist_Dx = []
        self.hist_DGx = []

    def draw(self, right_images, fake_images):
        # 在Image转Tensor过程中，图片的格式会由: H * W * C的格式转为: C * H * W格式。
        # 输出图片(64*3*64*64)意思是64*64*3的彩色图 第一个64对应的batchsize为64
        # a=right_images.numpy()
        # a=right_images.cpu().numpy()
        # a=right_images.cpu().detach().numpy()
        # a0=a[0]
        # d=np.transpose(a0)
        # c = np.transpose(a)
        i = self.i
        if i % 500 == 0:
            a = right_images.data.cpu().numpy()[:64] * 128 + 128
            np.save("/kaggle/working/npy/" + str(i) + "a.npy", a)
            b = fake_images.data.cpu().numpy()[:64] * 128 + 128  # 生成的是灰色的图片
            np.save("/kaggle/working/npy/" + str(i) + "b.npy", b)
        self.i += 1
        # print("ok")
        # input()
        # return
        # print(c.shape)
        # c0=c[0]
        # print(d.shape)
        # print(c0.shape)
        # plt.imshow("d", d)
        # plt.show()
        # input()
        # print("pause9")
        # 这两行代码产生大量的警告
        # self.viz.draw('generated images', fake_images.data.cpu().numpy()[:64] * 128 + 128)
        # input()
        # print("pause10")
        # self.viz.draw('real images', right_images.data.cpu().numpy()[:64] * 128 + 128)


#########################################################
class gan_factory(object):
    @staticmethod
    def generator_factory(type):
        if type == "gan":
            return generator()
        elif type == "wgan":
            return generator()
        elif type == "vanilla_gan":
            return generator()
        elif type == "vanilla_wgan":
            return generator()

    @staticmethod
    def discriminator_factory(type):
        if type == "gan":
            return discriminator()
        elif type == "wgan":
            return discriminator()
        elif type == "vanilla_gan":
            return discriminator()
        elif type == "vanilla_wgan":
            return discriminator()


class generator(nn.Module):
    def __init__(self):
        super(generator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.noise_dim = 100
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.latent_dim = self.noise_dim + self.projected_embed_dim
        self.ngf = 64
        self.projection = nn.Sequential(
            nn.Linear(
                in_features=self.embed_dim, out_features=self.projected_embed_dim
            ),
            nn.BatchNorm1d(num_features=self.projected_embed_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
        )
        # based on: https://github.com/pytorch/examples/blob/master/dcgan/main.py
        self.netG = nn.Sequential(
            nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (num_channels) x 64 x 64
        )

    def forward(self, embed_vector, z):
        projected_embed = self.projection(embed_vector).unsqueeze(2).unsqueeze(3)
        latent_vector = torch.cat([projected_embed, z], 1)
        output = self.netG(latent_vector)
        return output


class discriminator(nn.Module):
    def __init__(self):
        super(discriminator, self).__init__()
        self.image_size = 64
        self.num_channels = 3
        self.embed_dim = 1024
        self.projected_embed_dim = 128
        self.ndf = 64
        self.B_dim = 128
        self.C_dim = 16
        self.netD_1 = nn.Sequential(
            # input is (nc) x 64 x 64
            nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x 8 x 8
            nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
        )
        self.projector = Concat_embed(self.embed_dim, self.projected_embed_dim)
        self.netD_2 = nn.Sequential(
            # state size. (ndf*8) x 4 x 4
            nn.Conv2d(self.ndf * 8 + self.projected_embed_dim, 1, 4, 1, 0, bias=False),
            nn.Sigmoid(),
        )

    def forward(self, inp, embed):
        x_intermediate = self.netD_1(inp)
        x = self.projector(x_intermediate, embed)
        x = self.netD_2(x)
        return x.view(-1, 1).squeeze(1), x_intermediate


################################################################
# trainer.py
# config
class Trainer(object):
    def __init__(
        self,
        type,
        dataset,
        split,
        lr,
        diter,
        vis_screen,
        save_path,
        l1_coef,
        l2_coef,
        pre_trained_gen,
        pre_trained_disc,
        batch_size,
        num_workers,
        epochs,
    ):
        #         with open('config.yaml', 'r') as f:
        #             config = yaml.load(f, Loader=yaml.FullLoader)
        self.generator = torch.nn.DataParallel(
            gan_factory.generator_factory(type).cuda()
        )
        # self.discriminator模型
        self.discriminator = torch.nn.DataParallel(
            gan_factory.discriminator_factory(type).cuda()
        )
        if pre_trained_disc:
            self.discriminator.load_state_dict(torch.load(pre_trained_disc))
        else:
            self.discriminator.apply(Utils.weights_init)
        if pre_trained_gen:
            self.generator.load_state_dict(torch.load(pre_trained_gen))
        else:
            self.generator.apply(Utils.weights_init)
        if dataset == "birds":
            # self.dataset = Text2ImageDataset(config['birds_dataset_path'], split=split)
            # self.dataset=Text2ImageDataset(r'/kaggle/working/birds.hdf5', split=split)
            self.dataset = Text2ImageDataset(
                r"/kaggle/input/birdss/birds.hdf5", split=split
            )  # 源数据
        elif dataset == "flowers":
            print("error")
            return 0
            # self.dataset = Text2ImageDataset(config['flowers_dataset_path'], split=split)
        else:
            print("Dataset not supported, please select either birds or flowers.")
            exit()
        self.noise_dim = 100
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.lr = lr
        self.beta1 = 0.5
        self.num_epochs = epochs
        self.DITER = diter
        self.l1_coef = l1_coef
        self.l2_coef = l2_coef
        self.data_loader = DataLoader(
            self.dataset,
            batch_size=self.batch_size,
            shuffle=True,
            num_workers=self.num_workers,
        )
        self.optimD = torch.optim.Adam(
            self.discriminator.parameters(), lr=self.lr, betas=(self.beta1, 0.999)
        )
        self.optimG = torch.optim.Adam(
            self.generator.parameters(), lr=self.lr, betas=(self.beta1, 0.999)
        )
        # print(vis_screen)#gan
        # print("pause0")
        # input()
        self.logger = Logger(vis_screen)  # 这里涉及到了visdom
        # print("pause1")
        # input()
        self.checkpoints_path = "checkpoints"
        self.save_path = save_path
        self.type = type

    def train(self, cls=False):
        if self.type == "wgan":
            self._train_wgan(cls)
        elif self.type == "gan":
            self._train_gan(cls)
        elif self.type == "vanilla_wgan":
            self._train_vanilla_wgan()
        elif self.type == "vanilla_gan":
            self._train_vanilla_gan()

    def _train_wgan(self, cls):
        one = torch.FloatTensor([1])
        mone = one * -1
        one = Variable(one).cuda()
        mone = Variable(mone).cuda()
        gen_iteration = 0
        for epoch in range(self.num_epochs):
            iterator = 0
            data_iterator = iter(self.data_loader)
            while iterator < len(self.data_loader):
                if gen_iteration < 25 or gen_iteration % 500 == 0:
                    d_iter_count = 100
                else:
                    d_iter_count = self.DITER
                d_iter = 0
                # Train the discriminator
                while d_iter < d_iter_count and iterator < len(self.data_loader):
                    d_iter += 1
                    for p in self.discriminator.parameters():
                        p.requires_grad = True
                    self.discriminator.zero_grad()
                    sample = next(data_iterator)
                    iterator += 1
                    right_images = sample["right_images"]
                    right_embed = sample["right_embed"]
                    wrong_images = sample["wrong_images"]
                    right_images = Variable(right_images.float()).cuda()
                    right_embed = Variable(right_embed.float()).cuda()
                    wrong_images = Variable(wrong_images.float()).cuda()
                    outputs, _ = self.discriminator(right_images, right_embed)
                    real_loss = torch.mean(outputs)
                    real_loss.backward(mone)
                    if cls:
                        outputs, _ = self.discriminator(wrong_images, right_embed)
                        wrong_loss = torch.mean(outputs)
                        wrong_loss.backward(one)
                    noise = Variable(
                        torch.randn(right_images.size(0), self.noise_dim), volatile=True
                    ).cuda()
                    noise = noise.view(noise.size(0), self.noise_dim, 1, 1)
                    fake_images = Variable(self.generator(right_embed, noise).data)
                    outputs, _ = self.discriminator(fake_images, right_embed)
                    fake_loss = torch.mean(outputs)
                    fake_loss.backward(one)
                    ## NOTE: Pytorch had a bug with gradient penalty at the time of this project development
                    ##       , uncomment the next two lines and remove the params clamping below if you want to try gradient penalty
                    # gp = Utils.compute_GP(self.discriminator, right_images.data, right_embed, fake_images.data, LAMBDA=10)
                    # gp.backward()
                    d_loss = real_loss - fake_loss
                    if cls:
                        d_loss = d_loss - wrong_loss
                    self.optimD.step()
                    for p in self.discriminator.parameters():
                        p.data.clamp_(-0.01, 0.01)
                # Train Generator
                for p in self.discriminator.parameters():
                    p.requires_grad = False
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(right_embed, noise)
                outputs, _ = self.discriminator(fake_images, right_embed)
                g_loss = torch.mean(outputs)
                g_loss.backward(mone)
                g_loss = -g_loss
                self.optimG.step()
                gen_iteration += 1
                self.logger.draw(right_images, fake_images)
                self.logger.log_iteration_wgan(
                    epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss
                )
            self.logger.plot_epoch(gen_iteration)
            if (epoch + 1) % 50 == 0:
                Utils.save_checkpoint(
                    self.discriminator, self.generator, self.checkpoints_path, epoch
                )

    def _train_gan(self, cls):
        # print("pause2")
        criterion = nn.BCELoss()
        l2_loss = nn.MSELoss()
        l1_loss = nn.L1Loss()
        iteration = 0
        for epoch in range(self.num_epochs):
            for sample in self.data_loader:
                # print("pause3")
                # input()
                iteration += 1
                right_images = sample["right_images"]
                right_embed = sample["right_embed"]
                wrong_images = sample["wrong_images"]
                right_images = Variable(right_images.float()).cuda()
                right_embed = Variable(right_embed.float()).cuda()
                wrong_images = Variable(wrong_images.float()).cuda()
                # print("pause4")
                # input()
                real_labels = torch.ones(right_images.size(0))
                fake_labels = torch.zeros(right_images.size(0))
                # ======== One sided label smoothing ==========
                # Helps preventing the discriminator from overpowering the
                # generator adding penalty when the discriminator is too confident
                # =============================================
                smoothed_real_labels = torch.FloatTensor(
                    Utils.smooth_label(real_labels.numpy(), -0.1)
                )
                real_labels = Variable(real_labels).cuda()
                smoothed_real_labels = Variable(smoothed_real_labels).cuda()
                fake_labels = Variable(fake_labels).cuda()
                # Train the discriminator
                self.discriminator.zero_grad()
                outputs, activation_real = self.discriminator(right_images, right_embed)
                real_loss = criterion(outputs, smoothed_real_labels)
                real_score = outputs
                if cls:
                    outputs, _ = self.discriminator(wrong_images, right_embed)
                    wrong_loss = criterion(outputs, fake_labels)
                    wrong_score = outputs
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(right_embed, noise)
                outputs, _ = self.discriminator(fake_images, right_embed)
                fake_loss = criterion(outputs, fake_labels)
                fake_score = outputs
                d_loss = real_loss + fake_loss
                if cls:
                    d_loss = d_loss + wrong_loss
                d_loss.backward()
                self.optimD.step()
                # Train the generator
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(right_embed, noise)
                outputs, activation_fake = self.discriminator(fake_images, right_embed)
                _, activation_real = self.discriminator(right_images, right_embed)
                activation_fake = torch.mean(activation_fake, 0)
                activation_real = torch.mean(activation_real, 0)
                # ======= Generator Loss function============
                # This is a customized loss function, the first term is the regular cross entropy loss
                # The second term is feature matching loss, this measure the distance between the real and generated
                # images statistics by comparing intermediate layers activations
                # The third term is L1 distance between the generated and real images, this is helpful for the conditional case
                # because it links the embedding feature vector directly to certain pixel values.
                # ===========================================
                g_loss = (
                    criterion(outputs, real_labels)
                    + self.l2_coef * l2_loss(activation_fake, activation_real.detach())
                    + self.l1_coef * l1_loss(fake_images, right_images)
                )
                g_loss.backward()
                self.optimG.step()
                # print("pause5")
                if iteration % 5 == 0:
                    # print("pause6")
                    self.logger.log_iteration_gan(
                        epoch, d_loss, g_loss, real_score, fake_score
                    )
                    # print("pause7")
                    self.logger.draw(right_images, fake_images)  # 大量警告在此处
                    # print("pause8")
            # 多次训练后进入这里
            self.logger.plot_epoch_w_scores(epoch)
            if (epoch) % 10 == 0:
                Utils.save_checkpoint(
                    self.discriminator,
                    self.generator,
                    self.checkpoints_path,
                    self.save_path,
                    epoch,
                )

    def _train_vanilla_wgan(self):
        one = Variable(torch.FloatTensor([1])).cuda()
        mone = one * -1
        gen_iteration = 0
        for epoch in range(self.num_epochs):
            iterator = 0
            data_iterator = iter(self.data_loader)
            while iterator < len(self.data_loader):
                if gen_iteration < 25 or gen_iteration % 500 == 0:
                    d_iter_count = 100
                else:
                    d_iter_count = self.DITER
                d_iter = 0
                # Train the discriminator
                while d_iter < d_iter_count and iterator < len(self.data_loader):
                    d_iter += 1
                    for p in self.discriminator.parameters():
                        p.requires_grad = True
                    self.discriminator.zero_grad()
                    sample = next(data_iterator)
                    iterator += 1
                    right_images = sample["right_images"]
                    right_images = Variable(right_images.float()).cuda()
                    outputs, _ = self.discriminator(right_images)
                    real_loss = torch.mean(outputs)
                    real_loss.backward(mone)
                    noise = Variable(
                        torch.randn(right_images.size(0), self.noise_dim), volatile=True
                    ).cuda()
                    noise = noise.view(noise.size(0), self.noise_dim, 1, 1)
                    fake_images = Variable(self.generator(noise).data)
                    outputs, _ = self.discriminator(fake_images)
                    fake_loss = torch.mean(outputs)
                    fake_loss.backward(one)
                    ## NOTE: Pytorch had a bug with gradient penalty at the time of this project development
                    ##       , uncomment the next two lines and remove the params clamping below if you want to try gradient penalty
                    # gp = Utils.compute_GP(self.discriminator, right_images.data, right_embed, fake_images.data, LAMBDA=10)
                    # gp.backward()
                    d_loss = real_loss - fake_loss
                    self.optimD.step()
                    for p in self.discriminator.parameters():
                        p.data.clamp_(-0.01, 0.01)
                # Train Generator
                for p in self.discriminator.parameters():
                    p.requires_grad = False
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(noise)
                outputs, _ = self.discriminator(fake_images)
                g_loss = torch.mean(outputs)
                g_loss.backward(mone)
                g_loss = -g_loss
                self.optimG.step()
                gen_iteration += 1
                self.logger.draw(right_images, fake_images)
                self.logger.log_iteration_wgan(
                    epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss
                )
            self.logger.plot_epoch(gen_iteration)
            if (epoch + 1) % 50 == 0:
                Utils.save_checkpoint(
                    self.discriminator, self.generator, self.checkpoints_path, epoch
                )

    def _train_vanilla_gan(self):
        criterion = nn.BCELoss()
        l2_loss = nn.MSELoss()
        l1_loss = nn.L1Loss()
        iteration = 0
        for epoch in range(self.num_epochs):
            for sample in self.data_loader:
                iteration += 1
                right_images = sample["right_images"]
                right_images = Variable(right_images.float()).cuda()
                real_labels = torch.ones(right_images.size(0))
                fake_labels = torch.zeros(right_images.size(0))
                # ======== One sided label smoothing ==========
                # Helps preventing the discriminator from overpowering the
                # generator adding penalty when the discriminator is too confident
                # =============================================
                smoothed_real_labels = torch.FloatTensor(
                    Utils.smooth_label(real_labels.numpy(), -0.1)
                )
                real_labels = Variable(real_labels).cuda()
                smoothed_real_labels = Variable(smoothed_real_labels).cuda()
                fake_labels = Variable(fake_labels).cuda()
                # Train the discriminator
                self.discriminator.zero_grad()
                outputs, activation_real = self.discriminator(right_images)
                real_loss = criterion(outputs, smoothed_real_labels)
                real_score = outputs
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(noise)
                outputs, _ = self.discriminator(fake_images)
                fake_loss = criterion(outputs, fake_labels)
                fake_score = outputs
                d_loss = real_loss + fake_loss
                d_loss.backward()
                self.optimD.step()
                # Train the generator
                self.generator.zero_grad()
                noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
                noise = noise.view(noise.size(0), 100, 1, 1)
                fake_images = self.generator(noise)
                outputs, activation_fake = self.discriminator(fake_images)
                _, activation_real = self.discriminator(right_images)
                activation_fake = torch.mean(activation_fake, 0)
                activation_real = torch.mean(activation_real, 0)
                # ======= Generator Loss function============
                # This is a customized loss function, the first term is the regular cross entropy loss
                # The second term is feature matching loss, this measure the distance between the real and generated
                # images statistics by comparing intermediate layers activations
                # The third term is L1 distance between the generated and real images, this is helpful for the conditional case
                # because it links the embedding feature vector directly to certain pixel values.
                g_loss = (
                    criterion(outputs, real_labels)
                    + self.l2_coef * l2_loss(activation_fake, activation_real.detach())
                    + self.l1_coef * l1_loss(fake_images, right_images)
                )
                g_loss.backward()
                self.optimG.step()
                if iteration % 5 == 0:
                    self.logger.log_iteration_gan(
                        epoch, d_loss, g_loss, real_score, fake_score
                    )
                    self.logger.draw(right_images, fake_images)
            self.logger.plot_epoch_w_scores(iteration)
            if (epoch) % 50 == 0:
                Utils.save_checkpoint(
                    self.discriminator, self.generator, self.checkpoints_path, epoch
                )

    def predict(self):
        i = 0
        for sample in self.data_loader:
            right_images = sample["right_images"]
            right_embed = sample["right_embed"]
            txt = sample["txt"]
            #             if not os.path.exists('results/{0}'.format(self.save_path)):
            #                 os.makedirs('results/{0}'.format(self.save_path))
            right_images = Variable(right_images.float()).cuda()
            right_embed = Variable(right_embed.float()).cuda()
            # Train the generator
            noise = Variable(torch.randn(right_images.size(0), 100)).cuda()
            noise = noise.view(noise.size(0), 100, 1, 1)
            fake_images = self.generator(right_embed, noise)
            self.logger.draw(right_images, fake_images)
            for image, t in zip(fake_images, txt):
                im = Image.fromarray(
                    image.data.mul_(127.5)
                    .add_(127.5)
                    .byte()
                    .permute(1, 2, 0)
                    .cpu()
                    .numpy()
                )
                # im.save('{0}/{1}.jpg'.format(self.save_path, t.replace("/", "")[:100]))
                if i % 1000 == 0:
                    print(i)
                    im.save(
                        "{0}/{1}.jpg".format(
                            self.save_path, str(i).replace("/", "")[:100]
                        )
                    )
                i = i + 1
                # print('{0}/{1}/.jpg'.format(self.save_path, t.replace("/", "")[:100]))
                # print(t)
                # input()
        print("predict end")


# txt2image_dataset
import os
import io
from torch.utils.data import Dataset, DataLoader
import h5py
import numpy as np
import pdb
from PIL import Image
import torch
from torch.autograd import Variable
import pdb
import torch.nn.functional as F


class Text2ImageDataset(Dataset):
    def __init__(self, datasetFile, transform=None, split=0):
        self.datasetFile = datasetFile
        self.transform = transform
        self.dataset = None
        self.dataset_keys = None
        self.split = "train" if split == 0 else "valid" if split == 1 else "test"
        self.h5py2int = lambda x: int(np.array(x))

    def __len__(self):
        f = h5py.File(self.datasetFile, "r")
        self.dataset_keys = [str(k) for k in f[self.split].keys()]
        length = len(f[self.split])
        f.close()
        return length

    def __getitem__(self, idx):
        if self.dataset is None:
            self.dataset = h5py.File(self.datasetFile, mode="r")
            self.dataset_keys = [str(k) for k in self.dataset[self.split].keys()]
        example_name = self.dataset_keys[idx]
        example = self.dataset[self.split][example_name]
        # pdb.set_trace()
        right_image = bytes(np.array(example["img"]))
        right_embed = np.array(example["embeddings"], dtype=float)
        wrong_image = bytes(np.array(self.find_wrong_image(example["class"])))
        inter_embed = np.array(self.find_inter_embed())
        right_image = Image.open(io.BytesIO(right_image)).resize((64, 64))
        wrong_image = Image.open(io.BytesIO(wrong_image)).resize((64, 64))
        right_image = self.validate_image(right_image)
        wrong_image = self.validate_image(wrong_image)
        txt = np.array(example["txt"]).astype(str)
        sample = {
            "right_images": torch.FloatTensor(right_image),
            "right_embed": torch.FloatTensor(right_embed),
            "wrong_images": torch.FloatTensor(wrong_image),
            "inter_embed": torch.FloatTensor(inter_embed),
            "txt": str(txt),
        }
        sample["right_images"] = sample["right_images"].sub_(127.5).div_(127.5)
        sample["wrong_images"] = sample["wrong_images"].sub_(127.5).div_(127.5)
        return sample

    def find_wrong_image(self, category):
        idx = np.random.randint(len(self.dataset_keys))
        example_name = self.dataset_keys[idx]
        example = self.dataset[self.split][example_name]
        _category = example["class"]
        if _category != category:
            return example["img"]
        return self.find_wrong_image(category)

    def find_inter_embed(self):
        idx = np.random.randint(len(self.dataset_keys))
        example_name = self.dataset_keys[idx]
        example = self.dataset[self.split][example_name]
        return example["embeddings"]

    def validate_image(self, img):
        img = np.array(img, dtype=float)
        if len(img.shape) < 3:
            rgb = np.empty((64, 64, 3), dtype=np.float32)
            rgb[:, :, 0] = img
            rgb[:, :, 1] = img
            rgb[:, :, 2] = img
            img = rgb
        return img.transpose(2, 0, 1)


### runtime.py
# from trainer import Trainer
import argparse
from PIL import Image
import os

parser = argparse.ArgumentParser()
parser.add_argument("--type", default="gan")
parser.add_argument("--lr", default=0.0002, type=float)  # 0.0002
parser.add_argument("--l1_coef", default=50, type=float)
parser.add_argument("--l2_coef", default=100, type=float)
parser.add_argument("--diter", default=5, type=int)
parser.add_argument("--cls", default=False, action="store_true")
parser.add_argument("--vis_screen", default="gan")
parser.add_argument("--save_path", default=r"/kaggle/working")
# parser.add_argument("--inference", default=False, action='store_true')#训练或者预测
parser.add_argument("--inference", default=True, action="store_true")
parser.add_argument("--pre_trained_disc", default=None)
parser.add_argument("--pre_trained_gen", default=None)
parser.add_argument("--dataset", default="birds")
parser.add_argument("--split", default=0, type=int)
parser.add_argument("--batch_size", default=64, type=int)  # 64
parser.add_argument("--num_workers", default=0, type=int)
parser.add_argument("--epochs", default=200, type=int)  # 200
# args = parser.parse_args()
args = parser.parse_known_args()[0]
# print(args)
trainer = Trainer(
    type=args.type,
    dataset=args.dataset,
    split=args.split,
    lr=args.lr,
    diter=args.diter,
    vis_screen=args.vis_screen,
    save_path=args.save_path,
    l1_coef=args.l1_coef,
    l2_coef=args.l2_coef,
    pre_trained_disc=args.pre_trained_disc,
    pre_trained_gen=args.pre_trained_gen,
    batch_size=args.batch_size,
    num_workers=args.num_workers,
    epochs=args.epochs,
)
# 删除图表文件
import os

path1 = r"/kaggle/working/Discriminator.txt"
path2 = r"/kaggle/working/Generator.txt"
path3 = r"/kaggle/working/D(X).txt"
path4 = r"/kaggle/working/D(G(X)).txt"
if os.path.exists(path1):
    os.remove(path1)
if os.path.exists(path2):
    os.remove(path2)
if os.path.exists(path3):
    os.remove(path3)
if os.path.exists(path4):
    os.remove(path4)
# 创建npy文件夹
if not os.path.exists(r"/kaggle/working/npy"):
    os.mkdir(r"/kaggle/working/npy")
# 创建图表txt
path1 = r"/kaggle/working/Discriminator.txt"
path2 = r"/kaggle/working/Generator.txt"
path3 = r"/kaggle/working/D(X).txt"
path4 = r"/kaggle/working/D(G(X)).txt"
if not os.path.exists(path1):
    with open(path1, "w", encoding="utf-8") as f:
        f.write("")
if not os.path.exists(path2):
    with open(path2, "w", encoding="utf-8") as f:
        f.write("")
if not os.path.exists(path3):
    with open(path3, "w", encoding="utf-8") as f:
        f.write("")
if not os.path.exists(path4):
    with open(path4, "w", encoding="utf-8") as f:
        f.write("")
# 一次Epoch写入一次txt文件
# if not args.inference:
#     print("train")
#     trainer.train(args.cls)
# else:
#     print("predict")
#     trainer.predict()
trainer.train(args.cls)
# trainer.predict()
print("end")
# 下面是额外的内容
print("123")
# #删除图表文件
# import os
# path1=r'/kaggle/working/Discriminator.txt'
# path2=r'/kaggle/working/Generator.txt'
# path3=r'/kaggle/working/D(X).txt'
# path4=r'/kaggle/working/D(G(X)).txt'
# if os.path.exists(path1):
#     os.remove(path1)
# if os.path.exists(path2):
#     os.remove(path2)
# if os.path.exists(path3):
#     os.remove(path3)
# if os.path.exists(path4):
#     os.remove(path4)
# #创建图表文件
# import os
# path1=r'/kaggle/working/Discriminator.txt'
# path2=r'/kaggle/working/Generator.txt'
# path3=r'/kaggle/working/D(X).txt'
# path4=r'/kaggle/working/D(G(X)).txt'
# if not os.path.exists(path1):
#     with open(path1, "w", encoding="utf-8") as f:
#         f.write("")
# if not os.path.exists(path2):
#     with open(path2, "w", encoding="utf-8") as f:
#         f.write("")
# if not os.path.exists(path3):
#     with open(path3, "w", encoding="utf-8") as f:
#         f.write("")
# if not os.path.exists(path4):
#     with open(path4, "w", encoding="utf-8") as f:
#         f.write("")
# 下面是额外的内容
# pip show torchfile#查看安装包的版本
# pip uninstall visdom#卸载visdom
# pip list --outdated#查看是否有新版本更新
# pip list#查看所有包的版本
# import socket
# # 获取本机计算机名称
# hostname = socket.gethostname()
# # 获取本机ip
# ip = socket.gethostbyname(hostname)
# print(hostname)
# print(ip)
# #配套在pycharm上面使用
# import cv2
# import numpy as np
# import visdom
# #启动服务python -m visdom.server
# # #读取npy文件
# viz = visdom.Visdom()
# def fun1():
#     print("fun1")
#     # viz.text('Hello, world!')
#     # viz.image(np.ones((3, 10, 10)))
#     a=np.load(r'D:\.android\2000a.npy')
#     b=np.load(r'D:\.android\2000b.npy')
#     viz.images(a)
#     viz.images(b)
# #读取txt文件并且绘图
# def fun2():
#     plots={}
#     def plot(var_name, split_name, x, y, xlabel='epoch'):
#         if var_name not in plots:
#             plots[var_name] = viz.line(X=np.array([x, x]), Y=np.array([y, y]), opts=dict(
#                 legend=[split_name],
#                 title=var_name,
#                 xlabel=xlabel,
#                 ylabel=var_name
#             ))
#             #viz.line(X=np.array([x, x]), Y=np.array([y, y]))
#         else:
#             viz.line(X=np.array([x, x]), Y=np.array([y, y]),win=plots[var_name],
#                                       name=split_name,update='append')
#             # viz.updateTrace(X=np.array([x]), Y=np.array([y]), win=plots[var_name],
#             #                         name=split_name)
#     path1=r'D:\.android\D(G(X)).txt'
#     path2=r'D:\.android\D(X).txt'
#     path3=r'D:\.android\Discriminator.txt'
#     path4=r'D:\.android\Generator.txt'
#     i=0
#     with open(path1, 'r') as f:
#         for line in f:
#             line = float(line)
#             #print(line)
#             plot('D(G(X))', 'train', i, line)
#             #print(line)
#             i+=1
#     i=0
#     with open(path2, 'r') as f:
#         for line in f:
#             line = float(line)
#             plot('D(X)', 'train', i, line)
#             i+=1
#     i=0
#     with open(path3, 'r') as f:
#         for line in f:
#             line = float(line)
#             plot('Discriminator', 'train', i, line)
#             i+=1
#     i=0
#     with open(path4, 'r') as f:
#         for line in f:
#             line = float(line)
#             plot('Generator', 'train', i, line)
#             i+=1
#     #self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean())
#     #self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean())
#     #self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean())
#     #self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean())
# fun1()
# #fun2()
# import os
# if not os.path.exists(r'/kaggle/working/npy'):
#     os.mkdir(r'/kaggle/working/npy')
# 检查图片是否正确
# aa=np.load("/kaggle/working/img2.npy")
# print(aa.shape)
# print(aa)
# pip install wmi
# pip install pypiwin32
# #查看kaggle的版本
# # PythonVersion:python2.7
# # filename:sys_info.py
# import sys
# import wmi
# import socket
# import platform
# import imp
# imp.reload(sys)
# #reload(sys)
# #sys.setdefaultencoding('utf-8')
# c = wmi.WMI()
# # 系统信息
# print(u'操作系统名称' + platform.platform()[:-(len(platform.version()) + 1)])
# print(u'操作系统版本号' + platform.version())
# print(u'操作系统的位数' + platform.architecture()[0])
# hostname = socket.getfqdn(socket.gethostname())
# ip = socket.gethostbyname(hostname)
# print('ip:' + ip)
# # CPU信息
# def get_CPU():
#     cpumsg = []
#     for cpu in c.Win32_Processor():
#         tmpmsg = {}
#         tmpmsg['Name'] = cpu.Name
#         cpumsg.append(tmpmsg)
#     print(cpumsg)
# # 内存信息
# def get_PhysicalMemory():
#     memorys = []
#     for mem in c.Win32_PhysicalMemory():
#         tmpmsg = {}
#         tmpmsg['Tag'] = mem.Tag
#         tmpmsg['ConfiguredClockSpeed'] = str(mem.ConfiguredClockSpeed) + 'MHz'
#         memorys.append(tmpmsg)
#     print(memorys)
# # 显卡信息
# def get_video():
#     videos = []
#     for v in c.Win32_VideoController():
#         tmpmsg = {}
#         tmpmsg['Caption'] = v.Caption
#         tmpmsg['AdapterRAM'] = str(abs(v.AdapterRAM) / (1024 ** 3)) + 'G'
#         videos.append(tmpmsg)
#     print(videos)
# # 网卡mac地址
# def get_MacAddress():
#     macs = []
#     for n in c.Win32_NetworkAdapter():
#         mactmp = n.MACAddress
#         if mactmp and len(mactmp.strip()) > 5:
#             tmpmsg = {}
#             tmpmsg['ProductName'] = n.ProductName
#             tmpmsg['NetConnectionID'] = n.NetConnectionID
#             tmpmsg['MACAddress'] = n.MACAddress
#             macs.append(tmpmsg)
#     print(macs)
# def main():
#     get_CPU()
#     get_PhysicalMemory()
#     get_video()
#     get_MacAddress()
# if __name__ == '__main__':
#     main()
# #下载运行结果
# import os
# os.chdir(r'/kaggle/working/npy')
# print(os.getcwd())
# print(os.listdir("/kaggle/working/npy"))
# from IPython.display import FileLink
# #FileLink('a colorful bird containing a bright blue head, nape neck and back, blue white and black feathers, bu.jpg')
# FileLink('2000b.npy')
# from torch.utils.data import DataLoader
# birds_dataset_path='/kaggle/working/birds.hdf5'
# dataset = Text2ImageDataset(birds_dataset_path,split=split)
# #注意调整batch_size和num_workers不然cuda 虚拟环境的共享内存不足要报错
# #或者多线程操作时就有可能导致线程套线程，容易出现死锁的情况
# data_loader = DataLoader(dataset, batch_size=64,shuffle=True,num_workers=0)
# import h5py
# datasetFile='/kaggle/working/birds.hdf5'
# f = h5py.File(datasetFile, 'r')
# split='train'
# print(f)
# print(split)
# print(f.keys())
# print("123")
# print(f['train'].keys())
# print("ok")
# dataset_keys = [str(k) for k in f[split].keys()]
# print(dataset_keys)
