You are on page 1of 52

Machine learning với thí dụ Keras(TensorFlow), FastAI

(PyTorch) chạy trên Google Colab Nvidia CUDA


Tuan Nguyen Nov 2019

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)

Vài cái mà mình sẽ làm trong bài này


DATASET (TRAINING DATA) -> CNN -> Đoán (Prediction)
NB: bài không viết về “image regions”

Uống ly nước trà hột điều trước khi bắt đầu:


https://www.youtube.com/watch?v=JwIiPjbdoc8
Lý thuyết
Trong machine learning các em thường nghe “đào tạo dữ kiện” (training data), vậy nghĩa là gì? Mình có
thể đào tạo một đứa con ngon, đào tạo một học trò xuất sắc, tại sao phải đào tạo dữ kiện?

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)

Object detection: tìm những objects trong hình

Bài này chỉ nói về “image classification”.

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

pyTorch (developed by Facebook)


TensorFlow (developed by Google)

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.

Những neural network:

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.

Một CNN network

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”)

Tại sao CNN có nhiều layer:


Bởi 1 tấm hình có rất nhiều dử kiện (information) ta phải dùng nhiều layer để nhận diện ra những gì có
trong tấm hình và cũng làm sao cho bài toán đơn giản, không mất cả tháng để tính. Trong trường hợp
với “phân tích hình” neuron sẽ đi ngang qua những cái filter như tìm cạnh (ranh giới giữa sáng và tối),
tìm hình thể….

Điển hình 1 CNN network , em coi chương này rõ hơn.


Nói tóm lại những tấm hình khi đi ngang cái neural network với filter, output là những con số điển hình
cho 1 vật nào đó (con số, xe, con chó, hình con nít, cây, hotdog, bánh, quần …)

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:

InceptionV3 là chinh xác nhất.

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)

“Feature extractor” cũng được gọi là “CNN architecture”.


https://towardsdatascience.com/review-g-rmi-winner-in-2016-coco-detection-object-detection-
af3f2eaf87e4
Machine learning

Vài hàng ngắn về những model khi học về machine learning.


Có ba loại anh ta viết supervised (xài lại những kinh nghiệm trước), unsupervised (hoàn toàn mới) và
semisupervised (có lẫn kinh nghiệm mới và cũ).

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/

Để đoán một con số

Từ layer bên trái tới layer bên phải


Tùy theo độ sáng mỗi pixel có 1 số, 0 là tối, 1 là sáng nhất. Khi qua layer tới con số đó sẽ thay đổi bởi cái
neuron (mỗi neuron sẽ gặp 1 phương trình khác) kế tiếp. Bài toán là làm sao máy tìm ra biên giới giữa
trắng và đen và hình dạng số 3 ra sao. Phương pháp là mỗi pixel (28*28=784 pixels) phải đi ngang qua
nhiều layer để cuối cùng tính được một kết quả nói đó là số 3.

2 layer với 16 neuron.

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…

Vói 3x3 filter


mỗi hàng sẽ
ra 4 con số.

Để ý 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ẻ.

Xem chi tiết thêm về nhận diện số ở đây:


http://yann.lecun.com/exdb/mnist/

Dùng cost: cost gần 0 là thành công.


AI hardware (không phải chỉ có GPU)

Nvidia Jetson Nano vs. Google Coral Dev board, Detailed Comparison
https://www.youtube.com/watch?v=RgTXqP8yZ_Y

Google TPU (tensor(flow) processing unit) ; 149USD

SOM = system on a module (giống SoC = system on a chip)

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

A weight: một giá trị nào


đó, trong thí dụ con số
trong bài này, pixel càng
sáng thì weight càng lớn

NB: trong transfer


learning (xem bài
pytorch) mình xài pre-
trained data, cái data đó
cũng gọi là “weight”. 2 cái
hoàn toàn khác nhau. Nếu dataset không giống nhau (bên trái trục X; X là ngang; Y là truc dọc) và
dataset của mình lớn (bên trên trục Y) thì nên train hoàn toàn.
Thí dụ Yolo dùng COCO Nếu dataset ta nhỏ và giống dataset của YOLO thì chỉ train layer cuối (hình
testset cho đi ngang qua tay phải dưới)
neural network CNN, nó
compile (những kinh
ghiệm) thành “weights”.
yolov3.weights
248.007.048
Bytes (2019). Khi train,
nếu dataset mình giống
“yolo weight” thì nên
dùng weights cho nhanh,
không cần phát minh lại.
Xem 2 links ở
Reference/Pre-train.

Bias (đường nghiên,


khuynh hướng): dùng nó
để có thể di chuyển
output của mình. Thí dụ
cái pixel đó ít sáng, nó
không quan trọng, thôi đè
nó xuống, khỏi phải tính.

