• 大小: 35.95MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-11
  • 语言: 其他
  • 标签: GAN  Attention  Network  

资源简介

该代码是CVPR2018一篇关于文本到图像合成的文章,经过测试可以使用

资源截图

代码片段和文件信息

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals


from nltk.tokenize import RegexpTokenizer
from collections import defaultdict
from miscc.config import cfg

import torch
import torch.utils.data as data
from torch.autograd import Variable
import torchvision.transforms as transforms

import os
import sys
import numpy as np
import pandas as pd
from PIL import Image
import numpy.random as random
if sys.version_info[0] == 2:
    import cPickle as pickle
else:
    import pickle


def prepare_data(data):
    imgs captions captions_lens class_ids keys = data

    # sort data by the length in a decreasing order
    sorted_cap_lens sorted_cap_indices = \
        torch.sort(captions_lens 0 True)

    real_imgs = []
    for i in range(len(imgs)):
        imgs[i] = imgs[i][sorted_cap_indices]
        if cfg.CUDA:
            real_imgs.append(Variable(imgs[i]).cuda())
        else:
            real_imgs.append(Variable(imgs[i]))

    captions = captions[sorted_cap_indices].squeeze()
    class_ids = class_ids[sorted_cap_indices].numpy()
    # sent_indices = sent_indices[sorted_cap_indices]
    keys = [keys[i] for i in sorted_cap_indices.numpy()]
    # print(‘keys‘ type(keys) keys[-1])  # list
    if cfg.CUDA:
        captions = Variable(captions).cuda()
        sorted_cap_lens = Variable(sorted_cap_lens).cuda()
    else:
        captions = Variable(captions)
        sorted_cap_lens = Variable(sorted_cap_lens)

    return [real_imgs captions sorted_cap_lens
            class_ids keys]


def get_imgs(img_path imsize bbox=None
             transform=None normalize=None):
    img = Image.open(img_path).convert(‘RGB‘)
    width height = img.size
    if bbox is not None:
        r = int(np.maximum(bbox[2] bbox[3]) * 0.75)
        center_x = int((2 * bbox[0] + bbox[2]) / 2)
        center_y = int((2 * bbox[1] + bbox[3]) / 2)
        y1 = np.maximum(0 center_y - r)
        y2 = np.minimum(height center_y + r)
        x1 = np.maximum(0 center_x - r)
        x2 = np.minimum(width center_x + r)
        img = img.crop([x1 y1 x2 y2])

    if transform is not None:
        img = transform(img)

    ret = []
    if cfg.GAN.B_DCGAN:
        ret = [normalize(img)]
    else:
        for i in range(cfg.TREE.BRANCH_NUM):
            # print(imsize[i])
            if i < (cfg.TREE.BRANCH_NUM - 1):
                re_img = transforms.Scale(imsize[i])(img)
            else:
                re_img = img
            ret.append(normalize(re_img))

    return ret


class TextDataset(data.Dataset):
    def __init__(self data_dir split=‘train‘
                 base_size=64
                 transform=None target_transform=None):
        self.transform = transform
        self.norm = transforms.Compose([
            transforms.ToTensor()
            transforms.Normalize((0.5 0.5 0.5) (0.5 0.5 0.5))])
        self.target_transform = target_transform
      

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-07 00:12  AttnGAN-master\
     文件          59  2018-05-07 00:12  AttnGAN-master\.gitignore
     目录           0  2018-05-07 00:12  AttnGAN-master\DAMSMencoders\
     文件          14  2018-05-07 00:12  AttnGAN-master\DAMSMencoders\.gitignore
     文件        1063  2018-05-07 00:12  AttnGAN-master\LICENSE
     文件        4461  2018-05-07 00:12  AttnGAN-master\README.md
     目录           0  2018-05-07 00:12  AttnGAN-master\code\
     文件          17  2018-05-07 00:12  AttnGAN-master\code\.gitignore
     文件        4091  2018-05-07 00:12  AttnGAN-master\code\GlobalAttention.py
     目录           0  2018-05-07 00:12  AttnGAN-master\code\cfg\
     目录           0  2018-05-07 00:12  AttnGAN-master\code\cfg\DAMSM\
     文件         569  2018-05-07 00:12  AttnGAN-master\code\cfg\DAMSM\bird.yml
     文件         552  2018-05-07 00:12  AttnGAN-master\code\cfg\DAMSM\coco.yml
     文件         657  2018-05-07 00:12  AttnGAN-master\code\cfg\bird_attn2.yml
     文件         678  2018-05-07 00:12  AttnGAN-master\code\cfg\bird_attnDCGAN2.yml
     文件         675  2018-05-07 00:12  AttnGAN-master\code\cfg\coco_attn2.yml
     文件         453  2018-05-07 00:12  AttnGAN-master\code\cfg\eval_bird.yml
     文件         462  2018-05-07 00:12  AttnGAN-master\code\cfg\eval_bird_attnDCGAN2.yml
     文件         433  2018-05-07 00:12  AttnGAN-master\code\cfg\eval_coco.yml
     文件       11122  2018-05-07 00:12  AttnGAN-master\code\datasets.py
     文件        5185  2018-05-07 00:12  AttnGAN-master\code\main.py
     目录           0  2018-05-07 00:12  AttnGAN-master\code\miscc\
     文件          70  2018-05-07 00:12  AttnGAN-master\code\miscc\__init__.py
     文件        2542  2018-05-07 00:12  AttnGAN-master\code\miscc\config.py
     文件        8135  2018-05-07 00:12  AttnGAN-master\code\miscc\losses.py
     文件       11025  2018-05-07 00:12  AttnGAN-master\code\miscc\utils.py
     文件       21623  2018-05-07 00:12  AttnGAN-master\code\model.py
     文件       10747  2018-05-07 00:12  AttnGAN-master\code\pretrain_DAMSM.py
     文件       22725  2018-05-07 00:12  AttnGAN-master\code\trainer.py
     目录           0  2018-05-07 00:12  AttnGAN-master\data\
     文件          14  2018-05-07 00:12  AttnGAN-master\data\.gitignore
............此处省略24个文件信息

评论

共有 条评论