Deep CNN
0

Simple CNN Example

This model implements a simple deep CNN in Keras.

Training

For example, training on the CIFAR-10 dataset:

mantra train deep_cnn --dataset cifar10 --image-dim 32 32

To alter the dropout parameter:

mantra train deep_cnn --dataset cifar10 --image-dim 32 32 --dropout 0.5

Importing

To import this model to your project, run:

mantra import RJT1990/models/deep_cnn

Model
import tensorflow from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten from tensorflow.keras.layers import Conv2D, MaxPooling2D from mantraml.models import MantraModel from mantraml.models.keras.callbacks import TensorBoard, StoreTrial, EvaluateTask, ModelCheckpoint class CIFAR10CNN(MantraModel): """ This class implements a Keras Model for CIFAR """ model_name = "Deep CNN" model_image = "default.gif" model_notebook = 'notebook.ipynb' model_tags = ['cnn', 'classification'] def __init__(self, data=None, task=None, **kwargs): self.data = data self.task = task self.dropout = float(kwargs.get('dropout', 0.25)) self.optimizer = kwargs.get('optimizer', 'adam') self.loss = kwargs.get('loss', 'categorical_crossentropy') self.metrics = kwargs.get('metrics', ['accuracy']) self.loss_weights = kwargs.get('loss_weights', None) self.sample_weight_mode = kwargs.get('sample_weight_mode', None) self.weighted_metrics = kwargs.get('weighted_metrics', None) self.target_tensors = kwargs.get('target_tensors', None) def run(self): """ Runs the training """ num_classes = self.data.y.shape[1] model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=self.data.X.shape[1:])) model.add(Activation('relu')) model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(self.dropout)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(self.dropout)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(self.dropout)) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.compile( loss=self.loss, optimizer=self.optimizer, metrics=self.metrics, loss_weights=self.loss_weights, sample_weight_mode=self.sample_weight_mode, weighted_metrics=self.weighted_metrics, target_tensors=self.target_tensors) self.model = model tb_callback = TensorBoard(mantra_model=self, histogram_freq=0, write_graph=True, write_images=True) exp_callback = StoreTrial(mantra_model=self) eval_callback = EvaluateTask(mantra_model=self) checkpoint_callback = ModelCheckpoint(mantra_model=self) callbacks = [tb_callback, eval_callback, checkpoint_callback, exp_callback] self.model.fit(self.data.X, self.data.y, epochs=self.epochs, batch_size=self.batch_size, callbacks=callbacks) def predict(self, X): return self.model.predict(X)
Code
deep_cnn / README.md
25 lines | 469 bytes

Simple CNN Example

This model implements a simple deep CNN in Keras.

Training

For example, training on the CIFAR-10 dataset:

mantra train deep_cnn --dataset cifar10 --image-dim 32 32

To alter the dropout parameter:

mantra train deep_cnn --dataset cifar10 --image-dim 32 32 --dropout 0.5

Importing

To import this model to your project, run:

mantra import RJT1990/models/deep_cnn