-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MRG] Simple version Image Pipeline Implemented (#650)
* legacy * new files * Update demo.py * test tunner bug fixed * classes and funtions in the demo created * network implemented not tested * travis to ignore legacy tests * test connectedHyperparameter * test fixed * some basic blocks implemented * still debuging * basic test for hypergraph passed * merge layer implemented * hyper_graph fully tested * more args added to auto model * test fixed * local changes * test auto_model * auto_model fit signature changed for validation data * super classes extending object * change n_ to num_ * refactored automodel to extend hypermodel and removed tuner from signature * rename HyperNode to Node. TextInput added * rename HyperGraph to GraphAutoModel extending AutoModel * refactor hyperhead, removed tensor heads * removed unnecessary blocks, rename build_output to build * changed some functions and attributes to private * remove trails from AutoModel public API * test cases changed accordingly * import modules instead of objects * tuner deleted from AutoModel contructor * change trails to num_trials * use the same quote sign * revised auto pipeline docs * removed compile from AutoModel * loss and metrics moved to hyper heads * do not flatten by default in hyperheads * inputs and outputs down to GraphAutoModel * changed AutoModel * name_scope changed to tf 2.0 and moved to hyperparameters and hypermodel * remove HierarchicalHyperParameters * renaming the tests * auto_pipeline * image module tested * image regressor tested * removed some requirements * update tf to 2.0 beta * Refactor (#646) * super classes extending object * change n_ to num_ * refactored automodel to extend hypermodel and removed tuner from signature * rename HyperNode to Node. TextInput added * rename HyperGraph to GraphAutoModel extending AutoModel * refactor hyperhead, removed tensor heads * removed unnecessary blocks, rename build_output to build * changed some functions and attributes to private * remove trails from AutoModel public API * test cases changed accordingly * import modules instead of objects * tuner deleted from AutoModel contructor * change trails to num_trials * use the same quote sign * revised auto pipeline docs * removed compile from AutoModel * loss and metrics moved to hyper heads * do not flatten by default in hyperheads * inputs and outputs down to GraphAutoModel * changed AutoModel * name_scope changed to tf 2.0 and moved to hyperparameters and hypermodel * remove HierarchicalHyperParameters * renaming some variables and make private some members * demo update * remove legacy * test fixed * changed setup.py * Update hyper_block.py Improve the DenseBlock: Add some other layers'category to it. * Update hyper_block.py * pep8 style fix * changed docstrings to use markdown * renaming some of the variables and classes * extracted check new and old search space to tuner base class * fixing some pylint issues * change the normalization to use mean and stddev only * dependency * dependency changed to keras-tuner * pep8 formatting
- Loading branch information
1 parent
1e76e7f
commit d6947d6
Showing
20 changed files
with
477 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
from autokeras.hypermodel import * | ||
from autokeras.auto.auto_model import AutoModel | ||
from autokeras.auto.preprocessor import image_augment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import kerastuner | ||
import tensorflow as tf | ||
|
||
from autokeras import const | ||
from autokeras.auto import processor, auto_model | ||
from autokeras.hypermodel import hyper_block, hyper_node | ||
from autokeras.hypermodel import hyper_head | ||
|
||
|
||
class SupervisedImagePipeline(auto_model.AutoModel): | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.image_block = hyper_block.ImageBlock() | ||
self.head = None | ||
self.normalizer = processor.Normalizer() | ||
|
||
def fit(self, x=None, y=None, trials=None, **kwargs): | ||
self.normalizer.fit(x) | ||
self.inputs = [hyper_node.ImageInput()] | ||
super().fit(x=self.normalizer.transform(x), y=y, **kwargs) | ||
|
||
def build(self, hp): | ||
input_node = self.inputs[0].build(hp) | ||
output_node = self.image_block.build(hp, input_node) | ||
output_node = self.head.build(hp, output_node) | ||
model = tf.keras.Model(input_node, output_node) | ||
optimizer = hp.Choice('optimizer', | ||
['adam', | ||
'adadelta', | ||
'sgd']) | ||
|
||
model.compile(optimizer=optimizer, | ||
loss=self.head.loss, | ||
metrics=self.head.metrics) | ||
|
||
return model | ||
|
||
|
||
class ImageClassifier(SupervisedImagePipeline): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.head = hyper_head.ClassificationHead() | ||
self.label_encoder = processor.OneHotEncoder() | ||
|
||
def fit(self, x=None, y=None, **kwargs): | ||
self.label_encoder.fit(y) | ||
self.head.output_shape = (self.label_encoder.num_classes,) | ||
super().fit(x=x, y=self.label_encoder.transform(y), **kwargs) | ||
|
||
def predict(self, x, **kwargs): | ||
return self.label_encoder.inverse_transform(super().predict(x, **kwargs)) | ||
|
||
|
||
class ImageRegressor(SupervisedImagePipeline): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.head = hyper_head.RegressionHead() | ||
|
||
def fit(self, x=None, y=None, **kwargs): | ||
self.head.output_shape = (1,) | ||
super().fit(x=x, y=y, **kwargs) | ||
|
||
def predict(self, x, **kwargs): | ||
return super().predict(x, **kwargs).flatten() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import numpy as np | ||
|
||
|
||
class OneHotEncoder(object): | ||
"""A class that can format data. | ||
This class provides ways to transform data's classification label into | ||
vector. | ||
# Attributes | ||
data: The input data | ||
num_classes: The number of classes in the classification problem. | ||
labels: The number of labels. | ||
label_to_vec: Mapping from label to vector. | ||
int_to_label: Mapping from int to label. | ||
""" | ||
|
||
def __init__(self): | ||
"""Initialize a OneHotEncoder""" | ||
self.data = None | ||
self.num_classes = 0 | ||
self.labels = None | ||
self.label_to_vec = {} | ||
self.int_to_label = {} | ||
|
||
def fit(self, data): | ||
"""Create mapping from label to vector, and vector to label.""" | ||
data = np.array(data).flatten() | ||
self.labels = set(data) | ||
self.num_classes = len(self.labels) | ||
for index, label in enumerate(self.labels): | ||
vec = np.array([0] * self.num_classes) | ||
vec[index] = 1 | ||
self.label_to_vec[label] = vec | ||
self.int_to_label[index] = label | ||
|
||
def transform(self, data): | ||
"""Get vector for every element in the data array.""" | ||
data = np.array(data) | ||
if len(data.shape) > 1: | ||
data = data.flatten() | ||
return np.array(list(map(lambda x: self.label_to_vec[x], data))) | ||
|
||
def inverse_transform(self, data): | ||
"""Get label for every element in data.""" | ||
return np.array(list(map(lambda x: self.int_to_label[x], | ||
np.argmax(np.array(data), axis=1)))) | ||
|
||
|
||
class Normalizer(object): | ||
""" Perform basic image transformation and augmentation. | ||
# Attributes | ||
max_val: the maximum value of all data. | ||
mean: the mean value. | ||
std: the standard deviation. | ||
""" | ||
|
||
def __init__(self): | ||
self.mean = None | ||
self.std = None | ||
|
||
def fit(self, data): | ||
self.mean = np.mean(data, axis=(0, 1, 2), keepdims=True).flatten() | ||
self.std = np.std(data, axis=(0, 1, 2), keepdims=True).flatten() | ||
|
||
def transform(self, data): | ||
""" Transform the test data, perform normalization. | ||
# Arguments | ||
data: Numpy array. The data to be transformed. | ||
# Returns | ||
A DataLoader instance. | ||
""" | ||
# channel-wise normalize the image | ||
data = (data - self.mean) / self.std | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
from autokeras.hypermodel.hypermodel import HyperModel | ||
# from autokeras.hypermodel.resnet_block import * | ||
Oops, something went wrong.