示例#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, 2)

    net = Net([Dense(100), ReLU(), Dense(30), ReLU(), Dense(1)])

    model = Model(net=net,
                  loss=SigmoidCrossEntropyLoss(),
                  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))
        for timer in model.timers.values():
            timer.report()
        # evaluate
        model.set_phase("TEST")
        test_y_idx = np.asarray(test_y).reshape(-1)
        test_pred = model.forward(test_x)
        test_pred[test_pred > 0] = 1
        test_pred[test_pred <= 0] = 0
        test_pred_idx = test_pred.reshape(-1)
        res = evaluator.evaluate(test_pred_idx, test_y_idx)
        print(res)
        model.set_phase("TRAIN")
示例#2
0
def main(args):
    if args.seed >= 0:
        random_seed(args.seed)

    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)

    train_x = Tensor(train_x)
    train_y = Tensor(train_y)
    test_x = Tensor(test_x)
    test_y = Tensor(test_y)

    net = Net([
        Dense(200),
        ReLU(),
        Dense(100),
        ReLU(),
        Dense(70),
        ReLU(),
        Dense(30),
        ReLU(),
        Dense(10)
    ])

    model = Model(net=net,
                  loss=SoftmaxCrossEntropyLoss(),
                  optimizer=Adam(lr=args.lr))
    loss_layer = SoftmaxCrossEntropyLoss()
    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):
            model.zero_grad()
            pred = model.forward(batch.inputs)
            loss = loss_layer.loss(pred, batch.targets)
            loss.backward()
            model.step()
            loss_list.append(loss.values)
        print("Epoch %d tim 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 = test_y.values
        res = evaluator.evaluate(test_pred_idx, test_y_idx)
        print(res)
        model.set_phase("TRAIN")
示例#3
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)
示例#4
0
def convert_dense(spec, previous_layer):
    return Dense(
        name=spec._get_name(),
        channels=spec.units,
        use_bias=spec.use_bias,
        activation=spec.activation,
        inbound_nodes=[previous_layer],
    )
示例#5
0
def convert_dense(identifier, spec, previous_layer):
    return Dense(
        name=identifier,
        channels=spec.out_features,
        use_bias=True,
        activation='linear',
        inbound_nodes=[previous_layer.name],
    )
示例#6
0
文件: run.py 项目: aron-castle/tinynn
def main(args):
    if args.seed >= 0:
        random_seed(args.seed)

    # data preparing
    data_path = os.path.join(args.data_dir, args.file_name)
    train_x, train_y, img_shape = prepare_dataset(data_path)

    net = Net([
        Dense(30),
        ReLU(),
        Dense(60),
        ReLU(),
        Dense(60),
        ReLU(),
        Dense(30),
        ReLU(),
        Dense(3),
        Sigmoid()
    ])

    model = Model(net=net, loss=MSELoss(), optimizer=Adam())
    mse_evaluator = MSEEvaluator()
    iterator = BatchIterator(batch_size=args.batch_size)
    for epoch in range(args.num_ep):
        t_start = time.time()
        for batch in iterator(train_x, train_y):
            preds = model.forward(batch.inputs)
            loss, grads = model.backward(preds, batch.targets)
            model.apply_grad(grads)

        # evaluate
        preds = net.forward(train_x)
        mse = mse_evaluator.evaluate(preds, train_y)
        print(mse)

        if args.paint:
            # generate painting
            preds = preds.reshape(img_shape[0], img_shape[1], -1)
            preds = (preds * 255.0).astype("uint8")
            filename, ext = os.path.splitext(args.file_name)
            output_filename = "output" + ext
            output_path = os.path.join(args.data_dir, output_filename)
            Image.fromarray(preds).save(output_path)
        print("Epoch %d time cost: %.2f" % (epoch, time.time() - t_start))
