반응형

안녕하세요. 오랜만에 글을 작성합니다. 

 

오늘은 예제만 돌려봤다면, 실제로 학습된 모델을 이용하여 얼굴을 찾도록 하겠습니다. 

 

라즈베리파이로 얼굴인식을 찾아보면 Haarcascade 알고리즘이 많이 사용됩니다. 

 

하지만 오래전에 나온 알고리즘이고 좀 더 빠르고 더 정확하게 인공지능으로 얼굴 인식을 할 수 있습니다.

 

그리고 얼굴인식 자체를 Edge TPU가 처리하기 때문에 라즈베리파이의 CPU는 거의 사용되지 않습니다. 

 

그러기 때문에 라즈베리파이에서 부담 없이 강력한 효과를 보여줍니다. 

 

아래 글을 읽어보시고 가시는 것을 추천드립니다. 

 

coding-yoon.tistory.com/87?category=866905

coding-yoon.tistory.com/88?category=866905

coding-yoon.tistory.com/91?category=866905

 

[Coral] TensorFlow Lite : Tflite 모델 생성 & Edge TPU로 Inferece까지 간단하게 개발하기!

안녕하세요. 만약, 처음 이 글을 보신다면 아래 글을 먼저 읽어보시는 것을 추천드립니다. 1편 : Coral Board 대신 라즈베리파이를 사용하는 이유 : https://coding-yoon.tistory.com/85?category=866905 2편 :..

coding-yoon.tistory.com

 

우선, coral에서 Face detection tflite 파일을 먼저 받겠습니다. 

coral.ai/models/

 

Models | Coral

Download pre-compiled demo models that are compatible with the Edge TPU, or use the model source files to retrain the models for your own applications

coral.ai

위 파일 Edge TPU model을 눌러 파일을 받습니다.

ssd_mobilenet_v2_face_quant_postprocess_edgetpu.tflite
6.39MB

네트워크는 경량 된 MobileNet V2와 객체인식 SSD(Single Shot MultiBox Detection)으로 학습된 모델입니다. 

궁금하시면 두 논문을 참고하시면 될 것 같습니다.

 

arxiv.org/abs/1801.04381

 

MobileNetV2: Inverted Residuals and Linear Bottlenecks

In this paper we describe a new mobile architecture, MobileNetV2, that improves the state of the art performance of mobile models on multiple tasks and benchmarks as well as across a spectrum of different model sizes. We also describe efficient ways of app

arxiv.org

arxiv.org/abs/1512.02325

 

SSD: Single Shot MultiBox Detector

We present a method for detecting objects in images using a single deep neural network. Our approach, named SSD, discretizes the output space of bounding boxes into a set of default boxes over different aspect ratios and scales per feature map location. At

arxiv.org

 

detect.py (google git에서 구할 수 있음)

# Lint as: python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions to work with detection models."""

import collections
import numpy as np

Object = collections.namedtuple('Object', ['id', 'score', 'bbox'])


class BBox(collections.namedtuple('BBox', ['xmin', 'ymin', 'xmax', 'ymax'])):
  """Bounding box.
  Represents a rectangle which sides are either vertical or horizontal, parallel
  to the x or y axis.
  """
  __slots__ = ()

  @property
  def width(self):
    """Returns bounding box width."""
    return self.xmax - self.xmin

  @property
  def height(self):
    """Returns bounding box height."""
    return self.ymax - self.ymin

  @property
  def area(self):
    """Returns bound box area."""
    return self.width * self.height

  @property
  def valid(self):
    """Returns whether bounding box is valid or not.
    Valid bounding box has xmin <= xmax and ymin <= ymax which is equivalent to
    width >= 0 and height >= 0.
    """
    return self.width >= 0 and self.height >= 0

  def scale(self, sx, sy):
    """Returns scaled bounding box."""
    return BBox(xmin=sx * self.xmin,
                ymin=sy * self.ymin,
                xmax=sx * self.xmax,
                ymax=sy * self.ymax)

  def translate(self, dx, dy):
    """Returns translated bounding box."""
    return BBox(xmin=dx + self.xmin,
                ymin=dy + self.ymin,
                xmax=dx + self.xmax,
                ymax=dy + self.ymax)

  def map(self, f):
    """Returns bounding box modified by applying f for each coordinate."""
    return BBox(xmin=f(self.xmin),
                ymin=f(self.ymin),
                xmax=f(self.xmax),
                ymax=f(self.ymax))

  @staticmethod
  def intersect(a, b):
    """Returns the intersection of two bounding boxes (may be invalid)."""
    return BBox(xmin=max(a.xmin, b.xmin),
                ymin=max(a.ymin, b.ymin),
                xmax=min(a.xmax, b.xmax),
                ymax=min(a.ymax, b.ymax))

  @staticmethod
  def union(a, b):
    """Returns the union of two bounding boxes (always valid)."""
    return BBox(xmin=min(a.xmin, b.xmin),
                ymin=min(a.ymin, b.ymin),
                xmax=max(a.xmax, b.xmax),
                ymax=max(a.ymax, b.ymax))

  @staticmethod
  def iou(a, b):
    """Returns intersection-over-union value."""
    intersection = BBox.intersect(a, b)
    if not intersection.valid:
      return 0.0
    area = intersection.area
    return area / (a.area + b.area - area)


