카테고리 없음

쥬피터 예제 HTML Download

coding art 2020. 1. 26. 16:29
728x90
In [1]:
import tensorflow as tf
In [2]:
tf.__version__
Out[2]:
'1.15.0'

Jupyter Notebook

1. Features of Jupyter Notebook

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)
WARNING:tensorflow:From C:\Users\Ysc\Anaconda3\envs\tensor_env\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 5s 91us/sample - loss: 0.4715 - acc: 0.8761
Epoch 2/5
60000/60000 [==============================] - 6s 105us/sample - loss: 0.3044 - acc: 0.9149
Epoch 3/5
60000/60000 [==============================] - 5s 83us/sample - loss: 0.2831 - acc: 0.9209
Epoch 4/5
60000/60000 [==============================] - 5s 85us/sample - loss: 0.2735 - acc: 0.9237
Epoch 5/5
60000/60000 [==============================] - 6s 93us/sample - loss: 0.2672 - acc: 0.9249
10000/10000 [==============================] - 0s 41us/sample - loss: 0.2685 - acc: 0.9240
Out[3]:
[0.26849432081729174, 0.924]

3. expression of equation

(x2+y2)

abf(x)dx=F(b)F(a)

f(a)=limxaf(x)f(a)xa

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]:
<function matplotlib.pyplot.show(*args, **kw)>

4.1 image

4.2 video

4.3 table

4.4 chart

5 Wrapup

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 [ ]: