8.GAN 和 DCGAN 网络生成图像


GAN 和 DCGAN 网络生成图像,采用python语言编写程序实现生成网络,并给出网络的具体训练代码
资源截图
代码片段和文件信息
“““
Modification of https://github.com/stanfordnlp/treelstm/blob/master/scripts/download.py

Downloads the following:
- Celeb-A dataset
- LSUN dataset
- MNIST dataset
“““

from __future__ import print_function
import os
import sys
import gzip
import json
import shutil
import zipfile
import argparse
import requests
import subprocess
from tqdm import tqdm
from six.moves import urllib

parser = argparse.ArgumentParser(description=‘Download dataset for DCGAN.‘)
parser.add_argument(‘datasets‘ metavar=‘N‘ type=str nargs=‘+‘ choices=[‘celebA‘ ‘lsun‘ ‘mnist‘]
           help=‘name of dataset to download [celebA lsun mnist]‘)

def download(url dirpath):
  filename = url.split(‘/‘)[-1]
  filepath = os.path.join(dirpath filename)
  u = urllib.request.urlopen(url)
  f = open(filepath ‘wb‘)
  filesize = int(u.headers[“Content-Length“])
  print(“Downloading: %s Bytes: %s“ % (filename filesize))

  downloaded = 0
  block_sz = 8192
  status_width = 70
  while True:
    buf = u.read(block_sz)
    if not buf:
      print(‘‘)
      break
    else:
      print(‘‘ end=‘
‘)
    downloaded += len(buf)
    f.write(buf)
    status = ((“[%-“ + str(status_width + 1) + “s] %3.2f%%“) %
      (‘=‘ * int(float(downloaded) / filesize * status_width) + ‘>‘ downloaded * 100. / filesize))
    print(status end=‘‘)
    sys.stdout.flush()
  f.close()
  return filepath

def download_file_from_google_drive(id destination):
  URL = “https://docs.google.com/uc?export=download“
  session = requests.Session()

  response = session.get(URL params={ ‘id‘: id } stream=True)
  token = get_confirm_token(response)

  if token:
    params = { ‘id‘ : id ‘confirm‘ : token }
    response = session.get(URL params=params stream=True)

  save_response_content(response destination)

def get_confirm_token(response):
  for key value in response.cookies.items():
    if key.startswith(‘download_warning‘):
      return value
  return None

def save_response_content(response destination chunk_size=32*1024):
  total_size = int(response.headers.get(‘content-length‘ 0))
  with open(destination “wb“) as f:
    for chunk in tqdm(response.iter_content(chunk_size) total=total_size
              unit=‘B‘ unit_scale=True desc=destination):
      if chunk: # filter out keep-alive new chunks
        f.write(chunk)

def unzip(filepath):
  print(“Extracting: “ + filepath)
  dirpath = os.path.dirname(filepath)
  with zipfile.ZipFile(filepath) as zf:
    zf.extractall(dirpath)
  os.remove(filepath)

def download_celeb_a(dirpath):
  data_dir = ‘celebA‘
  if os.path.exists(os.path.join(dirpath data_dir)):
    print(‘Found Celeb-A - skip‘)
    return

  filename drive_id  = “img_align_celeba.zip“ “0B7EVK8r0v71pZjFTYXZWM3FlRnM“
  save_path = os.path.join(dirpath filename)

  if os.path.exists(save_path):
    print(‘[*] {} already exists‘.format(save_path))
  else:
    download_file_from_google_drive(drive_id save_path)

  zip_dir = ‘‘
  with zipfile.ZipFile(save_path) as zf:
    zip_dir = z

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-07-22 02:35  chapter_8
     文件        8647  2018-07-22 02:35  chapter_8utils.py
     文件        3656  2018-07-22 02:35  chapter_8ops.py
     文件       20079  2018-07-22 02:35  chapter_8model.py
     文件        3945  2018-07-22 02:35  chapter_8main.py
     文件        5375  2018-07-22 02:35  chapter_8download.py
     文件        3338  2018-07-22 02:35  chapter_8README_eng.md
     文件        1766  2018-07-22 02:35  chapter_8README.md
     文件        1078  2018-07-22 02:35  chapter_8LICENSE
     文件      150616  2018-07-22 02:35  chapter_8DCGAN.png
     文件         989  2018-07-22 02:35  chapter_8.gitignore
     目录           0  2018-07-22 02:35  chapter_8web
     文件       28424  2018-07-22 02:35  chapter_8webindex.html
     文件         283  2018-07-22 02:35  chapter_8webapp.py
     目录           0  2018-07-22 02:35  chapter_8assets
     文件    15299008  2018-07-22 02:35  chapter_8assets raining.gif
     文件      529986  2018-07-22 02:35  chapter_8assets
esult_16_01_04_.png
     文件      522641  2018-07-22 02:35  chapter_8assets
esult_16_01_04.png
     文件      591739  2018-07-22 02:35  chapter_8assets
esult_16_01_03.png
     文件       27204  2018-07-22 02:35  chapter_8assetsmnist3.png
     文件       26911  2018-07-22 02:35  chapter_8assetsmnist2.png
     文件       28739  2018-07-22 02:35  chapter_8assetsmnist1.png
     文件      107790  2018-07-22 02:35  chapter_8assetsg_loss.png
     文件       98740  2018-07-22 02:35  chapter_8assetsd_loss.png
     文件      389671  2018-07-22 02:35  chapter_8assetsd_hist.png
     文件      354853  2018-07-22 02:35  chapter_8assetsd__hist.png
     文件      499759  2018-07-22 02:35  chapter_8assetscustom_dataset.png
     目录           0  2018-07-22 02:35  chapter_8webvideos
     文件     3870558  2018-07-22 02:35  chapter_8webvideos raining.mp4
     文件     1079329  2018-07-22 02:35  chapter_8webvideos
andom.mp4
     文件     1922773  2018-07-22 02:35  chapter_8webvideosackground.mp4
............此处省略60个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)