Skip to content

Instantly share code, notes, and snippets.

@tomthetrainer
Created November 14, 2016 23:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomthetrainer/218d4afc54be7928fc466441d68901c0 to your computer and use it in GitHub Desktop.
Save tomthetrainer/218d4afc54be7928fc466441d68901c0 to your computer and use it in GitHub Desktop.
package org.deeplearning4j.examples.dataExamples;
import org.datavec.image.loader.NativeImageLoader;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.util.ModelSerializer;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import java.io.File;
import java.util.Arrays;
import java.util.List;
/**
* Created by tomhanlon on 11/11/16.
*/
public class MnistImagePipelineLoadChooser {
private static Logger log = LoggerFactory.getLogger(MnistImagePipelineLoadChooser.class);
public static String fileChose(){
JFileChooser fc = new JFileChooser();
int ret = fc.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
String filename = file.getAbsolutePath();
return filename;
}
else {
return null;
}
}
public static void main(String[] args) throws Exception{
int height = 28;
int width = 28;
int channels = 1;
// recordReader.getLabels()
List<Integer> labelList = Arrays.asList(2,3,7,1,6,4,0,5,8,9);
// pop up file chooser
String filechose = fileChose().toString();
//LOAD NEURAL NETWORK
// Where to save model
File locationToSave = new File("trained_mnist_model.zip");
MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(locationToSave);
log.info("*********TEST YOUR IMAGE AGAINST SAVED NETWORK********");
// FileChose is a string we will need a file
File file = new File(filechose);
// Use NativeImageLoader to convert to numerical matrix
NativeImageLoader loader = new NativeImageLoader(height, width, channels);
// Get the image into an INDarray
INDArray image = loader.asMatrix(file);
// 0-255
// 0-1
DataNormalization scaler = new ImagePreProcessingScaler(0,1);
scaler.transform(image);
// Pass through to neural Net
INDArray output = model.output(image);
log.info("## The FILE CHOSEN WAS " + filechose);
log.info("## The Neural Nets Pediction ##");
log.info("## list of probabilities per label ##");
log.info("## List of Labels in Order## ");
log.info(output.toString());
log.info(labelList.toString());
}
}
@yilaguan
Copy link

Yes. you need to use the lastst version of dl4j-0.7.1. It is a bug in dl4j-0.6.0.

@Alek-dr
Copy link

Alek-dr commented Mar 29, 2017

Very strange output
o.d.e.d.MnistImagePipelineLoadChooser - [ �, �, �, �, �, �, �, �, �, �]
what's the matter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment