-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombined_model.py
122 lines (91 loc) · 3.63 KB
/
combined_model.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
from testmodel import load_resnet, change_model
import torch.nn as nn
import torch
from learning_rate_cyclic import train_model
from text_model import load_data_loaders
import torch.optim as optim
import matplotlib.pyplot as plt
from text_model import create_model_3, create_model_19
from combined_dataloaders import *
class CombinedModel(nn.Module):
"""
Model combining cover and title
"""
def __init__(self, n_outputs = 30):
super().__init__()
resnet = 18
trained_layers = 10
self.image_model = load_resnet(resnet)
self.image_model = change_model(self.image_model, trained_layers, 30)
self.image_model.load_state_dict(torch.load("cover_final/64 w relu/model64"))
removed = list(self.image_model.fc.children())[:-2]
self.image_model.fc = nn.Sequential(*removed)
self.text_model = create_model_3(4096, 30)
self.text_model.load_state_dict(torch.load("text_models/final_text_model_adam.pt"))
removed = list(self.text_model.children())[:-2]
self.text_model = nn.Sequential(*removed)
self.join_layer = nn.Sequential(nn.Dropout(0.5),
nn.Linear(1256, n_outputs),
nn.Softmax(0)
)
def forward(self, inputs):
cover = inputs[0]
title_emb = inputs[1]
cover_output = self.image_model(cover)
title_output = self.text_model(title_emb)
merged_output = torch.cat((cover_output, title_output), 1)
return self.join_layer(merged_output)
def test_combined_model():
"""
Test and save final combined model
"""
BATCH_SIZE = 32
EPOCHS = 20
LR = 0.001
MODEL_DIR = "combined_models/"
PLOT_DIR = "plots_combined_model/"
model = CombinedModel()
data_loaders = load_data_loaders("dataloaders/combined_data_loaders_{}.pickle".format(BATCH_SIZE))
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
dataset_sizes = {phase: len(data_loader.dataset) for phase, data_loader in data_loaders.items()}
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr = LR)
print("train")
model, stats, lrstats = train_model(model, data_loaders, dataset_sizes, BATCH_SIZE, criterion, optimizer, num_epochs = EPOCHS, device = device, clip_gradient = True)
print(stats)
torch.save(model.state_dict(), MODEL_DIR + "combined_model.pt")
plt.plot(stats.epochs['val'], stats.accuracies['val'])
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.grid(True)
plt.savefig(PLOT_DIR + "combined_model.pdf")
return model, stats, lrstats
def test_combined_model_10():
"""
Test sans save final combined model for 10 classes dataset
"""
BATCH_SIZE = 4
EPOCHS = 20
LR = 0.001
MODEL_DIR = "combined_models/"
PLOT_DIR = "plots_combined_model/"
model = CombinedModel(10)
data_loaders = load_data_loaders("dataloaders/combined_data_loaders_{}_10.pickle".format(BATCH_SIZE))
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
dataset_sizes = {phase: len(data_loader.dataset) for phase, data_loader in data_loaders.items()}
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr = LR)
print("train")
model, stats, lrstats = train_model(model, data_loaders, dataset_sizes, BATCH_SIZE, criterion, optimizer, num_epochs = EPOCHS, device = device, clip_gradient = True)
print(stats)
torch.save(model.state_dict(), MODEL_DIR + "combined_model_10.pt")
plt.plot(stats.epochs['val'], stats.accuracies['val'])
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.grid(True)
plt.savefig(PLOT_DIR + "combined_model_10.pdf")
return model, stats, lrstats
if __name__ == "__main__":
test_combined_model_10()