Beispiel #1
0
    # 输入数据为 mnist 数据集
    x_test = x_test.astype('float32').reshape(-1, 28, 28, 1)
    x_test = x_test / 255

    y_test = keras.utils.to_categorical(y_test, 10)
    score = model.evaluate(x_test, y_test)
    return score[1]


if __name__ == '__main__':
    model_path = '../ModelA_raw.hdf5'
    model = load_model(model_path)
    score = accuracy_mnist(model, mnist)
    print('Origin Test accuracy: %.4f' % score)
    acclst = []
    bound_data_lst = get_bound_data_mnist(model, 10)
    #print len(bound_data_lst)
    index = np.random.choice(120, 30)
    for i in range(30):
        model_change = weight_shuffling(model,
                                        Layer='dense_1',
                                        neuron_index=index[i])
        model_change.compile(loss='categorical_crossentropy',
                             optimizer='sgd',
                             metrics=['accuracy'])
        #print 'Mutated Test accuracy: ',accuracy_cifar(model_change)
        #acc= accuracy_in_bound_data_mnist(model_change,bound_data_lst)
        #acclst.append(acc)
        model_change.save('mutated/MODEL_A/ModelA_WShuff' + str(i) + '.hdf5')
    #print 'Mutated accuracy in bound data(dense1):',[round(i,4) for i in acclst]
    acclst = []
Beispiel #2
0
    return : acc of mnist
    '''
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    # 输入数据为 mnist 数据集
    x_test = x_test.astype('float32').reshape(-1,28,28,1)
    x_test = x_test / 255

    y_test = keras.utils.to_categorical(y_test, 10)
    score = model.evaluate(x_test, y_test)
    return score[1]


if __name__=='__main__':
    model_path='../ModelB_raw.hdf5'
    model=load_model(model_path)    
    bound_data_lst = boundary.get_bound_data_mnist(model,10)#第一是第二的10倍以内,算边界值
    unbound_data_lst = boundary.get_unbound_data_mnist(model,10)
    
    path_lst=glob.glob('./mutated/MODEL_B/*.hdf5')
    bound_acc_list=[]#变异模型在边界值集合上的准确率
    unbound_acc_list=[]#变异模型在非边界值集合上的准确率
    acc_list=[]
    print('swj')
    for index,path in enumerate(path_lst):
        print index
        mutant_model=load_model(path)
        acc = accuracy_mnist(mutant_model,mnist)
        acc_list.append(acc)
        bound_acc =boundary.accuracy_in_bound_data_mnist(mutant_model,bound_data_lst)
        unbound_acc =boundary.accuracy_in_unbound_data_mnist(mutant_model,unbound_data_lst)
        bound_acc_list.append(bound_acc)
Beispiel #3
0
def train(filename='beta_mnist_shift_right3',
          bound_ratio=2,
          ramdon=True,
          samplesize=100):
    #(x_train, y_train), (__, __) = mnist.load_data()
    npzfile = np.load('../mnist.npz')
    x_train = npzfile['x_train']
    y_train = npzfile['y_train']
    #(x_train, y_train), (x_test, y_test) = mnist.load_data()

    npzfile = np.load('../backup/' + filename + '.npz')
    y_test = npzfile['y_test']
    x_test = npzfile['x_test']

    model_path = '../ModelA_raw.hdf5'
    model = load_model(model_path)
    len_bound = 0
    #bound_ratio=2
    if ramdon == False:
        x_bound, y_bound = boundary.get_bound_data_mnist(
            model, x_test, y_test, bound_ratio)
        #x_train = x_bound
        #y_train = y_bound
        x_train = np.concatenate((x_bound, x_train), axis=0)
        y_train = np.concatenate((y_bound, y_train), axis=0)
        len_bound = len(x_bound)
    else:
        x_random, y_random = random_select(samplesize, x_test, y_test)
        #x_train = x_random
        #y_train = y_random
        x_train = np.concatenate((x_random, x_train), axis=0)
        y_train = np.concatenate((y_random, y_train), axis=0)

    #x_train = x_bound
    #y_train = y_bound

    x_train = x_train.astype('float32').reshape(-1, 28, 28, 1)
    x_test = x_test.astype('float32').reshape(-1, 28, 28, 1)

    x_train = x_train / 255
    x_test = x_test / 255

    y_test = keras.utils.to_categorical(y_test, 10)
    y_train = keras.utils.to_categorical(y_train, 10)

    score = model.evaluate(x_test, y_test)
    #print('Test Loss: %.4f' % score[0])
    print('Test accuracy: %.4f' % score[1])
    origin_acc = score[1]
    '''
    npzfile=np.load('./backup/beta_mnist_shift_up2.npz') 
    y_beta= npzfile['y_test']
    x_beta= npzfile['x_test'] 
    '''

    sgd = optimizers.SGD(lr=0.01)
    model.compile(loss='categorical_crossentropy',
                  optimizer=sgd,
                  metrics=['accuracy'])

    model.fit(x_train,
              y_train,
              batch_size=100,
              epochs=5,
              verbose=1,
              validation_data=(x_test, y_test))
    score = model.evaluate(x_test, y_test)
    print('Test accuracy: %.4f' % score[1])

    return score[1], len_bound, origin_acc