728x90
In [1]:
import tensorflow as tf
In [2]:
tf.__version__
Out[2]:
MNIST 문자 인식 예제를 실행시켜 보자. 이 예제는 tensorflow 버전 1.15.0 나 2.0에서 공통적으로 실행이 가능하다. 구글 tensorflow 홈페이지의 keras 예제는 하나의 은닉층(hidden layer)을 가지는 97.6% 수준의 인식률을 보여 주는 뉴럴 네트워크 예제를 제시하고 있으나 머신 러닝 초보자라면 은닉층이 없는 가장 단순한 예제 코드부터 실행시켜 볼 필요가 있다. 이 단순한 코드에서 최대 92.5% 수준의 인식률이 얻어진다.
2. Code example¶
In [3]:
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
Out[3]:
3. expression of equation¶
4. Visualization¶
In [9]:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('result')
plt.xlabel('variables')
plt.grid(True)
plt.show
Out[9]:
In [10]:
import cv2
import numpy as np
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
# Default resolutions of the frame are obtained.The default resolutions are system dependent.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
while(True):
ret, frame = cap.read()
if ret == True:
# Write the frame into the file 'output.avi'
out.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
# Press Q on keyboard to stop recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and video write objects
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows()
In [ ]: