Skip to content

Commit

Permalink
2/26 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
seravee08 committed Feb 27, 2020
1 parent 93df2bc commit d068b98
Show file tree
Hide file tree
Showing 11 changed files with 734 additions and 89 deletions.
147 changes: 130 additions & 17 deletions Archpool.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -83,13 +83,13 @@
" [\"ConvT2d\", [128, 512, 4, 1, 0, False]],\n",
" [\"BN2d\", [512]],\n",
" [\"Relu\", [True]],\n",
" [\"ResBlock2d\", [512, 256, 3, \"zero\", \"BN2d\", False, False, 1.0]],\n",
" [\"ResBlock2d\", [512, 256, 3, \"zero\", \"BN2d\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"Relu\", [True]],\n",
" [\"Interpolate\", [2, 'nearest']],\n",
" [\"ResBlock2d\", [256, 128, 3, \"zero\", \"BN2d\", False, False, 1.0]],\n",
" [\"ResBlock2d\", [256, 128, 3, \"zero\", \"BN2d\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"Relu\", [True]],\n",
" [\"Interpolate\", [2, 'nearest']],\n",
" [\"ResBlock2d\", [128, 64, 3, \"zero\", \"BN2d\", False, False, 1.0]],\n",
" [\"ResBlock2d\", [128, 64, 3, \"zero\", \"BN2d\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"Relu\", [True]],\n",
" [\"Interpolate\", [2, 'nearest']],\n",
" [\"ConvT2d\", [64, 1, 4, 2, 1, False]],\n",
Expand All @@ -101,13 +101,124 @@
" [\"input_dimensions\", [1, 64, 64]],\n",
" [\"Conv2d\", [1, 64, 3, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"ResBlock2d\", [64, 128, 3, \"zero\", \"None\", False, False, 1.0]],\n",
" [\"ResBlock2d\", [64, 128, 3, \"zero\", \"None\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"ResBlock2d\", [128, 256, 3, \"zero\", \"None\", False, False, 1.0]],\n",
" [\"ResBlock2d\", [128, 256, 3, \"zero\", \"None\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"ResBlock2d\", [256, 512, 3, \"zero\", \"None\", False, False, 1.0]],\n",
" [\"ResBlock2d\", [256, 512, 3, \"zero\", \"None\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"Conv2d\", [512, 1, 4, 1, 0, False]]\n",
" ]\n",
" \n",
" if idx == 6: # [DCGAN, generator, grayscale, 128x128, wgangp]\n",
" arch = [\n",
" [\"input_dimensions\", [128, 1, 1]],\n",
" [\"ConvT2d\", [128, 512, 4, 1, 0, False]],\n",
" [\"BN2d\", [512]],\n",
" [\"Relu\", [True]],\n",
" [\"ConvT2d\", [512, 256, 4, 2, 1, False]],\n",
" [\"BN2d\", [256]],\n",
" [\"Relu\", [True]],\n",
" [\"ConvT2d\", [256, 128, 4, 2, 1, False]],\n",
" [\"BN2d\", [128]],\n",
" [\"Relu\", [True]],\n",
" [\"ConvT2d\", [128, 64, 4, 2, 1, False]],\n",
" [\"BN2d\", [64]],\n",
" [\"Relu\", [True]],\n",
" [\"ConvT2d\", [64, 32, 4, 2, 1, False]],\n",
" [\"BN2d\", [32]],\n",
" [\"Relu\", [True]],\n",
" [\"ConvT2d\", [32, 1, 4, 2, 1, False]],\n",
" [\"Tanh\", []]\n",
" ]\n",
" \n",
" if idx == 7: # [DCGAN, discriminator, grayscale, 128x128, wgangp]\n",
" arch = [\n",
" [\"input_dimensions\", [1, 128, 128]],\n",
" [\"Conv2d\", [1, 32, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"Conv2d\", [32, 64, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"Conv2d\", [64, 128, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"Conv2d\", [128, 256, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"Conv2d\", [256, 512, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"Conv2d\", [512, 1, 4, 1, 0, False]]\n",
" ]\n",
" \n",
" if idx == 8: # [Resnet, generator, grayscale, 128x128, wgangp]\n",
" arch = [\n",
" [\"input_dimensions\", [128, 1, 1]],\n",
" [\"ConvT2d\", [128, 1024, 4, 1, 0, False]],\n",
" [\"BN2d\", [1024]],\n",
" [\"Relu\", [True]],\n",
" [\"ResBlock2d\", [1024, 512, 3, \"zero\", \"BN2d\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"Relu\", [True]],\n",
" [\"Interpolate\", [2, 'nearest']],\n",
" [\"ResBlock2d\", [512, 256, 3, \"zero\", \"BN2d\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"Relu\", [True]],\n",
" [\"Interpolate\", [2, 'nearest']],\n",
" [\"ResBlock2d\", [256, 128, 3, \"zero\", \"BN2d\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"Relu\", [True]],\n",
" [\"Interpolate\", [2, 'nearest']],\n",
" [\"ResBlock2d\", [128, 64, 3, \"zero\", \"BN2d\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"Relu\", [True]],\n",
" [\"Interpolate\", [2, 'nearest']],\n",
" [\"ConvT2d\", [64, 1, 4, 2, 1, False]],\n",
" [\"Tanh\", []]\n",
" ]\n",
" \n",
" if idx == 9: # [Resnet, discriminator, grayscale, 128x128, wgangp]\n",
" arch = [\n",
" [\"input_dimensions\", [1, 64, 64]],\n",
" [\"Conv2d\", [1, 64, 3, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"ResBlock2d\", [64, 128, 3, \"zero\", \"None\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"ResBlock2d\", [128, 256, 3, \"zero\", \"None\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"ResBlock2d\", [256, 512, 3, \"zero\", \"None\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"ResBlock2d\", [512, 1024, 3, \"zero\", \"None\", False, False, 1.0, \"Conv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"Conv2d\", [1024, 1, 4, 1, 0, False]]\n",
" ]\n",
" \n",
" if idx == 10: # [DCGAN, discriminator, grayscale, 64x64, wgangp, spectral_normalization]\n",
" arch = [\n",
" [\"input_dimensions\", [1, 64, 64]],\n",
" [\"SNConv2d\", [1, 64, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"SNConv2d\", [64, 128, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"SNConv2d\", [128, 256, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"SNConv2d\", [256, 512, 4, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"SNConv2d\", [512, 1, 4, 1, 0, False]]\n",
" ]\n",
" \n",
" if idx == 11: # [Resnet, discriminator, grayscale, 64x64, wgangp, spectral_normalization]\n",
" arch = [\n",
" [\"input_dimensions\", [1, 64, 64]],\n",
" [\"Conv2d\", [1, 64, 3, 2, 1, False]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"ResBlock2d\", [64, 128, 3, \"zero\", \"None\", False, False, 1.0, \"SNConv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"ResBlock2d\", [128, 256, 3, \"zero\", \"None\", False, False, 1.0, \"SNConv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"ResBlock2d\", [256, 512, 3, \"zero\", \"None\", False, False, 1.0, \"SNConv2d\"]],\n",
" [\"LeakyRelu\", [0.2, True]],\n",
" [\"AvgPool2d\", [3, 2, 1]],\n",
" [\"Conv2d\", [512, 1, 4, 1, 0, False]]\n",
Expand Down Expand Up @@ -141,10 +252,12 @@
" \"pixels\": [64, 64]\n",
" }\n",
" advanced_params[\"Topo_edge\"] = {\n",
" \"topology_loss_weight\": 0.0005,\n",
" \"target_topo_dimension\": 1,\n",
" \"topology_loss_weight\": 0.0005, # 0.0005 for cremi/isbi\n",
" \"use_blind_force\": False,\n",
" \"project_2_dim\": 0, # 0 for dim 1; 1 for dim 0\n",
" \"image_watershed\": 0.0, # has to be !FLOAT! number\n",
" \"target_topo_threshold\": 1.0,\n",
" \"target_topo_threshold\": 1.0, # needes to be 0.0 for 0dim\n",
" \"number_of_pts_to_fix\": 2,\n",
" \"hole_test_kernel_radius\": 5,\n",
" \"hole_test_iteration\": 1,\n",
Expand All @@ -154,7 +267,7 @@
" \"detect_opposite_2pts\": True,\n",
" \"connect_2pts\": True,\n",
" \"segment_thickness\": 4,\n",
" \"pd_subset_rate\": 0.5,\n",
" \"pd_subset_rate\": 0.1, # isbi 0.5, cremi 0.1\n",
" \"shuffle_subset\": True\n",
" }\n",
" return advanced_params\n",
Expand All @@ -172,8 +285,8 @@
" }\n",
" if idx == 1:\n",
" data_settings = {\n",
" \"epochs\": 20000,\n",
" \"batch_size\": 9,\n",
" \"epochs\": 2000,\n",
" \"batch_size\": 32,\n",
" \"batch_workers\": 0,\n",
" \"shuffle\": True,\n",
" \"drop_last\": True\n",
Expand All @@ -184,17 +297,17 @@
" \n",
" settings = dict()\n",
" settings[\"Basic\"] = {\n",
" \"branch_name\": \"isbi_patch64\", #\"cremi_patch64\",\n",
" \"branch_name\": \"cremi_patch64\", #\"map_patch64\", cremi_patch64\", \"isbi_patch64\", \"retina_patch128\"\n",
" \"continue_model\": True,\n",
" \"model_step\": 16800,\n",
" \"model_step\": 8500,\n",
" \"data_extension\": \"png\",\n",
" \"dataset\": \"custom\"\n",
" }\n",
" settings[\"Path\"] = {\n",
" \"save_path\": \"D:/Data/LOG_pt\",\n",
" \"data_path\": \"D:/Data/isbi_patch64\",\n",
" \"pims_path\": \"D:/Data/isbi_patch64/pims\",\n",
" \"pds_path\": \"D:/Data/isbi_patch64/pds\"\n",
" \"data_path\": \"D:/Data/cremi_patch64\",\n",
" \"pims_path\": \"D:/Data/cremi_patch64/pims\",\n",
" \"pds_path\": \"D:/Data/cremi_patch64/pds\"\n",
" }\n",
" settings[\"Monitor\"] = {\n",
" \"print_step\": 100,\n",
Expand Down
154 changes: 147 additions & 7 deletions Examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,32 @@
"adv_params = return_advanced_params()\n",
"et = Edges_(adv_params, False)\n",
"\n",
"img_bat = Utility_general.read_image_subset(\"D:/Data/cremi_exp/cremi_gen\", \"png\", 1.0, shuffle=False)\n",
"# =========== Targeted topo structure is Dim 1\n",
"img_bat = Utility_general.read_image_subset(\"D:/Data/facades/test\", \"png\", 1.0, shuffle=False)\n",
"img_bat = Utility_general.normalize_data_(img_bat, 127.5)\n",
"img_bat = np.asarray(img_bat)\n",
"et.load_pd_pool(\"D:/Data/cremi_patch64/pds\", \"dat\", 1.0, 101, 0)\n",
"et.load_pd_pool(\"D:/Data/facades/facade128/pds\", \"dat\", 1.0, 32)\n",
"gen_res, mean_wasdis = et.fix_with_topo(img_bat, 1, -1.0, 1.0, blind=True)\n",
"\n",
"idx = 0\n",
"# =========== Targeted topo structure is Dim 0\n",
"# img_bat = Utility_general.read_image_subset(\"D:/Data/Retina/t\", \"png\", 1.0, shuffle=False)\n",
"# img_bat = Utility_general.normalize_data_(img_bat, 127.5)\n",
"# img_bat = np.asarray(img_bat)\n",
"# et.load_pd_pool(\"D:/Data/Retina/retina128/pds\", \"dat\", 1.0, 4)\n",
"# gen_res, mean_wasdis = et.fix_with_topo_test(img_bat, 0, -1.0, 1.0, blind=True)\n",
"\n",
"# idx = 0\n",
"# Viewer.imshow_(img_bat[idx])\n",
"# Viewer.imshow_(gen_res[idx])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"idx = 2\n",
"Viewer.imshow_(img_bat[idx])\n",
"Viewer.imshow_(gen_res[idx])"
]
Expand Down Expand Up @@ -228,7 +247,9 @@
"while pix2pix requires same names in two folders. Unet needs to convert masks, pix2pix doesn't.\n",
"'''\n",
"\n",
"FileIO.make_mask_unet(\"D:/Data/cremi_new/compare0.0005\", \"D:/Data/cremi_new/compare0.0005_maskunet\", 'png', 'gif')\n",
"FileIO.make_mask_unet(\"D:/Data/Retina/compare0.00025\", \"D:/Data/Retina/compare0.00025_maskunet\", 'png', 'gif')\n",
"#FileIO.convert_google_maps_2_binary(\"D:/Data/maps/map_original_seg\", \"jpg\", \"D:/Data/maps/map_seg\", \"png\", 252, 2, 5)\n",
"#FileIO.convert_facades_2_binary(\"D:/Data/facades/facade_original_seg\", \"jpg\", \"D:/Data/facades/facade128_maskp2p\", \"jpg\", 150, 80, 2, 2, 128, 128, 1)\n",
"\n",
"'''\n",
"===== Launch docker container:\n",
Expand All @@ -246,8 +267,9 @@
"\n",
"===== Segmentation training:\n",
"python3 train.py --images /root/shared/dataset/cremi/match0.0005_tx/ --masks /root/shared/dataset/cremi/match0.0005_maskunet/ -e 20 -b 32 -l 0.005 -s 1.0 -c 1 -n match0.0005 --gpu_ids '0'\n",
"python3 predict.py --infolder ~/shared/a --outfolder ~/shared/b -m checkpoints/match0.0005/CP_epoch19.pth -c 1 -s 1.0\n",
"python3 predict.py --infolder ~/shared/a --outfolder ~/shared/b -m checkpoints/match0.0005/CP_epoch19.pth -c 1 -s 1.0 --gpu_ids '2'\n",
"python3 pix_accuracy.py --resfolder ../b --gthfolder ../c\n",
"python3 dice_score.py --resfolder ../b --gthfolder ../c\n",
"'''\n",
"\n",
"'''\n",
Expand All @@ -267,9 +289,127 @@
"%run FileIO.ipynb\n",
"%run Archpool.ipynb\n",
"\n",
"root = 'D:/Data/isbi_patch64'\n",
"root = 'D:/Data/facades/facade128'\n",
"adv_params = return_advanced_params()\n",
"FileIO.compute_pd_save(root, root+'/pds', \"png\", adv_params, 1)"
"FileIO.compute_pd_save(root, root+'/pds', \"jpg\", adv_params, 1, 127.5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%run Topo_treatment.ipynb\n",
"%run Utility_general.ipynb\n",
"%run Utility_topo.ipynb\n",
"%run Archpool.ipynb\n",
"\n",
"def unbiased_MMD(dist, same):\n",
" nl = dist.shape[0]\n",
" nr = dist.shape[1]\n",
" if same:\n",
" assert(nl == nr)\n",
" dist_list = []\n",
" for i in range(nl):\n",
" for j in range(nl):\n",
" if i != j:\n",
" dist_list.append(dist[i,j])\n",
" sigma = np.median(dist_list)\n",
" sigma_sq = sigma * sigma\n",
" if sigma_sq == 0.0:\n",
" sigma_sq = sigma_sq + 1e-5\n",
" summation = 0.0\n",
" for i in range(nl):\n",
" for j in range(nl):\n",
" if i != j:\n",
" summation = summation + math.exp(-dist[i,j]/sigma_sq)\n",
" return summation / (nl * (nl - 1))\n",
" else:\n",
" dist_list = []\n",
" for i in range(nl):\n",
" for j in range(nr):\n",
" dist_list.append(dist[i,j])\n",
" sigma = np.median(dist_list)\n",
" sigma_sq = sigma * sigma\n",
" if sigma_sq == 0.0:\n",
" sigma_sq = sigma_sq + 1e-5\n",
" summation = 0.0\n",
" for i in range(nl):\n",
" for j in range(nr):\n",
" summation = summation + math.exp(-dist[i,j]/sigma_sq)\n",
" return summation / (nl * nr)\n",
" \n",
"def unbiased_MMD_sigma_(dist, same, sigma):\n",
" nl = dist.shape[0]\n",
" nr = dist.shape[1]\n",
" assert(sigma != 0.0)\n",
" sigma_sq = sigma * sigma\n",
" if same:\n",
" assert(nl == nr)\n",
" summation = 0.0\n",
" for i in range(nl):\n",
" for j in range(nl):\n",
" if i != j:\n",
" summation = summation + math.exp(-dist[i,j]/sigma_sq)\n",
" return summation / (nl * (nl - 1))\n",
" else:\n",
" summation = 0.0\n",
" for i in range(nl):\n",
" for j in range(nr):\n",
" summation = summation + math.exp(-dist[i,j]/sigma_sq)\n",
" return summation / (nl * nr)\n",
"\n",
"def compute_unbiased_MMD(folderA, extA, folderB, extB, dim, thresh, proj2dim, wassertein_dist):\n",
" adv_params = return_advanced_params()\n",
" et = Edges_(adv_params, False)\n",
" \n",
" img_bat = Utility_general.read_image_subset(folderA, extA, 0.5, shuffle=True)\n",
" img_bat = np.stack(img_bat)\n",
" B1, D1, pd1, _ = et.pd_batch(img_bat, dim, debug=False, old_form=False, binarize=False, disttrfm=False)\n",
" img_bat = Utility_general.read_image_subset(folderB, extB, 0.5, shuffle=True)\n",
" img_bat = np.stack(img_bat)\n",
" B2, D2, pd2, _ = et.pd_batch(img_bat, dim, debug=False, old_form=False, binarize=False, disttrfm=False)\n",
" \n",
" if thresh > 0:\n",
" B1, D1, pd1 = Utility_topo.topo_filter_retmat_mul(B1, D1, pd1, thresh)\n",
" B2, D2, pd2 = Utility_topo.topo_filter_retmat_mul(B2, D2, pd2, thresh)\n",
" if proj2dim >= 0:\n",
" pd1 = Utility_topo.extract_dim_from_list(pd1, proj2dim)\n",
" pd2 = Utility_topo.extract_dim_from_list(pd2, proj2dim)\n",
" \n",
" distAA, GAA = Utility_general.wasserstein_set_distance(pd1, pd1, wassertein_dist)\n",
" distBB, GBB = Utility_general.wasserstein_set_distance(pd2, pd2, wassertein_dist)\n",
" distAB, GAB = Utility_general.wasserstein_set_distance(pd1, pd2, wassertein_dist)\n",
" \n",
" dist_list = []\n",
" for i in range(distAA.shape[0]):\n",
" for j in range(distAA.shape[0]):\n",
" if i != j:\n",
" dist_list.append(distAA[i,j])\n",
" for i in range(distBB.shape[0]):\n",
" for j in range(distBB.shape[0]):\n",
" if i != j:\n",
" dist_list.append(distBB[i,j])\n",
" for i in range(distAB.shape[0]):\n",
" for j in range(distAB.shape[1]):\n",
" dist_list.append(distAB[i,j])\n",
" sigma = np.median(dist_list)\n",
" print(sigma)\n",
" \n",
"# sAA = unbiased_MMD(distAA, True)\n",
"# sBB = unbiased_MMD(distBB, True)\n",
"# sAB = unbiased_MMD(distAB, False)\n",
" \n",
" sAA = unbiased_MMD_sigma_(distAA, True, sigma)\n",
" sBB = unbiased_MMD_sigma_(distBB, True, sigma)\n",
" sAB = unbiased_MMD_sigma_(distAB, False, sigma)\n",
" \n",
" return sAA + sBB - 2*sAB\n",
" \n",
"compute_unbiased_MMD(\"D:/Data/facades/facade128\", \"jpg\", \"D:/Data/facades/compare0.0005\", \"png\", 1, 1.0, 0, 1.0)"
]
},
{
Expand Down
Loading

0 comments on commit d068b98

Please sign in to comment.