Permalink
Browse files

add code to segment image pixel by pixel

  • Loading branch information...
sdpython committed Dec 25, 2017
1 parent 26ab5c8 commit 89d84e0d063a4e280b668d551819690095e569fb

Large diffs are not rendered by default.

Oops, something went wrong.
Binary file not shown.
@@ -67,4 +67,6 @@
latex_elements['preamble'] += custom_preamble
mathdef_link_only = True
epkg_dictionary['deep learning'] = 'https://en.wikipedia.org/wiki/Deep_learning'
epkg_dictionary['fcn'] = 'https://github.com/wkentaro/fcn'
epkg_dictionary['scratch'] = "https://scratch.mit.edu/"
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,74 @@
#-*- coding: utf-8 -*-
"""
@brief test log(time=14s)
"""
import sys
import os
import unittest
import skimage
try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src
try:
import pyquickhelper as skip_
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper as skip_
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.ai import DLImageSegmentation
class TestSegImage(ExtTestCase):
def test_seg_image1(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_seg_image1")
img1 = os.path.join(temp, "..", "data", "Mona_elephant.jpg")
img2 = os.path.join(temp, "..", "data", "Tesla_circa_1890c.jpg")
imgs = [img1, img2]
dl = DLImageSegmentation(fLOG=fLOG)
for i, img in enumerate(imgs):
res = dl.predict(img)
viz = dl.plot(img, res)
out_file = os.path.join(temp, "out_img%d.png" % i)
skimage.io.imsave(out_file, viz)
self.assertExists(out_file)
skimage.io.imshow(viz)
skimage.io.show()
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,76 @@
#-*- coding: utf-8 -*-
"""
@brief test log(time=15s)
"""
import sys
import os
import unittest
import shutil
try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src
try:
import pyquickhelper as skip_
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper as skip_
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, add_missing_development_version
from pyquickhelper.ipythonhelper import execute_notebook_list_finalize_ut
from src.code_beatrix.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook
import src.code_beatrix
class TestNotebookAi(unittest.TestCase):
def setUp(self):
add_missing_development_version(["pymyinstall", "pyensae", "pymmails", "ensae_projects",
"jyquickhelper"], __file__, hide=True)
def test_notebook_ai(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_ai")
keepnote = ls_notebooks("ai")
self.assertTrue(len(keepnote) > 0)
source = os.path.join(os.path.dirname(keepnote[0]), "images")
images = os.path.join(temp, 'images')
os.mkdir(images)
for img in os.listdir(source):
shutil.copy(os.path.join(source, img), images)
res = execute_notebooks(temp, keepnote, lambda i, n: True, fLOG=fLOG,
clean_function=clean_function_notebook)
execute_notebook_list_finalize_ut(
res, fLOG=fLOG, dump=src.code_beatrix)
if __name__ == "__main__":
unittest.main()
View
@@ -33,10 +33,11 @@ install:
- "%PYTHON%\\Scripts\\pip install pyrsslocal"
- "%PYTHON%\\Scripts\\pip install ensae_projects"
- "%PYTHON%\\Scripts\\pip install pygame"
- "%PYTHON%\\Scripts\\pymy_install3 opencv_python"
- "%PYTHON%\\Scripts\\pymy_install3 yaml pyproj"
- "%PYTHON%\\Scripts\\pymy_install3 opencv_python cupy"
- "%PYTHON%\\Scripts\\pymy_install3 yaml pyproj skimage"
- "%PYTHON%\\Scripts\\pip install tutormagic"
- "%PYTHON%\\Scripts\\pip install pycodestyle"
- "%PYTHON%\\Scripts\\pip install chainer gdown"
build: off
View
@@ -2,9 +2,11 @@ antlr4-python3-runtime
autopep8
babel!=2.0
blockdiag
chainer
coverage
ensae_projects
folium
gdown
hachibee_sphinx_theme
jyquickhelper
mpld3
View
@@ -18,4 +18,5 @@ scikit-learn
scipy
seaborn
setuptools
skimage
Sphinx
@@ -0,0 +1,7 @@
"""
@file
@brief Shortcuts to ai. This folder contains some
functions which use pretrained :epkg:`deep learning` model.
"""
from .image_segmentation import DLImageSegmentation
@@ -0,0 +1,61 @@
"""
@file
@brief Base class for deep learning models.
"""
import os
class DeepLearningBase:
"""
Implements a common interface to manipulate pre-trained
deep learning models.
"""
def __init__(self, model, gpu, fLOG=None):
"""
@param model model (url, filename, ...)
@param gpu use gpu
@param fLOG logging function
"""
self._gpu = gpu
if model is None:
raise ValueError("model must be specified")
if isinstance(model, str):
if not os.path.exists(model):
raise FileNotFoundError("Unable to find '{0}'".format(model))
raise NotImplementedError(
"Unable to load model '{0}'".format(model))
else:
self._model = model
self._fLOG = fLOG
def log(self, *l, **p):
"""
Log something.
"""
if self._fLOG:
self._fLOG(*l, **p)
def predict(self, X):
"""
Applies the model on features *X*.
@param X features
@return prediction
"""
raise NotImplementedError("Method predict is not implemented.")
class DeepLearningImage(DeepLearningBase):
"""
Implements a common interface to manipulate pre-trained
deep learning models processing images.
"""
def __init__(self, model, gpu=False, fLOG=None):
"""
@param model model name
@param gpu use gpu
@param fLOG logging function
"""
DeepLearningBase.__init__(self, model, gpu=gpu, fLOG=fLOG)
@@ -0,0 +1,130 @@
"""
@file
@brief Extracts objects from an image based on deep learning.
"""
from contextlib import redirect_stdout
import io
import os
import chainer
import fcn
import skimage
import numpy
from .dlbase import DeepLearningImage
class DLImageSegmentation(DeepLearningImage):
"""
Segments an image.
Inspired from
`infer.py <https://github.com/wkentaro/fcn/blob/master/examples/voc/infer.py>`_.
See notebook :ref:`imagesegmentationrst`.
"""
def __init__(self, model="FCN8s", n_class=21, gpu=False, class_name=None, fLOG=None):
"""
@param model model name
@param n_class number of classes
@param gpu use gpu
@param class_name class names
@param fLOG logging function
List of known models:
* ``'FCN8s'``: image segmentation
"""
self._fLOG = fLOG
if model == "FCN8s":
self.log(
"[DLImageSegmentation] download model '{0}'".format(model))
f = io.StringIO()
with redirect_stdout(f):
model_file = fcn.models.FCN8s.download()
self.log('[DLImageSegmentation] {0}'.format(f.getvalue()))
self._model_file = model_file
model_class = fcn.models.FCN8s
model = model_class(n_class=n_class)
self.log("[DLImageSegmentation] load_npz '{0}'".format(model_file))
chainer.serializers.load_npz(model_file, model)
else:
raise NotImplementedError(
"Unable to interpret '{0}'".format(model))
DeepLearningImage.__init__(self, model, gpu=gpu, fLOG=fLOG)
self._n_class = n_class
if class_name is None:
self._class_name = class_name = fcn.datasets.VOC2012ClassSeg.class_names
else:
self._class_name = class_name
self.log("[DLImageSegmentation] class_name '{0}'".format(class_name))
if gpu:
self.log("[DLImageSegmentation] gpu")
chainer.cuda.get_device(self._gpu).use()
model.to_gpu()
else:
self.log("[DLImageSegmentation] cpu")
@property
def ModelFile(self):
"""
Returns the model file name.
"""
return self._model_file
def _load_image(self, img, preprocess=True):
"""
Loads an image as a :epkg:`numpy:array`.
@param img image
@param preprocess transform images
@return :epkg:`numpy:array`
"""
if isinstance(img, str):
# Loads the image.
if not os.path.exists(img):
raise FileNotFoundError(img)
feat = skimage.io.imread(img)
elif isinstance(img, numpy.array):
feat = img
else:
raise NotImplementedError(
"Not implemented for type '{0}'".format(type(img)))
if preprocess:
input, = fcn.datasets.transform_lsvrc2012_vgg16((feat,))
input = input[numpy.newaxis, :, :, :]
return input
else:
return feat
def predict(self, img):
"""
Applies the model on features *X*.
@param img image
@return prediction
"""
input = self._load_image(img)
if self._gpu:
input = chainer.cuda.to_gpu(input)
with chainer.no_backprop_mode():
input = chainer.Variable(input)
with chainer.using_config('train', False):
self._model(input)
lbl_pred = chainer.functions.argmax(
self._model.score, axis=1)[0]
lbl_pred = chainer.cuda.to_cpu(lbl_pred.data)
return lbl_pred
def plot(self, img, pred):
"""
Displays the segmentation.
@param img initial image
@return new image
"""
img = self._load_image(img, preprocess=False)
viz = fcn.utils.visualize_segmentation(
lbl_pred=pred, img=img, n_class=self._n_class, label_names=self._class_name)
return viz

0 comments on commit 89d84e0

Please sign in to comment.