예제 #1
0
    def __init__(
        self,
        n_classes,
        deno,
        in_out=None,
        feat_dim=None,
        graph_size=None,
        method='cos',
        sparsify=[0.8],
        non_lin='HT',
        aft_nonlin=None,
    ):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify

        if in_out is None:
            in_out = [2048, 64]

        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = len(sparsify)

        print 'NUM LAYERS', num_layers, in_out

        self.bn = None
        # nn.BatchNorm1d(2048, affine = False)

        # self.linear_layer = nn.Linear(feat_dim[0], feat_dim[1], bias = True)

        self.graph_layers = nn.ModuleList()

        # self.last_graph = nn.ModuleList()

        for num_layer in range(num_layers):
            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[0],
                                    n_out=in_out[1],
                                    non_lin=non_lin,
                                    method=method,
                                    aft_nonlin=aft_nonlin))

        last_graph = []
        if len(in_out) == 3:
            last_graph.append(nn.Linear(in_out[1] * num_layers, in_out[2]))
            last_graph.append(nn.Hardtanh())
            last_graph.append(nn.Dropout(0.5))
            last_graph.append(nn.Linear(in_out[-1], n_classes))
        else:
            last_graph.append(nn.Dropout(0.5))
            last_graph.append(nn.Linear(in_out[1] * num_layers, n_classes))
        self.last_graph = nn.Sequential(*last_graph)
        # self.last_graphs.append(last_graph)

        self.num_branches = num_layers
        print 'self.num_branches', self.num_branches
    def __init__(
        self,
        n_classes,
        deno,
        in_out=None,
        feat_dim=None,
        graph_size=None,
        method='cos',
        sparsify=[0.8],
        att=256,
        non_lin='HT',
        aft_nonlin=None,
    ):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify

        if in_out is None:
            in_out = [2048, 64]

        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = 1

        print 'NUM LAYERS', num_layers, in_out

        self.linear_layer = nn.Linear(feat_dim[0], feat_dim[1], bias=True)

        att_module = []
        att_module.append(nn.Linear(feat_dim[0], att, bias=True))
        att_module.append(nn.ReLU())
        att_module.append(nn.Linear(att, 1))
        att_module.append(nn.Sigmoid())
        self.att_module = nn.Sequential(*att_module)

        self.graph_layers = nn.ModuleList()

        self.last_graphs = nn.ModuleList()

        for num_layer in range(num_layers):
            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[0],
                                    n_out=in_out[1],
                                    non_lin=non_lin,
                                    method=method,
                                    aft_nonlin=aft_nonlin))

            last_graph = []
            last_graph.append(nn.Dropout(0.5))
            last_graph.append(nn.Linear(in_out[-1], n_classes))
            last_graph = nn.Sequential(*last_graph)
            self.last_graphs.append(last_graph)

        self.num_branches = num_layers
        print 'self.num_branches', self.num_branches
예제 #3
0
    def __init__(
        self,
        n_classes,
        deno,
        in_out=None,
        feat_dim=None,
        graph_size=None,
        method='cos',
        sparsify=False,
        non_lin='HT',
        gk=8,
        aft_nonlin=None,
    ):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify
        self.gk = gk
        if in_out is None:
            in_out = [2048, 64]
        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = len(in_out) - 1

        print 'NUM LAYERS', num_layers, in_out

        self.linear_layer = nn.Linear(feat_dim[0], feat_dim[1], bias=True)

        self.graph_layers = nn.ModuleList()
        for num_layer in range(num_layers):
            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[num_layer],
                                    n_out=in_out[num_layer + 1],
                                    non_lin=non_lin,
                                    method=method,
                                    aft_nonlin=aft_nonlin))

        last_graph = []

        last_graph.append(nn.Dropout(0.5))
        last_graph.append(nn.Linear(in_out[-1], n_classes))
        last_graph = nn.Sequential(*last_graph)
        self.last_graph = last_graph
