Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
saksman authored Feb 16, 2022
1 parent 73c163b commit fdee084
Show file tree
Hide file tree
Showing 36 changed files with 23,701 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Alzayat Saleh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions active_learning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
''' Active learning wrapper model for pytoch Models.
Implement function get_active_learning_layers() and pass your model to constructor of
the ActiveLearning class.
'''
import torch
import torch.nn as nn
import torch.nn.functional as F


class ActiveLearning(nn.Module):

def __init__(self, base_model, global_avg_pool_size=1, fc_width=128):
super(ActiveLearning, self).__init__()
self.base_model = base_model
self.channel_counts = base_model.get_active_learning_feature_channel_counts()
self.fc = []
for channels in self.channel_counts:
self.fc.append(nn.Linear(
channels * global_avg_pool_size * global_avg_pool_size, fc_width))

self.fc = nn.ModuleList(self.fc)
self.loss_pred = nn.Linear(len(self.channel_counts)*fc_width,1)
self.avgpool = nn.AdaptiveAvgPool2d((global_avg_pool_size, global_avg_pool_size))

def forward(self, x, detach_lp = False):
out_p = self.base_model.forward(x)
active_learning_features = self.base_model.get_active_learning_features()
fc_outputs = []
for idx, features in enumerate(active_learning_features):
if detach_lp:
features = features.detach()
out = self.avgpool(features)
out = torch.flatten(out, 1)
out = self.fc[idx](out)
fc_outputs.append(F.relu(out))
fc_cat = torch.cat(fc_outputs, dim=1)
loss_pred = self.loss_pred(fc_cat)
return out_p, loss_pred

# Used for segmentation with DRN only.
def optim_parameters(self):
for param in self.base_model.optim_parameters():
yield param
for param in self.fc.parameters():
yield param
for param in self.loss_pred.parameters():
yield param

Loading

0 comments on commit fdee084

Please sign in to comment.