2020年11月19日 星期四

opencv training images for object detection memo

官方說明網址 https://docs.opencv.org/3.4/dc/d88/tutorial_traincascade.html

OpenCV使用版本: opencv-3.4.12-vc14_vc15

#這是註解

#資料夾結構

cascade_opencv_train --- pos    #正面影像檔

                                     ---neg    #負面影像檔

#產生neg.txt正面影像檔清單, pos.txt負面影像檔清單

dir /b neg>neg.txt    #部分內容如下

pos\WIN_20201026_13_48_02_Pro.jpg

pos\WIN_20201026_13_48_16_Pro.jpg


dir /b pos>pos.txt    #部分內容如下

neg\frame_100.jpg

neg\frame_1000.jpg

neg\frame_1001.jpg


#使用內建的程式,標註之後要尋找之物件的教學圖案

opencv_annotation.exe --annotations=annotations.txt --images=pos\     #部分內容如下

pos\WIN_20201026_13_48_02_Pro.jpg 1 238 405 135 130

pos\WIN_20201026_13_48_16_Pro.jpg 1 161 346 152 153


#從標註的圖案清單建立sample.vec 檔案

opencv_createsamples -info annotations.txt -vec samples.vec        #下面是我的結果

Info file name: annotations.txt

Img file name: (NULL)

Vec file name: samples.vec

BG  file name: (NULL)

Num: 1000

BG color: 0

BG threshold: 80

Invert: FALSE

Max intensity deviation: 40

Max x angle: 1.1

Max y angle: 1.1

Max z angle: 0.5

Show samples: FALSE

Width: 24

Height: 24

Max Scale: -1

RNG Seed: 12345

Create training samples from images collection...

annotations.txt(171) : parse errorDone. Created 170 samples

#開始訓練,注意,預設的寬與高是24x24,我的正向照片為170個,負面照片為4614個。

opencv_traincascade -data . -vec samples.vec -bg neg.txt -numStages 8 -minHitRate 0.995 -maxFalseAlarmRate 0.3 -featureType LBP -numPos 170 -numNeg 4614

PARAMETERS:
cascadeDirName: .
vecFileName: samples.vec
bgFileName: neg.txt
numPos: 170
numNeg: 4614
numStages: 8
precalcValBufSize[Mb] : 1024
precalcIdxBufSize[Mb] : 1024
acceptanceRatioBreakValue : -1
stageType: BOOST
featureType: LBP
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.3
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
Number of unique features given windowSize [24,24] : 8464

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 1
Precalculation time: 0.914
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0247074|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 1 seconds.

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.024649
Precalculation time: 1.021
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0264413|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 3 seconds.

===== TRAINING 2-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.0021845
Precalculation time: 0.979
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0383615|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 26 seconds.

===== TRAINING 3-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.00032392
Precalculation time: 0.91
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0448635|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 59 seconds.

===== TRAINING 4-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    0 : 0
Required leaf false alarm rate achieved. Branch training terminated.

#訓練之後就會產生cascade.xml的檔案,可以從下面的連結1下載demo程式去偵測看看。

#最後

#參考網頁,一開始建議先從基本的開始做,之後再看1.的連結,會比較懂。別像我花了一堆時間train,結果都不理想。後來才從2.的連結與官網才成功。

1. https://chtseng.wordpress.com/2020/02/07/%E5%A6%82%E4%BD%95%E8%A8%93%E7%B7%B4%E4%B8%80%E5%80%8B%E8%B2%93%E8%87%89%E5%81%B5%E6%B8%AC%E5%99%A8/

2. https://johnallen.github.io/opencv-object-detection-tutorial/



2018年8月8日 星期三

My study note of MNIST and Keras

1. To copy source code and how to use keras as below link.
https://fgc.stpi.narl.org.tw/activity/videoDetail/4b1141305d9cd231015d9d03cfd10027

2. To save training model as below.
    model.save("keras_1_save.h5") #save model
    model.save_weights('keras_1_save_weights.h5')

3. To load training model as below.
    model = load_model('keras_1_save.h5')

