Skip to content

Commit

Permalink
server activations properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Gibson committed Jul 13, 2015
1 parent eff7c78 commit 4209192
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,10 @@ public void invoke() {
public void iterationDone(Model model, int iteration) {
if(iteration % this.iteration == 0) {
Layer l = (Layer) model;
INDArray weights = Transforms.sigmoid(l.activationMean());
if(weights.rank() < 4) {
if(weights.rank() == 2) {
weights = weights.reshape(1,1, weights.rows(), weights.columns());
}
else if(weights.rank() == 3) {
weights = weights.reshape(Ints.concat(new int[]{1}, weights.shape()));
}
}
INDArray activationMean = l.activate();
INDArray weights = Transforms.sigmoid(activationMean);



BufferedImage image = ImageLoader.toImage(weights);
try {
ImageIO.write(image, "png", outputFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package org.deeplearning4j.util;

import org.deeplearning4j.plot.FilterRenderer;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.ops.transforms.Transforms;
import org.nd4j.linalg.util.ArrayUtil;

import java.awt.*;
Expand Down Expand Up @@ -135,16 +135,17 @@ public static BufferedImage toBufferedImageRGB(INDArray arr) {
* @return
*/
public static BufferedImage toImage(INDArray matrix) {
BufferedImage img = new BufferedImage(matrix.size(-2), matrix.size(-1), BufferedImage.TYPE_INT_ARGB);
BufferedImage img = new BufferedImage(matrix.size(-1), matrix.size(-2), BufferedImage.TYPE_INT_RGB);
if(matrix.isMatrix()) {
INDArray toRound = matrix;
WritableRaster r = img.getRaster();
int[] equiv = new int[matrix.length()];
for(int i = 0; i < equiv.length; i++) {
equiv[i] = (int) matrix.getScalar(i).getDouble(i);
equiv[i] = (int) toRound.linearView().getDouble(i) * 256;
}


r.setDataElements(0,0,matrix.rows(),matrix.columns(),equiv);
r.setDataElements(0,0,matrix.columns(),matrix.rows(),equiv);
}

else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

package org.deeplearning4j.ui.activation;

import org.apache.commons.compress.utils.IOUtils;

import javax.activation.MimetypesFileTypeMap;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import javax.ws.rs.core.StreamingOutput;
import java.io.*;
import java.util.Collections;

/**
Expand All @@ -33,7 +36,7 @@
@Path("/activations")
@Produces(MediaType.TEXT_HTML)
public class ActivationsResource {
private String imagePath = "render.png";
private String imagePath = "activations.png";

@GET
public RenderView get() {
Expand All @@ -57,14 +60,24 @@ public Response image() {
throw new WebApplicationException(404);
}

File f = new File(imagePath);
final File f = new File(imagePath);

if (!f.exists()) {
throw new WebApplicationException(404);
}

String mt = new MimetypesFileTypeMap().getContentType(f);
return Response.ok(f, mt).build();
return Response.ok().entity(new StreamingOutput(){
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
FileInputStream fis = new FileInputStream(f);
byte[] bytes = IOUtils.toByteArray(fis);
fis.close();
output.write(bytes);
output.flush();


}
}).build();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
},3000);
</script>
<style type="text/css">
#embed {
background: #2F4F4F;
}
</style>
</head>

Expand Down

0 comments on commit 4209192

Please sign in to comment.