Update train_mlp.py
Browse files- train_mlp.py +11 -25
train_mlp.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
-
#imports
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import csv
|
| 5 |
import torch
|
| 6 |
from torch import nn
|
| 7 |
from torch.utils.data import DataLoader
|
| 8 |
from torchvision import datasets
|
| 9 |
-
from torchvision.transforms import ToTensor, Normalize,
|
| 10 |
from contextualizer_mlp import Contextualizer
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
transform = Compose([
|
| 15 |
|
|
@@ -31,7 +29,7 @@ test_data = datasets.CIFAR10(
|
|
| 31 |
download=True,
|
| 32 |
transform=transform
|
| 33 |
)
|
| 34 |
-
|
| 35 |
|
| 36 |
batch_size = 128
|
| 37 |
|
|
@@ -44,22 +42,12 @@ for X, y in test_dataloader:
|
|
| 44 |
print(f"Shape of y:{y.shape}{y.dtype}")
|
| 45 |
break
|
| 46 |
|
| 47 |
-
# size checking for loading images
|
| 48 |
-
def check_sizes(image_size, patch_size):
|
| 49 |
-
sqrt_num_patches, remainder = divmod(image_size, patch_size)
|
| 50 |
-
assert remainder == 0, "`image_size` must be divisibe by `patch_size`"
|
| 51 |
-
num_patches = sqrt_num_patches ** 2
|
| 52 |
-
return num_patches
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
# create model
|
| 57 |
-
# Get cpu or gpu device for training.
|
| 58 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 59 |
|
| 60 |
print(f"using {device} device")
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
class ContextualizerImageClassification(Contextualizer):
|
| 65 |
def __init__(
|
|
@@ -74,7 +62,6 @@ class ContextualizerImageClassification(Contextualizer):
|
|
| 74 |
num_layers=4,
|
| 75 |
dropout=0.5
|
| 76 |
):
|
| 77 |
-
num_patches = check_sizes(image_size, patch_size)
|
| 78 |
super().__init__(d_model,d_ffn,num_layers,dropout, num_tokens)
|
| 79 |
self.patcher = nn.Conv2d(
|
| 80 |
in_channels, d_model, kernel_size=patch_size, stride=patch_size
|
|
@@ -88,20 +75,20 @@ class ContextualizerImageClassification(Contextualizer):
|
|
| 88 |
patches = patches.permute(0, 2, 3, 1)
|
| 89 |
patches = patches.view(batch_size, -1, num_channels)
|
| 90 |
embedding = self.model(patches)
|
| 91 |
-
embedding = embedding.mean(dim=1)
|
| 92 |
out = self.classifier(embedding)
|
| 93 |
return out
|
| 94 |
|
| 95 |
model = ContextualizerImageClassification().to(device)
|
| 96 |
print(model)
|
| 97 |
|
| 98 |
-
|
| 99 |
|
| 100 |
loss_fn = nn.CrossEntropyLoss()
|
| 101 |
optimizer = torch.optim.Adam(model.parameters(),lr=1e-3)
|
| 102 |
|
| 103 |
|
| 104 |
-
|
| 105 |
|
| 106 |
def train(dataloader, model, loss_fn, optimizer):
|
| 107 |
size = len(dataloader.dataset)
|
|
@@ -112,11 +99,11 @@ def train(dataloader, model, loss_fn, optimizer):
|
|
| 112 |
for batch, (X,y) in enumerate(dataloader):
|
| 113 |
X, y = X.to(device), y.to(device)
|
| 114 |
|
| 115 |
-
|
| 116 |
pred = model(X)
|
| 117 |
loss = loss_fn(pred,y)
|
| 118 |
|
| 119 |
-
|
| 120 |
optimizer.zero_grad()
|
| 121 |
loss.backward()
|
| 122 |
optimizer.step()
|
|
@@ -138,7 +125,6 @@ def train(dataloader, model, loss_fn, optimizer):
|
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
-
# Test loop
|
| 142 |
|
| 143 |
def test(dataloader, model, loss_fn):
|
| 144 |
size = len(dataloader.dataset)
|
|
@@ -160,7 +146,7 @@ def test(dataloader, model, loss_fn):
|
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
-
|
| 164 |
|
| 165 |
logname = "/PATH/Contextualizer_mlp/Experiments_cifar10/logs_contextualizer/logs_cifar10.csv"
|
| 166 |
if not os.path.exists(logname):
|
|
@@ -184,7 +170,7 @@ for epoch in range(epochs):
|
|
| 184 |
test_loss, test_acc])
|
| 185 |
print("Done!")
|
| 186 |
|
| 187 |
-
|
| 188 |
|
| 189 |
path = "/PATH/Contextualizer_mlp/Experiments_cifar10/weights_contextualizer"
|
| 190 |
model_name = "ContextualizerImageClassification_cifar10"
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import csv
|
| 3 |
import torch
|
| 4 |
from torch import nn
|
| 5 |
from torch.utils.data import DataLoader
|
| 6 |
from torchvision import datasets
|
| 7 |
+
from torchvision.transforms import ToTensor, Normalize, Compose
|
| 8 |
from contextualizer_mlp import Contextualizer
|
| 9 |
|
| 10 |
+
|
| 11 |
|
| 12 |
transform = Compose([
|
| 13 |
|
|
|
|
| 29 |
download=True,
|
| 30 |
transform=transform
|
| 31 |
)
|
| 32 |
+
|
| 33 |
|
| 34 |
batch_size = 128
|
| 35 |
|
|
|
|
| 42 |
print(f"Shape of y:{y.shape}{y.dtype}")
|
| 43 |
break
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
| 46 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 47 |
|
| 48 |
print(f"using {device} device")
|
| 49 |
|
| 50 |
+
|
| 51 |
|
| 52 |
class ContextualizerImageClassification(Contextualizer):
|
| 53 |
def __init__(
|
|
|
|
| 62 |
num_layers=4,
|
| 63 |
dropout=0.5
|
| 64 |
):
|
|
|
|
| 65 |
super().__init__(d_model,d_ffn,num_layers,dropout, num_tokens)
|
| 66 |
self.patcher = nn.Conv2d(
|
| 67 |
in_channels, d_model, kernel_size=patch_size, stride=patch_size
|
|
|
|
| 75 |
patches = patches.permute(0, 2, 3, 1)
|
| 76 |
patches = patches.view(batch_size, -1, num_channels)
|
| 77 |
embedding = self.model(patches)
|
| 78 |
+
embedding = embedding.mean(dim=1)
|
| 79 |
out = self.classifier(embedding)
|
| 80 |
return out
|
| 81 |
|
| 82 |
model = ContextualizerImageClassification().to(device)
|
| 83 |
print(model)
|
| 84 |
|
| 85 |
+
|
| 86 |
|
| 87 |
loss_fn = nn.CrossEntropyLoss()
|
| 88 |
optimizer = torch.optim.Adam(model.parameters(),lr=1e-3)
|
| 89 |
|
| 90 |
|
| 91 |
+
|
| 92 |
|
| 93 |
def train(dataloader, model, loss_fn, optimizer):
|
| 94 |
size = len(dataloader.dataset)
|
|
|
|
| 99 |
for batch, (X,y) in enumerate(dataloader):
|
| 100 |
X, y = X.to(device), y.to(device)
|
| 101 |
|
| 102 |
+
|
| 103 |
pred = model(X)
|
| 104 |
loss = loss_fn(pred,y)
|
| 105 |
|
| 106 |
+
|
| 107 |
optimizer.zero_grad()
|
| 108 |
loss.backward()
|
| 109 |
optimizer.step()
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
|
|
|
|
| 128 |
|
| 129 |
def test(dataloader, model, loss_fn):
|
| 130 |
size = len(dataloader.dataset)
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
+
|
| 150 |
|
| 151 |
logname = "/PATH/Contextualizer_mlp/Experiments_cifar10/logs_contextualizer/logs_cifar10.csv"
|
| 152 |
if not os.path.exists(logname):
|
|
|
|
| 170 |
test_loss, test_acc])
|
| 171 |
print("Done!")
|
| 172 |
|
| 173 |
+
|
| 174 |
|
| 175 |
path = "/PATH/Contextualizer_mlp/Experiments_cifar10/weights_contextualizer"
|
| 176 |
model_name = "ContextualizerImageClassification_cifar10"
|