27 changed files with 299 additions and 1658 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB |
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB |
File diff suppressed because one or more lines are too long
@ -0,0 +1,110 @@ |
|||||
|
import argparse |
||||
|
import os |
||||
|
from utils import utils |
||||
|
import torch |
||||
|
|
||||
|
|
||||
|
class BaseOptions(): |
||||
|
"""This class defines options used during both training and test time. |
||||
|
|
||||
|
It also implements several helper functions such as parsing, printing, and saving the options. |
||||
|
It also gathers additional options defined in <modify_commandline_options> functions in both dataset class and model class. |
||||
|
""" |
||||
|
|
||||
|
def __init__(self): |
||||
|
"""Reset the class; indicates the class hasn't been initailized""" |
||||
|
self.initialized = False |
||||
|
|
||||
|
def initialize(self, parser): |
||||
|
"""Define the common options that are used in both training and test.""" |
||||
|
# basic parameters |
||||
|
parser.add_argument('--dataroot', type=str, default='./datasets', help='root path to datasets') |
||||
|
parser.add_argument('--name', type=str, default='experiment_name', help='name of the experiment. It decides where to store samples and models') |
||||
|
parser.add_argument('--gpu_id', type=str, default='0', help='gpu ids: e.g. 0, 1, ... . use -1 for CPU') |
||||
|
parser.add_argument('--device', type=str, default='cuda:0', help='generate device with gpu_id and usable user device') |
||||
|
parser.add_argument('--checkpoints_dir', type=str, default='./checkpoints', help='models are saved here') |
||||
|
parser.add_argument('--ms_ratio', type=int, default=5, help='multiscale ratio') |
||||
|
|
||||
|
# model parameters |
||||
|
parser.add_argument('--model', type=str, default='ANN', help='chooses which model to use. [ANN | CNN | AutoEncoder]') |
||||
|
# dataset parameters |
||||
|
# parser.add_argument('--resolution', type=str, default='180_60', help='data resolution. nelx_nely here') |
||||
|
parser.add_argument('--nelx', type=int, default=180, help='num of elements on x-axis') |
||||
|
parser.add_argument('--nely', type=int, default=60, help='num of elements on y-axis') |
||||
|
parser.add_argument('--nelz', type=int, default=1, help='num of elements on z-axis') |
||||
|
parser.add_argument('--dimension', type=int, default=2, help='dimension of dataset models') |
||||
|
parser.add_argument('--is_standard', type=bool, default=False, help='whether need standardization or not') |
||||
|
|
||||
|
# additional parameters |
||||
|
parser.add_argument('--epoch', type=str, default='latest', help='which epoch to load? set to latest to use latest cached model') |
||||
|
parser.add_argument('--load_iter', type=int, default=0, help='which iteration to load? if load_iter > 0, the code will load models by iter_[load_iter]; otherwise, the code will load models by [epoch]') |
||||
|
parser.add_argument('--verbose', action='store_true', help='if specified, print more debugging information') |
||||
|
parser.add_argument('--suffix', type=str, default='', help='customized suffix: opt.name = opt.name + suffix: e.g., {model}_{netG}_size{load_size}') |
||||
|
|
||||
|
# identify initializiation timing |
||||
|
self.initialized = True |
||||
|
return parser |
||||
|
|
||||
|
def gather_options(self): |
||||
|
"""Initialize our parser with basic options(only once). |
||||
|
Add additional model-specific and dataset-specific options. |
||||
|
These options are defined in the <modify_commandline_options> function |
||||
|
in model and dataset classes. |
||||
|
""" |
||||
|
if not self.initialized: # check if it has been initialized |
||||
|
parser = argparse.ArgumentParser() # customize help formatting with <formatter_class> |
||||
|
parser = self.initialize(parser) |
||||
|
|
||||
|
# get the basic options |
||||
|
opt, _ = parser.parse_known_args() |
||||
|
|
||||
|
# save and return the parser |
||||
|
self.parser = parser |
||||
|
return parser.parse_args() |
||||
|
|
||||
|
def print_options(self, opt): |
||||
|
"""Print and save options |
||||
|
|
||||
|
It will print both current options and default values(if different). |
||||
|
It will save options into a text file / [checkpoints_dir] / opt.txt |
||||
|
""" |
||||
|
message = '' |
||||
|
message += '----------------- Options ---------------\n' |
||||
|
for k, v in sorted(vars(opt).items()): |
||||
|
comment = '' |
||||
|
default = self.parser.get_default(k) |
||||
|
if v != default: |
||||
|
comment = '\t[default: %s]' % str(default) |
||||
|
message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment) |
||||
|
message += '----------------- End -------------------' |
||||
|
print(message) |
||||
|
|
||||
|
# save to the disk |
||||
|
expr_dir = os.path.join(opt.checkpoints_dir, opt.model+'_'+opt.mod) |
||||
|
utils.mkdir(expr_dir) |
||||
|
file_name = os.path.join(expr_dir, '{}_opt.txt'.format(opt.phase)) |
||||
|
with open(file_name, 'wt') as opt_file: |
||||
|
opt_file.write(message) |
||||
|
opt_file.write('\n') |
||||
|
|
||||
|
def parse(self): |
||||
|
"""Parse our options, create checkpoints directory suffix, and set up gpu device.""" |
||||
|
opt = self.gather_options() |
||||
|
opt.isTrain = self.isTrain # train or test |
||||
|
|
||||
|
# process opt.suffix |
||||
|
if opt.suffix: |
||||
|
suffix = ('_' + opt.suffix.format(**vars(opt))) if opt.suffix != '' else '' |
||||
|
opt.name = opt.name + suffix |
||||
|
|
||||
|
self.print_options(opt) |
||||
|
|
||||
|
# set device with gpu id |
||||
|
if opt.gpu_id == -1: |
||||
|
opt.device = 'cpu' |
||||
|
else: |
||||
|
opt.device = f'cuda:{opt.gpu_id}' if torch.cuda.is_available() else 'cpu' |
||||
|
|
||||
|
self.opt = opt |
||||
|
return self.opt |
||||
|
|
@ -0,0 +1,19 @@ |
|||||
|
from .base_options import BaseOptions |
||||
|
|
||||
|
|
||||
|
class TestOptions(BaseOptions): |
||||
|
"""This class includes test options. |
||||
|
|
||||
|
It also includes shared options defined in BaseOptions. |
||||
|
""" |
||||
|
|
||||
|
def initialize(self, parser): |
||||
|
parser = BaseOptions.initialize(self, parser) # define shared options |
||||
|
parser.add_argument('--results_dir', type=str, default='./results', help='saves results here.') |
||||
|
parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') |
||||
|
parser.add_argument('--mod', type=str, default='mod3', help='chooses which dataset model for test. mod1....') |
||||
|
parser.add_argument('--pretrained_model_path', type=str, default='./checkpoints/ANN_mod1/ANN_mod1_opt.pt', help='pretrained model file load path') |
||||
|
|
||||
|
|
||||
|
self.isTrain = False |
||||
|
return parser |
@ -0,0 +1,23 @@ |
|||||
|
from .base_options import BaseOptions |
||||
|
|
||||
|
|
||||
|
class TrainOptions(BaseOptions): |
||||
|
"""This class includes training options. |
||||
|
|
||||
|
It also includes shared options defined in BaseOptions. |
||||
|
""" |
||||
|
|
||||
|
def initialize(self, parser): |
||||
|
parser = BaseOptions.initialize(self, parser) |
||||
|
# visdom and HTML visualization parameters |
||||
|
|
||||
|
# network saving and loading parameters |
||||
|
parser.add_argument('--phase', type=str, default='train', help='train, val, test, etc') |
||||
|
|
||||
|
# training parameters |
||||
|
parser.add_argument('--epochs', type=int, default=10000, help='number of epochs') |
||||
|
parser.add_argument('--lr', type=float, default=0.001, help='initial learning rate for adam') |
||||
|
parser.add_argument('--mod', type=str, default='mod2', help='chooses which dataset model for train. mod1....') |
||||
|
|
||||
|
self.isTrain = True |
||||
|
return parser |
@ -1,85 +1,76 @@ |
|||||
import numpy as np |
|
||||
import time |
import time |
||||
|
import os |
||||
|
import numpy as np |
||||
import matplotlib.pyplot as plt |
import matplotlib.pyplot as plt |
||||
|
|
||||
from sklearn.model_selection import train_test_split |
|
||||
from utils.data_standardizer import standardization |
|
||||
from utils.data_loader import data_loader |
|
||||
|
|
||||
import torch |
import torch |
||||
import torch.nn as nn |
import torch.nn as nn |
||||
import torch.nn.functional as F |
import torch.nn.functional as F |
||||
|
|
||||
from models.ANN import ANN_Model |
from utils.data_standardizer import standardization |
||||
|
from utils.data_loader import data_loader |
||||
|
from options.train_options import TrainOptions |
||||
|
|
||||
|
from models.ANN import ANN_Model |
||||
|
|
||||
def train(X, Y, epochs=10000, mod='mod1', standard = False, device = 0): |
def train(X, Y, opt): |
||||
if standard: |
if opt.is_standard: |
||||
X = standardization(X) |
X = standardization(X) |
||||
Y = standardization(Y) |
Y = standardization(Y) |
||||
|
|
||||
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,random_state=0) |
X_train=torch.from_numpy(X).type(torch.float32).to(opt.device) |
||||
|
Y_train=torch.from_numpy(Y).type(torch.float32).to(opt.device) |
||||
device = f'cuda:{device}' if torch.cuda.is_available() else 'cpu' |
|
||||
|
|
||||
X_train=torch.from_numpy(X_train).type(torch.float32).to(device) |
|
||||
X_test=torch.from_numpy(X_test).type(torch.float32).to(device) |
|
||||
Y_train=torch.from_numpy(Y_train).type(torch.float32).to(device) |
|
||||
Y_test=torch.from_numpy(Y_test).type(torch.float32).to(device) |
|
||||
|
|
||||
# Load net model |
# Load net model |
||||
torch.manual_seed(20) |
torch.manual_seed(20) |
||||
model = ANN_Model() |
model_name=opt.model+'_Model' |
||||
# model = CNN_Model() |
model = eval(model_name)() # ANN_Model() as default |
||||
# model = ENCODER_Model() |
|
||||
model.parameters |
model.parameters |
||||
model=model.to(device) |
model=model.to(opt.device) |
||||
print(model) |
print(model) |
||||
|
|
||||
# Set loss function |
# Set loss function |
||||
loss_function = nn.MSELoss() |
loss_function = nn.MSELoss() |
||||
# MSE_loss=nn.MSELoss() |
|
||||
# BCE_loss=nn.BCELoss() |
|
||||
|
|
||||
# Set adam optimizer |
# Set adam optimizer |
||||
optimizer=torch.optim.Adam(model.parameters(),lr=0.001) # ANN 学习率最好0.001 左右(无归一化) |
optimizer=torch.optim.Adam(model.parameters(),lr=opt.lr) # ANN 学习率最好0.001 左右(无归一化) |
||||
|
|
||||
# Train |
# Train |
||||
start_time=time.time() |
start_time=time.time() |
||||
losses=[] |
losses=[] |
||||
for i in range(epochs): |
for i in range(opt.epochs): |
||||
pred = model.forward(X_train) |
pred = model.forward(X_train) |
||||
loss=loss_function(pred,Y_train) |
loss=loss_function(pred,Y_train) |
||||
# loss.requires_grad_(True) |
# loss.requires_grad_(True) |
||||
losses.append(loss.cpu().detach().numpy()) |
losses.append(loss.cpu().detach().numpy()) |
||||
if i%(epochs/10)==1: |
if i%(opt.epochs/10)==1: |
||||
print("Epoch number: {} and the loss : {}".format(i,loss.item())) |
print("Epoch number: {} and the loss : {}".format(i,loss.item())) |
||||
optimizer.zero_grad() |
optimizer.zero_grad() |
||||
loss.backward() |
loss.backward() |
||||
optimizer.step() |
optimizer.step() |
||||
print(time.time()-start_time) |
print(time.time()-start_time) |
||||
|
|
||||
torch.save(model, 'checkpoints/' + str(model).split('_')[0] + '_' + mod + '_' + 'opt.pt') |
# save trained model, mkdir opt has done in options/base_options.py |
||||
|
save_path=os.path.join(opt.checkpoints_dir, opt.model+'_'+opt.mod, opt.model+'_'+opt.mod+'_opt.pt') |
||||
|
torch.save(model, save_path) |
||||
|
|
||||
return losses |
return losses |
||||
|
|
||||
|
|
||||
if __name__=='__main__': |
if __name__=='__main__': |
||||
# Load datasets |
# Load parmetaers |
||||
# train data select: |
opt = TrainOptions().parse() |
||||
data_mod='mod1' # opt: mod1 mod2 mod3 |
|
||||
|
|
||||
dst_path='datasets/top88_'+ data_mod + '_xPhys_180_60.npy' |
# Load datasets, mod1 as default |
||||
U_path='datasets/top88_'+ data_mod + '_u_180_60.npy' |
global_density, global_displace, coarse_density, coarse_displace, fine_displace = data_loader(opt) |
||||
global_density, global_displace, coarse_density, coarse_displace, fine_displace = data_loader(dst_path, U_path) |
|
||||
X = np.hstack((coarse_density[:,:] , coarse_displace[:,:,0] , coarse_displace[:,:,1])) |
X = np.hstack((coarse_density[:,:] , coarse_displace[:,:,0] , coarse_displace[:,:,1])) |
||||
Y = fine_displace[:,:] |
Y = fine_displace[:,:] |
||||
|
|
||||
# Train |
# Train |
||||
losses = train(X, Y, epochs=10000, mod=data_mod) |
losses = train(X, Y, opt) |
||||
|
|
||||
# plot loss |
# plot loss |
||||
plt.plot(range(10000),losses) |
plt.plot(range(opt.epochs),losses) |
||||
plt.ylabel('Loss') |
plt.ylabel('Loss') |
||||
plt.xlabel('Epoch') |
plt.xlabel('Epoch') |
||||
plt.show() |
plt.show() |
||||
|
@ -0,0 +1,23 @@ |
|||||
|
import os |
||||
|
|
||||
|
def mkdirs(paths): |
||||
|
"""create empty directories if they don't exist |
||||
|
|
||||
|
Parameters: |
||||
|
paths (str list) -- a list of directory paths |
||||
|
""" |
||||
|
if isinstance(paths, list) and not isinstance(paths, str): |
||||
|
for path in paths: |
||||
|
mkdir(path) |
||||
|
else: |
||||
|
mkdir(paths) |
||||
|
|
||||
|
|
||||
|
def mkdir(path): |
||||
|
"""create a single empty directory if it didn't exist |
||||
|
|
||||
|
Parameters: |
||||
|
path (str) -- a single directory path |
||||
|
""" |
||||
|
if not os.path.exists(path): |
||||
|
os.makedirs(path) |
Loading…
Reference in new issue