๐Ÿ‘ฉ‍๐Ÿ’ป

[ART] ART for TensorFlow v2 - Callable ์ฝ”๋“œ ๋ถ„์„

geum 2022. 1. 3. 13:21

 

โœ… ์ฝ”๋“œ :  https://github.com/Trusted-AI/adversarial-robustness-toolbox/blob/main/notebooks/art-for-tensorflow-v2-callable.ipynb

 

GitHub - Trusted-AI/adversarial-robustness-toolbox: Adversarial Robustness Toolbox (ART) - Python Library for Machine Learning S

Adversarial Robustness Toolbox (ART) - Python Library for Machine Learning Security - Evasion, Poisoning, Extraction, Inference - Red and Blue Teams - GitHub - Trusted-AI/adversarial-robustness-too...

github.com

 

์ปค์Šคํ…€ ๋ชจ๋ธ ํด๋ž˜์Šค ์ƒ์„ฑ

class KerasModel(Model):
    # super(KerasModel: super๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ž๊ธฐ ์ž์‹ , self: super๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฐธ์กฐ ๊ฐ์ฒด)
    def __init__(self):
        super(KerasModel, self).__init__()
        self.conv1 = Conv2D(filters=3, kernel_size=3, activation='relu')
        self.flatten = Flatten()
        self.dense1 = Dense(10, activation='softmax')

    def call(self, x):
        x = self.conv1(x)
        x = self.flatten(x)
        x = self.dense1(x)
        return x

 

์ž…๋ ฅ ํŒŒ์ดํ”„๋ผ์ธ ์ƒ์„ฑ

ํ•™์Šต๊ณผ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์ž…๋ ฅ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ƒ์„ฑํ•œ๋‹ค.

 

train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)

 

1. tf.data.Dataset.from_tensor_slices

1) from_tensor_slices

์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ Tensor๋กœ ์ž๋ฅธ๋‹ค. train_ds, test_ds์—์„œ shuffle, batch ๋ถ€๋ถ„์„ ๋นผ๊ณ  print๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด TensorSliceDataset shapes๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

 

2) shuffle / batch

โ—ฝ shuffle : ๋ฐ์ดํ„ฐ์…‹์˜ ์š”์†Œ๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ์„ž๋Š”๋‹ค. ์ฒซ ์ธ์ž๊ฐ€ ์„ž๋Š” ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ค„ ์•Œ์•˜๋Š”๋ฐ..! buffer_size์˜€๋‹ค. 

โ—ฝ batch : ๋ฐ์ดํ„ฐ์…‹์„ ์ง€์ •ํ•œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆˆ๋‹ค.

 

ํ•™์Šต/ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„ ์ •์˜

@tf.function
def train_step(images, labels):
    with tf.GradientTape() as tape:
        predictions = model(images)
        loss = loss_object(labels, predictions)
    # ์ž…๋ ฅ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์†์‹ค ํ•จ์ˆ˜์˜ ๊ธฐ์šธ๊ธฐ ๊ณ„์‚ฐ
    gradients = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))

    train_loss(loss)
    train_accuracy(labels, predictions)
    
@tf.function
def test_step(images, labels):
    predictions = model(images)
    t_loss = loss_object(labels, predictions)

    test_loss(t_loss)
    test_accuracy(labels, predictions)

 

1. @tf.function

tf.function์„ ์ง์ ‘ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•˜์—ฌ TensorFlow์—์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค๊ณ  ์‹คํ–‰ํ•œ๋‹ค. ์ผ๋ฐ˜ ํ•จ์ˆ˜๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ  Function์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, Function์€ Python ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ TensorFlow ๊ทธ๋ž˜ํ”„๋ฅผ ๋นŒ๋“œํ•˜๋Š” Python callable์ด๋‹ค.

 

๐Ÿ” ์ •๋ณด ์ถœ์ฒ˜ : https://tensorflow.google.cn/guide/intro_to_graphs?hl=ko

๐Ÿง ์ฐธ๊ณ  ์ž๋ฃŒ : https://www.tensorflow.org/api_docs/python/tf/function

 

2. with tf.GradientTape() as tape

tf.GradientTape๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์˜ ๊ทธ๋ž˜๋””์–ธํŠธ๋ฅผ ์ž๋™์œผ๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด TensorFlow์—์„œ ์ œ๊ณตํ•˜๋Š” API์ด๋‹ค. ์ปจํ…์ŠคํŠธ ์•ˆ์—์„œ ์‹คํ–‰๋œ ๋ชจ๋“  ์—ฐ์‚ฐ์„ ํ…Œ์ดํ”„์— ๊ธฐ๋กํ•˜๊ณ  reverse mode differentiation์„ ์‚ฌ์šฉํ•ด ํ…Œ์ดํ”„์— ๊ธฐ๋ก๋œ ์—ฐ์‚ฐ์˜ ๊ทธ๋ž˜๋””์–ธํŠธ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด ๊ณผ์ •์€ ๋ชจ๋‘ TensorFlow๊ฐ€ ์•Œ์•„์„œ ํ•ด์ค€๋‹ค.

 

3. loss_object(labels, predictions)

**๋” ์ฐพ์•„๋ด์•ผ ํ•จ**

 

4. tape.gradient(loss, model.trainable_variables)

model.trainable_variables์— ๋Œ€ํ•œ loss์˜ ๋ฏธ๋ถ„๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค.

 

5. optimizer.apply_gradients(~)

**๋” ์ฐพ์•„๋ด์•ผ ํ•จ**

 

์ปค์Šคํ…€ ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ์œ„ํ•œ TensorFlow v2 classifier ์ƒ์„ฑ

# TensorFlowV2Classifier: art/estimators/classification/tensorflow.py
classifier = TensorFlowV2Classifier(model=model, nb_classes=10, input_shape=(28, 28, 1), loss_object=loss_object, 
                                    clip_values=(0, 1), channels_first=False)

 

ํŒŒ๋ผ๋ฏธํ„ฐ ์˜๋ฏธ

โ—ฝ model : ๋ชจ๋ธ์ด ์ •์˜๋œ ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ๋˜๋Š” callable ํด๋ž˜์Šค. ๋ชจ๋ธ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ output์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

โ—ฝ nb_classes : ๋ถ„๋ฅ˜ ํด๋ž˜์Šค์˜ ์ˆ˜(MNIST ๋ฐ์ดํ„ฐ๋‹ˆ๊นŒ 0~9 ์ด 10๊ฐ€์ง€ ํด๋ž˜์Šค)

โ—ฝ loss_objects : tf.keras.losses

โ—ฝ channels_first : ์ฑ„๋„์„ ์ฒซ๋ฒˆ์งธ๋กœ ํ•  ๊ฑด์ง€ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•  ๊ฑด์ง€ ์ง€์ • → ์•„์ง ์ •ํ™•ํ•œ ์˜๋ฏธ๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ์Œ