def input_size(interpreter):
  """Returns input image size as (width, height) tuple."""
  _, height, width, _ = interpreter.get_input_details()[0]['shape']
  return width, height


def input_tensor(interpreter):
  """Returns input tensor view as numpy array of shape (height, width, 3)."""
  tensor_index = interpreter.get_input_details()[0]['index']
  return interpreter.tensor(tensor_index)()[0]


def set_input(interpreter, size, resize):
  """Copies a resized and properly zero-padded image to the input tensor.
  Args:
    interpreter: Interpreter object.
    size: original image size as (width, height) tuple.
    resize: a function that takes a (width, height) tuple, and returns an RGB
      image resized to those dimensions.
  Returns:
    Actual resize ratio, which should be passed to `get_output` function.
  """
  width, height = input_size(interpreter)
  w, h = size
  scale = min(width / w, height / h)
  w, h = int(w * scale), int(h * scale)
  tensor = input_tensor(interpreter)
  tensor.fill(0)  # padding
  _, _, channel = tensor.shape
  tensor[:h, :w] = np.reshape(resize((w, h)), (h, w, channel))
  return scale, scale


def output_tensor(interpreter, i):
  """Returns output tensor view."""
  tensor = interpreter.tensor(interpreter.get_output_details()[i]['index'])()
  return np.squeeze(tensor)


def get_output(interpreter, score_threshold, image_scale=(1.0, 1.0)):
  """Returns list of detected objects."""
  boxes = output_tensor(interpreter, 0)
  class_ids = output_tensor(interpreter, 1)
  scores = output_tensor(interpreter, 2)
  count = int(output_tensor(interpreter, 3))

  width, height = input_size(interpreter)
  image_scale_x, image_scale_y = image_scale
  sx, sy = width / image_scale_x, height / image_scale_y

  def make(i):
    ymin, xmin, ymax, xmax = boxes[i]

    return Object(
        id=int(class_ids[i]),
        score=float(scores[i]),
        bbox=BBox(xmin=xmin,
                  ymin=ymin,
                  xmax=xmax,
                  ymax=ymax).scale(sx, sy).map(int))

  return [make(i) for i in range(count) if scores[i] >= score_threshold]

 

main.py

import detect
import tflite_runtime.interpreter as tflite
import time

from PIL import Image
from PIL import ImageDraw
import cv2
import numpy as np
import time
import os



# .tflite interpreter
interpreter = tflite.Interpreter(
    os.path.join(os.getcwd(), "ssd_mobilenet_v2_face_quant_postprocess_edgetpu.tflite"),
    experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')]
    )
interpreter.allocate_tensors()

