14 KiB
14 KiB
from torch_snippets import *
from sklearn.model_selection import train_test_split
df = pd.read_csv('df.csv')
trn_df, val_df = train_test_split(df, random_state=10)
df_mini = df[df.ImageID.isin(df.ImageID.unique()[:500].tolist())]
trn_df_mini, val_df_mini = train_test_split(df_mini, random_state=10)
df_micro = df[df.ImageID.isin(df.ImageID.unique()[:50].tolist())]
trn_df_micro, val_df_micro = train_test_split(df_micro, random_state=10)
len(df)
df.head()
ImageID | Source | LabelName | Confidence | XMin | XMax | YMin | YMax | IsOccluded | IsTruncated | ... | IsDepiction | IsInside | XClick1X | XClick2X | XClick3X | XClick4X | XClick1Y | XClick2Y | XClick3Y | XClick4Y | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0000599864fd15b3 | xclick | Bus | 1 | 0.343750 | 0.908750 | 0.156162 | 0.650047 | 1 | 0 | ... | 0 | 0 | 0.421875 | 0.343750 | 0.795000 | 0.908750 | 0.156162 | 0.512700 | 0.650047 | 0.457197 |
1 | 00006bdb1eb5cd74 | xclick | Truck | 1 | 0.276667 | 0.697500 | 0.141604 | 0.437343 | 1 | 0 | ... | 0 | 0 | 0.299167 | 0.276667 | 0.697500 | 0.659167 | 0.141604 | 0.241855 | 0.352130 | 0.437343 |
2 | 00006bdb1eb5cd74 | xclick | Truck | 1 | 0.702500 | 0.999167 | 0.204261 | 0.409774 | 1 | 1 | ... | 0 | 0 | 0.849167 | 0.702500 | 0.906667 | 0.999167 | 0.204261 | 0.398496 | 0.409774 | 0.295739 |
3 | 00010bf498b64bab | xclick | Bus | 1 | 0.156250 | 0.371250 | 0.269188 | 0.705228 | 0 | 0 | ... | 0 | 0 | 0.274375 | 0.371250 | 0.311875 | 0.156250 | 0.269188 | 0.493882 | 0.705228 | 0.521691 |
4 | 00013f14dd4e168f | xclick | Bus | 1 | 0.287500 | 0.999375 | 0.194184 | 0.999062 | 0 | 1 | ... | 0 | 0 | 0.920000 | 0.999375 | 0.648750 | 0.287500 | 0.194184 | 0.303940 | 0.999062 | 0.523452 |
5 rows × 21 columns
labels = ['Bus', 'Truck']
def do(trn_df, val_df, folder):
splits = [('train', trn_df), ('val', val_df)]
for split, df in splits:
os.makedirs(f'{folder}/labels', exist_ok=True)
ImageIDs = df.ImageID.unique()
for ImageID in ImageIDs:
fname = ImageID
_df = df[df['ImageID'] == ImageID][['LabelName','XMin','YMin','XMax','YMax']]
_df['Xc'] = (_df['XMax'] + _df['XMin'])/2
_df['Yc'] = (_df['YMax'] + _df['YMin'])/2
_df['w'] = _df['XMax'] - _df['XMin']
_df['h'] = _df['YMax'] - _df['YMin']
_df['LabelName'] = _df['LabelName'].map(lambda x: labels.index(x))
_df.drop(['XMin','YMin','XMax','YMax'], inplace=True, axis=1)
with open(f'{folder}/labels/{fname}.txt', 'w') as f:
for a,b,c,d,e in _df.values:
f.write(f'{int(a)} {b} {c} {d} {e}')
f.write('\n')
with open(f'{folder}/{split}.txt', 'w') as f:
for ImageID in ImageIDs:
f.write(f'{ImageID}.jpg\n')
do(trn_df_micro, val_df_micro, 'yolo_labels/micro/')
do(trn_df_mini, val_df_mini, 'yolo_labels/mini/')
do(trn_df, val_df, 'yolo_labels/all/')
[0;31m---------------------------------------------------------------------------[0m [0;31mFileNotFoundError[0m Traceback (most recent call last) [0;32m<ipython-input-26-ca0e2222a2b7>[0m in [0;36m<module>[0;34m[0m [1;32m 25[0m [0mdo[0m[0;34m([0m[0mtrn_df_micro[0m[0;34m,[0m [0mval_df_micro[0m[0;34m,[0m [0;34m'yolo_labels/micro/'[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 26[0m [0mdo[0m[0;34m([0m[0mtrn_df_mini[0m[0;34m,[0m [0mval_df_mini[0m[0;34m,[0m [0;34m'yolo_labels/mini/'[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;32m---> 27[0;31m [0mdo[0m[0;34m([0m[0mtrn_df[0m[0;34m,[0m [0mval_df[0m[0;34m,[0m [0;34m'yolo_labels/all/'[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m [0;32m<ipython-input-26-ca0e2222a2b7>[0m in [0;36mdo[0;34m(trn_df, val_df, folder)[0m [1;32m 15[0m [0m_df[0m[0;34m[[0m[0;34m'LabelName'[0m[0;34m][0m [0;34m=[0m [0m_df[0m[0;34m[[0m[0;34m'LabelName'[0m[0;34m][0m[0;34m.[0m[0mmap[0m[0;34m([0m[0;32mlambda[0m [0mx[0m[0;34m:[0m [0mlabels[0m[0;34m.[0m[0mindex[0m[0;34m([0m[0mx[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 16[0m [0m_df[0m[0;34m.[0m[0mdrop[0m[0;34m([0m[0;34m[[0m[0;34m'XMin'[0m[0;34m,[0m[0;34m'YMin'[0m[0;34m,[0m[0;34m'XMax'[0m[0;34m,[0m[0;34m'YMax'[0m[0;34m][0m[0;34m,[0m [0minplace[0m[0;34m=[0m[0;32mTrue[0m[0;34m,[0m [0maxis[0m[0;34m=[0m[0;36m1[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;32m---> 17[0;31m [0;32mwith[0m [0mopen[0m[0;34m([0m[0;34mf'{folder}/{fname}.txt'[0m[0;34m,[0m [0;34m'w'[0m[0;34m)[0m [0;32mas[0m [0mf[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 18[0m [0;32mfor[0m [0ma[0m[0;34m,[0m[0mb[0m[0;34m,[0m[0mc[0m[0;34m,[0m[0md[0m[0;34m,[0m[0me[0m [0;32min[0m [0m_df[0m[0;34m.[0m[0mvalues[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [1;32m 19[0m [0mf[0m[0;34m.[0m[0mwrite[0m[0;34m([0m[0;34mf'{int(a)} {b} {c} {d} {e}'[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;31mFileNotFoundError[0m: [Errno 2] No such file or directory: 'yolo_labels/all//b708f693265636d6.txt'