Ver3.0 ๐ ์์ฉํ๋ก๊ทธ๋จ์์ AI ๋ชจ๋ธ ์ฐ๋ํ๊ธฐ

๐ ์์ฉํ๋ก๊ทธ๋จ์์ AI ๋ชจ๋ธ ์ฐ๋ํ๊ธฐ
TensorFlow & PyTorch๋ก ๋๋ํ ์ฑ ๋ง๋ค๊ธฐ
์๋
! ๐ ์์ฆ AI๊ฐ ๋์ธ์์? ์ฑ๋ด, ์ด๋ฏธ์ง ์ธ์, ์ถ์ฒ ์์คํ
๊น์ง... ์ด๋ฐ ๋ฉ์ง ๊ธฐ๋ฅ๋ค์ ๋ด๊ฐ ๋ง๋ ์ฑ์๋ ๋ฃ๊ณ ์ถ๋ค๋ ์๊ฐ ํ ๋ฒ์ฏค ํด๋ดค์ ๊ฑฐ์ผ. ๊ทธ๋ฐ๋ฐ ๋ง์ ์์ํ๋ ค๋๊น "์ด๋ป๊ฒ ์ฐ๋ํ์ง?" ํ๋ ๊ณ ๋ฏผ์ด ์๊ธฐ์ง?
์ค๋์ ๋ฐ๋ก ๊ทธ ๊ณ ๋ฏผ์ ํด๊ฒฐํด์ค๊ฒ! TensorFlow์ PyTorch๋ผ๋ ๋ ๊ฐ์ง ๊ฐ๋ ฅํ AI ํ๋ ์์ํฌ๋ฅผ ํ์ฉํด์ ์ค์ ์์ฉํ๋ก๊ทธ๋จ์ AI ๋ชจ๋ธ์ ์ฐ๋ํ๋ ๋ฐฉ๋ฒ์ ์น๊ตฌ์ฒ๋ผ ํธํ๊ฒ ์๋ ค์ค ๊ฑฐ์ผ. ๐ฏ
โ TensorFlow์ PyTorch์ ๊ธฐ๋ณธ ๊ฐ๋ ๊ณผ ์ฐจ์ด์
โ AI ๋ชจ๋ธ์ ์ค์ ์ฑ์ ํตํฉํ๋ ๊ตฌ์ฒด์ ์ธ ๋ฐฉ๋ฒ
โ ๋ชจ๋ฐ์ผ, ์น, ์๋ฒ ํ๊ฒฝ๋ณ ์ฐ๋ ์ ๋ต
โ ์ค์ ์ฝ๋ ์์ ์ ์ต์ ํ ํ
โ ์ค๋ฌด์์ ๋ง์ฃผ์น๋ ๋ฌธ์ ๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ
๐ฏ ์ TensorFlow์ PyTorch์ธ๊ฐ?
AI ๋ชจ๋ธ์ ๋ง๋ค๊ณ ๋ฐฐํฌํ ์ ์๋ ํ๋ ์์ํฌ๋ ๋ง์. ํ์ง๋ง TensorFlow์ PyTorch๋ ํ์ฌ ์ ๊ณ ํ์ค์ด๋ผ๊ณ ํ ์ ์์ง. ๋ง์น ์ค๋งํธํฐ์์ ์๋๋ก์ด๋์ iOS์ฒ๋ผ ๋ง์ด์ผ!
๐ง TensorFlow - ๊ตฌ๊ธ์ ๊ฐ๋ ฅํ ๋ฌด๊ธฐ
TensorFlow๋ ๊ตฌ๊ธ์ด ๋ง๋ ์คํ์์ค ๋จธ์ ๋ฌ๋ ํ๋ ์์ํฌ์ผ. 2015๋ ์ ๊ณต๊ฐ๋๋๋ฐ, ์ง๊ธ์ ์ ์ธ๊ณ์์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ AI ํ๋ ์์ํฌ ์ค ํ๋์ง.
1. ํ๋ก๋์ ํ๊ฒฝ์ ์ต์ ํ ๐ญ
์ค์ ์๋น์ค์ ๋ฐฐํฌํ๊ธฐ ์ ๋ง ์ข์. TensorFlow Serving, TensorFlow Lite, TensorFlow.js ๊ฐ์ ๋ค์ํ ๋ฐฐํฌ ๋๊ตฌ๋ค์ด ์์ด์ ์๋ฒ, ๋ชจ๋ฐ์ผ, ์น ์ด๋๋ ์ฝ๊ฒ ์ ์ฉํ ์ ์์ง.
2. ๊ฐ๋ ฅํ ์ํ๊ณ ๐ณ
Keras๋ผ๋ ๊ณ ์์ค API๊ฐ ํตํฉ๋์ด ์์ด์ ์ด๋ณด์๋ ์ฝ๊ฒ ์์ํ ์ ์์ด. ๋ํ TensorBoard๋ผ๋ ์๊ฐํ ๋๊ตฌ๋ก ๋ชจ๋ธ ํ์ต ๊ณผ์ ์ ํ๋์ ๋ณผ ์ ์์ง.
3. ๋ชจ๋ฐ์ผ/์๋ฒ ๋๋ ์ง์ ๐ฑ
TensorFlow Lite๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋งํธํฐ์ด๋ IoT ๊ธฐ๊ธฐ์์๋ AI ๋ชจ๋ธ์ ๋๋ฆด ์ ์์ด. ์ฉ๋๋ ์๊ณ ์๋๋ ๋น ๋ฅด์ง!
๐ฅ PyTorch - ์ฐ๊ตฌ์๋ค์ ์ฌ๋
PyTorch๋ ํ์ด์ค๋ถ(ํ Meta)์ด ๊ฐ๋ฐํ ํ๋ ์์ํฌ์ผ. 2016๋ ์ ๋์๋๋ฐ, ํนํ ์ฐ๊ตฌ ๋ถ์ผ์์ ์์ฒญ๋ ์ธ๊ธฐ๋ฅผ ๋๊ณ ์์ด.
1. ์ง๊ด์ ์ธ ์ฝ๋ โจ
ํ์ด์ฌ์ค๋ฌ์ด ์ฝ๋ ์คํ์ผ์ด๋ผ ๋ฐฐ์ฐ๊ธฐ ์ฌ์. ๋๋ฒ๊น ๋ ์ผ๋ฐ ํ์ด์ฌ ์ฝ๋์ฒ๋ผ ํ ์ ์์ด์ ๊ฐ๋ฐ ์๋๊ฐ ๋น ๋ฅด์ง.
2. ๋์ ๊ณ์ฐ ๊ทธ๋ํ ๐
์คํ ์ค์ ๊ทธ๋ํ๋ฅผ ๋ณ๊ฒฝํ ์ ์์ด์ ์ ์ฐํ ๋ชจ๋ธ ์ค๊ณ๊ฐ ๊ฐ๋ฅํด. ํนํ ์์ฐ์ด ์ฒ๋ฆฌ๋ ๋ณต์กํ ๋ชจ๋ธ์์ ๊ฐ์ ์ ๋ณด์ฌ.
3. ํ๋ฐํ ์ฐ๊ตฌ ์ปค๋ฎค๋ํฐ ๐ฅ
์ต์ ๋ ผ๋ฌธ๋ค์ด PyTorch๋ก ๊ตฌํ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์. ์ต์ ๊ธฐ์ ์ ๋น ๋ฅด๊ฒ ์ ์ฉํ๊ณ ์ถ๋ค๋ฉด PyTorch๊ฐ ์ ๋ฆฌํ์ง.
์ ๋ต์ "์ํฉ์ ๋ฐ๋ผ ๋ค๋ฅด๋ค"์ผ! ๐
โข ํ๋ก๋์ ๋ฐฐํฌ๊ฐ ์ค์ํ๋ค๋ฉด โ TensorFlow
โข ์ฐ๊ตฌ๋ ์คํ์ด ๋ง๋ค๋ฉด โ PyTorch
โข ๋ชจ๋ฐ์ผ ์ฑ์ ๋ง๋ ๋ค๋ฉด โ TensorFlow Lite
โข ์ต์ ๊ธฐ์ ์ ๋น ๋ฅด๊ฒ ์ ์ฉํ๊ณ ์ถ๋ค๋ฉด โ PyTorch
์ฌ์ค ์์ฆ์ ๋ ํ๋ ์์ํฌ ๋ชจ๋ ์๋ก์ ์ฅ์ ์ ํก์ํ๋ฉด์ ๋ฐ์ ํ๊ณ ์์ด์, ๋ ๋ค ๋ฐฐ์๋๋ฉด ์ต๊ณ ์ง! ๐ช
๐ ๏ธ ๊ฐ๋ฐ ํ๊ฒฝ ์ค๋นํ๊ธฐ
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์์ํด๋ณผ๊น? ๋จผ์ ๊ฐ๋ฐ ํ๊ฒฝ์ ์ธํ ํด์ผ ํด. ๊ฑฑ์ ๋ง, ์๊ฐ๋ณด๋ค ์ด๋ ต์ง ์์!
๐ฆ TensorFlow ์ค์น
# Python ๊ฐ์ํ๊ฒฝ ์์ฑ (๊ถ์ฅ)
python -m venv tf_env
source tf_env/bin/activate # Windows: tf_env\Scripts\activate
# TensorFlow ์ค์น
pip install tensorflow
# GPU ๋ฒ์ ์ด ํ์ํ๋ค๋ฉด
pip install tensorflow-gpu
# ์ค์น ํ์ธ
python -c "import tensorflow as tf; print(tf.__version__)"
๐ฅ PyTorch ์ค์น
# Python ๊ฐ์ํ๊ฒฝ ์์ฑ
python -m venv pytorch_env
source pytorch_env/bin/activate # Windows: pytorch_env\Scripts\activate
# PyTorch ์ค์น (CPU ๋ฒ์ )
pip install torch torchvision torchaudio
# GPU ๋ฒ์ ์ ๊ณต์ ์ฌ์ดํธ์์ ์์คํ
์ ๋ง๋ ๋ช
๋ น์ด ํ์ธ
# https://pytorch.org/get-started/locally/
# ์ค์น ํ์ธ
python -c "import torch; print(torch.__version__)"
GPU๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด CUDA์ cuDNN์ ๋จผ์ ์ค์นํด์ผ ํด. NVIDIA GPU๊ฐ ์๋ค๋ฉด ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํด์ ์ค์นํ์. CPU๋ง์ผ๋ก๋ ์ถฉ๋ถํ ํ์ต๊ณผ ์ถ๋ก ์ด ๊ฐ๋ฅํ๋๊น ์ฒ์์ CPU ๋ฒ์ ์ผ๋ก ์์ํด๋ ๊ด์ฐฎ์!
๐จ ์ฒซ ๋ฒ์งธ AI ๋ชจ๋ธ ๋ง๋ค๊ธฐ
์ด๋ก ์ ์ด์ ๊ทธ๋ง! ์ค์ ๋ก ๊ฐ๋จํ ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ์ ๋ง๋ค์ด๋ณด์. ์๊ธ์จ ์ซ์๋ฅผ ์ธ์ํ๋ ํด๋์ํ ์์ ๋ก ์์ํ ๊ฒ. ๐๏ธ
๐ง TensorFlow๋ก ๋ชจ๋ธ ๋ง๋ค๊ธฐ
import tensorflow as tf
from tensorflow import keras
import numpy as np
# MNIST ๋ฐ์ดํฐ์
๋ก๋ (์๊ธ์จ ์ซ์ ์ด๋ฏธ์ง)
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ (0~255 ๊ฐ์ 0~1๋ก ์ ๊ทํ)
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# ๋ชจ๋ธ ๊ตฌ์กฐ ์ ์
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)), # 28x28 ์ด๋ฏธ์ง๋ฅผ 1์ฐจ์์ผ๋ก
keras.layers.Dense(128, activation='relu'), # ์๋์ธต 128๊ฐ ๋ด๋ฐ
keras.layers.Dropout(0.2), # ๊ณผ์ ํฉ ๋ฐฉ์ง
keras.layers.Dense(10, activation='softmax') # ์ถ๋ ฅ์ธต 10๊ฐ (0~9 ์ซ์)
])
# ๋ชจ๋ธ ์ปดํ์ผ
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# ๋ชจ๋ธ ํ์ต
history = model.fit(
x_train, y_train,
epochs=5,
validation_split=0.2,
batch_size=32
)
# ๋ชจ๋ธ ํ๊ฐ
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'ํ
์คํธ ์ ํ๋: {test_acc:.4f}')
# ๋ชจ๋ธ ์ ์ฅ
model.save('mnist_model.h5')
์ง์! ๐ ์ด๋ ๊ฒ ํ๋ฉด ์ฝ 97~98% ์ ํ๋์ ์๊ธ์จ ์ธ์ ๋ชจ๋ธ์ด ์์ฑ๋ผ. ์๊ฐ๋ณด๋ค ๊ฐ๋จํ์ง?
๐ฅ PyTorch๋ก ๊ฐ์ ๋ชจ๋ธ ๋ง๋ค๊ธฐ
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ ์ ์
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
# ๋ฐ์ดํฐ์
๋ก๋
train_dataset = datasets.MNIST('./data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST('./data', train=False, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
# ๋ชจ๋ธ ์ ์
class MNISTModel(nn.Module):
def __init__(self):
super(MNISTModel, self).__init__()
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(28*28, 128)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.2)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.flatten(x)
x = self.fc1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.fc2(x)
return x
# ๋ชจ๋ธ, ์์คํจ์, ์ตํฐ๋ง์ด์ ์ด๊ธฐํ
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = MNISTModel().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
# ํ์ต ํจ์
def train(model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
# ํ๊ฐ ํจ์
def test(model, device, test_loader):
model.eval()
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
accuracy = correct / len(test_loader.dataset)
print(f'ํ
์คํธ ์ ํ๋: {accuracy:.4f}')
# ํ์ต ์คํ
for epoch in range(1, 6):
train(model, device, train_loader, optimizer, epoch)
test(model, device, test_loader)
# ๋ชจ๋ธ ์ ์ฅ
torch.save(model.state_dict(), 'mnist_model.pth')
PyTorch ๋ฒ์ ์ ์ฝ๋๊ฐ ์ข ๋ ๊ธธ์ด ๋ณด์ด์ง๋ง, ๊ฐ ๋จ๊ณ๊ฐ ๋ช ํํ๊ฒ ๋ถ๋ฆฌ๋์ด ์์ด์ ์ดํดํ๊ธฐ ์ฌ์. ๋ ํ๋ ์์ํฌ ๋ชจ๋ ๋น์ทํ ๊ฒฐ๊ณผ๋ฅผ ๋ด์ง๋ง, ์ฝ๋ฉ ์คํ์ผ์ด ๋ค๋ฅด๋ค๋ ๊ฑธ ๋๋ ์ ์์ ๊ฑฐ์ผ! ๐
๐ฑ ์์ฉํ๋ก๊ทธ๋จ์ ๋ชจ๋ธ ์ฐ๋ํ๊ธฐ
์, ์ด์ ์ง์ง ์ค์ํ ๋ถ๋ถ์ด์ผ! ํ์ต๋ ๋ชจ๋ธ์ ์ค์ ์ฑ์ ์ด๋ป๊ฒ ๋ฃ์๊น? ํ๊ฒฝ๋ณ๋ก ์์๋ณด์.
๐ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ฐ๋
์น ์ฑ์์ AI ๋ชจ๋ธ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ ๊ฐ์ง์ผ. ์๋ฒ ์ฌ์ด๋์์ ์ฒ๋ฆฌํ๊ฑฐ๋, ํด๋ผ์ด์ธํธ ์ฌ์ด๋์์ ์ง์ ์คํํ๋ ๊ฑฐ์ง.
from flask import Flask, request, jsonify
import tensorflow as tf
import numpy as np
from PIL import Image
import io
app = Flask(__name__)
# ๋ชจ๋ธ ๋ก๋
model = tf.keras.models.load_model('mnist_model.h5')
@app.route('/predict', methods=['POST'])
def predict():
# ์ด๋ฏธ์ง ํ์ผ ๋ฐ๊ธฐ
if 'image' not in request.files:
return jsonify({'error': '์ด๋ฏธ์ง๊ฐ ์์ต๋๋ค'}), 400
file = request.files['image']
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
img = Image.open(io.BytesIO(file.read())).convert('L') # ํ๋ฐฑ ๋ณํ
img = img.resize((28, 28)) # ํฌ๊ธฐ ์กฐ์
img_array = np.array(img) / 255.0 # ์ ๊ทํ
img_array = img_array.reshape(1, 28, 28) # ๋ฐฐ์น ์ฐจ์ ์ถ๊ฐ
# ์์ธก
predictions = model.predict(img_array)
predicted_class = int(np.argmax(predictions[0]))
confidence = float(predictions[0][predicted_class])
return jsonify({
'predicted_digit': predicted_class,
'confidence': confidence,
'all_probabilities': predictions[0].tolist()
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
์ด์ ํด๋ผ์ด์ธํธ์์๋ ์ด๋ ๊ฒ ํธ์ถํ๋ฉด ๋ผ:
// JavaScript ์์
async function predictDigit(imageFile) {
const formData = new FormData();
formData.append('image', imageFile);
const response = await fetch('http://localhost:5000/predict', {
method: 'POST',
body: formData
});
const result = await response.json();
console.log('์์ธก ๊ฒฐ๊ณผ:', result.predicted_digit);
console.log('์ ๋ขฐ๋:', result.confidence);
}
์๋ฒ ์์ด ๋ธ๋ผ์ฐ์ ์์ ๋ฐ๋ก AI ๋ชจ๋ธ์ ๋๋ฆด ์๋ ์์ด! ์ด๊ฑด ์ง์ง ์ ๊ธฐํด. ๐คฏ
// ๋จผ์ Python์์ ๋ชจ๋ธ์ TensorFlow.js ํ์์ผ๋ก ๋ณํ
# pip install tensorflowjs
# tensorflowjs_converter --input_format keras mnist_model.h5 tfjs_model/
// HTML/JavaScript
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
<script>
let model;
// ๋ชจ๋ธ ๋ก๋
async function loadModel() {
model = await tf.loadLayersModel('tfjs_model/model.json');
console.log('๋ชจ๋ธ ๋ก๋ ์๋ฃ!');
}
// ์์ธก ํจ์
async function predict(imageElement) {
// ์ด๋ฏธ์ง๋ฅผ ํ
์๋ก ๋ณํ
let tensor = tf.browser.fromPixels(imageElement, 1) // ํ๋ฐฑ
.resizeNearestNeighbor([28, 28])
.toFloat()
.div(255.0)
.expandDims(0);
// ์์ธก
const predictions = await model.predict(tensor).data();
const predictedClass = predictions.indexOf(Math.max(...predictions));
console.log('์์ธก๋ ์ซ์:', predictedClass);
console.log('์ ๋ขฐ๋:', predictions[predictedClass]);
// ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
tensor.dispose();
return predictedClass;
}
// ํ์ด์ง ๋ก๋ ์ ๋ชจ๋ธ ๋ก๋
loadModel();
</script>
์๋ฒ ์ฌ์ด๋ ์ฅ์ :
โข ๊ฐ๋ ฅํ ํ๋์จ์ด ํ์ฉ ๊ฐ๋ฅ (GPU ๋ฑ)
โข ๋ชจ๋ธ ์ ๋ฐ์ดํธ๊ฐ ์ฌ์
โข ๋ชจ๋ธ ๋ณด์ ์ ์ง
ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ์ฅ์ :
โข ์๋ฒ ๋น์ฉ ์ ๊ฐ
โข ๋น ๋ฅธ ์๋ต ์๋ (๋คํธ์ํฌ ์ง์ฐ ์์)
โข ์คํ๋ผ์ธ์์๋ ์๋
โข ๊ฐ์ธ์ ๋ณด ๋ณดํธ (๋ฐ์ดํฐ๊ฐ ์๋ฒ๋ก ์ ๊ฐ)
๐ฑ ๋ชจ๋ฐ์ผ ์ ํ๋ฆฌ์ผ์ด์ ์ฐ๋
์ค๋งํธํฐ์์ AI๋ฅผ ๋๋ฆฌ๋ ๊ฑด ์ ๋ง ๋ฉ์ง ์ผ์ด์ผ! ์ฌ์ง ์ฐ์ผ๋ฉด ๋ฐ๋ก ์ธ์ํ๊ณ , ์ค์๊ฐ์ผ๋ก ์ฒ๋ฆฌํ๊ณ ... ์๊ฐ๋ง ํด๋ ์ ๋์ง? ๐
๋จผ์ ๋ชจ๋ธ์ ๋ชจ๋ฐ์ผ์ฉ์ผ๋ก ๋ณํํด์ผ ํด:
# Python์์ TFLite ๋ชจ๋ธ๋ก ๋ณํ
import tensorflow as tf
# Keras ๋ชจ๋ธ ๋ก๋
model = tf.keras.models.load_model('mnist_model.h5')
# TFLite ๋ณํ๊ธฐ ์์ฑ
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# ์ต์ ํ ์ต์
(์ ํ์ฌํญ)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# ๋ณํ ์คํ
tflite_model = converter.convert()
# ํ์ผ๋ก ์ ์ฅ
with open('mnist_model.tflite', 'wb') as f:
f.write(tflite_model)
print('TFLite ๋ชจ๋ธ ๋ณํ ์๋ฃ!')
์ด์ Android ์ฑ์์ ์ฌ์ฉํด๋ณด์:
// build.gradle์ ์์กด์ฑ ์ถ๊ฐ
dependencies {
implementation 'org.tensorflow:tensorflow-lite:2.13.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.4.4'
}
// Kotlin ์ฝ๋
import org.tensorflow.lite.Interpreter
import java.nio.ByteBuffer
import java.nio.ByteOrder
class DigitClassifier(private val context: Context) {
private var interpreter: Interpreter? = null
// ๋ชจ๋ธ ๋ก๋
fun initialize() {
val model = loadModelFile("mnist_model.tflite")
interpreter = Interpreter(model)
}
private fun loadModelFile(filename: String): ByteBuffer {
val assetFileDescriptor = context.assets.openFd(filename)
val inputStream = FileInputStream(assetFileDescriptor.fileDescriptor)
val fileChannel = inputStream.channel
val startOffset = assetFileDescriptor.startOffset
val declaredLength = assetFileDescriptor.declaredLength
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
}
// ์์ธก ํจ์
fun classify(bitmap: Bitmap): Int {
// ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
val resizedBitmap = Bitmap.createScaledBitmap(bitmap, 28, 28, true)
val inputBuffer = ByteBuffer.allocateDirect(4 * 28 * 28)
inputBuffer.order(ByteOrder.nativeOrder())
val pixels = IntArray(28 * 28)
resizedBitmap.getPixels(pixels, 0, 28, 0, 0, 28, 28)
for (pixel in pixels) {
val gray = (Color.red(pixel) + Color.green(pixel) + Color.blue(pixel)) / 3
inputBuffer.putFloat(gray / 255.0f)
}
// ์ถ๋ ฅ ๋ฒํผ
val outputBuffer = Array(1) { FloatArray(10) }
// ์ถ๋ก ์คํ
interpreter?.run(inputBuffer, outputBuffer)
// ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ํด๋์ค ๋ฐํ
return outputBuffer[0].indices.maxByOrNull { outputBuffer[0][it] } ?: -1
}
fun close() {
interpreter?.close()
}
}
PyTorch๋ ๋ชจ๋ฐ์ผ์ ์ง์ํด! iOS์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณผ๊น?
# Python์์ ๋ชจ๋ฐ์ผ์ฉ ๋ชจ๋ธ๋ก ๋ณํ
import torch
from torch.utils.mobile_optimizer import optimize_for_mobile
# ๋ชจ๋ธ ๋ก๋
model = MNISTModel()
model.load_state_dict(torch.load('mnist_model.pth'))
model.eval()
# ์์ ์
๋ ฅ์ผ๋ก trace
example_input = torch.rand(1, 1, 28, 28)
traced_script_module = torch.jit.trace(model, example_input)
# ๋ชจ๋ฐ์ผ ์ต์ ํ
optimized_model = optimize_for_mobile(traced_script_module)
# ์ ์ฅ
optimized_model._save_for_lite_interpreter("mnist_model.ptl")
print('PyTorch Mobile ๋ชจ๋ธ ๋ณํ ์๋ฃ!')
iOS ์ฑ์์ ์ฌ์ฉ:
// Swift ์ฝ๋
import LibTorch
class DigitClassifier {
private var module: TorchModule?
init() {
// ๋ชจ๋ธ ๋ก๋
if let filePath = Bundle.main.path(forResource: "mnist_model", ofType: "ptl") {
module = TorchModule(fileAtPath: filePath)
}
}
func predict(image: UIImage) -> Int? {
guard let module = module else { return nil }
// ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
guard let resizedImage = image.resized(to: CGSize(width: 28, height: 28)),
let pixelBuffer = resizedImage.normalized() else {
return nil
}
// ์ถ๋ก ์คํ
guard let outputs = module.predict(image: UnsafeMutableRawPointer(mutating: pixelBuffer)) else {
return nil
}
// ๊ฒฐ๊ณผ ํ์ฑ
let outputArray = outputs.assumingMemoryBound(to: Float.self)
var maxValue: Float = -Float.infinity
var maxIndex = 0
for i in 0..<10 {
if outputArray[i] > maxValue {
maxValue = outputArray[i]
maxIndex = i
}
}
return maxIndex
}
}
// UIImage ํ์ฅ
extension UIImage {
func resized(to size: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
defer { UIGraphicsEndImageContext() }
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()
}
func normalized() -> [Float]? {
guard let cgImage = self.cgImage else { return nil }
let width = cgImage.width
let height = cgImage.height
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * width
let bitsPerComponent = 8
var pixelData = [UInt8](repeating: 0, count: width * height * bytesPerPixel)
guard let context = CGContext(
data: &pixelData,
width: width,
height: height,
bitsPerComponent: bitsPerComponent,
bytesPerRow: bytesPerRow,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue
) else { return nil }
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
var normalizedData = [Float]()
for i in stride(from: 0, to: pixelData.count, by: bytesPerPixel) {
let gray = (Float(pixelData[i]) + Float(pixelData[i+1]) + Float(pixelData[i+2])) / 3.0
normalizedData.append(gray / 255.0)
}
return normalizedData
}
}
โก ์ฑ๋ฅ ์ต์ ํ ์ ๋ต
AI ๋ชจ๋ธ์ ์ฑ์ ๋ฃ์๋ค๊ณ ๋์ด ์๋์ผ. ์ค์ ์ฌ์ฉ์๋ค์ด ์ธ ๋ ๋น ๋ฅด๊ณ ํจ์จ์ ์ผ๋ก ๋์ํด์ผ ํ์์? ์ฑ๋ฅ ์ต์ ํ๋ ์ ๋ง ์ค์ํด! ๐
๐ฏ ๋ชจ๋ธ ๊ฒฝ๋ํ ๊ธฐ๋ฒ
๋ชจ๋ธ์ ๊ฐ์ค์น๋ฅผ 32๋นํธ ๋ถ๋์์์ ์์ 8๋นํธ ์ ์๋ก ๋ณํํ๋ ๊ฑฐ์ผ. ๋ชจ๋ธ ํฌ๊ธฐ๊ฐ 4๋ฐฐ ์ค์ด๋ค๊ณ ์๋๋ ๋นจ๋ผ์ ธ!
# TensorFlow ์์ํ
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# ๋ ๊ณต๊ฒฉ์ ์ธ ์์ํ (์ ํ๋ ์ฝ๊ฐ ๊ฐ์)
converter.target_spec.supported_types = [tf.float16]
# ์์ ์ ์ ์์ํ
def representative_dataset():
for _ in range(100):
data = np.random.rand(1, 28, 28, 1).astype(np.float32)
yield [data]
converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()
์ค์ํ์ง ์์ ๋ด๋ฐ ์ฐ๊ฒฐ์ ์ ๊ฑฐํ๋ ๊ฑฐ์ผ. ๋๋ฌด ๊ฐ์ง์น๊ธฐ์ฒ๋ผ!
import tensorflow_model_optimization as tfmot
# ํ๋ฃจ๋ ์ค์
pruning_params = {
'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(
initial_sparsity=0.0,
final_sparsity=0.5, # 50%์ ๊ฐ์ค์น ์ ๊ฑฐ
begin_step=0,
end_step=1000
)
}
# ๋ชจ๋ธ์ ํ๋ฃจ๋ ์ ์ฉ
model_for_pruning = tfmot.sparsity.keras.prune_low_magnitude(model, **pruning_params)
# ์ฌํ์ต
model_for_pruning.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
callbacks = [tfmot.sparsity.keras.UpdatePruningStep()]
model_for_pruning.fit(
x_train, y_train,
epochs=5,
validation_split=0.2,
callbacks=callbacks
)
# ํ๋ฃจ๋ ์ ๊ฑฐ ํ ์ต์ข
๋ชจ๋ธ
final_model = tfmot.sparsity.keras.strip_pruning(model_for_pruning)
ํฐ ๋ชจ๋ธ(์ ์๋)์ ์ง์์ ์์ ๋ชจ๋ธ(ํ์)์๊ฒ ์ ๋ฌํ๋ ๊ฑฐ์ผ. ์์ ๋ชจ๋ธ์ด ํฐ ๋ชจ๋ธ์ ์ฑ๋ฅ์ ํ๋ด๋ผ ์ ์์ง!
# ์ ์๋ ๋ชจ๋ธ (ํฐ ๋ชจ๋ธ)
teacher_model = create_large_model()
teacher_model.load_weights('teacher_weights.h5')
# ํ์ ๋ชจ๋ธ (์์ ๋ชจ๋ธ)
student_model = create_small_model()
# ์ฆ๋ฅ ์์ค ํจ์
def distillation_loss(y_true, y_pred, teacher_pred, temperature=3.0):
# ์ํํธ ํ๊ฒ ์์ค
soft_loss = tf.keras.losses.KLDivergence()(
tf.nn.softmax(teacher_pred / temperature),
tf.nn.softmax(y_pred / temperature)
) * (temperature ** 2)
# ํ๋ ํ๊ฒ ์์ค
hard_loss = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
return 0.5 * soft_loss + 0.5 * hard_loss
# ํ์ ๋ชจ๋ธ ํ์ต
for epoch in range(epochs):
for x_batch, y_batch in train_dataset:
teacher_pred = teacher_model(x_batch, training=False)
with tf.GradientTape() as tape:
student_pred = student_model(x_batch, training=True)
loss = distillation_loss(y_batch, student_pred, teacher_pred)
gradients = tape.gradient(loss, student_model.trainable_variables)
optimizer.apply_gradients(zip(gradients, student_model.trainable_variables))
๐พ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ
ํนํ ๋ชจ๋ฐ์ผ์ด๋ ์๋ฒ ๋๋ ํ๊ฒฝ์์๋ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ํ์ ์ด์ผ. ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํด์ผ ํด!
# TensorFlow์์ ๋ฉ๋ชจ๋ฆฌ ์ฆ๊ฐ ํ์ฉ
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
# PyTorch์์ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
import gc
import torch
def clear_memory():
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
# ์ถ๋ก ์ ๊ทธ๋๋์ธํธ ๊ณ์ฐ ๋นํ์ฑํ
with torch.no_grad():
output = model(input_tensor)
# ์ฌ์ฉ ํ ํ
์ ์ญ์
del input_tensor, output
clear_memory()
โก ๋ฐฐ์น ์ฒ๋ฆฌ์ ๋น๋๊ธฐ ์คํ
์ฌ๋ฌ ์์ฒญ์ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ฉด ํจ์จ์ด ์ฌ๋ผ๊ฐ!
# Flask์์ ๋ฐฐ์น ์ฒ๋ฆฌ ์์
from queue import Queue
import threading
import time
class BatchPredictor:
def __init__(self, model, batch_size=32, wait_time=0.1):
self.model = model
self.batch_size = batch_size
self.wait_time = wait_time
self.queue = Queue()
self.results = {}
# ๋ฐฑ๊ทธ๋ผ์ด๋ ์ค๋ ๋ ์์
self.thread = threading.Thread(target=self._process_batches, daemon=True)
self.thread.start()
def predict(self, data, request_id):
# ํ์ ์์ฒญ ์ถ๊ฐ
self.queue.put((request_id, data))
# ๊ฒฐ๊ณผ ๋๊ธฐ
while request_id not in self.results:
time.sleep(0.01)
result = self.results[request_id]
del self.results[request_id]
return result
def _process_batches(self):
while True:
batch_data = []
batch_ids = []
# ๋ฐฐ์น ํฌ๊ธฐ๋งํผ ๋๋ ๋๊ธฐ ์๊ฐ๊น์ง ์์ง
start_time = time.time()
while len(batch_data) < self.batch_size:
if time.time() - start_time > self.wait_time and batch_data:
break
if not self.queue.empty():
request_id, data = self.queue.get()
batch_ids.append(request_id)
batch_data.append(data)
else:
time.sleep(0.01)
if batch_data:
# ๋ฐฐ์น ์์ธก
batch_array = np.array(batch_data)
predictions = self.model.predict(batch_array)
# ๊ฒฐ๊ณผ ์ ์ฅ
for i, request_id in enumerate(batch_ids):
self.results[request_id] = predictions[i]
# ์ฌ์ฉ ์์
batch_predictor = BatchPredictor(model)
@app.route('/predict', methods=['POST'])
def predict():
data = preprocess_input(request.files['image'])
request_id = str(uuid.uuid4())
result = batch_predictor.predict(data, request_id)
return jsonify({'prediction': result.tolist()})
๐ง ์ค์ ๋ฌธ์ ํด๊ฒฐ
์ค์ ๋ก AI ๋ชจ๋ธ์ ๋ฐฐํฌํ๋ค ๋ณด๋ฉด ์ฌ๋ฌ ๋ฌธ์ ์ ๋ถ๋ชํ๊ฒ ๋ผ. ๋ด๊ฐ ๊ฒช์๋ ๋ฌธ์ ๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๊ณต์ ํ ๊ฒ! ๐ช
โ ๋ฌธ์ 1: ๋ชจ๋ธ ๋ฒ์ ๋ถ์ผ์น
ํด๊ฒฐ์ฑ :
# requirements.txt๋ก ๋ฒ์ ๊ณ ์
tensorflow==2.13.0
torch==2.0.1
numpy==1.24.3
# Docker๋ก ํ๊ฒฝ ํต์ผ
FROM python:3.9-slim
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
WORKDIR /app
CMD ["python", "app.py"]
# ๋ชจ๋ธ ์ ์ฅ ์ ๋ฒ์ ์ ๋ณด ํจ๊ป ์ ์ฅ
import json
model_info = {
'tensorflow_version': tf.__version__,
'model_architecture': model.to_json(),
'training_date': datetime.now().isoformat()
}
with open('model_info.json', 'w') as f:
json.dump(model_info, f)
โ ๋ฌธ์ 2: ์ถ๋ก ์๋๊ฐ ๋๋ฌด ๋๋ฆผ
ํด๊ฒฐ์ฑ :
์์์ ์ค๋ช ํ ์์ํ, ํ๋ฃจ๋ ์ ์ฉ
2. ํ๋์จ์ด ๊ฐ์ ํ์ฉ
# TensorFlow GPU ์ฌ์ฉ
with tf.device('/GPU:0'):
predictions = model.predict(data)
# ONNX Runtime์ผ๋ก ๋ณํ (๋ ๋น ๋ฅธ ์ถ๋ก )
# pip install tf2onnx onnxruntime
import tf2onnx
import onnxruntime as ort
# TensorFlow ๋ชจ๋ธ์ ONNX๋ก ๋ณํ
spec = (tf.TensorSpec((None, 28, 28), tf.float32, name="input"),)
output_path = "model.onnx"
model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, output_path=output_path)
# ONNX Runtime์ผ๋ก ์ถ๋ก
session = ort.InferenceSession("model.onnx")
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
result = session.run([output_name], {input_name: data})
from functools import lru_cache
import hashlib
# ๋์ผํ ์
๋ ฅ์ ๋ํด ๊ฒฐ๊ณผ ์บ์ฑ
@lru_cache(maxsize=1000)
def predict_cached(image_hash):
# ์ค์ ์์ธก ์ํ
return model.predict(image_data)
def predict_with_cache(image_data):
# ์ด๋ฏธ์ง ํด์ ์์ฑ
image_hash = hashlib.md5(image_data.tobytes()).hexdigest()
return predict_cached(image_hash)
โ ๋ฌธ์ 3: ๋ชจ๋ธ ํฌ๊ธฐ๊ฐ ๋๋ฌด ์ปค์ ์ฑ ์ฉ๋ ์ฆ๊ฐ
ํด๊ฒฐ์ฑ :
// Android์์ Firebase ML Kit ์ฌ์ฉ
dependencies {
implementation 'com.google.firebase:firebase-ml-model-interpreter:22.0.4'
}
// ๋ชจ๋ธ์ Firebase์ ์
๋ก๋ํ๊ณ ํ์ํ ๋ ๋ค์ด๋ก๋
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder()
.requireWifi()
.build();
FirebaseRemoteModel remoteModel = new FirebaseRemoteModel.Builder("mnist_model")
.enableModelUpdates(true)
.setInitialDownloadConditions(conditions)
.setUpdatesDownloadConditions(conditions)
.build();
FirebaseModelManager.getInstance().registerRemoteModel(remoteModel);
# ๋ชจ๋ธ ํ์ผ ์์ถ
import gzip
import shutil
with open('model.tflite', 'rb') as f_in:
with gzip.open('model.tflite.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
# ์ฑ์์ ์์ถ ํด์ ํ ์ฌ์ฉ
import gzip
def load_compressed_model(compressed_path):
with gzip.open(compressed_path, 'rb') as f:
model_data = f.read()
# ์์ ํ์ผ๋ก ์ ์ฅ
temp_path = 'temp_model.tflite'
with open(temp_path, 'wb') as f:
f.write(model_data)
return temp_path
โ ๋ฌธ์ 4: ๋ค์ํ ์ ๋ ฅ ํ์ ์ฒ๋ฆฌ
ํด๊ฒฐ์ฑ :
from PIL import Image
import numpy as np
def robust_preprocess(image_input):
"""
๋ค์ํ ์
๋ ฅ ํ์์ ์ฒ๋ฆฌํ๋ ์ ์ฒ๋ฆฌ ํจ์
"""
try:
# ํ์ผ ๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ
if isinstance(image_input, str):
img = Image.open(image_input)
# PIL Image์ธ ๊ฒฝ์ฐ
elif isinstance(image_input, Image.Image):
img = image_input
# numpy array์ธ ๊ฒฝ์ฐ
elif isinstance(image_input, np.ndarray):
img = Image.fromarray(image_input)
# bytes์ธ ๊ฒฝ์ฐ
elif isinstance(image_input, bytes):
img = Image.open(io.BytesIO(image_input))
else:
raise ValueError(f"์ง์ํ์ง ์๋ ์
๋ ฅ ํ์
: {type(image_input)}")
# RGBA๋ฅผ RGB๋ก ๋ณํ
if img.mode == 'RGBA':
background = Image.new('RGB', img.size, (255, 255, 255))
background.paste(img, mask=img.split()[3])
img = background
# ํ๋ฐฑ ๋ณํ
img = img.convert('L')
# ํฌ๊ธฐ ์กฐ์
img = img.resize((28, 28), Image.Resampling.LANCZOS)
# numpy array๋ก ๋ณํ ๋ฐ ์ ๊ทํ
img_array = np.array(img).astype('float32') / 255.0
# ๋ฐฐ์น ์ฐจ์ ์ถ๊ฐ
img_array = np.expand_dims(img_array, axis=0)
return img_array
except Exception as e:
print(f"์ ์ฒ๋ฆฌ ์ค ์๋ฌ ๋ฐ์: {str(e)}")
return None
# ์ฌ์ฉ ์์
@app.route('/predict', methods=['POST'])
def predict():
try:
if 'image' in request.files:
image_input = request.files['image'].read()
elif 'image_url' in request.json:
response = requests.get(request.json['image_url'])
image_input = response.content
else:
return jsonify({'error': '์ด๋ฏธ์ง๊ฐ ์ ๊ณต๋์ง ์์์ต๋๋ค'}), 400
processed_image = robust_preprocess(image_input)
if processed_image is None:
return jsonify({'error': '์ด๋ฏธ์ง ์ฒ๋ฆฌ ์คํจ'}), 400
prediction = model.predict(processed_image)
return jsonify({
'success': True,
'prediction': int(np.argmax(prediction)),
'confidence': float(np.max(prediction))
})
except Exception as e:
return jsonify({'error': str(e)}), 500
๐ ๊ณ ๊ธ ํ์ฉ ์ฌ๋ก
๊ธฐ๋ณธ์ ๋์ด์ ์ข ๋ ๋ฉ์ง ๊ฑธ ๋ง๋ค์ด๋ณผ๊น? ์ค์ ํ๋ก์ ํธ์์ ํ์ฉํ ์ ์๋ ๊ณ ๊ธ ๊ธฐ๋ฒ๋ค์ ์๊ฐํ ๊ฒ! ๐จ
๐ญ ๋ฉํฐ๋ชจ๋ฌ AI ์์คํ
์ด๋ฏธ์ง์ ํ ์คํธ๋ฅผ ๋์์ ์ฒ๋ฆฌํ๋ ์์คํ ์ ๋ง๋ค์ด๋ณด์. ์๋ฅผ ๋ค์ด, ์ํ ์ด๋ฏธ์ง์ ์ค๋ช ์ ํจ๊ป ๋ถ์ํ๋ ๊ฑฐ์ผ.
import tensorflow as tf
from tensorflow.keras import layers, Model
class MultimodalModel(Model):
def __init__(self, num_classes=10):
super(MultimodalModel, self).__init__()
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ธ๋์น
self.image_conv1 = layers.Conv2D(32, 3, activation='relu')
self.image_pool1 = layers.MaxPooling2D()
self.image_conv2 = layers.Conv2D(64, 3, activation='relu')
self.image_pool2 = layers.MaxPooling2D()
self.image_flatten = layers.Flatten()
self.image_dense = layers.Dense(128, activation='relu')
# ํ
์คํธ ์ฒ๋ฆฌ ๋ธ๋์น
self.text_embedding = layers.Embedding(10000, 128)
self.text_lstm = layers.LSTM(64)
self.text_dense = layers.Dense(128, activation='relu')
# ๊ฒฐํฉ ๋ ์ด์ด
self.concat = layers.Concatenate()
self.fusion_dense1 = layers.Dense(256, activation='relu')
self.fusion_dropout = layers.Dropout(0.5)
self.fusion_dense2 = layers.Dense(128, activation='relu')
self.output_layer = layers.Dense(num_classes, activation='softmax')
def call(self, inputs):
image_input, text_input = inputs
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ
x_img = self.image_conv1(image_input)
x_img = self.image_pool1(x_img)
x_img = self.image_conv2(x_img)
x_img = self.image_pool2(x_img)
x_img = self.image_flatten(x_img)
x_img = self.image_dense(x_img)
# ํ
์คํธ ์ฒ๋ฆฌ
x_text = self.text_embedding(text_input)
x_text = self.text_lstm(x_text)
x_text = self.text_dense(x_text)
# ๊ฒฐํฉ
x = self.concat([x_img, x_text])
x = self.fusion_dense1(x)
x = self.fusion_dropout(x)
x = self.fusion_dense2(x)
output = self.output_layer(x)
return output
# ๋ชจ๋ธ ์์ฑ ๋ฐ ํ์ต
model = MultimodalModel(num_classes=10)
# ๋๋ฏธ ๋ฐ์ดํฐ๋ก ํ
์คํธ
image_data = tf.random.normal((32, 64, 64, 3))
text_data = tf.random.uniform((32, 100), maxval=10000, dtype=tf.int32)
output = model([image_data, text_data])
print(f"์ถ๋ ฅ ํํ: {output.shape}") # (32, 10)
๐ ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ์ฒ๋ฆฌ
๋น๋์ค ์คํธ๋ฆผ์์ ์ค์๊ฐ์ผ๋ก ๊ฐ์ฒด๋ฅผ ๊ฐ์งํ๋ ์์คํ ์ด์ผ. ์น์บ ์ด๋ CCTV ์์์ ์ฒ๋ฆฌํ ๋ ์ ์ฉํด!
import cv2
import numpy as np
import tensorflow as tf
from collections import deque
import time
class RealtimeDetector:
def __init__(self, model_path, fps_smoothing=30):
self.model = tf.keras.models.load_model(model_path)
self.fps_buffer = deque(maxlen=fps_smoothing)
self.last_time = time.time()
def preprocess_frame(self, frame):
# ํ๋ ์ ์ ์ฒ๋ฆฌ
resized = cv2.resize(frame, (224, 224))
normalized = resized.astype('float32') / 255.0
batched = np.expand_dims(normalized, axis=0)
return batched
def postprocess_prediction(self, prediction, threshold=0.5):
# ์์ธก ๊ฒฐ๊ณผ ํ์ฒ๋ฆฌ
class_id = np.argmax(prediction)
confidence = prediction[0][class_id]
if confidence > threshold:
return class_id, confidence
return None, 0.0
def calculate_fps(self):
current_time = time.time()
fps = 1.0 / (current_time - self.last_time)
self.last_time = current_time
self.fps_buffer.append(fps)
return np.mean(self.fps_buffer)
def draw_info(self, frame, class_id, confidence, fps):
# ์ ๋ณด ํ์
if class_id is not None:
text = f"Class: {class_id}, Conf: {confidence:.2f}"
cv2.putText(frame, text, (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
fps_text = f"FPS: {fps:.1f}"
cv2.putText(frame, fps_text, (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
return frame
def run(self, video_source=0):
cap = cv2.VideoCapture(video_source)
print("์ค์๊ฐ ๊ฐ์ง ์์... (q๋ฅผ ๋๋ฌ ์ข
๋ฃ)")
while True:
ret, frame = cap.read()
if not ret:
break
# ํ๋ ์ ์ฒ๋ฆฌ
processed = self.preprocess_frame(frame)
# ์์ธก
prediction = self.model.predict(processed, verbose=0)
# ํ์ฒ๋ฆฌ
class_id, confidence = self.postprocess_prediction(prediction)
# FPS ๊ณ์ฐ
fps = self.calculate_fps()
# ์ ๋ณด ํ์
display_frame = self.draw_info(frame, class_id, confidence, fps)
# ํ๋ฉด ์ถ๋ ฅ
cv2.imshow('Realtime Detection', display_frame)
# ์ข
๋ฃ ํค ํ์ธ
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# ์ฌ์ฉ ์์
detector = RealtimeDetector('path/to/model.h5')
detector.run(video_source=0) # 0์ ๊ธฐ๋ณธ ์น์บ
๐ ๋ถ์ฐ ์ถ๋ก ์์คํ
ํธ๋ํฝ์ด ๋ง์ ๋๋ ์ฌ๋ฌ ์๋ฒ์ ๋ถํ๋ฅผ ๋ถ์ฐํด์ผ ํด. ๋ก๋ ๋ฐธ๋ฐ์ฑ๊ณผ ํจ๊ป ์ฌ์ฉํ๋ ๋ถ์ฐ ์ถ๋ก ์์คํ ์ด์ผ.
import redis
import pickle
import uuid
from flask import Flask, request, jsonify
import tensorflow as tf
import numpy as np
from multiprocessing import Process, Queue
app = Flask(__name__)
# Redis ์ฐ๊ฒฐ (์์
ํ๋ก ์ฌ์ฉ)
redis_client = redis.Redis(host='localhost', port=6379, db=0)
class InferenceWorker:
def __init__(self, model_path, worker_id):
self.model = tf.keras.models.load_model(model_path)
self.worker_id = worker_id
print(f"Worker {worker_id} ์์๋จ")
def process_requests(self):
while True:
# Redis์์ ์์
๊ฐ์ ธ์ค๊ธฐ
task = redis_client.blpop('inference_queue', timeout=1)
if task:
_, task_data = task
task_info = pickle.loads(task_data)
request_id = task_info['request_id']
data = task_info['data']
try:
# ์ถ๋ก ์ํ
result = self.model.predict(data)
# ๊ฒฐ๊ณผ ์ ์ฅ
result_data = {
'success': True,
'prediction': result.tolist(),
'worker_id': self.worker_id
}
redis_client.setex(
f'result:{request_id}',
60, # 60์ด ํ ์๋ ์ญ์
pickle.dumps(result_data)
)
except Exception as e:
error_data = {
'success': False,
'error': str(e),
'worker_id': self.worker_id
}
redis_client.setex(
f'result:{request_id}',
60,
pickle.dumps(error_data)
)
def start_worker(model_path, worker_id):
worker = InferenceWorker(model_path, worker_id)
worker.process_requests()
# Flask API
@app.route('/predict', methods=['POST'])
def predict():
try:
# ์์ฒญ ๋ฐ์ดํฐ ๋ฐ๊ธฐ
data = np.array(request.json['data'])
request_id = str(uuid.uuid4())
# ์์
ํ์ ์ถ๊ฐ
task_data = {
'request_id': request_id,
'data': data
}
redis_client.rpush('inference_queue', pickle.dumps(task_data))
# ๊ฒฐ๊ณผ ๋๊ธฐ (์ต๋ 30์ด)
for _ in range(300): # 0.1์ด์ฉ 300๋ฒ = 30์ด
result_key = f'result:{request_id}'
result_data = redis_client.get(result_key)
if result_data:
result = pickle.loads(result_data)
redis_client.delete(result_key)
return jsonify(result)
time.sleep(0.1)
return jsonify({'error': 'ํ์์์'}), 408
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/health', methods=['GET'])
def health():
queue_length = redis_client.llen('inference_queue')
return jsonify({
'status': 'healthy',
'queue_length': queue_length
})
if __name__ == '__main__':
# ์์ปค ํ๋ก์ธ์ค ์์ (4๊ฐ)
num_workers = 4
model_path = 'path/to/model.h5'
workers = []
for i in range(num_workers):
p = Process(target=start_worker, args=(model_path, i))
p.start()
workers.append(p)
# Flask ์๋ฒ ์์
app.run(host='0.0.0.0', port=5000)
๐ ๋ชจ๋ํฐ๋ง๊ณผ ๋ก๊น
AI ๋ชจ๋ธ์ ๋ฐฐํฌํ ํ์๋ ์ ์๋ํ๋์ง ๊ณ์ ํ์ธํด์ผ ํด. ๋ชจ๋ํฐ๋ง๊ณผ ๋ก๊น ์ ์ ๋ง ์ค์ํด! ๐
๐ ์์ธ ๋ก๊น ์์คํ
import logging
import json
from datetime import datetime
import numpy as np
class ModelLogger:
def __init__(self, log_file='model_inference.log'):
self.logger = logging.getLogger('ModelInference')
self.logger.setLevel(logging.INFO)
# ํ์ผ ํธ๋ค๋ฌ
fh = logging.FileHandler(log_file)
fh.setLevel(logging.INFO)
# ํฌ๋งท ์ค์
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
fh.setFormatter(formatter)
self.logger.addHandler(fh)
def log_inference(self, input_data, prediction, confidence,
inference_time, metadata=None):
log_entry = {
'timestamp': datetime.now().isoformat(),
'input_shape': input_data.shape if hasattr(input_data, 'shape') else None,
'prediction': int(prediction) if isinstance(prediction, (np.integer, int)) else prediction,
'confidence': float(confidence),
'inference_time_ms': float(inference_time * 1000),
'metadata': metadata or {}
}
self.logger.info(json.dumps(log_entry))
def log_error(self, error_message, input_data=None, metadata=None):
log_entry = {
'timestamp': datetime.now().isoformat(),
'error': str(error_message),
'input_shape': input_data.shape if input_data is not None and hasattr(input_data, 'shape') else None,
'metadata': metadata or {}
}
self.logger.error(json.dumps(log_entry))
# ์ฌ์ฉ ์์
logger = ModelLogger()
@app.route('/predict', methods=['POST'])
def predict():
start_time = time.time()
try:
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ
data = preprocess_input(request.files['image'])
# ์์ธก
prediction = model.predict(data)
predicted_class = int(np.argmax(prediction))
confidence = float(np.max(prediction))
# ์ถ๋ก ์๊ฐ ๊ณ์ฐ
inference_time = time.time() - start_time
# ๋ก๊น
logger.log_inference(
input_data=data,
prediction=predicted_class,
confidence=confidence,
inference_time=inference_time,
metadata={
'user_agent': request.headers.get('User-Agent'),
'ip_address': request.remote_addr
}
)
return jsonify({
'prediction': predicted_class,
'confidence': confidence,
'inference_time': inference_time
})
except Exception as e:
logger.log_error(
error_message=str(e),
metadata={
'endpoint': '/predict',
'method': request.method
}
)
return jsonify({'error': str(e)}), 500
๐ ์ฑ๋ฅ ๋ฉํธ๋ฆญ ์์ง
from prometheus_client import Counter, Histogram, Gauge, start_http_server
import time
# Prometheus ๋ฉํธ๋ฆญ ์ ์
inference_counter = Counter(
'model_inference_total',
'Total number of inferences',
['model_name', 'status']
)
inference_duration = Histogram(
'model_inference_duration_seconds',
'Inference duration in seconds',
['model_name']
)
prediction_confidence = Histogram(
'model_prediction_confidence',
'Prediction confidence scores',
['model_name', 'predicted_class']
)
active_requests = Gauge(
'model_active_requests',
'Number of active inference requests',
['model_name']
)
class MetricsCollector:
def __init__(self, model_name='default'):
self.model_name = model_name
def record_inference(self, duration, confidence, predicted_class, success=True):
# ์ถ๋ก ํ์ ๊ธฐ๋ก
status = 'success' if success else 'error'
inference_counter.labels(
model_name=self.model_name,
status=status
).inc()
if success:
# ์ถ๋ก ์๊ฐ ๊ธฐ๋ก
inference_duration.labels(
model_name=self.model_name
).observe(duration)
# ์ ๋ขฐ๋ ๊ธฐ๋ก
prediction_confidence.labels(
model_name=self.model_name,
predicted_class=str(predicted_class)
).observe(confidence)
def track_active_request(self):
active_requests.labels(model_name=self.model_name).inc()
class RequestTracker:
def __init__(self, gauge, model_name):
self.gauge = gauge
self.model_name = model_name
def __enter__(self):
return self
def __exit__(self, *args):
self.gauge.labels(model_name=self.model_name).dec()
return RequestTracker(active_requests, self.model_name)
# Prometheus ์๋ฒ ์์ (๋ณ๋ ํฌํธ)
start_http_server(8000)
metrics = MetricsCollector(model_name='mnist_classifier')
@app.route('/predict', methods=['POST'])
def predict():
with metrics.track_active_request():
start_time = time.time()
try:
# ์์ธก ์ํ
data = preprocess_input(request.files['image'])
prediction = model.predict(data)
predicted_class = int(np.argmax(prediction))
confidence = float(np.max(prediction))
duration = time.time() - start_time
# ๋ฉํธ๋ฆญ ๊ธฐ๋ก
metrics.record_inference(
duration=duration,
confidence=confidence,
predicted_class=predicted_class,
success=True
)
return jsonify({
'prediction': predicted_class,
'confidence': confidence
})
except Exception as e:
duration = time.time() - start_time
metrics.record_inference(
duration=duration,
confidence=0.0,
predicted_class=-1,
success=False
)
return jsonify({'error': str(e)}), 500
๐ ์ค์ ํ๋ก์ ํธ ์์
์ด๋ก ๊ณผ ์ฝ๋๋ง ๋ณด๋ฉด ์ฌ๋ฏธ์์ง? ์ค์ ๋ก ๋ง๋ค ์ ์๋ ํ๋ก์ ํธ ์์ด๋์ด๋ฅผ ๋ช ๊ฐ์ง ์๊ฐํ ๊ฒ! ๐
์ค๋งํธํฐ์ผ๋ก ๋ฌธ์๋ฅผ ์ฐ์ผ๋ฉด ์๋์ผ๋ก ํ ์คํธ๋ฅผ ์ถ์ถํ๊ณ ์ ๋ฆฌํด์ฃผ๋ ์ฑ
ํ์ํ ๊ธฐ์ :
โข ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ (์๊ทผ ๋ณด์ , ๋ ธ์ด์ฆ ์ ๊ฑฐ)
โข OCR ๋ชจ๋ธ (TensorFlow/PyTorch)
โข ํ ์คํธ ํ์ฒ๋ฆฌ (๋ง์ถค๋ฒ ๊ฒ์ฌ, ํฌ๋งทํ )
โข ๋ชจ๋ฐ์ผ ์ฑ (TensorFlow Lite ํ์ฉ)
์ฌ์ฉ์์ ๋ฉ์์ง๋ฅผ ๋ถ์ํด์ ๊ฐ์ ์ํ๋ฅผ ํ์ ํ๊ณ ์ ์ ํ๊ฒ ์๋ตํ๋ ์ฑ๋ด
ํ์ํ ๊ธฐ์ :
โข ์์ฐ์ด ์ฒ๋ฆฌ (BERT, GPT ๋ฑ)
โข ๊ฐ์ ๋ถ๋ฅ ๋ชจ๋ธ
โข ์น์์ผ ํต์ (์ค์๊ฐ ์ฑํ )
โข Flask/FastAPI ๋ฐฑ์๋
์ฌ์ฉ์์ ๊ตฌ๋งค ์ด๋ ฅ๊ณผ ์ ํธ๋๋ฅผ ๋ถ์ํด์ ๋ง์ถคํ ์ํ์ ์ถ์ฒ
ํ์ํ ๊ธฐ์ :
โข ํ์ ํํฐ๋ง
โข ๋ฅ๋ฌ๋ ์ถ์ฒ ๋ชจ๋ธ (Neural Collaborative Filtering)
โข ์ด๋ฏธ์ง ์ ์ฌ๋ ๋ถ์
โข REST API ์๋ฒ
์ด๋ฐ ํ๋ก์ ํธ๋ค์ ๋ง๋ค๋ฉด์ ์ค๋ ฅ์ ์์ ์ ์์ด. ํนํ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์์ ์ AI ๊ฐ๋ฐ ๋ฅ๋ ฅ์ ๊ณต์ ํ๊ฑฐ๋, ํ๋ก์ ํธ๋ฅผ ์๋ขฐ๋ฐ์ ์ค์ ๊ฒฝํ์ ์๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ! ๐ช
๐ ๋ณด์๊ณผ ๊ฐ์ธ์ ๋ณด ๋ณดํธ
AI ๋ชจ๋ธ์ ๋ฐฐํฌํ ๋ ๋ณด์๋ ์ ๋ง ์ค์ํด. ํนํ ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฌ๋ค๋ฉด ๋์ฑ ์ ๊ฒฝ ์จ์ผ ํ์ง!
๐ก๏ธ ๋ชจ๋ธ ๋ณด์
from cryptography.fernet import Fernet
import pickle
# ์ํธํ ํค ์์ฑ
key = Fernet.generate_key()
cipher = Fernet(key)
# ๋ชจ๋ธ ์ ์ฅ ๋ฐ ์ํธํ
model_data = pickle.dumps(model)
encrypted_model = cipher.encrypt(model_data)
with open('encrypted_model.bin', 'wb') as f:
f.write(encrypted_model)
# ํค๋ ์์ ํ ๊ณณ์ ๋ณ๋ ๋ณด๊ด
with open('model_key.key', 'wb') as f:
f.write(key)
# ๋ชจ๋ธ ๋ก๋ ๋ฐ ๋ณตํธํ
def load_encrypted_model(model_path, key_path):
with open(key_path, 'rb') as f:
key = f.read()
cipher = Fernet(key)
with open(model_path, 'rb') as f:
encrypted_data = f.read()
decrypted_data = cipher.decrypt(encrypted_data)
model = pickle.loads(decrypted_data)
return model
from functools import wraps
from flask import request, jsonify
import jwt
import datetime
SECRET_KEY = 'your-secret-key-here'
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = request.headers.get('Authorization')
if not token:
return jsonify({'error': 'ํ ํฐ์ด ์์ต๋๋ค'}), 401
try:
# Bearer ํ ํฐ์์ ์ค์ ํ ํฐ ์ถ์ถ
token = token.split(' ')[1] if ' ' in token else token
# ํ ํฐ ๊ฒ์ฆ
data = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
request.user_id = data['user_id']
except jwt.ExpiredSignatureError:
return jsonify({'error': 'ํ ํฐ์ด ๋ง๋ฃ๋์์ต๋๋ค'}), 401
except jwt.InvalidTokenError:
return jsonify({'error': '์ ํจํ์ง ์์ ํ ํฐ์
๋๋ค'}), 401
return f(*args, **kwargs)
return decorated
@app.route('/predict', methods=['POST'])
@token_required
def predict():
# ์ธ์ฆ๋ ์ฌ์ฉ์๋ง ์ ๊ทผ ๊ฐ๋ฅ
user_id = request.user_id
# ... ์์ธก ๋ก์ง ...
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["200 per day", "50 per hour"]
)
@app.route('/predict', methods=['POST'])
@limiter.limit("10 per minute")
def predict():
# ๋ถ๋น 10ํ๋ก ์ ํ
# ... ์์ธก ๋ก์ง ...
๐ ๋ฐ์ดํฐ ํ๋ผ์ด๋ฒ์
import hashlib
def anonymize_user_data(user_id, data):
# ์ฌ์ฉ์ ID ํด์ฑ
hashed_id = hashlib.sha256(user_id.encode()).hexdigest()
# ๊ฐ์ธ์ ๋ณด ์ ๊ฑฐ
anonymized_data = {
'user_hash': hashed_id,
'timestamp': data.get('timestamp'),
'features': data.get('features'),
# ์ด๋ฆ, ์ด๋ฉ์ผ ๋ฑ ๊ฐ์ธ์ ๋ณด๋ ์ ์ธ
}
return anonymized_data
๊ฐ๋ฅํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ๋ณด๋ด์ง ์๊ณ ์ฌ์ฉ์ ๊ธฐ๊ธฐ์์ ์ง์ ์ฒ๋ฆฌํ๋ ๊ฒ ๊ฐ์ฅ ์์ ํด. TensorFlow Lite๋ PyTorch Mobile์ ํ์ฉํ๋ฉด ๋ผ!
3. ๋ฐ์ดํฐ ๋ณด๊ด ๊ธฐ๊ฐ ์ค์
from datetime import datetime, timedelta
import schedule
def cleanup_old_data():
"""30์ผ ์ด์ ๋ ๋ฐ์ดํฐ ์ญ์ """
cutoff_date = datetime.now() - timedelta(days=30)
# ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ค๋๋ ๋ฐ์ดํฐ ์ญ์
db.execute(
"DELETE FROM inference_logs WHERE timestamp < ?",
(cutoff_date,)
)
print(f"์ค๋๋ ๋ฐ์ดํฐ ์ ๋ฆฌ ์๋ฃ: {cutoff_date}")
# ๋งค์ผ ์์ ์ ์คํ
schedule.every().day.at("00:00").do(cleanup_old_data)
๐ ๋ฐฐํฌ ์ ๋ต
๊ฐ๋ฐ์ด ๋๋ฌ๋ค๋ฉด ์ด์ ์ค์ ์๋น์ค๋ก ๋ฐฐํฌํ ์ฐจ๋ก์ผ! ์์ ์ ์ธ ๋ฐฐํฌ๋ฅผ ์ํ ์ ๋ต๋ค์ ์์๋ณด์.
๐ณ Docker ์ปจํ ์ด๋ํ
# Dockerfile
FROM python:3.9-slim
# ์์
๋๋ ํ ๋ฆฌ ์ค์
WORKDIR /app
# ์์คํ
ํจํค์ง ์ค์น
RUN apt-get update && apt-get install -y \
libgomp1 \
&& rm -rf /var/lib/apt/lists/*
# Python ํจํค์ง ์ค์น
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# ์ ํ๋ฆฌ์ผ์ด์
์ฝ๋ ๋ณต์ฌ
COPY . .
# ๋ชจ๋ธ ํ์ผ ๋ณต์ฌ
COPY models/ /app/models/
# ํฌํธ ๋
ธ์ถ
EXPOSE 5000
# ํฌ์ค์ฒดํฌ
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:5000/health || exit 1
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "4", "--timeout", "120", "app:app"]
# docker-compose.yml
version: '3.8'
services:
ai-api:
build: .
ports:
- "5000:5000"
environment:
- MODEL_PATH=/app/models/model.h5
- REDIS_HOST=redis
- REDIS_PORT=6379
volumes:
- ./models:/app/models
depends_on:
- redis
restart: unless-stopped
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis-data:/data
restart: unless-stopped
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./ssl:/etc/nginx/ssl
depends_on:
- ai-api
restart: unless-stopped
volumes:
redis-data:
โ๏ธ ํด๋ผ์ฐ๋ ๋ฐฐํฌ
# AWS Elastic Beanstalk ์ค์
# .ebextensions/01_packages.config
packages:
yum:
git: []
gcc: []
# .ebextensions/02_python.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: app:app
aws:elasticbeanstalk:application:environment:
MODEL_PATH: "/var/app/current/models/model.h5"
# ๋ฐฐํฌ ๋ช
๋ น์ด
# eb init -p python-3.9 my-ai-app
# eb create my-ai-env
# eb deploy
# app.yaml (Google App Engine)
runtime: python39
entrypoint: gunicorn -b :$PORT app:app
instance_class: F4_1G
automatic_scaling:
target_cpu_utilization: 0.65
min_instances: 1
max_instances: 10
env_variables:
MODEL_PATH: "gs://my-bucket/models/model.h5"
# ๋ฐฐํฌ ๋ช
๋ น์ด
# gcloud app deploy
๐ CI/CD ํ์ดํ๋ผ์ธ
# .github/workflows/deploy.yml
name: Deploy AI Model
on:
push:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pytest
- name: Run tests
run: pytest tests/
build-and-deploy:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build Docker image
run: docker build -t my-ai-app:${{ github.sha }} .
- name: Push to Docker Hub
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker tag my-ai-app:${{ github.sha }} username/my-ai-app:latest
docker push username/my-ai-app:latest
- name: Deploy to production
run: |
# ๋ฐฐํฌ ์คํฌ๋ฆฝํธ ์คํ
./deploy.sh
๐ A/B ํ ์คํ ๊ณผ ๋ชจ๋ธ ์ ๋ฐ์ดํธ
์๋ก์ด ๋ชจ๋ธ์ ๋ฐฐํฌํ ๋๋ ์กฐ์ฌ์ค๋ฝ๊ฒ ํด์ผ ํด. A/B ํ ์คํ ์ผ๋ก ์์ ํ๊ฒ ์ ๋ฐ์ดํธํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์!
import random
import tensorflow as tf
class ModelManager:
def __init__(self):
self.models = {
'v1': tf.keras.models.load_model('model_v1.h5'),
'v2': tf.keras.models.load_model('model_v2.h5')
}
self.traffic_split = {
'v1': 0.9, # 90% ํธ๋ํฝ
'v2': 0.1 # 10% ํธ๋ํฝ
}
self.performance_metrics = {
'v1': {'count': 0, 'total_time': 0, 'errors': 0},
'v2': {'count': 0, 'total_time': 0, 'errors': 0}
}
def select_model(self, user_id=None):
"""ํธ๋ํฝ ๋ถํ ์ ๋ฐ๋ผ ๋ชจ๋ธ ์ ํ"""
if user_id:
# ๊ฐ์ ์ฌ์ฉ์๋ ํญ์ ๊ฐ์ ๋ชจ๋ธ ์ฌ์ฉ (์ผ๊ด์ฑ)
random.seed(user_id)
rand = random.random()
cumulative = 0
for version, ratio in self.traffic_split.items():
cumulative += ratio
if rand < cumulative:
return version, self.models[version]
return 'v1', self.models['v1']
def predict(self, data, user_id=None):
version, model = self.select_model(user_id)
start_time = time.time()
try:
result = model.predict(data)
inference_time = time.time() - start_time
# ๋ฉํธ๋ฆญ ์
๋ฐ์ดํธ
self.performance_metrics[version]['count'] += 1
self.performance_metrics[version]['total_time'] += inference_time
return {
'prediction': result,
'model_version': version,
'inference_time': inference_time
}
except Exception as e:
self.performance_metrics[version]['errors'] += 1
raise e
def get_performance_report(self):
"""๊ฐ ๋ชจ๋ธ์ ์ฑ๋ฅ ๋น๊ต"""
report = {}
for version, metrics in self.performance_metrics.items():
if metrics['count'] > 0:
avg_time = metrics['total_time'] / metrics['count']
error_rate = metrics['errors'] / metrics['count']
report[version] = {
'total_requests': metrics['count'],
'avg_inference_time': avg_time,
'error_rate': error_rate
}
return report
def update_traffic_split(self, new_split):
"""ํธ๋ํฝ ๋ถํ ๋น์จ ์
๋ฐ์ดํธ"""
if sum(new_split.values()) != 1.0:
raise ValueError("ํธ๋ํฝ ๋น์จ์ ํฉ์ 1.0์ด์ด์ผ ํฉ๋๋ค")
self.traffic_split = new_split
print(f"ํธ๋ํฝ ๋ถํ ์
๋ฐ์ดํธ: {new_split}")
# ์ฌ์ฉ ์์
model_manager = ModelManager()
@app.route('/predict', methods=['POST'])
def predict():
user_id = request.headers.get('User-ID')
data = preprocess_input(request.files['image'])
result = model_manager.predict(data, user_id)
return jsonify(result)
@app.route('/performance', methods=['GET'])
def performance():
report = model_manager.get_performance_report()
return jsonify(report)
@app.route('/update_traffic', methods=['POST'])
def update_traffic():
new_split = request.json
model_manager.update_traffic_split(new_split)
return jsonify({'status': 'success'})
1. ์ ์ง์ ๋กค์์: ์ฒ์์ 5-10%๋ง ์ ๋ชจ๋ธ๋ก ๋ณด๋ด๊ณ , ๋ฌธ์ ์์ผ๋ฉด ์ ์ฐจ ๋๋ ค๊ฐ
2. ๋ชจ๋ํฐ๋ง: ์๋ฌ์จ, ์๋ต ์๊ฐ, ์ ํ๋๋ฅผ ์ค์๊ฐ์ผ๋ก ์ฒดํฌํด
3. ๋กค๋ฐฑ ์ค๋น: ๋ฌธ์ ์๊ธฐ๋ฉด ์ฆ์ ์ด์ ๋ฒ์ ์ผ๋ก ๋์๊ฐ ์ ์๊ฒ ์ค๋นํด
4. ํต๊ณ์ ์ ์์ฑ: ์ถฉ๋ถํ ์ํ ์๋ฅผ ๋ชจ์์ ํ๋จํด (์ต์ 1000๊ฑด ์ด์)
๐ฏ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ฌ๊ธฐ๊น์ง ์ ๋ง ๊ธด ์ฌ์ ์ด์์ด. ๐ TensorFlow์ PyTorch๋ฅผ ํ์ฉํด์ AI ๋ชจ๋ธ์ ์ค์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฐ๋ํ๋ ๋ฐฉ๋ฒ์ ์ ๋ถ ๋ค๋ค๋ดค์ด.
- โ TensorFlow์ PyTorch์ ํน์ง๊ณผ ์ ํ ๊ธฐ์ค
- โ ๊ฐ๋ฐ ํ๊ฒฝ ์ค์ ๊ณผ ์ฒซ ๋ชจ๋ธ ๋ง๋ค๊ธฐ
- โ ์น, ๋ชจ๋ฐ์ผ, ์๋ฒ ํ๊ฒฝ๋ณ ์ฐ๋ ๋ฐฉ๋ฒ
- โ ์ฑ๋ฅ ์ต์ ํ ๊ธฐ๋ฒ (์์ํ, ํ๋ฃจ๋, ์ง์ ์ฆ๋ฅ)
- โ ์ค์ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
- โ ๊ณ ๊ธ ํ์ฉ ์ฌ๋ก (๋ฉํฐ๋ชจ๋ฌ, ์ค์๊ฐ ์ฒ๋ฆฌ, ๋ถ์ฐ ์์คํ )
- โ ๋ชจ๋ํฐ๋ง, ๋ก๊น , ๋ณด์
- โ ๋ฐฐํฌ ์ ๋ต๊ณผ A/B ํ ์คํ
AI ๋ชจ๋ธ ๊ฐ๋ฐ์ ๋์ด ์๋๋ผ ์์์ด์ผ. ์ค์ ๋ก ์ฌ์ฉ์๋ค์ด ์ธ ์ ์๋ ์๋น์ค๋ก ๋ง๋๋ ๊ฒ ์ง์ง ์ค์ํ์ง. ์ฒ์์ ์ด๋ ค์ ๋ณด์ด์ง๋ง, ํ๋์ฉ ์ฐจ๊ทผ์ฐจ๊ทผ ๋ฐ๋ผํ๋ค ๋ณด๋ฉด ์ด๋์ ๋ฉ์ง AI ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค ์ ์์ ๊ฑฐ์ผ! ๐ช
ํน์ AI ๊ฐ๋ฐ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ๋์์ด ํ์ํ๊ฑฐ๋, ์์ ์ AI ๊ฐ๋ฐ ๋ฅ๋ ฅ์ ๋ค๋ฅธ ์ฌ๋๋ค๊ณผ ๊ณต์ ํ๊ณ ์ถ๋ค๋ฉด ์ฌ๋ฅ๋ท์ ํ์ฉํด๋ณด๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ. ์ค์ ๊ฒฝํ์ ์์ผ๋ฉด์ ์์ต๋ ์ฌ๋ฆด ์ ์์ผ๋๊น ์ผ์์ด์กฐ์ง!
์ง๋ฌธ์ด๋ ํผ๋๋ฐฑ์ด ์๋ค๋ฉด ์ธ์ ๋ ํ์์ด์ผ! ํจ๊ป ์ฑ์ฅํ๋ ๊ฐ๋ฐ์๊ฐ ๋์! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.