Huawei's open-source self-developed AI framework Shengsi MindSpore application case: ResNet50 transfer learning

Pix2Pix overview
Pix2Pix is ​​a deep learning image conversion model based on conditional generative adversarial networks (cGAN, Condition Generative Adversarial Networks). This model was proposed by Phillip Isola and other authors on CVPR in 2017. It can realize semantic/label to real Conversion of images, grayscale images to color images, aerial images to maps, day to night, and line drawings to physical images. Pix2Pix is ​​a classic of applying cGAN to supervised image-to-image translation, which includes two models: generator and discriminator.
Traditionally, although the goal of such tasks is the same to predict pixels from pixels, each is handled by a separate dedicated machine. And the network used by Pix2Pix as a general framework, using the same architecture and objectives, only trained on different data, can get satisfactory results, in view of this many people have published their own artwork using this network .

If you are interested in MindSpore, you can follow the Shengsi MindSpore community

insert image description here

insert image description here

1. Environmental preparation

1. Enter ModelArts official website

The cloud platform helps users quickly create and deploy models, and manage full-cycle AI workflows. Select the following cloud platform to start using Shengsi MindSpore, get the installation command , install MindSpore2.0.0-alpha version, and enter the ModelArts official website in the Shengsi tutorial

insert image description here

Choose CodeLab below to experience it immediately

insert image description here

Wait for the environment to be built

insert image description here

2. Use CodeLab to experience Notebook instances

Download the NoteBook sample code , ResNet50 transfer learning , .ipynbas a sample code

insert image description here

Select ModelArts Upload Files to upload .ipynbfiles

insert image description here

insert image description here

insert image description here

Select the Kernel environment

insert image description here

Switch to the GPU environment, switch to the first time-limited free

insert image description here

Enter Shengsi MindSpore official website , click on the installation above

insert image description here

get install command

insert image description here

Back in the Notebook, add the command before the first block of code
insert image description here

conda update -n base -c defaults conda

insert image description here

Install MindSpore 2.0 GPU version

conda install mindspore=2.0.0a0 -c mindspore -c conda-forge

insert image description here

install mindvision

pip install mindvision

insert image description here

2. Data preparation

download dataset

Download the dog and wolf classification data set used in the case . The images in the data set come from ImageNet. Each classification has about 120 training images and 30 verification images. Use the download interface to download the dataset, and automatically decompress the downloaded dataset to the current directory.

insert image description here

insert image description here

installdownloaddownload

pip install download

insert image description here

from download import download

dataset_url = "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"

download(dataset_url, "./datasets-Canidae", kind="zip")

The directory structure of the dataset is as follows:

datasets-Canidae/data/
└── Canidae
    ├── train
    │   ├── dogs
    │   └── wolves
    └── val
        ├── dogs
        └── wolves

insert image description here

3. Load the dataset

The wolfdog dataset is extracted from the ImageNet classification dataset, and the mindspore.dataset.ImageFolderDataset interface is used to load the dataset and perform related image enhancement operations.

First the execution procedure defines some inputs:

insert image description here
insert image description here

Dataset visualization

The return value of the training data set loaded from the mindspore.dataset.ImageFolderDataset interface is a dictionary, and the user can create a data iterator through the create_dict_iterator interface, and use the next iteration to access the data set. In this chapter, batch_size is set to 18, so you can use next to get 18 images and label data at a time.

insert image description here
insert image description here

4. Training model

This chapter uses the ResNet50 model for training. After building the model framework, download the ResNet50 pre-trained model and load the weight parameters into the network by setting the pretrained parameter to True .

Build a Resnet50 network

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Model fine-tuning

Since the pre-training model in ResNet50 is classified for 1000 categories in the ImageNet dataset, only wolf and dog are classified in this chapter, so it is necessary to reset the classifier in the pre-training model and then re-fine-tune the network.

insert image description here
insert image description here

training and evaluation

Train and evaluate the network, and after the training is completed, save the ckpt file (resnet50-best.ckpt) with the highest evaluation accuracy to /BestCheckpoint in the current path. The save path and ckpt file name can be adjusted by yourself.

insert image description here

insert image description here

insert image description here

Visualize model predictions

Define the visualize_mode function to visualize model predictions.
insert image description here
insert image description here

Fixed features for training

When training with fixed features, you need to freeze all network layers except the last one. Freeze parameters by setting requires_grad == False so that gradients are not computed in backpropagation.
insert image description here
insert image description here
insert image description here

Visualize model predictions

Use the best.ckpt file obtained with fixed features to predict the wolf and dog image data of the validation set. If the prediction font is blue, the prediction is correct; if the prediction font is red, the prediction is wrong.
insert image description here
to be continued

Guess you like

Origin blog.csdn.net/qq_46207024/article/details/130480195