-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
44 lines (35 loc) · 1.13 KB
/
train.py
File metadata and controls
44 lines (35 loc) · 1.13 KB
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
import torch
import torch.nn as nn
from matplotlib import pyplot as plt
from pathlib import Path
from model import *
from fastai.vision.all import set_seed, DataBlock, ImageBlock, get_image_files, RandomSplitter, Resize, Learner
def get_y(x):
return x
if __name__ == "__main__":
set_seed(22)
dataset_path = Path.cwd()/"dataset"
batch_size=10
img_resize_dim=256
data_block = DataBlock(
blocks=(ImageBlock, ImageBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2),
get_y=get_y,
item_tfms=Resize(img_resize_dim),
)
dls = data_block.dataloaders(dataset_path / "train", bs=batch_size)
autoencoder_model = Autoencoder()
learn = Learner(dls, autoencoder_model, loss_func = nn.MSELoss())
model_name = "Autoencoder"
try:
learn.load(model_name)
print("Model already exists, training not performed")
except:
learn.fit_one_cycle(20)
learn.save(model_name)
plt.figure(figsize=(10, 5))
learn.recorder.plot_loss()
plt.title('Training Losses')
plt.savefig('training_losses.png')
print("Training ended")