|
5 | 5 | "id": "7cfb11f5", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | | - "# 0.2. Normalize and Crop Training Data\n", |
| 8 | + "# 0.3. Normalize and Crop Training Data\n", |
9 | 9 | "\n", |
10 | 10 | "Due to the large size of the training data, we will normalize and crop the images to a smaller size. This will help in reducing the computational load and make it easier to work with the data.\n", |
11 | 11 | "\n", |
|
17 | 17 | "id": "b21b2780", |
18 | 18 | "metadata": {}, |
19 | 19 | "source": [ |
20 | | - "## 0.2.1. Load Python Libraries" |
| 20 | + "## 0.3.1. Load Python Libraries" |
21 | 21 | ] |
22 | 22 | }, |
23 | 23 | { |
|
39 | 39 | "id": "e44e1ad1", |
40 | 40 | "metadata": {}, |
41 | 41 | "source": [ |
42 | | - "## 0.2.2. Load Custom Functions" |
| 42 | + "## 0.3.2. Load Custom Functions" |
43 | 43 | ] |
44 | 44 | }, |
45 | 45 | { |
|
119 | 119 | "id": "105eb73d", |
120 | 120 | "metadata": {}, |
121 | 121 | "source": [ |
122 | | - "## 0.2.3. Code to Normalize and Crop Training Data" |
| 122 | + "## 0.3.3. Code to Normalize and Crop Training Data" |
123 | 123 | ] |
124 | 124 | }, |
125 | 125 | { |
|
143 | 143 | "metadata": {}, |
144 | 144 | "outputs": [], |
145 | 145 | "source": [ |
146 | | - "# The path to the images and masks should be specified in the variables `img_directory`, `og_mask_directory`, and `watershed_label_directory`.\n", |
| 146 | + "# The path to the images and masks should be specified in the variables `img_directory`, and `watershed_label_directory`.\n", |
147 | 147 | "img_directory = \"directory/to/images\"\n", |
148 | 148 | "\n", |
149 | | - "og_mask_directory = \"directory/to/original/masks\"\n", |
150 | | - "\n", |
151 | 149 | "watershed_label_directory = \"directory/to/watershed/labels\"\n", |
152 | 150 | "\n", |
| 151 | + "# OPTIONAL: If you have an original mask directory, specify it here.\n", |
| 152 | + "# This is used to reduce the amount of similiar crops generated by cropping each label in a cluster individually.\n", |
| 153 | + "og_mask_directory = \"directory/to/original/masks\" # Set to None if not used\n", |
| 154 | + "\n", |
153 | 155 | "# Provide the directories to store the cropped images and labels\n", |
154 | 156 | "# They will be created if they do not exist\n", |
155 | 157 | "cropped_img_directory = \"directory/to/cropped/images\"\n", |
156 | 158 | "cropped_lbl_directory = \"directory/to/cropped/labels\"\n", |
157 | 159 | "\n", |
158 | 160 | "# Size of the crop in the x and y dimensions\n", |
| 161 | + "# Should be at least twice the patch size used for training the model\n", |
| 162 | + "# This is to ensure that an edge crop still has a sufficient size for training\n", |
| 163 | + "# For example, if the patch size is 128, a crop size of 256 is recommended.\n", |
159 | 164 | "crop_size_xy = 256\n", |
160 | 165 | "\n", |
161 | 166 | "# Minimum size of the crop in the z-dimension\n", |
|
181 | 186 | { |
182 | 187 | "cell_type": "code", |
183 | 188 | "execution_count": null, |
184 | | - "id": "48a21f98", |
| 189 | + "id": "b60a1b77", |
185 | 190 | "metadata": {}, |
186 | | - "outputs": [], |
| 191 | + "outputs": [ |
| 192 | + { |
| 193 | + "name": "stdout", |
| 194 | + "output_type": "stream", |
| 195 | + "text": [ |
| 196 | + "DONE: 08082024_rLabel_014.2_TAMRA_sense_P00002_C4scaled_oriScale.tif\n", |
| 197 | + "DONE: 08082024_rLabel_014.2_TAMRA_sense_P00006_C4scaled_oriScale.tif\n", |
| 198 | + "DONE: 08082024_rLabel_014.2_TAMRA_sense_P00009_C4scaled_oriScale.tif\n", |
| 199 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00013_C4scaled_oriScale.tif\n", |
| 200 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00017_C4scaled_oriScale.tif\n", |
| 201 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00020_C4scaled_oriScale.tif\n", |
| 202 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00023_C4scaled_oriScale.tif\n", |
| 203 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00027_C4scaled_oriScale.tif\n", |
| 204 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00031_C4scaled_oriScale.tif\n", |
| 205 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00033_C4scaled_oriScale.tif\n", |
| 206 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00045_C4scaled_oriScale.tif\n", |
| 207 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00049_C4scaled_oriScale.tif\n", |
| 208 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00051_C4scaled_oriScale.tif\n", |
| 209 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00052_C4scaled_oriScale.tif\n", |
| 210 | + "DONE: 21082024_rLabel_024_TAMRA_sense_P00055_C4scaled_oriScale.tif\n", |
| 211 | + "DONE: C3-03022022_Label49_t1_100x_0p21_02_POS_current_scaled_oriScale.tif\n", |
| 212 | + "DONE: C3-03022022_Label49_t1_100x_0p21_03_POS_current_scaled_oriScale.tif\n", |
| 213 | + "DONE: C3-03022022_Label49_t3_100x_0p21_03_POS_current_scaled_oriScale.tif\n", |
| 214 | + "DONE: C3-03022022_Label49_t3_100x_0p21_04_POS_current_scaled_oriScale.tif\n", |
| 215 | + "DONE: C3-26012022_Label48_t2_100x_0p21_03_POS_current_scaled_oriScale.tif\n", |
| 216 | + "DONE: C4-02122021_Label43_label1_343_0p25_100x_0p21_01_scaled_oriScale.tif\n", |
| 217 | + "DONE: C4-02122021_Label43_label1_343_0p25_100x_0p21_02_scaled_oriScale.tif\n", |
| 218 | + "DONE: C4-02122021_Label43_label1_343_0p25_100x_0p21_04_scaled_oriScale.tif\n", |
| 219 | + "DONE: C4-03022022_Label49_t2_100x_0p21_03_POS_current_scaled_oriScale.tif\n", |
| 220 | + "DONE: C4-03022022_Label49_t2_100x_0p21_04_POS_current_scaled_oriScale.tif\n", |
| 221 | + "DONE: C4-12012022_Label46_367L_BCd16low_100x_0p21_02_scaled_oriScale.tif\n", |
| 222 | + "DONE: C4-12012022_Label46_367L_Cd16_100x_0p21_01_scaled_oriScale.tif\n", |
| 223 | + "DONE: C4-12012022_Label46_367L_Cd16_100x_0p21_03_scaled_oriScale.tif\n", |
| 224 | + "DONE: C4-16122021_Label44_CD16_367L_w1_closetolabel_100x_0p21_01_scaled_oriScale.tif\n", |
| 225 | + "DONE: C4-16122021_Label44_CD16_367L_w1_closetolabel_100x_0p21_02_scaled_oriScale.tif\n", |
| 226 | + "DONE: C4-16122021_Label44_CD16_367L_w1_closetolabel_100x_0p21_03_scaled_oriScale.tif\n", |
| 227 | + "DONE: C4-16122021_Label44_CD16_367L_w1_closetolabel_100x_0p21_04_scaled_oriScale.tif\n", |
| 228 | + "DONE: C4-16122021_Label45_367L_w3_1076_100x_0p21_01_scaled_oriScale.tif\n", |
| 229 | + "DONE: C4-16122021_Label45_367L_w3_1076_100x_0p21_03_scaled_oriScale.tif\n", |
| 230 | + "DONE: C4-16122021_Label45_367L_w3_1077_100x_0p21_01_scaled_oriScale.tif\n", |
| 231 | + "DONE: C4-16122021_Label45_367L_w3_1079_100x_0p21_03_scaled_oriScale.tif\n", |
| 232 | + "DONE: C4-26012022_Label48_t1_100x_0p21_03_POS_current_scaled_oriScale.tif\n", |
| 233 | + "DONE: C4-26012022_Label48_t1strep_100x_0p21_02_POS_current_scaled_oriScale.tif\n", |
| 234 | + "DONE: C4-26012022_Label48_t1strep_100x_0p21_03_POS_current_scaled_oriScale.tif\n", |
| 235 | + "DONE: L72_w6_P00124_scaled_oriScale.tif\n", |
| 236 | + "DONE: L74_w9_P00101_scaled_oriScale.tif\n", |
| 237 | + "DONE: L74_w9_P00107_scaled_oriScale.tif\n", |
| 238 | + "DONE: rLabel_012.2_TAMRA_sense_P00019_C4scaled_oriScale.tif\n", |
| 239 | + "DONE: rLabel_012.2_TAMRA_sense_P00028_C4scaled_oriScale.tif\n", |
| 240 | + "DONE: rLabel_012.2_TAMRA_sense_P00033_C4scaled_oriScale.tif\n", |
| 241 | + "DONE: rLabel_012.2_TAMRA_sense_P00034_C4scaled_oriScale.tif\n", |
| 242 | + "DONE: rLabel_012.2_TAMRA_sense_P00041_C4scaled_oriScale.tif\n", |
| 243 | + "DONE: rLabel_012.2_TAMRA_sense_P00042_C4scaled_oriScale.tif\n" |
| 244 | + ] |
| 245 | + } |
| 246 | + ], |
187 | 247 | "source": [ |
188 | 248 | "# Get the list of files in the specified image directory\n", |
189 | 249 | "img_dir_list = sorted(os.listdir(img_directory))\n", |
|
193 | 253 | " # only process files with .tif or .tiff extensions\n", |
194 | 254 | " if file.endswith((\".tif\", \".tiff\")):\n", |
195 | 255 | " img = imread(os.path.join(img_directory, file))\n", |
196 | | - " mask = imread(os.path.join(og_mask_directory, file))\n", |
197 | 256 | " lbl = imread(os.path.join(watershed_label_directory, file))\n", |
198 | 257 | "\n", |
| 258 | + " # Handle different cases for og_mask_directory\n", |
| 259 | + " try:\n", |
| 260 | + " # Check if variable exists and is a valid directory path\n", |
| 261 | + " if og_mask_directory and os.path.isdir(og_mask_directory):\n", |
| 262 | + " mask = imread(os.path.join(og_mask_directory, file))\n", |
| 263 | + " else:\n", |
| 264 | + " mask = np.copy(lbl)\n", |
| 265 | + " except (NameError, TypeError):\n", |
| 266 | + " # Variable doesn't exist or is None\n", |
| 267 | + " mask = np.copy(lbl)\n", |
| 268 | + "\n", |
199 | 269 | " # Normalize the image from 1 to 99.8 percentile\n", |
200 | 270 | " img = normalize(img, 1, 99.8, axis=(0, 1, 2))\n", |
201 | 271 | "\n", |
|
0 commit comments