Rosella Machine Intelligence & Data Mining | |||
Computer Vision Coding Example: Java OpenCL and CudaCMSR ML Studio generated program source codes are very easy to integrate into your applocations. This page will show how to code to use Computer Vision functions. Depending on modeling types, calling parameters are different. Model Code Generation and CompilationFrom CMSR ML Studio, perform the followings to generate model codes;
Copy a model parameter file and the GPU shader file into locations where your main program can access. And modify access path in your main program. To create model parameter file, from "Modeling" menu, select "Export model as file (MyDataSay, BI Server, CNN, ...etc.)". In addition, you also need Java OpenCL (JOCL) or Java Cuda (JCUDA) jar file. You can get from the following links;
CNN/FCN Classification ExampleThe following code shows how CNN/FCN classification is called. You can control the number of CPU threads to use. Normally it is equal to the number of CPU cores or CPU hyper threads. Note that CMSRModel is the model class that CMSR Studio generates. import java.io.File; import java.io.FileInputStream; public class Eval1 { public static void main(String[] args) { String filebase = "C:\\YourPath...\\"; String gpudevicename = "Intel(R) HD Graphics 5500"; // null for the fist GPU String gpukernelfile = filebase+"CNNoclForward.cl"; // for OpenCL String gpukernelfile = filebase+"CNNcudaForward.ptx"; // for Cuda String modeltype, modelfilepath; int[][][] image; boolean blackandwhite = false; boolean r0g1b2 = true; CMSRModel model = new CMSRModel(); try { modeltype = "CNN"; modelfilepath = filebase+"\\modelfile."+modeltype.toLowerCase(); // initalize resource model.initializeGpuAndModel(gpudevicename, new FileInputStream(gpukernelfile), new FileInputStream(modelfilepath)); // repeat the followins as needed. can get image from camera. File testimagefile = new File(filebase+modeltype+"images.jpg"); image = getRGBArray(testimagefile, model.input_image_width, model.input_image_height); int outLabelCount = 4; int[] outLabelIndices = new int[outLabelCount+1]; // notice extra 1 float[] outLabelProbabilities = new float[outLabelCount+1]; // notice extra 1 model.evaluate(outLabelCount, outLabelIndices, outLabelProbabilities, blackandwhite, r0g1b2, image); for (int i=0; i < outLabelCount; i++) { System.out.println(""+i+": "+outLabelIndices[i]+"="+outLabelProbabilities[i]); } // release resources; model.releaseResources(); model.assignNulls(); System.out.println("Done."); } catch (Throwable e1) { e1.printStackTrace(); } } } M-CNN Multi-value Output Regression ExampleThe following code shows how M-CNN multivalue output regression is called. You can control the number of CPU threads to use. Normally it is equal to the number of CPU cores or CPU hyper threads. Note that CMSRModel is the model class that CMSR Studio generates. import java.io.File; import java.io.FileInputStream; public class Eval1 { public static void main(String[] args) { String filebase = "C:\\YourPath...\\"; String gpudevicename = "Intel(R) HD Graphics 5500"; // null for the fist GPU String gpukernelfile = filebase+"CNNoclForward.cl"; // for OpenCL String gpukernelfile = filebase+"CNNcudaForward.ptx"; // for Cuda String modeltype, modelfilepath; int[][][] image; boolean blackandwhite = false; boolean r0g1b2 = true; CMSRModel model = new CMSRModel(); try { modeltype = "MCNN"; modelfilepath = filebase+"\\modelfile."+modeltype.toLowerCase(); // initalize resources; model.initializeGpuAndModel(gpudevicename, new FileInputStream(gpukernelfile), new FileInputStream(modelfilepath)); // repeat the followings as needed. can get image from camera; File testimagefile = new File(filebase+modeltype+"images.jpg"); image = getRGBArray(testimagefile, model.input_image_width, model.input_image_height); int outLabelCount = model.getClassCount(); float[] outLabelProbabilities = new float[outLabelCount]; model.evaluate(outLabelProbabilities, blackandwhite, r0g1b2, image); for (int i=0; i < outLabelCount; i++) { System.out.println(""+i+": "+outLabelProbabilities[i]); } // release resources; model.releaseResources(); model.assignNulls(); System.out.println("Done."); } catch (Throwable e1) { e1.printStackTrace(); } } } OD-CNN Object Detection ExampleThe following code shows how OD-CNN object detection is called. You can control the number of CPU threads to use. Normally it is equal to the number of CPU cores or CPU hyper threads. Note that CMSRModel is the model class that CMSR Studio generates. The X/Y coordinates are the center locations of detected object bounding boxes. Output width and height are relative size where whole size is 1.0. import java.io.File; import java.io.FileInputStream; public class Eval1 { public static void main(String[] args) { String filebase = "C:\\YourPath...\\"; String gpudevicename = "Intel(R) HD Graphics 5500"; // null for the fist GPU String gpukernelfile = filebase+"CNNoclForward.cl"; // for OpenCL String gpukernelfile = filebase+"CNNcudaForward.ptx"; // for Cuda String modeltype, modelfilepath; int[][][] image; boolean blackandwhite = false; boolean r0g1b2 = true; CMSRModel model = new CMSRModel(); try { modeltype = "ODCNN"; modelfilepath = filebase+"\\modelfile."+modeltype.toLowerCase(); // initialize resources; model.initializeGpuAndModel(gpudevicename, new FileInputStream(gpukernelfile), new FileInputStream(modelfilepath)); // repeat the followings as needed. can get image from camera. File testimagefile = new File(filebase+modeltype+"images.jpg"); image = getRGBArray(testimagefile, model.input_image_width, model.input_image_height); int maxOutCount = 10; int[] outClassIndex = new int[maxOutCount+1]; // notice extra 1 float[] outClassProbability = new float[maxOutCount+1]; // notice extra 1 float[] outX = new float[maxOutCount+1]; // notice extra 1 float[] outY = new float[maxOutCount+1]; // notice extra 1 float[] outWidth = new float[maxOutCount+1]; // notice extra 1 float[] outHeight = new float[maxOutCount+1]; // notice extra 1 int outcount = model.evaluate(maxOutCount, outClassIndex, outClassProbability, outX, outY, outWidth, outHeight, blackandwhite, r0g1b2, image); for (int i=0; i < outcount; i++) { System.out.println(""+i+": "+ outClassIndex[i]+"="+outClassProbability[i]+"/"+ outX[i]+"="+outY[i]+"/"+ outWidth[i]+"="+outHeight[i] ); } // release resources; model.releaseResources(); model.assignNulls(); System.out.println("Done."); } catch (Throwable e1) { e1.printStackTrace(); } } } T-CNN Similarity Regression/Face Recognition ExampleThe following code shows how T-CNN similarity regression is called. You can control the number of CPU threads to use. Normally it is equal to the number of CPU cores or CPU hyper threads. Note that CMSRModel is the model class that CMSR Studio generates. import java.io.File; import java.io.FileInputStream; public class Eval1 { public static void main(String[] args) { String filebase = "C:\\YourPath...\\"; String gpudevicename = "Intel(R) HD Graphics 5500"; // null for the fist GPU String gpukernelfile = filebase+"CNNoclForward.cl"; // for OpenCL String gpukernelfile = filebase+"CNNcudaForward.ptx"; // for Cuda String modeltype, modelfilepath; int[][][] image; boolean blackandwhite = false; boolean r0g1b2 = true; CMSRModel model = new CMSRModel(); try { modeltype = "TCNN"; modelfilepath = filebase+"\\modelfile."+modeltype.toLowerCase(); // initialize resources; model.initializeGpuAndModel(gpudevicename, new FileInputStream(gpukernelfile), new FileInputStream(modelfilepath)); // repeat the followings as needed. can get images from camera. File testimagefile1 = new File(filebase+modeltype+"images1.jpg"); File testimagefile2 = new File(filebase+modeltype+"images2.jpg"); image = getRGBArray(testimagefile1, model.input_image_width, model.input_image_height); int[][][] image2 = getRGBArray(testimagefile2, model.input_image_width, model.input_image_height); float f = model.evaluate(blackandwhite, r0g1b2, image, image2); System.out.println("value="+f); // release resources; model.releaseResources(); model.assignNulls(); System.out.println("Done."); } catch (Throwable e1) { e1.printStackTrace(); } } } |
|||