transformer.py 4.4 KB
Newer Older
H
hypox64 已提交
1 2
import os
import random
H
hypox64 已提交
3 4
import numpy as np
import torch
H
hypox64 已提交
5
from . import dsp
H
hypox64 已提交
6
from . import array_operation as arr
H
hypox64 已提交
7
# import dsp
H
hypox64 已提交
8 9 10 11 12 13 14 15

def shuffledata(data,target):
    state = np.random.get_state()
    np.random.shuffle(data)
    np.random.set_state(state)
    np.random.shuffle(target)
    # return data,target

H
hypox64 已提交
16 17 18 19
def k_fold_generator(length,fold_num,separated=False):
    if separated:
        sequence = np.linspace(0, length-1,num = length,dtype='int')
        return sequence
H
hypox64 已提交
20
    else:
H
hypox64 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
        if fold_num == 0 or fold_num == 1:
            train_sequence = np.linspace(0,int(length*0.8)-1,int(length*0.8),dtype='int')[None]
            test_sequence = np.linspace(int(length*0.8),length-1,int(length*0.2),dtype='int')[None]
        else:
            sequence = np.linspace(0,length-1,length,dtype='int')
            train_length = int(length/fold_num*(fold_num-1))
            test_length = int(length/fold_num)
            train_sequence = np.zeros((fold_num,train_length), dtype = 'int')
            test_sequence = np.zeros((fold_num,test_length), dtype = 'int')
            for i in range(fold_num):
                test_sequence[i] = (sequence[test_length*i:test_length*(i+1)])[:test_length]
                train_sequence[i] = np.concatenate((sequence[0:test_length*i],sequence[test_length*(i+1):]),axis=0)[:train_length]
        return train_sequence,test_sequence
H
hypox64 已提交
34

H
hypox64 已提交
35 36 37 38 39 40 41
def batch_generator(data,target,sequence,shuffle = True):
    batchsize = len(sequence)
    out_data = np.zeros((batchsize,data.shape[1],data.shape[2]), data.dtype)
    out_target = np.zeros((batchsize), target.dtype)
    for i in range(batchsize):
        out_data[i] = data[sequence[i]]
        out_target[i] = target[sequence[i]]
H
hypox64 已提交
42 43

    return out_data,out_target
H
hypox64 已提交
44

H
hypox64 已提交
45

H
hypox64 已提交
46
def ToTensor(data,target=None,gpu_id=0):
H
hypox64 已提交
47 48 49
    if target is not None:
        data = torch.from_numpy(data).float()
        target = torch.from_numpy(target).long()
H
hypox64 已提交
50
        if gpu_id != -1:
H
hypox64 已提交
51 52 53 54 55
            data = data.cuda()
            target = target.cuda()
        return data,target
    else:
        data = torch.from_numpy(data).float()
H
hypox64 已提交
56
        if gpu_id != -1:
H
hypox64 已提交
57 58
            data = data.cuda()
        return data
H
hypox64 已提交
59 60

def random_transform_1d(data,finesize,test_flag):
H
hypox64 已提交
61
    batch_size,ch,length = data.shape
H
hypox64 已提交
62

H
hypox64 已提交
63 64
    if test_flag:
        move = int((length-finesize)*0.5)
H
hypox64 已提交
65
        result = data[:,:,move:move+finesize]
H
hypox64 已提交
66 67 68
    else:
        #random crop    
        move = int((length-finesize)*random.random())
H
hypox64 已提交
69
        result = data[:,:,move:move+finesize]
H
hypox64 已提交
70 71
        #random flip
        if random.random()<0.5:
H
hypox64 已提交
72
            result = result[:,:,::-1]
H
hypox64 已提交
73
        #random amp
H
hypox64 已提交
74 75
        result = result*random.uniform(0.9,1.1)
        #add noise
76 77
        # noise = np.random.rand(ch,finesize)
        # result = result + (noise-0.5)*0.01
H
hypox64 已提交
78 79 80 81 82 83 84 85 86 87
    return result

def random_transform_2d(img,finesize = (224,122),test_flag = True):
    h,w = img.shape[:2]
    if test_flag:
        h_move = 2
        w_move = int((w-finesize[1])*0.5)
        result = img[h_move:h_move+finesize[0],w_move:w_move+finesize[1]]
    else:
        #random crop
H
hypox64 已提交
88
        h_move = int(10*random.random()) #do not loss low freq signal infos
H
hypox64 已提交
89 90 91 92 93 94
        w_move = int((w-finesize[1])*random.random())
        result = img[h_move:h_move+finesize[0],w_move:w_move+finesize[1]]
        #random flip
        if random.random()<0.5:
            result = result[:,::-1]
        #random amp
H
hypox64 已提交
95
        result = result*random.uniform(0.9,1.1)+random.uniform(-0.05,0.05)
H
hypox64 已提交
96 97
    return result

H
hypox64 已提交
98
def ToInputShape(data,opt,test_flag = False):
H
hypox64 已提交
99
    #data = data.astype(np.float32)
H
hypox64 已提交
100

H
hypox64 已提交
101 102 103 104 105
    if opt.model_type == '1d':
        if opt.normliaze != 'None':
            for i in range(opt.batchsize):
                for j in range(opt.input_nc):
                    data[i][j] = arr.normliaze(data[i][j],mode = opt.normliaze)
H
hypox64 已提交
106 107
        result = random_transform_1d(data, opt.finesize, test_flag=test_flag)

H
hypox64 已提交
108 109 110 111 112 113 114 115 116 117 118 119
    elif opt.model_type == '2d':
        result = []
        for i in range(opt.batchsize):
            for j in range(opt.input_nc):
                spectrum = dsp.signal2spectrum(data[i][j],opt.stft_size,opt.stft_stride, not opt.stft_no_log)
                #spectrum = arr.normliaze(spectrum, mode = opt.normliaze)
                spectrum = (spectrum-2)/5
                # print(spectrum.shape)
                #spectrum = random_transform_2d(spectrum,(224,122),test_flag=test_flag)
                result.append(spectrum)
        h,w = spectrum.shape
        result = (np.array(result)).reshape(opt.batchsize,opt.input_nc,h,w)
H
hypox64 已提交
120 121


H
hypox64 已提交
122
    return result