Skip to content

Commit bed8e6c

Browse files
authored
Final merge before the first release
Final merge before the first release
2 parents f6559ca + 27dd4e2 commit bed8e6c

File tree

11 files changed

+499
-112
lines changed

11 files changed

+499
-112
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ MiraPy can be used for problem solving using ML techniques and will continue to
3232
- Astronomical Image Reconstruction using Autoencoder
3333
- Classification of the first catalog of variable stars by ATLAS
3434
- HTRU1 Pulsar Dataset Image Classification using Convolutional Neural Network
35-
- Curve Fitting using Autograd (incomplete implementation)
35+
- Variable star Classification using Recurrent Neural Network (RNN)
36+
- 2D visualization of feature sets using Principal Component Analysis (PCA)
37+
- Curve Fitting using Autograd (basic implementation)
3638

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

mirapy/autoencoder/models.py

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from keras.models import Model
1+
import os
2+
from keras.models import Model, load_model
23
from keras.layers import *
34
import matplotlib.pyplot as plt
45

56

67
class Autoencoder:
78
def __init__(self):
9+
"""
10+
Base Class for autoencoder models.
11+
"""
812
self.model = None
913
self.history = None
1014
self.dim = None
@@ -13,19 +17,41 @@ def __init__(self):
1317
self.decoded = None
1418

1519
def compile(self, optimizer, loss):
20+
"""
21+
Compile model with given configuration.
22+
23+
:param optimizer: Instance of optimizer.
24+
:param loss: String (name of loss function) or custom function.
25+
"""
1626
pass
1727

1828
def train(self, x, y, batch_size=32, epochs=100, validation_data=None,
1929
shuffle=True, verbose=1):
30+
"""
31+
Trains the model on the training data with given settings.
32+
33+
:param x: Numpy array of training data.
34+
:param y: Numpy array of target data.
35+
:param epochs: Integer. Number of epochs during training.
36+
:param batch_size: Number of samples per gradient update.
37+
:param validation_data: Numpy array of validation data.
38+
:param shuffle: Boolean. Shuffles the data before training.
39+
:param verbose: Value is 0, 1, or 2.
40+
"""
2041
pass
2142

2243
def predict(self, x):
23-
pass
44+
"""
45+
Predicts the output of the model for the given data as input.
2446
25-
def evaluate(self):
47+
:param x: Input data as Numpy arrays.
48+
"""
2649
pass
2750

2851
def plot_history(self):
52+
"""
53+
Plots loss vs epoch graph.
54+
"""
2955
plt.plot(self.history.history['loss'])
3056
if 'val_loss' in self.history.history.keys():
3157
plt.plot(self.history.history['val_loss'])
@@ -35,18 +61,42 @@ def plot_history(self):
3561
plt.legend(['Train', 'Test'], loc='upper right')
3662
plt.show()
3763

38-
def load_model(self, path, model_name):
39-
pass
40-
41-
def save_model(self, path, model_name):
42-
pass
64+
def save_model(self, model_name, path='models/'):
65+
"""
66+
Saves a model into a H5py file.
67+
68+
:param model_name: File name.
69+
:param path: Pa
70+
"""
71+
path += model_name
72+
self.model.save(path)
73+
74+
def load_model(self, model_name, path='models/'):
75+
"""
76+
Loads a model from a H5py file.
77+
78+
:param model_name: File name.
79+
:param path: Pa
80+
"""
81+
path += model_name
82+
if os.path.exists(path):
83+
self.model = load_model(path)
84+
else:
85+
raise FileNotFoundError("Model does not exists")
4386

4487
def summary(self):
4588
pass
4689

4790

4891
class DeNoisingAutoencoder(Autoencoder):
4992
def __init__(self, img_dim, activation='relu', padding='same'):
93+
"""
94+
De-noising Autoencoder used for the astronomical image reconstruction.
95+
96+
:param img_dim: Set. Dimension of input and output image.
97+
:param activation: String (activation function name).
98+
:param padding: String (type of padding in convolution layers).
99+
"""
50100
self.dim = img_dim
51101
self.input_img = Input(shape=(*img_dim, 1))
52102

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

72122
def compile(self, optimizer, loss):
123+
"""
124+
Compile model with given configuration.
125+
126+
:param optimizer: Instance of optimizer.
127+
:param loss: String (name of loss function) or custom function.
128+
"""
73129
self.model = Model(self.input_img, self.decoded)
74130
self.model.compile(optimizer=optimizer, loss=loss)
75131

76132
def train(self, x, y, batch_size=32, epochs=100, validation_data=None,
77133
shuffle=True, verbose=1):
134+
"""
135+
Trains the model on the training data with given settings.
136+
137+
:param x: Numpy array of training data.
138+
:param y: Numpy array of target data.
139+
:param epochs: Integer. Number of epochs during training.
140+
:param batch_size: Number of samples per gradient update.
141+
:param validation_data: Numpy array of validation data.
142+
:param shuffle: Boolean. Shuffles the data before training.
143+
:param verbose: Value is 0, 1, or 2.
144+
"""
78145
self.history = self.model.fit(x, y,
79146
epochs=epochs,
80147
batch_size=batch_size,
@@ -83,9 +150,21 @@ def train(self, x, y, batch_size=32, epochs=100, validation_data=None,
83150
verbose=verbose)
84151

85152
def predict(self, x):
153+
"""
154+
Predicts the output of the model for the given data as input.
155+
156+
:param x: Input data as Numpy arrays.
157+
"""
86158
return self.model.predict(x)
87159

88160
def show_image_pairs(self, original_images, decoded_images, max_images):
161+
"""
162+
Displays images in pair of images in grid form using Matplotlib.
163+
164+
:param original_images: Array of original images.
165+
:param decoded_images: Array of decoded images.
166+
:param max_images: Integer. Set number of images in a row.
167+
"""
89168
n = min(max_images, len(decoded_images))
90169

91170
plt.figure(figsize=(20, 8))

0 commit comments

Comments
 (0)