728x90
반응형
728x170
■ 컨볼루션 신경망을 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
import keras.layers as layers
import keras.layers.convolutional as convolutional
import keras.models as models
import keras.preprocessing.image as image
import numpy as np
np.random.seed(3)
print("데이터 로드를 시작합니다.")
trainImageDataGenerator = image.ImageDataGenerator(rescale = 1. / 255,
rotation_range = 10,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.7,
zoom_range = [0.9, 2.2],
horizontal_flip = True,
vertical_flip = True,
fill_mode = "nearest")
trainDirectoryIterator = trainImageDataGenerator.flow_from_directory("handwriting_shape/train", target_size = (24, 24), batch_size = 3, class_mode = "categorical")
testImageDataGenerator = image.ImageDataGenerator(rescale = 1. / 255)
testDirectoryIterator = testImageDataGenerator.flow_from_directory("handwriting_shape/test", target_size = (24, 24), batch_size = 3, class_mode = "categorical")
print("데이터 로드를 종료합니다.")
print("모델 정의를 시작합니다.")
model = models.Sequential()
model.add(convolutional.Conv2D(32, kernel_size = (3, 3), activation = "relu", input_shape = (24, 24, 3)))
model.add(convolutional.Conv2D(64, (3, 3), activation = "relu"))
model.add(convolutional.MaxPooling2D(pool_size = (2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation = "relu"))
model.add(layers.Dense(3, activation = "softmax"))
model.compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"])
print("모델 정의를 종료합니다.")
print("모델 학습을 시작합니다.")
model.fit_generator(trainDirectoryIterator, steps_per_epoch = 15 * 100, epochs = 200, validation_data = testDirectoryIterator, validation_steps = 5)
print("모델 학습을 종료합니다.")
print("모델 평가를 시작합니다.")
evaluationList = model.evaluate_generator(testDirectoryIterator, steps = 5)
print("%s : %.2f%%" % (model.metrics_names[1], evaluationList[1] * 100))
print("모델 평가를 종료합니다.")
print("모델 사용을 시작합니다.")
outputNDArray = model.predict_generator(testDirectoryIterator, steps = 5)
np.set_printoptions(formatter = {"float": lambda x: "{0:0.3f}".format(x)})
classDictionary = testDirectoryIterator.class_indices
print(classDictionary)
print(outputNDArray)
print("모델 사용을 종료합니다.")
728x90
반응형
그리드형(광고전용)
'Python > keras' 카테고리의 다른 글
[PYTHON/KERAS] 컨볼루션 신경망 만들기 (MNIST) (0) | 2018.09.24 |
---|---|
[PYTHON/KERAS] 다층 퍼셉트론 신경망 만들기 (MNIST) (0) | 2018.09.23 |
[PYTHON/KERAS] 순환 신경망 만들기 (나비야) : 상태 유지, 입력 속성 2개 (0) | 2018.08.26 |
[PYTHON/KERAS] 순환 신경망 만들기 (나비야) : 상태 유지, 입력 속성 1개 (0) | 2018.08.26 |
[PYTHON/KERAS] 순환 신경망 만들기 (나비야) (0) | 2018.08.26 |
[PYTHON/KERAS] 컨볼루션 신경망 만들기 (손글씨) (0) | 2018.08.25 |
[PYTHON/KERAS] 다층 퍼셉트론 신경망 만들기 (PIMA) (0) | 2018.08.20 |
[PYTHON/KERAS] Sequential 클래스 : model_from_yaml 메소드를 사용해 모델 구하기 (0) | 2018.08.20 |
[PYTHON/KERAS] Sequential 클래스 : to_yaml 메소드를 사용해 YAML 문자열 구하기 (0) | 2018.08.20 |
[PYTHON/KERAS] Sequential 클래스 : model_from_json 메소드를 사용해 모델 구하기 (0) | 2018.08.20 |
댓글을 달아 주세요