블로그 이미지
devtang
Instagram : @taebr0 devtaehyeong@gmail.com

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2020. 4. 14. 17:05 AI/Keras

 

 

 

본 게시물은 전 게시물의 세팅환경에서 진행하였습니다.

 

numpy 1.15
tensorflow 1.4
keras 2.0.3
cuda8.0
cudnn 5.1

 

https://ultrakid.tistory.com/22

 

[Keras] Anaconda를 이용한 Faster R-CNN 세팅 및 예제 실행

본 글은 Windows 10 환경에서 Anaconda에 가상환경을 세팅하여 진행하였습니다. Faster R-CNN Faster R-CNN의 가장 핵심 부분은 Region Proposal Network(RPN) 입니다. R-CNN은 이미지 내에 객체가 존재할 것 같..

ultrakid.tistory.com

공부를 하던중에 사실 Faster R-CNN 기반의 Real-time object detection(실시간 물체 인식)에 관한 예제를 찾고 있었으나, 몇일동안 찾아도 Windows 기반으로 구축된 글은 외국 사이트를 뒤져봐도 저의 능력으론 찾지 못했습니다..

 

Faster R-CNN 추론한 사진 (GTA 5)

 

그나마 다행인건 동영상을 인식하는 예제가 있긴 했는데 살펴보니 동영상 자체를 input시켜 detection 하는 것이 아닌 동영상을 프레임?으로 쪼개어 사진 한장마다 인식을해서 인식이된 모든 사진을 다시 합쳐 동영상형태로 만드는 예제 코드를 찾았습니다. 처리 모델로는 ResNet50 을 사용하였습니다.

 

우선 전 게시물에 예제 코드를 하나 받아야합니다.

https://github.com/riadhayachi/faster-rcnn-keras/blob/master/test_frcnn_count.py

 

riadhayachi/faster-rcnn-keras

object detection in video. Contribute to riadhayachi/faster-rcnn-keras development by creating an account on GitHub.

github.com

 

다운을 받고 폴더안에 넣어줍니다.

마찬가지로 Anaconda Prompt를 실행시켜 만들어준 fasterRCNN 가상환경을 activate 해줍시다.

자이제 위의 폴더로 들어가서 코드를 입력합니다.

python test_frcnn_count.py --input_file 경로/처리할동영상.mp4 --output_file 경로/출력동영상이름.mp4 --frame_rate=25

frame rate는 25로 default값을 설정해줍니다.

 

띠용..

텐서플로우도 backend로 실행 잘되는가 싶더니 코드에 문제가 없지만 TypeError가 떴습니다.

https://github.com/pender/chatbot-rnn/issues/31

 

Pickle TypeError: a bytes-like object is required, not 'str' · Issue #31 · pender/chatbot-rnn

Getting this error: Traceback (most recent call last): File "chatbot.py", line 301, in main() File "chatbot.py", line 35, in main sample_main(args) File "chatbot.py", ...

github.com

 

음영 처리된 부분중 with open(config_output_filename, 'r') as f_in: 부분에서 'r'을 'rb'로 고쳐야합니다.

 

 

 

또 음영처리된 174번째 라인중 저부분이 원래는 var로 되어있으나 str(var)로 고쳐줘야합니다..

 

 

 

도대체 어디서부터 잘못된걸까 이틀동안 찾아본것 같습니다.

아마 이 코드가 python2.x버전대에서 작성한 코드라서 3.6버전에 안맞았던 것 같습니다.

2버전에서 3버전으로 올라가면서 문법이 약간씩 수정되었다고 합니다.

https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

 

The key differences between Python 2.7.x and Python 3.x with examples

Many beginning Python users are wondering with which version of Python they should start. My answer to this question is usually something along the lines “ju...

sebastianraschka.com

아래와 같이 변경해줍니다.

cv2.rectangle(img_scaled,(x1, y1),(x2, y2), (int(class_to_color[key][0]),int(class_to_color[key][1]),int(class_to_color[key][2])),2)

이제 다시 코드를 입력하고 실행시켜줍니다.

python test_frcnn_count.py --input_file 경로/처리할동영상.mp4 --output_file 경로/출력동영상이름.mp4 --frame_rate=25

동영상을 프레임으로 쪼개어 한장씩 Object Detection을 수행한후 종합된 result image를 다시 합치는 방식입니다.

 

 

 

아래는 변환된 동영상입니다.

https://youtu.be/CyvN4hZTMxo

 

 

