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 : ์ฑ๋์ ์ฒซ๋ฒ์งธ๋ก ํ ๊ฑด์ง ๋ง์ง๋ง์ผ๋ก ํ ๊ฑด์ง ์ง์ → ์์ง ์ ํํ ์๋ฏธ๋ ์ ๋ชจ๋ฅด๊ฒ ์
'๐ฉโ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ART] attack_defence_imagenet.ipynb ์ฝ๋ ์ค์ต (0) | 2022.01.18 |
---|---|
[ART] adversarial_training_mnist.ipynb ์ฝ๋ ๋ถ์ (0) | 2022.01.12 |
[ART] ART for TensorFlow v2 - Keras API ์ฝ๋ ๋ถ์ (0) | 2021.12.31 |
[DACON] ์ฝ๋ ๋ถ์ - MNIST : ์ซ์ ์ด๋ฏธ์ง ๋ถ๋ฅ (0) | 2021.05.25 |
[kaggle] Intermediate Machine Learning (0) | 2021.04.28 |