I just found Jeremy’s fastai tutorial on youtube,it’s amazing and i learned a lot, thanks in advance.Btw, i’ve implemented the hooker part of features extraction in VGG16
### pytorch hook implement:
def calc_features_with_hooks(imgs, model, target_layers=(18, 25)):
# Normalize the images (assuming 'normalize' is a predefined function)
x = normalize(imgs)
# Container to hold the features from the target layers
features = []
# Function to be called by the hook, appends the output of the layer to 'features'
def hook_fn(module, input, output):
features.append(output.clone())
# Register hooks on the target layers
hooks = []
for layer_idx in target_layers:
layer = model[layer_idx]
hook = layer.register_forward_hook(hook_fn)
hooks.append(hook)
# Execute the forward pass (hooks will automatically capture the outputs)
model(x)
# Remove the hooks after use to prevent memory leak
for hook in hooks:
hook.remove()
return features
features = calc_features_with_hooks(content_im, vgg16, target_layers=(18, 25))
[f.shape for f in features]
### miniai hooker class implementation
def append_features(hook, module, inp, output):
if not hasattr(hook,'features'): hook.features =
hook.features.append(to_cpu(output.clone()))
def extract_features(imgs, model, target_layers=(18, 25)):
x = normalize(imgs)
feats = []
hooks = []
for layer_idx in target_layers:
layer = model[layer_idx]
hook = Hook(layer, append_features)
hooks.append(hook)
# Execute the forward pass (hooks will automatically capture the outputs)
model(x)
for h in hooks:
feats.append(h.features)
# Remove the hooks after use to prevent memory leak
for hook in hooks:
hook.remove()
return feats
features = extract_features(content_im, vgg16)
[feature[0].shape for feature in features]
### miniai Hooks objects like contextmanager implementation
target_layers=(18, 25)
layers = []
feats = []
x = normalize(content_im)
for layer_idx in target_layers:
layers.append(vgg16[layer_idx])
with Hooks(layers, append_features) as hooks:
# Execute the forward pass (hooks will automatically capture the outputs)
vgg16(x)
for h in hooks:
feats.append(h.features)
[feature[0].shape for feature in feats]