The advantage of using Colab:
The computer needs to have an nvidia graphics card (I don't have one) to build a cuda environment, but there is no need to set up an environment on Colab, and it is free and very friendly.
step:
1. Log in to Google Drive: https://drive.google.com
2. Click the right mouse button on the blank space of Google Drive, select More -> Associate More Applications;
3. Enter cloaboratory in the search box, find the software and install it.
4. Enter Colab, go back to the Google hard disk section, click the right mouse button in the blank area again, and select More. At this time, you will find that Colab has been installed, just open it.
5. Open the interface as follows, and click the connection in the upper right corner to wait for the connection to be successful.
6. Modify in the upper left corner, open the notebook settings, and select GPU.
7. Click in the order shown in the figure below, select to connect to Google Drive in the pop-up window, and the file directory drive appears on the left.
The drive appears
. 8. Click "+" to add code, download the sample program, and click the triangle to run.
!git clone https://github.com/AlexeyAB/darknet
9. Configure it
%cd darknet
!sed -i 's/OPENCV=0/OPENCV=1/' Makefile
!sed -i 's/GPU=0/GPU=1/' Makefile
!sed -i 's/CUDNN=0/CUDNN=1/' Makefile
10. Compile (a bit slow, takes about a minute)
!make
11. Download coco data and pre-trained weights (you need to ensure that the network is smooth, otherwise it will be very slow).
!wget https://pjreddie.com/media/files/yolov3.weights
12. Add codes for displaying, uploading and downloading files.
# 显示
def imShow(path):
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
image = cv2.imread(path)
height, width = image.shape[:2]
resized_image = cv2.resize(image,(3*width, 3*height), interpolation = cv2.INTER_CUBIC)
fig = plt.gcf()
fig.set_size_inches(18, 10)
plt.axis("off")
plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB))
plt.show()
# 上传文件
def upload():
from google.colab import files
uploaded = files.upload()
for name, data in uploaded.items():
with open(name, 'wb') as f:
f.write(data)
print ('saved file', name)
# 下载文件
def download(path):
from google.colab import files
files.download(path)
13. All the work up to here has been completed. Detect the environment and then use the official photos to test and display (the training process is slow, just wait patiently).
%cd /content/darknet
!./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
imShow('predictions.jpg')
The results are as follows:
As of now, it has been completed. If you want to train your own pictures, you can continue to look down.
14. Run the following code, and then upload your own pictures to the disk where you have pictures
%cd ..
upload()
After uploading:
15. Run the loading code and change the picture name to your own.
%cd darknet
!./darknet detect cfg/yolov3.cfg yolov3.weights ../3.jpg
imShow('predictions.jpg')
16. Effect display:
This is the original image:
This is the output image:
Completed.