machine-learning - Pytorch,预训练模型:如何同时使用特征和分类器
问题描述
我正在使用 vgg16 提取图像特征向量。我想从倒数第二层获得 1 1 4096 向量。
我的代码:
def get_model():
model = models.vgg16(pretrained=True)#.features[:].classifier[:4]
model = model.eval()
# model.cuda() # send the model to GPU, DO NOT include this line if you haven't a GPU
return model
但我只能从最后一层得到 1 1 1000 个向量。
我知道如何使用feathers
和classifier
,但我不知道如何同时使用它们。
仅使用分类器:
仅使用羽毛:
同时使用它们:
日志:
Traceback (most recent call last):
File "/mnt/c/Users/sunji/PycharmProjects/image_cluster_pytorch/main.py", line 7, in <module>
model = calc.get_model()
File "/mnt/c/Users/sunji/PycharmProjects/image_cluster_pytorch/imagecluster/calc.py", line 17, in get_model
model = models.vgg16(pretrained=True).features[:].classifier[:4]
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 771, in __getattr__
raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.ModuleAttributeError: 'Sequential' object has no attribute 'classifier'
解决方案
推荐阅读
- python - 无法将符号 Keras 输入/输出转换为简单 CNN 中的 numpy 数组
- r - 如何选择第一个实例,每个人,每个条件?
- excel - 带有 Vlookup 的验证下拉列表
- laravel - Laravel vue惯性创建分页搜索时出错
- asp.net-core - .Net Core 3.1 EdmModel的Json序列化
- python - 当我在特定物理位置执行我的代码时,会发生错误(声音设备库)
- algorithm - 以最少的迭代次数找到所需方向的算法
- javascript - Page refreshes every 2 mins instead of 20 mins of inactivity
- android - 在 Android Studio Flutter YAML 文件中设置私有仓库的版本
- sharepoint-online - 如何自定义共享点在线超级菜单