0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MNISTを画像保存する

0
Posted at

よく画像系のAIモデルモックを作る時、なんやかんだでMNISTをよく使う

そのままtorchvisionで読み込んでバイナリデータで使ってもいいのだけれど、画像ファイルにしておいた方が諸々検証しやすかったりする

だから画像ファイルとして残す方法をメモしておく
(この作業、割と毎回やっているので)

import os
from glob import glob

from PIL import Image
import torchvision


# 画像保存先フォルダ生成
for no in range(0, 10):
    os.makedirs(f'./mnist/{no}')

# MNIST読込
datasets = torchvision.datasets.MNIST(
    root='./data', download=True,
)

# 1フォルダ100枚迄保存したら終了とする
threshold = 100

for target, img in zip(datasets.targets, datasets.data):
    save_dir = f'./mnist/{target}'
    n = len(glob(f'{save_dir}/*'))
    
    if n >= threshold:
        continue

    Image.fromarray(img.numpy()).save(f'{save_dir}/label{n+1}.png')

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?