def run(self): #训练模型 # 1. 模型训练 self.option.signalInfo.emit(-1,"开始导入数据...") dataloader = load_data(self.option) model = Ganomaly(self.option, dataloader) self.option.signalInfo.emit(10, "导入数据完毕!") self.modelINFO = model.train() # 2. 对训练结果进行处理,生成一个字典 self.option.signalInfo.emit(100,"") self.modelINFO['opt'] = vars(self.option) self.modelINFO['opt'].pop('signalInfo') signal = self.modelINFO['opt'].pop('signal') self.modelINFO['modelName'] = self.modelINFO['opt'].pop('dataset') self.modelINFO['raw_path'] = self.modelINFO['opt'].pop('dataroot') self.modelINFO['desc'] = self.modelINFO['opt'].pop('desc') # 3. 将训练结果字典保存到json文件中 ## 默认保存路径./output/modelsData/models.json # filename = './output/modelsData/models.json' # data = {} # with open(filename,'r',encoding='utf-8') as f: # try: # data = json.load(f) # except json.decoder.JSONDecodeError: # 此处源文件没有数据,即尚未有模型被训练 # data = {} # with open(filename, 'w', encoding="utf-8") as f: # data[self.modelINFO['modelName']] = self.modelINFO # json.dump(data,f,sort_keys=True,indent=2) # 3. 将训练结果字典通过信号传递给主函数 signal.emit(copy.deepcopy(self.modelINFO))
def FinalTest(): dataset = 'cus_mnist' # dataroot = './data/cus_mnist' opt = Options().parse(dataset) opt.isTrain = False opt.load_weights = True ## # LOAD DATA dataloader = load_data(opt) print(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## # TRAIN MODEL # model.train() minVal = None maxVal = None threshold = None with open(opt.dataroot + '/performance.txt', 'r+', encoding='utf-8') as f: res = f.readline() res = res.split('&') res = [float(i) for i in res] minVal = res[0] maxVal = res[1] threshold = res[2] model.FinalTest(minVal, maxVal, threshold)
def run(self): # self.option.isize = 128 self.dataloader = load_data(self.option) model = Ganomaly(self.option, self.dataloader) minVal = self.modelData[0] maxVal = self.modelData[1] threshold = self.modelData[2] resNor, resAbn = model.FinalTest(minVal, maxVal, threshold) opt = {} opt['path'] = self.option.dataroot opt['modelName'] = self.option.dataset self.option.signal.emit(copy.deepcopy(resNor), copy.deepcopy(resAbn), copy.deepcopy(opt))
def train(): """ Training """ dataset = 'cus_mnist' dataroot = 'E:/ProjectSet/Pycharm/WAIBAO/Code01/GAN/data/cus_mnist' opt = Options().parse(dataset) opt.load_weights = False dataloader = load_data(opt) print(opt) # LOAD MODEL model = Ganomaly(opt, dataloader) model.train()
def test(): ## # ARGUMENTS opt = Options().parse() ## # LOAD DATA dataloader = load_data(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## # test model performance = model.test() print(performance)
def train(): """ Training """ ## # ARGUMENTS opt = Options().parse() ## # LOAD DATA dataloader = load_data(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## # TRAIN MODEL model.train()
def test(): """ Training """ ## # ARGUMENTS opt = Options().parse() ## # LOAD DATA dataloader = load_data(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## # MODEL TEST res = model.test() print('AUC:%f\n' % res['AUC'])
def train(self): """ Training """ self.textEdit.append('开始训练...') dataset = 'cus_mnist_2' # dataroot = './data/cus_mnist' dataroot = 'E:\ProjectSet\Pycharm\WAIBAO\cus_mnist2' opt = Options().parse(dataset,dataroot) opt.signal = self.Signal_TrainFinished opt.load_weights = False dataloader = load_data(opt) print(opt) # LOAD MODEL opt.showProcess = self.progressBar opt.showText = self.textEdit model = Ganomaly(opt, dataloader) model.train()
def test(): """ Testing """ dataset = 'cus_mnist' #dataroot = './data/cus_mnist' opt = Options().parse(dataset) opt.isTrain = False opt.load_weights = True ## # LOAD DATA dataloader = load_data(opt) print(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) print(model.test())
def train(): """ Training """ ## # ARGUMENTS opt = Options().parse() ## # LOAD DATA dataloader = load_data(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## # MODEL TEST res = model.test() model.z_train()
def train(): """ Training """ ## # ARGUMENTS opt = Options().parse() ## # LOAD DATA dataloader = load_data(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## if opt.phase == 'train': # TRAIN MODEL model.train() elif opt.phase =='test': performance=model.test() print(performance)
def run(self): opt = self.opt # model.testOne() model = Ganomaly(self.opt, self.dataloader) ## # TRAIN MODEL # model.train() minVal = None maxVal = None threshold = None with open(opt.dataroot + '/performance.txt', 'r+', encoding='utf-8') as f: res = f.readline() res = res.split('&') res = [float(i) for i in res] minVal = res[0] maxVal = res[1] threshold = res[2] model.FinalTest(minVal, maxVal, threshold)
def test(self): self.textEdit.append('开始测试,得出阈值...') """ Testing """ dataset = 'cus_mnist' # dataroot = './data/cus_mnist' opt = Options().parse(dataset) opt.isTrain = False opt.load_weights = True ## # LOAD DATA dataloader = load_data(opt) print(opt) ## # LOAD MODEL opt.showProcess = self.progressBar opt.showText = self.textEdit model = Ganomaly(opt, dataloader) print(model.test())
from lib.data_preprocess_KDD import load_data_kdd from lib.data_preprocess_arr import load_data_arr from lib.data_preprocess_ucr import load_data_ucr ## # def main(): """ Training """ ## # ARGUMENTS opt = Options().parse() print(opt.anomaly_class) ## # LOAD DATA # dataloader = load_data(opt) # dataloader = load_data_kdd(opt) # dataloader = load_data_arr(opt) dataloader = load_data_ucr(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## # TRAIN MODEL model.train() # if __name__ == '__main__': # main()
def detect(self, image): data_loader = self.__preprocess(image) fake_blocks = self.model(data_loader) fake_blocks = self.__normalization(fake_blocks) rec_fake = self.__reconstruct_image(fake_blocks, int(1920 / 128), int(1080 / 128)) return rec_fake if __name__ == '__main__': path = "./output/ganomaly/NanjingRail_blocks/train/weights/netG.pth" path_alpha = './models/cifar10_dim_128_lambda_40_zlambd_0_epochs_100.torch' opt = Options().parse() gan_network = Ganomaly(opt) model_ganomaly = AnomalyModel(gan_network, path) from anomaly import model as alpha_model model_alpha = AnomalyModelAlpha(alpha_model, path_alpha) detector_ganomaly = AnomalyDetector(model_ganomaly) detector_alpha = AnomalyDetector(model_alpha) for index in range(22, 23): img = Image.open('./data/test{}.jpg'.format(index)) rec_a = detector_alpha.detect(img) rec_g = detector_ganomaly.detect(img) img_fake_a = Image.fromarray(rec_a) img_fake_a.save('./data/fake{}_a.png'.format(index)) img_fake_g = Image.fromarray(rec_g)
def train(): """ Training """ ## # ARGUMENTS opt = Options().parse() ## # LOAD DATA dataloader = load_data(opt) ## # LOAD MODEL model = Ganomaly(opt, dataloader) ## # TRAIN MODEL model.train() train_1 = model.train_final() train_1 = train_1.cpu().numpy() test_1, y_true, y_true_original, auroc_value, auprc_value = model.test_final( ) test_1 = test_1.cpu().numpy() y_true = y_true.cpu().numpy() y_true_original = y_true_original.cpu().numpy() test_path = os.path.join(opt.outf, opt.dataset, 'test', 'OCSVM', 'abnormal' + str(opt.abnormal_class), 'seed' + str(opt.manualseed)) if not os.path.isdir(test_path): os.makedirs(test_path) print("GANomaly AUROC: {}".format(auroc_value)) np.save(test_path + '/ganomaly_aucroc.npy', auroc_value) for i in range(len(y_true)): if y_true[i] == 1: y_true[i] = 0 else: y_true[i] = 1 ################################ cf = svm.OneClassSVM(gamma='scale', nu=0.1) train_ind = np.random.choice(train_1.shape[0], 10000, replace=False) cf.fit(train_1[train_ind, :]) y_scores = cf.score_samples(test_1) y_scores = (y_scores - min(y_scores)) / (max(y_scores) - min(y_scores)) auroc = metrics.roc_auc_score(y_true, y_scores) print("HybridGAN AUROC: {}".format(auroc)) np.save(test_path + '/svm_aucroc1.npy', auroc) np.save(test_path + '/svm_aucroc1_transduct_' + str(0) + '.npy', auroc) bandwidth = get_bandwidth(y_scores, test_1) for trans_iter in np.arange(0, 30, 1): optimal_threshold = find_optimal_threshold(y_scores=y_scores, train_1=train_1, test_1=test_1, y_true=y_true, train_ind=train_ind, test_path=test_path, bandwidth=bandwidth) abn_idx = np.where( y_scores < np.percentile(y_scores, optimal_threshold)) abn_tst_latent = test_1[abn_idx] kmeans = KMeans(n_clusters=1, random_state=0).fit(abn_tst_latent) train_1 = np.concatenate((train_1, kmeans.transform(train_1)), axis=1) test_1 = np.concatenate((test_1, kmeans.transform(test_1)), axis=1) cf = svm.OneClassSVM(gamma='scale', nu=0.1) cf.fit(train_1[train_ind, :]) y_scores = cf.score_samples(test_1) y_scores = (y_scores - min(y_scores)) / (max(y_scores) - min(y_scores)) auroc = metrics.roc_auc_score(y_true, y_scores) print("TransdeepOCSVM AUROC after {} iterations: {}".format( trans_iter + 1, auroc)) print("Optimal_threshold after {} iterations: {}".format( trans_iter + 1, optimal_threshold[0])) np.save( test_path + '/svm_aucroc1_transduct_' + str(trans_iter + 1) + '.npy', auroc) np.save( test_path + '/optimal_threshold_' + str(trans_iter + 1) + '.npy', optimal_threshold)
import os import numpy as np import pandas as pd import torch from pathlib import Path os.chdir('E:\ganomly') from options import Options from lib.data import load_data from lib.model import Ganomaly from lib.evaluate import roc ## # def main(): opt = Options().parse() dataloader = load_data(opt) model = Ganomaly(opt, dataloader) torch.cuda.empty_cache() model.train() # Test out the model: per, an, gt, dat = model.test() def assessment(an, gt, dat): groundtruth = an.cpu().data.numpy() anomaly = gt.cpu().data.numpy() id_u = [] for i in np.array(dat): id_u.append(i) id_s = []