use of java.awt.image.BufferedImage in project h2o-3 by h2oai.
the class GenModel method img2pixels.
public static void img2pixels(BufferedImage img, int w, int h, int channels, float[] pixels, int start, float[] mean) throws IOException {
// resize the image
BufferedImage scaledImg = new BufferedImage(w, h, img.getType());
Graphics2D g2d = scaledImg.createGraphics();
g2d.drawImage(img, 0, 0, w, h, null);
g2d.dispose();
int r_idx = start;
int g_idx = r_idx + w * h;
int b_idx = g_idx + w * h;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Color mycolor = new Color(scaledImg.getRGB(j, i));
int red = mycolor.getRed();
int green = mycolor.getGreen();
int blue = mycolor.getBlue();
if (channels == 1) {
pixels[r_idx] = (red + green + blue) / 3;
if (mean != null) {
pixels[r_idx] -= mean[r_idx];
}
} else {
pixels[r_idx] = red;
pixels[g_idx] = green;
pixels[b_idx] = blue;
if (mean != null) {
pixels[r_idx] -= mean[r_idx - start];
pixels[g_idx] -= mean[g_idx - start];
pixels[b_idx] -= mean[b_idx - start];
}
}
r_idx++;
g_idx++;
b_idx++;
}
}
}
use of java.awt.image.BufferedImage in project h2o-2 by h2oai.
the class DeepLearningVisualization method paint.
@Override
public void paint(Graphics g) {
Neurons layer = _neurons[_level];
int edge = 56, pad = 10;
final int EDGE = (int) Math.ceil(Math.sqrt(layer._previous._a.size()));
assert (layer._previous._a.size() <= EDGE * EDGE);
int offset = pad;
int buf = EDGE + pad + pad;
double mean = 0;
long n = layer._w.size();
for (int i = 0; i < n; i++) mean += layer._w.raw()[i];
mean /= layer._w.size();
double sigma = 0;
for (int i = 0; i < layer._w.size(); i++) {
double d = layer._w.raw()[i] - mean;
sigma += d * d;
}
sigma = Math.sqrt(sigma / (layer._w.size() - 1));
for (int o = 0; o < layer._a.size(); o++) {
if (o % 10 == 0) {
offset = pad;
buf += pad + edge;
}
int[] pic = new int[EDGE * EDGE];
for (int i = 0; i < layer._previous._a.size(); i++) {
double w = layer._w.get(o, i);
w = ((w - mean) / sigma) * 200;
if (w >= 0)
//GREEN
pic[i] = ((int) Math.min(+w, 255)) << 8;
else
//RED
pic[i] = ((int) Math.min(-w, 255)) << 16;
}
BufferedImage out = new BufferedImage(EDGE, EDGE, BufferedImage.TYPE_INT_RGB);
WritableRaster r = out.getRaster();
r.setDataElements(0, 0, EDGE, EDGE, pic);
BufferedImage resized = new BufferedImage(edge, edge, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resized.createGraphics();
try {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.clearRect(0, 0, edge, edge);
g2.drawImage(out, 0, 0, edge, edge, null);
} finally {
g2.dispose();
}
g.drawImage(resized, buf, offset, null);
offset += pad + edge;
}
}
use of java.awt.image.BufferedImage in project h2o-3 by h2oai.
the class DeepWaterMXNetIntegrationTest method inceptionPredictionMX.
// This test has nothing to do with H2O - Pure integration test of deepwater/backends/mxnet
@Test
public void inceptionPredictionMX() throws IOException {
for (boolean gpu : new boolean[] { true, false }) {
// Set model parameters
int w = 224, h = 224, channels = 3, nclasses = 1000;
ImageDataSet id = new ImageDataSet(w, h, channels, nclasses);
RuntimeOptions opts = new RuntimeOptions();
opts.setSeed(1234);
opts.setUseGPU(gpu);
BackendParams bparm = new BackendParams();
bparm.set("mini_batch_size", 1);
// Load the model
String path = "deepwater/backends/mxnet/models/Inception/";
BackendModel _model = backend.buildNet(id, opts, bparm, nclasses, StringUtils.expandPath(extractFile(path, "Inception_BN-symbol.json")));
backend.loadParam(_model, StringUtils.expandPath(extractFile(path, "Inception_BN-0039.params")));
water.fvec.Frame labels = parse_test_file(extractFile(path, "synset.txt"));
float[] mean = backend.loadMeanImage(_model, extractFile(path, "mean_224.nd"));
// Turn the image into a vector of the correct size
File imgFile = FileUtils.getFile("smalldata/deepwater/imagenet/test2.jpg");
BufferedImage img = ImageIO.read(imgFile);
BufferedImage scaledImg = new BufferedImage(w, h, img.getType());
Graphics2D g2d = scaledImg.createGraphics();
g2d.drawImage(img, 0, 0, w, h, null);
g2d.dispose();
float[] pixels = new float[w * h * channels];
int r_idx = 0;
int g_idx = r_idx + w * h;
int b_idx = g_idx + w * h;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Color mycolor = new Color(scaledImg.getRGB(j, i));
int red = mycolor.getRed();
int green = mycolor.getGreen();
int blue = mycolor.getBlue();
pixels[r_idx] = red - mean[r_idx];
r_idx++;
pixels[g_idx] = green - mean[g_idx];
g_idx++;
pixels[b_idx] = blue - mean[b_idx];
b_idx++;
}
}
float[] preds = backend.predict(_model, pixels);
int K = 5;
int[] topK = new int[K];
for (int i = 0; i < preds.length; i++) {
for (int j = 0; j < K; j++) {
if (preds[i] > preds[topK[j]]) {
topK[j] = i;
break;
}
}
}
// Display the top 5 predictions
StringBuilder sb = new StringBuilder();
sb.append("\nTop " + K + " predictions:\n");
BufferedString str = new BufferedString();
for (int j = 0; j < K; j++) {
String label = labels.anyVec().atStr(str, topK[j]).toString();
sb.append(" Score: " + String.format("%.4f", preds[topK[j]]) + "\t" + label + "\n");
}
System.out.println("\n\n" + sb.toString() + "\n\n");
Assert.assertTrue("Illegal predictions!", sb.toString().substring(40, 60).contains("Pembroke"));
labels.remove();
}
}
use of java.awt.image.BufferedImage in project smile by haifengl.
the class PlotPanel method save.
/**
* Exports the plot to an image file.
* @param file the destination file.
* @throws IOException if an error occurs during writing.
*/
public void save(File file) throws IOException {
BufferedImage bi = new BufferedImage(contentPane.getWidth(), contentPane.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
contentPane.printAll(g2d);
ImageIO.write(bi, FileChooser.getExtension(file), file);
}
use of java.awt.image.BufferedImage in project spring-security-oauth by spring-projects.
the class SparklrController method photo.
@RequestMapping("/sparklr/photos/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id, HttpServletRequest request) throws Exception {
InputStream photo = sparklrService.loadSparklrPhoto(id);
if (photo == null) {
throw new UnavailableException("The requested photo does not exist");
}
BufferedImage body;
MediaType contentType = MediaType.IMAGE_JPEG;
Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
if (imageReaders.hasNext()) {
ImageReader imageReader = imageReaders.next();
ImageReadParam irp = imageReader.getDefaultReadParam();
imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
body = imageReader.read(0, irp);
} else {
throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.IMAGE_JPEG));
return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Aggregations