# Draws the bounding box and label for each object.
def draw_objects(image, objs):
    for obj in objs:
        bbox = obj.bbox
        
        cv2.rectangle(image,(bbox.xmin, bbox.ymin), (bbox.xmax, bbox.ymax), (0, 255, 0),2)

        bbox_point_w = bbox.xmin + ((bbox.xmax-bbox.xmin) // 2)
        bbox_point_h = bbox.ymin + ((bbox.ymax-bbox.ymin) // 2) 
        
        cv2.circle(image, (bbox_point_w, bbox.ymax-bbox.ymin), 5, (0,0,255),-1)
        cv2.putText(image, text='%d%%' % (obj.score*100), org=(bbox.xmin, bbox.ymin), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1.0, color=(0, 255, 0), thickness=1, lineType=cv2.LINE_AA)

        

        
def main():
    cap = cv2.VideoCapture(0)    

    while True:
        ret, image = cap.read()

	    # image reshape
        image = cv2.resize(image, dsize=(320, 320), interpolation=cv2.INTER_AREA)
	    # image BGR to RGB
        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

       	tensor = detect.input_tensor(interpreter=interpreter)[:, :] = image.copy() 
        tensor.fill(0)  # padding        
        interpreter.invoke()  # start
        
        objs = detect.get_output(interpreter, 0.5, (1.0, 1.0))
        
        if len(image):
            draw_objects(image, objs)

        image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
        cv2.imshow('face detector', image)

        k = cv2.waitKey(30) & 0xff
        if k == 27: # press 'ESC' to quit # ESC exit
            break


if __name__ == '__main__':
    main()

 

 

위 코드를 돌려보시면, delay가 전혀 없이 높은 성능으로 얼굴 인식을 합니다. 

 

그렇기 때문에 얼굴 인식하고 다른 프로세스를 추가할 수 있습니다. 예를 들어 얼굴 인식하고 마스크를 검출하더라도 

전혀 delay가 없습니다.

 

그리고 얼굴인식을 하기 전,  전처리가 없습니다. 

 

딥러닝으로 만든 모델을 돌려보면, 실제로 적용되지 않는 경우가 많습니다. 

 

만들어진 모델을 성능을 높이기 위해 OpenCV의 전처리를 많이 사용하는 경우가 많습니다. 

 

하지만 이 같은 경우는 OpenCV는 사이즈를 줄이거나, 단순히 View 역할만 하고 그 외 작업은 전혀 하지 않습니다. 

 

 

마스크를 쓰지 않았을 때
마쓰크를 썻을 때

딥러닝을 공부하신다면, 실제로 모델을 적용해보시는 것도 많은 공부를 하는데 도움이 될 것 같습니다. 

728x90
반응형
반응형

 

안녕하세요. 

 

만약, 처음 이 글을 보신다면 아래 글을 먼저 읽어보시는 것을 추천드립니다.

 

1편 : Coral Board 대신 라즈베리파이를 사용하는 이유 : https://coding-yoon.tistory.com/85?category=866905

2편 : USB Accelerator(Edge TPU) 환경설정 : https://coding-yoon.tistory.com/87?category=866905

3편 : Coral Edge TPU 예제 돌리기 : https://coding-yoon.tistory.com/88

 

 

이 그림을 이해하기만 하면 끝!

Edge TPU는 CNN과 같은 Deep feed-forward 신경망을 실행할 수 있습니다.

 

8비트 Edge TPU 용으로 컴파일 된 TensorFlow Lite 모델만 지원하며, 32비트 TensorFlow는 지원하지 않습니다. 

 

Tensor Lite로 직접 모델을 학습시킬 수 없고, Transfer Learning(전이 학습)을 통해 모델을 향샹시킬 수 있습니다.

 

TensorFlow(.pb)나 Keras(.h5)(32bit)를  통해 학습된 모델을 TensorFlow Lite Converter를 사용하여 TensorFlow 파일에서 TensorFlow Lite (.tflite)로 모델을 변환해야합니다. 

 

 

<참고> 

저는 Coral로 개발을 할 때 개인적으로 Keras를 추천드리고 싶습니다. 

 

Edge TPU가 모든 연산을 하는 것은 아닙니다. 공식 홈페이지에서 Edge TPU가 지원하는 연산들을 소개합니다. 

 

모바일넷과 같은 경량 모델에서 사용하는 연산들을 지원하며, 예제들 또한 Keras모델을 불러와 학습시켰습니다.

 

예를 들면 Depthwise Seperable Convolution을 찾을 수 있고, 경량 모델 : MobileNet,  Xception 등을 볼 수 있습니다.

https://coding-yoon.tistory.com/77?category=825914

 

[딥러닝] Depthwise Separable Covolution with Pytorch( feat. Convolution parameters VS Depthwise Separable Covolution paramet

안녕하세요. Google Coral에서 학습된 모델을 통해 추론을 할 수 있는 Coral Board & USB Accelator 가 있습니다. 저는 Coral Board를 사용하지 않고, 라즈베리파이4에 USB Accelator를 연결하여 사용할 생각입니..

coding-yoon.tistory.com

Coral에서 만든 예제들

 

 

우선, TensorFlow나 Keras로 학습된 모델을 저장해야 합니다. 

 

TensorFlow로 학습했다면, (.pb)파일,

Keras로 학습했다면 (.h5)파일

 

 

학습된 모델(32bit)을 TensorFlow Lite Converter(8bit)로 변환해야합니다.

 

변환한 모델을 저장합니다.

 

이제 모델이 준비가 끝났습니다. 최소한의 코드로 작성했습니다.

불러오기

Operation name

Runtime version*

Known limitations

Add

All

 

AveragePool2d

All

No fused activation function.

Concatenation

All

No fused activation function.
If any input is a compile-time constant tensor, there must be only 2 inputs, and this constant tensor must be all zeros (effectively, a zero-padding op).

Conv2d

All

Must use the same dilation in x and y dimensions.

DepthwiseConv2d

12

Dilated conv kernels are not supported.

13

Must use the same dilation in x and y dimensions.

ExpandDims

13

 

FullyConnected

All

Only the default format is supported for fully-connected weights. Output tensor is one-dimensional.

L2Normalization

All

 

Logistic

All

 

Maximum

All

 

MaxPool2d

All

No fused activation function.

Mean

12

No reduction in batch dimension. Supports reduction along x- and/or y-dimensions only.

13

No reduction in batch dimension. If a z-reduction, the z-dimension must be multiple of 4.

 

지원하는 연산입니다. 지원하는 연산을 더 보고 싶으시면 아래 주소를 달아놓을테니 보시면 좋을 것 같습니다.

 

 

파란색 Input_data가 추론하고자 하는 실제 데이터입니다. 

 

input쪽이 코드가 길어져 input_tensor로 따로 함수화를 시켰습니다.

Coral 공식 GitHub예제를 따라했습니다.

 

output쪽은 그렇게 길지 않기 때문에 tensorflow lite 공식 홈페이지를 따라했습니다.

 

추론을 시작하는 코드는 Interpreter.invoke()입니다.

 

추론을 완료하면 output_data에 추론 값이 담겨 있습니다.

 

 

다음 글은 간단한 모델을 하나 불러와 실제로 추론을 한 번 해보겠습니다. 

 

https://coral.ai/docs/edgetpu/models-intro/#compatibility-overview

https://www.tensorflow.org/tutorials/keras/save_and_load?hl=ko

https://www.tensorflow.org/lite/convert/python_api

https://coral.ai/docs/edgetpu/tflite-python/#load-tensorflow-lite-and-run-an-inference

https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter

728x90
반응형
반응형

1편 : Coral Board 대신 라즈베리파이를 사용하는 이유 : https://coding-yoon.tistory.com/85?category=866905

2편 : USB Accelerator(Edge TPU) 환경설정 : https://coding-yoon.tistory.com/87?category=866905

 

안녕하세요.

 

라즈베리파이에서 인공지능 예제를 간단하게 돌려보겠습니다. 

 

혹시 준비가 되지 않으셨다면 2편을 읽고 오시는 것을 추천드립니다. 

 

 

mkdir coral && cd coral  # coral 디렉토리 생성

git clone https://github.com/google-coral/tflite.git  

 

 

cd tflite/python/examples/classification

bash install_requirements.sh
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   189  100   189    0     0    314      0 --:--:-- --:--:-- --:--:--   313
100 3972k  100 3972k    0     0  1622k      0  0:00:02  0:00:02 --:--:-- 4012k
100   181    0   181    0     0    332      0 --:--:-- --:--:-- --:--:--   332
100 3448k  100 3448k    0     0  1739k      0  0:00:01  0:00:01 --:--:-- 6748k
100   158  100   158    0     0    541      0 --:--:-- --:--:-- --:--:--   541
100 40895  100 40895    0     0  63699      0 --:--:-- --:--:-- --:--:-- 3968k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   148  100   148    0     0    361      0 --:--:-- --:--:-- --:--:--   361
100  582k  100  582k    0     0   483k      0  0:00:01  0:00:01 --:--:-- 2277k

github에서 classification과 detection이 같이 설치가 됩니다. 

 

document처럼 classification을 따라할겁니다. detection read.md를 보시고 따라하시면 됩니다. 

 

 

python3 classify_image.py \
--model models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
--labels models/inat_bird_labels.txt \
--input images/parrot.jpg

 

classify_image.py를 실행하고 뒤에 3개의 Argumnets가 따라옵니다. 

 

python의 argparse 라이브러리를 사용해 인자 값을 달리 줘서 다르게  동작시킬 수 있습니다. 

 

머신러닝에서 많이 사용하는 함수인데, 이것은 설명할 수 있는 기회가 있으면 자세히 설명하도록 하겠습니다.

 

인자 값으로 세 개의 파라미터가 전달됩니다. 

 

꼭 argparse를 사용할 필요는 없습니다. 나중에 tensorflow lite interpreter를 사용하는 방법에 대해 설명할 것입니다. 

 

지금은 아 그런가보다 하고 넘어가시면 됩니다.

 

실행해보겠습니다.

 

parrot.jpg

----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
17.1ms
4.4ms
4.4ms
4.3ms
4.5ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.77734

# 77% Ara macao 라는 새라고 예측함

Ara macao

예측을 잘한 것 같습니다. 속도도 ms 단위로 나름 빠르게 inference해줍히다. 

 

libedgetpu1-max를 시켜주면 더 빨라지겠죠?

 

다음 글은 자신이 만든 모델을 TPU에 넣기 좋은 모양으로 만드는 방법을 올리겠습니다.

 

https://coral.ai/docs/accelerator/get-started/#3-run-a-model-using-the-tensorflow-lite-api

 

Get started with the USB Accelerator | Coral

Learn how to set up the Coral USB Accelerator and run some demo code.

coral.ai

 

728x90
반응형
반응형

안녕하세요. 

 

한 달만에 USB Accelator 에 대해 드디어 글을 작성합니다. 

 

https://www.devicemart.co.kr/goods/view?no=12379075

 

Google Coral USB Accelerator

구글 코랄 USB 액셀레이터 / 컴퓨터에 꽂아 Google Edge TPU 보조프로세서로 기능하는 제품 / USB 3.0 C타입 / Google 클라우드와 호환 / 로컬 인공지능

www.devicemart.co.kr

99,000원에 디바이스마트에서 구매할 수 있습니다. 

오... 
개봉!
장착
Coral-USB-Accelerator-datasheet.pdf
1.46MB

자세한 데이터 시트를 자세히 보고 싶으시면 위 pdf를 참고하시길 바랍니다. 

 

간단하게 요약하면

 

제가 공부자료로 만들어 놓은 PPT의 일부분입니다. 

 

C타입 포트 TPU 이며, USB 3.0 Port(파란색), Python은 3.5, 3.6. 3.7만 지원합니다. 

(Python 3.8은 지원하지 않습니다.)

 

바로 시작해보겠습니다.

 

 

 

USB Accelator(Edge TPU)를 사용하기 위해서는 The Edge TPU runtime을 설치해야합니다. 

 

(윈도우로 따지면 드라이버같은 거라고 생각하시면 편할 것 같습니다. )

 

라즈비안이나 우분투는 Linux 계통이기 때문에 Linux 단계로 따라가겠습니다.

(Mac이나 Window에서 원하시면 옆 주소를 확인하시기 바랍니다. https://coral.ai/docs/accelerator/get-started/

 

1. Repository에 추가 

echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list

curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

sudo apt-get update

 

2. Edge TPU 설치

sudo apt-get install libedgetpu1-std

 

3. 옵션(속도 최대화 : 대신 기기가 뜨거워질 수 있으니 주의)

sudo apt-get install libedgetpu1-max

USB Accelator(Edge TPU)는 직접 모델을 학습시킬 수 없습니다. Inference용입니다. 

 

대신, TensorFlow나 Keras로 학습된 모델(32bit)TensorFlow lite(8bit)로 변환 후 .tflite로 변경시키고 좀 더 성능을 향샹시키려면 Transfer Learning(전이학습)을 하면 됩니다. 

 

오늘은 인공지능을 할 수 있는 셋팅까지만 설명하겠습니다. 

 

이제 TensorFlow Lite 인터프리터를 설치하겠습니다.

 

 

 

 

 

https://www.tensorflow.org/lite/guide/python

 

Python 빠른 시작  |  TensorFlow Lite

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trade

www.tensorflow.org

위를 참고하시기 바랍니다.

 

참고로 라즈베리파이는 ARM Architecture 32bit 입니다. 

 

자신의 파이썬 버젼에 맞게 깔아주시면 됩니다. (python3 -V) 

 

pip3 install <해당 URL 복사/붙여넣기>
플랫폼 파이썬 URL
Linux (ARM 32) 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_armv7l.whl
3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_armv7l.whl
3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
3.8 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_armv7l.whl

 

이렇게 라즈베리파이에서 인공지능 추론을 할 수 있는 환경을 다 갖췄습니다.

 

다음 글은 간단한 예제 하나를 돌려보도록 하겠습니다.

728x90
반응형

+ Recent posts