Browse Source

First functions added

Jerome Buisine 2 years ago
parent
commit
d338201ddc
9 changed files with 191 additions and 3 deletions
  1. 1 0
      .python-version
  2. 1 0
      MANIFEST.in
  3. 0 3
      README.md
  4. 27 0
      README.rst
  5. 0 0
      ipfml/__init__.py
  6. 32 0
      ipfml/image_processing.py
  7. 42 0
      ipfml/metrics.py
  8. 59 0
      ipfml/ts_model_helper.py
  9. 29 0
      setup.py

+ 1 - 0
.python-version

@@ -0,0 +1 @@
+thesis-venv

+ 1 - 0
MANIFEST.in

@@ -0,0 +1 @@
+include README.rst

+ 0 - 3
README.md

@@ -1,3 +0,0 @@
-# ThesisModules
-
-Project which contains all usefull function used into others thesis project

+ 27 - 0
README.rst

@@ -0,0 +1,27 @@
+# IPFML
+
+Image Processing For Machine Learning package.
+
+## Modules
+
+This project contains modules.
+
+### **img_processing** : *PIL image processing part*
+    - fig2data(fig): *Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it*
+    - fig2img(fig): *Convert a Matplotlib figure to a PIL Image in RGB format and return it*
+
+### **metrics** : *Metrics computation of PIL image*
+    - get_SVD(image): *Transforms PIL Image into SVD*
+    - get_SVD_s(image): *Transforms PIL Image into SVD and returns only 's' part*
+    - get_SVD_U(image): *Transforms PIL Image into SVD and returns only 'U' part*
+    - get_SVD_V(image): *Transforms PIL Image into SVD and returns only 'V' part*
+
+### **ts_model_helper** : *contains helpful function to save or display model information and performance of tensorflow model*
+    - save(history, filename): *Function which saves data from neural network model*
+    - show(history, filename): *Function which shows data from neural network model*
+
+All these modules will be enhanced during development of the project
+
+## How to contribute
+
+This git project uses [git-flow](https://danielkummer.github.io/git-flow-cheatsheet/) implementation. You are free to contribute to it.

+ 0 - 0
ipfml/__init__.py


+ 32 - 0
ipfml/image_processing.py

@@ -0,0 +1,32 @@
+from PIL import Image
+
+import numpy as np
+
+def fig2data(fig):
+    """
+    @brief Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it
+    @param fig a matplotlib figure
+    @return a numpy 3D array of RGB values
+    """
+    # draw the renderer
+    fig.canvas.draw()
+ 
+    # Get the RGBA buffer from the figure
+    w,h = fig.canvas.get_width_height()
+    buf = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
+    buf.shape = (w, h, 3)
+ 
+    # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
+    buf = np.roll(buf, 3, axis=2)
+    return buf
+    
+def fig2img(fig):
+    """
+    @brief Convert a Matplotlib figure to a PIL Image in RGB format and return it
+    @param fig a matplotlib figure
+    @return a Python Imaging Library (PIL) image : default size (480,640,3)
+    """
+    # put the figure pixmap into a numpy array
+    buf = fig2data(fig)
+    w, h, d = buf.shape
+    return Image.frombytes("RGB", (w, h), buf.tostring())

+ 42 - 0
ipfml/metrics.py

@@ -0,0 +1,42 @@
+# module file which contains all image metrics used in project
+
+from numpy.linalg import svd
+from scipy import misc
+
+import numpy as np
+from sklearn import preprocessing
+
+def get_SVD(image):
+    """
+    @brief Transforms PIL Image into SVD
+    @param image to convert
+    @return U, s, V image decomposition
+    """
+    return svd(image, full_matrices=False)
+
+def get_SVD_s(image):
+    """
+    @brief Transforms PIL Image into SVD and returns only 's' part
+    @param image to convert
+    @return s
+    """
+    U, s, V = svd(image, full_matrices=False)
+    return s
+
+def get_SVD_U(image):
+    """
+    @brief Transforms PIL Image into SVD and returns only 'U' part
+    @param image to convert
+    @return U
+    """
+    U, s, V = svd(image, full_matrices=False)
+    return U
+
+def get_SVD_V(image):
+    """
+    @brief Transforms PIL Image into SVD and returns only 'V' part
+    @param image to convert
+    @return V
+    """
+    U, s, V = svd(image, full_matrices=False)
+    return V

+ 59 - 0
ipfml/ts_model_helper.py

@@ -0,0 +1,59 @@
+# module filewhich contains helpful display function
+
+# avoid tk issue
+import matplotlib
+#matplotlib.use('agg')
+import matplotlib.pyplot as plt
+
+def save(history, filename):
+    '''
+    @brief Function which saves data from neural network model
+    @param history : tensorflow model history
+    @param filename : information about model filename
+    @return nothing
+    '''
+    # summarize history for accuracy
+    plt.plot(history.history['acc'])
+    plt.plot(history.history['val_acc'])
+    plt.title('model accuracy')
+    plt.ylabel('accuracy')
+    plt.xlabel('epoch')
+    plt.legend(['train', 'test'], loc='upper left')
+    plt.savefig(str('%s_accuracy.png' % filename))
+
+    # clear plt history
+    plt.gcf().clear()
+
+    # summarize history for loss
+    plt.plot(history.history['loss'])
+    plt.plot(history.history['val_loss'])
+    plt.title('model loss')
+    plt.ylabel('loss')
+    plt.xlabel('epoch')
+    plt.legend(['train', 'test'], loc='upper left')
+    plt.savefig(str('%s_loss.png' % filename))
+
+def show(history, filename):
+    '''
+    @brief Function which shows data from neural network model
+    @param history : tensorflow model history
+    @param filename : information about model filename
+    @return nothing
+    '''
+    # summarize history for accuracy
+    plt.plot(history.history['acc'])
+    plt.plot(history.history['val_acc'])
+    plt.title('model accuracy')
+    plt.ylabel('accuracy')
+    plt.xlabel('epoch')
+    plt.legend(['train', 'test'], loc='upper left')
+    plt.show()
+
+    # summarize history for loss
+    plt.plot(history.history['loss'])
+    plt.plot(history.history['val_loss'])
+    plt.title('model loss')
+    plt.ylabel('loss')
+    plt.xlabel('epoch')
+    plt.legend(['train', 'test'], loc='upper left')
+    plt.show()

+ 29 - 0
setup.py

@@ -0,0 +1,29 @@
+from setuptools import setup
+
+def readme():
+    with open('README.rst') as f:
+        return f.read()
+
+setup(name='IPFML',
+      version='0.0.1',
+      description='Image Processing For Machine Learning',
+      long_description=readme(),
+      classifiers=[
+        'Development Status :: 3 - Alpha',
+        'License :: OSI Approved :: MIT License',
+        'Programming Language :: Python :: 3.6',
+        'Topic :: Image Processing :: Machine Learning',
+      ],
+      url='https://gogs.univ-littoral.fr/jerome.buisine/IPFML',
+      author='Jérôme BUISINE',
+      author_email='jerome.buisine@univ-littoral.fr',
+      license='MIT',
+      packages=['ipfml'],
+      install_requires=[
+          'matplotlib',
+          'numpy',
+          'Pillow',
+          'sklearn',
+          'scipy'
+      ],
+      zip_safe=False)