private static int toColour(RealVector ambient) {
   final double r = ambient.getEntry(0);
   final double g = ambient.getEntry(1);
   final double b = ambient.getEntry(2);
   int rc = 256 * 256 * (int) (255. * r);
   int rg = 256 * (int) (255. * g);
   int rb = (int) (255. * b);
   return rc + rg + rb;
 }
 @Override
 public void train(List<Instance> instances) {
   // ------------------------ initialize rows and columns ---------------------
   int rows = instances.size();
   int columns = 0;
   // get max columns
   for (Instance i : instances) {
     int localColumns = Collections.max(i.getFeatureVector().getFeatureMap().keySet());
     if (localColumns > columns) columns = localColumns;
   }
   // ------------------------ initialize alpha vector -----------------------
   alpha = new ArrayRealVector(rows, 0);
   // ------------------------ initialize base X and Y for use --------------------------
   double[][] X = new double[rows][columns];
   double[] Y = new double[rows];
   for (int i = 0; i < rows; i++) {
     Y[i] = ((ClassificationLabel) instances.get(i).getLabel()).getLabelValue();
     for (int j = 0; j < columns; j++) {
       X[i][j] = instances.get(i).getFeatureVector().get(j + 1);
     }
   }
   // ---------------------- gram matrix -------------------
   matrixX = new Array2DRowRealMatrix(X);
   RealMatrix gram = new Array2DRowRealMatrix(rows, rows);
   for (int i = 0; i < rows; i++) {
     for (int j = 0; j < rows; j++) {
       gram.setEntry(i, j, kernelFunction(matrixX.getRowVector(i), matrixX.getRowVector(j)));
     }
   }
   // ---------------------- gradient ascent --------------------------
   Sigmoid g = new Sigmoid(); // helper function
   System.out.println("Training start...");
   System.out.println(
       "Learning rate: " + _learning_rate + " Training times: " + _training_iterations);
   for (int idx = 0; idx < _training_iterations; idx++) {
     System.out.println("Training iteration: " + (idx + 1));
     for (int k = 0; k < rows; k++) {
       double gradient_ascent = 0.0;
       RealVector alpha_gram = gram.operate(alpha);
       for (int i = 0; i < rows; i++) {
         double lambda = alpha_gram.getEntry(i);
         double kernel = gram.getEntry(i, k);
         gradient_ascent =
             gradient_ascent
                 + Y[i] * g.value(-lambda) * kernel
                 + (1 - Y[i]) * g.value(lambda) * (-kernel);
       }
       alpha.setEntry(k, alpha.getEntry(k) + _learning_rate * gradient_ascent);
     }
   }
   System.out.println("Training done!");
 }
 @Override
 public Label predict(Instance instance) {
   Label l = null;
   if (instance.getLabel() instanceof ClassificationLabel || instance.getLabel() == null) {
     // ----------------- declare variables ------------------
     double lambda = 0.0;
     RealVector x_instance = new ArrayRealVector(matrixX.getColumnDimension(), 0);
     double result = 0.0;
     // -------------------------- initialize xi -------------------------
     for (int idx = 0; idx < matrixX.getColumnDimension(); idx++) {
       x_instance.setEntry(idx, instance.getFeatureVector().get(idx + 1));
     }
     // ------------------ get lambda -----------------------
     for (int j = 0; j < alpha.getDimension(); j++) {
       lambda += alpha.getEntry(j) * kernelFunction(matrixX.getRowVector(j), x_instance);
     }
     // ----------------- make prediction -----------------
     Sigmoid g = new Sigmoid(); // helper function
     result = g.value(lambda);
     l = new ClassificationLabel(result < 0.5 ? 0 : 1);
   } else {
     System.out.println("label type error!");
   }
   return l;
 }
 private static Intersection getIntersection(
     TriangleObject obj, DecompositionSolver solver, Vector3D p, Vector3D pc, double[] params) {
   RealVector constants = new ArrayRealVector(params, false);
   RealVector solution = solver.solve(constants);
   double alpfa = solution.getEntry(0);
   double beta = solution.getEntry(1);
   boolean match = false;
   if (alpfa >= 0 && beta >= 0 && alpfa + beta <= 1.0) {
     match = true;
   } else {
     match = false;
   }
   final Intersection intersection = new Intersection(match);
   intersection.setObject(obj);
   intersection.setP(p);
   return intersection;
 }
