Update models/resnet50.py
Browse files- models/resnet50.py +8 -21
models/resnet50.py
CHANGED
|
@@ -8,17 +8,8 @@ import torchvision.transforms as transforms
|
|
| 8 |
import os
|
| 9 |
import pandas as pd
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
mapping = {
|
| 14 |
-
"Non_Damage": 0,
|
| 15 |
-
"Land_Disaster": 1,
|
| 16 |
-
"Fire_Disaster": 2,
|
| 17 |
-
"Water_Disaster": 3
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
indices = list(mapping[category] for category in labels)
|
| 21 |
-
return indices
|
| 22 |
|
| 23 |
def write_to_csv(predicted, actual, probs, write_path, header):
|
| 24 |
|
|
@@ -42,7 +33,7 @@ def write_to_csv(predicted, actual, probs, write_path, header):
|
|
| 42 |
|
| 43 |
class ResNet50():
|
| 44 |
|
| 45 |
-
def __init__(self, num_classes, lr=0.01, momentum=0.9):
|
| 46 |
self.model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
| 47 |
self.num_classes = num_classes
|
| 48 |
self.lr = lr
|
|
@@ -53,6 +44,8 @@ class ResNet50():
|
|
| 53 |
self.criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
|
| 54 |
self.optimizer = optim.SGD(self.model.parameters(), lr=self.lr, momentum=self.momentum)
|
| 55 |
|
|
|
|
|
|
|
| 56 |
def train(self, epochs, train_loader):
|
| 57 |
loss_over_time = []
|
| 58 |
num_epochs = list(range(1, epochs + 1))
|
|
@@ -63,7 +56,7 @@ class ResNet50():
|
|
| 63 |
inputs, labels = data
|
| 64 |
self.optimizer.zero_grad()
|
| 65 |
outputs = self.model(data[inputs].float())
|
| 66 |
-
indices = create_indices(data[labels])
|
| 67 |
target = torch.tensor(indices)
|
| 68 |
loss = self.criterion(outputs, target)
|
| 69 |
loss.backward()
|
|
@@ -79,13 +72,6 @@ class ResNet50():
|
|
| 79 |
"loss": loss_over_time
|
| 80 |
}, "model_weights.pth")
|
| 81 |
|
| 82 |
-
data = {
|
| 83 |
-
"Epochs": num_epochs,
|
| 84 |
-
"Loss": loss_over_time
|
| 85 |
-
}
|
| 86 |
-
data = pd.DataFrame(data=data)
|
| 87 |
-
data.to_csv("results/model_progress.csv", index=False)
|
| 88 |
-
|
| 89 |
def eval(self, test_loader, write_path=None):
|
| 90 |
self.model.eval()
|
| 91 |
header = True
|
|
@@ -97,7 +83,8 @@ class ResNet50():
|
|
| 97 |
images, labels = data
|
| 98 |
images = data[images].float()
|
| 99 |
labels = data[labels]
|
| 100 |
-
indices = create_indices(labels)
|
|
|
|
| 101 |
labels = torch.tensor(indices)
|
| 102 |
|
| 103 |
outputs = self.model(images)
|
|
|
|
| 8 |
import os
|
| 9 |
import pandas as pd
|
| 10 |
|
| 11 |
+
def create_indices(labels, mapping):
|
| 12 |
+
return [mapping[label] for label in labels]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def write_to_csv(predicted, actual, probs, write_path, header):
|
| 15 |
|
|
|
|
| 33 |
|
| 34 |
class ResNet50():
|
| 35 |
|
| 36 |
+
def __init__(self, num_classes, lr=0.01, momentum=0.9, mapping=None):
|
| 37 |
self.model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
| 38 |
self.num_classes = num_classes
|
| 39 |
self.lr = lr
|
|
|
|
| 44 |
self.criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
|
| 45 |
self.optimizer = optim.SGD(self.model.parameters(), lr=self.lr, momentum=self.momentum)
|
| 46 |
|
| 47 |
+
self.mapping = mapping
|
| 48 |
+
|
| 49 |
def train(self, epochs, train_loader):
|
| 50 |
loss_over_time = []
|
| 51 |
num_epochs = list(range(1, epochs + 1))
|
|
|
|
| 56 |
inputs, labels = data
|
| 57 |
self.optimizer.zero_grad()
|
| 58 |
outputs = self.model(data[inputs].float())
|
| 59 |
+
indices = create_indices(data[labels], self.mapping)
|
| 60 |
target = torch.tensor(indices)
|
| 61 |
loss = self.criterion(outputs, target)
|
| 62 |
loss.backward()
|
|
|
|
| 72 |
"loss": loss_over_time
|
| 73 |
}, "model_weights.pth")
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def eval(self, test_loader, write_path=None):
|
| 76 |
self.model.eval()
|
| 77 |
header = True
|
|
|
|
| 83 |
images, labels = data
|
| 84 |
images = data[images].float()
|
| 85 |
labels = data[labels]
|
| 86 |
+
# indices = create_indices(labels)
|
| 87 |
+
indices = create_indices(labels, self.mapping)
|
| 88 |
labels = torch.tensor(indices)
|
| 89 |
|
| 90 |
outputs = self.model(images)
|