Skip to content

Commit

Permalink
[local] 데이터 증식 test 준비과정
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon11112222 committed Nov 23, 2023
1 parent 064b49b commit dd06e38
Showing 1 changed file with 361 additions and 0 deletions.
361 changes: 361 additions & 0 deletions 231123imageAugmentation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,361 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 버스만 있는 이미지"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"valid_total_df = pd.read_csv('valid_total_txt_label.csv')\n",
"\n",
"condition1 = valid_total_df['car_cnt'].isnull()\n",
"condition2 = valid_total_df['truck_cnt'].isnull()\n",
"\n",
"busonlyList = list(valid_total_df[(condition1) & (condition2)]['txt_in_valid_folder'].values)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Suwon_CH05_20200720_1830_MON_9m_NH_highway_OW5_sunny_FHD_066.txt',\n",
" 'Suwon_CH05_20200720_1830_MON_9m_NH_highway_OW5_sunny_FHD_085.txt',\n",
" 'Suwon_CH06_20200722_1900_WED_9m_NH_highway_OW5_sunny_FHD_033.txt',\n",
" 'Suwon_CH10_20201213_0752_SUN_9m_NH_highway_OW5_snow_FHD_041.txt',\n",
" 'Suwon_CH10_20201213_0752_SUN_9m_NH_highway_OW5_snow_FHD_082.txt']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"\n",
"valid_filename = []\n",
"for i in busonlyList:\n",
" basename = os.path.basename(i)\n",
" valid_filename.append(basename)\n",
"valid_filename"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from glob import glob\n",
"import cv2\n",
"import numpy as np\n",
"\n",
"\n",
"def glob_files(path, file_type=\"*\"):\n",
" search_string = os.path.join(path, file_type)\n",
" files = glob(search_string)\n",
"\n",
" # print('searching ', path)\n",
" paths = []\n",
" for f in files:\n",
" if os.path.isdir(f):\n",
" sub_paths = glob_files(f + '/')\n",
" paths += sub_paths\n",
" else:\n",
" paths.append(f)\n",
"\n",
" # We sort the images in alphabetical order to match them\n",
" # to the annotation files\n",
" paths.sort()\n",
"\n",
" return paths\n",
"\n",
"\n",
"# IMAGE_SIZE = 600\n",
"\n",
"def load_images(path):\n",
" files = glob_files(path, \"*.png\")\n",
"\n",
" # print(files)\n",
" X_data = []\n",
" for file in files:\n",
" image = cv2.imread(file)\n",
" # print(image.shape)\n",
" # x = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)\n",
"\n",
" X_data.append(image)\n",
" \n",
" return np.array(X_data)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(5, 1920, 1080, 3)\n"
]
}
],
"source": [
"basePath_image = 'highway/valid/image/'\n",
"\n",
"X_test = load_images(basePath_image)\n",
"print(X_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"WIDTH = 1080\n",
"HEIGHT = 1920\n",
"\n",
"def load_labels(path):\n",
" files = glob_files(path, \"*.txt\")\n",
"\n",
" Y_data = []\n",
" for file in files:\n",
" with open(file) as f:\n",
" lines = f.readlines()\n",
"\n",
" boxes = []\n",
" for line in lines:\n",
" tokens = line.split()\n",
"\n",
" class_id = int(tokens[0])\n",
" xc = float(tokens[1]) * WIDTH\n",
" yc = float(tokens[2]) * HEIGHT\n",
" width = float(tokens[3]) * WIDTH\n",
" height = float(tokens[4]) * HEIGHT\n",
"\n",
" boxes.append(np.array([class_id, xc, yc, width, height]))\n",
" # print(class_id, xc, yc, width, height)\n",
"\n",
" Y_data.append(np.array(boxes))\n",
" # print(lines)\n",
" return Y_data ## 이미지마다 탐지 대상 개수가 달라서(행 개수가 달라서) 정해진 shape의 넘파이 어레이로 변환할 수 없다! 변환하려면 패딩 등이 필요.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## car, truck, bus 가 모두 포함된 이미지"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>txt_in_valid_folder</th>\n",
" <th>car_cnt</th>\n",
" <th>truck_cnt</th>\n",
" <th>bus_cnt</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>highway/valid/label/Suwon_CH01_20200720_1830_M...</td>\n",
" <td>5.0</td>\n",
" <td>9.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>highway/valid/label/Suwon_CH01_20200720_1830_M...</td>\n",
" <td>8.0</td>\n",
" <td>7.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>highway/valid/label/Suwon_CH01_20200720_1830_M...</td>\n",
" <td>9.0</td>\n",
" <td>4.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>highway/valid/label/Suwon_CH01_20200720_1830_M...</td>\n",
" <td>9.0</td>\n",
" <td>4.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>highway/valid/label/Suwon_CH01_20200720_1830_M...</td>\n",
" <td>7.0</td>\n",
" <td>4.0</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9290</th>\n",
" <td>highway/valid/label/Suwon_CH10_20201213_0752_S...</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9311</th>\n",
" <td>highway/valid/label/Suwon_CH10_20201213_0752_S...</td>\n",
" <td>2.0</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9312</th>\n",
" <td>highway/valid/label/Suwon_CH10_20201213_0752_S...</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9315</th>\n",
" <td>highway/valid/label/Suwon_CH10_20201213_0752_S...</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9324</th>\n",
" <td>highway/valid/label/Suwon_CH10_20201213_0752_S...</td>\n",
" <td>4.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1175 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" txt_in_valid_folder car_cnt truck_cnt \\\n",
"2 highway/valid/label/Suwon_CH01_20200720_1830_M... 5.0 9.0 \n",
"3 highway/valid/label/Suwon_CH01_20200720_1830_M... 8.0 7.0 \n",
"5 highway/valid/label/Suwon_CH01_20200720_1830_M... 9.0 4.0 \n",
"6 highway/valid/label/Suwon_CH01_20200720_1830_M... 9.0 4.0 \n",
"7 highway/valid/label/Suwon_CH01_20200720_1830_M... 7.0 4.0 \n",
"... ... ... ... \n",
"9290 highway/valid/label/Suwon_CH10_20201213_0752_S... 2.0 1.0 \n",
"9311 highway/valid/label/Suwon_CH10_20201213_0752_S... 2.0 2.0 \n",
"9312 highway/valid/label/Suwon_CH10_20201213_0752_S... 1.0 1.0 \n",
"9315 highway/valid/label/Suwon_CH10_20201213_0752_S... 1.0 1.0 \n",
"9324 highway/valid/label/Suwon_CH10_20201213_0752_S... 4.0 1.0 \n",
"\n",
" bus_cnt \n",
"2 1.0 \n",
"3 1.0 \n",
"5 1.0 \n",
"6 1.0 \n",
"7 3.0 \n",
"... ... \n",
"9290 1.0 \n",
"9311 1.0 \n",
"9312 1.0 \n",
"9315 1.0 \n",
"9324 1.0 \n",
"\n",
"[1175 rows x 4 columns]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"condition3 = valid_total_df['car_cnt'].notnull()\n",
"condition4 = valid_total_df['truck_cnt'].notnull()\n",
"condition5 = valid_total_df['bus_cnt'].notnull()\n",
"\n",
"valid_notnull_df = valid_total_df[(condition3) & (condition4) & (condition5)]\n",
"valid_notnull_df[valid_notnull_df['car_cnt'] <= 10]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "yolov8",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit dd06e38

Please sign in to comment.