kaczmarj commited on
Commit
10eca26
1 Parent(s): a83c681

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +109 -0
README.md CHANGED
@@ -1,3 +1,112 @@
1
  ---
 
 
 
 
2
  license: bsd-3-clause
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ tags:
3
+ - image-classification
4
+ - pytorch
5
+ library_tag: pytorch
6
  license: bsd-3-clause
7
+ datasets:
8
+ - imagenet-1k
9
+ pipeline_tag: feature-extraction
10
  ---
11
+
12
+ # Model card for resnet50-truncated.tv_in1k
13
+
14
+ A truncated ResNet-50 feature extraction model, as used in [CLAM](https://www.nature.com/articles/s41551-020-00682-w).
15
+
16
+ This model features:
17
+ * ReLU activations
18
+ * single layer 7x7 convolution with pooling
19
+ * 1x1 convolution shortcut downsample
20
+
21
+ Trained on ImageNet-1k, original torchvision model weight, truncated to exclude layer 4
22
+ and the fully connected layer.
23
+
24
+ This model card was adapted from https://huggingface.co/timm/resnet50.tv_in1k.
25
+
26
+ ## Model Details
27
+ - **Model Type:** Feature backbone
28
+ - **Model Stats:**
29
+ - Params (M): 8.5
30
+ - Image size: 224 x 224
31
+ - **Papers:**
32
+ - Deep Residual Learning for Image Recognition: https://arxiv.org/abs/1512.03385
33
+ - **Original:** https://github.com/pytorch/vision
34
+
35
+ ## Model Creation
36
+
37
+ ```python
38
+ import types
39
+ import torch
40
+ from torchvision.models import ResNet
41
+ from torchvision.models import resnet50
42
+
43
+ def _forward_impl(self, x: torch.Tensor) -> torch.Tensor:
44
+ x = self.conv1(x)
45
+ x = self.bn1(x)
46
+ x = self.relu(x)
47
+ x = self.maxpool(x)
48
+
49
+ x = self.layer1(x)
50
+ x = self.layer2(x)
51
+ x = self.layer3(x)
52
+
53
+ x = self.avgpool(x)
54
+ x = x.view(x.size(0), -1)
55
+
56
+ return x
57
+
58
+ model = resnet50(weights=None)
59
+ del model.layer4, model.fc
60
+
61
+ model._forward_impl = types.MethodType(_forward_impl, model)
62
+
63
+ state_dict = torch.hub.load_state_dict_from_url(
64
+ "https://download.pytorch.org/models/resnet50-19c8e357.pth"
65
+ )
66
+ # Remove truncated keys.
67
+ state_dict = {k: v for k, v in state_dict.items() if not k.startswith("layer4.") and not k.startswith("fc.")}
68
+
69
+ model.load_state_dict(state_dict, strict=True)
70
+ model.eval()
71
+ ```
72
+
73
+ ## Model Usage
74
+
75
+ ### Image Embeddings
76
+ ```python
77
+ from urllib.request import urlopen
78
+ from PIL import Image
79
+ import torch
80
+
81
+ img = Image.open(urlopen(
82
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
83
+ ))
84
+
85
+ # See above for how to load the model. Or load a TorchScript version of the model,
86
+ # which can be loaded automatically with
87
+ # model = torch.jit.load("torchscript_model.bin")
88
+ model = model.eval()
89
+
90
+ transform = transforms.Compose([
91
+ # Depending on the pipeline, this may be 256x256 or a different value.
92
+ transforms.Resize((224, 224)),
93
+ transforms.ToTensor(),
94
+ transforms.Normalize(
95
+ mean=(0.485, 0.456, 0.406),
96
+ std=(0.229, 0.224, 0.225)),
97
+ ])
98
+
99
+ with torch.no_grad():
100
+ output = model(transform(img).unsqueeze(0)) # unsqueeze single image into batch of 1
101
+ output.shape # 1x1024
102
+ ```
103
+
104
+ ## Citation
105
+ ```bibtex
106
+ @article{He2015,
107
+ author = {Kaiming He and Xiangyu Zhang and Shaoqing Ren and Jian Sun},
108
+ title = {Deep Residual Learning for Image Recognition},
109
+ journal = {arXiv preprint arXiv:1512.03385},
110
+ year = {2015}
111
+ }
112
+ ```