import os
import glob
from PIL import Image
help_msg = """
The dataset can be downloaded from https://cityscapes-dataset.com.
Please download the datasets [gtFine_trainvaltest.zip] and [leftImg8bit_trainvaltest.zip] and unzip them.
gtFine contains the semantics segmentations. Use --gtFine_dir to specify the path to the unzipped gtFine_trainvaltest directory.
leftImg8bit contains the dashcam photographs. Use --leftImg8bit_dir to specify the path to the unzipped leftImg8bit_trainvaltest directory.
The processed images will be placed at --output_dir.
Example usage:
python prepare_cityscapes_dataset.py --gitFine_dir ./gtFine/ --leftImg8bit_dir ./leftImg8bit --output_dir ./datasets/cityscapes/
"""
def load_resized_img(path):
return Image.open(path).convert('RGB').resize((256, 256))
def check_matching_pair(segmap_path, photo_path):
segmap_identifier = os.path.basename(segmap_path).replace('_gtFine_color', '')
photo_identifier = os.path.basename(photo_path).replace('_leftImg8bit', '')
assert segmap_identifier == photo_identifier, \
"[%s] and [%s] don't seem to be matching. Aborting." % (segmap_path, photo_path)
def process_cityscapes(gtFine_dir, leftImg8bit_dir, output_dir, phase):
save_phase = 'test' if phase == 'val' else 'train'
savedir = os.path.join(output_dir, save_phase)
os.makedirs(savedir, exist_ok=True)
os.makedirs(savedir + 'A', exist_ok=True)
os.makedirs(savedir + 'B', exist_ok=True)
print("Directory structure prepared at %s" % output_dir)
segmap_expr = os.path.join(gtFine_dir, phase) + "/*/*_color.png"
segmap_paths = glob.glob(segmap_expr)
segmap_paths = sorted(segmap_paths)
photo_expr = os.path.join(leftImg8bit_dir, phase) + "/*/*_leftImg8bit.png"
photo_paths = glob.glob(photo_expr)
photo_paths = sorted(photo_paths)
assert len(segmap_paths) == len(photo_paths), \
"%d images that match [%s], and %d images that match [%s]. Aborting." % (len(segmap_paths), segmap_expr, len(photo_paths), photo_expr)
for i, (segmap_path, photo_path) in enumerate(zip(segmap_paths, photo_paths)):
check_matching_pair(segmap_path, photo_path)
segmap = load_resized_img(segmap_path)
photo = load_resized_img(photo_path)
# data for pix2pix where the two images are placed side-by-side
sidebyside = Image.new('RGB', (512, 256))
sidebyside.paste(segmap, (256, 0))
sidebyside.paste(photo, (0, 0))
savepath = os.path.join(savedir, "%d.jpg" % i)
sidebyside.save(savepath, format='JPEG', subsampling=0, quality=100)
# data for cyclegan where the two images are stored at two distinct directories
savepath = os.path.join(savedir + 'A', "%d_A.jpg" % i)
photo.save(savepath, format='JPEG', subsampling=0, quality=100)
savepath = os.path.join(savedir + 'B', "%d_B.jpg" % i)
segmap.save(savepath, format='JPEG', subsampling=0, quality=100)
if i % (len(segmap_paths) // 10) == 0:
print("%d / %d: last image saved at %s, " % (i, len(segmap_paths), savepath))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--gtFine_dir', type=str, required=True,
help='Path to the Cityscapes gtFine directory.')
parser.add_argument('--leftImg8bit_dir', type=str, required=True,
help='Path to the Cityscapes leftImg8bit_trainvaltest directory.')
parser.add_argument('--output_dir', type=str, required=True,
default='./datasets/cityscapes',
help='Directory the output images will be written to.')
opt = parser.parse_args()
print(help_msg)
print('Preparing Cityscapes Dataset for val phase')
process_cityscapes(opt.gtFine_dir, opt.leftImg8bit_dir, opt.output_dir, "val")
print('Preparing Cityscapes Dataset for train phase')
process_cityscapes(opt.gtFine_dir, opt.leftImg8bit_dir, opt.output_dir, "train")
print('Done')
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本项目的代码质量非常高,你可以基于它去魔改,或者实现你毕设中想要实现的功 能。风格迁移的核心思想就是:可以从一副图像中提取出风格 style(比如梵高的夜空 风格)以及内容 content(比如你能在画中看出河边有匹马)。你可以告诉计算机,把 A 用 B 的风格再画一遍。本网络可以从绘画中生成照片、将马变成斑马、将夏天的场景变为冬天等风格转换。
资源推荐
资源详情
资源评论
收起资源包目录
CycleGAN.zip (46个子文件)
CycleGAN
options.lua 7KB
pretrained_models
download_vgg.sh 340B
download_model.sh 526B
places_vgg.prototxt 8KB
data
base_data_loader.lua 1KB
unaligned_data_loader.lua 1KB
donkey_folder.lua 6KB
data_util.lua 764B
dataset.lua 14KB
data.lua 3KB
aligned_data_loader.lua 1KB
train.lua 6KB
LICENSE 3KB
examples
train_maps.sh 781B
test_vangogh_style_on_ae_photos.sh 845B
datasets
bibtex
facades.tex 253B
cityscapes.tex 393B
download_dataset.sh 1KB
prepare_cityscapes_dataset.py 4KB
说明文档.md 13KB
models
content_gan_model.lua 7KB
pix2pix_model.lua 7KB
base_model.lua 2KB
architectures.lua 18KB
bigan_model.lua 9KB
cycle_gan_model.lua 13KB
one_direction_test_model.lua 2KB
.gitignore 723B
imgs
photo_enhancement.jpg 1.1MB
horse2zebra.gif 7.33MB
objects.jpg 681KB
photo2painting.jpg 470KB
painting2photo.jpg 1.41MB
teaser.jpg 554KB
paper_thumbnail.jpg 9KB
season.jpg 407KB
failure_putin.jpg 98KB
util
VGG_preprocess.lua 925B
util.lua 6KB
InstanceNormalization.lua 3KB
cudnn_convert_custom.lua 2KB
visualizer.lua 2KB
content_loss.lua 3KB
image_pool.lua 1KB
plot_util.lua 931B
test.lua 4KB
共 46 条
- 1
资源评论
- qq_428444102023-02-27资源有很好的参考价值,总算找到了自己需要的资源啦。
- llllllumos2024-02-26终于找到了超赞的宝藏资源,果断冲冲冲,支持!
- cjxhxxxx2024-04-07非常有用的资源,有一定的参考价值,受益匪浅,值得下载。
振华OPPO
- 粉丝: 27w+
- 资源: 533
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功