In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article is to share with you about how to use the code to achieve AI image recognition, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
With the emergence of powerful computer chips such as NVIDIA GPU and top depth learning algorithms for image recognition, such as AlexNet algorithm created by Alex Krizhevsky in 2012, ResNet algorithm created by Kaeming He in 2015, SqueezeNet algorithm created by Forrest Landola in 2016, DenseNet algorithm created by Gao Huang in 2016, etc., it is possible for custom artificial intelligence models to identify other similar things by learning things in a group of pictures.
Training an artificial intelligence model that can recognize things in pictures involves a lot of professional knowledge of applied mathematics and deep learning libraries, not to mention spending a lot of time and pressure to write the code to implement the algorithm and match it with the image. This is the problem we are trying to solve.
Our team at AI Common has built a Python library called ImageAI that can train artificial intelligence models to recognize any object in just five lines of code. This library is built so that students, developers, researchers and other professionals can use only five lines of code to build systems and applications with state-of-the-art computer vision functions. Now, let's start building your first image recognition artificial intelligence model.
Now that you know how to prepare a picture dataset for training artificial intelligence models, we will continue to guide you through the use of ImageAI to train a person's industrial intelligence model to identify occupations.
First of all, you must download the IdenProf dataset package through this link. You can also use the link below to view all the details and sample results of the artificial intelligence model used to identify occupations.
Because training artificial intelligence models requires high-performance computer systems, I strongly recommend that you make sure that the computers used for model training have NVIDIA GPU. You can also use Google Colab (Google Lab) to do this experiment, which provides a free NVIDIA K80 GPU.
Next you must install ImageAI and its dependent modules.
1) download and install Pyhton3 from the Python official website
Https://python.org
2) use pip to install the following dependency packages
I. Tensorflow
Pip install tensorflow
ii. Numpy
Pip install numpy
iii. SciPy
Pip install scipy
iv. OpenCV
Pip install opencv-python
V. Pillow
Pip install pillow
vi. Matplotlib
Pip install matplotlib
vii. H5py
Pip install h6py
viii. Keras
Pip install keras
ix. ImageAI
Pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/imageai-2.0.2-py3-none-any.whl
Create a new python file, such as "FirstTraining.py", copy the IdenProf dataset package to your Python file directory and extract it. Then copy the following code to the python file. (such as FirstTraining.py)
From imageai.Prediction.Custom import ModelTrainingmodel_trainer = ModelTraining () model_trainer.setModelTypeAsResNet () model_trainer.setDataDirectory ("idenprof") model_trainer.trainModel (num_objects=10, num_experiments=200, enhance_data=True, batch_size=32, show_network_summary=True)
It is! That's all the code you need to train artificial intelligence models. Before you run it, let's explain this code.
In the first line, import ImageAI's model training class. In the second line, create an instance of the model training class. In the third line, we set the model type to ResNet (there are four model types to choose from, namely SqueezeNet, ResNet, InceptionV3, and DenseNet). In the fourth line, set the data directory (dataset directory) for the dataset folder you unzipped. Then on the fifth line, we call the trainModel function and assign values to the following parameters:
Number_objects: the number of classifications for different occupations in the dataset
Num_experiments: for maximum accuracy, the number of times the model learns all the pictures in the dataset
Enhance_data (optional): used to tell the model trainer to create copies of modified images in the dataset to ensure maximum accuracy
Batch_size: the number of pictures learned by the trainer at a time until all the pictures have been learned.
Show_network_summary (optional): displays the type structure of your current training artificial intelligence model
Now you can start running the Python file and start training. When the training begins, you can see the following results:
Total params: 23608202
Trainable params: 23555082
Non-trainable params: 53120
_ _
Using Enhanced Data Generation
Found 4000 images belonging to 4 classes.
Found 800 images belonging to 4 classes.
JSON Mapping for the model classes saved to C:\ Users\ User\ PycharmProjects\ FirstTraining\ idenprof\ json\ model_class.json
Number of experiments (Epochs): 200
Epoch 1/100
1CM280 [>..]-ETA: 52s-loss: 2.3026-acc: 0.2500
2CM280 [>..]-ETA: 52s-loss: 2.3026-acc: 0.2500
3Universe 280 [>..]-ETA: 52s-loss: 2.3026-acc: 0.2500
..
..
..
279 loss 280 [= >..]-ETA: 1s-loss: 2.3097-acc: 0.0625Epoch 00000: saving model to C:\ Users\ User\ PycharmProjects\ FirstTraining\ idenprof\ models\ model_ex-000_acc-0.100000.h6
280pm 280 [=]-51s-loss: 2.3095-acc: 0.0600-val_loss: 2.3026-val_acc: 0.1000
Let's explain the above in detail:
1. The phrase "JSON Mapping for the model classes saved to C:\ Users\ User\ PycharmProjects\ FirstTraining\ idenprof\ json\ model_class.json" indicates that the model trainer has been saved as a JSON file and can use custom image prediction classes to identify other images (which will be explained later).
The line 2.Epoch 1Acer 200 indicates that the network has performed the first of 200 trainings with a target value of 200.
3.1pm 280 [> … ..] - ETA: 52s - loss: 2.3026 - acc: 0.2500 this line indicates the batch that has been trained in the current experiment.
4.Epoch 00000: saving model to C:\ Users\ User\ PycharmProjects\ FirstTraining\ idenprof\ models\ model_ex-000_acc-0.100000.h6 this line represents the model saved after the current training. Ex_000 represents the current phase of the experiment, while acc0.100000 and valacc represent the model accuracy after the current experiment (the highest accuracy is 1.0). This result helps us to know which image prediction model performs best. After training the model, you can use the "CustomImagePrediction" category of the most accurate model for image prediction.
In case you lack NVIDIA GPU and cannot train artificial intelligence models, for the purpose of this tutorial, we provide an artificial intelligence model trained with IdenProf datasets, which you can use to make career predictions of images in the dataset.
The model has achieved 79% accuracy after 61 training sessions. Click this link to download the model.
Similarly, if you don't have model training, download the JSON file for the IdenProf model through the link. You can then follow the following instructions and use the model for image recognition.
Next, create a Python file and name it like FirstCustomImageRecognition.py, copy the AI model you downloaded or the model you trained with the highest accuracy, and copy it to the directory where your new Python file is located. Also copy the downloaded or trained JSON file and copy it to the directory where the new Python file is located.
Copy one or more pictures that belong to the occupational category in the IdenProf dataset to the directory where the new Python file is located. Then copy the following code to your Python file.
From imageai.Prediction.Custom import CustomImagePredictionimport osexecution_path = os.getcwd () prediction = CustomImagePrediction () prediction.setModelTypeAsResNet () prediction.setModelPath ("idenprof_061-0.7933.h6") prediction.setJsonPath ("idenprof_model_class.json") prediction.loadModel (num_objects=10) predictions, probabilities = prediction.predictImage ("image.jpg", result_count=3) for eachPrediction, eachProbability in zip (predictions, probabilities): print (eachPrediction,: ", eachProbability)
Please look at the sample pictures and results below.
Isn't it easy! Now let's explain the code above that produces this prediction.
The first and second lines of code import the CustomImagePrediction class (custom image prediction class) and os class for image recognition in ImageAI, respectively. The third line creates a variable and assigns a path that contains a Python file (such as a FirstCustomImageRecognition.py file) and a downloaded or trained ResNet model file.
Above, we create an instance of the ImagePrediction () class in line 4, and then set the model type of the prediction object to ResNet by calling .setModelTypeAsResNet () in line 5. Then in line 6, we set the model path to the artificial intelligence model path copied to the project folder (idenprof_061-0.7933.h6). On line 7, we set the path to the JSON file and load the model on line 8. Finally, the image is predicted and the result is printed to the command line.
The above is how to use the code to achieve AI image recognition, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.