第1步：获取激活

`hook = hook_output(learn.model[0][7][2])`

第3步：展平

```def get_actns(learn:Learner, dl:DataLoader, hook:Hook, pool=AdaptiveConcatPool2d, pool_dim:int=4, train:bool=True):
"""Gets the activations at the layer specified by `hook`,
applies `pool` of dim `pool_dim` and concatenates."""```

```   pool = pool(pool_dim)

actns = []
learn.model.eval()
for i,(xb,yb) in enumerate(dl):
learn.model(xb)
actns.append((hook.stored).cpu())

return pool(torch.cat(actns)).view(len(dl.x), -1)```

第4步：计算相似之处

```def comb_similarity(t1: torch.Tensor, t2: torch.Tensor, sim_func=nn.CosineSimilarity(dim=0)):
"""Computes the similarity function `sim_func` between each embedding
of `t1` and `t2` matrices.
t1` and `t2` should have dimensions [n_embeddings, n_features]."""

self_sim = False
if torch.equal(t1, t2): self_sim = True

sims = np.zeros((t1.shape[0], t2.shape[0]))
for idx1 in range(t1.shape[0]):
for idx2 in range(t2.shape[0]):
if not self_sim or idx1>idx2:
ex1 = t1[idx1,:]
ex2 = t2[idx2,:]
sims[idx1][idx2] = sim_func(ex1,ex2)

return sims```

第五步：结果

（一个有签名）

`来自fastai.widgets进口*`

```ds，fns_idxs = DatasetFormatter.from_similars（'learner'）
ImageCleaner（ds，fns_idxs，duplicates = True）```

fastai库：

https://github.com/fastai/fastai

callbacks 功能：

https://docs.fast.ai/callbacks.hooks.html

Pooling :

http://deeplearning.stanford.edu/tutorial/supervised/Pooling/

Oxford-IIIT Pet Dataset :

http://www.robots.ox.ac.uk/~vgg/data/pets/

fastai的交互式小部件 :

https://github.com/fpingham/dataset-cleaner