일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 리눅스 비밀번호
- 전체내용
- restnet
- Rust설치
- 듀얼부트
- #enviroment variable
- Ubuntu 설치
- ubuntu 원격
- stepsize
- 포트번호 변경
- 학습성공
- #cuda
- Lagrange Multiplier
- Git branch 보이기
- Xpad설치
- #cudnn
- #python gpu
- 치환 기능
- log파일
- Ubuntu동글
- DenseNet
- weight histogram
- #ubuntu 14.04(LTS)
- tensorboard
- Hidden layer output
- VGGNet
- canon mf416dw
- twolay
- 3D convolution
- 화면확대고정
- Today
- Total
save the world
이미지로 데이터파일 만들기 본문
그림인 jpg 파일이 jpegimage 폴더에 11321개 있고
그림에 대한 정보 xml 파일이 annotation 폴더에 11321개 있다.
각각의 jpg와 xml 파일의 이름은 같고 ( ex) 2007_008561.jpg, 2007_008561.xml )xml 파일에 사진의 정보가 들어있다.
# -*- coding: utf-8 -*- #한글사용
"""
Basic data set generation
Sungjoon Choi (sungjoon.choi@cpslab.snu.ac.kr)
"""
# Import packs
import numpy as np
import os
from scipy.misc import imread, imresize
import xml.etree.ElementTree as ET
print ("Package loaded")
# Print Current Folder
cwd = os.getcwd()
print ("Current folder is %s" % cwd)
# Training set folder
imgpath = "VOC2010/JPEGImages/"
xmlpath = "VOC2010/Annotations/"
# The reshape size
imgsize = [112, 112]
# Save name
data_name = "makingnpz1"
# First, check the total number of training data
"""
The reason for doing this a priori is that
it is better to pre-allocate memory rather than
dynamically adjust the size of the matrix.
"""
valid_exts = [".jpg", ".xml"]
nclass = 20 # class 개수 PASCAL clasification 20가지 class 있어
imgfullpath = cwd + "/" + imgpath # 사진 파일이 있는 경로 (파일이름 제외)
xmlfullpath = cwd + "/" + xmlpath # xml 파일이 있는 경로 (파일이름 제외)
imgcnt = 11321 # 원래 이미지 갯수 카운트하는 부분이 있어야 할 것 같다.
# Then, let's save them!
totalimg = np.ndarray((imgcnt, imgsize[0] * imgsize[1] * 3))
totallabel = np.zeros((imgcnt, nclass)) # totallabel는 11321행 20열 (이미지 11321개, class 20개)
flist = sorted(os.listdir(imgfullpath)) # 파일 '이름 순으로' 리스트 만들기
xlist = sorted(os.listdir(xmlfullpath)) # 파일 '이름 순으로' 리스트 만들기
taglist = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow',
'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedpland', 'sheep', 'sofa', 'train', 'tvmonitor']
# class2num 이라는 Dictionary 만들기
class2num={}
for classname, num in zip(taglist, range(nclass)):
class2num[classname] = num
# class2num = {'aeroplane':0, 'bicycle':1, 'bird':2, 'boat':3, 'bottle':4,'bus':5,'car':6,'cat':7,'chair':8,'cow':9,'diningtable':10,'dog':11,'horse':12,'motorbike':13,'person':14,'pottedpland':15,'sheep':16,'sofa':17,'train':18,'tvmonitor':19}
imgcnt = 0 # 이미지 개수 초기화.
for f, x in zip(flist, xlist): # 파일이름들을 f에 하나 씩 대입
print(imgcnt)
if os.path.splitext(f)[1].lower() not in valid_exts: # 파일 확장자 명이 유효 확장자 명 리스트에 없으면 continue
continue
if os.path.splitext(x)[1].lower() not in valid_exts:
continue
fullpath = os.path.join(imgfullpath, f) # 파일이 있는 경로 + 파일이름
currimg = imread(fullpath) # 픽셀 값 출력하여 currimg 에 저장
grayimg = currimg
# Reshape
graysmall = imresize(grayimg, [imgsize[0], imgsize[1]]) / 255. # 이미지 크키 변경 후 픽셀 값들 normalization하기
grayvec = np.reshape(graysmall, (1, -1)) # 이미지 픽셀 값으로 vectorization 하기
# xml에서 tag 추출
img_info = list()
tree = ET.parse(xmlfullpath + xlist[imgcnt]) # bringing file
root = tree.getroot()
for name in root.iter("name"): # name 이라는 태그명의 주소를 name 에 대입
# print(name.tag, name.text) # name이라는 태그를 가진 주소의 태그값에서 tag이름과 내용을 print해줌
if name.text not in taglist: # name태그의 값이 taglist 에 없는것들이면 [ex)head foot hand]색
continue # 다음태그 검색
img_info.append(name.text) # name태그의 내용 리스트에 추가(append)
for tagname in img_info: # tagname에 img_info 리스트를 대입
totallabel[imgcnt, class2num[tagname]] = 1 # a번째행(a번째이미지) class2num[tagname]번째 열에 1 대입
# Save
totalimg[imgcnt, :] = grayvec # imgcnt행(사진번호0~23)에 grayvec값 대입. 즉, 모든사진들의 값을 갖고 있음
imgcnt += 1
# Divide total d기ata into training and test set
# randidx = np.random.randint(imgcnt, size=imgcnt) # 최대 imgcnt까지의 숫자를 랜덤하게 한 행에 size만큼의 열로 랜덤하게 생성(중복있음) / 중복없어야할듯 randint대신 shuffle 사용
randidx = np.arange(imgcnt) # imgcnt 만큼 숫자생성
np.random.shuffle(randidx) # 순서 섞어주기
trainidx = randidx[0:int(imgcnt * 0.8)] # train할 이미지(번호)를 랜덤하게 앞에서부터 80% 뽑는다
testidx = randidx[int(imgcnt * 0.8):imgcnt] # test 할 이미지(번호)를 나머지 20%로 한다. randidx[int(3):6] 은 3번째원소부터 6번전의(5번째) 원소까지 출력 시작은 0부터
trainimg = totalimg[trainidx, :] # train 이미지는 trainidx(예)->[0,5,4,6,3]. 이미지들(vectorization된 픽셀값들)
trainlabel = totallabel[trainidx, :] # trainidx 번째 이미지의 label 값 2행의(두번째사진의) 개(0 1)
testimg = totalimg[testidx, :] # test 이미지는 testidx 이미지들(vectorization된 픽셀값들)
testlabel = totallabel[testidx, :] # testidx 번째 이미지의 label 값 5행의(두번째사진의) 고양이(1 0)
# Plot them all
# This code can be used for mini-batch learning!
#ntrain = trainimg.shape[0] # traninimg.shape[0] = 9056 이므로 ntrain 의미는 train 이미지 개수
# batch_size = 10
# randidx = np.random.randint(ntrain, size=batch_size)
# for i in randidx:
# currlabel_onehot = trainlabel[i, :] # currlabel_onthot에 각 이미지의 label 대입
# currlabel = np.argmax(currlabel_onehot) # 가장 큰 값의 index값 출력
# currimg = np.reshape(trainimg[i, :], (imgsize[0], imgsize[1], 3))
# plt.imshow(currimg)
# Save them!
savepath = cwd + "/sjchoi/data/" + data_name + ".npz"
np.savez(savepath, trainimg=trainimg, trainlabel=trainlabel, testimg=testimg, testlabel=testlabel)
print ("Saved to %s" % savepath)
'PYTHON' 카테고리의 다른 글
python training (0) | 2016.06.08 |
---|---|
Pycharm Library 다운받기 (0) | 2016.06.03 |
Python gpu version 설치 (0) | 2016.06.03 |