【Temporal Segment Networks】 pytorch代码总结

代码写的特别好,很有借鉴意义。链接如下:
[github]: https://github.com/yjxiong/tsn-pytorch

1. @property

class VideoRecord(object):
    def __init__(self, row):
        self._data = row
    @property  #@prproperty的用法 将一个类方法转变成一个类属性 
    #试图将该属性设为其他值,我们会引发一个AttributeError错误
    def path(self):
        return self._data[0]

    @property
    def num_frames(self):
        return int(self._data[1])

    @property   
    def label(self):
        return int(self._data[2])

2. np.multiply

if average_duration > 0: # np.multiply 的使用 np.multiply([0, 1, 2], x) = [0, x, 2x]
            offsets = np.multiply(list(range(self.num_segments)), average_duration) + randint(average_duration, size=self.num_segments)

3. topk

# 计算top1 top5正确率
# topk=(1,5)
def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res

4. getattr & setattr

if 'resnet' in base_model or 'vgg' in base_model:
            #getattr() 函数用于返回一个对象属性值。
            #torchvision.model pytorch 自带的复现模型 
            #这句话相当于 models.resnet101(pretrained=True)
            self.base_model = getattr(torchvision.models, base_model)(True)

5. Freeze BN layers

if self._enable_pbn:
    print("Freezing BatchNorm2D except the first one.")
    for m in self.base_model.modules():
        if isinstance(m, nn.BatchNorm2d):
            count += 1
            if count >= (2 if self._enable_pbn else 1):
                m.eval()
                # shutdown update in frozen mode
                m.weight.requires_grad = False
                m.bias.requires_grad = False
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
This book covers construction, exploration, analysis, and visualization of complex networks using NetworkX (a Python library), as well as several other Python modules, and Gephi, an interactive environment for network analysts. The book is not an introduction to Python. I assume that you already know the language, at least at the level of a freshman programming course. The book consists of five parts, each covering specific aspects of complex networks. Each part comes with one or more detailed case studies. Part I presents an overview of the main Python CNA modules: NetworkX, iGraph, graph-tool, and networkit. It then goes over the construction of very simple networks both programmatically (using NetworkX) and interactively (in Gephi), and it concludes by presenting a network of Wikipedia pages related to complex networks. In Part II, you’ll look into networks based on explicit relationships (such as social networks and communication networks). This part addresses advanced network construction and measurement techniques. The capstone case study—a network of “Panama papers”—illustrates possible money-laundering patterns in Central Asia. Networks based on spatial and temporal co-occurrences—such as semantic and product networks—are the subject of Part III. The third part also explores macroscopic and mesoscopic complex network structure. It paves the way to network-based cultural domain analysis and a marketing study of Sephora cosmetic products. If you cannot find any direct or indirect relationships between the items, but still would like to build a network of them, the contents of Part IV come to the rescue. You will learn how to find out if items are similar, and you will convert quantitative similarities into network edges. A network of psychological trauma types is one of the outcomes of the fourth part. The book concludes with Part V: directed networks with plenty of examples, including a network of qualitative adjectives that you could use in computer games or
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值