Exemple #5
0
  public void update2DFixed() {
    double[] delta = new double[Peak.NFITPARAMS];
    double[] jt = new double[4];
    double[] jacobian = new double[4];
    double[][] hessian = new double[4][4];

    for (FitPeak fit : this.fits) {
      if (fit.peak.getStatus() == PeakStatus.RUNNING) {

        for (int i = 0; i < 4; i++) {
          jacobian[i] = 0.0;
          hessian[i][0] = 0.0;
          hessian[i][1] = 0.0;
          hessian[i][2] = 0.0;
          hessian[i][3] = 0.0;
        }

        int offset = fit.offset;
        int wx = fit.wx;
        int wy = fit.wy;
        double height = fit.peak.getHeight();
        double width = fit.peak.getXWidth();

        for (int i = -wy; i <= +wy; i++) {
          double yt = fit.yt[i + wy];
          double eyt = fit.eyt[i + wy];
          for (int j = -wx; j <= +wx; j++) {
            int idx = (i * this.imgSizeX) + (j + offset);

            // est = foreground + average background
            double fi = this.fgData[idx] + (this.bgData[idx] / ((double) this.bgCounts[idx]));

            double xi = this.imgData[idx];
            double xt = fit.xt[j + wx];
            double ext = fit.ext[j + wx];
            double et = ext * eyt;

            // TODO: Remember to change order to match peak array in peak class
            jt[0] = et;
            jt[1] = 2.0 * height * width * xt * et;
            jt[2] = 2.0 * height * width * yt * et;
            // TODO: this should be # 2
            jt[3] = 1.0; // this is background

            // calculate jacobian;
            double t1 = 2.0 * (1.0 - xi / fi);
            jacobian[0] += t1 * jt[0];
            jacobian[1] += t1 * jt[1];
            jacobian[2] += t1 * jt[2];
            jacobian[3] += t1 * jt[3];

            // calculate hessian (without second deriv terms)
            double t2 = 2.0 * xi / (fi * fi);
            hessian[0][0] += t2 * jt[0] * jt[0];
            hessian[0][1] += t2 * jt[0] * jt[1];
            hessian[0][2] += t2 * jt[0] * jt[2];
            hessian[0][3] += t2 * jt[0] * jt[3];

            // hessian[4]
            hessian[1][1] += t2 * jt[1] * jt[1];
            hessian[1][2] += t2 * jt[1] * jt[2];
            hessian[1][3] += t2 * jt[1] * jt[3];

            // hessian[8]
            // hessian[9]
            hessian[2][2] += t2 * jt[2] * jt[2];
            hessian[2][3] += t2 * jt[2] * jt[3];

            // hessian[12]
            // hessian[13]
            // hessian[14]
            hessian[3][3] += t2 * jt[3] * jt[3];
          }
        }

        // subtract old peak out of foreground and background arrays
        subtractPeak(fit);

        // use lapack to solve Ax=B to calculate update vector;
        boolean error = false;
        RealMatrix hessianMatrix = MatrixUtils.createRealMatrix(hessian);
        RealVector jacobianVector = MatrixUtils.createRealVector(jacobian);

        try {
          MatrixUtils.solveUpperTriangularSystem(hessianMatrix, jacobianVector);
        } catch (Exception ex) {
          fit.peak.setStatus(PeakStatus.ERROR);
          error = true;
        }
        if (!error) {
          // update parameters
          // height
          // TODO: Rearranged the jacobian vector entries
          delta[Peak.HEIGHT] = jacobianVector.getEntry(0); // height
          delta[Peak.BACKGROUND] = jacobianVector.getEntry(3); // background
          delta[Peak.XCENTER] = jacobianVector.getEntry(1); // x center
          delta[Peak.YCENTER] = jacobianVector.getEntry(2); // y center

          // update fits data
          fitDataUpdate(fit, delta);

          // add the new peak to the foreground and background arrays
          if (!fit.peak.hasStatus(PeakStatus.ERROR)) {
            addPeak(fit);
          }
        }
      }
    }
  }
