1aurent commited on
Commit
0f765e4
·
1 Parent(s): dca30d0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +126 -1
README.md CHANGED
@@ -1,8 +1,133 @@
1
  ---
2
  tags:
 
3
  - image-classification
4
  - timm
 
 
 
5
  library_name: timm
6
- license: apache-2.0
 
 
 
 
7
  ---
 
8
  # Model card for swin_tiny_patch4_window7_224.CTransPath
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  tags:
3
+ - feature-extraction
4
  - image-classification
5
  - timm
6
+ - biology
7
+ - cancer
8
+ - histology
9
  library_name: timm
10
+ license: gpl-3.0
11
+ pipeline_tag: feature-extraction
12
+ inference: false
13
+ metrics:
14
+ - accuracy
15
  ---
16
+
17
  # Model card for swin_tiny_patch4_window7_224.CTransPath
18
+
19
+ A Swin Transformer image classification model. \
20
+ Trained on 15M histology patches from PAIP and TCGA.
21
+
22
+ ![](https://ars.els-cdn.com/content/image/1-s2.0-S1361841522002043-ga1_lrg.jpg)
23
+
24
+ ## Model Details
25
+
26
+ - **Model Type:** Feature backbone
27
+ - **Model Stats:**
28
+ - Params (M): 27.5
29
+ - Image size: 224 x 224 x 3
30
+ - **Papers:**
31
+ - Transformer-based unsupervised contrastive learning for histopathological image classification: https://www.sciencedirect.com/science/article/abs/pii/S1361841522002043
32
+ - **Dataset:** TCGA: https://portal.gdc.cancer.gov/
33
+ - **Original:** https://github.com/Xiyue-Wang/TransPath
34
+ - **License:** [GPLv3](https://github.com/Xiyue-Wang/TransPath/blob/main/LICENSE.md)
35
+
36
+ ## Model Usage
37
+
38
+ ### Custom Patch Embed Layer Definition
39
+
40
+ ```python
41
+ from timm.layers.helpers import to_2tuple
42
+ import timm
43
+ import torch.nn as nn
44
+
45
+ class ConvStem(nn.Module):
46
+ """Custom Patch Embed Layer.
47
+
48
+ Adapted from https://github.com/Xiyue-Wang/TransPath/blob/main/ctran.py#L6-L44
49
+ """
50
+
51
+ def __init__(self, img_size=224, patch_size=4, in_chans=3, embed_dim=768, norm_layer=None, flatten=False, **kwargs):
52
+ super().__init__()
53
+
54
+ assert patch_size == 4
55
+ assert embed_dim % 8 == 0
56
+
57
+ img_size = to_2tuple(img_size)
58
+ patch_size = to_2tuple(patch_size)
59
+ self.img_size = img_size
60
+ self.patch_size = patch_size
61
+ self.grid_size = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
62
+ self.num_patches = self.grid_size[0] * self.grid_size[1]
63
+ self.flatten = flatten
64
+
65
+ stem = []
66
+ input_dim, output_dim = 3, embed_dim // 8
67
+ for l in range(2):
68
+ stem.append(nn.Conv2d(input_dim, output_dim, kernel_size=3, stride=2, padding=1, bias=False))
69
+ stem.append(nn.BatchNorm2d(output_dim))
70
+ stem.append(nn.ReLU(inplace=True))
71
+ input_dim = output_dim
72
+ output_dim *= 2
73
+ stem.append(nn.Conv2d(input_dim, embed_dim, kernel_size=1))
74
+ self.proj = nn.Sequential(*stem)
75
+
76
+ self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
77
+
78
+ def forward(self, x):
79
+ B, C, H, W = x.shape
80
+ assert H == self.img_size[0] and W == self.img_size[1], \
81
+ f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
82
+ x = self.proj(x)
83
+ if self.flatten:
84
+ x = x.flatten(2).transpose(1, 2) # BCHW -> BNC
85
+ x = x.permute(0, 2, 3, 1) # BCHW -> BHWC
86
+ x = self.norm(x)
87
+ return x
88
+ ```
89
+
90
+ ### Image Embeddings
91
+ ```python
92
+ from urllib.request import urlopen
93
+ from PIL import Image
94
+ import timm
95
+
96
+ # get example histology image
97
+ img = Image.open(
98
+ urlopen(
99
+ "https://github.com/owkin/HistoSSLscaling/raw/main/assets/example.tif"
100
+ )
101
+ )
102
+
103
+ # load model from the hub
104
+ model = timm.create_model(
105
+ model_name="hf-hub:1aurent/swin_tiny_patch4_window7_224.CTransPath",
106
+ embed_layer=ConvStem, # defined above
107
+ pretrained=True,
108
+ ).eval()
109
+
110
+ # get model specific transforms (normalization, resize)
111
+ data_config = timm.data.resolve_model_data_config(model)
112
+ transforms = timm.data.create_transform(**data_config, is_training=False)
113
+
114
+ data = transforms(img).unsqueeze(0) # input is (batch_size, num_channels, img_size, img_size) shaped tensor
115
+ output = model(data) # output is (batch_size, num_features) shaped tensor
116
+ ```
117
+
118
+ ## Citation
119
+ ```bibtex
120
+ @article{WANG2022102559,
121
+ title = {Transformer-based unsupervised contrastive learning for histopathological image classification},
122
+ journal = {Medical Image Analysis},
123
+ volume = {81},
124
+ pages = {102559},
125
+ year = {2022},
126
+ issn = {1361-8415},
127
+ doi = {https://doi.org/10.1016/j.media.2022.102559},
128
+ url = {https://www.sciencedirect.com/science/article/pii/S1361841522002043},
129
+ author = {Xiyue Wang and Sen Yang and Jun Zhang and Minghui Wang and Jing Zhang and Wei Yang and Junzhou Huang and Xiao Han},
130
+ keywords = {Histopathology, Transformer, Self-supervised learning, Feature extraction},
131
+ abstract = {A large-scale and well-annotated dataset is a key factor for the success of deep learning in medical image analysis. However, assembling such large annotations is very challenging, especially for histopathological images with unique characteristics (e.g., gigapixel image size, multiple cancer types, and wide staining variations). To alleviate this issue, self-supervised learning (SSL) could be a promising solution that relies only on unlabeled data to generate informative representations and generalizes well to various downstream tasks even with limited annotations. In this work, we propose a novel SSL strategy called semantically-relevant contrastive learning (SRCL), which compares relevance between instances to mine more positive pairs. Compared to the two views from an instance in traditional contrastive learning, our SRCL aligns multiple positive instances with similar visual concepts, which increases the diversity of positives and then results in more informative representations. We employ a hybrid model (CTransPath) as the backbone, which is designed by integrating a convolutional neural network (CNN) and a multi-scale Swin Transformer architecture. The CTransPath is pretrained on massively unlabeled histopathological images that could serve as a collaborative local–global feature extractor to learn universal feature representations more suitable for tasks in the histopathology image domain. The effectiveness of our SRCL-pretrained CTransPath is investigated on five types of downstream tasks (patch retrieval, patch classification, weakly-supervised whole-slide image classification, mitosis detection, and colorectal adenocarcinoma gland segmentation), covering nine public datasets. The results show that our SRCL-based visual representations not only achieve state-of-the-art performance in each dataset, but are also more robust and transferable than other SSL methods and ImageNet pretraining (both supervised and self-supervised methods). Our code and pretrained model are available at https://github.com/Xiyue-Wang/TransPath.}
132
+ }
133
+ ```