Building Deep Learning Applications with Raspberry Pi 4b: Software Setup

Introduction

In the previous article, we prepared all the hardware. In this article, we will set up the Raspberry Pi system and install some necessary software tools.

Building Deep Learning Applications with Raspberry Pi 4b: Software Setup

Building Deep Learning Applications with Raspberry Pi 4b: Software Setup

Raspberry Pi Image Burning

Building Deep Learning Applications with Raspberry Pi 4b: Software Setup

The operating system options on the Raspberry Pi are numerous. Here, we will take the official latest version 2020.8.20 as an example, assuming the main computer is running Windows 10.
First, download the burning software Raspberry Pi Imager from the official website, and then download the system image file with recommended software.
https://www.raspberrypi.org/downloads/
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
After installing Raspberry Pi Imager, insert the MicroSD card, in Operating System –> Use custom, select the downloaded image file, then choose the corresponding SD card in CHOOSE SD CARD, and finally click WRITE to burn.
Enable SSH and Configure Wi-Fi

If you haven’t configured the keyboard and screen, you need to create two files to configure the Wi-Fi connection and enable SSH before the first boot of the OS.

1 Enable SSH
In the boot partition of the just burned MicroSD, create a blank file and rename it to ssh, all lowercase and without an extension.
2 Configure Wi-Fi
Also in the root directory, create a wpa_supplicant.conf file with the following content:
country=CNctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1 network={ssid="SSID"psk="PASSWORD"key_mgmt=WPA-PSKpriority=1}

Where ssid is the Wi-Fi network’s SSID;

psk is the connection password;

key_mgmt is the encryption method:

WPA-PSK = WPA pre-shared key (this requires the 'psk' field)WPA-EAP = WPA using EAP authenticationEEE8021X = IEEE 802.1X using EAP authentication and dynamically generated keysNONE = No WPA; can use plaintext password or static WEPWPA-PSK-SHA256 = Similar to WPA-PSK but uses enhanced SHA256-based encryption algorithmWPA-EAP-SHA256 = Similar to WPA-EAP but uses enhanced SHA256-based encryption algorithmIf not set, the mode will be set to WPA-PSK WPA-EAP

Priority is the priority level, the larger the number, the higher the priority;

Modify the corresponding ssid, psk, and key_mgmt values according to your router configuration.

After booting, the wpa_supplicant.conf file will automatically move to the /etc/wpa_supplicant directory, and the ssh file will be converted to the command to enable the SSH service and then deleted. Therefore, if you need to change the configuration, you must add this file again next time.

Configure Raspberry Pi
Now you can remove the MicroSD card, insert it into the Raspberry Pi, and then power it on normally.
✎ Tip

Pay attention to the two indicator lights on the Raspberry Pi, the red one represents power, and the green one generally represents SD read/write. Under normal circumstances, the red light is always on, and the green one flashes every few seconds, indicating a successful network connection; while the green light flashing regularly indicates that the SD card is not properly inserted or is invalid.

1 Check Raspberry Pi IP Address
If you have a monitor, you can directly use ifconfig to get the IP address;
If you do not have a monitor, you can use a local network scanning tool like Advanced IP Scanner to get the corresponding information:
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
2 SSH Login
With the IP address, you can log in using SSH. Enter the command
ssh [email protected]

Then enter the default password raspberry to log in normally.

3 Configure Raspberry Pi
In the logged-in SSH connection, enter the command
sudo raspi-config

Building Deep Learning Applications with Raspberry Pi 4b: Software Setup

Select 1 Change User Password to change the current user’s password;

Select 5 Interfacing Options –> P1 Camera to enable the camera;

Select 5 Interfacing Options –> P2 SSH to enable SSH (already enabled);

Select 5 Interfacing Options –> P3 VNC to enable VNC;

Select 7 Advanced Option –> A1 Expand Filesystem to expand available space;

Select 7 Advanced Option –> A5 Resolution to change the screen resolution.

Remote Connect to Raspberry Pi
1 Use VNC Remote Desktop
Install VNC-Viewer-6.20.113-Windows, create a new connection, fill in the Raspberry Pi’s IP address, enter the username pi, and password raspberry to log in.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
✎ Tip

If the VNC connection shows “Cannot currently show the desktop”, just modify the screen resolution and restart.