Exemple #6
0
  public void update2D() {
    double[] delta = new double[Peak.NFITPARAMS];
    double[] jt = new double[5];
    double[] jacobian = new double[5];
    double[][] hessian = new double[5][5];

    for (FitPeak fit : this.fits) {
      if (fit.peak.hasStatus(PeakStatus.RUNNING)) {

        for (int i = 0; i < 5; i++) {
          jacobian[i] = 0.0;
          hessian[i][0] = 0.0;
          hessian[i][1] = 0.0;
          hessian[i][2] = 0.0;
          hessian[i][3] = 0.0;
          hessian[i][4] = 0.0;
        }

        final int offset = fit.offset;
        final int wx = fit.wx;
        final int wy = fit.wy;
        final double height = fit.peak.getHeight();
        final double width = fit.peak.getXWidth();

        for (int i = -wy; i <= +wy; i++) {
          final double yt = fit.yt[i + wy];
          final double eyt = fit.eyt[i + wy];

          for (int j = -wx; j <= +wx; j++) {
            final double xt = fit.xt[j + wx];
            final double ext = fit.ext[j + wx];

            final int idx = (i * this.imgSizeX) + (j + offset);
            final double xi = this.imgData[idx];
            final double fi = this.fgData[idx] + (this.bgData[idx] / ((double) this.bgCounts[idx]));

            final double et = ext * eyt;
            jt[0] = et;
            jt[1] = 2.0 * height * width * xt * et;
            jt[2] = 2.0 * height * width * yt * et;
            jt[3] = (-height * xt * xt * et) - (height * yt * yt * et);
            jt[4] = 1.0;

            // calculate jacobian
            final double t1 = 2.0 * (1.0 - xi / fi);
            jacobian[0] += t1 * jt[0];
            jacobian[1] += t1 * jt[1];
            jacobian[2] += t1 * jt[2];
            jacobian[3] += t1 * jt[3];
            jacobian[4] += t1 * jt[4];

            // calculate hessian
            final double t2 = 2.0 * xi / (fi * fi);

            // calculate hessian without second derivative terms.
            // (symmetric upper triangular)
            hessian[0][0] += t2 * jt[0] * jt[0];
            hessian[0][1] += t2 * jt[0] * jt[1];
            hessian[0][2] += t2 * jt[0] * jt[2];
            hessian[0][3] += t2 * jt[0] * jt[3];
            hessian[0][4] += t2 * jt[0] * jt[4];

            hessian[1][1] += t2 * jt[1] * jt[1];
            hessian[1][2] += t2 * jt[1] * jt[2];
            hessian[1][3] += t2 * jt[1] * jt[3];
            hessian[1][4] += t2 * jt[1] * jt[4];

            hessian[2][2] += t2 * jt[2] * jt[2];
            hessian[2][3] += t2 * jt[2] * jt[3];
            hessian[2][4] += t2 * jt[2] * jt[4];

            hessian[3][3] += t2 * jt[3] * jt[3];
            hessian[3][4] += t2 * jt[3] * jt[4];

            hessian[4][4] += t2 * jt[4] * jt[4];
          }
        }

        // subtract the old peak out of the foreground and background arrays
        subtractPeak(fit);

        // use lapack to solve Ax=B to calculate update vector;
        boolean error = false;
        final RealMatrix hessianMatrix = MatrixUtils.createRealMatrix(hessian);
        RealVector jacobianVector = MatrixUtils.createRealVector(jacobian);

        try {
          MatrixUtils.solveUpperTriangularSystem(hessianMatrix, jacobianVector);
        } catch (Exception ex) {
          fit.peak.setStatus(PeakStatus.ERROR);
          error = true;
        }
        if (!error) {
          // TODO: Rearranged the jacobian vector entries
          delta[Peak.HEIGHT] = jacobianVector.getEntry(0); // height
          delta[Peak.XCENTER] = jacobianVector.getEntry(1); // x center
          delta[Peak.YCENTER] = jacobianVector.getEntry(2); // y center
          delta[Peak.XWIDTH] = jacobianVector.getEntry(3); // width
          delta[Peak.YWIDTH] = jacobianVector.getEntry(3); // width
          delta[Peak.BACKGROUND] = jacobianVector.getEntry(4); // background

          // update fits data
          fitDataUpdate(fit, delta);

          // add the peak to the foreground and background arrays
          // recalculate peak fit area as the peak width may have changed
          if (!fit.peak.hasStatus(PeakStatus.ERROR)) {
            fit.wx = calcWidth(fit.peak.getXWidth(), fit.wx);
            fit.wy = fit.wx;
            addPeak(fit);
          }
        }
      }
    }
  }
