@lone1
Profile
Registered: 3 months, 2 weeks ago
Using PyTorch Lightning and CoreML to achieve accelerated computing on Apple's neural network engine 1. Mission statement The task this time is the classification of 400 species of birds. From training to inference, the Macbook Air equipped with the M1 chip is used to complete, and finally deployed on the iPhone to realize Apple's one-stop experiment. The AI model development tool this time is PyTorch Lightning, which is a development tool based on PyTorch, and integrates the functions that engineers will use when developing models to help engineers develop more conveniently and quickly. A github link is attached at the end of the article . The following examples are based on GitHub content. Finally, I would like to remind that in the past, AI development tools were designed and developed using Nvidia graphics cards, but the Apple processor is based on the ARM architecture, which is quite different from the past. Not yet in place. As ARM chips become more and more mainstream, it is believed that the relevant functions of this part will become more and more perfect. 2. Data set preparation and description There are 400 species of bird data sets, the training data set has a total of 58388 pictures, and the verification data and test data sets have 2000 pictures respectively. All the picture sizes are (224, 244, 3) color pictures. 3. Training process Combining the functions of pytorch dataset and dataloader, the three dataloaders of train, validation and test are also integrated into this Module. class ImageDataset Description In the initialization phase, read the data set downloaded by Kaggle, birds.csv, there are 4 fields in csv, mainly to collect filepaths and class_index for subsequent image reading and training preparation. class_index : the label of the recorded image, the minimum is 0, the maximum is 399, a total of 400 classes filepaths : relative path of the record image labels : record image label name data set: Which one does the picture belong to: train, validation, or test In the training phase, the ImageDataset has been converted into an iterator through the dataloader, and the PIL image opening action is performed only when __getitem__ is called. dataloader explanation dataloader determines the batch size, whether to shuffle data during training Define the parameters during training, how many epochs to train, what device to use, whether to record the log, and finally use fit to train. Fit needs to pass in two parameters: 1. LightningModule class object, 2. LightningDataModule class object. Next, we need to use coremltools for model prediction, so we need to convert the trained model into a coremltools model, and the PyTorch model path needs to be modified. Use convert in coremltools to convert, where PyTorch must be a traced model, so you need to use torch.jit.trace() to convert the trained model, and then input it to coremltools. When using coremltools to convert the model, pay attention to the calculation method setting of the parameter compute_units. The detailed parameters are as follows. This example uses the ALL parameter. This example is an image classification task, and the input is an image matrix, so when using coremltools, the inputs parameter should be in the format of ct.ImageType.
Website: https://techtuba.com/
Forums
Topics Started: 0
Replies Created: 0
Forum Role: Participant