colab中的变量怎么读取_Fizyr Retinanet在Colab中进行目标检测

colab中的变量怎么读取

by RomRoc

由RomRoc

带有Fizyr Retinanet的Google Colab中的对象检测 (Object Detection in Google Colab with Fizyr Retinanet)

Let’s continue our journey to explore the best machine learning frameworks in computer vision.

让我们继续我们的旅程,探索计算机视觉中最好的机器学习框架。

In the first article we explored object detection with the official Tensorflow APIs. The second article was dedicated to an excellent framework for instance segmentation, Matterport Mask R-CNN based on Keras.

第一篇文章中,我们探讨了使用官方Tensorflow API进行对象检测。 第二篇文章致力于一个很好的实例分割框架,基于Keras的Matterport Mask R-CNN。

In this article we examine Keras implementation of RetinaNet object detection developed by Fizyr. RetinaNet, as described in Focal Loss for Dense Object Detection, is the state of the art for object detection.The object to detect with the trained model will be my little goat Rosa.

在本文中,我们研究Fizyr开发的RetinaNet对象检测的Keras实现 。 RetinaNet,如密集物体检测的焦点损失中所述,是物体检测的最新技术。使用训练模型进行检测的物体将是我的小山羊Rosa。

The colab notebook and dataset are available in my Github repo.

我的Github存储库中提供了colab笔记本和数据集。

In this article, we go through all the steps in a single Google Colab netebook to train a model starting from a custom dataset.

在本文中,我们将在单个Google Colab上网本中完成所有步骤,以训练从自定义数据集开始的模型。

We will keep in mind these principles:

我们将牢记以下原则:

  • illustrate how to make the annotation dataset

    说明如何制作注释数据集
  • describe all the steps in a single Notebook

    在一个笔记本中描述所有步骤
  • use free software, Google Colab and Google Drive, so it’s based exclusively on free cloud resources

    使用免费软件,Google Colab和Google云端硬盘,因此它完全基于免费的云资源

At the end of the article you will be surprised by the simplicity of use and the good results we will obtain through this object detection framework.

在本文的结尾,您将对使用的简单性以及通过此对象检测框架获得的良好结果感到惊讶。

Despite its ease of use, Fizyr is a great framework, also used by the winner of the Kaggle competition “RSNA Pneumonia Detection Challenge”.

尽管使用方便,但Fizyr是一个很好的框架, Kaggle竞赛 “ RSNA肺炎检测挑战赛”的获胜者也使用了该框架。

制作数据集 (Making the dataset)

We start by creating annotations for the training and validation dataset, using the tool LabelImg. This excellent annotation tool let you quickly annotate the bounding boxes of the objects to train the machine learning model.

我们首先使用工具LabelImg为训练和验证数据集创建注释。 这个出色的注释工具可让您快速注释对象的边界框,以训练机器学习模型。

LabelImg creates annotations in PascalVoc format, so we need to convert annotations to Fizyr format:

LabelImg以PascalVoc格式创建注释,因此我们需要将注释转换为Fizyr格式:

  • create a zip file containing training dataset images and annotations with the same filename (check my example dataset in Github)

    创建一个zip文件,其中包含具有相同文件名的训练数据集图像和注释(请在Github中查看我的示例数据集)
objdet_dataset.zip|- img1.jpg|- img1.xml|- img2.jpg|- img2.xml...
  • Upload zip file in Google Drive, get Drive file id, and substitute the DATASET_DRIVEID value

    在Google云端硬盘中上传zip文件,获取云端硬盘文件ID,然后替换DATASET_DRIVEID值
  • Run cell that iterates over the xml files and creates annotations.csv file

    运行在xml文件上迭代并创建annotations.csv文件的单元格

Note: you can see my answer on Stackoverflow to get the Drive file id.

注意:您可以在Stackoverflow上查看我的答案以获取驱动器文件ID。

模型训练 (Model training)

Model training is the core of the notebook. Fizyr offers various parameters, described in Github, to run and optimize this step.

模型训练是笔记本的核心。 Fizyr提供了各种参数(如Github中所述 )来运行和优化此步骤。

It’s a good option to start from a pretrained model instead of training a model from scratch. Fizyr released a model based on ResNet50 architecture, pretrained on Coco dataset.

从预训练的模型开始而不是从头开始训练模型是一个很好的选择。 Fizyr发布了基于ResNet50架构的模型,该模型在Coco数据集上进行了预训练。

URL_MODEL = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.0/resnet50_coco_best_v2.1.0.h5'

We can even use our pretrained model, and continue the training from it. This option is particularly useful to train for some epochs, so save it in Google Drive, and later restart the training from the saved model. In this way we can bypass the 12-hour execution limit in Colab, and we can train the model for many epochs.

我们甚至可以使用我们的预训练模型,并从中继续训练。 此选项对于某些时期的训练特别有用,因此请将其保存在Google云端硬盘中,然后再从保存的模型中重新开始训练。 通过这种方式,我们可以绕过Colab中12小时的执行限制,并且可以针对许多时期训练模型。

From my tests, a high value of batch_size and steps offers better results, but they greatly increase the execution time of each epoch.