4. To online your AI after Machine Learning by predict.

def myPredict(data):
#open image by PIL https://stackoverflow.com/questions/7762948/how-to-convert-an-rgb-image-to-numpy-array
    data=np.reshape(data,(28*28))
    data = np.expand_dims(data, axis=0)    
    result=model.predict(data, batch_size=None, verbose=0, steps=None)
    print("result: ",result)
    print("data:",data)
    print("shape: ",data.shape)
    return data
 myPredict(x_train[1])

5. Summary.
    Due to the MNIST data is difference with my create data by paint.exe in windows. so it still need some of transfer to match MNIST format, because I do not have a lot train/test data.


-------my created data start -----------------------------------------------------------------------
img: <PIL.Image.Image image mode=L size=28x28 at 0x9F73908>
data: [[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][0.][0.][0.][0.][0.][0.][0.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][0.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][0.][0.][0.][0.][0.][0.][0.][0.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][0.][0.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]
[[1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.][1.]]]
[[1.5385496e-05 1.9843709e-08 9.8800381e-09 2.5987378e-03 2.1317775e-109.0811986e-01 8.7126344e-02 2.9842662e-09 2.1397399e-03 1.7077168e-08]]
-------my created data end -----------------------------------------------------------------------




2018年5月17日 星期四

Tensorflow and CUDA install conditions



Tensorflow 1.8 + Cuda capability is 3.0
    1. Install CUDA, patch and cudnn
        cuda_9.0.176_win10_network ( custom install CUDA only, if install get failed)
        cuda_9.0.176.1_windows
        cuda_9.0.176.2_windows
        cudnn-9.0-windows10-x64-v7.1
            unzip to relate cuda folder

    2. CUDA for Python (Install for anaconda)
        conda update conda
        conda install numba
        conda install cudatoolkit

    3. Check CUDA version
        nvcc -V

Geforce 525M is CUDA 2.1

2017年12月22日 星期五

Python networking note

Function Not ready, don't use it. 
netstat -aon

#Ethan_s.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import socket, os, sys, time, subprocess        # Import socket module
from subprocess import Popen, PIPE
reload(sys)
sys.setdefaultencoding('utf8')
#0==================================                                #http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
port = 60001                    # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
s.bind((host, port))            # Bind to the port
s.listen(5)                     # Now wait for client connection.
print 'Server listening....'

#1==================================
while True: #Loop for listening
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print('Server received', conn.recv(1024)) #R0 Hello server!, repr() like as str()

