1. Keras 소개
    1. [시작하기] Sequential 모델 가이드
    2. [시작하기] 함수형 API 가이드
    3. [시작하기] FAQ
    4. [모델] Keras의 모델
    5. [모델] 함수형 API
    6. [모델] Sequential
    7. [계층] Keras의 계층
    8. [계층] Core 계층
    9. [계층] 합성곱 계층
    10. [계층] 풀링 계층
    11. [계층] 부분적으로 연결된 계층
    12. [계층] 재발 계층
    13. [계층] 임베딩 계층
    14. [계층] Merge 계층
    15. [계층] 고급 활성화 계층
    16. [계층] 표준화 계층
    17. [계층] 노이즈 계층
    18. [계층] 계층 Wrappers
    19. [계층] 자신만의 Keras 계층 만들기
    20. [전처리] 시퀀스 전처리
    21. [전처리] 텍스트 전처리
    22. [전처리] 이미지 전처리
  2. Metrics
  3. 손실
  4. 최적화기
  5. 활성화
  6. Callbacks
  7. 데이터셋
  8. 애플리케이션
  9. 백엔드 1
  10. 백엔드 2
  11. 백엔드 3
  12. 백엔드 4
  13. 백엔드 5
  14. 초기화기
  15. 정규화기
  16. 제한
  17. 시각화
  18. Scikit-Learn API
  19. 유용한 도구

애플리케이션

Keras의 애플리케이션은 미리 훈련된 가중치를 가진 딥러닝 모델입니다. 이 모델은 예측, feature 추출, 상세 조정(fine-tuning)에 사용할 수 있습니다.

가중치는 모델 인스턴스를 만들면 자동으로 다운로드 되어 ~/.keras/models/에 저장됩니다.

사용가능한 모델

ImageNet에서 훈련된 가중치를 가진 이미지 분류 모델:

모든 아키텍처는(Xception, MobileNet 제외) TensorFlow와 Theano 모두에서 사용 가능하며, 모델 인스턴스를 만들면 ~/.keras/keras.json의 Keras 구성 파일 이미지 데이터 형식 설정에 따라 생성됩니다. 예를 들어 image_data_format=channels_last로 설정했다면, 이 저장소에서 올려지는 모델은 TensorFlow 데이터 형식의 관습인 "Width-Height-Depth"을 따라 만들어질 것입니다.

Xception 모델은 SeparableConvolution 계층에 대한 의존성으로 인해 TensorFlow에서만 사용 가능합니다. MobileNet 모델은 DepthwiseConvolution 계층에 대한 의존성으로 인해 TensorFlow에서만 사용 가능합니다.


이미지 분류 모델에 대한 사용법 예제

ResNet50를 이용한 ImageNet 클래스 분류

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras.utils.data_utils import get_file
import numpy as np
from pprint import pprint

model = ResNet50(weights='imagenet')

img_path = get_file('elephant.jpg',
                    '<http://datasets.lablup.ai/public/tutorials/elephant.jpg>')
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted: ')
pprint(decode_predictions(preds, top=3)[0])
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]

VGG16을 이용한 feature 추출

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.utils.data_utils import get_file
import numpy as np
from pprint import pprint

model = VGG16(weights='imagenet', include_top=False)

img_path = get_file('elephant.jpg',
                    '<http://datasets.lablup.ai/public/tutorials/elephant.jpg>')
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)
pprint(features)

임의의 매개 계층과 VGG19을 이용한 feature 추출

from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
from keras.utils.data_utils import get_file
import numpy as np
from pprint import pprint

base_model = VGG19(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)

img_path = get_file('elephant.jpg',
                    '<http://datasets.lablup.ai/public/tutorials/elephant.jpg>')
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

block4_pool_features = model.predict(x)
pprint(block4_pool_features)

새로운 클래스에 대한 InceptionV3 상세 조정

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# train the model on the new data for a few epochs
model.fit_generator(...)

# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.

# let's visualize layer names and layer indices to see how many layers
# we should freeze:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 249 layers and unfreeze the rest:
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True

# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit_generator(...)

사용자 지정 입력 텐서로 InceptionV3 만들기

1
from keras.applications.inception_v3 import InceptionV3
2
from keras.layers import Input
3

4
# this could also be the output a different Keras model or layer
5
input_tensor = Input(shape=(224, 224, 3))  # this assumes K.image_data_format() == 'channels_last'
6

7
model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)
Python-Tensor

개별 모델에 대한 문서


Xception

keras.applications.xception.Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)

ImageNet에서 가중치가 미리 훈련된 Xception V1 모델입니다.

ImageNet 상에서, 이 모델은 top-1(가장 높은 확률의 예측이 목표 레이블과 일치) 검증 정확도 0.790, top-5(가장 높은 확률의 예측 5개 중 목표 레이블이 있을 때) 검증 정확도 0.945 입니다.

이 모델은 SeparableConvolution 계층에 대한 의존성으로 인해 TensorFlow 백엔드에서만 사용 가능함에 주의하세요. 또한 "channels_last" (height, width, channels) 데이터 형식만 지원합니다.

입력 크기 기본값은 299x299 입니다.

인수

반환값

Keras 모델 인스턴스를 반환합니다.

참고

라이센스

MIT 라이센스 하에 배포되었습니다.


VGG16

keras.applications.vgg16.VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)

ImageNet에서 가중치가 미리 훈련된 VGG16모델입니다.

이 모델은 Theano와 TensorFlow 백엔드 모두에서 사용 가능하며 "channels_first"(channels, height, width), "channels_last"(height, width, channels) 두 데이터 형식 모두를 사용할 수 있습니다.

입력 크기 기본값은 224x224 입니다.

인수

반환값

Keras 모델 인스턴스를 반환합니다.

참고

라이센스

가중치는 released by VGG at Oxford으로부터 Creative Commons Attribution License 하에 포팅되었습니다.


VGG19