コード例 #1
0
ファイル: run.py プロジェクト: ybin/tinynn
def main(args):
    train_set, valid_set, test_set = prepare_dataset(args.data_dir)
    train_x, train_y = train_set
    test_x, test_y = test_set
    train_y = get_one_hot(train_y, 10)

    if args.model_type == "cnn":
        train_x = train_x.reshape((-1, 28, 28, 1))
        test_x = test_x.reshape((-1, 28, 28, 1))

    if args.model_type == "cnn":
        net = Net([
            Conv2D(kernel=[5, 5, 1, 8], stride=[2, 2], padding="SAME"),
            ReLU(),
            Conv2D(kernel=[5, 5, 8, 16], stride=[2, 2], padding="SAME"),
            ReLU(),
            Conv2D(kernel=[5, 5, 16, 32], stride=[2, 2], padding="SAME"),
            ReLU(),
            Flatten(),
            Dense(10)
        ])
    elif args.model_type == "dense":
        net = Net([
            Dense(200),
            ReLU(),
            Dense(100),
            ReLU(),
            Dense(70),
            ReLU(),
            Dense(30),
            ReLU(),
            Dense(10)
        ])
    else:
        raise ValueError(
            "Invalid argument model_type! Must be 'cnn' or 'dense'")

    model = Model(net=net,
                  loss=SoftmaxCrossEntropyLoss(),
                  optimizer=Adam(lr=args.lr))

    iterator = BatchIterator(batch_size=args.batch_size)
    evaluator = AccEvaluator()
    loss_list = list()
    for epoch in range(args.num_ep):
        t_start = time.time()
        for batch in iterator(train_x, train_y):
            pred = model.forward(batch.inputs)
            loss, grads = model.backward(pred, batch.targets)
            model.apply_grad(grads)
            loss_list.append(loss)
        print("Epoch %d time cost: %.4f" % (epoch, time.time() - t_start))
        # evaluate
        model.set_phase("TEST")
        test_pred = model.forward(test_x)
        test_pred_idx = np.argmax(test_pred, axis=1)
        test_y_idx = np.asarray(test_y)
        res = evaluator.evaluate(test_pred_idx, test_y_idx)
        print(res)
        model.set_phase("TRAIN")
コード例 #2
0
  def __call__(self, inputs):
    """Generate Computation Graph"""
    with tf.variable_scope(self.scope):
      if self.data_format == 'channels_first':
        inputs = tf.transpose(inputs, [0, 3, 1, 2])

      inputs = inputs / 255

      inputs, route2, route4 = darknet53(inputs, data_format=self.data_format)

      inputs, route1 = feature_pyramid_network(inputs, filters=512, data_format=self.data_format)
      detect1 = yolo_layer(inputs,
                           n_classes=self.n_classes,
                           anchors=_ANCHORS[6:],
                           img_size=self.input_size,
                           data_format=self.data_format)

      inputs = Conv2D(route1, filters=256, kernel_size=1, data_format=self.data_format)
      inputs = BatchNormalization(inputs, data_format=self.data_format)
      inputs = LeakyReLU(inputs)

      upsample_size = route2.get_shape().as_list()
      inputs = upsample(inputs, out_shape=upsample_size, data_format=self.data_format)
      axis = 1 if self.data_format == 'channels_first' else 3
      inputs = tf.concat([inputs, route2], axis=axis)

      inputs, route3 = feature_pyramid_network(inputs, filters=256, data_format=self.data_format)
      detect2 = yolo_layer(inputs,
                           n_classes=self.n_classes,
                           anchors=_ANCHORS[3:6],
                           img_size=self.input_size,
                           data_format=self.data_format)

      inputs = Conv2D(route3, filters=128, kernel_size=1, data_format=self.data_format)
      inputs = BatchNormalization(inputs, data_format=self.data_format)
      inputs = LeakyReLU(inputs)

      upsample_size = route4.get_shape().as_list()
      inputs = upsample(inputs, out_shape=upsample_size, data_format=self.data_format)
      axis = 1 if self.data_format == 'channels_first' else 3
      inputs = tf.concat([inputs, route4], axis=axis)

      inputs, _ = feature_pyramid_network(inputs, filters=128, data_format=self.data_format)
      detect3 = yolo_layer(inputs,
                           n_classes=self.n_classes,
                           anchors=_ANCHORS[:3],
                           img_size=self.input_size,
                           data_format=self.data_format)

      inputs = tf.concat([detect1, detect2, detect3], axis=1)
      inputs = build_boxes(inputs)
      boxes_dicts = non_max_suppression(inputs,
                                        n_classes=self.n_classes,
                                        max_output_size=self.max_output_size,
                                        iou_threshold=self.iou_threshold,
                                        confidence_threshold=self.confidence_threshold)
      return boxes_dicts
コード例 #3
0
def darknet53_residual_block(inputs, filters, data_format, strides=1):
  """Creates a residual block for Darknet."""
  shortcut = inputs

  inputs = Conv2D(inputs, filters=filters, kernel_size=1, strides=strides, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)

  filters *= 2
  inputs = Conv2D(inputs, filters=filters, kernel_size=3, strides=strides, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)

  inputs += shortcut
  return inputs