#2==================================
    try:
        while conn:
            data = conn.recv(4096)  #R1, 4096 is recommand
            print ("Receiving...: "+data)
           
            p=subprocess.Popen(data,shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
            stdout,stderr =p.communicate()         

            send_data = str("\nServer got msg: \n===========START================\n"+
                        "cmd: "+data+"\n"+
                        "stdout: \n"+stdout+"\n"+
                        "stderr: \n"+stderr+"\n"+
                        "\n===========END================\n")
           

            conn.sendall(send_data) #S1, buffer不足
            #while (send_data):
            #    conn.send(send_data)
            print("Sending... "+send_data)


    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
    except socket.error as msg:
        print "Socket Error: %s" % msg
    except TypeError as msg:
        print "Type Error: %s" % msg
    finally:
        print('Successfully !')
        #print('Sending finished')
        #conn.send('Thank you for connecting\n')
        conn.close()
        print '\n\n\nServer listening....'



"""
Know issue buffer not ready 如果傳太多data會lose data
"""




#Ethan_c.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import socket, os, sys, time          # Import socket module
from subprocess import Popen   #http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
reload(sys)
sys.setdefaultencoding('utf8')
#0==================================
port = 60001   
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
                                # Reserve a port for your service.



#1==================================
s.connect(('10.110.145.250', port))    #s.connect((host, port))

#10.110.146.77
#10.110.136.52
#10.110.136.159

s.send("Hello server!") #S0


#2==================================
try:
    while True :
        remote_cmd = raw_input("please input the cmd (default is dir): \n")#"please input the cmd (default is dir):", "dir"
     
        if remote_cmd.lower() == "":
            recv_data = s.recv(1024)
            print ("\nReceive... \n"+ recv_data)     
        elif remote_cmd.lower() == "exit" or remote_cmd.lower() == "q":
            s.send("Close by client")
            s.close()
        else:
            s.send(remote_cmd)   #S1
            print('Sending... ',repr(remote_cmd))

        time.sleep(1)   #delay
        recv_data = s.recv(1024)    #R1, buffer not enough
        if recv_data:
            print ("\nReceive... \n"+ recv_data)
            recv_data = s.recv(1024)  # keep waiting for data coming. Bug at here
            if not recv_data:
                s.close()
                break
            time.sleep(1)





except IOError as e:
    print "I/O error({0}): {1}".format(e.errno, e.strerror)
except socket.error as msg:
    print "Socket Error: %s" % msg
except TypeError as msg:
    print "Type Error: %s" % msg 
finally:
    #print("Sever: " +s.recv(1024))
    s.close()
    print('connection closed')

#p=Popen('received_file.bat')
#stdout,stderr =p.communicate()

"""
Know issue buffer not ready 如果傳太多data會lose data
"""

2017年12月14日 星期四

Game

Snes9X

FF6
https://www.ng173.com/thread-567246-1-1.html


7E1862 98

道具欄第一格種類

7E1869 xx

道具欄第一格數量99
7E1969 63

青魔法
7E1D29 FF
7E1D2A FF
7E1D2B FF

全道具
C3272D8A
C3272EEA
C3272FEA
C32741A9
C3274263
C32743EA

輸入完全部金手指後,使用一下道具欄上方「せいとん(整頓)」的指令後就會全道具出現




2017年10月14日 星期六

Tensorflow for JAVA



 微軟的線上 jupyter notebook
https://notebooks.azure.com/


Tensorflow for Python
深度學習的好網站
https://ithelp.ithome.com.tw/articles/10187648
該作者的github
https://github.com/c1mone/Tensorflow-101/blob/master/notebooks/3_MNIST_CNN_Layer_Visualization.ipynb

Tensorflow for JAVA on Windows
https://www.tensorflow.org/install/install_java

下載下面兩個檔案
https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-1.3.0.jar
https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-windows-x86_64-1.3.0.zip

HelloTF.java的原始碼如下,紅色是我加的,其他都跟官網一樣。
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;

public class HelloTF {
  public static void main(String[] args) throws Exception {
System.loadLibrary("tensorflow_jni");
    try (Graph g = new Graph()) {
      final String value = "Hello from " + TensorFlow.version();

      // Construct the computation graph with a single operation, a constant
      // named "MyConst" with a value "value".
      try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
        // The Java API doesn't yet include convenience functions for adding operations.
        g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
      }

      // Execute the "MyConst" operation in a Session.
      try (Session s = new Session(g);
           Tensor output = s.runner().fetch("MyConst").run().get(0)) {
        System.out.println(new String(output.bytesValue(), "UTF-8"));
      }
    }
  }
}


編譯
javac -cp libtensorflow-1.3.0.jar HelloTF.java
執行
java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=jni HelloTF

執行
java -cp libtensorflow-1.3.0.jar;. -Djava.library.path="存放tensorflow_jni.dll檔案的資料夾" HelloTF

結果
2017-10-15 12:33:30.150485: W tensorflow/core/platform/cpu_feature_guard.cc:45]
The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-10-15 12:33:30.150485: W tensorflow/core/platform/cpu_feature_guard.cc:45]
The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
Hello from 1.3.0

另一個範例程式
https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java

需要搭配下面的參考檔案
https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip

執行的方式
java -cp libtensorflow-1.3.0.jar;. -Djava.library.path="存放tensorflow_jni的資料夾" -jar LabelImage.jar "放tensorflow_inception_graph.pb的資料夾"  "測試的圖片.jpg"

結果
2017-10-19 09:41:56.723777: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructio
ns, but these are available on your machine and could speed up CPU computations.
BEST MATCH: bolo tie (26.88% likely)