728x90
레이어 추가 및 이미지 차원 추가 여러가지 방법들
import tensorflow as tf
import numpy as np
from keras.api._v2.keras import activations
from keras.layers import Reshape
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout, MaxPooling2D
from keras.engine.input_layer import InputLayer
tf.keras.datasets.mnist.load_data()
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train / 255.
x_test = x_test / 255.
# 레이어추가 요런 방법도 있다
model = Sequential()
model.add(InputLayer(input_shape=(28,28)))
model.add(Reshape(target_shape=(28,28,1)))
model.add(Conv2D(
filters=12,
kernel_size=(3,3),
activation="relu"
))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(256, activation="relu"))
model.add(Dense(10, activation="softmax"))
# 이미지 차원 추가의 여러가지 방법
# 기본
x_train = x_train.reshape( (x_train.shape[0] , 28, 28, 1) )
x_test = x_test.reshape( (x_test.shape[0] , 28, 28, 1) )
# 기타 다른 방법들. 출력결과 => (60000, 28, 28, 1)
[*x_train.shape, 1]
np.expand_dims(x_train, -1).shape
tf.expand_dims(x_train, -1).shape
x_train[..., tf.newaxis].shape

모델을 경량화하는 방법들
converter (데이터 타입 변경)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
# 모델의 input_type, output_type 출력해주는 함수
def check_in_out_type(model_content):
interpreter = tf.lite.Interpreter(model_content=model_content)
def get_details(func):
return func()[0]["dtype"]
input_type = interpreter.get_input_details()[0]["dtype"]
output_type = interpreter.get_output_details()[0]["dtype"]
print("input_type, output_type: ", input_type, output_type)
# 데이터타입 float32
def representative_data_gen():
for elem in tf.data.Dataset.from_tensor_slices(
x_train.astype(np.float32)
).batch(1).take(100):
yield[elem]
# 데이터타입 uint8 변경용 컨버터
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
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_quant = converter.convert()
# 데이터타입 float16 변경용 컨버터
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.float16]
tflite_model_fp16 = converter.convert()
# 모델 저장하기
import pathlib
tflite_model_dir = pathlib.Path("/tmp/mnist_tflite_models/")
tflite_model_dir.mkdir(exist_ok=True, parents=True)
# Float32 Model
tflite_model_file = tflite_model_dir/"mnist_model.tflite"
tflite_model_file.write_bytes(tflite_model)
# Int8 Model
tflite_model_quant_file = tflite_model_dir/"tflite_model_quant"
tflite_model_quant_file.write_bytes(tflite_model_quant)
# Float16 Model
tflite_model_fp16_file = tflite_model_dir/"tflite_model_fp16"
tflite_model_fp16_file.write_bytes(tflite_model_fp16)
728x90
'#02.천재교육 빅데이터 > +14.딥러닝_심화' 카테고리의 다른 글
| 230524 API Server (딥러닝 모델 만들고 serving) (0) | 2023.05.24 |
|---|---|
| 230522 Keras Sequential -> Functional type (0) | 2023.05.22 |