Skip to content

Commit

Permalink
Final merge before the first release
Browse files Browse the repository at this point in the history
Final merge before the first release
  • Loading branch information
swapsha96 authored May 15, 2019
2 parents f6559ca + 27dd4e2 commit bed8e6c
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 112 deletions.
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ MiraPy can be used for problem solving using ML techniques and will continue to
- Astronomical Image Reconstruction using Autoencoder
- Classification of the first catalog of variable stars by ATLAS
- HTRU1 Pulsar Dataset Image Classification using Convolutional Neural Network
- Curve Fitting using Autograd (incomplete implementation)
- Variable star Classification using Recurrent Neural Network (RNN)
- 2D visualization of feature sets using Principal Component Analysis (PCA)
- Curve Fitting using Autograd (basic implementation)

There are more projects that we will add soon and some of them are as following:

Expand Down
95 changes: 87 additions & 8 deletions mirapy/autoencoder/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from keras.models import Model
import os
from keras.models import Model, load_model
from keras.layers import *
import matplotlib.pyplot as plt


class Autoencoder:
def __init__(self):
"""
Base Class for autoencoder models.
"""
self.model = None
self.history = None
self.dim = None
Expand All @@ -13,19 +17,41 @@ def __init__(self):
self.decoded = None

def compile(self, optimizer, loss):
"""
Compile model with given configuration.
:param optimizer: Instance of optimizer.
:param loss: String (name of loss function) or custom function.
"""
pass

def train(self, x, y, batch_size=32, epochs=100, validation_data=None,
shuffle=True, verbose=1):
"""
Trains the model on the training data with given settings.
:param x: Numpy array of training data.
:param y: Numpy array of target data.
:param epochs: Integer. Number of epochs during training.
:param batch_size: Number of samples per gradient update.
:param validation_data: Numpy array of validation data.
:param shuffle: Boolean. Shuffles the data before training.
:param verbose: Value is 0, 1, or 2.
"""
pass

def predict(self, x):
pass
"""
Predicts the output of the model for the given data as input.
def evaluate(self):
:param x: Input data as Numpy arrays.
"""
pass

def plot_history(self):
"""
Plots loss vs epoch graph.
"""
plt.plot(self.history.history['loss'])
if 'val_loss' in self.history.history.keys():
plt.plot(self.history.history['val_loss'])
Expand All @@ -35,18 +61,42 @@ def plot_history(self):
plt.legend(['Train', 'Test'], loc='upper right')
plt.show()

def load_model(self, path, model_name):
pass

def save_model(self, path, model_name):
pass
def save_model(self, model_name, path='models/'):
"""
Saves a model into a H5py file.
:param model_name: File name.
:param path: Pa
"""
path += model_name
self.model.save(path)

def load_model(self, model_name, path='models/'):
"""
Loads a model from a H5py file.
:param model_name: File name.
:param path: Pa
"""
path += model_name
if os.path.exists(path):
self.model = load_model(path)
else:
raise FileNotFoundError("Model does not exists")

def summary(self):
pass


class DeNoisingAutoencoder(Autoencoder):
def __init__(self, img_dim, activation='relu', padding='same'):
"""
De-noising Autoencoder used for the astronomical image reconstruction.
:param img_dim: Set. Dimension of input and output image.
:param activation: String (activation function name).
:param padding: String (type of padding in convolution layers).
"""
self.dim = img_dim
self.input_img = Input(shape=(*img_dim, 1))

Expand All @@ -70,11 +120,28 @@ def __init__(self, img_dim, activation='relu', padding='same'):
padding=padding)(x)

def compile(self, optimizer, loss):
"""
Compile model with given configuration.
:param optimizer: Instance of optimizer.
:param loss: String (name of loss function) or custom function.
"""
self.model = Model(self.input_img, self.decoded)
self.model.compile(optimizer=optimizer, loss=loss)

def train(self, x, y, batch_size=32, epochs=100, validation_data=None,
shuffle=True, verbose=1):
"""
Trains the model on the training data with given settings.
:param x: Numpy array of training data.
:param y: Numpy array of target data.
:param epochs: Integer. Number of epochs during training.
:param batch_size: Number of samples per gradient update.
:param validation_data: Numpy array of validation data.
:param shuffle: Boolean. Shuffles the data before training.
:param verbose: Value is 0, 1, or 2.
"""
self.history = self.model.fit(x, y,
epochs=epochs,
batch_size=batch_size,
Expand All @@ -83,9 +150,21 @@ def train(self, x, y, batch_size=32, epochs=100, validation_data=None,
verbose=verbose)

def predict(self, x):
"""
Predicts the output of the model for the given data as input.
:param x: Input data as Numpy arrays.
"""
return self.model.predict(x)

def show_image_pairs(self, original_images, decoded_images, max_images):
"""
Displays images in pair of images in grid form using Matplotlib.
:param original_images: Array of original images.
:param decoded_images: Array of decoded images.
:param max_images: Integer. Set number of images in a row.
"""
n = min(max_images, len(decoded_images))

plt.figure(figsize=(20, 8))
Expand Down
Loading

0 comments on commit bed8e6c

Please sign in to comment.