Using NetGraph as FeatureExtractor?

I'd like to compute FeatureDistance between two images with a NetGraph:

rs = NetModel["ResNet-152 Trained on ImageNet Competition Data"];
fe = NetTake[rs, {1, -3}]


Ok, so this works:

FeatureNearest[imgs -> {"Index", "Distance"}, FeatureExtractor -> fe]


But then why doesn't this:

FeatureDistance[img1, img2, fe]


Is there a way to turn a NetGraph[] into a valid FeatureExtractorFunction[]?

• Sorry why use Features stuff ? you are just computing Norm@Differences@fe@{img1,img2 } Nov 18 '18 at 15:27

Yes, there is a way.

net = NetChain[
{ConvolutionLayer[32, {3, 3}], AggregationLayer[Mean]},
"Input" -> NetEncoder[{"Image", {64, 64}, ColorSpace -> "RGB"}]
] // NetInitialize


imgs = Table[RandomImage[1, {64, 64}, ColorSpace -> "RGB"], {20}];

fe = FeatureExtraction[imgs, net]


FeatureDistance[imgs[[1]], imgs[[2]], fe]


0.0268319

• Ok great. However there's something weird going on in FeatureDistance: fe = FeatureExtraction[imgs, NetTake[rs, {1, -3}]] FeatureDistance[ExampleData[{"TestImage", "Lena"}], ExampleData[{"TestImage", "Sailboat"}], fe] the distances are only 0 or 1... unless I set DistanceFunction -> CosineDistance. Do you know what FeatureDistance does by default? Mar 19 '18 at 13:57
• @user5601 EuclideanDistance. i.stack.imgur.com/ky1wD.png Mar 20 '18 at 8:02