# Expand dimensions to match expected EEGNet input X = (np.expand_dims(X, axis=-1)) # number of temporal sample per trial n_samples = np.shape(X)[2] # convert labels to one-hot encodings. y_cat = np_utils.to_categorical(y) # using 5 folds kf = KFold(n_splits = num_splits) split_ctr = 0 for train, test in kf.split(X, y): # init model model = models.EEGNet(nb_classes = num_classes, Chans=n_ch, Samples=n_samples, regRate=0.25, dropoutRate=0.2, kernLength=kernLength, poolLength=poolLength, numFilters=8, dropoutType='Dropout') #print(model.summary()) # Set Learning Rate adam_alpha = Adam(lr=(0.0001)) model.compile(loss='categorical_crossentropy', optimizer=adam_alpha, metrics = ['accuracy']) np.random.seed(42*(split_ctr+1)) np.random.shuffle(train) # do training history = model.fit(X[train], y_cat[train], validation_data=(X[test], y_cat[test]), batch_size = 16, epochs = n_epochs, callbacks=[lrate], verbose = 2) acc[split_ctr] = save_results(history,num_classes,n_ds,n_ch,T,split_ctr)
print(x_train.shape) nb_classes = len(np.unique( y_train)) # np.unique是去除重复数字然后排序输出,这样输出它的长度表明总共的分类数-可尝试增加分类数看结果的变化 samples = x_train.shape[3] channels = x_train.shape[2] racc = [] rbca = [] rasr = [] pruning_idx = [30, 60, 180] #CNN剪枝的参数 for i in range(repeat): # Build Model if model_used == 'EEGNet': model = models.EEGNet(nb_classes=nb_classes, Chans=channels, Samples=samples) elif model_used == 'DeepCNN': model = models.DeepConvNet(nb_classes=nb_classes, Chans=channels, Samples=samples) else: raise Exception('No such model:{}'.format(model_used)) #######读取之前存储的模型 model = load_model(filepath='model_poison_MI_{}_{}_{}.h5'.format( npp_params[0], npp_params[1], npp_params[2])) #model = load_model(filepath='model_poison_ERN_pruning6_{}_{}_{}.h5'.format(npp_params[0], npp_params[1], npp_params[2])) #model = load_model(filepath='model_orginal_retrain{}_{}_{}.h5'.format(npp_params[0], npp_params[1], npp_params[2])) #model = load_model(filepath='model_CNNERN_pruning_{}_{}_{}_{}.h5'.format(npp_params[0], pruning_idx[0], pruning_idx[1],pruning_idx[2])) #model = load_model(filepath='model_orginal_poison_CNNP300pruning-{}_{}_{}.h5'.format(npp_params[0], npp_params[1], npp_params[2]))
def Net(n_class, n_ch, n_time): embedding_net = models.EEGNet(n_class, n_ch, n_time) model = models.FcClfNet(embedding_net) return model