def convert_max_pooling(identifier, spec, previous_layer): return MaxPooling2D( name=identifier, pool_size=(spec.kernel_size, spec.kernel_size), strides=(spec.stride, spec.stride), inbound_nodes=[previous_layer.name], )
def darknet(inputs, data_format): """Creates Darknet model""" filters = 16 for _ in range(4): inputs = Conv2D(inputs, filters, kernel_size=3, data_format=data_format) inputs = BatchNormalization(inputs, data_format=data_format) inputs = LeakyReLU(inputs) inputs = MaxPooling2D(inputs, pool_size=[2, 2], strides=[2, 2], data_format=data_format) filters *= 2 inputs = Conv2D(inputs, filters=256, kernel_size=3, data_format=data_format) inputs = BatchNormalization(inputs, data_format=data_format) inputs = LeakyReLU(inputs) route = inputs # layers 8 inputs = MaxPooling2D(inputs, pool_size=[2, 2], strides=[2, 2], data_format=data_format) inputs = Conv2D(inputs, filters=512, kernel_size=3, data_format=data_format) inputs = BatchNormalization(inputs, data_format=data_format) inputs = LeakyReLU(inputs) inputs = MaxPooling2D(inputs, pool_size=[2, 2], strides=[1, 1], data_format=data_format) inputs = Conv2D(inputs, filters=1024, kernel_size=3, data_format=data_format) inputs = BatchNormalization(inputs, data_format=data_format) inputs = LeakyReLU(inputs) return inputs, route
def convert_max_pooling(spec, previous_layer): print(dir(spec)) return MaxPooling2D( name=spec._get_name(), pool_size=spec.kernel_size, strides=spec.stride, inbound_nodes=[previous_layer], )
def convert(keras_model, class_map, description="Neural Network Model"): """ Convert a keras model to PMML @model. The keras model object @class_map. A map in the form {class_id: class_name} @description. A short description of the model Returns a DeepNeuralNetwork object which can be exported to PMML """ pmml = DeepNetwork(description=description, class_map=class_map) pmml.keras_model = keras_model pmml.model_name = keras_model.name config = keras_model.get_config() for layer in config['layers']: layer_class = layer['class_name'] layer_config = layer['config'] layer_inbound_nodes = layer['inbound_nodes'] # Input if layer_class is "InputLayer": pmml._append_layer( InputLayer(name=layer_config['name'], input_size=layer_config['batch_input_shape'][1:])) # Conv2D elif layer_class is "Conv2D": pmml._append_layer( Conv2D( name=layer_config['name'], channels=layer_config['filters'], kernel_size=layer_config['kernel_size'], dilation_rate=layer_config['dilation_rate'], use_bias=layer_config['use_bias'], activation=layer_config['activation'], strides=layer_config['strides'], padding=layer_config['padding'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # DepthwiseConv2D elif layer_class is "DepthwiseConv2D": pmml._append_layer( DepthwiseConv2D( name=layer_config['name'], kernel_size=layer_config['kernel_size'], depth_multiplier=layer_config['depth_multiplier'], use_bias=layer_config['use_bias'], activation=layer_config['activation'], strides=layer_config['strides'], padding=layer_config['padding'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # MaxPooling elif layer_class is "MaxPooling2D": pmml._append_layer( MaxPooling2D( name=layer_config['name'], pool_size=layer_config['pool_size'], strides=layer_config['strides'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) elif layer_class is "AveragePooling2D": pmml._append_layer( AveragePooling2D( name=layer_config['name'], pool_size=layer_config['pool_size'], strides=layer_config['strides'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) elif layer_class is "GlobalAveragePooling2D": pmml._append_layer( GlobalAveragePooling2D( name=layer_config['name'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # Flatten elif layer_class is "Flatten": pmml._append_layer( Flatten( name=layer_config['name'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # Dense elif layer_class is "Dense": pmml._append_layer( Dense( name=layer_config['name'], channels=layer_config['units'], use_bias=layer_config['use_bias'], activation=layer_config['activation'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # Zero padding layer elif layer_class is "ZeroPadding2D": pmml._append_layer( ZeroPadding2D( name=layer_config['name'], padding=layer_config['padding'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # Reshape layer elif layer_class is "Reshape": pmml._append_layer( Reshape( name=layer_config['name'], target_shape=layer_config['target_shape'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) elif layer_class is "Dropout": pmml._append_layer( Dropout( name=layer_config['name'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # Batch Normalization elif layer_class is "BatchNormalization": pmml._append_layer( BatchNormalization( name=layer_config['name'], axis=layer_config['axis'], momentum=layer_config['momentum'], epsilon=layer_config['epsilon'], center=layer_config['center'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) elif layer_class is "Add": pmml._append_layer( Merge(name=layer_config['name'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes))) elif layer_class is "Subtract": pmml._append_layer( Merge(name=layer_config['name'], operator='subtract', inbound_nodes=get_inbound_nodes(layer_inbound_nodes))) elif layer_class is "Dot": pmml._append_layer( Merge(name=layer_config['name'], operator='dot', inbound_nodes=get_inbound_nodes(layer_inbound_nodes))) elif layer_class is "Concatenate": pmml._append_layer( Merge(name=layer_config['name'], axis=layer_config['axis'], operator='concatenate', inbound_nodes=get_inbound_nodes(layer_inbound_nodes))) elif layer_class is "Activation": pmml._append_layer( Activation( name=layer_config['name'], activation=layer_config['activation'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) elif layer_class is "ReLU": pmml._append_layer( Activation( name=layer_config['name'], activation='relu', threshold=layer_config['threshold'], max_value=layer_config['max_value'], negative_slope=layer_config['negative_slope'], inbound_nodes=get_inbound_nodes(layer_inbound_nodes), )) # Unknown layer else: raise ValueError("Unknown layer type:", layer_class) return pmml
import numpy as np from core.layers import Dense, Dropout, Conv2D, Flatten, ReLU, Softmax, MaxPooling2D from core.model import Model from core.utils import load_mnist from core.optimizer import SGD from core.loss import CategoricalCrossEntropy import matplotlib.pyplot as plt np.random.seed(1234) model = Model() model.add(Conv2D(filters=1, shape=(28, 28, 1), kernel_size=(3, 3))) model.add(ReLU()) model.add(MaxPooling2D(shape=(2, 2))) model.add(Flatten()) model.add(Dense(shape=(169, 128))) #676 model.add(ReLU()) model.add(Dense(shape=(128, 10))) model.add(Softmax()) model.compile(optimizer=SGD(lr=0.01), loss=CategoricalCrossEntropy()) (x, y), (x_test, y_test) = load_mnist() x = x.reshape(x.shape[0], 28, 28, 1) x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) train_loss_cnn, train_acc_cnn, val_loss_cnn, val_acc_cnn = model.fit( x, y, x_test, y_test, epoch=10, batch_size=32) plt.plot(train_acc_cnn, label='cnn train accuracy')