public INDArray executeArray(ImageWritable image) throws IOException { Random random = null; if (seed != 0) { random = new Random(seed); } ImageWritable currentImage = image; for (ImageTransform transform : transformList) { currentImage = transform.transform(currentImage, random); } NativeImageLoader imageLoader = new NativeImageLoader(); return imageLoader.asMatrix(currentImage); }
public void asMatrixView(File f, INDArray view) throws IOException { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { asMatrixView(bis, view); } }
public ImageWritable transformFileUriToInput(URI uri) throws IOException { NativeImageLoader imageLoader = new NativeImageLoader(); ImageWritable img = imageLoader.asWritable(new File(uri)); return img; }
@Override public void initialize(Map<String, Param> params) throws TikaConfigException { //STEP 1: resolve weights file, download if necessary modelWeightsPath = mayBeDownloadFile(modelWeightsPath); //STEP 2: Load labels map try (InputStream stream = retrieveResource(mayBeDownloadFile(labelFile))) { this.labelMap = loadClassIndex(stream); } catch (IOException | ParseException e) { LOG.error("Could not load labels map", e); return; } //STEP 3: initialize the graph try { this.imageLoader = new NativeImageLoader(imgHeight, imgWidth, imgChannels); LOG.info("Going to load Inception network..."); long st = System.currentTimeMillis(); KerasModelBuilder builder = new KerasModel().modelBuilder().modelHdf5Filename(modelWeightsPath) .enforceTrainingConfig(false); builder.inputShape(new int[]{imgHeight, imgWidth, 3}); KerasModel model = builder.buildModel(); this.graph = model.getComputationGraph(); long time = System.currentTimeMillis() - st; LOG.info("Loaded the Inception model. Time taken={}ms", time); } catch (IOException | InvalidKerasConfigurationException | UnsupportedKerasConfigurationException e) { throw new TikaConfigException(e.getMessage(), e); } }
@Override public List<RecognisedObject> recognise(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { INDArray image = imageLoader.asMatrix(stream); preProcessor.transform(image); INDArray[] output = model.output(false, image); return predict(output[0]); } private List<RecognisedObject> predict(INDArray predictions)
@Override public List<Writable> next(int num) { if (imageLoader == null) { imageLoader = new NativeImageLoader(height, width, channels, imageTransform); for (int i = 0; i < cnt; i++) { try { ((NativeImageLoader) imageLoader).asMatrixView(currBatch.get(i), features.tensorAlongDimension(i, 1, 2, 3)); } catch (Exception e) {
NativeImageLoader imageLoader = new NativeImageLoader(); CanvasFrame frame = new CanvasFrame("RedBloodCellDetection"); OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat(); log.info(file.getName() + ": " + objs); Mat mat = imageLoader.asMat(features); Mat convertedMat = new Mat(); mat.convertTo(convertedMat, CV_8U, 255, 0);
@Override public INDArray asMatrix(InputStream is) throws IOException { byte[] bytes = IOUtils.toByteArray(is); Mat image = imdecode(new Mat(bytes), CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if (image == null || image.empty()) { PIX pix = pixReadMem(bytes, bytes.length); if (pix == null) { throw new IOException("Could not decode image from input stream"); } image = convert(pix); pixDestroy(pix); } INDArray a = asMatrix(image); image.deallocate(); return a; }
public void asMatrixView(InputStream is, INDArray view) throws IOException { byte[] bytes = IOUtils.toByteArray(is); Mat image = imdecode(new Mat(bytes), CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if (image == null || image.empty()) { PIX pix = pixReadMem(bytes, bytes.length); if (pix == null) { throw new IOException("Could not decode image from input stream"); } image = convert(pix); pixDestroy(pix); } if (image == null) throw new RuntimeException(); asMatrixView(image, view); image.deallocate(); }
@Override protected List<List<Writable>> loadData(File file, InputStream inputStream) throws IOException { List<List<Writable>> record = new ArrayList<>(); try (FFmpegFrameGrabber fg = inputStream != null ? new FFmpegFrameGrabber(inputStream) : new FFmpegFrameGrabber(file)) { if (numFrames >= 1) { fg.start(); if (startFrame != 0) fg.setFrameNumber(startFrame); for (int i = startFrame; i < startFrame + numFrames; i++) { Frame grab = fg.grabImage(); record.add(RecordConverter.toRecord(imageLoader.asRowVector(converter.convert(grab)))); } } else { if (framesPerSecond < 1) throw new IllegalStateException("No frames or frame time intervals specified"); else { fg.start(); for (double i = 0; i < videoLength; i += framesPerSecond) { fg.setTimestamp(Math.round(i * 1000000L)); Frame grab = fg.grabImage(); record.add(RecordConverter.toRecord(imageLoader.asRowVector(converter.convert(grab)))); } } } } return record; }
@Override public void setConf(Configuration conf) { super.setConf(conf); converter = new OpenCVFrameConverter.ToMat(); imageLoader = new NativeImageLoader(rows, cols); }
@Override public List<RecognisedObject> recognise( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { INDArray image = preProcessImage(imageLoader.asMatrix(stream)); INDArray scores = graph.outputSingle(image); List<RecognisedObject> result = new ArrayList<>(); for (int i = 0; i < scores.length(); i++) { if (scores.getDouble(i) > minConfidence) { String label = labelMap.get(i); String id = i + ""; result.add(new RecognisedObject(label, labelLang, id, scores.getDouble(i))); LOG.debug("Found Object {}", label); } } return result; } }
private INDArray prepareImage(Mat file, int width, int height) throws IOException { NativeImageLoader loader = new NativeImageLoader(height, width, 3); ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1); INDArray indArray = loader.asMatrix(file); imagePreProcessingScaler.transform(indArray); return indArray; }
@Override public List<Writable> record(URI uri, DataInputStream dataInputStream) throws IOException { invokeListeners(uri); if (imageLoader == null) { imageLoader = new NativeImageLoader(height, width, channels, imageTransform); } INDArray row = imageLoader.asMatrix(dataInputStream); List<Writable> ret = RecordConverter.toRecord(row); if (appendLabel) ret.add(new IntWritable(labels.indexOf(getLabel(uri.getPath())))); return ret; }
@Override public INDArray asMatrix(File f) throws IOException { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { return asMatrix(bis); } }
.restoreMultiLayerNetworkAndNormalizer(new File("model/AlexNet.zip"), false); NativeImageLoader imageLoader = new NativeImageLoader(256, 256, 3); INDArray indArray = imageLoader.asMatrix(file); normalizer.transform(indArray);
@Override public void initialize(InputSplit split) throws IOException { if (imageLoader == null) { imageLoader = new NativeImageLoader(height, width, channels, imageTransform);
/** * Convert a file to a row vector * * @param f the image to convert * @return the flattened image * @throws IOException */ @Override public INDArray asRowVector(File f) throws IOException { return asMatrix(f).ravel(); }
NativeImageLoader imageLoader = new NativeImageLoader(height, width, channel); INDArray imgNdarray = imageLoader.asMatrix(imgFile);
@Override public void initialize(InputSplit split) throws IOException, InterruptedException { if (imageLoader == null) { imageLoader = new NativeImageLoader(height, width);