Lesson 20 official topic

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]

Sharing my implementation of Feature Extraction using Hooks Context Manager here:

def collect_feature(hook, mod, inp, outp): hook.feats = outp.clone()

target_layers = (18, 25)
ms = fc.L(vgg16)[target_layers]
with Hooks(ms, collect_feature) as hooks:
    x = normalize(content_im)
    x = vgg16(x)
    feats_hooks = [h.feats for h in hooks]
    print([f.shape for f in feats_hooks])

anyone who use mps gpu ??, I can’t do accelerated_cb in apple gpu what Can I do ?