Facts speak louder than words, can Apple MacOs play machine/deep (ml/dl) learning (Python3.10/Tensorflow2)

There are rumors that the MacOs system is not suitable for machine (ml) learning and deep (dl) learning. This is a solid stereotype, as if someone said that girls are not suitable for programming. Nowadays, whether it is the MPS mode of the Pytorch framework or the latest Tensorflow2 framework, it is already possible to use the GPU graphics card device in the Mac system with the M1/M2 chip without restraint. This time we will share how to install it on the Apple MacOS system. And configure the Tensorflow2 framework (CPU/GPU).

Tensorflow2 deep learning environment installation and configuration

First of all, you don’t need any virtual environment, just install Python3.10 locally. ) to install and configure the Python3.10 development environment , so I won’t go into details here.

Then install the Tensorflow ontology:

pip3 install tensorflow-macos

Here the system will automatically select the Tensorflow installation package of the current Python version:

➜  ~ pip install tensorflow-macos  
Collecting tensorflow-macos  
  Downloading tensorflow_macos-2.12.0-cp310-cp310-macosx_12_0_arm64.whl (200.8 MB)  
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 200.8/200.8 MB 4.7 MB/s eta 0:00:00

The size of the installation package is about 200 megabytes. If you can’t download it, you can choose to download the python3.10-based installation package directly from the pip official website: pypi.org/project/tensorflow-macos/#files

Then directly drag the whl file to the terminal to install it.

Then install Tensorflow's GPU plugin: tensorflow-metal, which is a TensorFlow backend that uses Apple's Metal graphics API to accelerate neural network calculations. Metal is a high-performance graphics and computing API designed specifically for the GPU of Apple devices, enabling faster neural network computing. Using tensorflow-metal can significantly improve the performance of running TensorFlow on Apple devices, especially when using Apple silicon-based devices such as Macs M1 and M2.

pip3 install --user tensorflow-metal

Note that the installation command here must carry the –user parameter, otherwise this error may be reported:



Non-OK-status: stream_executor::MultiPlatformManager::RegisterPlatform( std::move(cplatform)) status: INTERNAL: platform is already registered with name: "METAL"


Once installed, run the command in the Python terminal:

import tensorflow  
tensorflow.config.list_physical_devices()

The program returns:

>>> import tensorflow  
>>> tensorflow.config.list_physical_devices()  
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

It can be seen that the physical device used by Tensorflow for computing supports both the CPU and the GPU, that is, the graphics card.

Next, write a complete test script test.py:

import sys  
import tensorflow.keras  
import pandas as pd  
import sklearn as sk  
import scipy as sp  
import tensorflow as tf  
import platform  
print(f"Python Platform: {platform.platform()}")  
print(f"Tensor Flow Version: {tf.__version__}")  
print(f"Keras Version: {tensorflow.keras.__version__}")  
print()  
print(f"Python {sys.version}")  
print(f"Pandas {pd.__version__}")  
print(f"Scikit-Learn {sk.__version__}")  
print(f"SciPy {sp.__version__}")  
gpu = len(tf.config.list_physical_devices('GPU'))>0  
print("GPU is", "available" if gpu else "NOT AVAILABLE")

Here are the commonly used libraries and version numbers in deep learning scenarios:

➜  chatgpt_async git:(main) ✗ /opt/homebrew/bin/python3.10 "/Users/liuyue/wodfan/work/chatgpt_async/tensof_test.py"  
Python Platform: macOS-13.3.1-arm64-arm-64bit  
Tensor Flow Version: 2.12.0  
Keras Version: 2.12.0  
  
Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 (clang-1400.0.29.202)]  
Pandas 1.5.2  
Scikit-Learn 1.2.0  
SciPy 1.10.0  
GPU is available

At a glance, in the latest macOS-13.3.1 system, there is no problem with playing Tensorflow2.1 based on Python3.10.9.

At this point, Tensorflow2 is configured.

Tensorflow framework GPU and CPU test

Why must Tensorflow support GPU? GPUs, or Graphics Processing Units, are similar to CPUs in that they have many cores, allowing them to perform faster calculations simultaneously (parallelism). This feature is very suitable for performing large-scale mathematical calculations, such as computing image matrices, computing eigenvalues, determinants, and so on.

In short, the GPU can run code in parallel and obtain concise results, and because it can handle high-intensity calculations, it can obtain calculation results faster than the CPU.

Here we test through the CIFAR-10 project. The TensorFlow CIFAR-10 project is a classic computer vision project designed to train a model that can classify images in the CIFAR-10 dataset. The CIFAR-10 dataset contains 60,000 color images of 32x32 pixels, divided into 10 categories, each category contains 6,000 images. The goal of this project is to train a deep neural network model capable of accurately classifying these images:

import tensorflow as tf  
from tensorflow import keras  
import numpy as np  
import matplotlib.pyplot as plt  
(X_train, y_train), (X_test, y_test) = keras.datasets.cifar10.load_data()  
  
X_train_scaled = X_train/255  
X_test_scaled = X_test/255  
# one hot encoding labels  
y_train_encoded = keras.utils.to_categorical(y_train, num_classes = 10, dtype = 'float32')  
y_test_encoded = keras.utils.to_categorical(y_test, num_classes = 10, dtype = 'float32')  
  
def get_model():  
    model = keras.Sequential([  
        keras.layers.Flatten(input_shape=(32,32,3)),  
        keras.layers.Dense(3000, activation='relu'),  
        keras.layers.Dense(1000, activation='relu'),  
        keras.layers.Dense(10, activation='sigmoid')      
    ])  
    model.compile(optimizer='SGD',  
              loss='categorical_crossentropy',  
              metrics=['accuracy'])  
    return model

First test the CPU performance:

%%timeit -n1 -r1  
# CPU  
with tf.device('/CPU:0'):  
    model_cpu = get_model()  
    model_cpu.fit(X_train_scaled, y_train_encoded, epochs = 10)

This code uses the %%timeit -n1 -r1 magic command to test the time to train the model on the CPU. -n1 means to run only once, and -r1 means to run only one round. If these parameters are not specified, it is run multiple times and the average is calculated. /CPU:0 refers to the first CPU (or the only CPU if the computer has only one CPU).

Here use the get_model() function to obtain the model, use the model_cpu.fit() method to train the model on the CPU, use X_train_scaled and y_train_encoded as input data, and train within 10 epochs. Finally, use the %%timeit command to test the time it takes to train the model in order to compare the performance of different devices.

The program returns:

50000/50000 [==========================] - 80s 2ms/sample  
  
14min 9s

It takes 14 minutes.

Then test the GPU performance:

%%timeit -n1 -r1  
# GPU  
with tf.device('/GPU:0'):  
    model_gpu = get_model()  
    model_gpu.fit(X_train_scaled, y_train_encoded, epochs = 10)

The program returns:

50000/50000 [==========================] - 11s 227us/sample  
  
1min 55s

After a minute or so, it becomes clear that training the model on the GPU is faster than on the CPU because the GPU can handle multiple tasks simultaneously.

epilogue

Apple's MacOs system can undertake deep learning tasks, but it is an indisputable fact that the computing power is still not as good as other platforms equipped with N cards. Yes, a better choice is RTX3090, or even 4090, but the price of an RTX4090 graphics card is about 1,500 knives, which also means that the CPU, memory, motherboard and power supply have to be bought separately, and a Mac book air with m2 chip What is the price?

Guess you like

Origin blog.csdn.net/zcxey2911/article/details/130082240