SIGNATEコンペ(SUBARU 画像認識チャレンジ) データ変換

SIGNATEコンペ用にデータ変換用のプログラムを作成したので、メモっておく。 与えられるデータは、ステレオカメラの動画、視差画像、速度、ハンドル角度、車間距離などがある。自分は視差画像から車間距離と相対速度をよそくするNNをつくるので、それに関する情報のみを抜き出し、hdf5形式で保存する。データは数十秒のデータが複数シーン(学習データ:739、評価データ:241)あるので、それぞれのシーンに区切って保存する。データは../data/に解凍しておく。

コピペして使用してもらっても問題ないありませんが、動作は保証しませんのであしからず。

学習データ

視差画像の読み込み

globは順番がごちゃごちゃになるので、ソートしておく。PILで画像を読み込むと次元がHWCだが、PyTorchはCHWなので順番を入れ替えておく。TFで使用するときには、この入れ替えはなくす。

from PIL import Image
from io import BytesIO
import tqdm
import glob
import numpy as np

total_count = 0
img_list = []

for dire in tqdm.tqdm(sorted(glob.glob("../data/train_videos/*"))):
    for file in sorted(glob.glob(dire + "/" + "disparity_PNG" + "/*")):
        with open(file, 'rb') as f:
            binary = f.read()
        img_list.append(np.asarray(Image.open(BytesIO(binary))).transpose(2, 0, 1))

車間距離と相対速度

import json

OwnSpeed = []
Distance_ref = []
TgtSpeed_ref = []
relative_speed = []

for file in tqdm.tqdm(list(glob.glob("../data/train_annotations/*"))):
    with open(file, "r", encoding='utf-8') as f:
        json_dict = json.load(f)
        for data in json_dict["sequence"]:
            OwnSpeed.append(data["OwnSpeed"])
            Distance_ref.append(data["Distance_ref"])
            TgtSpeed_ref.append(data["TgtSpeed_ref"])
            relative_speed.append(data["TgtSpeed_ref"] - data["OwnSpeed"])

hdf5ファイルへの保存

import h5py
import os

total_count = 0
size = img_list[0].shape

with h5py.File('../data/data.hdf5', "w") as f:
    group_img = f.create_group('/img')    
    group_ann = f.create_group('/ann')    
    for dire in tqdm.tqdm(sorted(glob.glob("../data/train_videos/*"))):
        data_len = len(list(glob.glob(dire + "/" + "disparity_PNG" + "/*")))
        f.create_dataset(
            name='/img/' + dire.split("/")[-1], shape=(data_len, size[0], size[1], size[2]), 
            data=np.array(img_list[total_count:total_count+data_len]))
        f.create_dataset(
            name='/ann/OwnSpeed/' + dire.split("/")[-1], shape=(data_len), 
            data=np.array(OwnSpeed[total_count:total_count+data_len]))
        f.create_dataset(
            name='/ann/Distance_ref/' + dire.split("/")[-1], shape=(data_len), 
            data=np.array(Distance_ref[total_count:total_count+data_len]))
        f.create_dataset(
            name='/ann/TgtSpeed_ref/' + dire.split("/")[-1], shape=(data_len), 
            data=np.array(TgtSpeed_ref[total_count:total_count+data_len]))
        f.create_dataset(
            name='/ann/relative_speed/' + dire.split("/")[-1], shape=(data_len), 
            data=np.array(relative_speed[total_count:total_count+data_len]))
        total_count += data_len

評価データ

train_videosをtest_videosにするだけ。