예제 #4
0
    def __init__(self,
                 n_classes,
                 deno,
                 in_out=None,
                 feat_dim=None,
                 graph_size=None,
                 method='cos',
                 sparsify=False,
                 non_lin='HT',
                 normalize=[True, True]):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify

        if in_out is None:
            in_out = [2048, 64]
        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = len(in_out) - 1

        print 'NUM LAYERS', num_layers, in_out

        self.linear_layers = nn.ModuleList()
        self.linear_layers_after = nn.ModuleList()
        for idx_layer_num, layer_num in enumerate(range(num_layers)):

            if non_lin == 'HT':
                non_lin_curr = nn.Hardtanh()
            elif non_lin == 'RL':
                non_lin_curr = nn.ReLU()
            else:
                error_message = str('Non lin %s not valid', non_lin)
                raise ValueError(error_message)

            idx_curr = idx_layer_num * 2

            self.linear_layers.append(
                nn.Linear(feat_dim[idx_curr],
                          feat_dim[idx_curr + 1],
                          bias=False))

            last_linear = []
            last_linear.append(non_lin_curr)
            if normalize[0]:
                last_linear.append(Normalize())
            last_linear.append(nn.Dropout(0.5))
            last_linear.append(nn.Linear(feat_dim[idx_curr + 1], n_classes))
            last_linear = nn.Sequential(*last_linear)
            self.linear_layers_after.append(last_linear)

        self.graph_layers = nn.ModuleList()
        for num_layer in range(num_layers):
            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[num_layer],
                                    n_out=in_out[num_layer + 1],
                                    non_lin=non_lin,
                                    method=method))

        self.num_branches = num_layers + 1

        print 'self.num_branches', self.num_branches
