Professional Documents
Culture Documents
Machine Learning V I Keras (TensorFlow), FastAI (PyTorch) Trên Colab
Machine Learning V I Keras (TensorFlow), FastAI (PyTorch) Trên Colab
Mở đầu ................................................................................................................................................... 2
Vài cái mà mình sẽ làm trong bài này ....................................................................................................... 2
Lý thuyết ................................................................................................................................................. 4
Neural network ................................................................................................................................... 6
Machine learning ............................................................................................................................... 11
Để đoán một con số........................................................................................................................... 12
Từ layer này qua lấy kia được tính như thế nào? ............................................................................ 13
AI hardware (không phải chỉ có GPU) ................................................................................................. 20
Từ điển machine learning .................................................................................................................. 21
Hãy xem thêm 4 cái video này để hiểu sâu về CNN ............................................................................ 23
Tìm nhạc............................................................................................................................................ 25
Toán học (make it simple, xài vector) ................................................................................................. 25
Có nhiều cách để làm model, vậy em nên xài framework nào, tensorflow, pytorch, keras, darknet ........ 27
Thôi dọc đi anh ...................................................................................................................................... 29
Update nvidia graphic driver .............................................................................................................. 29
Install python và fastai ....................................................................................................................... 30
Test CUDA trong pytorch:............................................................................................................. 32
Thử làm Hello World với tensorflow .................................................................................................. 34
Dùng GPU của Google Colab .................................................................................................................. 34
Dùng Keras (Tensorflow) với darknet – làm Classification Model trong vòng 10 Minutes! ...................... 36
Let go ................................................................................................................................................ 38
Dùng fastAI (pytorch) với resnet34 để tìm xúc xích ................................................................................ 41
Let go ................................................................................................................................................ 42
Conclusion............................................................................................................................................. 45
Troubleshooting .................................................................................................................................... 45
BONUS: Đi tìm một mớ hình trên net dùng chrome ............................................................................... 47
BONUS: xài jupyter trên laptop ............................................................................................................. 49
Reference .............................................................................................................................................. 51
Mở đầu
Bài này anh hy vọng sẽ giúp các em làm đòn bẩy trên sự nghiệp CN4 với Big Data cho các em, anh cố viết
làm sao cho dễ hiểu, thực tế (nhiều thí dụ và hình) và không trừu tưởng. Căn bản là quan trọng, nhớ đọc
nhiều lần nếu có chỗ nào khó hiểu. Kẹt quá chạy bộ 1 vòng ngoài ven sông, sau đó đọc lại em sẽ thấy rõ
hơn. Một cách khác giúp cho dễ hiểu là “colab, colab, google” chạy code, dọc code vài ngày, sau đó lý
thuyết sẽ rõ hơn.
Khi đọc nhớ xài “bảng nội dung” để tìm cái gì em cần hiểu sâu thêm.
AI (Articial intelligence; trí tuệ nhân tạo) đã được đề cập tới từ 1950.
Machine learning là một ứng dụng của trí tuệ nhân tạo (AI) cung cấp cho các hệ thống khả năng tự động
học hỏi và cải thiện từ kinh nghiệm mà không cần lập trình rõ ràng.
Deep learning nói một cách đơn giản là dùng neural networks với nhiều neurons, layer, được kết nối
chung lại để ra một kết quả/output (mình dùng nó để quyết định một công chuyện kế tiếp).
Bài viết này viết về neural network CNN (convolutional neural network: kết nối một network chung với
nhiều bài toán) để dùng cho computer vision (nhận dạng).
Khi đọc xong, kiến thức em được là: install python và các thư viện, dùng jupyter trên PC hay là Google
Colab, hiểu về Nvidia CUDA (GPU), cách tìm dữ liệu nhanh với chrome và java, dùng CNN để train data
bằng cách sử dụng software Keras (nó dùng TensorFlow), FastAI (nó dùng PyTorch)
Trong lãnh vật “computer vision”, mục tiêu là nhận dạng (gọi là “Image Classification”) trong 1 tấm hình,
đó là con sư tử phải không?. Để máy làm được điều đó khoa học đưa bộ não ta áp dụng vào bộ máy, và
gọi là “neural network” (giống như hàng tỉ cái neurons trong não con người).
Mục tiêu thứ 2 của computer vision là “Object detection”, tìm ra có những vật gì trong một tấm hình.
Image classification: cái này là xúc xích phải không? (yes/no hay là thuộc category/class nào)
Mình bắt đầu cho máy học những tấm hình, dạy máy “con sư tử nhìn như thế này”, cho nó 500 tấm hình
con sư tử. Dạy nó tìm cạnh (ranh giới giữa trắng và đen), tìm góc, tìm những mẫu hình (pattern; như con
mắt, cái đuôi, 4 chân, cái mũi…). Những kinh nghiệm từ 500 tấm hình con sư tử này là cái nền tảng cho
“machine learning”.
Filter:
Filter (đồ lộc): Dùng filter để detect mẫu (pattern)
500 tấm hình đó gọi là training data. Cái quá trình mình dạy máy gọi là mình xây dựng một cái model
(đọc tại đây). Trong machine learning người ta nói rằng phuong pháp mà mình tìm tới là làm xao
“minimize error”, thử đi thử lại khi nào error gần zero, hãy nghe video của Luis Serrano. Trong 2 cái bài
thực tập anh có đề cập tới cost (hay là lost), mình cho máy training đến khi cost gần bằng zero (giống
như ngoài đời chi tiêu (cost) càng thấp càng tốt).
Software như keras/tensorflow, fastai/pytorch giúp ta làm cái model đó. Khi SW làm model nó dùng
nhung cái filter như bên trên.
Trong fastAI/pytorch learning data (models) được cất với command “learner.save('KN_cho')”, nằm dưới
data/models. Với keras/tensorflow thì dùng tf.train.Saver(), xem
https://www.youtube.com/watch?v=HxtBIwfy0kM
https://www.tensorflow.org/tutorials/keras/save_and_load
TensorFlow và PyTorch cung cấp các tóm tắt hữu ích để giảm số lượng mã soạn sẵn và tăng tốc độ phát
triển mô hình (TensorFlow and PyTorch provide useful abstractions to reduce amounts of boilerplate
code and speed up model development)
Neural network
Chữ mà em hay nghe trong lãnh vật AI “neural network”, nghĩa là gì? Neuron (nerve) giống như trong bộ
não chúng ta. Khoa học muốn dựa vào nó và đưa vào máy.
Hình bên trên cho thấy, output của neuron bên tay trái đi vào neuron bên tay phải. Bộ óc ta có cả 100 tỉ
neurons.
Cái này là CNN: Convolutional neural networks (dịch là “nhiều lớp kết hợp/nối nhau để mang tới một kết
quả”. Mỗi lớp có nhiều neuron)
Mình sẽ xài CNN trong bài này.
Trong machine learning, 1 neuron là 1 phương trình có input rồi output. Khi nhiều neuron nối lại nhau
lập thành một “neural network” (hay là “deep neural network”)
https://cv-tricks.com/cnn/understand-resnet-alexnet-vgg-inception/
https://towardsdatascience.com/illustrated-10-cnn-architectures-95d78ace614d
Đây là những CNN-architecture như AlexNet, VGG, Inception, ResNet, Inception, GoogleNet, Mobilnet,
darknet…mỗi có những đặc tính khác nhau, có cái thì nhiều Conv layers, ít FC layers (xem hình bên trên).
Nhiều layers cũng có thể định nghĩa cho sự chính xác nhưng chậm (phài dùng nhiều GPU để giải bài
toán).
Nb: trong 2 thí dụ trong bài này dùng “noname” net (model tự quyết định Conv và FC layer) và resnet.
AlexNet:
Inception-v4 chính xác nhất, VGG-19 tuy cần nhiều resource (chậm) nhưng lại không bằng Ineception-v4.
Mobilenet (dành cho mobile và embedded box) speed nhanh, fps lên cả 25. (fps càng lớn càng tốt)
Regression được gọi là “ước tính”, khi mình đã có vài kết quả trước mình có thể “ước tính” cái kết quả
mới. Thí dụ từ Hanoi tới Hải Phòng 120km mất 1t50p (110p), mình ước tính tới Vịnh Hạ Long (nhớ địa lý,
biết nó nằm ra sao) khoảng 180km, sẽ mất khoảng 170p (2t50p).
Ref: https://www.youtube.com/watch?v=YlGEQyEM_a8
Cái thí nghiệm trong bài này sẽ là “classification” (supervised) machine learning, vì mình sẽ dùng những
data/learning có trước. Cũng gọi là “transfer learning”.
The machine learning world: https://vas3k.com/blog/machine_learning/
Ref:
https://www.analyticsvidhya.com/blog/2018/12/guide-convolutional-neural-network-cnn/
https://www.youtube.com/watch?v=aircAruvnKk&feature=emb_logo (Good Blue1Brown)
Từ layer này qua lấy kia được tính như thế nào?
Để ý những con số trong cái filter dưới đây, (-1 -1 -1) nghĩa là đen, (1 1 1) là trắng.
Bài toán là tính con số mới khi 1 cái filter 3x3 đi ngang qua một cái 6x6 image.
Convolve
(dời, trượt,
slide) 6x6
tấm hình với
3x3 filter. Cái
filter dời
từng pixel
một, đầu
tiên là khu
màu đỏ, sau
vàng, xanh lá Em xem filter trượt đây: http://www.chezmoi.dk/upload/conv-full-layer.gif
cây…hàng
ngang
xong…xuống
hàng tới,
màu xanh…
Để ý số trong
cái filter, (-1
-1 -1) nghĩa
là đen đấy,
vậy cái filter
đó dùng tìm
cạnh dọc
(vertical
edges)
Khu màu đỏ
sau khi cộng
với filter 3x3.
3*1 + 0*0 +
1*(-1) + 1*1
+ 5*0 + 8*(-
1) + 2*1 +
7*0 + 2*(-1)
= -5
Em thử làm
khu vàng, kết
quả phải là -
4
Khu xanh
blue là:
1*1 + 5*0 +
8*(-1) + 2*1
+ 7*0 + 2*(-
1) + 0*1 +
1*0 + 3*(-1)
= -10
Sau khi
chuyển
(convolve)
cai 3x3 filter
qua 6x6
image , kết
quả là.
Công thức tìm output matrix khi xài padding:
Output: (n+2p-f+1) X (n+2p-f+1)
Hay quá từ 1
hình lớn
Công thức tìm output matrix khi xài padding và stride:
matrix 6x6,
Output: [(n+2p-f)/s+1] X [(n+2p-f)/s+1]
bây giờ chỉ
còn matrix
4x4, mình
convolve vài
lần cái
matrix sẽ
nhỏ đi, khi
đó những bài
tính sẽ nhẹ
hơn
Yếu điểm là
vài chi tiết
tấm hình sẽ
mất, mình
dùng
padding để
matrix vẫn là
6x6, rồi dùng
stride để làm
nhỏ nó lại
Khi có 3 màu
RxGxB (3
color
channels),
khi cộng sộ
mình cộng cả
3 channel
một lúc.
Output là
4x4
Đây dùng 2
filter 3x3x3
cùng một lúc
A typical
convolution
al network.
Input
convolve qua
nhiều layer ,
matrix nhỏ
đi. Chú ý độ
sâu lớn ra.
Để máy tính
nhanh hơn,
giảm độ lớn
của matrix,
dùng max
pooling Cho pooling vào, với filter =2, stride =2, đi từ 8x8 -> 2x2 matrix. Lấy số lớn nhất (max)
của mỗi khu, khu màu đỏ là số 2, khu tím là 6.
CNN
example
Một cách dễ
hiểu cho các
layer trong
CNN
CNN layer
Softmax
đang tính
prediction,
xem
document
trong
Reference
NB: A matrix is simply a rectangular array of numbers and a vector is a row (or column) of a matrix
NB2: The fully connected (FC) layer in the CNN represents the feature vector for the input. ... The
convolution layers before the FC layer(s) hold information regarding local features in the input image
such as edges, blobs, shapes, etc. Each conv layer hold several filters that represent one of the local
features.
Ref: https://www.analyticsvidhya.com/blog/2018/12/guide-convolutional-neural-network-cnn/
Training
https://www.youtube.com/watch?v=aircAruvnKk&feature=emb_logo
Trong video này có 13.000 thông số mình có thể chỉnh, nếu chỉnh đúng output sẽ ra con số mình mong
đợi. Vi thế họ có 2 cái hidden layers, đằng sau nó là những kinh nghiệm mà những nhà nghiên cứu chia
sẻ.
Nvidia Jetson Nano vs. Google Coral Dev board, Detailed Comparison
https://www.youtube.com/watch?v=RgTXqP8yZ_Y
https://medium.com/sciforce/ai-hardware-and-the-battle-for-more-computational-power-
3272045160a6
https://www.forbes.com/sites/moorinsights/2019/10/07/ai-hardware-harder-than-it-
looks/#7912df60471f
Từ điển machine learning
2 layer:
Learning: finding the right weights and biases to solve the problem.
How CNN (Convolutional https://www.youtube.com/watch?v=oI2rvjbzVmI
Neural Networks - Deep
Learning) algorithm works
Thales Sehn Körting
Tại sao không dọc cả 2 thế giới đó để sau này em kiếm cơm rất là dễ dàng hơn.
March 2019: I am new to deep learning too, but I find Keras the
Keras vs Pytorch vs fastai? most straight forward.
https://www.kaggle.com/c/jigsaw-unintended- If you are not going to modify the NN much, you
bias-in-toxicity-classification/discussion/88604 should stick to Keras.
I really dont understand why you people are saying
that Fastai is faster than Pytorch…. as Fastai is an Fastai is great, but requires some time to learn
API that sits in top of Pytorch, they are not what's going on under that one line of code.
separate…. it cant never be faster as Fastai is Torch is the most flexible and easiest to modify.
Pytorch…
In the other hand, keras has many problems and is FastAi is a research lab with the mission of making
an API that sits in top of Tensorflow or other AI accessible by providing an easy to use library
libraries….. build on top of PyTorch, as well as exceptionally
good tutorials/courses like the Practical Deep
Learning for Coders course which I am currently
enrolled in.
Tensorflow is based on Theano and has been TensorFlow is arguably one of the best deep
developed by Google, whereas PyTorch is based learning frameworks and has been adopted by
on Torch and has been developed by Facebook. several giants such as Airbus, Twitter, IBM, and
Point #2: The most important difference between others mainly due to its highly flexible system
the two is the way these frameworks define the architecture
computational graphs. ... But in PyTorch, you can
define/manipulate your graph on-the-go
Is TensorFlow faster than PyTorch? The wrapper of tensorflow is designed for
TensorFlow, PyTorch, and MXNet are the most engineers not for researchers. ... Considering the
widely used three frameworks with GPU support. large community of Tensorflow, it is impossible for
... For example, TensorFlow training speed is 49% Pytorch to replace tensorflow, but I believe
faster than MXNet in VGG16 training, PyTorch is Pytorch will largely be used by some top
24% faster than MXNet universities.
https://thegradient.pub/state-of-ml-frameworks-
2019-pytorch-dominates-research-tensorflow-
dominates-industry/
Note that PyTorch saw a larger number of So, both TensorFlow and PyTorch provide useful
additional listings than TensorFlow on all job abstractions to reduce amounts of boilerplate
search sites other than LinkedIn. code and speed up model development. The main
difference between them is that PyTorch may feel
more “pythonic” and has an object-oriented
approach while TensorFlow has several options
from which you may choose.
Tensorflow is the most famous library used in Keras has a simple architecture. It is more readable
production for deep learning models. ... However and concise . Tensorflow on the other hand is not
TensorFlow is not that easy to use. On the other very easy to use even though it provides Keras as a
hand, Keras is a high level API built on TensorFlow framework that makes work easier. PyTorch has a
(and can be used on top of Theano too). It is more complex architecture and the readability is less
user-friendly and easy to use as compared to TF. when compared to Keras
TensorFlow provides excellent functionalities and Darkflow: It is a nickname of an implementation by
services when compared to other popular deep Trinh Hoang Trieu of YOLO on TensorFlow.
learning frameworks. These high-level operations
are essential for carrying out complex parallel
computations and for building advanced neural
network models. TensorFlow is a low-level library
which provides more flexibility
Tips to speed up Keras LSTM time per epoch? Deep learning is a subset of machine learning in
artificial intelligence (AI) that has networks
Buy a GTX NVIDIA 1080 video card. capable of learning unsupervised from data that is
Buy 32 GB of 3300 Mhz Ram. unstructured or unlabeled. Also known as deep
Install all the Cuda stuff correctly (like adding it neural learning or deep neural network.
to the system path)
Use keras.layers.CuDNNLSTM instead of
keras.layers.LSTM.
It (darknet) was made with different use case in
mind, that's it. Tensorflow and Pytorch are general
tensorflow algebra libraries with Python
interfaces, general enough to implement YOLO
and many more architectures. Darknet (C
language) was made to build YOLO and to run it
fast.
Thôi dọc đi anh
Ok, mình sẽ dọc 2 bài, một bài dùng fastai/pytorch, bài thứ hai dùng keras/tensorflow
Mình phải chuẩn bị cái laptop/PC mình trước chứ. PyTorch/TensorFlow dùng Nvidia CUDA cho nhanh.
Anh check cái driver trước.
NB: ngày nay khi cần giải quyết một bài toán lớn (big data: khoa học, kinh tế, khí hậu, tình báo…), họ
dùng GPU (nvidia CUDA).
https://www.omnisci.com/learn/resources/technical-glossary/CPU-to-GPU
d:\Download\418.81-quadro-desktop-notebook-win10-64bit-international-whql.exe 416.119.352
16/11/2019 21.36 -a—
https://www.nvidia.com/Download/driverResults.aspx/142448/en-us
C:\Program Files\NVIDIA Corporation\NVSMI>nvidia-smi.exe -q
==============NVSMI LOG==============
Timestamp : Sun Nov 17 14:14:30 2019
Driver Version : 418.81
CUDA Version : 10.1
Attached GPUs : 1
GPU 00000000:01:00.0
Product Name : Quadro K1100M
Product Brand : Quadro
NB: cách dễ nhất là xài “Anaconda Distribution”, nếu em không có kinh nghiệm với python. Nó xài
“conda” thay vì “pip”, em xem tại đây https://jupyter.org/install
C:\Users\tuan>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
Reboot
3) Install torch (“pip install torch” có lỗi ‘No module named 'tools.nnwrap', dùng phương pháp
khác)
Nb: fastai dùng pytorch, bởi nó là pytorch on-top ☺
C:\Users\tuan>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> device = torch.device('cuda')
>>> x = torch.tensor([1., 2.], device=device)
D:\Program Files\Python37\lib\site-packages\torch\cuda\__init__.py:118: UserWarning:
Found GPU0 Quadro K1100M which is of cuda capability 3.0.
PyTorch no longer supports this GPU because it is too old.
The minimum cuda capability that we support is 3.5.
OPS, cái Nvidia của anh cũ quá, không xài pytorch được!! tí nữa khi chạy jupyter sẽ gặp vấn đề
NB: mình có thể xài CPU, thôi nó chậm lắm.
OPS, cũng không được, mình có thể xài CPU, thôi nó chậm lắm. Chắc đi mua máy mới.
4) Install torchvision
C:\>pip install https://download.pytorch.org/whl/cu90/torchvision-0.3.0-cp37-cp37m-
win_amd64.whl
Collecting torchvision==0.3.0
Successfully installed pillow-6.2.1 torchvision-0.3.0
Nb: nếu muốn hiểu thêm về fastai setup, đọc README.md từ fastai github.
d:\Download\fastai-master.zip\fastai-master\README.md
6) Install jupyterlab
C:\Users\tuan>pip install jupyterlab
Collecting jupyterlab
Downloading
https://files.pythonhosted.org/packages/eb/94/cd9e53b9d3e4c35202dec30e1b623c355bd9de2b0c53e1bc7f4af31d25d8
/jupyterlab-1.2.3-py2.py3-none-any.whl (6.4MB)
|████████████████████████████████| 6.4MB 467kB/s
Collecting notebook>=4.3.1
….
Successfully installed MarkupSafe-1.1.1 Send2Trash-1.5.0 attrs-19.3.0 backcall-0.1.0 bleach-3.1.0
colorama-0.4.1 decorator-4.4.1 defusedxml-0.6.0 entrypoints-0.3 ipykernel-5.1.3 ipython-7.9.0 ipython-
genutils-0.2.0 jedi-0.15.1 jinja2-2.10.3 json5-0.8.5 jsonschema-3.1.1 jupyter-client-5.3.4 jupyter-core-
4.6.1 jupyterlab-1.2.3 jupyterlab-server-1.0.6 mistune-0.8.4 nbconvert-5.6.1 nbformat-4.4.0 notebook-6.0.2
pandocfilters-1.4.2 parso-0.5.1 pickleshare-0.7.5 prometheus-client-0.7.1 prompt-toolkit-2.0.10 pygments-
2.4.2 pyrsistent-0.15.5 pywin32-227 pywinpty-0.5.5 pyzmq-18.1.1 terminado-0.8.3 testpath-0.4.4 tornado-
6.0.3 traitlets-4.3.3 wcwidth-0.1.7 webencodings-0.5.1
C:\Users\tuan>
Thử làm Hello World với tensorflow
hello.ipynb
from __future__ import print_function
import tensorflow as tf
# Simple hello world using TensorFlow
# Create a Constant op
# The op is added as a node to the default graph.
# The value returned by the constructor represents the output of the Constant op.
hello = tf.constant('Hello, TensorFlow!')
# Start tf session
sess = tf.Session()
# Run the op
print(sess.run(hello))
C:\Users\tuan\Downloads>python hello.ipynb
2019-11-17 18:46:01.560243: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load
dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
2019-11-17 18:46:11.534997: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully
opened dynamic library nvcuda.dll
2019-11-17 18:46:12.944821: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with
properties:
name: Quadro K1100M major: 3 minor: 0 memoryClockRate(GHz): 0.7055
pciBusID: 0000:01:00.0
2019-11-17 18:46:12.974344: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU
libraries are statically linked, skip dlopen check.
2019-11-17 18:46:13.004356: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1700] Ignoring visible gpu
device (device: 0, name: Quadro K1100M, pci bus id: 0000:01:00.0, compute capability: 3.0) with Cuda
compute capability 3.0. The minimum required Cuda capability is 3.5.
2019-11-17 18:46:13.052395: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports
instructions that this TensorFlow binary was not compiled to use: AVX2
2019-11-17 18:46:13.087823: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect
StreamExecutor with strength 1 edge matrix:
2019-11-17 18:46:13.114753: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]
Traceback (most recent call last):
File "hello.ipynb", line 22, in <module>
sess = tf.Session()
AttributeError: module 'tensorflow' has no attribute 'Session'
Ops!! Tiếc quá cái nvidia Quadro K1100M của anh cũ quá, “hello world” chạy không được. Thôi tắc
đèn đi ngủ đây, sáng mai qua Vintech ☺
Colab rất dễ xài, nó giống kaggle editor (đọc bài trên chezmoi “Data analysis (Big Data)” )
Login với gmail account, giống như với Kaggle, Google cho em mượn máy của họ hoàn toàn free.
https://colab.research.google.com/drive/
As of October 13, 2018, Google Colab provides a single 12GB NVIDIA Tesla K80 GPU that can be used up
to 12 hours continuously. Recently, Colab also started offering free TPU.
NB: colab là virtual machine, khi em logout và login lại những cái gì ngày hôm qua sẽ không còn nữa. Khi
save code Colab cất ipynb em làm ở “Google drive”. Nếu có gì quan trong trên Colab, download nó
xuống hay là copy nó qua “Google drive”.
Với Colab, em có thể dùng “!pip install XXX” cái package nào mà em cần, nó có sẵn fastai cho mình xài,
Nếu thieu package nào, cái này hiện ra, bấm “OPEN EXAMPLES”
Anh cần path.py, anh viết “!pip install path.py”, click run (cái icon ngay bên trái).
Tất cả những gì em cần nẳm đây trong zip file. Nếu anh cần update gì thì anh update cái zip luôn.
http://www.chezmoi.dk/image-classification-CNN-using-keras(tensorflow).zip
(98MB)
Unpack và lấy ra Image_Classification_Model_in_10min.ipynb và 2 cái zip file. Upload 2 cái zip lên
Google drive
Trong ipynb file anh viết nhiều comment, trong bài này em sẽ cho máy nhận diện ra các loại áo quần
thuộc category nào. Có 10 category/class, mình cho máy luyện 60.000 28x28pixels hình. Keras dùng 80%
của 60.000 tấm hình để train, 20% còn lại để validate (để test cái training của nó). Trong ipynb script anh
có viết nhiều về vụ này.
Sau khi luyện xong mình dùng test zip với 10.000 hình để test nó. Kết quả sẽ nằm trong Colab folder
/content/sample_cnn.csv . Right click để download về laptop.
Label Description
0 T-shirt/top
1 Trouser
2 Pullover
3 Dress
4 Coat
5 Sandal
6 Shirt
7 Sneaker
8 Bag
9 Ankle boot
Let go
Trước khi đề ba em hãy gài cái addon (plugin) này trong Firefox để xem cái anh đã làm, ipynb có output,
hình đầy đủ, Image_Classification_Model_in_10min.ipynb
Đây là cai Firefox plugins để em xem ipynb dễ dàng trực tiếp trên Firefox
Python Notebook Viewer: https://addons.mozilla.org/en-US/firefox/addon/python-notebook-viewer/
login with gmail account https://colab.research.google.com/ -> new python 3 Notebook -> open
notebook -> click upload -> chọn Image_Classification_Model_in_10min.ipynb
Mỗi hàng tên gọi là 1 cell, em có thể chạy từng cell một, không phải chạy từ đầu mỗi lần. Ngay bên dưới
cái cell là Output
Click “Runtime” em sẽ thấy nhiều cách chạy. Khi muốn cho thêm 1 cell, click cai icon “+ Code” bên trên
tay trái.
Em thử đi, nó nhanh lắm, chỉ tốn 4 phút bởi vì mình xài Google GPU và files chỉ là 28x28 pixels.
Google những options của keras/tensorflow command, chỉnh nó tới chỉnh nó lui vài ngày để lấy kinh
nghiệm. Em xem đây nhà nghiên cứu người ta test ra sao http://yann.lecun.com/exdb/mnist/ , thấy tỉ
mỉ không?
GOOD LUCK em
Choripan là 1 loại xúc xích ở Nam Mỹ (Chile, Argentina). Mình có 2 category/class, “hot-dog” và “not-
hot-dog”. Trong folder “train”, mỗi category có 399 1024x1024 jpg. FastAI dùng nó để học. Nó vừa học
nó vừa dùng 50 cái 1024x1024 jpg trong folder “valid” (validation) để kiểm tra cái “prediction” (dự đoán)
của nó.
Khi xong hết mình upload vài tấm hình lên để test nó.
Code xài pre-trained model từ resnet34, cùng với resnet và training testset tạo ra cái model cho bài này.
Em download zip file và unpack fastai-choripan.ipynb, choripan-not-choripan.zip (training file)
và 3 cái test images. Em chỉ cần upload images lên Google drive, ipynb script sẽ download trực
tiếp choripan-not-choripan.zip từ chezmoi.
Chú ý anh ta dùng pre-trained data, nên khi anh ta cho model chạy, có vài layer bị khóa lại (freeze), đọc
them về pre-train tại đây.
Let go
Trước khi đề ba em hãy gài cái addon (plugin) này trong Firefox để xem cái anh đã làm, ipynb có output,
hình đầy đủ, fastai-choripan.ipynb
Mỗi hàng tên gọi là 1 cell, em có thể chạy từng cell một, không phải chạy từ đầu mỗi lần. Ngay bên dưới
cái cell là Output
Click “Runtime” em sẽ thấy nhiều cách chạy. Khi muốn cho thêm 1 cell, click cai icon “+ Code” bên trên
tay trái.
Bên tay trái em sẽ thấy Colab directoy và “Google drive”
Anh dùng chả giò để test nó ☺ yes, nó đoán đúng, chả gió không thuộc category “hot-dog” mà là “not-
hot-dog”.
Anh đã viết những comment trong code, hãy đọc kỹ. Chú ý cái Accuracy (chính sát) và loss (hay gọi là
cost, càng rẻ cang tốt; gần zero).
Em thử đi, nó chạy khoảng 11 phút bởi mình xài Google GPU.
Cobol command:
#copy từ google drive (nhớ mount nó trước)
!cp "/content/drive/My Drive/cha-gio.jpg" /content/data
#xóa folder
!rm -rf data/choripan-not-choripan/test
Google những options của fastai/pytorch command, chỉnh nó tới chỉnh nó lui vài ngày để lấy kinh
nghiệm. Em xem đây nhà nghiên cứu người ta test ra sao http://yann.lecun.com/exdb/mnist/ , thấy tỉ
mỉ không?
GOOD LUCK em
Conclusion
Con đường lên AI còn dài và gian khổ, đây chỉ là 1 ít căn bản anh chia sẻ, hy vọng nó cho các em một
nghị lực vô cùng lớn để rồi tiếp tục sự nghiệp của mình trong tương lai. 30-40 năm sau em ngồi nghĩ lại
và hãnh diện, wow không ngờ mình làm được thế cho nhân loại, cái đó mới gọi là một di sản cho
Vietnam.
Anh người Ấn Độ này Pulkit Sharma có rất nhiều bài hay (cụ thể, có thí dụ đúng, không có nói trên trời
dưới đất) tụi em có thể học và nghiên cứu thêm ở đây
https://www.analyticsvidhya.com/blog/author/pulkits/
20 tỉ đồng dơ không bằng 1 đồng kiến thức trong sạch các em kiếm được.
Troubleshooting
1)
pip search tensor
pip install tensorflow
pip install --upgrade tensorflow-gpu
pip freeze (list all the libraries)
C:\Users\tuan>pip freeze
..
Bottleneck==1.3.0
cudatools==0.0.1
fastai==1.0.59
fastprogress==0.1.21
google-auth==1.7.1
google-auth-oauthlib==0.4.1
google-pasta==0.1.8
ipython==7.9.0
ipython-genutils==0.2.0
json5==0.8.5
jsonschema==3.1.1
jupyter-client==5.3.4
jupyter-core==4.6.1
jupyterlab==1.2.3
jupyterlab-server==1.0.6
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
matplotlib==3.1.0
notebook==6.0.2
numexpr==2.7.0
numpy==1.16.4
nvidia-ml-py3==7.352.0
oauthlib==3.1.0
opencv-python==4.1.0.25
pandas==0.24.2
pandocfilters==1.4.2
path.py==12.0.2
python-dateutil==2.8.0
rsa==4.0
scikit-learn==0.21.2
tensorboard==2.0.1
tensorflow==2.0.0
tensorflow-estimator==2.0.1
tensorflow-gpu==2.0.0
torch==1.1.0
torchvision==0.3.0
2) Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
See https://stackoverflow.com/questions/47068709/your-cpu-supports-instructions-that-this-
tensorflow-binary-was-not-compiled-to-u
Cái này OK
import os, zipfile
filename = 'choripan-not-choripan.zip' # Name of dataset
base_url = 'http://www.chezmoi.dk/'
url = os.path.join(base_url, filename)
output_file = os.path.join(os.getcwd(),'data', filename)
!wget -nc $url -P 'data'
with zipfile.ZipFile(output_file,"r") as zip_ref:
zip_ref.extractall('data')
!rm -R $output_file
5) Cái này nghĩa là google drive timeout, chạy lại từ đầu từ cell 1
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'
During handling of the above exception, another exception occurred:
InvalidConfigError: Invalid client secrets file ('Error opening file',
'client_secrets.json', 'No such file or directory', 2)
6) zz
BONUS: Đi tìm một mớ hình trên net dùng chrome
Khi em cần tài lieu mới em có thể tìm trên Google, image-net.org, pinterest ….Dưới đây là cách tìm hình
với chrome.
1) Dùng Chrome đi tìm hình “people face”, dùng console (ấn Ctrl-Shift-J ) để cất URL (địa chỉ của
từng hình) rồi sau đó download nó về máy PC.
Khi search “people face”, kéo chuột xuống dưới đến khi nào hết hình mới ấn Ctrl-Shift-J
https://www.thestatesman.com/wp-content/uploads/2017/08/1493458748-beauty-face-517.jpg
https://icdn5.digitaltrends.com/image/screen-shot-2019-02-15-at-19-16-58-720x720.jpg
https://images.fastcompany.net/image/upload/w_596,c_limit,q_auto:best,f_auto/wp-cms/uploads/2019/02/5-
create-fake-people-in-2-seconds-on-this-insane-site.jpg
https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-
1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80
https://dw8stlw9qt0iz.cloudfront.net/KlcQ2vgQfUBQydZYPsioopw6N6A=/fit-
in/800x450/filters:format(jpeg):quality(75)/curiosity-
data.s3.amazonaws.com/images/content/landscape/standard/386af779-139a-4e37-c86e-9a21328dbfad.png
http://facelab.org/People/images/smile/wincenciak.jpg
https://images.theconversation.com/files/129697/original/image-20160707-30713-18qtf3f.jpg?ixlib=rb-
1.1.0&q=45&auto=format&w=926&fit=clip
https://amp.businessinsider.com/images/589dbb873149a101788b4c85-750-562.jpg
2) Dùng Chrome đi tìm hình “woman”, sau đó dùng console (ấn Ctrl-Shift-J ) để cất URL (địa chỉ của
từng hình) rồi sau đó lấy hình về máy PC.
Khi search “woman”, kéo chuột xuống dưới đến khi nào hết hình mới ấn Ctrl-Shift-J
Download từ python
download_images('download', 'people')
có 789 tấm:
Có 756 tấm:
Done
Để có nhiều hình thêm em có thể dùng tiếng việt hay là tiếng hoa v.v. khi search hình.
https://towardsdatascience.com/cutting-edge-face-recognition-is-complicated-these-spreadsheets-
make-it-easier-e7864dbf0e1a Good CNN Softmax
Pre-trained:
https://www.analyticsvidhya.com/blog/2017/06/transfer-learning-the-art-of-fine-tuning-a-pre-trained-
model/
https://towardsdatascience.com/transfer-learning-from-pre-trained-models-f2393f124751
Nhiều thí nghiệm hay, người Ấn Độ viết rất nhiều articles hay về AI trên Medium & Quora.com
https://www.analyticsvidhya.com/blog/author/pulkits/ - GOOD Pulkit Sharma
https://vas3k.com/blog/machine_learning/ GOOD
ML ở bệnh viện: Kiểm tra xem xray ngực có được hoàn thành với chất lượng không (định vị và xoay tốt),
sau đó kiểm tra xem phổi có vấn đề rồi báo liền cho bác sĩ X quang.
https://www.gehealthcare.com/products/radiography/mobile-xray-systems/critical-care-suite-on-
optima-xr240amx
https://www.linkedin.com/pulse/need-speed-new-fda-cleared-x-ray-ai-delivers-when-tom-mcguinness
Coi thêm excel file em sẽ thấy cái layer tới tính như thế nào.
https://github.com/fastai/courses/blob/master/deeplearning1/excel/conv-example.xlsx
https://www.youtube.com/watch?v=V2h3IOBDvrA
https://cs224d.stanford.edu/lectures/CS224d-Lecture7.pdf Tensorflow theory
https://course.fast.ai/index.html
https://forums.fast.ai/
https://github.com/pytorch/examples/tree/master/imagenet ; using commandline
https://medium.com/@nicolas.metallo/train-a-choripan-classifier-with-fast-ai-v1-in-google-colab-
6e438817656a
https://medium.com/@intprogrammer/how-to-scrape-google-for-images-to-train-your-machine-learning-classifiers-on-
565076972ce
https://www.youtube.com/watch?v=mWSrzJ-jANM ; Gilbert Tanner - FastAI Tutorial #1 - Image
Classification
https://www.youtube.com/watch?v=YRhxdVk_sIs ; GOOD Convolutional Neural Networks (CNNs)
explained
https://blogs.oracle.com/bigdata/difference-ai-machine-learning-deep-learning
Good question:
https://forums.fast.ai/t/classify-a-single-image-image-classifier/30569
AI Deep fake:
https://www.youtube.com/watch?v=dMF2i3A9Lzw