Transfer learning: dùng


lại những kinh nghiệm
trước (cũng gọi là pre-
trained model).

Cost (loss) https://www.youtube.com/watch?v=IHZwWFHWa-


Trong thí dụ này có w&list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi&index=3&t=0s ;
13.000 (chỉ có 2 layer) 3BLUE1BROWN
variables khác nhau để
chỉnh, họ dùng phuong
pháp cost. Cái cost càng
nhỏ (minimizing cost)
nghĩa là máy gần tìm ra
con số
Epoche train data bao nhiêu lần, thử 5 lần, rồi 10 lần, để ý khi nào accuracy (gần 1 =
100%) (và loss (cost, gần zero)) tốt nhất
overfitting underfitting Overfitting (overtraining): chính xác quá, data tương lai sẽ xai
Underfitting (undertraining): model không được chính xác lắm

Hãy xem thêm 4 cái video này để hiểu sâu về CNN


https://towardsdatascience.com/cutting-edge-face-recognition-is-complicated-these-spreadsheets-
make-it-easier-e7864dbf0e1a

But what is a Neural https://www.youtube.com/watch?v=aircAruvnKk&feature=emb_logo


Network? | Deep learning,
chapter 1
3BLUE1BROWN

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

Convolutional Neural https://www.youtube.com/watch?v=YRhxdVk_sIs


Networks (CNNs) explained
Deeplizard

A friendly introduction to https://www.youtube.com/watch?v=BR9h47Jtqyw


Deep Learning and Neural
Networks
Luis Serrano

AND Gradient descent, how https://www.youtube.com/watch?v=IHZwWFHWa-w


neural networks learn | Deep
learning, chapter 2
3BLUE1BROWN
Tìm nhạc
Nhac biến dạng thành “frequency domain”, cái đó cất trong 1 tấm hình PNG

Toán học (make it simple, xài vector)


Neural network play chess:
Có nhiều cách để làm model, vậy em nên xài framework nào, tensorflow,
pytorch, keras, darknet
Tại sao xài cái này, tại sao dùng cái kia, em hãy đọc comments dưới đây. Nói chung là cả hai tensorflow,
pytorch đều nhanh, nếu là nhà nghiên cứu nên dùng pytorch (fastai).

Chú ý Keras là Tensorflow, FastAI là Pytorch.

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

Nvidia demos: https://www.nvidia.com/coolstuff/demos

Best graphic card (chỉ có Nvidia mới có CUDA): https://benchmarks.ul.com/compare/best-


gpus?redirected=true#

Update nvidia graphic driver


Nb: chắc anh sẽ gặp vấn đề với FastAI (PyTorch) bởi vì cái nvidia của anh 5 năm cũ rồi!!!

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

Install python và fastai

Anh dùng Windows 10pro 64 bit.

1) Install python và vài library


https://www.python.org/downloads/windows/

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()

C:\Users\tuan>python -m pip install --upgrade pip


C:\Users\tuan>pip install path.py

2) Gài Visual C++ libraries


https://www.scivision.dev/python-windows-visual-c-14-required/
NB: khi khong có nó em sẽ thấy lỗi này khi run “pip install xx”
ERROR: Could not build wheels for bottleneck which use PEP 517 and cannot be installed directly
Windows Python needs Visual C++ libraries installed via the SDK to build code
Download and run: https://aka.ms/vs/16/release/vs_buildtools.exe

Cần khoảng 4GB.

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:\>pip install https://download.pytorch.org/whl/cu90/torch-1.1.0-cp37-cp37m-win_amd64.whl


Collecting torch==1.1.0
Downloading https://download.pytorch.org/whl/cu90/torch-1.1.0-cp37-cp37m-
win_amd64.whl (728.6MB)
|████████████████████████████████| 728.6MB 8.9kB/s
Requirement already satisfied: numpy in d:\program files\python37\lib\site-packages (from
torch==1.1.0) (1.16.4)
Installing collected packages: torch
Successfully installed torch-1.1.0

Test CUDA trong pytorch:


import torch
device = torch.device('cuda')
x = torch.tensor([1., 2.], device=device)

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.

warnings.warn(old_gpu_warn % (d, name, major, capability[1]))


>>>

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.

Thử test với tensorflow xem sao


import tensorflow as tf
tf.test.gpu_device_name()
>>> import tensorflow as tf
2019-11-18 17:45:01.199409: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load
dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
>>> tf.test.gpu_device_name()
2019-11-18 17:45:26.682677: 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-18 17:45:26.758991: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully
opened dynamic library nvcuda.dll
2019-11-18 17:45:26.911041: F tensorflow/stream_executor/lib/statusor.cc:34] Attempting to fetch value
instead of handling error Internal: failed to get device attribute 13 for device 0: CUDA_ERROR_UNKNOWN:
unknown error

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

