import argparse import datetime import logging import numpy as np impo

  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
import argparse
import datetime
import logging
import numpy as np
import operator
import os
import pandas as pd
import re
import shutil
import sys
import torchvision
from tensorboardX import SummaryWriter
from time import gmtime, strftime
import torch
from tqdm import tqdm
from torch.utils.data import DataLoader
from torchvision import transforms
from src.data_loader import MakeDataSynt
from src.loss import MSE_synthetic_loss
from src.models import DLD
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, required=True, help='What model to use, one of ["DLD", ]', default='DLD')
parser.add_argument('--n_epochs', type=int, help='Num of epochs for training', default=10)
parser.add_argument('--datadir', type=str, help='Path to training dataset')
parser.add_argument('--valdatadir', type=str, help='Path to validation dataset')
parser.add_argument('--batch_size', type=int, default=8)
parser.add_argument('--name', type=str, help='Name of the experiment')
args = parser.parse_args()
return args
def get_dataloaders(args):
train_transform = transforms.Compose([
transforms.ToTensor(),
transforms.ColorJitter()
])
dset_synt = MakeDataSynt(args.datadir, args.datadir, train_transform)
dset_val_synt = MakeDataSynt(args.valdatadir, args.valdatadir, train_transform)
train_loader = DataLoader(dset_synt,
batch_size=1,
shuffle=True,
num_workers=0, # 1 for CUDA
pin_memory=False)
val_loader = DataLoader(dset_val_synt,
batch_size=1,
shuffle=True,
num_workers=0, # 1 for CUDA
pin_memory=False)
return train_loader, val_loader
def validate(tb, val_loader, model, device, global_step):
val_loss_epoch = []
for X_batch, y_batch_h, y_batch_nh in tqdm(val_loader):
X_batch = torch.FloatTensor(X_batch).to(device)
y_batch_nh = y_batch_nh.type(torch.FloatTensor).to(device)
y_batch_h = y_batch_h.type(torch.FloatTensor).to(device)
loss = MSE_synthetic_loss(X_batch, y_batch_nh, y_batch_h, model, device)
val_loss_epoch.append(loss.cpu().data.numpy())
del loss
tb.add_scalar('val_loss', np.mean(val_loss_epoch), global_step=global_step)
out = model(X_batch)[0]
out_grid = torchvision.utils.make_grid(out.cpu())
input_grid = torchvision.utils.make_grid(X_batch.cpu())
tb.add_image(tag='val_out', img_tensor=out_grid, global_step=global_step)
tb.add_image(tag='val_input', img_tensor=input_grid, global_step=global_step)
def main(args):
tb_dir = './tb_logs/' + args.name + ' ' + strftime("%Y-%m-%d %H:%M:%S", gmtime())
tb = SummaryWriter(tb_dir)
device = torch.device("cuda:0")
train_loader, val_loader = get_dataloaders(args)
model = DLD().to(device)
# model.load_state_dict(torch.load('./model.pth'))
model.eval()
tb.add_text(tag='model', text_string=repr(model))
opt = torch.optim.Adam(model.parameters(), lr=0.001)
global_step = 0
for epoch in range(args.n_epochs):
validate(tb, val_loader, model, device, global_step=global_step)
model.train(True)
for X_batch, y_batch_h, y_batch_nh in tqdm(train_loader):
X_batch = torch.FloatTensor(X_batch).to(device)
y_batch_nh = y_batch_nh.type(torch.FloatTensor).to(device)
y_batch_h = y_batch_h.type(torch.FloatTensor).to(device)
loss = MSE_synthetic_loss(X_batch, y_batch_nh, y_batch_h, model, device)
loss.backward()
opt.step()
opt.zero_grad()
tb.add_scalar('train_loss', loss.cpu().data.numpy(), global_step=global_step)
global_step += 1
out = model(X_batch)[0]
out_grid = torchvision.utils.make_grid(out.cpu())
input_grid = torchvision.utils.make_grid(X_batch.cpu())
tb.add_image(tag='train_out', img_tensor=out_grid, global_step=global_step)
tb.add_image(tag='train_input', img_tensor=input_grid, global_step=global_step)
model.train(False)
if __name__ == '__main__':
args = parse_args()
main(args)