-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
859f446
commit 247a113
Showing
10 changed files
with
552 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "446c6e4f-a5f0-4e22-982b-1a90fa6c5742", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Codes for the papar by Tsuyama et al (2023) PlosOne\n", | ||
"#### 001 : Data preparation for UNET" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "bf1fd544-7a20-46b5-a876-540f0a8f9387", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from common.data_prep import *\n", | ||
"from common.model import *\n", | ||
"# %matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a7efcd6d-1bf3-4542-b08d-4801d999ef48", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# define your job number under _run directory\n", | ||
"job_num = '003'\n", | ||
"# set reduction_factor of the initial image \n", | ||
"r_f = 2\n", | ||
"# set unet training directory\n", | ||
"train_set = ['image','mask']\n", | ||
"#\n", | ||
"set_dir(os.path.join('./_run',job_num))\n", | ||
"set_dir(os.path.join('./_run',job_num,'model'))\n", | ||
"for ids in train_set:\n", | ||
" set_dir(os.path.join('./_run',job_num, 'train', ids))\n", | ||
"set_dir(os.path.join('./_run',job_num,'segmentation')) # segmented VB areas in png\n", | ||
"set_dir(os.path.join('./_run',job_num,'morphology')) # calculated VB morphological parameters\n", | ||
"set_dir(os.path.join('./_run',job_num,'extracted_VB_images')) # extracted VBs along the radial positions" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b36b0439-c00e-469c-be92-0dfc146e46ef", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# prepare training set for unet\n", | ||
"path =\"./_original_png\"\n", | ||
"filenumbers, Images, Labels = make_unet_data(path, r_f) \n", | ||
"\n", | ||
"n=0\n", | ||
"for i in list(range(len(filenumbers))):\n", | ||
" for k in list(range(filenumbers[i])):\n", | ||
" n += 1\n", | ||
" #cv2.cvtColor(img_bgrL, cv2.COLOR_BGR2GRAY)\n", | ||
" cv2.imwrite(os.path.join('./_run',job_num, 'train', train_set[0],str(n)+'.png'), Images[i][k][:])\n", | ||
" cv2.imwrite(os.path.join('./_run',job_num, 'train', train_set[1],str(n)+'.png'), Labels[i][k][:])\n", | ||
"print(n, ' images were generated')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5fbcf3de-093a-4811-947c-eea78a6e52cb", | ||
"metadata": {}, | ||
"source": [ | ||
"The following code generates the model for segmentation of vasucular bundles " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "44989d80-bdaf-4565-b31a-bce966f846d7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data_gen_args = dict(rotation_range=1,\n", | ||
" width_shift_range=0.1,\n", | ||
" height_shift_range=0.1,\n", | ||
" shear_range=0.1,\n", | ||
" zoom_range=0.1,\n", | ||
" horizontal_flip=True,\n", | ||
" fill_mode='nearest')\n", | ||
"myGene = trainGenerator(12,os.path.join('./_run',job_num, 'train'),'image','mask',data_gen_args,save_to_dir = None)\n", | ||
"model = unet()\n", | ||
"model_checkpoint = ModelCheckpoint(os.path.join('./_run',job_num, 'model','unet_moso_2.hdf5'), monitor='loss',verbose=1, save_best_only=True)\n", | ||
"model.fit_generator(myGene,steps_per_epoch=300,epochs=20,callbacks=[model_checkpoint])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0f150f33-2f42-46b1-a44b-fcb9238d44a0", | ||
"metadata": {}, | ||
"source": [ | ||
"The end of codes" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8c48287a-b60f-4e95-a2b3-b9b93f0978d7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "JS", | ||
"language": "python", | ||
"name": "js" | ||
}, | ||
"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.7.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
107 changes: 107 additions & 0 deletions
107
.ipynb_checkpoints/002_segment_analysis-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3f934f2c-7259-4414-ac8f-6da7a42e76b7", | ||
"metadata": {}, | ||
"source": [ | ||
"### 002: codes for segmentation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "734d6277-a4b9-44cf-af6e-0e19905c5dd4", | ||
"metadata": {}, | ||
"source": [ | ||
"Read microscope images in the target directory, split them into patch images and predict VB area using the UNET model.\n", | ||
"Generated segmented patches are then reassembled to the images and stored in r_target directory as original_filename+_predicted.png." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c3babe8f-7b37-4024-a812-bcdac363c0bc", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from common.data_prep import *\n", | ||
"from common.model import *\n", | ||
"from common.img_patch import *\n", | ||
"\n", | ||
"\n", | ||
"# target image for segmentation\n", | ||
"target = './_test'\n", | ||
"\n", | ||
"# define your job number under _run directory\n", | ||
"job_num = '001'\n", | ||
"# set reduction_factor of the initial image \n", | ||
"r_f = 2\n", | ||
"\n", | ||
"\n", | ||
"set_dir(os.path.join('./_run',job_num,'segmentation')) # segmented VB areas of test images\n", | ||
"#set_dir(os.path.join('./_run',job_num,'morphology')) # calculated VB morphological parameters\n", | ||
"#set_dir(os.path.join('./_run',job_num,'extracted_VB_images')) # extracted VBs along the radial positions\n", | ||
"\n", | ||
" \n", | ||
"target ='./_test'\n", | ||
"r_target = os.path.join('./_run',job_num,'segmentation')\n", | ||
"\n", | ||
"\n", | ||
"file_list=sorted([filename for filename in os.listdir(target) if not filename.startswith('.')])\n", | ||
"\n", | ||
"\n", | ||
"for fln in range(len(file_list)):\n", | ||
" img_path=os.path.join(target, file_list[fln])\n", | ||
" res_path=os.path.join(r_target, file_list[fln])\n", | ||
" img=io.imread(img_path, as_gray=True) # gray scale\n", | ||
"\n", | ||
" # r_f=2 #factor of image reduction\n", | ||
" px=int(img.shape[0]/r_f)\n", | ||
" py=int(img.shape[1]/r_f)\n", | ||
" img_s=trans.resize(img, (px,py))\n", | ||
" # making image patch of 512*512 )\n", | ||
" testdata = patch_Gen(img_s)\n", | ||
" pos = patch_Counter(img_s)\n", | ||
" # predicton of vascular bundles by UNET \n", | ||
" model = unet()\n", | ||
" model.load_weights(os.path.join('./_run',job_num, 'model','unet_moso_2.hdf5'))\n", | ||
" results = model.predict_generator(testdata,len(pos),verbose=1)\n", | ||
" #saveResult(\"./prediction\",results)\n", | ||
" # reassemble to the image\n", | ||
" rec_img=patch_Assemble(img_s, results)\n", | ||
" rec_img=img_as_ubyte(rec_img)\n", | ||
" # 合成された画像を保存\n", | ||
" cv2.imwrite(res_path.replace('.png', '_predicted.png'),rec_img)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "781e6731-93d7-4581-a6e6-07984aed3084", | ||
"metadata": {}, | ||
"source": [ | ||
"The end of codes" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "JS", | ||
"language": "python", | ||
"name": "js" | ||
}, | ||
"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.7.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.