IBM / CNN-Cert

Codes for reproducing the experimental results in "CNN-Cert: An Efficient Framework for Certifying Robustness of Convolutional Neural Networks", published at AAAI 2019
Apache License 2.0
27 stars 16 forks source link

GPUs #7

Open miaoxiaodaiblack opened 3 years ago

miaoxiaodaiblack commented 3 years ago

Thank you for your reply!

And I have another question that how many GPUs you used in that experiment?

And if I want to calculate the bound of another model with video data, how many GPUs it will use?

Thank you!

AkhilanB commented 3 years ago

Thanks for your question!

The code to find certified bounds does not require GPUs and actually currently does not support GPUs. The experiments were all done using CPUs. It may be possible to modify the code to use GPUs using, for example, Numba's GPU support.

In principle it is possible to use the current code with CPUs to calculate bounds on a model with video data. However, if it is too slow, adapting the code to use GPUs may make it faster.

miaoxiaodaiblack commented 3 years ago

Thank you for your reply and I used two 2080ti, but it's full of video memory. And I want to know how much video memory may be used. Thank you!

AkhilanB commented 3 years ago

I believe any video memory restrictions would come from the specific hardware, models and data you are using and not from the certification code. I expect that the certification code can be adapted to be made to work with any hardware. If there are memory constraints on your GPUs, I suggest trying a smaller model or certifying on CPUs instead.

miaoxiaodaiblack commented 3 years ago

Thank you for your reply!

And the code I tested was the 'cnn_to_mlp'. When I try to do this code, it will be full of video memory.

miaoxiaodaiblack commented 3 years ago

And the model and data are the same as you use. I do not change them.

I'm so confused. Thank you very much!

AkhilanB commented 3 years ago

I suggest modifying the code of the 'convert' function in 'cnn_to_mlp.py' to avoid evaluating the network:

#Main function to convert CNN to MLP model
def convert(file_name, new_name, cifar = False):
    if not cifar:
        eq_weights, new_params = get_weights(file_name)
        data = MNIST()
    else:
        eq_weights, new_params = get_weights(file_name, inp_shape = (32,32,3))
        data = CIFAR()
    model = Sequential()
    model.add(Flatten(input_shape=data.train_data.shape[1:]))
    for param in new_params:
        model.add(Dense(param))
        model.add(Lambda(lambda x: tf.nn.relu(x)))
    model.add(Dense(10))

    for i in range(len(eq_weights)):
        try:
            print(eq_weights[i][0].shape)
        except:
            pass
        model.layers[i].set_weights(eq_weights[i])

    sgd = SGD(lr=0.01, decay=1e-5, momentum=0.9, nesterov=True)

    model.compile(loss=fn,
                  optimizer=sgd,
                  metrics=['accuracy'])

    model.save(new_name)
    '''
    acc = model.evaluate(data.validation_data, data.validation_labels)[1]
    printlog("Converting CNN to MLP")
    nlayer = file_name.split('_')[-3][0]
    filters = file_name.split('_')[-2]
    kernel_size = file_name.split('_')[-1]
    printlog("model name = {0}, numlayer = {1}, filters = {2}, kernel size = {3}".format(file_name,nlayer,filters,kernel_size))
    printlog("Model accuracy: {:.3f}".format(acc))
    printlog("-----------------------------------")
    return acc
   '''

If this does not work, I recommend forcing the code to avoid the GPU by, for example, adding the following code to the top of the 'cnn_to_mlp.py' file:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

The reason for the high memory usage may be that when cnn models are converted to mlp, the memory of the models is much larger. However, this conversion is only necessary when certifying using the baseline certification methods- CNN-cert can certify the models without converting them to mlp first.

AkhilanB commented 3 years ago

Thank you for letting us know! The correct url is "https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz" which is found in 'setup_cifar.py'.

Could you let us know where you encountered the other url? I can then update the url to the correct one.

miaoxiaodaiblack commented 3 years ago

Thank you very much! Your work is so surprising!

miaoxiaodaiblack commented 3 years ago

The other 'URL' is also in the 'setup_cifar.py'. When I run the 'cnn_to_mlp.py', it will turn to run the 'setup_cifar.py' and download the two datasets 'cifar-10-binary.tar.gz' and 'cifar-data.tar.gz'. And I have found the right link, that is 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'

miaoxiaodaiblack commented 3 years ago

Thank you for your reply!

miaoxiaodaiblack commented 3 years ago

Hi! Thank you! Does CNN-Cert work on C3D?

AkhilanB commented 3 years ago

CNN-Cert does not currently support 3d convolutions (only 2d convolutions are supported). I believe it would be possible to extend the code to support 3d convolutions, although this would require significant modification of the functions in "cnn_bounds_full_core.py" and "cnn_bounds_full.py." Currently the 'A' and 'B' arrays storing bound coefficients have 6 and 3 axes respectively- they would need to be modified to contain 8 and 4 axes to support 3 spatial dimensions.

miaoxiaodaiblack commented 3 years ago

Thank you! Do the equations of computing A and B should be modified?

AkhilanB commented 3 years ago

Yes, the equations for computing A and B would need to be modified. However, this can be done by treating the additional spatial dimension in the same way as the other two spatial dimensions. For example, the line in https://github.com/IBM/CNN-Cert/blob/master/cnn_bounds_full_core.py#L227 could be replaced by something like:

y[a,b,c,d] += A[a,b,c,d,i,j,k,l]*x[s_h*a+i-p_hl,s_w*b+j-p_wl,s_d*c+k,-p_dl,l]

and the for loops and if condition also modified correspondingly. Currently the 1st, 2nd, 4th and 5th dimensions of A correspond to spatial dimensions, and under the above modification the 1st, 2nd, 3rd, 5th, 6th and 7th dimensions of A would be spatial. Similarly, B currently has 1st and 2nd dimensions as spatial dimensions; it could be modified to have the first 3 dimensions be spatial.