posted by devtang
2020. 4. 3. 13:19 AI/Keras

본 글은 Windows 10 환경에서 Anaconda에 가상환경을 세팅하여 진행하였습니다.

 

Faster R-CNN

Faster R-CNN 구조

Faster R-CNN의 가장 핵심 부분은 Region Proposal Network(RPN) 입니다.

R-CNN은 이미지 내에 객체가 존재할 것 같은 위치를 제안 하면, 제한된 위치의 이미지를 잘라냅니다.

잘라낸 이미지의 feature map을 추출하여 그것에 대해 CNN(Convolutional Neural Network)을 사용했습니다.

 

하지만 Faster R-CNN은 잘라낸 영역마다 CNN을 사용하는것이 아니라 이미지 전체를 CNN에 통과시킨 후,

큰 feature map을 추출해내어  제안된 영역을 feature map에 투영하는 방식입니다.

즉 여러번 처리할일을 한번에 하게되어 처리속도가 전 모델보다 훨씬 빨라졌으며, 전작 대비 정확도가 상승했습니다.

 

우선 Anaconda 과Visual C++ 2015 Build Tools 가 설치되있는 가정하에 진행하겠습니다.

 

아나콘다 프롬프트창을 열어줍니다. 저는 가상환경이름을 fasterRCNN으로 지었습니다.

(base) C:\Users\@USERNAME@>conda create -n fasterRCNN python=3.6 pip

가상환경이 설정된 후 가상환경으로 진입합니다.

activate fasterRCNN

진입하면 다음과 같습니다.

pip install tensorflow-gpu==1.4

pip install keras==2.0.3

pip install numpy==1.15
pip install sklearn h5py

pip install opencv-python ipython

필요한 요소들을 설치해줍니다. (cudatoolkit==8.0 cudnn==5.1 버전사용, 혹시나 cuda 관련 에러뜰시 따로 설치필요!)

 

그리고 faster R-CNN을 git에 들어가서 다운로드해줍니다.

https://github.com/you359/Keras-FasterRCNN

 

you359/Keras-FasterRCNN

keras implementation of Faster R-CNN. Contribute to you359/Keras-FasterRCNN development by creating an account on GitHub.

github.com

저는 이폴더를 바탕화면에 두었습니다.

 

그리고 예제에 필요한 웨이트파일을 다운받아서 git에서 받은 faster rcnn폴더에 넣어줍니다.

 

이제 예측할 input image를 넣을 폴더를 생성해줍니다.

(fasterRCNN) C:\Users\@USERNAME@\Desktop\keras-frcnn-master>mkdir images

images 폴더안에 자신이 예측하고싶은 사진을 넣습니다.

저는 YOLO 예제돌릴때 쓰는 개사진과 바다에 배가 떠있는 사진을 예제로 테스트해봤습니다.

(fasterRCNN) C:\Users\@USERNAME@\Desktop\keras-frcnn-master>python test_frcnn.py -p images

 

제 컴퓨터의 그래픽카드는 GTX1050TI이며 한사진당 detection 시 2초정도 걸린것 같습니다.

 

참고사이트

https://github.com/you359/Keras-FasterRCNN

 

you359/Keras-FasterRCNN

keras implementation of Faster R-CNN. Contribute to you359/Keras-FasterRCNN development by creating an account on GitHub.

github.com

https://inspace4u.github.io/dllab/lecture/2017/09/09/Keras_FRCNN_Testing.html

 

Keras 기반 F-RCNN 실습

본 포스트에서는 Keras 기반으로 구현한 Faster RCNN 코드를 직접 실행 및 실습해 보겠습니다. 실행 환경 이 예제에서는 기본적인 Tensorflow와 Keras 이외에 이미지 처리를 위한 OpenCV 라이브러리와 대용량 데이터를 다루는 포맷인 hdf5를 지원하기 위한 h5py 패키지가 추가로 필요합니다. 만약 Windows 10에서 텐서플로우, 케라스 오프라인 설치 또는 Windows 7 포스트의 설명을 따라 환경을 구축하셨다면 이 두 패키지가

inspace4u.github.io

2020-08-12 수정 내용

weights 파일이 본글에서 만료되어서 아래 링크를 클릭하시면 다운받으실 수 있습니다!!

https://drive.google.com/file/d/1oQbhyRJFLRmbd9d2msAZCL77_nXbJvLR/view?usp=sharing

 

model_frcnn.hdf5

 

drive.google.com

config.pickle
0.00MB

 

posted by devtang
prev 1 next