-
Notifications
You must be signed in to change notification settings - Fork 15
/
pytorch_lightning_regression_image_only.py
174 lines (121 loc) · 4.92 KB
/
pytorch_lightning_regression_image_only.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
import pandas as pd
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
from torchvision import transforms
import pytorch_lightning as pl
from pytorch_lightning.logging import TensorBoardLogger
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
data_path = "./data/"
class ImageDataset(Dataset):
"""Tabular and Image dataset."""
def __init__(self, pickle_file, image_dir):
self.image_dir = image_dir
self.pickle_file = pickle_file
self.tabular = pd.read_pickle(pickle_file)
def __len__(self):
return len(self.tabular)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
tabular = self.tabular.iloc[idx, 0:]
y = tabular["price"]
image = Image.open(f"{self.image_dir}/{tabular['zpid']}.png")
image = np.array(image)
image = image[..., :3]
image = transforms.functional.to_tensor(image)
tabular = tabular[["latitude", "longitude", "beds", "baths", "area"]]
tabular = tabular.tolist()
tabular = torch.FloatTensor(tabular)
return image, y
def conv_block(input_size, output_size):
block = nn.Sequential(
nn.Conv2d(input_size, output_size, (3, 3)), nn.ReLU(), nn.BatchNorm2d(output_size), nn.MaxPool2d((2, 2)),
)
return block
class LitClassifier(pl.LightningModule):
def __init__(
self, lr: float = 1e-3, num_workers: int = 4, batch_size: int = 32,
):
super().__init__()
self.lr = lr
self.num_workers = num_workers
self.batch_size = batch_size
self.conv1 = conv_block(3, 16)
self.conv2 = conv_block(16, 32)
self.conv3 = conv_block(32, 64)
self.ln1 = nn.Linear(64 * 26 * 26, 16)
self.relu = nn.ReLU()
self.batchnorm = nn.BatchNorm1d(16)
self.dropout = nn.Dropout2d(0.5)
self.ln2 = nn.Linear(16, 4)
self.ln3 = nn.Linear(4, 1)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = x.reshape(x.shape[0], -1)
x = self.ln1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.ln2(x)
x = self.relu(x)
return self.ln3(x)
def training_step(self, batch, batch_idx):
x, y = batch
criterion = torch.nn.L1Loss()
y_pred = torch.flatten(self(x))
y_pred = y_pred.double()
loss = criterion(y_pred, y)
tensorboard_logs = {"train_loss": loss}
return {"loss": loss, "log": tensorboard_logs}
def validation_step(self, batch, batch_idx):
x, y = batch
criterion = torch.nn.L1Loss()
y_pred = torch.flatten(self(x))
y_pred = y_pred.double()
val_loss = criterion(y_pred, y)
return {"val_loss": val_loss}
def validation_epoch_end(self, outputs):
avg_loss = torch.stack([x["val_loss"] for x in outputs]).mean()
tensorboard_logs = {"val_loss": avg_loss}
return {"val_loss": avg_loss, "log": tensorboard_logs}
def test_step(self, batch, batch_idx):
x, y = batch
criterion = torch.nn.L1Loss()
y_pred = torch.flatten(self(x))
y_pred = y_pred.double()
test_loss = criterion(y_pred, y)
return {"test_loss": test_loss}
def test_epoch_end(self, outputs):
avg_loss = torch.stack([x["test_loss"] for x in outputs]).mean()
logs = {"test_loss": avg_loss}
return {"test_loss": avg_loss, "log": logs, "progress_bar": logs}
def setup(self, stage):
image_data = ImageDataset(pickle_file=f"{data_path}df.pkl", image_dir=f"{data_path}processed_images/")
train_size = int(0.80 * len(image_data))
val_size = int((len(image_data) - train_size) / 2)
test_size = int((len(image_data) - train_size) / 2)
self.train_set, self.val_set, self.test_set = random_split(image_data, (train_size, val_size, test_size))
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=(self.lr))
def train_dataloader(self):
return DataLoader(self.train_set, batch_size=self.batch_size)
def val_dataloader(self):
return DataLoader(self.val_set, batch_size=self.batch_size)
def test_dataloader(self):
return DataLoader(self.test_set, batch_size=self.batch_size)
if __name__ == "__main__":
logger = TensorBoardLogger("lightning_logs", name="image_only")
early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=5000, patience=7, verbose=False, mode="min")
model = LitClassifier()
trainer = pl.Trainer(gpus=1, logger=logger, early_stop_callback=early_stop_callback)
lr_finder = trainer.lr_find(model)
fig = lr_finder.plot(suggest=True, show=True)
new_lr = lr_finder.suggestion()
print(new_lr)
model.hparams.lr = new_lr
trainer.fit(model)
trainer.test(model)