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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags: []
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Model Card for Model ID
|
| 7 |
+
|
| 8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## Model Details
|
| 13 |
+
|
| 14 |
+
### Model Description
|
| 15 |
+
|
| 16 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
+
|
| 18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
+
|
| 20 |
+
- **Developed by:** [More Information Needed]
|
| 21 |
+
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
+
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
+
- **Model type:** [More Information Needed]
|
| 24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
+
- **License:** [More Information Needed]
|
| 26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
+
|
| 28 |
+
### Model Sources [optional]
|
| 29 |
+
|
| 30 |
+
<!-- Provide the basic links for the model. -->
|
| 31 |
+
|
| 32 |
+
- **Repository:** [More Information Needed]
|
| 33 |
+
- **Paper [optional]:** [More Information Needed]
|
| 34 |
+
- **Demo [optional]:** [More Information Needed]
|
| 35 |
+
|
| 36 |
+
## Uses
|
| 37 |
+
|
| 38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
+
|
| 40 |
+
### Direct Use
|
| 41 |
+
|
| 42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
+
|
| 44 |
+
[More Information Needed]
|
| 45 |
+
|
| 46 |
+
### Downstream Use [optional]
|
| 47 |
+
|
| 48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
+
|
| 50 |
+
[More Information Needed]
|
| 51 |
+
|
| 52 |
+
### Out-of-Scope Use
|
| 53 |
+
|
| 54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
+
|
| 56 |
+
[More Information Needed]
|
| 57 |
+
|
| 58 |
+
## Bias, Risks, and Limitations
|
| 59 |
+
|
| 60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
+
|
| 62 |
+
[More Information Needed]
|
| 63 |
+
|
| 64 |
+
### Recommendations
|
| 65 |
+
|
| 66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
+
|
| 68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
+
|
| 70 |
+
## How to Get Started with the Model
|
| 71 |
+
|
| 72 |
+
Use the code below to get started with the model.
|
| 73 |
+
|
| 74 |
+
[More Information Needed]
|
| 75 |
+
|
| 76 |
+
## Training Details
|
| 77 |
+
|
| 78 |
+
### Training Data
|
| 79 |
+
|
| 80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
+
|
| 82 |
+
[More Information Needed]
|
| 83 |
+
|
| 84 |
+
### Training Procedure
|
| 85 |
+
|
| 86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
+
|
| 88 |
+
#### Preprocessing [optional]
|
| 89 |
+
|
| 90 |
+
[More Information Needed]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
#### Training Hyperparameters
|
| 94 |
+
|
| 95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
+
|
| 97 |
+
#### Speeds, Sizes, Times [optional]
|
| 98 |
+
|
| 99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
+
|
| 101 |
+
[More Information Needed]
|
| 102 |
+
|
| 103 |
+
## Evaluation
|
| 104 |
+
|
| 105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
+
|
| 107 |
+
### Testing Data, Factors & Metrics
|
| 108 |
+
|
| 109 |
+
#### Testing Data
|
| 110 |
+
|
| 111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
+
|
| 113 |
+
[More Information Needed]
|
| 114 |
+
|
| 115 |
+
#### Factors
|
| 116 |
+
|
| 117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
+
|
| 119 |
+
[More Information Needed]
|
| 120 |
+
|
| 121 |
+
#### Metrics
|
| 122 |
+
|
| 123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
+
|
| 125 |
+
[More Information Needed]
|
| 126 |
+
|
| 127 |
+
### Results
|
| 128 |
+
|
| 129 |
+
[More Information Needed]
|
| 130 |
+
|
| 131 |
+
#### Summary
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
## Model Examination [optional]
|
| 136 |
+
|
| 137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
+
|
| 139 |
+
[More Information Needed]
|
| 140 |
+
|
| 141 |
+
## Environmental Impact
|
| 142 |
+
|
| 143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
+
|
| 145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
+
|
| 147 |
+
- **Hardware Type:** [More Information Needed]
|
| 148 |
+
- **Hours used:** [More Information Needed]
|
| 149 |
+
- **Cloud Provider:** [More Information Needed]
|
| 150 |
+
- **Compute Region:** [More Information Needed]
|
| 151 |
+
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
+
|
| 153 |
+
## Technical Specifications [optional]
|
| 154 |
+
|
| 155 |
+
### Model Architecture and Objective
|
| 156 |
+
|
| 157 |
+
[More Information Needed]
|
| 158 |
+
|
| 159 |
+
### Compute Infrastructure
|
| 160 |
+
|
| 161 |
+
[More Information Needed]
|
| 162 |
+
|
| 163 |
+
#### Hardware
|
| 164 |
+
|
| 165 |
+
[More Information Needed]
|
| 166 |
+
|
| 167 |
+
#### Software
|
| 168 |
+
|
| 169 |
+
[More Information Needed]
|
| 170 |
+
|
| 171 |
+
## Citation [optional]
|
| 172 |
+
|
| 173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
+
|
| 175 |
+
**BibTeX:**
|
| 176 |
+
|
| 177 |
+
[More Information Needed]
|
| 178 |
+
|
| 179 |
+
**APA:**
|
| 180 |
+
|
| 181 |
+
[More Information Needed]
|
| 182 |
+
|
| 183 |
+
## Glossary [optional]
|
| 184 |
+
|
| 185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
+
|
| 187 |
+
[More Information Needed]
|
| 188 |
+
|
| 189 |
+
## More Information [optional]
|
| 190 |
+
|
| 191 |
+
[More Information Needed]
|
| 192 |
+
|
| 193 |
+
## Model Card Authors [optional]
|
| 194 |
+
|
| 195 |
+
[More Information Needed]
|
| 196 |
+
|
| 197 |
+
## Model Card Contact
|
| 198 |
+
|
| 199 |
+
[More Information Needed]
|
config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Resnet3DScrollprizeModel"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_resnet3d.Resnet3DScrollprizeConfig",
|
| 7 |
+
"AutoModel": "modeling_resnet3d.Resnet3DScrollprizeModel"
|
| 8 |
+
},
|
| 9 |
+
"model_depth": 50,
|
| 10 |
+
"model_type": "resnetscrollprize",
|
| 11 |
+
"n_classes": 1139,
|
| 12 |
+
"num_layers": 18,
|
| 13 |
+
"torch_dtype": "float32",
|
| 14 |
+
"transformers_version": "4.46.3",
|
| 15 |
+
"window_size": 256
|
| 16 |
+
}
|
configuration_resnet3d.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
class Resnet3DScrollprizeConfig(PretrainedConfig):
|
| 4 |
+
model_type = "resnetscrollprize"
|
| 5 |
+
|
| 6 |
+
def __init__(
|
| 7 |
+
self,
|
| 8 |
+
window_size=64,
|
| 9 |
+
model_depth=50,
|
| 10 |
+
n_classes=1039,
|
| 11 |
+
num_layers=18,
|
| 12 |
+
**kwargs,
|
| 13 |
+
):
|
| 14 |
+
self.window_size=window_size
|
| 15 |
+
self.model_depth=model_depth
|
| 16 |
+
self.n_classes=n_classes
|
| 17 |
+
self.num_layers=num_layers
|
| 18 |
+
super().__init__(**kwargs)
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:052670ea5013e1fa67b009e28d4d9d7e63e006ff4c705a43cd90f72f4f5f7b76
|
| 3 |
+
size 342867728
|
modeling_resnet3d.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedModel
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from .resnetall import generate_model
|
| 5 |
+
from .configuration_resnet3d import Resnet3DScrollprizeConfig
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
class Decoder(nn.Module):
|
| 8 |
+
def __init__(self, encoder_dims, upscale):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.convs = nn.ModuleList([
|
| 11 |
+
nn.Sequential(
|
| 12 |
+
nn.Conv2d(encoder_dims[i]+encoder_dims[i-1], encoder_dims[i-1], 3, 1, 1, bias=False),
|
| 13 |
+
nn.BatchNorm2d(encoder_dims[i-1]),
|
| 14 |
+
nn.ReLU(inplace=True)
|
| 15 |
+
) for i in range(1, len(encoder_dims))])
|
| 16 |
+
|
| 17 |
+
self.logit = nn.Conv2d(encoder_dims[0], 1, 1, 1, 0)
|
| 18 |
+
self.up = nn.Upsample(scale_factor=upscale, mode="bilinear")
|
| 19 |
+
|
| 20 |
+
def forward(self, feature_maps):
|
| 21 |
+
for i in range(len(feature_maps)-1, 0, -1):
|
| 22 |
+
f_up = F.interpolate(feature_maps[i], scale_factor=2, mode="bilinear")
|
| 23 |
+
f = torch.cat([feature_maps[i-1], f_up], dim=1)
|
| 24 |
+
f_down = self.convs[i-1](f)
|
| 25 |
+
feature_maps[i-1] = f_down
|
| 26 |
+
|
| 27 |
+
x = self.logit(feature_maps[0])
|
| 28 |
+
mask = self.up(x)
|
| 29 |
+
return mask
|
| 30 |
+
|
| 31 |
+
class Resnet3DScrollprizeModel(PreTrainedModel):
|
| 32 |
+
config_class = Resnet3DScrollprizeConfig
|
| 33 |
+
|
| 34 |
+
def __init__(self, config):
|
| 35 |
+
super().__init__(config)
|
| 36 |
+
self.backbone= generate_model(model_depth=config.model_depth, n_input_channels=1,forward_features=True,n_classes=config.n_classes)
|
| 37 |
+
self.decoder = Decoder(encoder_dims=[x.size(1) for x in self.backbone(torch.rand(1,1,20,256,256))], upscale=1)
|
| 38 |
+
def forward(self, tensor):
|
| 39 |
+
feat_maps = self.backbone(tensor)
|
| 40 |
+
feat_maps_pooled = [torch.max(f, dim=2)[0] for f in feat_maps]
|
| 41 |
+
pred_mask = self.decoder(feat_maps_pooled)
|
| 42 |
+
return pred_mask
|
resnetall.py
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 2 |
+
from functools import partial
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_inplanes():
|
| 10 |
+
return [64, 128, 256, 512]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def conv3x3x3(in_planes, out_planes, stride=1):
|
| 14 |
+
return nn.Conv3d(in_planes,
|
| 15 |
+
out_planes,
|
| 16 |
+
kernel_size=3,
|
| 17 |
+
stride=stride,
|
| 18 |
+
padding=1,
|
| 19 |
+
bias=False)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def conv1x1x1(in_planes, out_planes, stride=1):
|
| 23 |
+
return nn.Conv3d(in_planes,
|
| 24 |
+
out_planes,
|
| 25 |
+
kernel_size=1,
|
| 26 |
+
stride=stride,
|
| 27 |
+
bias=False)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class BasicBlock(nn.Module):
|
| 31 |
+
expansion = 1
|
| 32 |
+
|
| 33 |
+
def __init__(self, in_planes, planes, stride=1, downsample=None):
|
| 34 |
+
super().__init__()
|
| 35 |
+
|
| 36 |
+
self.conv1 = conv3x3x3(in_planes, planes, stride)
|
| 37 |
+
self.bn1 = nn.BatchNorm3d(planes)
|
| 38 |
+
self.relu = nn.ReLU(inplace=True)
|
| 39 |
+
self.conv2 = conv3x3x3(planes, planes)
|
| 40 |
+
self.bn2 = nn.BatchNorm3d(planes)
|
| 41 |
+
self.downsample = downsample
|
| 42 |
+
self.stride = stride
|
| 43 |
+
|
| 44 |
+
def forward(self, x):
|
| 45 |
+
residual = x
|
| 46 |
+
|
| 47 |
+
out = self.conv1(x)
|
| 48 |
+
out = self.bn1(out)
|
| 49 |
+
out = self.relu(out)
|
| 50 |
+
|
| 51 |
+
out = self.conv2(out)
|
| 52 |
+
out = self.bn2(out)
|
| 53 |
+
|
| 54 |
+
if self.downsample is not None:
|
| 55 |
+
residual = self.downsample(x)
|
| 56 |
+
|
| 57 |
+
out += residual
|
| 58 |
+
out = self.relu(out)
|
| 59 |
+
|
| 60 |
+
return out
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class Bottleneck(nn.Module):
|
| 64 |
+
expansion = 4
|
| 65 |
+
|
| 66 |
+
def __init__(self, in_planes, planes, stride=1, downsample=None):
|
| 67 |
+
super().__init__()
|
| 68 |
+
|
| 69 |
+
self.conv1 = conv1x1x1(in_planes, planes)
|
| 70 |
+
self.bn1 = nn.BatchNorm3d(planes)
|
| 71 |
+
self.conv2 = conv3x3x3(planes, planes, stride)
|
| 72 |
+
self.bn2 = nn.BatchNorm3d(planes)
|
| 73 |
+
self.conv3 = conv1x1x1(planes, planes * self.expansion)
|
| 74 |
+
self.bn3 = nn.BatchNorm3d(planes * self.expansion)
|
| 75 |
+
self.relu = nn.ReLU(inplace=True)
|
| 76 |
+
self.downsample = downsample
|
| 77 |
+
self.stride = stride
|
| 78 |
+
|
| 79 |
+
def forward(self, x):
|
| 80 |
+
residual = x
|
| 81 |
+
|
| 82 |
+
out = self.conv1(x)
|
| 83 |
+
out = self.bn1(out)
|
| 84 |
+
out = self.relu(out)
|
| 85 |
+
|
| 86 |
+
out = self.conv2(out)
|
| 87 |
+
out = self.bn2(out)
|
| 88 |
+
out = self.relu(out)
|
| 89 |
+
|
| 90 |
+
out = self.conv3(out)
|
| 91 |
+
out = self.bn3(out)
|
| 92 |
+
|
| 93 |
+
if self.downsample is not None:
|
| 94 |
+
residual = self.downsample(x)
|
| 95 |
+
|
| 96 |
+
out += residual
|
| 97 |
+
out = self.relu(out)
|
| 98 |
+
|
| 99 |
+
return out
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class ResNet(nn.Module):
|
| 103 |
+
|
| 104 |
+
def __init__(self,
|
| 105 |
+
block,
|
| 106 |
+
layers,
|
| 107 |
+
block_inplanes,
|
| 108 |
+
n_input_channels=3,
|
| 109 |
+
conv1_t_size=7,
|
| 110 |
+
conv1_t_stride=1,
|
| 111 |
+
no_max_pool=False,
|
| 112 |
+
shortcut_type='B',
|
| 113 |
+
widen_factor=1.0,
|
| 114 |
+
n_classes=400,
|
| 115 |
+
forward_features=False,
|
| 116 |
+
):
|
| 117 |
+
super().__init__()
|
| 118 |
+
self.forward_features=forward_features
|
| 119 |
+
block_inplanes = [int(x * widen_factor) for x in block_inplanes]
|
| 120 |
+
|
| 121 |
+
self.in_planes = block_inplanes[0]
|
| 122 |
+
self.no_max_pool = no_max_pool
|
| 123 |
+
|
| 124 |
+
self.conv1 = nn.Conv3d(n_input_channels,
|
| 125 |
+
self.in_planes,
|
| 126 |
+
kernel_size=(conv1_t_size, 7, 7),
|
| 127 |
+
stride=(conv1_t_stride, 2, 2),
|
| 128 |
+
padding=(conv1_t_size // 2, 3, 3),
|
| 129 |
+
bias=False)
|
| 130 |
+
self.bn1 = nn.BatchNorm3d(self.in_planes)
|
| 131 |
+
self.relu = nn.ReLU(inplace=True)
|
| 132 |
+
self.maxpool = nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(1, 2, 2), padding=(0, 1, 1))
|
| 133 |
+
# self.maxpool = nn.MaxPool3d(kernel_size=3, stride=2, padding=1)
|
| 134 |
+
|
| 135 |
+
self.layer1 = self._make_layer(block, block_inplanes[0], layers[0],
|
| 136 |
+
shortcut_type)
|
| 137 |
+
self.layer2 = self._make_layer(block,
|
| 138 |
+
block_inplanes[1],
|
| 139 |
+
layers[1],
|
| 140 |
+
shortcut_type,
|
| 141 |
+
stride=2)
|
| 142 |
+
self.layer3 = self._make_layer(block,
|
| 143 |
+
block_inplanes[2],
|
| 144 |
+
layers[2],
|
| 145 |
+
shortcut_type,
|
| 146 |
+
stride=2)
|
| 147 |
+
self.layer4 = self._make_layer(block,
|
| 148 |
+
block_inplanes[3],
|
| 149 |
+
layers[3],
|
| 150 |
+
shortcut_type,
|
| 151 |
+
stride=2)
|
| 152 |
+
|
| 153 |
+
self.avgpool = nn.AdaptiveMaxPool3d((1, 1, 1))
|
| 154 |
+
self.fc = nn.Linear(block_inplanes[3] * block.expansion, n_classes)
|
| 155 |
+
|
| 156 |
+
for m in self.modules():
|
| 157 |
+
if isinstance(m, nn.Conv3d):
|
| 158 |
+
nn.init.kaiming_normal_(m.weight,
|
| 159 |
+
mode='fan_out',
|
| 160 |
+
nonlinearity='relu')
|
| 161 |
+
elif isinstance(m, nn.BatchNorm3d):
|
| 162 |
+
nn.init.constant_(m.weight, 1)
|
| 163 |
+
nn.init.constant_(m.bias, 0)
|
| 164 |
+
|
| 165 |
+
def _downsample_basic_block(self, x, planes, stride):
|
| 166 |
+
out = F.avg_pool3d(x, kernel_size=1, stride=stride)
|
| 167 |
+
zero_pads = torch.zeros(out.size(0), planes - out.size(1), out.size(2),
|
| 168 |
+
out.size(3), out.size(4))
|
| 169 |
+
if isinstance(out.data, torch.cuda.FloatTensor):
|
| 170 |
+
zero_pads = zero_pads.cuda()
|
| 171 |
+
|
| 172 |
+
out = torch.cat([out.data, zero_pads], dim=1)
|
| 173 |
+
|
| 174 |
+
return out
|
| 175 |
+
|
| 176 |
+
def _make_layer(self, block, planes, blocks, shortcut_type, stride=1):
|
| 177 |
+
downsample = None
|
| 178 |
+
if stride != 1 or self.in_planes != planes * block.expansion:
|
| 179 |
+
if shortcut_type == 'A':
|
| 180 |
+
downsample = partial(self._downsample_basic_block,
|
| 181 |
+
planes=planes * block.expansion,
|
| 182 |
+
stride=stride)
|
| 183 |
+
else:
|
| 184 |
+
downsample = nn.Sequential(
|
| 185 |
+
conv1x1x1(self.in_planes, planes * block.expansion, stride),
|
| 186 |
+
nn.BatchNorm3d(planes * block.expansion))
|
| 187 |
+
|
| 188 |
+
layers = []
|
| 189 |
+
layers.append(
|
| 190 |
+
block(in_planes=self.in_planes,
|
| 191 |
+
planes=planes,
|
| 192 |
+
stride=stride,
|
| 193 |
+
downsample=downsample))
|
| 194 |
+
self.in_planes = planes * block.expansion
|
| 195 |
+
for i in range(1, blocks):
|
| 196 |
+
layers.append(block(self.in_planes, planes))
|
| 197 |
+
|
| 198 |
+
return nn.Sequential(*layers)
|
| 199 |
+
|
| 200 |
+
def forward(self, x):
|
| 201 |
+
x = self.conv1(x)
|
| 202 |
+
x = self.bn1(x)
|
| 203 |
+
x = self.relu(x)
|
| 204 |
+
if not self.no_max_pool:
|
| 205 |
+
x = self.maxpool(x)
|
| 206 |
+
|
| 207 |
+
x1 = self.layer1(x)
|
| 208 |
+
x2 = self.layer2(x1)
|
| 209 |
+
x3 = self.layer3(x2)
|
| 210 |
+
x4 = self.layer4(x3)
|
| 211 |
+
if self.forward_features:
|
| 212 |
+
return [x1,x2,x3,x4]
|
| 213 |
+
else:
|
| 214 |
+
x = self.avgpool(x4)
|
| 215 |
+
|
| 216 |
+
x = x.view(x.size(0), -1)
|
| 217 |
+
x = self.fc(x)
|
| 218 |
+
|
| 219 |
+
return x
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def generate_model(model_depth, **kwargs):
|
| 223 |
+
assert model_depth in [10, 18, 34, 50, 101, 152, 200]
|
| 224 |
+
|
| 225 |
+
if model_depth == 10:
|
| 226 |
+
model = ResNet(BasicBlock, [1, 1, 1, 1], get_inplanes(), **kwargs)
|
| 227 |
+
elif model_depth == 18:
|
| 228 |
+
model = ResNet(BasicBlock, [2, 2, 2, 2], get_inplanes(), **kwargs)
|
| 229 |
+
elif model_depth == 34:
|
| 230 |
+
model = ResNet(BasicBlock, [3, 4, 6, 3], get_inplanes(), **kwargs)
|
| 231 |
+
elif model_depth == 50:
|
| 232 |
+
model = ResNet(Bottleneck, [3, 4, 6, 3], get_inplanes(), **kwargs)
|
| 233 |
+
elif model_depth == 101:
|
| 234 |
+
model = ResNet(Bottleneck, [3, 4, 23, 3], get_inplanes(), **kwargs)
|
| 235 |
+
elif model_depth == 152:
|
| 236 |
+
model = ResNet(Bottleneck, [3, 8, 36, 3], get_inplanes(), **kwargs)
|
| 237 |
+
elif model_depth == 200:
|
| 238 |
+
model = ResNet(Bottleneck, [3, 24, 36, 3], get_inplanes(), **kwargs)
|
| 239 |
+
|
| 240 |
+
return model
|