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
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"cells": [
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": null,
|
20 |
+
"metadata": {
|
21 |
+
"id": "9QLlZv6DlPC1"
|
22 |
+
},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"from google.colab import drive\n",
|
26 |
+
"drive.mount('/content/drive')\n",
|
27 |
+
"%cd /content/drive/MyDrive/sta_663/soybean/"
|
28 |
+
]
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"cell_type": "code",
|
32 |
+
"source": [
|
33 |
+
"import pandas as pd\n",
|
34 |
+
"\n",
|
35 |
+
"# Function to read ids from a file and return them as a list with leading zeros\n",
|
36 |
+
"def read_ids(file_path):\n",
|
37 |
+
" with open(file_path, 'r') as file:\n",
|
38 |
+
" # Read the IDs, ensuring they are 6 digits long with leading zeros\n",
|
39 |
+
" return [line.zfill(6) for line in file.read().splitlines()]\n",
|
40 |
+
"\n",
|
41 |
+
"# Function to read ids from a file and assign a set type\n",
|
42 |
+
"def assign_set_type(file_path, set_type):\n",
|
43 |
+
" # Read the file content\n",
|
44 |
+
" with open(file_path, 'r') as file:\n",
|
45 |
+
" ids = file.read().splitlines()\n",
|
46 |
+
" # Update the 'sets' column based on the ids in the file\n",
|
47 |
+
" df.loc[df['unique_id'].isin(ids), 'sets'] = set_type\n",
|
48 |
+
"\n"
|
49 |
+
],
|
50 |
+
"metadata": {
|
51 |
+
"id": "prkF3wVLld_k"
|
52 |
+
},
|
53 |
+
"execution_count": null,
|
54 |
+
"outputs": []
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"cell_type": "code",
|
58 |
+
"source": [
|
59 |
+
"# Read unique_ids from all.txt\n",
|
60 |
+
"all_file_path = '/content/drive/MyDrive/sta_663/soybean/ImageSets/Segmentation/all.txt'\n",
|
61 |
+
"unique_ids = read_ids(all_file_path)\n",
|
62 |
+
"\n",
|
63 |
+
"# Initialize the DataFrame with unique_ids and default 'train' set\n",
|
64 |
+
"df = pd.DataFrame(unique_ids, columns=['unique_id'])\n",
|
65 |
+
"df['sets'] = 'train'\n",
|
66 |
+
"\n",
|
67 |
+
"# Assign 'test' to the sets column for IDs from test.txt\n",
|
68 |
+
"test_file_path = '/content/drive/MyDrive/sta_663/soybean/ImageSets/Segmentation/test.txt'\n",
|
69 |
+
"assign_set_type(test_file_path, 'test')\n",
|
70 |
+
"\n",
|
71 |
+
"# Assign 'valid' to the sets column for IDs from val.txt\n",
|
72 |
+
"val_file_path = '/content/drive/MyDrive/sta_663/soybean/ImageSets/Segmentation/val.txt'\n",
|
73 |
+
"assign_set_type(val_file_path, 'valid')\n",
|
74 |
+
"\n"
|
75 |
+
],
|
76 |
+
"metadata": {
|
77 |
+
"id": "nsFdyvBzlgB_"
|
78 |
+
},
|
79 |
+
"execution_count": null,
|
80 |
+
"outputs": []
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"cell_type": "code",
|
84 |
+
"source": [
|
85 |
+
"file_path = '/content/drive/MyDrive/sta_663/soybean/dataset.csv'\n",
|
86 |
+
"df.to_csv(file_path, index=False)"
|
87 |
+
],
|
88 |
+
"metadata": {
|
89 |
+
"id": "KjRGHJivliym"
|
90 |
+
},
|
91 |
+
"execution_count": null,
|
92 |
+
"outputs": []
|
93 |
+
},
|
94 |
+
{
|
95 |
+
"cell_type": "markdown",
|
96 |
+
"source": [
|
97 |
+
"Download the dataset.csv file and put into the same directory as the downloaded zip file"
|
98 |
+
],
|
99 |
+
"metadata": {
|
100 |
+
"id": "qyyjofnUmXsh"
|
101 |
+
}
|
102 |
+
},
|
103 |
+
{
|
104 |
+
"cell_type": "code",
|
105 |
+
"source": [
|
106 |
+
"import os\n",
|
107 |
+
"import pandas as pd\n",
|
108 |
+
"import shutil"
|
109 |
+
],
|
110 |
+
"metadata": {
|
111 |
+
"id": "hm7ZaB5ImAeA"
|
112 |
+
},
|
113 |
+
"execution_count": null,
|
114 |
+
"outputs": []
|
115 |
+
},
|
116 |
+
{
|
117 |
+
"cell_type": "code",
|
118 |
+
"source": [
|
119 |
+
"# Replace with the path to your CSV file\n",
|
120 |
+
"csv_file_path = 'D:\\STA 663\\project_1\\dataset.csv'\n",
|
121 |
+
"# Replace with the directory where your images are currently stored\n",
|
122 |
+
"images_directory = 'D:\\STA 663\\project_1\\soybean\\JPEGImages'\n",
|
123 |
+
"# Replace with the directory where you want to create test/train/validate directories\n",
|
124 |
+
"output_base_directory = 'D:\\STA 663\\project_1'\n",
|
125 |
+
"\n",
|
126 |
+
"# Read the dataset\n",
|
127 |
+
"df = pd.read_csv(csv_file_path)\n",
|
128 |
+
"df['unique_id'] = df['unique_id'].astype(str).str.zfill(6)"
|
129 |
+
],
|
130 |
+
"metadata": {
|
131 |
+
"id": "iTKsnTUdmI3N"
|
132 |
+
},
|
133 |
+
"execution_count": null,
|
134 |
+
"outputs": []
|
135 |
+
},
|
136 |
+
{
|
137 |
+
"cell_type": "code",
|
138 |
+
"source": [
|
139 |
+
"# Create directories for the sets if they don't exist\n",
|
140 |
+
"for set_type in ['test', 'train', 'valid']:\n",
|
141 |
+
" set_directory = os.path.join(output_base_directory, set_type)\n",
|
142 |
+
" if not os.path.exists(set_directory):\n",
|
143 |
+
" os.makedirs(set_directory)\n",
|
144 |
+
"\n",
|
145 |
+
"# Function to move and rename files\n",
|
146 |
+
"def move_and_rename_files(row):\n",
|
147 |
+
" file_name = f\"{row['unique_id']}.jpg\" # Assuming the images are .jpg\n",
|
148 |
+
" original_path = os.path.join(images_directory, file_name)\n",
|
149 |
+
" if os.path.isfile(original_path):\n",
|
150 |
+
" set_type = row['sets']\n",
|
151 |
+
" new_name = f\"{row['unique_id']}_original.jpg\"\n",
|
152 |
+
" new_path = os.path.join(output_base_directory, set_type, new_name)\n",
|
153 |
+
" # Move and rename the file\n",
|
154 |
+
" shutil.copy(original_path, new_path) # Use shutil.copy if you want to keep the originals\n",
|
155 |
+
"\n",
|
156 |
+
"# Apply the function to each row in the dataframe\n",
|
157 |
+
"df.apply(move_and_rename_files, axis=1)"
|
158 |
+
],
|
159 |
+
"metadata": {
|
160 |
+
"id": "2dvMgZcOmLt2"
|
161 |
+
},
|
162 |
+
"execution_count": null,
|
163 |
+
"outputs": []
|
164 |
+
},
|
165 |
+
{
|
166 |
+
"cell_type": "code",
|
167 |
+
"source": [
|
168 |
+
"### Do the same thing for segmentation class"
|
169 |
+
],
|
170 |
+
"metadata": {
|
171 |
+
"id": "aZb9yoXumrrp"
|
172 |
+
},
|
173 |
+
"execution_count": null,
|
174 |
+
"outputs": []
|
175 |
+
},
|
176 |
+
{
|
177 |
+
"cell_type": "code",
|
178 |
+
"source": [
|
179 |
+
"# Replace with the path to your CSV file\n",
|
180 |
+
"csv_file_path = 'D:\\STA 663\\project_1\\dataset.csv'\n",
|
181 |
+
"# Replace with the directory where your images are currently stored\n",
|
182 |
+
"images_directory = 'D:\\STA 663\\project_1\\soybean\\SegmentationClass'\n",
|
183 |
+
"# Replace with the directory where you want to create test/train/validate directories\n",
|
184 |
+
"output_base_directory = 'D:\\STA 663\\project_1'"
|
185 |
+
],
|
186 |
+
"metadata": {
|
187 |
+
"id": "Ud79pkDMmyA_"
|
188 |
+
},
|
189 |
+
"execution_count": null,
|
190 |
+
"outputs": []
|
191 |
+
},
|
192 |
+
{
|
193 |
+
"cell_type": "code",
|
194 |
+
"source": [
|
195 |
+
"# Function to move and rename files\n",
|
196 |
+
"def move_and_rename_files(row):\n",
|
197 |
+
" file_name = f\"{row['unique_id']}.png\" # Assuming the images are .jpg\n",
|
198 |
+
" original_path = os.path.join(images_directory, file_name)\n",
|
199 |
+
" if os.path.isfile(original_path):\n",
|
200 |
+
" set_type = row['sets']\n",
|
201 |
+
" new_name = f\"{row['unique_id']}_segmentation.jpg\"\n",
|
202 |
+
" new_path = os.path.join(output_base_directory, set_type, new_name)\n",
|
203 |
+
" # Move and rename the file\n",
|
204 |
+
" shutil.copy(original_path, new_path) # Use shutil.copy if you want to keep the originals\n",
|
205 |
+
"\n",
|
206 |
+
"# Apply the function to each row in the dataframe\n",
|
207 |
+
"df.apply(move_and_rename_files, axis=1)"
|
208 |
+
],
|
209 |
+
"metadata": {
|
210 |
+
"id": "UoJLs5-Dm2u5"
|
211 |
+
},
|
212 |
+
"execution_count": null,
|
213 |
+
"outputs": []
|
214 |
+
}
|
215 |
+
]
|
216 |
+
}
|