Java 从deeplearning4j层提取特征

Java 从deeplearning4j层提取特征,java,multidimensional-array,neural-network,deep-learning,deeplearning4j,Java,Multidimensional Array,Neural Network,Deep Learning,Deeplearning4j,我试图提取图层激活,将其保存为本地功能。 我还是CNNs的新手,所以我想展示一下我所做的,我想知道我所做的是否正确: public static void main(String[] args) throws IOException { ComputationGraph vgg16transfer = getComputationGraph(); for (File file : new File(ImageClassifier.class.getClassLoader().g

我试图提取图层激活,将其保存为本地功能。 我还是CNNs的新手,所以我想展示一下我所做的,我想知道我所做的是否正确:

public static void main(String[] args) throws IOException {
    ComputationGraph vgg16transfer = getComputationGraph();

    for (File file : new File(ImageClassifier.class.getClassLoader().getResource("mydirectory").getFile()).listFiles()) {
        Map<String, INDArray> stringINDArrayMap = extractTwo(file, vgg16transfer);
        //Extract the features from the last fully connected layers
        saveCompressed(file,stringINDArrayMap.get("fc2"));
    }
}

/**
 * Retrieves the VGG16 computation graph
 * @return ComputationGraph from the pretrained VGG16
 * @throws IOException
 */
public static ComputationGraph getComputationGraph() throws IOException {
    ZooModel zooModel = new VGG16();
    return (ComputationGraph) zooModel.initPretrained(PretrainedType.IMAGENET);
}

/**
 * Compresses the input INDArray and writes it to file
 * @param imageFile the original image file
 * @param array INDArray to be saved (features)
 * @throws IOException
 */
private static void saveCompressed(File imageFile, INDArray array) throws IOException {
    INDArray compress = BasicNDArrayCompressor.getInstance().compress(array);
    Nd4j.write(compress,new DataOutputStream(new FileOutputStream(new File("features/" + imageFile.getName()+ "feat"))));
}

/**
 * Given an input image and a ComputationGraph it calls the feedForward method after rescaling the image.
 * @param imageFile the image whose features need to be extracted 
 * @param vgg16 the ComputationGraph to be used.
 * @return a map of activations for each layer
 * @throws IOException
 */
public static Map<String, INDArray> extractTwo(File imageFile, ComputationGraph vgg16) throws IOException {
    // Convert file to INDArray
    NativeImageLoader loader = new NativeImageLoader(224, 224, 3);
    INDArray image = loader.asMatrix(imageFile);

    // Mean subtraction pre-processing step for VGG
    DataNormalization scaler = new VGG16ImagePreProcessor();
    scaler.transform(image);

    //Call the feedForward method to get a map of activations for each layer
    return vgg16.feedForward(image, false);
}
publicstaticvoidmain(字符串[]args)引发IOException{
ComputationGraph vgg16transfer=getComputationGraph();
对于(文件:新文件(ImageClassifier.class.getClassLoader().getResource(“mydirectory”).getFile()).listFiles()){
映射StringInArrayMap=extractTwo(文件,vgg16transfer);
//从最后完全连接的图层中提取特征
saveCompressed(文件,stringINDArrayMap.get(“fc2”);
}
}
/**
*检索VGG16计算图
*@来自预训练VGG16的返回图
*@抛出异常
*/
公共静态计算图getComputationGraph()引发IOException{
ZooModel ZooModel=新VGG16();
返回(计算图)zooModel.initPretrained(PretrainedType.IMAGENET);
}
/**
*压缩输入数组并将其写入文件
*@param imageFile原始图像文件
*@param数组INDArray要保存(功能)
*@抛出异常
*/
私有静态void saveCompressed(文件imageFile,INDArray数组)引发IOException{
INDArray compress=BasicNDArrayCompressor.getInstance().compress(数组);
write(压缩,新数据输出流(新文件输出流(新文件(“features/”+imageFile.getName()+“feat”))));
}
/**
*给定输入图像和计算图,它在重新缩放图像后调用前馈方法。
*@param imageFile需要提取其特征的图像
*@param vgg16要使用的计算图。
*@返回每个层的激活地图
*@抛出异常
*/
公共静态映射extractwo(文件imageFile,计算图vgg16)引发IOException{
//将文件转换为索引数组
NativeImageLoader=新的NativeImageLoader(2242243);
INDArray image=loader.asMatrix(imageFile);
//VGG的平均减法预处理步骤
DataNormalization scaler=新的VGG16ImagePreProcessor();
缩放变换(图像);
//调用前馈方法获取每个层的激活图
返回vgg16.前馈(图像,假);
}
所以基本上我调用前馈方法并从fc2层获得激活

关于这一点,我有几个问题:

1) 我写的代码是否确实提取了可以保存和存储以供进一步使用的功能

2) 如何对提取的特征进行PCA/美白

3) 是否有任何方法可以按照建议将其编码到VLAD,但该文件:

4) 然后我想比较保存的特征,我使用了一个简单的欧几里德距离,虽然结果不是最好的,但它似乎是有效的。我是否应该进行某种预处理,或者保存的功能是否可以直接比较


谢谢。

对于提取的特征,Nd4j有一个PCA,您可以使用:

不过这有点多余。我会直接考虑使用转移学习。您不必手动执行这些操作。 有关更多示例,请参见此处的文档:


迁移学习api将为您提供使用预训练模型所需的功能,同时仅修改它们以使用不同的输出层。在上面的示例中,我们甚至用VGG16介绍了这一点。

请在DL4J支持频道上询问您的问题:gitter.im/deeplearning4j/deeplearning4j