2 Change Raspberry Pi to a Fixed IP Address
Every time the Raspberry Pi is powered on, the IP address assigned by the router DHCP may change. Here, we set a fixed address to facilitate the next connection.
After connecting with VNC, you can see the desktop of the Raspberry Pi. Right-click on the Wi-Fi icon in the upper right corner of the screen, and select Wireless & Wired Network Settings.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
In the Configure section, select SSID, bind to the specified Wi-Fi connection, and fill in the corresponding subnet:

IPv4 Address: 192.168.1.101

Router: 192.168.1.1

DNS Servers: 192.168.1.1

Building Deep Learning Applications with Raspberry Pi 4b: Software Setup

3 Use PuTTY to Access Raspberry Pi
After changing the Raspberry Pi’s IP address and restarting, you can connect using PuTTY via 192.168.1.101.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
PuTTY not only provides SSH but also includes pscp and psftp for easy file transfer between the main computer and Raspberry Pi.
4 Use pscp to Transfer Files
i. Upload files/folders from the Windows system to the Raspberry Pi
# Copy the entire directorypscp -r d:\raspi [email protected]:/home/pi # Copy a single filepscp d:\raspi\test.txt [email protected]:/home/pi 
ii. Download files/folders from the Raspberry Pi to the Windows system
# Copy the entire directorypscp -r [email protected]:/home/pi d:\raspi# Copy a single filepscp [email protected]:/home/pi/test.txt d:\raspi
Detailed parameters:
Usage: pscp [options] [user@]host:source target      pscp [options] source [other sources...] [user@]host:target      pscp [options] -ls [user@]host:specific file options:  -V        Display version information and exit  -pgpfp    Display PGP key fingerprint and exit  -p        Preserve file attributes  -q        Quiet mode, do not display status information  -r        Recursively copy directories  -v        Display detailed information  -load session name  Load saved session information  -P port   Connect to the specified port  -l username Use the specified username to connect  -pw password  Log in with the specified password  -1 -2     Force use of SSH protocol version  -4 -6     Force use of IPv4 or IPv6 version  -C        Allow compression  -i key   Key file for authentication  -noagent  Disable Pageant authentication agent  -agent    Enable Pageant authentication agent  -hostkey aa:bb:cc:...            Manually specify the host key (may repeat)  -batch    Disable all interactive prompts  -proxycmd command            Use 'command' as the local proxy  -unsafe   Allow server-side wildcard (dangerous operation)  -sftp     Force use of SFTP protocol  -scp      Force use of SCP protocol  -sshlog file  -sshrawlog file  Record detailed protocol logs to the specified file
5 Use PSFTP to Transfer Files
psftp is similar to pscp, faster but does not display a progress bar.
psftp 192.168.1.101# Enter pi and raspberry# Upload fileput d:\raspi\test.txt # Download fileget /home/pi/test.txt
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
Common commands:cd Change directory. ls View directory files. get  [fileName] Download the file to the current local directory. get -r  [dir] Recursively download all files from a remote directory to the current local directory, i.e., download the folder. put [fileName] Upload the file to the remote current directory. put -r [dir] Recursively upload all files from a local folder to the remote target directory, i.e., upload the folder. delete [fileName/dir] Delete a file or folder
6 Use WinSCP to Transfer Files
Both of the above methods are command-line based. You can also install WinSCP, establish a connection, and transfer files more conveniently like a file manager.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
Update Raspberry Pi System
1 Change Mirror Source
Due to well-known reasons, it is impossible to access the Raspberry Pi update server from within the country, so we can change to a domestic mirror site as an update point.
sudo nano /etc/apt/sources.list
Comment out the first line, then add the Tsinghua source, the file content is as follows:
#deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi# Uncomment line below then 'apt-get update' to enable 'apt-get source'#deb-src http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpideb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpideb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
Press ctrl + o to write, then press ctrl + x to exit.
2 Change pip Source
Also change pip to Aliyun and Douban sources.
sudo mkdir ~/.pipls .pipsudo nano pip.conf
Write the following content into the configuration file:
[global]timeout = 10index-url =  http://mirrors.aliyun.com/pypi/simple/extra-index-url= http://pypi.douban.com/simple/[install]trusted-host=    mirrors.aliyun.com    pypi.douban.com
3 Connect to Official Source Using VPN
Domestic mirrors update asynchronously, and sometimes certain third-party library files cannot be accessed, especially raw resources on GitHub. Using a VPN will be more convenient. Here, I will take expressVPN as an example, and other VPNs are similar.
i. Upload the installation package to the Raspberry Pi’s Download directory using WinSCP;
i. After entering the directory, install the VPN client;
cd Downloads/sudo dpkg -i expressVPN_xv_2.6.3.3-1_armhf.deb
i. Activate the software
expressvpn activate
i. Connect to VPN
expressvpn connect  # Connect to VPNexpressvpn list  # View available server listexpressvpn status  # View connection statusexpressvpn disconnect  # Disconnect
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
4 Update System
After solving the connection issue, you can proceed with the system update.
sudo apt-get updatesudo apt-get upgradepip3 install -U pip
5 Install Chinese Font and Input Method
# Install Chinese fontsudo apt-get install fonts-wqy-zenhei# Install Chinese input methodfcitx and Google Pinyin input methodsudo apt-get install fcitx fcitx-googlepinyin fcitx-module-cloudpinyin fcitx-sunpinyin
After installation, restart to take effect, and switch to Chinese input method using ctrl+space.
Tool Software
1 Linux_Reader
You can directly read files in the Raspberry Pi MicroSD under Windows.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
2 Teamviewer
VNC requires being on the same local area network to connect remotely. Installing Teamviewer allows access to the Raspberry Pi from anywhere via the Internet. Especially in AIoT applications, it allows access to devices via 4G networks.
i. Install TeamViewer_Setup.exe client under Windows;
i. Upload the installation package teamviewer-host_15.9.4_armhf.deb to the Downloads directory on the Raspberry Pi.
cd Downloads/sudo dpkg -i teamviewer-host_15.9.4_armhf.deb
If there is an error due to missing dependencies for teamviewer-host, fix it
sudo apt-get -f installsudo apt-get install gdebisudo gdebi teamviewer-host_armhf.deb
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
Accept the agreement, and you will see the teamviewer icon in the upper right corner of the desktop. Click to easily access, then log in to your account to connect remotely.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
3 Win32DiskImager
In addition to the official burning tool from the Raspberry Pi website, you can also use Win32DiskImager to write images, which is small, convenient, and portable without installation.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
This software can not only write but also export images, which makes it easier for us to save multiple development environments, especially since the process of configuring OpenCV and PyTorch is cumbersome, avoiding the need to redo the system and configurations each time.
System Backup
The size of the file backed up by Win32DiskImager depends on the size of the MicroSD, so it is recommended to use the dd command under Linux to back up and then compress the image.
1 Check Device Name
Use lsblk to check the disk letter of the MicroSD, usually sdb.
lsblk
2 dd Command to Backup SD Card
Use the dd command to copy the SD card data and save it as an img image file.
sudo dd if=/dev/sdb of=./rpi_backup.img bs=8M
3 Compress Backup Image
Use PiShrink to reduce the image size
git clone https://github.com/Drewsif/PiShrink.gitcd PiShrinkchmod +x pishrink.shsudo mv pishrink.sh /usr/local/binsudo pishrink.sh -s rpi_backup.img new_rpi_backup.img
More parameters are as follows:
Usage: $0 [-adhrspvzZ] imagefile.img [newimagefile.img] -s         Don't expand filesystem when image is booted the first time  -v         Be verbose  -r         Use advanced filesystem repair option if the normal one fails  -z         Compress image after shrinking with gzip  -Z         Compress image after shrinking with xz  -a         Compress image in parallel using multiple cores  -p         Remove logs, apt archives, dhcp leases and ssh hostkeys  -d         Write debug messages in a debug log file
4 SD Card Clone Backup
Use Win32DiskImager (backup file is large and fast) or dd command followed by compression (takes longer but results in smaller files). Another method is to directly clone the SD card in the Raspberry Pi. Remember to buy a few extra MicroSD cards as recommended in the hardware section; this will come in handy.
# Check disk space and SD card mount informationdf -h 
Insert the blank card via a USB card reader into the Raspberry Pi, then go to the main menu –> Accessories –> SD Card Copier, select /dev/mmcblk0 as the source, choose /dev/sda as the target disk, and click Start to begin copying.
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
Insert another Raspberry Pi directly to use, or insert two USB card readers to restore images from each other.
Software Download
Building Deep Learning Applications with Raspberry Pi 4b: Software Setup
The related software mentioned above has been uploaded to Baidu Cloud. Reply “rpi02” in the public account backend to get the download link.

Next Article

We will start compiling OpenCV, PyTorch

and installing development environments like TensorFlow,

stay tuned…

Building Deep Learning Applications with Raspberry Pi 4b: Software Setup

Leave a Comment

Your email address will not be published. Required fields are marked *