【发布时间】:2018-07-04 15:29:14
【问题描述】:
我正在尝试提取层激活以将它们作为特征保存在本地。 我还是 CNN 的新手,所以我想展示我所做的,我想知道我所做的是否正确:
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);
}
所以基本上我是在调用 feedForward 方法并从 fc2 层获取激活。
我对此有几个问题:
1) 我编写的代码是否确实提取了可以保存和存储以供进一步使用的特征?
2) 我将如何对提取的特征进行 PCA/白化?
3) 有什么方法可以按照建议将其编码为 VLAD,但是这样的论文:https://arxiv.org/pdf/1707.00058.pdf
4)然后我想比较保存的特征,我使用简单的欧几里德距离进行比较,虽然结果不是最好的,但它似乎正在工作。有没有我应该做的某种预处理,或者保存的特征可以直接比较?
谢谢。
【问题讨论】:
-
请在 DL4J 支持频道提问:gitter.im/deeplearning4j/deeplearning4j
标签: java multidimensional-array neural-network deep-learning deeplearning4j