-
Notifications
You must be signed in to change notification settings - Fork 0
/
traintest.py
276 lines (215 loc) · 9.21 KB
/
traintest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import torch
from torch import nn
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader, ConcatDataset, Subset
from torchvision import transforms, datasets, models
from sklearn.model_selection import train_test_split
from utils import max_samples_per_class
import matplotlib.pyplot as plt
import numpy as np
import argparse
def main():
# Parse arguments from command line
parser = argparse.ArgumentParser()
parser.add_argument("theme", help="Model to use for generating images", type=str)
args = parser.parse_args()
theme = args.theme
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
criterion = nn.CrossEntropyLoss()
num_epochs = 30
# Define a transform to normalize the data
transform = transforms.Compose([
transforms.Resize((256, 256)), # Resize images if they are not the same size
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # Normalization parameters for pre-trained models
])
genmodels = ["stable_diffusion_2_1", "realistic_vision_1_4", "kandinsky_2_2", "openjourney_v4"]
numberOfSamples = 1000
# Create datasets
dataset_real = datasets.ImageFolder(root=f"real_datasets/{theme}", transform=transform)
train_set_real, test_set_real = train_test_split(dataset_real, test_size=0.2) #, random_state=42)
train_real_indexes = max_samples_per_class(train_set_real, numberOfSamples)
train_set_real = Subset(train_set_real, train_real_indexes)
subset_real = Subset(train_set_real, np.random.choice(len(train_set_real), len(train_set_real) // 2, replace=False))
# Create dataloaders
train_loader_real = DataLoader(train_set_real, batch_size=32, shuffle=True)
test_loader_real = DataLoader(test_set_real, batch_size=32, shuffle=True)
subset_loader_real = DataLoader(subset_real, batch_size=32, shuffle=True)
# Create real model
model_real = models.resnet34()
model_real.fc = torch.nn.Sequential(
torch.nn.Linear(
in_features=512,
out_features=10 if theme == "animals" else 5
)
)
optimizer_real = torch.optim.Adam(model_real.parameters(), lr=0.001)
scheduler_real = lr_scheduler.StepLR(optimizer_real, step_size=7, gamma=0.1)
model_real.to(device)
# Cretae subset model
model_subset = models.resnet34()
model_subset.fc = torch.nn.Sequential(
torch.nn.Linear(
in_features=512,
out_features=10 if theme == "animals" else 5
)
)
optimizer_subset = torch.optim.Adam(model_subset.parameters(), lr=0.001)
scheduler_subset = lr_scheduler.StepLR(optimizer_subset, step_size=7, gamma=0.1)
model_subset.to(device)
# Real model train
for epoch in range(num_epochs):
# Training pass
model_real.train()
running_loss = 0.0
for images, labels in train_loader_real:
images, labels = images.to(device), labels.to(device)
optimizer_real.zero_grad()
outputs = model_real(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer_real.step()
running_loss += loss.item()
scheduler_real.step()
if epoch % 5 == 0:
print(f"Training Epoch [{epoch}/{num_epochs}], Real, {theme}")
# Subset model train
for epoch in range(num_epochs):
# Training pass
model_subset.train()
running_loss = 0.0
for images, labels in subset_loader_real:
images, labels = images.to(device), labels.to(device)
optimizer_subset.zero_grad()
outputs = model_subset(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer_subset.step()
running_loss += loss.item()
scheduler_subset.step()
if epoch % 5 == 0:
print(f"Training Epoch [{epoch}/{num_epochs}], Subset real, {theme}")
# Real model test
model_real.eval()
total = 0
total_correct = 0
with torch.no_grad():
for i, (images, labels) in enumerate(test_loader_real):
images, labels = images.to(device), labels.to(device)
outputs = model_real(images.to(device))
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
total_correct += (predicted == labels).sum().item()
# Log accuracy
with open(f"./testlogs.txt", "a") as f:
f.write(f"Theme: {theme}, Dataset: Real, Accuracy: {total_correct / total}\n")
# Subset model test
model_subset.eval()
total = 0
total_correct = 0
with torch.no_grad():
for i, (images, labels) in enumerate(test_loader_real):
images, labels = images.to(device), labels.to(device)
outputs = model_subset(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
total_correct += (predicted == labels).sum().item()
# Log accuracy
with open(f"./testlogs.txt", "a") as f:
f.write(f"Theme: {theme}, Dataset: Subset_real, Accuracy: {total_correct / total}\n")
for genmodel in genmodels:
# Create datasets
dataset = datasets.ImageFolder(root=f"generated_datasets/{theme}/{genmodel}/256", transform=transform)
subset = Subset(dataset, np.random.choice(len(dataset), len(dataset) // 2, replace=False))
mixed_dataset = ConcatDataset([subset, subset_real])
# Create dataloaders
train_loader = DataLoader(dataset, batch_size=32, shuffle=True)
mixed_loader = DataLoader(mixed_dataset, batch_size=32, shuffle=True)
# Create artificial model
model = models.resnet34()
model.fc = torch.nn.Sequential(
torch.nn.Linear(
in_features=512,
out_features=10 if theme == "animals" else 5
),
)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
model.to(device)
# Create mixed model
model_mixed = models.resnet34()
model_mixed.fc = torch.nn.Sequential(
torch.nn.Linear(
in_features=512,
out_features=10 if theme == "animals" else 5
),
)
optimizer_mixed = torch.optim.Adam(model_mixed.parameters(), lr=0.001)
scheduler_mixed = lr_scheduler.StepLR(optimizer_mixed, step_size=7, gamma=0.1)
model_mixed.to(device)
# Training loop
# Artificial model
for epoch in range(num_epochs):
# Training pass
model.train()
running_loss = 0.0
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
scheduler.step()
if epoch % 5 == 0:
print(f"Training Epoch [{epoch}/{num_epochs}], Artificial, {genmodel}, {theme}")
# Mixed model
for epoch in range(num_epochs):
# Training pass
model_mixed.train()
running_loss = 0.0
for images, labels in mixed_loader:
images, labels = images.to(device), labels.to(device)
optimizer_mixed.zero_grad()
outputs = model_mixed(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer_mixed.step()
running_loss += loss.item()
scheduler_mixed.step()
if epoch % 5 == 0:
print(f"Training Epoch [{epoch}/{num_epochs}], Mixed, {genmodel}, {theme}")
# Test performance
# Artificial model
model.eval()
total = 0
total_correct = 0
with torch.no_grad():
for i, (images, labels) in enumerate(test_loader_real):
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
total_correct += (predicted == labels).sum().item()
# Log accuracy
with open(f"./testlogs.txt", "a") as f:
f.write(f"Theme: {theme}, Model: {genmodel}, Dataset: Artificial, Accuracy: {total_correct / total}\n")
# Mixed model
model_mixed.eval()
total = 0
total_correct = 0
with torch.no_grad():
for i, (images, labels) in enumerate(test_loader_real):
images, labels = images.to(device), labels.to(device)
outputs = model_mixed(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
total_correct += (predicted == labels).sum().item()
# Log accuracy
with open(f"./testlogs.txt", "a") as f:
f.write(f"Theme: {theme}, Model: {genmodel}, Dataset: Mixed, Accuracy: {total_correct / total}\n")
with open(f"./testlogs.txt", "a") as f:
f.write("\n\n")
if __name__ == "__main__":
main()