-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb8922d
commit 99c0b0f
Showing
7 changed files
with
981 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class DomainSpecificBatchNorm1D(nn.Module): | ||
_version = 2 | ||
|
||
def __init__(self,num_features, num_domains=2, eps=1e-5,momentum=0.1, affine=True, track_running_stats=True): | ||
super(DomainSpecificBatchNorm1D,self).__init__() | ||
self.bns = nn.ModuleList( | ||
[nn.BatchNorm1d(num_features,eps,momentum,affine,track_running_stats) for _ in range(num_domains)] | ||
) | ||
|
||
def reset_running_stats(self): | ||
for bn in self.bns: | ||
bn.reset_running_stats() | ||
|
||
def reset_parameters(self): | ||
for bn in self.bns: | ||
bn.reset_parameters() | ||
|
||
def _chect_input_dim(self,input): | ||
if input.dim() != 3: | ||
raise ValueError('expected 3D input, but got {}D input'.format(input.dim())) | ||
|
||
def forward(self, x, domain_label): | ||
self._chect_input_dim(x) | ||
if domain_label == 'source' or domain_label=='s': | ||
bn = self.bns[0] | ||
elif domain_label == 'target' or domain_label=='t': | ||
bn = self.bns[1] | ||
else : | ||
raise ValueError('"domain label" must be "source/s" or "target/t", but got "{}".'.format(domain_label)) | ||
return bn(x) | ||
|
||
if __name__ == "__main__": | ||
xs = torch.randn(16,32,100) # batch_size, channel, seq_len | ||
xt = torch.randn(16,32,100) | ||
dsbn = DomainSpecificBatchNorm1D(num_features=32) | ||
y1 = dsbn(xs,'source') | ||
y2 = dsbn(xt,'target') | ||
print(dsbn) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import torch | ||
import torch.nn as nn | ||
from DSBN import DomainSpecificBatchNorm1D | ||
|
||
class Swish_act(nn.Module): | ||
def __init__(self): | ||
super(Swish_act, self).__init__() | ||
|
||
def forward(self, x): | ||
x = x * torch.sigmoid(x) | ||
return x | ||
|
||
class EncoderBlock(nn.Module): | ||
def __init__(self, input_channel, output_channel, stride): | ||
super(EncoderBlock, self).__init__() | ||
self.conv1 = nn.Conv1d(input_channel, output_channel, kernel_size=3, stride=stride, padding=1) | ||
self.bn1 = DomainSpecificBatchNorm1D(output_channel) | ||
self.activation = Swish_act() | ||
self.conv2 = nn.Conv1d(output_channel, output_channel, kernel_size=3, stride=1, padding=1) | ||
self.bn2 = DomainSpecificBatchNorm1D(output_channel) | ||
|
||
self.input_channel = input_channel | ||
self.output_channel = output_channel | ||
|
||
|
||
self.conv_skip = nn.Conv1d(input_channel, output_channel, kernel_size=1, stride=stride) | ||
self.bn_skip = DomainSpecificBatchNorm1D(output_channel) | ||
|
||
def forward(self, x, domain_label): | ||
residual = x | ||
out = self.conv1(x) | ||
out = self.bn1(out,domain_label) | ||
out = self.activation(out) | ||
|
||
out = self.conv2(out) | ||
out = self.bn2(out,domain_label) | ||
|
||
if self.input_channel != self.output_channel: | ||
residual = self.conv_skip(x) | ||
residual = self.bn_skip(residual,domain_label) | ||
out = out + residual | ||
out = self.activation(out) | ||
|
||
return out | ||
|
||
class Encoder(nn.Module): | ||
''' | ||
(batch_size, Channel, Seq_len) ——> (batch_size, embedding_length) | ||
(batch_size, C, 128) --> (batch_size, 256) | ||
''' | ||
def __init__(self, input_channel=3,embedding_length=256): # (batch_size, C, 128) | ||
super(Encoder, self).__init__() | ||
self.embedding_length = embedding_length | ||
|
||
self.conv1 = nn.Conv1d(input_channel, 16, kernel_size=1, stride=1, padding=0) # batch_size, 16, 128 | ||
self.bn1 = DomainSpecificBatchNorm1D(16) | ||
self.activation = Swish_act() | ||
|
||
|
||
|
||
self.layer1 = EncoderBlock(16, 32, stride=1) # batch, 32, 128 | ||
self.layer2 = EncoderBlock(32, 64, stride=2) # batch, 64, 64 | ||
self.layer3 = EncoderBlock(64, 128, stride=2) # batch, 128, 32 | ||
self.layer4 = EncoderBlock(128, 192, stride=2) # batch, 192, 16 | ||
|
||
|
||
|
||
self.linear = nn.Sequential( | ||
nn.Linear(192 * 16, 512), | ||
nn.Dropout(), | ||
nn.Linear(512, embedding_length) | ||
) | ||
|
||
def forward(self, x, domain_label): | ||
out = self.conv1(x) | ||
out = self.bn1(out,domain_label) | ||
out = self.activation(out) | ||
|
||
|
||
out = self.layer1(out,domain_label) | ||
out = self.layer2(out,domain_label) | ||
out = self.layer3(out,domain_label) | ||
out = self.layer4(out,domain_label) | ||
pred = self.linear(out.view(out.size(0), -1)) | ||
return pred | ||
|
||
def output_dim(self): | ||
return self.embedding_length | ||
|
||
|
||
class Discriminator(nn.Module): | ||
''' | ||
(batch_size, embedding_length) --> (batch_size,) | ||
(batch_size, 256) --> (batch_size, ) | ||
''' | ||
def __init__(self, embedding_length=256, hidden_dim=128): | ||
super(Discriminator, self).__init__() | ||
self.input_dim = embedding_length | ||
self.hidden_dim = hidden_dim | ||
layers = [ | ||
nn.Linear(embedding_length, hidden_dim), | ||
nn.BatchNorm1d(hidden_dim), | ||
nn.ReLU(), | ||
nn.Linear(hidden_dim, 1), | ||
nn.Sigmoid() | ||
] | ||
self.layers = torch.nn.Sequential(*layers) | ||
|
||
def forward(self, x): | ||
return self.layers(x) | ||
|
||
class DecoderBlock(nn.Module): | ||
def __init__(self, input_channel, output_channel, stride): | ||
super(DecoderBlock, self).__init__() | ||
self.conv = nn.Sequential( | ||
nn.ConvTranspose1d(input_channel, output_channel, kernel_size=3, stride=stride, padding=1,output_padding=1), | ||
nn.BatchNorm1d(output_channel), | ||
Swish_act(), | ||
|
||
nn.ConvTranspose1d(output_channel, output_channel, kernel_size=3, stride=1, padding=1), | ||
nn.BatchNorm1d(output_channel) | ||
) | ||
|
||
self.skip_connection = nn.Sequential() | ||
#if output_channel != input_channel: | ||
self.skip_connection = nn.Sequential( | ||
nn.ConvTranspose1d(input_channel, output_channel, kernel_size=1, stride=stride,output_padding=1), | ||
nn.BatchNorm1d(output_channel) | ||
) | ||
|
||
self.Lrelu = Swish_act() | ||
|
||
def forward(self, x): | ||
out = self.conv(x) | ||
#print(out.shape,x.shape,self.skip_connection(x).shape) | ||
out = self.skip_connection(x) + out | ||
out = self.Lrelu(out) | ||
return out | ||
|
||
class Decoder(nn.Module): | ||
''' | ||
(batch_size, embedding_length) --> (batch_size, Channel, Seq_len) | ||
(batch_size, 256) --> (batch_size, C, 128) | ||
''' | ||
def __init__(self,embedding_length=256,output_channel=3): | ||
super(Decoder,self).__init__() | ||
self.embedding_length = embedding_length | ||
self.output_channel = output_channel | ||
|
||
self.linear = nn.Linear(embedding_length,1024) # batch_size, 1, 1024 -> batch_szie,32,32 | ||
|
||
self.layer1 = DecoderBlock(32,32,2) # batch_size, 32, 64 | ||
self.layer2 = DecoderBlock(32,32,2) # batch_szie, 32,128 | ||
|
||
self.conv1 = nn.Conv1d(32, output_channel,kernel_size=1) | ||
|
||
|
||
def forward(self,x): | ||
batch_size = x.shape[0] | ||
x = self.linear(x) | ||
x = x.view(batch_size,32,32) | ||
out = self.layer1(x) | ||
out = self.layer2(out) | ||
out = self.conv1(out) | ||
return out | ||
|
||
class Predictor(nn.Module): | ||
''' | ||
(batch_size, embedding_length) --> (batch_size,) | ||
(batch_size, 256) --> (batch_size, ) | ||
''' | ||
def __init__(self,embedding_length=256): | ||
super(Predictor,self).__init__() | ||
self.predit = nn.Sequential( | ||
nn.Linear(embedding_length,128), | ||
nn.ReLU(), | ||
nn.Linear(128, 1) | ||
) | ||
def forward(self,x): | ||
out = self.predit(x) | ||
return out | ||
|
||
if __name__ == '__main__': | ||
batch_size,input_channel, seq_len = 16, 3, 256 | ||
x = torch.randn(batch_size,seq_len) | ||
p = Decoder() | ||
y = p(x) | ||
print(y.shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from torch.utils.data import TensorDataset,DataLoader | ||
import numpy as np | ||
from utils import Scaler | ||
import torch | ||
|
||
|
||
|
||
def load_data(args): | ||
window_len = args.window_len | ||
if args.merge_direction == 'feature': | ||
merge_direction = 1 | ||
else: | ||
merge_direction = 0 | ||
source_dataset = args.source_dataset | ||
target_dataset = args.target_dataset | ||
|
||
############ source data ########### | ||
source_x = [] | ||
source_y = [] | ||
for i in range(len(eval(f"args.{source_dataset}")['x'])): | ||
x_path = f'data/{source_dataset}/' + eval(f"args.{source_dataset}")['x'][i] + '.npy' | ||
y_path = f'data/{source_dataset}/' + eval(f"args.{source_dataset}")['y'][i] + '.npy' | ||
x_i = np.load(x_path) | ||
y_i = np.load(y_path) | ||
for j in range(x_i.shape[0]-window_len+1): # sliding window | ||
source_x.append(np.concatenate(x_i[j:j+window_len],axis=merge_direction)) | ||
source_y.append(y_i[j+window_len-1]) | ||
source_x = np.array(source_x,dtype=np.float32) | ||
source_y = np.array(source_y,dtype=np.float32) | ||
|
||
########### target data ########### | ||
target_num = len(eval(f"args.{target_dataset}")['x']) | ||
target_train_num = target_num - args.target_test_num | ||
target_train_x = [] | ||
target_train_y = [] | ||
target_test_x = [] | ||
target_test_y = [] | ||
count = 0 | ||
for i in range(len(eval(f"args.{target_dataset}")['x'])): | ||
count += 1 | ||
x_path = f'data/{target_dataset}/' + eval(f"args.{target_dataset}")['x'][i] + '.npy' | ||
y_path = f'data/{target_dataset}/' + eval(f"args.{target_dataset}")['y'][i] + '.npy' | ||
x_i = np.load(x_path) | ||
y_i = np.load(y_path) | ||
for j in range(x_i.shape[0] - window_len + 1): # sliding window | ||
if count <= target_train_num: | ||
target_train_x.append(np.concatenate(x_i[j:j + window_len], axis=merge_direction)) | ||
target_train_y.append(y_i[j + window_len - 1]) | ||
else: | ||
target_test_x.append(np.concatenate(x_i[j:j + window_len], axis=merge_direction)) | ||
target_test_y.append(y_i[j + window_len - 1]) | ||
target_train_x = np.array(target_train_x, dtype=np.float32) | ||
target_train_y = np.array(target_train_y, dtype=np.float32) | ||
target_test_x = np.array(target_test_x, dtype=np.float32) | ||
target_test_y = np.array(target_test_y, dtype=np.float32) | ||
|
||
print('source :', source_x.shape, source_y.shape) | ||
print('target train :', target_train_x.shape,target_train_y.shape) | ||
print('target test :',target_test_x.shape, target_test_y.shape) | ||
|
||
|
||
############ Normalize ############## | ||
print('-' * 30) | ||
print('normalized data !') | ||
if args.normalize_type == 'minmax': | ||
target_train_x, target_test_x = Scaler(target_train_x,target_test_x).minmax() | ||
target_train_y, target_test_y = Scaler(target_train_y, target_test_y).minmax() | ||
source_x = Scaler(source_x).minmax() | ||
source_y = Scaler(source_y).minmax() | ||
elif args.normalize_type == 'standerd': | ||
target_train_x, target_test_x = Scaler(target_train_x,target_test_x).standerd() | ||
target_train_y, target_test_y = Scaler(target_train_y, target_test_y).standerd() | ||
source_x = Scaler(source_x).standerd() | ||
source_y = Scaler(source_y).standerd() | ||
|
||
########## dataloader ############# | ||
target_train_x = torch.from_numpy(np.transpose(target_train_x,(0,2,1))) | ||
target_train_y = torch.from_numpy(target_train_y).view(-1,1) | ||
target_test_x = torch.from_numpy(np.transpose(target_test_x,(0,2,1))) | ||
target_test_y = torch.from_numpy(target_test_y).view(-1,1) | ||
source_x = torch.from_numpy(np.transpose(source_x,(0,2,1))) | ||
source_y = torch.from_numpy(source_y).view(-1,1) | ||
|
||
source_loader = DataLoader(TensorDataset(source_x, source_y), batch_size=args.batch_size, shuffle=True) | ||
target_train_loader = DataLoader(TensorDataset(target_train_x, target_train_y), batch_size=args.batch_size, | ||
shuffle=True) | ||
target_test_loader = DataLoader(TensorDataset(target_test_x, target_test_y), batch_size=args.batch_size, | ||
shuffle=False) | ||
return source_loader, target_train_loader, target_test_loader | ||
|
||
|
||
if __name__ == '__main__': | ||
from main import get_args | ||
args = get_args() | ||
load_data(args) |
Oops, something went wrong.