예제 #5
0
    def __init__(self,
                 n_classes,
                 deno,
                 in_out=None,
                 feat_dim=None,
                 graph_size=None,
                 method='cos',
                 sparsify=[0.8],
                 non_lin='HT',
                 aft_nonlin=None,
                 sigmoid=False,
                 layer_bef=None,
                 num_graphs=1,
                 graph_sum=False):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify
        self.graph_sum = graph_sum
        assert (len(in_out) - 1) == num_graphs

        if in_out is None:
            in_out = [2048, 64]

        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = len(sparsify)

        print 'NUM LAYERS', num_layers, in_out

        self.bn = None
        # nn.BatchNorm1d(2048, affine = False)

        if layer_bef is None:
            self.layer_bef = None
        else:
            self.layer_bef = []
            self.layer_bef.append(
                nn.Linear(layer_bef[0], layer_bef[1], bias=True))
            self.layer_bef.append(nn.ReLU())
            # self.layer_bef.append(Normalize())
            self.layer_bef = nn.Sequential(*self.layer_bef)

        self.linear_layers = nn.ModuleList()
        # Linear(feat_dim[0], feat_dim[1], bias = True)

        self.graph_layers = nn.ModuleList()

        self.last_graphs = nn.ModuleList()

        for num_layer in range(num_layers):

            graph_pipe = nn.ModuleList()

            lin_pipe = nn.ModuleList()

            for graph_num in range(num_graphs):
                lin_pipe.append(
                    nn.Linear(in_out[graph_num], feat_dim[graph_num]))
                graph_pipe.append(
                    Graph_Layer_Wrapper(in_out[graph_num],
                                        n_out=in_out[graph_num + 1],
                                        non_lin=non_lin,
                                        method=method,
                                        aft_nonlin=aft_nonlin))

            self.linear_layers.append(lin_pipe)
            self.graph_layers.append(graph_pipe)
            # Graph_Layer_Wrapper(in_out[0],n_out = in_out[1], non_lin = non_lin, method = method, aft_nonlin = aft_nonlin))

            last_graph = []
            last_graph.append(nn.Dropout(0.5))
            last_graph.append(nn.Linear(in_out[-1], n_classes))
            if sigmoid:
                last_graph.append(nn.Sigmoid())
            last_graph = nn.Sequential(*last_graph)
            self.last_graphs.append(last_graph)

        self.num_branches = num_layers
        print 'self.num_branches', self.num_branches
    def __init__(self,
                 n_classes,
                 deno,
                 in_out=None,
                 feat_dim=None,
                 graph_size=None,
                 method='cos',
                 num_switch=1,
                 focus=0,
                 sparsify=False,
                 non_lin='HT',
                 normalize=[True, True]):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify

        if in_out is None:
            in_out = [2048, 64]
        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = len(in_out) - 1
        # non_lin = 'HT'

        print 'NUM LAYERS', num_layers, in_out

        # if pretrained=='ucf':
        #     model_file = '../experiments/just_mill_flexible_deno_8_n_classes_20_layer_sizes_2048_64_ucf/all_classes_False_just_primary_False_limit_500_cw_True_MultiCrossEntropy_100_step_100_0.1_0.001_0.001/model_99.pt'
        # elif pretrained=='activitynet':
        #     model_file = '../experiments/just_mill_flexible_deno_8_n_classes_100_layer_sizes_2048_64_activitynet/all_classes_False_just_primary_False_limit_500_cw_True_MultiCrossEntropy_50_step_50_0.1_0.001_0.001/model_49.pt'
        # elif pretrained=='random':
        #     model_file = '../experiments/just_mill_flexible_deno_8_n_classes_20_layer_sizes_2048_64_ucf/all_classes_False_just_primary_False_limit_500_cw_True_MultiCrossEntropy_100_step_100_0.1_0_0.001/model_99.pt'
        # elif pretrained=='default':
        #     model_file = None
        # else:
        #     error_message = 'Similarity method %s not valid' % method
        #     raise ValueError(error_message)

        # if model_file is not None:
        #     model_temp = torch.load(model_file)
        #     # self.linear_layer.weight.data = model_temp.linear.weight.data
        # else:
        #     print 'NO MODEL FILE AAAAAAAA'

        self.linear_layers = nn.ModuleList()
        for idx_layer_num, layer_num in enumerate(range(num_layers)):

            if non_lin == 'HT':
                non_lin_curr = nn.Hardtanh()
            elif non_lin == 'RL':
                non_lin_curr = nn.ReLU()
            else:
                error_message = str('Non lin %s not valid', non_lin)
                raise ValueError(error_message)

            last_linear = []
            idx_curr = idx_layer_num * 2
            last_linear.append(
                nn.Linear(feat_dim[idx_curr],
                          feat_dim[idx_curr + 1],
                          bias=False))
            last_linear.append(non_lin_curr)
            if normalize[0]:
                last_linear.append(Normalize())
            last_linear.append(nn.Dropout(0.5))
            last_linear.append(nn.Linear(feat_dim[idx_curr + 1], n_classes))
            last_linear = nn.Sequential(*last_linear)
            self.linear_layers.append(last_linear)

        self.graph_layers = nn.ModuleList()
        for num_layer in range(num_layers):
            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[num_layer],
                                    n_out=in_out[num_layer + 1],
                                    non_lin=non_lin,
                                    method=method))

        # last_linear = []
        # if non_lin =='HT':
        #     last_linear.append(nn.Hardtanh())
        # elif non_lin =='RL':
        #     last_linear.append(nn.ReLU())
        # else:
        #     error_message = str('Non lin %s not valid', non_lin)
        #     raise ValueError(error_message)

        # last_linear.append(nn.Dropout(0.5))
        # last_linear.append(nn.Linear(in_out[1],n_classes))
        # last_linear = nn.Sequential(*last_linear)
        # self.last_linear = last_linear

        last_graph = []
        if non_lin == 'HT':
            last_graph.append(nn.Hardtanh())
        elif non_lin == 'RL':
            last_graph.append(nn.ReLU())
        else:
            error_message = str('Non lin %s not valid', non_lin)
            raise ValueError(error_message)

        if normalize[1]:
            last_graph.append(Normalize())

        last_graph.append(nn.Dropout(0.5))
        last_graph.append(nn.Linear(in_out[-1], n_classes))
        last_graph = nn.Sequential(*last_graph)
        self.last_graph = last_graph

        self.num_branches = num_layers + 1

        if type(num_switch) == type(1):
            num_switch = [num_switch] * self.num_branches

        self.num_switch = num_switch
        self.epoch_counters = [0] * self.num_branches
        self.focus = focus
        self.epoch_last = 0

        print 'self.num_branches', self.num_branches
        print 'self.num_switch', self.num_switch
        print 'self.epoch_counters', self.epoch_counters
        print 'self.focus', self.focus
        print 'self.epoch_last', self.epoch_last
    def __init__(
        self,
        n_classes,
        deno,
        in_out=None,
        feat_dim=None,
        graph_size=None,
        method='cos',
        sparsify=False,
        non_lin='HT',
        normalize=[True, True],
        attention=False,
        gk=8,
        aft_nonlin=None,
    ):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify
        self.gk = gk
        if in_out is None:
            in_out = [2048, 64]
        # if feat_dim is None:
        #     feat_dim = [2048,64]

        num_layers = len(in_out) - 1

        print 'NUM LAYERS', num_layers, in_out

        self.bn = None
        # nn.BatchNorm1d(2048, affine = False)

        # self.linear_layers = nn.ModuleList()
        # self.linear_layers_after = nn.ModuleList()
        # for idx_layer_num,layer_num in enumerate(range(num_layers)):

        #     if non_lin =='HT':
        #         non_lin_curr = nn.Hardtanh()
        #     elif non_lin =='RL':
        #         non_lin_curr = nn.ReLU()
        #     else:
        #         error_message = str('Non lin %s not valid', non_lin)
        #         raise ValueError(error_message)

        #     idx_curr = idx_layer_num*2

        #     self.linear_layers.append(nn.Linear(feat_dim[idx_curr], feat_dim[idx_curr+1], bias = True))

        #     last_linear = []
        #     last_linear.append(non_lin_curr)
        #     if normalize[0]:
        #         last_linear.append(Normalize())
        #     last_linear.append(nn.Dropout(0.5))
        #     last_linear.append(nn.Linear(feat_dim[idx_curr+1],n_classes))
        #     last_linear = nn.Sequential(*last_linear)
        #     self.linear_layers_after.append(last_linear)

        self.graph_layers = nn.ModuleList()
        for num_layer in range(num_layers):
            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[num_layer],
                                    n_out=in_out[num_layer + 1],
                                    non_lin=non_lin,
                                    method=method,
                                    aft_nonlin=aft_nonlin))

        last_graph = []
        if aft_nonlin is None:
            if non_lin == 'HT':
                last_graph.append(nn.Hardtanh())
            elif non_lin == 'RL':
                last_graph.append(nn.ReLU())
            else:
                error_message = str('Non lin %s not valid', non_lin)
                raise ValueError(error_message)

            if normalize[1]:
                last_graph.append(Normalize())

        last_graph.append(nn.Dropout(0.5))
        last_graph.append(nn.Linear(in_out[-1], n_classes))
        last_graph = nn.Sequential(*last_graph)
        self.last_graph = last_graph

        self.num_branches = 1
        # num_layers+1
        self.attention = attention
        print 'self.num_branches', self.num_branches
    def __init__(self,
                 n_classes,
                 deno,
                 in_out=None,
                 feat_dim=None,
                 graph_size=None,
                 method='cos',
                 sparsify=[0.8],
                 non_lin='HT',
                 aft_nonlin=None,
                 sigmoid=False,
                 layer_bef=None,
                 graph_sum=False,
                 sameF=False):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify
        self.graph_sum = graph_sum
        self.sameF = sameF

        if in_out is None:
            in_out = [2048, 64]

        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = n_classes

        print 'NUM LAYERS', num_layers, in_out

        self.bn = None

        if layer_bef is None:
            self.layer_bef = None
        else:
            self.layer_bef = []
            self.layer_bef.append(
                nn.Linear(layer_bef[0], layer_bef[1], bias=True))
            self.layer_bef.append(nn.ReLU())
            # self.layer_bef.append(Normalize())
            self.layer_bef = nn.Sequential(*self.layer_bef)

        if sameF:
            self.linear_layers = nn.Linear(feat_dim[0], feat_dim[1], bias=True)
        else:
            self.linear_layers = nn.ModuleList()

        self.graph_layers = nn.ModuleList()

        self.last_graphs = nn.ModuleList()

        for num_layer in range(num_layers):
            if not sameF:
                self.linear_layers.append(
                    nn.Linear(feat_dim[0], feat_dim[1], bias=True))

            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[0],
                                    n_out=in_out[1],
                                    non_lin=non_lin,
                                    method=method,
                                    aft_nonlin=aft_nonlin))

            last_graph = []
            if in_out[-1] > 1:
                last_graph.append(nn.Dropout(0.5))
                last_graph.append(nn.Linear(in_out[-1], 1))
            if sigmoid:
                last_graph.append(nn.Sigmoid())
            last_graph = nn.Sequential(*last_graph)
            self.last_graphs.append(last_graph)

        self.num_branches = num_layers
        print 'self.num_branches', self.num_branches
