make_image_classifier 分类模型

本文指导如何使用make_image_classifier工具,通过TensorFlow和TensorFlow Hub创建一个基础的图像分类模型,从本地图片库或TFFlowers数据集进行训练,并导出为TensorFlow Lite模型。涉及关键步骤如模块选择、预处理、训练参数设置和模型部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

make_image_classifier


 

安装

需安装tensorflow和tensorflow_hub libraries:

$ pip install "tensorflow~=2.0"
$ pip install "tensorflow-hub[make_image_classifier]~=0.6"

测试:

$ make_image_classifier --help

最好安装GPU 版本TF2  "tensorflow-gpu~=2.0".

基本用法

$ make_image_classifier \
  --image_dir my_image_dir \
  --tfhub_module https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4 \
  --image_size 224 \
  --saved_model_dir my_dir/new_model \
  --labels_output_file class_labels.txt \
  --tflite_output_file new_mobile_model.tflite \
  --summaries_dir my_log_dir

--image_dir 为图片目录,类似下图

my_image_dir
|-- cat
|   |-- a_feline_photo.jpg
|   |-- another_cat_pic.jpg
|   `-- ...
|-- dog
|   |-- PuppyInBasket.JPG
|   |-- walking_the_dog.jpeg
|   `-- ...
`-- rabbit
    |-- IMG87654321.JPG
    |-- my_fluffy_rabbit.JPEG
    `-- ...

Good training results need many images (many dozens, possibly hundreds per class).

Note: For a quick demo, omit --image_dir. This will download and use the "TF Flowers" dataset and train a model to classify photos of flowers as daisy, dandelion, rose, sunflower or tulip.

The --tfhub_module is the URL of a pre-trained model piece, or "module", on TensorFlow Hub. You can point your browser to the module URL to see documentation for it. This tool requires a module for image feature extraction in TF2 format. You can find them on TF Hub with this search.

Images are resized to the given --image_size after reading from disk. It depends on the TF Hub module whether it accepts only a fixed size (in which case you can omit this flag) or an arbitrary size (in which case you should start off by setting this to the standard value advertised in the module documentation).

Model training consumes your input data multiple times ("epochs"). Some part of the data is set aside as validation data; the partially trained model is evaluated on that after each epoch. You can see progress bars and accuracy indicators on the console.

After training, the given --saved_model_dir is created and filled with several files that represent the complete image classification model in TensorFlow's SavedModel format. This can be deployed to TensorFlow Serving.

If --labels_output_file is given, the names of the classes are written to that text file, one per line, in the same order as they appear in the predictions output by the model.

If --tflite_output_file is given, the complete image classification model is written to that file in TensorFlow Lite's model format ("flatbuffers"). This can be deployed to TF Lite on mobile devices. If you are not deploying to TF Lite, you can simply omit this flag.

If --summaries_dir is given, you can monitor your model training on TensorBoard. See this guide on how to enable TensorBoard.

If you set all the flags as in the example above, you can test the resulting TF Lite model with tensorflow/lite/examples/python/label_image.py by downloading that program and running on an image like

python label_image.py \
  --input_mean 0 --input_std 255 \
  --model_file new_mobile_model.tflite --label_file class_labels.txt \
  --image my_image_dir/cat/a_feline_photo.jpg  # <<< Adjust filename.

Advanced usage

Additional command-line flags let you control the training process. In particular, you can increase --train_epochs to train more, and set the --learning_rate and --momentum for the SGD optimizer.

Also, you can set --do_fine_tuning to train the TensorFlow Hub module together with the classifier.

There is other hyperparameters for regularization such as --l1_regularizer and --l2_regularizer, and for data augmentations such as --rotation_range and --horizontal_flip. Generally, the default values can give a good performance. You can find a full list of hyperparameters available in make_image_classifier.py and their default values in make_image_classifier_lib.py.

With tensorflow>=2.5 and tensorflow-hub>=0.12, you can control whether to read input with a tf.data.Dataset and use TF ops for preprocessing using the use_tf_data_input flag. Note that the shear data augmentation is not supported in this mode. If set to False, Keras' legacy Python ImageDataGenerator with numpy ops will be used for data augmentation and other preprocessing.

### 如何用 PyTorch 构建和训练 VGG16 分类模型 #### 模型构建 VGG16 是一种经典的卷积神经网络架构,其主要特点是通过堆叠多个 3×3 的小型卷积核来提取特征。以下是使用 PyTorch 实现 VGG16 的方法: ```python import torch.nn as nn import torch.utils.model_zoo as model_zoo class VGG(nn.Module): def __init__(self, features, num_classes=1000): super(VGG, self).__init__() self.features = features self.classifier = nn.Sequential( nn.Linear(512 * 7 * 7, 4096), nn.ReLU(True), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(True), nn.Dropout(), nn.Linear(4096, num_classes), ) def forward(self, x): x = self.features(x) x = x.view(x.size(0), -1) x = self.classifier(x) return x def make_layers(cfg, batch_norm=False): layers = [] in_channels = 3 for v in cfg: if v == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] else: conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1) if batch_norm: layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] else: layers += [conv2d, nn.ReLU(inplace=True)] in_channels = v return nn.Sequential(*layers) cfg_vgg16 = [ 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M' ] model = VGG(make_layers(cfg_vgg16)) ``` 上述代码展示了如何定义 VGG16 的基本结构[^1]。 #### 数据预处理 为了使输入数据适合 VGG16 模型,通常需要对图像进行标准化和其他必要的变换操作。以下是一个简单的数据预处理流程: ```python from torchvision import datasets, transforms transform_train = transforms.Compose([ transforms.Resize((224, 224)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) train_dataset = datasets.ImageFolder(root='path_to_train_data', transform=transform_train) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True) ``` 此部分描述了如何准备适用于 VGG16 的数据集[^2]。 #### 训练过程 在完成模型构建和数据预处理之后,可以开始训练模型。下面是一段典型的训练代码片段: ```python import torch.optim as optim criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) for epoch in range(num_epochs): running_loss = 0.0 for i, data in enumerate(train_loader, 0): inputs, labels = data[0].to(device), data[1].to(device) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() print(f'Epoch {epoch + 1}, Loss: {running_loss / (i + 1)}') ``` 这段代码实现了模型的训练逻辑。 #### 预测功能 当模型训练完成后,可以通过加载已保存的最佳权重来进行预测。以下是如何定义预测函数的一个例子: ```python def predict(image_path, model, device): image_transforms = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) img = Image.open(image_path).convert('RGB') img_tensor = image_transforms(img).unsqueeze_(0) img_tensor = img_tensor.to(device) with torch.no_grad(): output = model(img_tensor) _, predicted_class = torch.max(output, 1) return predicted_class.item() ``` 这是基于已有模型进行推理的部分[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值