This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from x import task2 | |
| @task | |
| def my_task(): | |
| ... | |
| def relocate(task, context="."): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def cos_loss(input, target): | |
| return 1 - F.cosine_similarity(input, target).mean() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = models.resnet34(pretrained=True).cuda() | |
| # Freeze the base of the network and only train the new custom layers | |
| for param in model.parameters(): | |
| param.requires_grad = False | |
| p=0.1 | |
| model.fc = nn.Sequential(nn.BatchNorm1d(512), | |
| nn.Dropout(p), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| layer = 40 | |
| filter = 265 | |
| FV = FilterVisualizer(size=56, upscaling_steps=12, upscaling_factor=1.2) | |
| FV.visualize(layer, filter, blur=5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FilterVisualizer(): | |
| def __init__(self, size=56, upscaling_steps=12, upscaling_factor=1.2): | |
| self.size, self.upscaling_steps, self.upscaling_factor = size, upscaling_steps, upscaling_factor | |
| self.model = vgg16(pre=True).cuda().eval() | |
| set_trainable(self.model, False) | |
| def visualize(self, layer, filter, lr=0.1, opt_steps=20, blur=None): | |
| sz = self.size | |
| img = np.uint8(np.random.uniform(150, 180, (sz, sz, 3)))/255 # generate random image | |
| activations = SaveFeatures(list(self.model.children())[layer]) # register hook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SaveFeatures(): | |
| def __init__(self, module): | |
| self.hook = module.register_forward_hook(self.hook_fn) | |
| def hook_fn(self, module, input, output): | |
| self.features = torch.tensor(output,requires_grad=True).cuda() | |
| def close(self): | |
| self.hook.remove() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class YourCustomModel(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| # truncated base network, „True“ refers to pretrained | |
| self.backbone = nn.Sequential(*list(resnet34(True).children())[:8]) | |
| # and your custom layers | |
| self.features = nn.Sequential( | |
| self.backbone, | |
| # custom layers: |