使用__setattr__()方法魔改python

setattr()在属性赋值时被调用,并且将值存储在实例字典中,这个字典应该是self的__dict__属性。即:在类实例的每个属性进行赋值时,都会首先调用哦__setattr__()方法,并在__setattr__()方法中将属性名和属性值添加到类实例的__dict__属性中。
1、实例属性管理__dict__
下面的测试代码中定义了三个实例属性,每个实例属性注册后都print()此时的__dict__

class AnotherFun:
    def __init__(self):
        self.name = "Liu"
        print(self.__dict__)
        self.age = 12
        print(self.__dict__)
        self.male = True
        print(self.__dict__)
another_fun = AnotherFun()

得出结果显示出,每次实例属性赋值时,都会将属性名和对应值存储到__dict__字典中
2、setattr()与__dict__
由于每次类实例进行属性赋值都会调用__setattr__(),所以可以重载__setattr__()方法,来动态的观察每次实例属性赋值时__dict__()的变化。下面的Fun类重载了__setattr__()方法,并且将实例的属性和属性值作为__dict__的键值对。

class Fun:
    def __init__(self):
        self.name = "Liu"
        self.age = 12
        self.male = True
        
    def __setattr__(self, key, value):
        print("*"*50)
        print("setting:{},  with:{}".format(key[], value))
        print("current __dict__ : {}".format(self.__dict__))
        # 属性注册
        self.__dict__[key] = value
fun = Fun()    

通过在__setattr__()中将属性名作为key,并将属性值作为value,添加到了__dict__中,得到的结果如下:

**************************************************
setting:name,  with:Liu
current __dict__ : {}
**************************************************
setting:age,  with:12
current __dict__ : {'name': 'Liu'}
**************************************************
setting:male,  with:True
current __dict__ : {'name': 'Liu', 'age': 12}

可以看出,init()中三个属性赋值时,每次都会调用一次__setattr__()函数。
3、重载__setattr__()必须谨慎
由于__setattr__()负责在__dict__中对属性进行注册,所以自己在重载时必须进行属性注册过程,下面是__setattr__()不进行属性注册的例子:

class NotFun:
    def __init__(self):
        self.name = "Liu"
        self.age = 12
        self.male = True
    
    def __setattr__(self, key, value):
        pass
not_fun = NotFun()
print(not_fun.name)

由于__setattr__中并没有将属性注册到__dict__中,所以not_fun对象并没有name属性,因此最后的print(not_fun.name)会报出属性不存在的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-21-6158d7aaef71> in <module>()
      8         pass
      9 not_fun = NotFun()
---> 10 print(not_fun.name)

AttributeError: 'NotFun' object has no attribute 'name'

所以重载__setattr__时必须要考虑是否在__dict__中进行属性注册。
总结
python的实例属性的定义、获取和管理可以通过__setattr__()和__dict__配合进行,当然还有对应的__getattr__()方法,本文暂时不做分析。

### 如何自定义修ResNet50网络结构 #### 修ResNet50的输入尺寸 对于不同应用场景,可能需要调整ResNet50接受的不同大小图像作为输入。这通常不需要变模型内部架构,在数据预处理阶段完成即可。 ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.Resize((new_height, new_width)), # 调整到所需的高度宽度 transforms.ToTensor(), ]) ``` #### 更输出类别数量 当目标不是原始ImageNet中的1000类时,需重新设置最后一层全连接层的输出节点数来匹配新的分类数目[^3]。 ```python from torchvision import models model_ft = models.resnet50(pretrained=True) num_ftrs = model_ft.fc.in_features model_ft.fc = nn.Linear(num_ftrs, number_of_classes) # 替换为所需的类别数 ``` #### 添加或删除卷积模块 如果希望增加或减少某些特定类型的特征提取能力,则可以在适当位置插入额外的卷积块或是移除现有的一些。需要注意的是这样做可能会破坏原有的权重初始化策略以及预训练参数的有效性。 ```python class CustomResNet(models.ResNet): def __init__(self, block, layers, num_classes=1000): super(CustomResNet, self).__init__(block, layers) # 增加一个新的layer4之后的conv layer self.extra_conv = nn.Conv2d(2048, 256, kernel_size=(1, 1), stride=(1, 1)) def forward(self, x): ... x = F.relu(self.extra_conv(x)) # 应用于原layer4后的feature map上 ... ``` #### 使用不同的激活函数替换ReLU 有时为了探索更好的性能表现,可以用其他形式如Leaky ReLU、PReLU等替代传统的ReLU激活单元。 ```python for module in model.modules(): if isinstance(module, nn.ReLU): module(inplace=False).apply(lambda m: setattr(m, 'inplace', False)).\ apply(lambda m: setattr(m, '_get_name()', "LeakyReLU")).\ apply(lambda m: setattr(m, 'negative_slope', 0.01)) ``` #### 自定义损失函数 除了交叉熵之外还可以尝试诸如Focal Loss之类的专为目标检测设计的损失计算方式;或者是针对多标签分类任务采用BCEWithLogitsLoss代替默认选项。 ```python criterion = nn.BCEWithLogitsLoss() # 或者更复杂的定制化loss function def custom_loss(output, target): loss = some_complex_calculation_based_on_output_and_target(output, target) return loss ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值