Exemple #7
0
  public void updateZ() {

    double[] delta = new double[Peak.NFITPARAMS];
    double[] jt = new double[5];
    double[] jacobian = new double[5];
    double[][] hessian = new double[5][5];

    for (FitPeak fit : this.fits) {
      if (fit.peak.hasStatus(PeakStatus.RUNNING)) {

        for (int i = 0; i < 5; i++) {
          jacobian[i] = 0;
          hessian[i][0] = 0.0;
          hessian[i][1] = 0.0;
          hessian[i][2] = 0.0;
          hessian[i][3] = 0.0;
          hessian[i][4] = 0.0;
        }

        final int offset = fit.offset;
        final int wx = fit.wx;
        final int wy = fit.wy;

        final double height = fit.peak.getHeight();
        final double xwidth = fit.peak.getXWidth();
        final double ywidth = fit.peak.getYWidth();

        // calculate dwx vs z
        final double zCenter = fit.peak.getZCenter();
        double z0 = (zCenter - this.wxZParams[1]) / this.wxZParams[2];
        double z1 = z0 * z0;
        double z2 = z1 * z0;
        double zt = (2.0 * z0) + (3.0 * this.wxZParams[3] * z1) + (4.0 * this.wxZParams[4] * z2);
        final double gx = -2.0 * zt / (this.wxZParams[0] * fit.wxTerm);

        // calculate dwy vs z
        z0 = (zCenter - this.wyZParams[1]) / this.wyZParams[2];
        z1 = z0 * z0;
        z2 = z1 * z0;
        zt = (2.0 * z0) + (3.0 * this.wyZParams[3] * z1) + (4.0 * this.wyZParams[4] * z2);
        final double gy = -2.0 * zt / (this.wyZParams[0] * fit.wyTerm);

        for (int i = -wy; i < +wy; i++) {

          final double yt = fit.yt[i + wy];
          final double eyt = fit.eyt[i + wy];

          for (int j = -wx; j < +wx; j++) {

            final double xt = fit.xt[j + wx];
            final double ext = fit.ext[j + wx];

            final int idx = (i * this.imgSizeX) + (j + offset);
            final double xi = this.imgData[idx];
            final double fi = this.fgData[idx] / (this.bgData[idx] / ((double) this.bgCounts[idx]));

            // first derivatives
            final double et = ext * eyt;
            jt[0] = et;
            jt[1] = 2.0 * height * xwidth * xt * et;
            jt[2] = 2.0 * height * ywidth * yt * et;
            jt[3] = (-height * xt * xt * gx * et) - (height * yt * yt * gy * et);
            jt[4] = 1.0;

            // calculate jacobian
            final double t1 = 2.0 * (1.0 - xi / fi);
            jacobian[0] += t1 * jt[0];
            jacobian[1] += t1 * jt[1];
            jacobian[2] += t1 * jt[2];
            jacobian[3] += t1 * jt[3];
            jacobian[4] += t1 * jt[4];

            // calculate hessian
            final double t2 = 2.0 * xi / (fi * fi);

            // calculate hessian without second derivative terms.
            hessian[0][0] += t2 * jt[0] * jt[0];
            hessian[0][1] += t2 * jt[0] * jt[1];
            hessian[0][2] += t2 * jt[0] * jt[2];
            hessian[0][3] += t2 * jt[0] * jt[3];
            hessian[0][4] += t2 * jt[0] * jt[4];

            hessian[1][1] += t2 * jt[1] * jt[1];
            hessian[1][2] += t2 * jt[1] * jt[2];
            hessian[1][3] += t2 * jt[1] * jt[3];
            hessian[1][4] += t2 * jt[1] * jt[4];

            hessian[2][2] += t2 * jt[2] * jt[2];
            hessian[2][3] += t2 * jt[2] * jt[3];
            hessian[2][4] += t2 * jt[2] * jt[4];

            hessian[3][3] += t2 * jt[3] * jt[3];
            hessian[3][4] += t2 * jt[3] * jt[4];

            hessian[4][4] += t2 * jt[4] * jt[4];
          }
        }

        // subtract the old peak out of the foreground and background arrays
        subtractPeak(fit);

        // use lapack to solve Ax=B to calculate update vector;
        boolean error = false;
        final RealMatrix hessianMatrix = MatrixUtils.createRealMatrix(hessian);
        RealVector jacobianVector = MatrixUtils.createRealVector(jacobian);
        try {
          MatrixUtils.solveUpperTriangularSystem(hessianMatrix, jacobianVector);
        } catch (Exception ex) {
          System.out.println("Fitting ERROR:");
          System.out.println(ex.getMessage());
          fit.peak.setStatus(PeakStatus.ERROR);
          error = true;
        }
        if (!error) {
          // update parameters
          delta[Peak.HEIGHT] = jacobianVector.getEntry(0);
          delta[Peak.XCENTER] = jacobianVector.getEntry(1);
          delta[Peak.YCENTER] = jacobianVector.getEntry(2);
          delta[Peak.ZCENTER] = jacobianVector.getEntry(3);
          delta[Peak.BACKGROUND] = jacobianVector.getEntry(4);

          fitDataUpdate(fit, delta);

          if (!fit.peak.hasStatus(PeakStatus.ERROR)) {
            // calculate new x, y, width and update fit area
            calcWidthsFromZ(fit);
            fit.wx = calcWidth(fit.peak.getXWidth(), fit.wx);
            fit.wy = calcWidth(fit.peak.getYWidth(), fit.wy);
            addPeak(fit);
          }
        }
      }
    }
  }