示例#7
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")
示例#8
0
文件: run.py 项目: alvinox/exercise
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)

    net = Net([
        Dense(784, 200),
        ReLU(),
        Dense(200, 100),
        ReLU(),
        Dense(100, 70),
        ReLU(),
        Dense(70, 30),
        ReLU(),
        Dense(30, 10)
    ])

    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)
        t_end = time.time()
        # evaluate
        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("Epoch %d time cost: %.4f\t %s" % (epoch, t_end - t_start, res))
示例#9
0
 def build_net(self):
     q_net = Net([Dense(100), ReLU(), Dense(self.action_dim)])
     return q_net
示例#10
0
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
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')
plt.plot(val_acc_cnn, label='cnn val accuracy')
示例#12
0
import numpy as np
from core.layers import Dense, Dropout, ReLU, Softmax
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(Dense(shape=(784, 512)))
model.add(ReLU())
model.add(Dropout(0.2))

model.add(Dense(shape=(512, 512)))
model.add(ReLU())
model.add(Dropout(0.2))

model.add(Dense(shape=(512, 10)))
model.add(Softmax())

model.compile(optimizer=SGD(lr=0.001), loss=CategoricalCrossEntropy())


(x, y), (x_test, y_test) = load_mnist()

train_loss, train_acc, val_loss, val_acc = model.fit(x, y, x_test, y_test, epoch=10, batch_size=128)

plt.plot(train_acc, label='train loss')
示例#13
0
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')

    # prepare and read dataset
    train_set, valid_set, test_set = prepare_dataset(args.data_dir)
    train_x, train_y = train_set
    test_x, test_y = test_set

    # batch iterator
    iterator = BatchIterator(batch_size=args.batch_size)

    # specify the encoder and decoder net structure
    encoder = Net([Dense(256), ReLU(), Dense(64)])

    decoder = Net([ReLU(), Dense(256), Tanh(), Dense(784), Tanh()])

    # create AutoEncoder model
    model = AutoEncoder(encoder=encoder,
                        decoder=decoder,
                        loss=MSELoss(),
                        optimizer=Adam(args.lr))

    # for pretrained model, test generated images from latent space
    if args.load_model is not None:
        # load pretrained model
        model.load(args.load_model)
        print('Loaded model from %s' % args.load_model)
        # transition from test[from_idx] to test[to_idx] in n steps
        idx_arr, n = [2, 4, 32, 12, 82], 160
        print("Transition in numbers", [test_y[i] for i in idx_arr],
              "in %d steps ..." % n)
        stops = [model.encoder.forward(test_x[i]) for i in idx_arr]
        k = int(n / (len(idx_arr) - 1))  # number of code per transition
        # generate all transition codes
        code_arr = []
        for i in range(len(stops) - 1):
            t = [c.copy() for c in transition(stops[i], stops[i + 1], k)]
            code_arr += t
        # apply decoding all n "code" from latent space...
        batch = None
        for code in code_arr:
            # translate latent space to image
            genn = model.decoder.forward(code)
            # save decoded results in a batch
            if batch is None:
                batch = np.array(genn)
            else:
                batch = np.concatenate((batch, genn))
        save_batch_as_images('output/genn-latent.png', batch)
        quit()

    # train the autoencoder
    for epoch in range(args.num_ep):
        print('epoch %d ...' % epoch)
        for batch in iterator(train_x, train_y):
            origin_in = batch.inputs
            # make noisy inputs
            m = origin_in.shape[0]  # batch size
            mu = args.guassian_mean  # mean
            sigma = args.guassian_std  # standard deviation
            noises = np.random.normal(mu, sigma, (m, 784))
            noises_in = origin_in + noises  # noisy inputs
            # train the representation
            genn = model.forward(noises_in)
            loss, grads = model.backward(genn, origin_in)
            model.apply_grad(grads)
        print('Loss: %.3f' % loss)
        # save all the generated images and original inputs for this batch
        save_batch_as_images('output/ep%d-input.png' % epoch,
                             noises_in,
                             titles=batch.targets)
        save_batch_as_images('output/ep%d-genn.png' % epoch,
                             genn,
                             titles=batch.targets)

    # save the model after training
    model.save('output/model.pkl')