根据我的测试,batch_size和steps的值较高可以提供更好的结果,但是它们大大增加了每个纪元的执行时间。

We can start training from our custom dataset with:

我们可以从我们的自定义数据集中开始训练:

!keras_retinanet/bin/train.py --freeze-backbone --random-transform --weights {PRETRAINED_MODEL} --batch-size 8 --steps 500 --epochs 10 csv annotations.csv classes.csv

Let’s analyze each argument passed to the script train.py.

让我们分析传递给脚本train.py的每个参数。

  • freeze-backbone: freeze the backbone layers, particularly useful when we use a small dataset, to avoid overfitting

    冻结主干:冻结主干层,当我们使用较小的数据集时特别有用,以避免过度拟合
  • random-transform: randomly transform the dataset to get data augmentation

    random-transform:随机变换数据集以获得数据扩充
  • weights: initialize the model with a pretrained model (your own model or one released by Fizyr)

    权重:使用预先训练的模型(您自己的模型或Fizyr发布的模型)初始化模型
  • batch-size: training batch size, higher value gives smoother learning curve

    批处理大小:训练批处理大小,更高的值可提供更平滑的学习曲线
  • steps: number of steps for epochs

    步骤:纪元的步骤数
  • epochs: number of epochs to train

    纪元:要训练的纪元数
  • csv: annotations files generated by the script above

    csv:以上脚本生成的注释文件

The training process output contains a description of layers and loss metrics during training, and as you can see, loss metrics decrease during each epoch:

训练过程的输出包含训练期间的层和损耗度量的描述,如您所见,损耗度量在每个时期都会减少:

Using TensorFlow backend....Layer (type)                    Output Shape         Param #     Connected toinput_1 (InputLayer)            (None, None, None, 3 0padding_conv1 (ZeroPadding2D)   (None, None, None, 3 0           input_1[0][0]                    ...Total params: 36,382,957Trainable params: 12,821,805Non-trainable params: 23,561,152NoneEpoch 1/10500/500 [==============================] - 1314s 3s/step - loss: 1.0659 - regression_loss: 0.6996 - classification_loss: 0.3663Epoch 2/10500/500 [==============================] - 1296s 3s/step - loss: 0.6747 - regression_loss: 0.5698 - classification_loss: 0.1048Epoch 3/10500/500 [==============================] - 1304s 3s/step - loss: 0.5763 - regression_loss: 0.5010 - classification_loss: 0.0753
Epoch 3/10500/500 [==============================] - 1257s 3s/step - loss: 0.5705 - regression_loss: 0.4974 - classification_loss: 0.0732

推理 (Inference)

The last step performs inference of test images with the trained model.The Fizyr framework allows us to perform inference using CPU, even if you trained the model with GPU. This feature is important in typical production environments, where people usually opt for less expensive hardware infrastructures for inference, without GPUs.

最后一步是使用经过训练的模型执行测试图像的推断。即使您使用GPU训练了模型,Fizyr框架也允许我们使用CPU进行推断。 此功能在典型的生产环境中很重要,在该环境中,人们通常选择较便宜的硬件基础架构来进行推理,而无需使用GPU。

Let’s examine the following lines in detail:

让我们详细研究以下几行:

model_path = os.path.join('snapshots', sorted(os.listdir('snapshots'), reverse=True)[0])print(model_path)
# load retinanet modelmodel = models.load_model(model_path, backbone_name='resnet50')model = models.convert_model(model)

The first line sets the model file as the last model generated by the training process in /snapshots directory. Then the model is loaded from the filesystem and converted to run inference.

第一行将模型文件设置为/ snapshots目录中训练过程生成的最后一个模型。 然后,从文件系统加载模型并转换为运行推理。

You can change the values of THRES_SCORE, which represents the confidence threshold to show an object detection.

您可以更改THRES_SCORE的值,该值表示显示对象检测的置信度阈值。

结论 (Conclusions)

We went through the complete journey to make object detection with Fizyr implementation of RetinaNet. We created a dataset, trained a model, and ran inference (here is my Github repo for the notebook and dataset).

我们经历了使用RetinaNet的Fizyr实现进行对象检测的完整过程。 我们创建了一个数据集,训练了一个模型,然后进行了推断( 是我的笔记本和数据集的Github存储库)。

I was impressed by the following aspects of this excellent framework:

这个出色的框架在以下方面给我留下了深刻的印象:

  • this framework is easy to use to get good inference, even without much customization

    即使没有太多定制,该框架也易于使用以获得良好的推断

  • it was simple to transform annotations to Fizyr’s dataset format, compared to other frameworks.

    与其他框架相比, 将注释转换为Fizyr的数据集格式很简单

In general Fizyr is a good choice to start an object detection project, in particular if you need to quickly get good results.

通常,Fizyr是启动对象检测项目的不错选择,尤其是在需要快速获得良好结果的情况下。

If you enjoyed this article, leave a few claps, it will encourage me to explore further machine learning opportunities :)

如果您喜欢这篇文章,请留下一些鼓掌,它会鼓励我探索更多的机器学习机会:)

翻译自: https://www.freecodecamp.org/news/object-detection-in-colab-with-fizyr-retinanet-efed36ac4af3/

colab中的变量怎么读取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值