일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 파이썬
- 커피쏟음
- 당근마켓중고차
- 영통역소고기
- 영통칠프로칠백식당
- 에어아시아
- 종이캐리어
- 푸르지오포레피스
- 사진에서 글자추출
- 커피
- 싱가폴중학교수학문제
- 고마워다음
- 가족소고기외식
- 주차넉넉
- 영통외식
- 사진문자추출
- 편도수술
- 양양솔비치 뷔페
- 양양솔비치조식
- 검색완료
- 홍시스무디
- 사진문자추출하기
- 결항
- 오트눈썰매장
- DFS
- 아이혼자다녀옴
- 중학교입학수학문제
- 결항전문
- 양양솔비치아침
- 양양솔비치세프스키친
- Today
- Total
너와나의 관심사
Keras model 로딩해서 layer 변경하기 본문
on device learning for android
안드로이드에서 on device model 을 base (head)모델과 fully connected layer 로 나누기 위해
Keras SavedModel 로딩해서 뒤에 몇개 layer 를 잘라내었다.
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import tensorflow as tf
import tensorflow.python.keras.models
import tensorflow_hub as hub
def create_model():
# 모델 define
model = tf.keras.models.Sequential([
# The first convolution
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(300, 300, 3)),
tf.keras.layers.MaxPool2D(2, 2),
# The second convolution
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPool2D(2, 2),
# The third convolution
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPool2D(2, 2),
# The fourth convolution
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPool2D(2, 2),
# The fifth convolution
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPool2D(2, 2),
# Flatten
tf.keras.layers.Flatten(),
# 512 Neuron (Hidden layer)
tf.keras.layers.Dense(512, activation='relu'),
# 1 Output neuron
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model;
loaded = tf.keras.models.load_model("./training_2")
loaded.summary()
loadedModel = tf.keras.models.load_model("./training_2")
base_input = loadedModel.input
base_output = loadedModel.layers[8].output
newModel = tf.keras.models.Model(inputs= base_input, outputs = base_output)
print(newModel.summary())
print("Done")
원본 모델
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 298, 298, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 149, 149, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 147, 147, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 73, 73, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 71, 71, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 35, 35, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 33, 33, 64) 36928
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 14, 14, 64) 36928
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 7, 7, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 3136) 0
_________________________________________________________________
dense (Dense) (None, 512) 1606144
_________________________________________________________________
dense_1 (Dense) (None, 1) 513
=================================================================
Total params: 1,704,097
Trainable params: 1,704,097
Non-trainable params: 0
____________________________________
수정한 모델
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_input (InputLayer) [(None, 300, 300, 3)] 0
_________________________________________________________________
conv2d (Conv2D) (None, 298, 298, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 149, 149, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 147, 147, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 73, 73, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 71, 71, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 35, 35, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 33, 33, 64) 36928
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 14, 14, 64) 36928
=================================================================
'머신러닝 > 딥러닝' 카테고리의 다른 글
Keras saved_model.h5 모델 로딩하기 (0) | 2023.01.31 |
---|---|
안드로이드 on device learning (0) | 2022.02.13 |
Keras 로 학습된 모델로 객체생성(compile) 하고 싶다면 (0) | 2022.02.06 |
tensorflow keras 로 모델 training save (ModelSave 활용) (0) | 2022.02.06 |