예제 #9
0
    def __init__(
        self,
        n_classes,
        deno,
        in_out=None,
        feat_dim='100_all',
        graph_size=None,
        method='cos',
        sparsify=False,
        non_lin='HT',
        gk=8,
        aft_nonlin=None,
    ):
        super(Graph_Multi_Video_Cooc, self).__init__()

        self.num_classes = n_classes
        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify
        self.gk = gk

        if in_out is None:
            in_out = [2048, 64]

        feat_dim = feat_dim.split('_')
        dir_feat = os.path.join('../data/ucf101/kmeans/3_' + feat_dim[0],
                                'npy_labeled')
        feat_dim = feat_dim[1:]
        feat_dicts = []
        for feat_curr in feat_dim:
            if feat_curr == 'all':
                feat_dicts.append(os.path.join(dir_feat,
                                               'all_classes_mul.npy'))
            elif feat_curr == 'ind':
                for class_name in class_names:
                    feat_dicts.append(
                        os.path.join(dir_feat, class_name + '.npy'))
            else:
                # print os.path.join(dir_feat,feat_curr+'.npy')
                feat_dicts.append(os.path.join(dir_feat, feat_curr + '.npy'))

        # if feat_dim is None:
        #     feat_dim = [2048,64]

        num_layers = len(feat_dicts)
        # len(in_out)-1

        print 'NUM LAYERS', num_layers, in_out

        # self.linear_layer = nn.Linear(feat_dim[0], feat_dim[1], bias = True)

        self.graph_layers = nn.ModuleList()
        for num_layer in range(num_layers):

            # branch_curr = []
            self.graph_layers.append(
                Graph_Layer_Wrapper(in_out[num_layer],
                                    n_out=in_out[num_layer + 1],
                                    non_lin=non_lin,
                                    method=method,
                                    aft_nonlin=aft_nonlin,
                                    affinity_dict=feat_dicts[num_layer]))

            # self.graph_layers.append(branch_curr)

        self.num_branches = num_layers
        self.last_graphs = nn.ModuleList()
        for num_layer in range(num_layers):
            last_graph = []
            last_graph.append(nn.Dropout(0.5))
            last_graph.append(nn.Linear(in_out[-1], n_classes))
            last_graph = nn.Sequential(*last_graph)
            self.last_graphs.append(last_graph)
    def __init__(self,
                 n_classes,
                 deno,
                 in_out=None,
                 feat_dim=None,
                 graph_size=None,
                 method='cos',
                 sparsify=[0.8],
                 non_lin='HT',
                 aft_nonlin=None,
                 sigmoid=False,
                 layer_bef=None,
                 graph_sum=False,
                 background=False,
                 just_graph=False):
        super(Graph_Multi_Video, self).__init__()

        self.num_classes = n_classes
        self.background = background

        if self.background:
            assert sigmoid
            n_classes += 1

        self.deno = deno
        self.graph_size = graph_size
        self.sparsify = sparsify
        self.graph_sum = graph_sum
        self.just_graph = just_graph

        if in_out is None:
            in_out = [2048, 64]

        if feat_dim is None:
            feat_dim = [2048, 64]

        num_layers = len(sparsify)

        print 'NUM LAYERS', num_layers, in_out

        self.bn = None
        # nn.BatchNorm1d(2048, affine = False)
        self.linear_layer = nn.Linear(feat_dim[0], feat_dim[1], bias=True)

        if layer_bef is None:
            self.layer_bef = None
        else:
            self.layer_bef = []
            self.layer_bef.append(
                nn.Linear(layer_bef[0], layer_bef[1], bias=True))
            self.layer_bef.append(nn.ReLU())
            # self.layer_bef.append(Normalize())
            self.layer_bef = nn.Sequential(*self.layer_bef)

        self.graph_layers = nn.ModuleList()

        self.last_graphs = nn.ModuleList()

        for num_layer in range(num_layers):
            if self.sparsify[num_layer] == 'lin':
                lin_curr = []

                if non_lin == 'HT':
                    lin_curr.append(nn.Hardtanh())
                elif non_lin.lower() == 'rl':
                    lin_curr.append(nn.ReLU())
                elif non_lin is not None:
                    error_message = str('non_lin %s not recognized', non_lin)
                    raise ValueError(error_message)

                lin_curr.append(nn.Linear(in_out[0], in_out[1]))

                to_pend = aft_nonlin.split('_')
                for tp in to_pend:
                    if tp.lower() == 'ht':
                        lin_curr.append(nn.Hardtanh())
                    elif tp.lower() == 'rl':
                        lin_curr.append(nn.ReLU())
                    elif tp.lower() == 'l2':
                        lin_curr.append(Normalize())
                    elif tp.lower() == 'ln':
                        lin_curr.append(nn.LayerNorm(n_out))
                    elif tp.lower() == 'bn':
                        lin_curr.append(
                            nn.BatchNorm1d(n_out,
                                           affine=False,
                                           track_running_stats=False))
                    else:
                        error_message = str('non_lin %s not recognized',
                                            non_lin)
                        raise ValueError(error_message)
                lin_curr = nn.Sequential(*lin_curr)
                self.graph_layers.append(lin_curr)
            else:
                self.graph_layers.append(
                    Graph_Layer_Wrapper(in_out[0],
                                        n_out=in_out[1],
                                        non_lin=non_lin,
                                        method=method,
                                        aft_nonlin=aft_nonlin))

            if self.just_graph:
                if sigmoid:
                    aft_nonlin_curr = 'sig'
                else:
                    aft_nonlin_curr = None
                last_graph = Graph_Layer_Wrapper(in_out[-1],
                                                 n_classes,
                                                 non_lin=non_lin,
                                                 method=method,
                                                 aft_nonlin=aft_nonlin_curr)
            else:
                last_graph = []
                last_graph.append(nn.Dropout(0.5))
                last_graph.append(nn.Linear(in_out[-1], n_classes))
                if sigmoid:
                    last_graph.append(nn.Sigmoid())
                last_graph = nn.Sequential(*last_graph)
            self.last_graphs.append(last_graph)

        self.num_branches = num_layers
        print 'self.num_branches', self.num_branches