Exemple #1
0
    def test_backward(self, b, w, h, c):

        # TODO: test backward correctly

        input = np.random.uniform(low=-10, high=10., size=(b, w, h, c))
        tf_input = tf.Variable(input)

        # init keras model
        inp = Input(batch_shape=(b, w, h, c))
        x = Activation(activation='relu')(inp)
        y = Activation(activation='tanh')(x)
        Concat = Concatenate(axis=-1)([x, y])  # concatenate of x and y
        model = Model(inputs=[inp], outputs=Concat)
        model.compile(optimizer='sgd', loss='mse')

        # init NumPyNet model
        net = Network(batch=b, input_shape=(w, h, c))
        net.add(Activation_layer(activation='relu'))  # layer 1
        net.add(Activation_layer(activation='tanh'))  # layer 2
        net.add(Route_layer(input_layers=(1, 2), by_channels=True))
        net.add(
            Cost_layer(cost_type='mse',
                       scale=1.,
                       ratio=0.,
                       noobject_scale=1.,
                       threshold=0.,
                       smoothing=0.))
        net.compile(optimizer=SGD())

        net._fitted = True

        # FORWARDS

        fwd_out_numpynet = net.predict(X=input)

        with tf.GradientTape() as tape:
            preds = model(tf_input)
            grads = tape.gradient(preds, tf_input)

            fwd_out_keras = preds.numpy()
            delta_keras = grads.numpy()

        np.testing.assert_allclose(fwd_out_keras,
                                   fwd_out_numpynet,
                                   rtol=1e-5,
                                   atol=1e-8)

        net._fitted = False

        # BACKWARD

        net._net[3].delta = np.ones(shape=fwd_out_numpynet.shape, dtype=float)
        net._backward(X=input)

        delta_numpynet = net._net[0].delta

        assert delta_numpynet.shape == delta_keras.shape
Exemple #2
0
def main():

    done = True
    args = parse_args()

    data_cfg = data_config(args.data_cfg)

    args.netcfg = data_cfg.get('cfg', default=args.netcfg)
    args.weights = data_cfg.get('weights', default=args.weights)
    args.namesfile = data_cfg.get('names', default=args.namesfile)

    if not args.netcfg or not args.weights:
        raise ValueError('Network config AND network weights must be given')

    names = get_labels(args.namesfile, args.classes)

    net = Network(batch=32)
    net.load(args.netcfg, args.weights)

    net_w, net_h, _ = net.input_shape

    if not args.input:
        args.input = input('Enter Image Path: ')
        done = False if not args.input else True

    # set the output filename
    args.outfile = args.outfile if args.outfile else splitext(
        args.input)[0] + '_detected'

    while done:

        # load image from file
        input_image = Image(filename=args.input)

        # pad-resize image if it is necessary
        input_image = input_image.letterbox(net_dim=(
            net_w,
            net_h)) if input_image.shape[:2] != net.shape else input_image

        _ = net.predict(X=input_image)

        # insert boxes evaluation and draw-detection and show image

        input_image.show(window_name=args.outfile,
                         ms=0,
                         fullscreen=args.fullscreen)

        if args.save:
            input_image.save(filename=args.outfile)

        args.input = input('Enter Image Path: ')
        done = False if not args.input else True
Exemple #3
0
def test_route_layer():

  np.random.seed(123)

  batch, w, h, c = (1, 5, 5, 3)
  input = np.random.uniform(low=-10, high=10. ,size=(batch, w, h, c)) # from -10 to 10 to see both the effect of Relu and TanH activation

  # init keras model
  inp    = Input(shape=(w, h, c), batch_shape=(batch, w, h, c))
  x      = Activation(activation='relu')(inp)
  y      = Activation(activation='tanh')(x)
  Concat = Concatenate( axis=-1)([x, y]) # concatenate of x and y
  model  = Model(inputs=[inp], outputs=Concat)

  # init NumPyNet model
  net = Network(batch=batch, input_shape=(w, h, c))

  net.add(Activation_layer(activation='relu')) # layer 1
  net.add(Activation_layer(activation='tanh')) # layer 2
  net.add(Route_layer(input_layers=(1,2), by_channels=True))

  net._fitted = True # False control

  # FORWARDS

  fwd_out_numpynet = net.predict(X=input)
  fwd_out_keras    = model.predict(x=input, batch_size=batch)

  assert np.allclose(fwd_out_keras, fwd_out_numpynet) # ok

  net.fitted = False # the correct state of the network

  # BACKWARD

  # try some derivatives
  gradient    = K.gradients(model.output, model.inputs)
  func        = K.function(model.inputs + model.outputs ,gradient)
  delta_keras = func([input])[0]

  net._net[3].delta = np.ones(shape=fwd_out_numpynet.shape)
  net._backward(X=input)

  delta_numpynet = net._net[0].delta