コード例 #4
0
ファイル: run.py プロジェクト: t-k-/nbnp
def main(args):
    if args.seed >= 0:
        random_seed(args.seed)

    # create output directory for saving result images
    if not os.path.exists('./output'): os.mkdir('./output')

    # define network we are going to load
    net = Net([
        Conv2D(kernel=[5, 5, 1, 6], stride=[1, 1], padding="SAME"),
        ReLU(),
        MaxPool2D(pool_size=[2, 2], stride=[2, 2]),
        Conv2D(kernel=[5, 5, 6, 16], stride=[1, 1], padding="SAME"),
        ReLU(),
        MaxPool2D(pool_size=[2, 2], stride=[2, 2]),
        Flatten(),
        Dense(120),
        ReLU(),
        Dense(84),
        ReLU(),
        Dense(10)
    ])

    # load the model
    model = Model(net=net, loss=SoftmaxCrossEntropyLoss(), optimizer=Adam())
    print('loading pre-trained model file', args.model_path)
    model.load(args.model_path)

    # create pyplot window for on-the-fly visualization
    img = np.ones((1, 28, 28, 1))
    fig = disp_mnist_batch(img)

    # actual visualization generations

    layer_name = 'conv-layer-1'
    print('[ ' + layer_name + ' ]')
    images = am_visualize_conv_layer(model, 0, fig)
    save_batch_as_images('output/{}.png'.format(layer_name),
                         images,
                         title='visualized feature maps for ' + layer_name)

    layer_name = 'conv-layer-2'
    print('[ ' + layer_name + ' ]')
    images = am_visualize_conv_layer(model, 3, fig)
    save_batch_as_images('output/{}.png'.format(layer_name),
                         images,
                         title='visualized feature maps for ' + layer_name)
コード例 #5
0
ファイル: yolo_tiny.py プロジェクト: hender14/Raspberry-Pi4
def feature_pyramid_network(inputs, data_format):
    """Creates convolution operations layer used after Darknet"""
    inputs = Conv2D(inputs,
                    filters=256,
                    kernel_size=1,
                    data_format=data_format)
    inputs = BatchNormalization(inputs, data_format=data_format)
    inputs = LeakyReLU(inputs)
    route = inputs

    inputs = Conv2D(inputs,
                    filters=512,
                    kernel_size=3,
                    data_format=data_format)
    inputs = BatchNormalization(inputs, data_format=data_format)
    inputs = LeakyReLU(inputs)
    return inputs, route
コード例 #6
0
ファイル: yolo_tiny.py プロジェクト: hender14/Raspberry-Pi4
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
コード例 #7
0
def convert_conv(identifier, spec, previous_layer):
    return Conv2D(
        name=identifier,
        channels=spec.out_channels,
        kernel_size=spec.kernel_size,
        dilation_rate=spec.dilation,
        use_bias=True,
        activation="linear",
        strides=spec.stride,
        padding='same' if spec.padding == (0, 0) else 'valid',
        inbound_nodes=[previous_layer.name],
    )
コード例 #8
0
ファイル: torch.py プロジェクト: seanjeong3/python-pmml
def convert_conv(spec, previous_layer):
    print(spec)
    print(dir(spec))
    print(spec.state_dict())
    return Conv2D(
        name=spec._get_name(),
        channels=spec.in_channels,
        kernel_size=spec.kernel_size,
        dilation_rate=spec.dilation,
        use_bias=True,
        activation="Relu",
        strides=spec.stride,
        padding=spec.padding,
        inbound_nodes=[previous_layer],
    )
コード例 #9
0
def darknet53(inputs, data_format):
  """Creates Darknet53 model"""
  inputs = Conv2D(inputs, filters=32, kernel_size=3, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)
  inputs = Conv2D(inputs, filters=64, kernel_size=3, strides=2, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)

  inputs = darknet53_residual_block(inputs, filters=32, data_format=data_format)

  inputs = Conv2D(inputs, filters=128, kernel_size=3, strides=2, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)

  for _ in range(2):
    inputs = darknet53_residual_block(inputs, filters=64, data_format=data_format)

  inputs = Conv2D(inputs, filters=256, kernel_size=3, strides=2, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)

  for _ in range(8):
    inputs = darknet53_residual_block(inputs, filters=128, data_format=data_format)
  route4 = inputs # layers 36

  inputs = Conv2D(inputs, filters=512, kernel_size=3, strides=2, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)

  for _ in range(8):
    inputs = darknet53_residual_block(inputs, filters=256, data_format=data_format)
  route2 = inputs # layers 61

  inputs = Conv2D(inputs, filters=1024, kernel_size=3, strides=2, data_format=data_format)
  inputs = BatchNormalization(inputs, data_format=data_format)
  inputs = LeakyReLU(inputs)

  for _ in range(4):
    inputs = darknet53_residual_block(inputs, filters=512, data_format=data_format)
  return inputs, route2, route4
コード例 #10
0
ファイル: keras.py プロジェクト: mdruger/python-pmml
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
コード例 #11
0
ファイル: cnn-mnist.py プロジェクト: RasmusAndersen/numpy-dl
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')