Skip to content

Instantly share code, notes, and snippets.

@jeanpat
Last active January 31, 2020 12:09
Show Gist options
  • Select an option

  • Save jeanpat/2b95e3ca23a983316dc0d3fa4c064c50 to your computer and use it in GitHub Desktop.

Select an option

Save jeanpat/2b95e3ca23a983316dc0d3fa4c064c50 to your computer and use it in GitHub Desktop.
This jupyter notebook can be used to clean the groundtruth images from the following dataset: https://github.com/jeanpat/DeepFISH/blob/master/dataset/LowRes_13434_overlapping_pairs.h5
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"!pip install git+git://github.com/waspinator/[email protected]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"import numpy as np\n",
"import os\n",
"#import skimage as sk\n",
"#print sk.__version__\n",
"#from skimage import io\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from pycocotools import mask\n",
"from skimage import measure"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#from skimage import filters\n",
"#from skimage import feature\n",
"#from skimage import io\n",
"#from scipy import ndimage as nd\n",
"#from scipy import misc"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def load_dataset(path):\n",
" if os.path.exists(path):\n",
" print('found ' + path)\n",
" h5f = h5py.File(path,'r')\n",
" #h5f = h5py.File('../dataset/overlapping_subset_pairs.h5','r')\n",
" pairs = h5f['dataset_1'][:]\n",
" h5f.close()\n",
" return pairs\n",
" else:\n",
" print(path +' not found')\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"total 116560\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 48208 janv. 1 21:12 'Clean Dataset from their spurious pixels.ipynb'\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 12514746 janv. 1 19:38 Cleaned_FullRes_2854_overlapping_pairs.h5\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 19019811 janv. 1 19:35 Cleaned_FullRes_2854_overlapping_pairs.npz\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 0 déc. 20 21:46 Cleaned_LowRes_13434_overlapping_pairs.h5\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 8911800 déc. 20 16:23 Cleaned_LowRes_13434_overlapping_pairs.tar.xz\r\n",
"lrwxrwxrwx 1 jeanpat jeanpat 51 janv. 15 17:20 dataset -> '/home/jeanpat/Developpement/DeepFISH master/dataset'\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 15409078 janv. 15 18:14 Detectron2_Tutorial.ipynb\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 209222 juil. 11 2018 input.jpg\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 14283 déc. 30 19:20 LoadingDataset.ipynb\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 24203163 déc. 20 16:48 LowRes_13434_overlapping_pairs.h5\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 66386 déc. 20 17:57 LowRes_13434_overlapping_pairs.h5.1\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 23797936 déc. 20 14:06 old_Cleaned_LowRes_13434_overlapping_pairs.h5\r\n",
"-rw-r--r-- 1 jeanpat jeanpat 8911800 déc. 20 13:54 ori_Cleaned_LowRes_13434_overlapping_pairs.tar.xz\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 5862281 déc. 30 18:53 overlapping_chromosomes_examples.h5\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 103203 déc. 20 10:38 plot_compact_watershed.ipynb\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 20357 déc. 20 16:44 Register_ChromDataSet.ipynb\r\n",
"-rw-rw-r-- 1 jeanpat jeanpat 230105 déc. 20 16:54 Tinkering_with_OverlapSegmentationNet-01.ipynb\r\n",
"drwxrwxr-x 2 jeanpat jeanpat 4096 janv. 16 17:37 To_COCO\r\n"
]
}
],
"source": [
"!ls -l"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## load some datasets in hdf5 format"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"fullres_data_path = './dataset/overlapping_chromosomes_examples.h5'\n",
"#fullres_data_path = './overlapping_chromosomes_examples.h5'\n",
"\n",
"lowres_data_path = './dataset/lowres_82146_overlapping_pairs_grey_DAPI-GroundTruth.h5'\n",
"midres_data_path = './LowRes_13434_overlapping_pairs.h5'\n",
"cleaned_midres_data_path = './Cleaned_LowRes_13434_overlapping_pairs.h5'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"found ./dataset/overlapping_chromosomes_examples.h5\n",
"(2853, 190, 189, 2)\n"
]
}
],
"source": [
"pairs = load_dataset(fullres_data_path)\n",
"print(pairs.shape)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2853, 190, 189, 2)\n",
"int8\n",
"95\n"
]
}
],
"source": [
"print(pairs.shape)\n",
"print(pairs[0,:,:,1].dtype)\n",
"print(pairs[0,:,:,0].max())"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1427, 190, 189, 2)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"subset = pairs[::2,:,:,:]\n",
"subset.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Display some pair"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7f00cdb092e8>"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAACqCAYAAACTZZUqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAVuUlEQVR4nO3dS2/bVtcF4EVSFGVZsiLDuRRG8AHNG3RcFCj64zvovA1QdNZB21ya2nGc0JJpSRRv38Ddx5tHlCKljo4v6wEKx7aurr20tbnPoVdVFYiIaPt81w+AiOi+YgATETnCACYicoQBTETkCAOYiMiR1iYX9jyPIxP0RVVV5W37Pvl7TV/ast/rjQIYAFqtja9CtJY8z10/BKKtYguCiMgRBjARkSMMYCIiRxjARESOMICJiBxhABMROcIAJiJyhAFMROQIA5iIyBEGMBGRIwxgIiJHGMBERI4wgImIHGEAExE5wgAmInKEAUxE5AgDmIjIEQYwEZEjDGAiIkcYwEREjjCAiYgcYQATETnCACYicoQBTETkCAOYiMgRBjARkSMMYCIiRxjARESOMICJiBxhABMROcIAJiJyhAFMROQIA5iIyBEGMBGRIwxgIiJHGMBERI4wgImIHGEAExE5wgAmInKEAUxE5AgDmIjIEQYwEZEjDGAiIkcYwEREjjCAiYgcYQATETnCACYicoQBvAWdTqf2kehuqKyPtCmvqtb/4XmeV7VarS/4cG4vCVffX3xNC4IAAFAUhfnaZDLZzgO7RfI8R1VV3rbv1/M8JshSm/5otv6/71ZY9nvNNL0G/X7fBK/v+yjLciGIJYSByyAOggBZlmE2m231sRKt73Nel+Q6DOJ1MIA/U7fbBQCEYYgoihCGIYDFalc+D8MQnudB3nHMZjOUZYnZbIbpdMogphviut4MMIjXwQD+D3T4hmFowtbzLn/pqqqCtGyCIDChbI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zx/9UUeCRHwf47u9xQAf6/pS1n6e+1VVbXNB0JERP9iC4KIyBEGMBGRIwxgIiJHGMBERI4wgImIHGEAExE5wgAmInKEAUxE5AgDmIjIkf8HZVk6jGdcTuEAAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"N = 150\n",
"grey = pairs[N,:,:,0]\n",
"grd_truth = pairs[N,:,:,1]\n",
"\n",
"plt.subplot(121,xticks=[],yticks=[])\n",
"plt.imshow(grey, cmap=plt.cm.gray)\n",
"plt.subplot(122,xticks=[],yticks=[])\n",
"plt.imshow(grd_truth.astype(np.uint8), cmap=plt.cm.flag_r)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]], dtype=int8)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"INFO = {\n",
" \"description\": \"Example Dataset\",\n",
" \"url\": \"https://github.com/waspinator/pycococreator\",\n",
" \"version\": \"0.1.0\",\n",
" \"year\": 2018,\n",
" \"contributor\": \"waspinator\",\n",
" \"date_created\": datetime.datetime.utcnow().isoformat(' ')\n",
"}\n",
"\n",
"LICENSES = [\n",
" {\n",
" \"id\": 1,\n",
" \"name\": \"Attribution-NonCommercial-ShareAlike License\",\n",
" \"url\": \"http://creativecommons.org/licenses/by-nc-sa/2.0/\"\n",
" }\n",
"]\n",
"\n",
"CATEGORIES = [\n",
" {\n",
" 'id': 1,\n",
" 'name': 'square',\n",
" 'supercategory': 'shape',\n",
" },\n",
" {\n",
" 'id': 2,\n",
" 'name': 'circle',\n",
" 'supercategory': 'shape',\n",
" },\n",
" {\n",
" 'id': 3,\n",
" 'name': 'triangle',\n",
" 'supercategory': 'shape',\n",
" },\n",
"]"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"import json\n",
"import numpy as np\n",
"from pycocotools import mask\n",
"from skimage import measure\n",
"\n",
"ground_truth_binary_mask = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],\n",
" [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)\n",
"\n",
"fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)\n",
"encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)\n",
"ground_truth_area = mask.area(encoded_ground_truth)\n",
"ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)\n",
"contours = measure.find_contours(ground_truth_binary_mask, 0.5)\n",
"\n",
"annotation = {\n",
" \"segmentation\": [],\n",
" \"area\": ground_truth_area.tolist(),\n",
" \"iscrowd\": 0,\n",
" \"image_id\": 123,\n",
" \"bbox\": ground_truth_bounding_box.tolist(),\n",
" \"category_id\": 1,\n",
" \"id\": 1\n",
" }\n",
"\n",
"for contour in contours:\n",
" contour = np.flip(contour, axis=1)\n",
" segmentation = contour.ravel().tolist()\n",
" annotation[\"segmentation\"].append(segmentation)\n",
" \n",
"print(json.dumps(annotation, indent=4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Create a directory suitable to create a COCO dataset\n",
" * first try to convert a multilevel mask into coordinates of contour points according to https://github.com/cocodataset/cocoapi/issues/131"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"grd_1 = grd_truth == 1\n",
"grd_2 = grd_truth == 2\n",
"grd_3 = grd_truth == 3\n",
"\n",
"fortran_ground_truth_binary_mask = np.asfortranarray(grd_3.astype(np.uint8))\n",
"encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)\n",
"ground_truth_area = mask.area(encoded_ground_truth)\n",
"ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)\n",
"contours = measure.find_contours(ground_truth_binary_mask, 0.5)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"segmentation\": [\n",
" [\n",
" 7.0,\n",
" 5.5,\n",
" 6.0,\n",
" 5.5,\n",
" 5.0,\n",
" 5.5,\n",
" 4.5,\n",
" 5.0,\n",
" 4.5,\n",
" 4.0,\n",
" 4.5,\n",
" 3.0,\n",
" 4.5,\n",
" 2.0,\n",
" 5.0,\n",
" 1.5,\n",
" 6.0,\n",
" 1.5,\n",
" 7.0,\n",
" 1.5,\n",
" 7.5,\n",
" 2.0,\n",
" 7.5,\n",
" 3.0,\n",
" 7.5,\n",
" 4.0,\n",
" 7.5,\n",
" 5.0,\n",
" 7.0,\n",
" 5.5\n",
" ],\n",
" [\n",
" 0.0,\n",
" 5.5,\n",
" 0.5,\n",
" 6.0,\n",
" 0.0,\n",
" 6.5\n",
" ]\n",
" ],\n",
" \"area\": 575,\n",
" \"iscrowd\": 0,\n",
" \"image_id\": 123,\n",
" \"bbox\": [\n",
" 77.0,\n",
" 92.0,\n",
" 29.0,\n",
" 28.0\n",
" ],\n",
" \"category_id\": 1,\n",
" \"id\": 1\n",
"}\n"
]
}
],
"source": [
"annotation = {\n",
" \"segmentation\": [],\n",
" \"area\": ground_truth_area.tolist(),\n",
" \"iscrowd\": 0,\n",
" \"image_id\": 123,\n",
" \"bbox\": ground_truth_bounding_box.tolist(),\n",
" \"category_id\": 1,\n",
" \"id\": 1\n",
" }\n",
"\n",
"for contour in contours:\n",
" contour = np.flip(contour, axis=1)\n",
" segmentation = contour.ravel().tolist()\n",
" annotation[\"segmentation\"].append(segmentation)\n",
" \n",
"print(json.dumps(annotation, indent=4))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['segmentation', 'area', 'iscrowd', 'image_id', 'bbox', 'category_id', 'id'])"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"annotation.keys()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[7.0,\n",
" 5.5,\n",
" 6.0,\n",
" 5.5,\n",
" 5.0,\n",
" 5.5,\n",
" 4.5,\n",
" 5.0,\n",
" 4.5,\n",
" 4.0,\n",
" 4.5,\n",
" 3.0,\n",
" 4.5,\n",
" 2.0,\n",
" 5.0,\n",
" 1.5,\n",
" 6.0,\n",
" 1.5,\n",
" 7.0,\n",
" 1.5,\n",
" 7.5,\n",
" 2.0,\n",
" 7.5,\n",
" 3.0,\n",
" 7.5,\n",
" 4.0,\n",
" 7.5,\n",
" 5.0,\n",
" 7.0,\n",
" 5.5]"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"annotation['segmentation'][0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment