Run YOLO v3 on AWS

Purpose

Run YOLO on AWS. ** I don't have much knowledge of AWS, so please see it as an example of a solution. ** **

What was used

[YOLO] https://github.com/ptxyasu/keras-yolo3 [AWS] EC2 https://aws.amazon.com/jp/ec2/

flow

  1. Build an environment on AWS
  2. Data installation required for AWS
  3. Run yolo

About AWS EC2

This time, I chose Ubuntu 16.04 AMI to match the development environment. The instance type is t2.micro, which is a free frame.

Problem 1 Volume shortage

When I build the environment and try to clone keras-yolo3, an error occurs that there is no volume.

Solution 1 Change EBS Volume

Change the volume size from 8GiB little by little. ** Note that you can only change it about once every 6 hours. However, be aware that the more you increase, the more you will be charged! !! ** ** As a result, it was increased to 20 GiB.

Problem 2 Insufficient memory (probably)

Even if I check the error text, I don't get much information.

Solution 2 Change instance type

I felt that one virtual CPU of t2.micro and 1GiB of memory were the problems. So I changed the instance type from t2.micro to t3a.2xlarge. Regarding this, it may have been good to change to t2.large or t2.xlarge. ** Please note that the price will be very high! ** **

Problem 3 Error running yolo

If you read the error statement, the following is probably the problem. I think there is no font in AWS.

yolo_video.py


font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor(8e-3 * image.size[1] + 0.5).astype('int32'))

Solution 3 Program changes

This time, it is only necessary to obtain the class name and its coordinates instead of the detected image, so the part related to depiction can be deleted.

yolo_video.py



def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            predicted_value = self.get_concentration(predicted_class)
            value = max(value,predicted_value)

            label = '{} {:.2f}'.format(predicted_class,score)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.

        end = timer()
        print(end - start)
        return image

    def close_session(self):
        self.sess.close()

Problem 4 Object not detected

By using the previous method, we were able to execute yolo. However, it becomes "Found 0 boxes for img" and the object is not recognized ...

Solution 4

A mistake in the learning model placed on the server? → There is no problem with the model Permission to open images and programs? → No change even if authority is given Development environment is multiple GPUs, AWS has multiple CPUs → It seems that it is not originally related, but it may have a slight effect (verification required)

result

It was executed, but no object was detected. The instance was deleted because the charge increased by about 5,000 yen due to the expansion of the volume and the change of the instance type.

We will work on it again after clarifying the cause of the failure and examining the optimal instance type. If you have any errors, please comment! Please.

Recommended Posts

Run YOLO v3 on AWS v2
Run YOLO v3 on AWS
Run Python on Schedule on AWS Lambda
Run TensorFlow on a GPU instance on AWS
Jupyter on AWS
[Python] Run Headless Chrome on AWS Lambda
YOLO on Chainer
Periodically run a python program on AWS Lambda
I tried running YOLO v3 on Google Colab
Run Django on PythonAnywhere
Run mysqlclient on Lambda
YOLO v4 environment construction ①
Run OpenMVG on Mac
Run GPU version tensorflow on AWS EC2 Spot Instances
Run Jupyter on Ubuntu on Windows
Run Openpose on Python (Windows)
Run AWS IoT Device SDK for Python on Raspberry Pi
Run Tensorflow 2.x on Python 3.7
Run Python CGI on CORESERVER
Run unix command on python
Run IPython Notebook on Docker
Run CircuitPython on Seeeduino XIAO
Run Jupyter Notebook on windows
Run FreeBSD on Linux + qemu
Run OpenVino on macOS Catalina
Run a Java app that resides on AWS EC2 as a daemon
Building an environment to run ChainerMN on a GPU instance on AWS
Run MuJoCo on WSL (2) (also GUI)
Install and run dropbox on Ubuntu 20.04
Preparing to run Flask on EC2
Run Tensorflow natively supported on windows
Run TensorFlow Docker Image on Python3
How to run matplotlib on heroku
Run SwitchBot on Windows 10 with Bleak
Run Matplotlib on a Docker container
Run azure ML on jupyter notebook
Run headless-chrome on a Debian-based image
Run and see AWS Kinesis Firehose
Run XGBoost on Bash on Ubuntu on Windows
Run TensorFlow2 on a VPS server
[Note] Run Django on Amazon Linux 2
Run PartiQL on Dynamodb via boto3
Run docker-compose on Amazon Linux2 on ARM64
Run Yocto on Ubuntu using QEMU.
Run Sesame on Sony's PaSoRi (RCS370)
[Python] I tried using YOLO v3
Periodically run Python on Heroku Scheduler
Run cron on Amazon Linux (set on Linux)
I tried object detection with YOLO v3 (TensorFlow 2.0) on a windows CPU!
Modules cannot be imported in Python on EC2 run from AWS Lambda