|
156 | 156 | "cell_type": "code", |
157 | 157 | "execution_count": null, |
158 | 158 | "metadata": { |
159 | | - "id": "fq21zJVFNASx", |
160 | | - "cellView": "form" |
| 159 | + "cellView": "form", |
| 160 | + "id": "fq21zJVFNASx" |
161 | 161 | }, |
162 | 162 | "outputs": [], |
163 | 163 | "source": [ |
|
238 | 238 | "cell_type": "code", |
239 | 239 | "execution_count": null, |
240 | 240 | "metadata": { |
241 | | - "id": "HMAdm-Mc9HFz", |
242 | | - "cellView": "form" |
| 241 | + "cellView": "form", |
| 242 | + "id": "HMAdm-Mc9HFz" |
243 | 243 | }, |
244 | 244 | "outputs": [], |
245 | 245 | "source": [ |
246 | 246 | "from __future__ import print_function, unicode_literals, absolute_import, division\n", |
247 | 247 | "#@markdown ##Load key dependencies\n", |
248 | | - "Notebook_version = '1.20.3'\n", |
| 248 | + "Notebook_version = '1.20.4'\n", |
249 | 249 | "Network = 'StarDist 2D'\n", |
250 | 250 | "\n", |
251 | 251 | "from builtins import any as b_any\n", |
|
909 | 909 | "cell_type": "code", |
910 | 910 | "execution_count": null, |
911 | 911 | "metadata": { |
912 | | - "id": "zCvebubeSaGY", |
913 | | - "cellView": "form" |
| 912 | + "cellView": "form", |
| 913 | + "id": "zCvebubeSaGY" |
914 | 914 | }, |
915 | 915 | "outputs": [], |
916 | 916 | "source": [ |
|
941 | 941 | "cell_type": "code", |
942 | 942 | "execution_count": null, |
943 | 943 | "metadata": { |
944 | | - "id": "01Djr8v-5pPk", |
945 | | - "cellView": "form" |
| 944 | + "cellView": "form", |
| 945 | + "id": "01Djr8v-5pPk" |
946 | 946 | }, |
947 | 947 | "outputs": [], |
948 | 948 | "source": [ |
|
989 | 989 | "cell_type": "code", |
990 | 990 | "execution_count": null, |
991 | 991 | "metadata": { |
992 | | - "id": "P1f_hZ_ApuNZ", |
993 | | - "cellView": "form" |
| 992 | + "cellView": "form", |
| 993 | + "id": "P1f_hZ_ApuNZ" |
994 | 994 | }, |
995 | 995 | "outputs": [], |
996 | 996 | "source": [ |
|
1070 | 1070 | "cell_type": "code", |
1071 | 1071 | "execution_count": null, |
1072 | 1072 | "metadata": { |
1073 | | - "id": "ewpNJ_I0Mv47", |
1074 | | - "cellView": "form" |
| 1073 | + "cellView": "form", |
| 1074 | + "id": "ewpNJ_I0Mv47" |
1075 | 1075 | }, |
1076 | 1076 | "outputs": [], |
1077 | 1077 | "source": [ |
|
1219 | 1219 | "cell_type": "code", |
1220 | 1220 | "execution_count": null, |
1221 | 1221 | "metadata": { |
1222 | | - "id": "DMqWq5-AxnFU", |
1223 | | - "cellView": "form" |
| 1222 | + "cellView": "form", |
| 1223 | + "id": "DMqWq5-AxnFU" |
1224 | 1224 | }, |
1225 | 1225 | "outputs": [], |
1226 | 1226 | "source": [ |
|
1296 | 1296 | "cell_type": "code", |
1297 | 1297 | "execution_count": null, |
1298 | 1298 | "metadata": { |
1299 | | - "id": "9vC2n-HeLdiJ", |
1300 | | - "cellView": "form" |
| 1299 | + "cellView": "form", |
| 1300 | + "id": "9vC2n-HeLdiJ" |
1301 | 1301 | }, |
1302 | 1302 | "outputs": [], |
1303 | 1303 | "source": [ |
|
1336 | 1336 | " pretrained_model_name = \"2D_Demo\"\n", |
1337 | 1337 | " pretrained_model_path = base_path + \"/\" + pretrained_model_name\n", |
1338 | 1338 | " print(\"Downloading the 2D_Demo_Model_from_Stardist_Github\")\n", |
1339 | | - " if os.path.exists(pretrained_model_path):\n", |
1340 | | - " shutil.rmtree(pretrained_model_path)\n", |
1341 | | - " os.makedirs(pretrained_model_path)\n", |
| 1339 | + " os.makedirs(pretrained_model_path, exist_ok=True)\n", |
1342 | 1340 | " wget.download(\"https://github.com/mpicbg-csbd/stardist/raw/master/models/examples/2D_demo/config.json\", pretrained_model_path)\n", |
1343 | 1341 | " wget.download(\"https://github.com/mpicbg-csbd/stardist/raw/master/models/examples/2D_demo/thresholds.json\", pretrained_model_path)\n", |
1344 | 1342 | " wget.download(\"https://github.com/mpicbg-csbd/stardist/blob/master/models/examples/2D_demo/weights_best.h5?raw=true\", pretrained_model_path)\n", |
|
1352 | 1350 | " pretrained_model_name = \"2D_versatile_fluo\"\n", |
1353 | 1351 | " pretrained_model_path = base_path + \"/\" + pretrained_model_name\n", |
1354 | 1352 | "\n", |
1355 | | - " if os.path.exists(pretrained_model_path):\n", |
1356 | | - " shutil.rmtree(pretrained_model_path)\n", |
1357 | | - " os.makedirs(pretrained_model_path)\n", |
| 1353 | + " os.makedirs(pretrained_model_path, exist_ok=True)\n", |
1358 | 1354 | "\n", |
1359 | 1355 | " wget.download(\"https://cloud.mpi-cbg.de/index.php/s/1k5Zcy7PpFWRb0Q/download?path=/versatile&files=2D_versatile_fluo.zip\", pretrained_model_path)\n", |
1360 | 1356 | "\n", |
|
1370 | 1366 | " pretrained_model_name = \"2D_versatile_he\"\n", |
1371 | 1367 | " pretrained_model_path = base_path + \"/\" + pretrained_model_name\n", |
1372 | 1368 | "\n", |
1373 | | - " if os.path.exists(pretrained_model_path):\n", |
1374 | | - " shutil.rmtree(pretrained_model_path)\n", |
1375 | | - " os.makedirs(pretrained_model_path)\n", |
| 1369 | + " os.makedirs(pretrained_model_path, exist_ok=True)\n", |
1376 | 1370 | "\n", |
1377 | 1371 | " wget.download(\"https://cloud.mpi-cbg.de/index.php/s/1k5Zcy7PpFWRb0Q/download?path=/versatile&files=2D_versatile_he.zip\", pretrained_model_path)\n", |
1378 | 1372 | "\n", |
|
1465 | 1459 | "cell_type": "code", |
1466 | 1460 | "execution_count": null, |
1467 | 1461 | "metadata": { |
1468 | | - "id": "lIUAOJ_LMv5E", |
1469 | | - "cellView": "form" |
| 1462 | + "cellView": "form", |
| 1463 | + "id": "lIUAOJ_LMv5E" |
1470 | 1464 | }, |
1471 | 1465 | "outputs": [], |
1472 | 1466 | "source": [ |
|
1623 | 1617 | "cell_type": "code", |
1624 | 1618 | "execution_count": null, |
1625 | 1619 | "metadata": { |
| 1620 | + "cellView": "form", |
1626 | 1621 | "id": "iwNmp1PUzRDQ", |
1627 | | - "scrolled": true, |
1628 | | - "cellView": "form" |
| 1622 | + "scrolled": true |
1629 | 1623 | }, |
1630 | 1624 | "outputs": [], |
1631 | 1625 | "source": [ |
|
1650 | 1644 | "# convert the history.history dict to a pandas DataFrame:\n", |
1651 | 1645 | "lossData = pd.DataFrame(history.history)\n", |
1652 | 1646 | "\n", |
1653 | | - "if os.path.exists(model_path+\"/\"+model_name+\"/Quality Control\"):\n", |
1654 | | - " shutil.rmtree(model_path+\"/\"+model_name+\"/Quality Control\")\n", |
1655 | | - "\n", |
1656 | | - "os.makedirs(model_path+\"/\"+model_name+\"/Quality Control\")\n", |
| 1647 | + "os.makedirs(model_path+\"/\"+model_name+\"/Quality Control\", exist_ok=True)\n", |
1657 | 1648 | "\n", |
1658 | 1649 | "# The training evaluation.csv is saved (overwrites the Files if needed).\n", |
1659 | 1650 | "lossDataCSVpath = model_path+'/'+model_name+'/Quality Control/training_evaluation.csv'\n", |
|
1703 | 1694 | "cell_type": "code", |
1704 | 1695 | "execution_count": null, |
1705 | 1696 | "metadata": { |
1706 | | - "id": "eAJzMwPA6tlH", |
1707 | | - "cellView": "form" |
| 1697 | + "cellView": "form", |
| 1698 | + "id": "eAJzMwPA6tlH" |
1708 | 1699 | }, |
1709 | 1700 | "outputs": [], |
1710 | 1701 | "source": [ |
|
1762 | 1753 | "cell_type": "code", |
1763 | 1754 | "execution_count": null, |
1764 | 1755 | "metadata": { |
1765 | | - "id": "vMzSP50kMv5p", |
1766 | | - "cellView": "form" |
| 1756 | + "cellView": "form", |
| 1757 | + "id": "vMzSP50kMv5p" |
1767 | 1758 | }, |
1768 | 1759 | "outputs": [], |
1769 | 1760 | "source": [ |
|
1831 | 1822 | "cell_type": "code", |
1832 | 1823 | "execution_count": null, |
1833 | 1824 | "metadata": { |
1834 | | - "id": "w90MdriMxhjD", |
1835 | | - "cellView": "form" |
| 1825 | + "cellView": "form", |
| 1826 | + "id": "w90MdriMxhjD" |
1836 | 1827 | }, |
1837 | 1828 | "outputs": [], |
1838 | 1829 | "source": [ |
|
1850 | 1841 | "Target_QC_folder = check_base_path(base_path, Target_QC_folder)\n", |
1851 | 1842 | "\n", |
1852 | 1843 | "#Create a quality control Folder and check if the folder already exist\n", |
1853 | | - "if os.path.exists(QC_model_path+\"/\"+QC_model_name+\"/Quality Control\") == False:\n", |
1854 | | - " os.makedirs(QC_model_path+\"/\"+QC_model_name+\"/Quality Control\")\n", |
1855 | | - "\n", |
1856 | | - "if os.path.exists(QC_model_path+\"/\"+QC_model_name+\"/Quality Control/Prediction\"):\n", |
1857 | | - " shutil.rmtree(QC_model_path+\"/\"+QC_model_name+\"/Quality Control/Prediction\")\n", |
1858 | | - "\n", |
1859 | | - "os.makedirs(QC_model_path+\"/\"+QC_model_name+\"/Quality Control/Prediction\")\n", |
| 1844 | + "os.makedirs(QC_model_path+\"/\"+QC_model_name+\"/Quality Control/Prediction\", exist_ok=True)\n", |
1860 | 1845 | "\n", |
1861 | 1846 | "# Check if the sourcce and target folders contain base_path, if not add it\n", |
1862 | 1847 | "Source_QC_folder = check_base_path(base_path, Source_QC_folder)\n", |
|
2032 | 2017 | "cell_type": "code", |
2033 | 2018 | "execution_count": null, |
2034 | 2019 | "metadata": { |
2035 | | - "id": "v5F2VWnQ_fL-", |
2036 | | - "cellView": "form" |
| 2020 | + "cellView": "form", |
| 2021 | + "id": "v5F2VWnQ_fL-" |
2037 | 2022 | }, |
2038 | 2023 | "outputs": [], |
2039 | 2024 | "source": [ |
|
2159 | 2144 | "cell_type": "code", |
2160 | 2145 | "execution_count": null, |
2161 | 2146 | "metadata": { |
2162 | | - "id": "y2TD5p7MZrEb", |
2163 | | - "cellView": "form" |
| 2147 | + "cellView": "form", |
| 2148 | + "id": "y2TD5p7MZrEb" |
2164 | 2149 | }, |
2165 | 2150 | "outputs": [], |
2166 | 2151 | "source": [ |
|
2177 | 2162 | "# Check that the base_path is on the path and otherwise add it\n", |
2178 | 2163 | "Results_folder = check_base_path(base_path, Results_folder)\n", |
2179 | 2164 | "\n", |
2180 | | - "if os.path.exists(Results_folder):\n", |
2181 | | - " shutil.rmtree(Results_folder)\n", |
2182 | | - "os.makedirs(Results_folder)\n", |
| 2165 | + "os.makedirs(Results_folder, exist_ok=True)\n", |
2183 | 2166 | "\n", |
2184 | 2167 | "#@markdown ###Are your data single images or stacks?\n", |
2185 | 2168 | "\n", |
|
2311 | 2294 | " short_name = os.path.splitext(image)\n", |
2312 | 2295 | " print(f\"{image} correctly loaded!\")\n", |
2313 | 2296 | "\n", |
| 2297 | + " # Check if the image has already been processed\n", |
| 2298 | + " output_frame_path = os.path.join(Results_folder, f\"{short_name[0]}\")\n", |
| 2299 | + " if os.path.exists(output_frame_path):\n", |
| 2300 | + " if len(os.listdir(output_frame_path)) == timelapse.shape[0]:\n", |
| 2301 | + " print(bcolors.WARNING + \"The stack \"+image+\" has already been processed. Please remove the folder \"+output_frame_path+\" if you want to reprocess the stack.\"+bcolors.ENDC)\n", |
| 2302 | + " continue\n", |
| 2303 | + " else:\n", |
| 2304 | + " print(bcolors.WARNING + \"The stack \"+image+\" is partially processed. Continuing with the unprocessed frames.\"+bcolors.ENDC)\n", |
| 2305 | + " num_processed_frames = len(os.listdir(output_frame_path))\n", |
| 2306 | + " else:\n", |
| 2307 | + " num_processed_frames = 0\n", |
| 2308 | + "\n", |
2314 | 2309 | " short_name = os.path.splitext(image)\n", |
2315 | 2310 | "\n", |
2316 | 2311 | " mi = np.percentile(timelapse,1,axis=(0,)+tuple(1+np.array(axis_norm)),keepdims=True)[0]\n", |
|
2330 | 2325 | " n_timepoint = timelapse.shape[0]\n", |
2331 | 2326 | " # prediction_stack = np.zeros((n_timepoint, timelapse.shape[1], timelapse.shape[2]))\n", |
2332 | 2327 | "\n", |
2333 | | - " for t in range(n_timepoint):\n", |
| 2328 | + " for t in range(num_processed_frames, n_timepoint):\n", |
2334 | 2329 | " print(f\"Analyzing timepoint {t}/{n_timepoint}\")\n", |
2335 | 2330 | " img_t = timelapse[t]\n", |
2336 | 2331 | " # Normalize the image with the percentiles from the whole stack\n", |
|
2409 | 2404 | "cell_type": "code", |
2410 | 2405 | "execution_count": null, |
2411 | 2406 | "metadata": { |
2412 | | - "id": "jxjHeOFFleSV", |
2413 | | - "cellView": "form" |
| 2407 | + "cellView": "form", |
| 2408 | + "id": "jxjHeOFFleSV" |
2414 | 2409 | }, |
2415 | 2410 | "outputs": [], |
2416 | 2411 | "source": [ |
|
2426 | 2421 | "# Check that the base_path is on the path and otherwise add it\n", |
2427 | 2422 | "Results_folder = check_base_path(base_path, Results_folder)\n", |
2428 | 2423 | "\n", |
2429 | | - "if os.path.exists(Results_folder):\n", |
2430 | | - " shutil.rmtree(Results_folder)\n", |
2431 | | - "os.makedirs(Results_folder)\n", |
| 2424 | + "os.makedirs(Results_folder, exist_ok=True)\n", |
2432 | 2425 | "\n", |
2433 | 2426 | "# model name and path\n", |
2434 | 2427 | "#@markdown ###Do you want to use the current trained model?\n", |
|
2534 | 2527 | "#Create a temp folder to save Zarr files\n", |
2535 | 2528 | "\n", |
2536 | 2529 | "Temp_folder = base_path + \"/Temp_folder\"\n", |
2537 | | - "\n", |
2538 | | - "if os.path.exists(Temp_folder):\n", |
2539 | | - " shutil.rmtree(Temp_folder)\n", |
2540 | | - "os.makedirs(Temp_folder)\n", |
| 2530 | + "os.makedirs(Temp_folder, exist_ok=True)\n", |
2541 | 2531 | "\n", |
2542 | 2532 | "\n", |
2543 | 2533 | "# mi, ma = np.percentile(img[::8], [1,99.8]) # compute percentiles from low-resolution image\n", |
2544 | 2534 | "# mi, ma = np.percentile(img[13000:16000,13000:16000], [1,99.8]) # compute percentiles from smaller crop\n", |
2545 | 2535 | "mi, ma = 0, 255 # use min and max dtype values (suitable here)\n", |
2546 | 2536 | "normalizer = MyNormalizer(mi, ma)\n", |
2547 | 2537 | "\n", |
2548 | | - "\n", |
2549 | 2538 | "np.random.seed(16)\n", |
2550 | 2539 | "lbl_cmap = random_label_cmap()\n", |
2551 | 2540 | "\n", |
|
2561 | 2550 | "\n", |
2562 | 2551 | " print(\"Image dimension \"+str(X.shape))\n", |
2563 | 2552 | "\n", |
2564 | | - " if is_stack:\n", |
| 2553 | + " if Is_stack:\n", |
| 2554 | + " stack_result_path = os.path.join(Results_folder, short_name[0])\n", |
| 2555 | + " polygon_stack_result_path = os.path.join(Results_folder, f\"{short_name[0]}_labels_roi\")\n", |
| 2556 | + "\n", |
| 2557 | + " # Check if the stack has been processed or is half processed\n", |
| 2558 | + " if os.path.exists(stack_result_path):\n", |
| 2559 | + " if len(os.listdir(stack_result_path)) == X.shape[0]:\n", |
| 2560 | + " print(bcolors.WARNING + \"The stack \"+image+\" has already been processed. Please remove the folder \"+stack_result_path+\" if you want to reprocess the stack.\"+bcolors.ENDC)\n", |
| 2561 | + " continue\n", |
| 2562 | + " else:\n", |
| 2563 | + " print(bcolors.WARNING + \"The stack \"+image+\" is partially processed. Continuing with the unprocessed frames.\"+bcolors.ENDC)\n", |
| 2564 | + " num_processed_frames = len(os.listdir(stack_result_path))\n", |
| 2565 | + " elif os.path.exists(polygon_stack_result_path):\n", |
| 2566 | + " if len(os.listdir(polygon_stack_result_path)) == X.shape[0]:\n", |
| 2567 | + " print(bcolors.WARNING + \"The stack \"+image+\" has already been processed. Please remove the folder \"+polygon_stack_result_path+\" if you want to reprocess the stack.\"+bcolors.ENDC)\n", |
| 2568 | + " continue\n", |
| 2569 | + " else:\n", |
| 2570 | + " print(bcolors.WARNING + \"The stack \"+image+\" is partially processed. Continuing with the unprocessed frames.\"+bcolors.ENDC)\n", |
| 2571 | + " num_processed_frames = len(os.listdir(polygon_stack_result_path))\n", |
| 2572 | + " else:\n", |
| 2573 | + " num_processed_frames = 0\n", |
| 2574 | + "\n", |
2565 | 2575 | " if Mask_images:\n", |
2566 | | - " stack_result_path = os.path.join(Results_folder, short_name[0])\n", |
2567 | 2576 | " os.makedirs(stack_result_path, exist_ok=True)\n", |
2568 | 2577 | " if Region_of_interests:\n", |
2569 | | - " polygon_stack_result_path = os.path.join(Results_folder, f\"{short_name[0]}_labels_roi\")\n", |
2570 | 2578 | " os.makedirs(polygon_stack_result_path, exist_ok=True)\n", |
2571 | 2579 | "\n", |
2572 | 2580 | " num_frames = X.shape[0]\n", |
|
2641 | 2649 | "# **7. Version log**\n", |
2642 | 2650 | "---\n", |
2643 | 2651 | "\n", |
| 2652 | + "<font size = 4>**v1.20.4**: \n", |
| 2653 | + "\n", |
| 2654 | + "* Modify the section `6. Using the trained model notebook`:\n", |
| 2655 | + " * On the stack prediction of section 6.1., to avoid out of memory error, the normalization of tha data is not done on the whole stack. Instead the minimum and maximum percentiles are taken from the full stack and then each frame is individually normalised.\n", |
| 2656 | + " * Also to avoid out of memory errors on section 6.1. each frame is individually saved in a folder. This is not efficient in terms of storage, but release the RAM memory required to store the predicted stack.\n", |
| 2657 | + " * On section 6.2., I included the 'Is_stack' option and included previous features.\n", |
| 2658 | + "\n", |
2644 | 2659 | "<font size = 4>**v1.20.3**: \n", |
2645 | 2660 | "\n", |
2646 | 2661 | "* Change the TensorFlow and Keras versions to Keras < 3. This has fixed the BioImage.IO model exportation.\n", |
|
2703 | 2718 | } |
2704 | 2719 | ], |
2705 | 2720 | "metadata": { |
| 2721 | + "accelerator": "GPU", |
2706 | 2722 | "colab": { |
2707 | | - "provenance": [], |
2708 | | - "gpuType": "T4" |
| 2723 | + "gpuType": "T4", |
| 2724 | + "provenance": [] |
2709 | 2725 | }, |
2710 | 2726 | "kernelspec": { |
2711 | 2727 | "display_name": "Python 3", |
|
2722 | 2738 | "nbconvert_exporter": "python", |
2723 | 2739 | "pygments_lexer": "ipython3", |
2724 | 2740 | "version": "3.6.12" |
2725 | | - }, |
2726 | | - "accelerator": "GPU" |
| 2741 | + } |
2727 | 2742 | }, |
2728 | 2743 | "nbformat": 4, |
2729 | 2744 | "nbformat_minor": 0 |
|
0 commit comments