Beispiel #1
0
def draw_pose(R, t, subspace=None, color=None):
    if subspace is None:
        subspace = eye(3)[:2]
    if color is None:
        color = 'r'

    C = -dot(R.T, t)
    Cp = pca.project(C, subspace)
    for i in range(3):
        ep = pca.project(C + R[:, i], subspace)
        plot_points([Cp, ep], '-' + color)
Beispiel #2
0
def draw_pose(R, t, subspace=None, color=None):
    if subspace is None:
        subspace = eye(3)[:2]
    if color is None:
        color = "r"

    C = -dot(R.T, t)
    Cp = pca.project(C, subspace)
    for i in range(3):
        ep = pca.project(C + R[:, i], subspace)
        plot_points([Cp, ep], "-" + color)
def main():

  parser = argparse.ArgumentParser(description=__doc__, epilog=__epilog__,
      formatter_class=argparse.RawDescriptionHelpFormatter)

  parser.add_argument('-t', '--test', action='store_true', default=False,
      help=argparse.SUPPRESS)

  parser.add_argument('-p', '--plot', action='store_true', default=False,
      help="Visualizes confusion matrices graphically (it is off by default)")

  parser.add_argument('machine_file', default='mlp.hdf5',
      metavar='PATH', help="Path to the filename where to store the trained machine (defaults to %(default)s)")

  args = parser.parse_args()

  # loads the machine file
  print("Loading Pen-digits training set...")
  X_train, labels_train = data.as_mnist('data/train.hdf5', 28)
  print("Loading Pen-digits development set...")
  X_devel, labels_devel = data.as_mnist('data/devel.hdf5', 28)

  # creates a matrix for the MLP output in which only the correct label
  # position is set with +1.
  y_train = -1*numpy.ones((10, len(labels_train)), dtype=float)
  y_train[labels_train, range(len(labels_train))] = 1.0
  y_devel = -1*numpy.ones((10, len(labels_devel)), dtype=float)
  y_devel[labels_devel, range(len(labels_devel))] = 1.0

  # Normalizing input set
  #print("Normalizing input data...")
  X_train  = X_train.astype(float)
  X_train /= 255.
  X_devel = X_devel.astype(float)
  X_devel /= 255.

  f = bob.io.base.HDF5File(args.machine_file, 'r')
  X_mean = f.read('X_mean')
  X_std = f.read('X_std')
  pca_comps = f.read('pca_comps')
  X_train -= X_mean
  X_train = pca.project(X_train, pca_comps)
  if X_std != False:
    X_train /= X_std
  X_devel -= X_mean
  X_devel = pca.project(X_devel, pca_comps)
  if X_std != False:
    X_devel /= X_std

  import project as answers

  machine = answers.Machine()
  machine.load(f)
  del f

  hidden = machine.w2.shape[0] - 1
  print("Number of inputs               : %d" % (28*28,))
  print("Number of hidden units         : %d" % (hidden,))
  print("Number of outputs              : 10")
  print("Total number of free parameters: %d" % \
      ((28*28+1)*hidden+(hidden+1)*10))


  print("** FULL Train set results (%d examples):" % X_train.shape[1])
  print("  * cost (J) = %f" % machine.J(X_train, y_train))
  cer = machine.CER(X_train, y_train)
  print('  * CER      = %g%% (%d sample(s))' % (100*cer, X_train.shape[1]*cer))

  print("** Development set results (%d examples):" % X_devel.shape[1])
  print("  * cost (J) = %f" % machine.J(X_devel, y_devel))
  cer = machine.CER(X_devel, y_devel)
  print('  * CER      = %g%% (%d sample(s))' % (100*cer, X_devel.shape[1]*cer))

  if args.test:

    print("Loading Pen-digits (writer-dependent) test set...")
    X_test, labels_test = data.as_mnist('data/test-dependent.hdf5', 28)

    # creates a matrix for the MLP output in which only the correct label
    # position is set with +1.
    y_test = -1*numpy.ones((10, len(labels_test)), dtype=float)
    y_test[labels_test, range(len(labels_test))] = 1.0

    X_test = X_test.astype(float)
    X_test /= 255.
    X_test -= X_mean
    X_test = pca.project(X_test, pca_comps)
    
    if X_std != False:
      X_test /= X_std

    print("** Test set results (%d examples):" % X_test.shape[1])
    print("  * cost (J) = %f" % machine.J(X_test, y_test))
    cer = machine.CER(X_test, y_test)
    print('  * CER      = %g%% (%d sample(s))' % (100*cer, X_test.shape[1]*cer))

  if args.plot:
    print("Plotting confusion matrices...")

    # plot confusion matrix
    N = 2
    if args.test: N = 3
    fig = mpl.figure(figsize=(N*6, 6))

    def plot_cm(X, y, set_name):

      # plot training
      cm = data.confusion_matrix(y.argmax(axis=0), machine.forward(X).argmax(axis=0))
      res = mpl.imshow(cm, cmap=mpl.cm.summer, interpolation='nearest')

      for x in numpy.arange(cm.shape[0]):
        for y in numpy.arange(cm.shape[1]):
          col = 'white'
          if cm[x,y] > 0.5: col = 'black'
          mpl.annotate('%.2f' % (100*cm[x,y],), xy=(y,x), color=col,
              fontsize=8, horizontalalignment='center', verticalalignment='center')

      classes = [str(k) for k in range(10)]

      mpl.xticks(numpy.arange(10), classes)
      mpl.yticks(numpy.arange(10), classes, rotation=90)
      mpl.ylabel("(Your prediction)")
      mpl.xlabel("(Real class)")
      mpl.title("Confusion Matrix (%s set) - in %%" % set_name)

    mpl.subplot(1, N, 1)
    plot_cm(X_train, y_train, 'train')
    mpl.subplot(1, N, 2)
    plot_cm(X_devel, y_devel, 'devel.')

    if args.test:
      mpl.subplot(1, N, 3)
      plot_cm(X_test, y_test, 'test')

    print("Close the plot window to terminate.")
    mpl.show()
    img2 = "data/crans_2_small.jpg"

    image1 = np.array(Image.open(img1).convert("L"))
    image2 = np.array(Image.open(img2).convert("L"))

    locations1, features1 = sift.siftFeature(img1)
    locations2, features2 = sift.siftFeature(img2)

    # use PCAac to reduce dimensions
    numberOfFeaturesOne = locations1.shape[0]
    numberOfFeaturesTwo = locations2.shape[0]

    features = np.vstack((features1, features2))
    V, S, mean = pca.pca(features)

    pcaFeatures1 = pca.project(features1, V, 36)
    pcaFeatures2 = pca.project(features2, V, 36)

    # normalize features
    util.normalize(pcaFeatures1)
    util.normalize(pcaFeatures2)

    np.savetxt("pcafeature1", pcaFeatures1, delimiter="\t")
    np.savetxt("pcafeature2", pcaFeatures2, delimiter="\t")

    # interface with Java program to do matching
    rowX = pcaFeatures1.shape[0]
    rowY = pcaFeatures2.shape[0]
    column = pcaFeatures1.shape[1]

    matchCommand = "java match " + str(rowX) + " " + str(rowY) + " " + str(
def main():

  parser = argparse.ArgumentParser(description=__doc__, epilog=__epilog__,
      formatter_class=argparse.RawDescriptionHelpFormatter)

  parser.add_argument('-s', '--seed', type=int, default=0,
      metavar='SEED (INT)', help="Random (initialization) seed (defaults to %(default)s)")

  parser.add_argument('-H', '--hidden', type=int, default=10,
      metavar='INT', help="Number of hidden units (defaults to %(default)s)")

  parser.add_argument('-p', '--plot', action='store_true', default=False,
      help="Turn-ON plotting **after** training (it is off by default)")
      
  parser.add_argument('-m', '--normalize', action='store_true', default=False, help="Turn-ON normalization of data (it is off by default)")

  parser.add_argument('-l', '--regularization', type=float, default=0.,
      metavar='FLOAT', help="Regularization parameter (defaults to %(default)s - i.e. no reguralization)")
            
  parser.add_argument('-c', '--components', default=236, type=int,
      help="Number of principal components to keep (defaults to %(default)s)")

  parser.add_argument('-n', '--projected-gradient-norm', type=float,
      default=1e-6, metavar='FLOAT', help='The norm of the projected gradient.  Training with LBFGS-B will stop when the surface respects this degree of "flatness" (defaults to %(default)s)')

  parser.add_argument('machine_file', default='mlp.hdf5',
      metavar='MACHINE', help="Path to the filename where to store the trained machine (defaults to %(default)s)")

  args = parser.parse_args()

  ##### START of program

  print("Pen-digit Classification using an MLP with tanh activation")
  print("Number of inputs               : %d" % (28*28,))
  print("Number of hidden units         : %d" % (args.hidden,))
  print("Number of outputs              : 10")
  print("Total number of free parameters: %d" % \
      ((28*28+1)*args.hidden+(args.hidden+1)*10))

  print("Loading Pen-digit training set...")
  X_train, labels_train = data.as_mnist('data/train.hdf5', 28)
  print("Loading Pen-digit development set...")
  X_devel, labels_devel = data.as_mnist('data/devel.hdf5', 28)

  # creates a matrix for the MLP output in which only the correct label
  # position is set with +1.
  y_train = -1*numpy.ones((10, len(labels_train)), dtype=float)
  y_train[labels_train, range(len(labels_train))] = 1.0
  y_devel = -1*numpy.ones((10, len(labels_devel)), dtype=float)
  y_devel[labels_devel, range(len(labels_devel))] = 1.0

  print("Using %d samples for training..." % len(y_train.T))

  # Normalizing input set
  #print("Normalizing input data...")
  X_train  = X_train.astype(float)
  X_train /= 255.
  X_mean   = X_train.mean(axis=1).reshape(-1,1)
  #X_std   = X_train.std(axis=1, ddof=1).reshape(-1,1)
  #X_std[X_std == 0] = 1
  #X_train = (X_train - X_mean) / X_std
  X_train -= X_mean

  # apply PCA for dimensionality reduction prior to MLP
  e, U = pca.pca_bob(X_train)
  # plot energy loading curve
  total_energy = sum(e)
  if args.plot:
    print("Plotting energy load curve...")
    mpl.plot(range(len(e)),
        100*numpy.cumsum(e)/total_energy)
    mpl.title('Energy loading curve for M-NIST (training set)')
    mpl.xlabel('Number of components')
    mpl.ylabel('Energy (percentage)')
    mpl.grid()
    print("Close the plot window to continue.")
    mpl.show()
    
  print("With %d components (your choice), you preserve %.2f%% of the energy" % (args.components, 100*sum(e[:args.components])/total_energy))

  pca_comps = U[:,0:args.components];
  X_train = pca.project(X_train, pca_comps)
  X_std = X_train.std(axis = 1, ddof = 1).reshape(-1,1)
  if args.normalize:
    X_train /= X_std  
  else:
    X_std = False
  import project as answers

  trainer = answers.Trainer(args.seed, args.hidden, args.regularization,
      args.projected_gradient_norm)
  start = time.time()
  machine = trainer.train(X_train, y_train*0.8)
  total = time.time() - start

  if machine is None:
    print("Training did **NOT** finish. Aborting...")
    sys.exit(1)

  sys.stdout.write("** Training is over, took %.2f minute(s)\n" % (total/60.))
  sys.stdout.flush()

  f = bob.io.base.HDF5File(args.machine_file, 'w')
  f.set('X_mean', X_mean)
  f.set('pca_comps', pca_comps)
  f.set('X_std', X_std)
  machine.save(f)
  del f

  X_devel = X_devel.astype(float)
  X_devel /= 255.
  X_devel -= X_mean
  X_devel = pca.project(X_devel, pca_comps)
  if args.normalize:
    X_devel /= X_std

  print("** Development set results (%d examples):" % X_devel.shape[1])
  print("  * cost (J) = %f" % machine.J(X_devel, y_devel))
  cer = machine.CER(X_devel, y_devel)
  print('  * CER      = %g%% (%d sample(s))' % (100*cer, X_devel.shape[1]*cer))
Beispiel #6
0
def draw_point_cloud(xs, subspace=None):
    if subspace is None:
        subspace = eye(3)[:2]
    plot_points(pca.project(xs, subspace), '.b')
    img2 = "data/crans_2_small.jpg"

    image1 = np.array(Image.open(img1).convert("L"))
    image2 = np.array(Image.open(img2).convert("L"))

    locations1, features1 = sift.siftFeature(img1)
    locations2, features2 = sift.siftFeature(img2)

    # use PCAac to reduce dimensions
    numberOfFeaturesOne = locations1.shape[0]
    numberOfFeaturesTwo = locations2.shape[0]

    features = np.vstack((features1, features2))
    V, S, mean = pca.pca(features)

    pcaFeatures1 = pca.project(features1, V, 36)
    pcaFeatures2 = pca.project(features2, V, 36)

    # normalize features
    util.normalize(pcaFeatures1)
    util.normalize(pcaFeatures2)

    np.savetxt("pcafeature1", pcaFeatures1, delimiter="\t")
    np.savetxt("pcafeature2", pcaFeatures2, delimiter="\t")

    # interface with Java program to do matching
    rowX = pcaFeatures1.shape[0]
    rowY = pcaFeatures2.shape[0]
    column = pcaFeatures1.shape[1]

    matchCommand = "java match " +str(rowX)+ " " +str(rowY)+ " " +str(column)
Beispiel #8
0
def draw_point_cloud(xs, subspace=None):
    if subspace is None:
        subspace = eye(3)[:2]
    plot_points(pca.project(xs, subspace), ".b")
    def identifyKeyFrame(self, SIFTFeatures, indices, threshold = 0.15):
        if len(indices) in [1,2]:
            lst = []
            lst.append(indices[0])
            return lst

        # build up graph structure
        numberOfNodes = len(SIFTFeatures)
        graph = Graph(numberOfNodes)

        for i in range(numberOfNodes):
            for j in range(i+1, numberOfNodes, 1):

                one = SIFTFeatures[i]
                two = SIFTFeatures[j]

                # check whether one and two are near duplicate
                pcaFeatures1 = pca.project(one, self.V, 36)
                pcaFeatures2 = pca.project(two, self.V, 36)

                # normalize features
                util.normalize(pcaFeatures1)
                util.normalize(pcaFeatures2)

                np.savetxt("pcafeature1", pcaFeatures1, delimiter="\t")
                np.savetxt("pcafeature2", pcaFeatures2, delimiter="\t")

                # interface with Java program to do matching
                rowX = pcaFeatures1.shape[0]
                rowY = pcaFeatures2.shape[0]
                column = pcaFeatures1.shape[1]

                matchCommand = "java match " +str(rowX)+ " " +str(rowY)+ " " +str(column)
                print matchCommand
                os.system(matchCommand)

                # plot according to match stored in "data" folder
                matchFile = open("data/match", 'r')
                lines = matchFile.readlines()

                matchSize = len(lines)
                oneSize = one.shape[0]
                twoSize = two.shape[0]

                ratio = matchSize / float(min(oneSize, twoSize))

                if ratio > threshold:
                    graph.connect(i,j)

        # Find nodes with largest edges in each connected component
        connectedComponents = graph.connectedComponent()

        tempkeyFrames = []
        for component in connectedComponents:
            edges = []

            for node in component:
                edges.append(graph.getNumOfEdges(node))

            maxIndice = 0
            maxValue = edges[0]
            for i in range(1, len(edges), 1):
                if maxValue < edges[i]:
                    maxValue = edges[i]
                    maxIndice = i

            # random choose one if there are many within one component
            maxEdges = []
            for i in range(len(edges)):
                if edges[i] == maxValue:
                    maxEdges.append(i)

            if len(maxEdges) == 1:
                tempkeyFrames.append(component[maxIndice])
            else:
                maxEdgeSize = len(maxEdges)
                randomNumber = randint(0, maxEdgeSize - 1)
                tempkeyFrames.append(component[randomNumber])

        keyFrames = []
        for indice in tempkeyFrames:
            keyFrames.append(indices[indice])

        return keyFrames