3
$\begingroup$

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]

enter image description here

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

$\endgroup$
1
  • $\begingroup$ Sorry why use Features stuff ? you are just computing Norm@Differences@fe@{img1,img2 } $\endgroup$
    – Fortsaint
    Nov 18 '18 at 15:27
2
$\begingroup$

Yes, there is a way.

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

enter image description here

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

fe = FeatureExtraction[imgs, net]

enter image description here

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

0.0268319

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – user5601
    Mar 19 '18 at 13:57
  • $\begingroup$ @user5601 EuclideanDistance. i.stack.imgur.com/ky1wD.png $\endgroup$ Mar 20 '18 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.