LoginSignup
1
0

More than 3 years have passed since last update.

Chainerメモ その2

Last updated at Posted at 2018-05-29

4. 学習用データ作成(画像パスとクラスがセットされたテキストファイル スクリプト修正)

vim chainer_imagenet_tools/make_train_data.py

import sys
import re
import os
import shutil

#labels
pwd = os.getcwd()
labels = os.listdir(pwd+os.sep+sys.argv[1])

#make directries
os.mkdir("images")

imageDir = pwd+os.sep+"images"
train = open('train.txt','w')
test = open('test.txt','w')
labelsTxt = open('labels.txt','w')

classNo=0
cnt = 0
#label = labels[classNo]
for label in labels:
        print(label)
        workdir = pwd+os.sep+sys.argv[1]+os.sep+label
        images = os.listdir(workdir)
        labelsTxt.write(label+"\n")
        startCnt=cnt
        length = len(images)
        for image in images:
                print(image)
                imagepath = imageDir+os.sep+"image%07d" %cnt +".jpg"
                shutil.copy(workdir+os.sep+image, imagepath)
                if cnt-startCnt < length*0.75:
                        train.write(imagepath+" %d\n" % classNo)
                else:
                        test.write(imagepath+" %d\n" % classNo)
                cnt += 1

        classNo += 1

train.close()
test.close()
labelsTxt.close()
1
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
1
0