즐거운프로그래밍

[딥러닝] 사용자 데이터로 CNN 학습하기- 2. 수집한 데이터 불러오기

수수께끼 고양이 2023. 11. 6. 10:22
728x90
반응형

 

2. 수집한 데이터 불러오기

수집한 데이터를 읽어와서 인공 신경망에서 사용할 수 있는 텐서로 변경합니다. 

텐서는 3차원 이상의 행렬을 의미하며 프로그래밍 언어 관점에서는 3차원 이상의 배열이 됩니다.

 

04_cnn_training_1.py

from tensorflow.keras.preprocessing import image as keras_image
import os
import numpy as np
from tqdm import tqdm
from PIL import ImageFile
import pandas as pd

dirname="data.1695035538.287210"

def image_to_tensor(img_path):
    img=keras_image.load_img(
        os.path.join(dirname, img_path),
        target_size=(120,160))
    x=keras_image.img_to_array(img)
    return np.expand_dims(x,axis=0) # expend_dims : 차원 하나 늘리는 것 ex.(120,160,3)->(1,120,160,3)

def data_to_tensor(img_paths):
    list_of_tensors=[
        image_to_tensor(img_path) for img_path in tqdm(img_paths)]
    return np.vstack(list_of_tensors) # np.vstack : numpy 데이터를 한덩어리로 합치는 것(vertical stack)

ImageFile.LOAD_TRUNCATED_IMAGES=True
# Load the data
data=pd.read_csv(os.path.join(dirname,"0_road_labels.csv"))
data=data.sample(frac=1) # 데이터를 섞는 작업

files=data['file']
targets=data['label'].values

tensors=data_to_tensor(files)

print(data.tail())
print(tensors.shape)
print(targets.shape)

 

 

 

 

 

 

728x90
반응형