Exemple #4
0
    def test_forward(self, b, w, h, c):

        input = np.random.uniform(low=-10, high=10.,
                                  size=(b, w, h, c)).astype(float)

        # init keras model
        inp = Input(batch_shape=(b, w, h, c))
        x = Activation(activation='relu')(inp)
        y = Activation(activation='tanh')(x)
        Concat = Concatenate(axis=-1)([x, y])  # concatenate of x and y
        model = Model(inputs=[inp], outputs=Concat)
        model.compile(optimizer='sgd', loss='mse')

        # init NumPyNet model
        net = Network(batch=b, input_shape=(w, h, c))
        net.add(Activation_layer(activation='relu'))  # layer 1
        net.add(Activation_layer(activation='tanh'))  # layer 2
        net.add(Route_layer(input_layers=(1, 2), by_channels=True))
        net.add(
            Cost_layer(cost_type='mse',
                       scale=1.,
                       ratio=0.,
                       noobject_scale=1.,
                       threshold=0.,
                       smoothing=0.))
        net.compile(optimizer=SGD())

        net.summary()

        assert net._fitted == False
        net._fitted = True  # False control

        # FORWARDS

        fwd_out_numpynet = net.predict(X=input)
        fwd_out_keras = model.predict(x=input, batch_size=b)

        np.testing.assert_allclose(fwd_out_keras,
                                   fwd_out_numpynet,
                                   rtol=1e-5,
                                   atol=1e-8)
Exemple #5
0
  model.fit(X=X_train, y=y_train.reshape(-1, 1, 1, 1), max_iter=10)

  print('\n***********START TESTING**************\n')

  # Test the prediction with timing
  loss, out = model.evaluate(X=X_test, truth=y_test, verbose=True)

  mae = mean_absolute_error(y_test, out)

  print('\n')
  print('Loss Score: {:.3f}'.format(loss))
  print('MAE Score: {:.3f}'.format(mae))

  # concatenate the prediction

  train_predicted = model.predict(X=X_train, verbose=False)
  test_predicted  = model.predict(X=X_test, verbose=False)

  predicted = np.concatenate((train_predicted, test_predicted), axis=0)

  fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 6))
  ax.plot(time[:- window_size*2], noisy_signal[:- window_size*2], 'b-', alpha=.75, label='true noisy signal')
  ax.plot(time[:predicted.shape[0]], predicted[:, 0, 0, 0], '-', color='orange', alpha=1, label='predicted signal')

  ax.vlines(time[train_predicted.shape[0]], noisy_signal.min(), noisy_signal.max(), colors='k', linestyle='dashed')

  ax.set_xlabel('Time', fontsize=14)
  ax.set_ylabel('Signal', fontsize=14)

  fig.legend(loc='upper right', fontsize=14)
  fig.tight_layout()
Exemple #6
0
                        activation='Linear'))
    model.add(Softmax_layer(spatial=True))

    print('*************************************')
    print('\n Total input dimension: {}'.format(X_train.shape), '\n')
    print('*************************************')

    model.compile(optimizer=Adam)
    model.summary()

    print('\n***********START TRAINING***********\n')

    # Fit the model on the training set

    model.fit(X=X_train, y=y_train, max_iter=5)

    print('\n***********END TRAINING**************\n')

    # Test the prediction

    out = model.predict(X=X_test)

    truth = y_test.argmax(axis=3).ravel()
    predicted = out.argmax(axis=3).ravel()

    #print('True      label: ', truth)
    #print('Predicted label: ', predicted)

    # accuracy test
    print('Accuracy Score : {:.3f}'.format(accuracy_score(truth, predicted)))