if opt.Cross_Validation == 'k_fold': signals, stages = transformer.batch_generator(signals, stages, opt.batchsize, shuffle=True) train_sequences, test_sequences = transformer.k_fold_generator( len(stages), opt.fold_num) batch_length = len(stages) print('length of batch:', batch_length) show_freq = int(len(train_sequences[0]) / 5) util.show_menory() t2 = time.time() print('load data cost time: %.2f' % (t2 - t1), 's') net = CreatNet(opt.model_name) torch.save(net.cpu().state_dict(), './checkpoints/' + opt.model_name + '.pth') weight = np.array([1, 1, 1, 1, 1]) if opt.weight_mod == 'avg_best': weight = np.log(1 / stage_cnt_per) weight[2] = weight[2] + 1 weight = np.clip(weight, 1, 5) print('Loss_weight:', weight) weight = torch.from_numpy(weight).float() # print(net) if not opt.no_cuda: net.cuda() weight = weight.cuda() if not opt.no_cudnn: cudnn.benchmark = True
import numpy as np import torch import matplotlib.pyplot as plt import util import transformer import dataloader from options import Options from creatnet import CreatNet ''' @hypox64 19/04/03 ''' opt = Options().getparse() net=CreatNet(opt.model_name) if not opt.no_cuda: net.cuda() if not opt.no_cudnn: import torch.backends.cudnn as cudnn cudnn.benchmark = True if opt.pretrained: net.load_state_dict(torch.load('./checkpoints/pretrained/'+opt.model_name+'.pth')) # N3(S4+S3)->0 N2->1 N1->2 REM->3 W->4 stage_map={0:'stage3',1:'stage2',2:'stage3',3:'REM',4:'Wake'} def runmodel(eegdata): eegdata = eegdata.reshape(1,-1) eegdata = transformer.ToInputShape(eegdata,opt.model_name,test_flag =True)
# 2.shape signals:[num,ch,length] labels:[num] # num:samples_num, ch :channel_num, num:length of each sample # for example: signals = np.zeros((10,1,10),dtype='np.float64') labels = np.array([0,0,0,0,0,1,1,1,1,1]) #0->class0 1->class1 * step2: input ```--dataset_dir your_dataset_dir``` when running code. ''' signals, labels = dataloader.loaddataset(opt) label_cnt, label_cnt_per, _ = statistics.label_statistics(labels) train_sequences, test_sequences = transformer.k_fold_generator( len(labels), opt.k_fold) t2 = time.time() print('load data cost time: %.2f' % (t2 - t1), 's') net = CreatNet(opt) util.writelog('network:\n' + str(net), opt, True) util.show_paramsnumber(net, opt) weight = np.ones(opt.label) if opt.weight_mod == 'auto': weight = 1 / label_cnt_per weight = weight / np.min(weight) util.writelog('label statistics: ' + str(label_cnt), opt, True) util.writelog('Loss_weight:' + str(weight), opt, True) weight = torch.from_numpy(weight).float() # print(net) if not opt.no_cuda: net.cuda() weight = weight.cuda()
import torch import matplotlib.pyplot as plt import util import transformer import dataloader from options import Options from creatnet import CreatNet ''' --------------------------------preload data-------------------------------- @hypox64 2020/04/03 ''' opt = Options().getparse() net = CreatNet(opt) #load data signals = np.load('./datasets/simple_test/signals.npy') labels = np.load('./datasets/simple_test/labels.npy') #load prtrained_model net.load_state_dict(torch.load('./checkpoints/pretrained/micro_multi_scale_resnet_1d_50class.pth')) net.eval() if not opt.no_cuda: net.cuda() for signal,true_label in zip(signals, labels): signal = signal.reshape(1,1,-1) #batchsize,ch,length true_label = true_label.reshape(1) #batchsize signal,true_label = transformer.ToTensor(signal,true_label,no_cuda =opt.no_cuda)