Upload folder using huggingface_hub
Browse files
model.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch.nn as nn
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision.models import resnet50
|
| 4 |
+
import os
|
| 5 |
+
os.environ["TORCH_HOME"] = os.getcwd()
|
| 6 |
+
|
| 7 |
+
# reference : https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py#L166
|
| 8 |
+
|
| 9 |
+
class Resnet50Custom(nn.Module):
|
| 10 |
+
def __init__(self, object_type_output_dim=15):
|
| 11 |
+
super().__init__()
|
| 12 |
+
self.resnet50 = resnet = resnet50(weights="IMAGENET1K_V2")
|
| 13 |
+
for param in self.resnet50.parameters():
|
| 14 |
+
param.requires_grad = False
|
| 15 |
+
self.resnet50.fc = nn.Linear(self.resnet50.fc.in_features, object_type_output_dim)
|
| 16 |
+
self.ff_defect_classification = nn.Linear(self.resnet50.fc.in_features, 2)
|
| 17 |
+
|
| 18 |
+
def forward(self, x):
|
| 19 |
+
x = self.resnet50.conv1(x)
|
| 20 |
+
x = self.resnet50.bn1(x)
|
| 21 |
+
x = self.resnet50.relu(x)
|
| 22 |
+
x = self.resnet50.maxpool(x)
|
| 23 |
+
|
| 24 |
+
x = self.resnet50.layer1(x)
|
| 25 |
+
x = self.resnet50.layer2(x)
|
| 26 |
+
x = self.resnet50.layer3(x)
|
| 27 |
+
x = self.resnet50.layer4(x)
|
| 28 |
+
|
| 29 |
+
x = self.resnet50.avgpool(x)
|
| 30 |
+
x = torch.flatten(x, 1)
|
| 31 |
+
|
| 32 |
+
ff_object_classification_output = self.resnet50.fc(x)
|
| 33 |
+
ff_defect_classification_output = self.ff_defect_classification(x)
|
| 34 |
+
|
| 35 |
+
return ff_object_classification_output, ff_defect_classification_output
|