1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

畳み込みニューラルネットワーク(CNN)の訓練データを作成するツールを作ってみた - ③データリストの作成

Posted at

はじめに

  • 前回前々回に続いて、今回は訓練データとテストデータのファイルリストを作成を行います。
  • このファイルリストを元に実際は学習プログラムを実行していく想定で作成しました。

プログラム

filelist.py
# !/usr/local/bin/python3
# !-*- coding: utf-8 -*-

import os
import random
import numpy as np

def createFilelist():

    labels = {"gyoza":0, "pizza":1, "sushi":2}

    dir_path = os.path.dirname(os.path.abspath(__file__))

    for label_name, label_key in labels.items():

        files = os.listdir(label_name)
        random.shuffle(files)
        files = np.array(files)

        train, test = np.split(files, [int(files.size * 0.7)])

        print(
            'ラベル名: ' + label_name,
            '訓練データ数: ' + str(len(train)),
            'テストデータ数: ' + str(len(test)),
        )

        for file in train:

            name, ext = os.path.splitext(file)

            if ext != '.jpg':
                print('[' + file + ']: 不正なファイルが含まれています。')

            with open('train.txt', 'a') as f:
                text = '{0}/{1}/{2}\t{3}\n'.format(dir_path, label_name, file, labels[label_name])
                f.write(text)

        for file in test:

            name, ext = os.path.splitext(file)

            if ext != '.jpg':
                print('[' + file + ']: 不正なファイルが含まれています。')

            with open('test.txt', 'a') as f:
                text = '{0}/{1}/{2}\t{3}\n'.format(dir_path, label_name, file, labels[label_name])
                f.write(text)

def main():

    createFilelist()

if __name__ == '__main__':

    main()

実行結果

{プロジェクト絶対パス}/gyoza/2827573.jpg	0
{プロジェクト絶対パス}/gyoza/2628927.jpg	0
1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?