util.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:variational-autoencoder 作者: musyoku 项目源码 文件源码
def load_labeled_images(image_dir, convert_to_grayscale=True, dist="bernoulli"):
    dataset = []
    labels = []
    fs = os.listdir(image_dir)
    i = 0
    for fn in fs:
        m = re.match("([0-9]+)_.+", fn)
        label = int(m.group(1))
        f = open("%s/%s" % (image_dir, fn), "rb")
        if convert_to_grayscale:
            img = np.asarray(Image.open(StringIO(f.read())).convert("L"), dtype=np.float32) / 255.0
        else:
            img = np.asarray(Image.open(StringIO(f.read())).convert("RGB"), dtype=np.float32).transpose(2, 0, 1) / 255.0
        if dist == "bernoulli":
            # Sampling
            img = preprocessing.binarize(img, threshold=0.5)
            pass
        elif dist == "gaussian":
            pass
        else:
            raise Exception()
        dataset.append(img)
        labels.append(label)
        f.close()
        i += 1
        if i % 100 == 0:
            sys.stdout.write("\rloading images...({:d} / {:d})".format(i, len(fs)))
            sys.stdout.flush()
    sys.stdout.write("\n")
    return dataset, labels
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号