5) Install fastai (fast AI)


https://github.com/fastai/fastai

click “clone & download”


d:\Download\fastai-master.zip 131.378.328 16/11/2019 19.45 -a— (131MB)
C:\>"Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"

C:\>pip install fastai


Collecting fastai
Using cached
https://files.pythonhosted.org/packages/94/58/d78c39b2fcbe3358a54a4042219eaf78bc196e771dc02a6f540ee2194a2e/fastai-
1.0.59.tar.gz
Collecting bottleneck
Using cached
https://files.pythonhosted.org/packages/b8/d9/2b0beb43e0f8b25624699f918d6b00e53c46812da911eb15ec9ffebf1917/Bottleneck-
1.3.0.tar.gz
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing wheel metadata ... done
Collecting fastprogress>=0.1.19
Using cached
……

Building wheels for collected packages: bottleneck


Building wheel for bottleneck (PEP 517) ... done
Created wheel for bottleneck: filename=Bottleneck-1.3.0-cp37-cp37m-win_amd64.whl size=105324
sha256=85c9cf6cb7a183069ba44d27f61386cb89f86b91a4f776ddbff0ed65f087c5ed
Stored in directory:
C:\Users\tuan\AppData\Local\pip\Cache\wheels\10\90\86\d59b7357e08bbc4c1018dffede068dc2ecb170b16bf20024bc
Successfully built bottleneck
Installing collected packages: bottleneck, fastprogress, soupsieve, beautifulsoup4, numexpr, nvidia-ml-py3, packaging,
urllib3, requests, srsly, cymem, wasabi, murmurhash, preshed, plac, more-itertools, zipp, importlib-metadata, blis, tqdm,
thinc, spacy, fastai
Running setup.py install for nvidia-ml-py3 ... done
Running setup.py install for fastai ... done
Successfully installed beautifulsoup4-4.8.1 blis-0.4.1 bottleneck-1.3.0 cymem-2.0.3 fastai-1.0.59 fastprogress-0.1.21
importlib-metadata-0.23 more-itertools-7.2.0 murmurhash-1.0.2 numexpr-2.7.0 nvidia-ml-py3-7.352.0 packaging-19.2 plac-
1.1.3 preshed-3.0.2 requests-2.22.0 soupsieve-1.9.5 spacy-2.2.2 srsly-0.2.0 thinc-7.3.1 tqdm-4.38.0 urllib3-1.25.7
wasabi-0.4.0 zipp-0.6.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

Trước khi tiếp tục thử check tensorflow’s “Hello world”.


NB: “hello world” là cái code đầu tiên mà lập trình viên bắt đầu với, để có khái niệm về một ngôn ngữ
mới. http://helloworldcollection.de/

C:\Users\tuan\Downloads>pip install tensorflow


C:\Users\tuan\Downloads>pip install --upgrade tensorflow-gpu

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 ☺

Dùng GPU của Google Colab


Hahaha, just keep reading, it pays off ☺. Mình xài Colab thay vì mua laptop mới.

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”.

Guide đây: https://towardsdatascience.com/fast-ai-lesson-1-on-google-colab-free-gpu-d2af89f53604


https://towardsdatascience.com/using-fastai-for-image-classification-54d2b39511ce

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).

Nhớ chọn new “Notebook Python 3” và enable GPU trong Colab.


Check tool

Dùng Keras (Tensorflow) với darknet – làm Classification Model trong


vòng 10 Minutes!
https://www.analyticsvidhya.com/blog/2019/01/build-image-classification-model-10-minutes/
Dataset download từ đây hay là zip file o trang chezmoi.
https://datahack.analyticsvidhya.com/contest/practice-problem-identify-the-apparels/

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.

TRAIN -> VAL -> TEST

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

Click đây để mở cái panel bên tay trái

Chọn GPU trong Colab và click “Run All” (ctrl +F9)


Mount google drive: Click link -> login google account -> Allow -> copy ID -> paste ID trong Colab và ấn
ENTER
NB: hôm nay Nov 2019, code còn xài tốt, khi Colab upgrade TensorFlow thành TensorFlow2, có vài chỗ
em phải chỉnh lại (chú Google sẽ chỉ em).

Click upgrade , it sent to https://www.tensorflow.org/guide/migrate ; from TF1 - > TF2


https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/migrate.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 đã 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ó 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

Dùng fastAI (pytorch) với resnet34 để tìm xúc xích


Thanks to https://medium.com/@nicolas.metallo/train-a-choripan-classifier-with-fast-ai-v1-in-google-
colab-6e438817656a

