Keras

영상입력 수치 예측 모델 레시피(컨볼루션 신경망 모델)

이부일 2018. 1. 1. 18:10

# 1. 사용할 패키지 불러오기
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
%matplotlib inline


# 2. 데이터 생성하기
width = 16
height = 16


def generate_dataset(samples):

ds_x = []
ds_y = []

for it in range(samples):
        num_pt = np.random.randint(0, width * height)
        img = generate_image(num_pt)

ds_y.append(num_pt)
ds_x.append(img)

return np.array(ds_x), np.array(ds_y).reshape(samples, 1)


def generate_image(points):

img = np.zeros((width, height))
pts = np.random.random((points, 2))

for ipt in pts:
        img[int(ipt[0] * width), int(ipt[1] * height)] = 1

return img.reshape(width, height, 1)


x_train, y_train = generate_dataset(1500)
x_val, y_val = generate_dataset(300)
x_test, y_test = generate_dataset(100)


# 3. 모델 구성하기
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(width, height, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(1))


# 4. 모델 학습과정 설정하기
model.compile(loss = "mse", optimizer = "adam")


# 5. 모델 학습시키기
hist = model.fit(x_train, y_train, batch_size=32, epochs=1000, validation_data=(x_val, y_val))


# 6. 학습과정 살펴보기
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.ylim(0.0, 300.0)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

자동 대체 텍스트를 사용할 수 없습니다.


# 7. 모델 평가하기
score = model.evaluate(x_test, y_test, batch_size=32)
print(score)

자동 대체 텍스트를 사용할 수 없습니다.


# 8. 모델 사용하기
yhat_test = model.predict(x_test, batch_size=32)

plt_row = 5
plt_col = 5

plt.rcParams["figure.figsize"] = (10,10)

f, axarr = plt.subplots(plt_row, plt_col)


for i in range(plt_row*plt_col):
        sub_plt = axarr[int(i/plt_row), int(i%plt_col)]
        sub_plt.axis('off')
        sub_plt.imshow(x_test[i].reshape(width, height))
        sub_plt.set_title('R %d P %.1f' % (y_test[i][0], yhat_test[i][0]))

plt.show()

이미지: 실내


[출처] 블록과 함께하는 파이썬 딥러닝 케라스, 김태영 지음, DigitalBooks, p218~221