Dataset anh ta đã xóa, em hãy download từ chezmoi.


http://www.chezmoi.dk/chori-not-chori.zip

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ó.

TRAIN -> VAL -> TEST

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.

Mọi chi tiết, comments anh ghi trong ipynb script.

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

Đây là cai Firefox plugins để em xem ipynb dễ dàng.


Python Notebook Viewer: https://addons.mozilla.org/en-US/firefox/addon/python-notebook-viewer/

Để mở fastai-choripan.ipynb trên firefox.


login with gmail account https://colab.research.google.com/ -> new python 3 Notebook -> open
notebook -> click upload -> chọn fastai-choripan.ipynb

Chọn GPU trong Colab và click “Run All” (ctrl +F9)

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/

Cái này cũng hay cho pytorch https://pytorch.org/tutorials/

20 tỉ đồng dơ không bằng 1 đồng kiến thức trong sạch các em kiếm được.

All the best em

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

3) "invalid character in identifier" error


Strange error, em xoa hết cái cell, copy/paste từng hàng rồi run từng hàng. Anh đổi language,
dùng notepad, viết bằng tay, không khoản trống…..không giúp. Đùng một cái nó Ok.

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

4) Remove files on colab


path = Path('data/choripan-not-choripan/val')
path.ls()
!rm -rf data/choripan-not-choripan/val

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.

Lấy URL (địa chỉ)

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

Trong console, copy/paste cái nay và ấn ENTER


urls = Array.from(document.querySelectorAll('.rg_di .rg_meta')).map(el=>JSON.parse(el.textContent).ou);
window.open('data:text/csv;charset=utf-8,' + escape(urls.join('\n')));

Output, file ”download” nằm đây

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

Trong console, copy/paste cái nay và ấn ENTER


urls = Array.from(document.querySelectorAll('.rg_di .rg_meta')).map(el=>JSON.parse(el.textContent).ou);
window.open('data:text/csv;charset=utf-8,' + escape(urls.join('\n')));

Output, file ”download (1)” nằm đây

Download hình về máy PC

Download từ python
download_images('download', 'people')

Create 2 cái thư viện mới “woman” và “people” ở c:\Users\tuan\Downloads

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.

Nếu có hình giống nhau, em dùng visipics để dọn sạch.


http://www.visipics.info/index.php?title=Main_Page

BONUS: xài jupyter trên laptop

NB: anh có gài thêm vài unix command ở d:\unix


http://unxutils.sourceforge.net/
Khi chạy “jupiter notebook” nó mở 1 trang web tự động, nếu không copy cái URL và paste vào firefox
hay là chrome.

Click >> để chạy tất cả các cell. * nghĩa là nó đang chạy.

Syntax/help (cách xài):


Nếu muốn đọc thêm cách xài, ghi doc() , thí dụ muốn đọc thêm về cnn_learner:
Click insert để insert 1 cái cell mới
Viết doc(cnn_learner) hay là help(cnn_learner)
Click Run
Reference
https://www.youtube.com/watch?v=aircAruvnKk&feature=emb_logo ; neural network Good
https://www.youtube.com/watch?v=oJNHXPs0XDk good video Steve Bruton
https://pytorch.org/tutorials/ Good examples

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

Tính Standardization, deviation, lot of math and “skynet/terminator”


https://medium.com/excel-with-ml/machine-learning-with-spreadsheets-part-1-gradient-descent-
f9316676db9b
https://docs.google.com/spreadsheets/d/1TJXPPQ6Cz-
4kVRXTSrbj4u4orcaamtpGvY58yuJbzHk/edit#gid=815223325

CNN architecture some benchtest:


https://www.intel.ai/practical-applications-of-deep-learning-build-a-diy-smart-security-camera-using-
the-intel-movidius-neural-compute-stick/#gs.x9po00

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://www.youtube.com/watch?v=oI2rvjbzVmI ; CNN Good


https://medium.com/@juliendespois/finding-the-genre-of-a-song-with-deep-learning-
da8f59a61194#.yhemoyql0 ; song music recognition

https://blogs.oracle.com/bigdata/difference-ai-machine-learning-deep-learning

https://www.coursera.org/learn/convolutional-neural-networks ; Andrew Ng Cours; 49usd/tháng


http://www.image-net.org/search?q=dog
https://github.com/fastai/
https://www.youtube.com/watch?v=IPBSB1HLNLo ; deep learning: fastAI 90min
https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/install.html
https://jupyter.org/install
https://www.youtube.com/watch?v=nbJ-2G2GXL0 ; Pytorch in 5 min

Good question:
https://forums.fast.ai/t/classify-a-single-image-image-classifier/30569

AI Deep fake:
https://www.youtube.com/watch?v=dMF2i3A9Lzw

You might also like