paddlehub迁移训练模型mobilenet_v2_imagenet进行图像分类,迁移学习模型后,进行预测时出错
Created by: jacbson
欢迎您反馈PaddleHub使用问题,非常感谢您对PaddleHub的贡献! 在留下您的问题时,辛苦您同步提供如下信息:
- 版本、环境信息 1)PaddleHub和PaddlePaddle版本:请提供您的PaddleHub和PaddlePaddle版本号,例如PaddleHub1.6.2,PaddlePaddle-gpu 1.7.2.post107 2)系统环境:请您描述系统类型,例如Linux/Windows/MacOS/,python版本 win10 python 3.7
- 复现信息:如为报错,请给出复现环境、复现步骤 用paddlehub迁移学习模型mobilenet_v2_imagenet后,进行预测时报错 import paddlehub as hub module = hub.Module(name="mobilenet_v2_imagenet") dataset = hub.dataset.DogCat() data_reader = hub.reader.ImageClassificationReader( image_width=module.get_expected_image_width(), image_height=module.get_expected_image_height(), images_mean=module.get_pretrained_images_mean(), images_std=module.get_pretrained_images_std(), dataset=dataset) config = hub.RunConfig( use_cuda=True, num_epoch=1, checkpoint_dir="cv_finetune_turtorial_demo", batch_size=32, eval_interval=50, strategy=hub.finetune.strategy.DefaultFinetuneStrategy()) input_dict, output_dict, program = module.context(trainable=True)
img = input_dict["image"]
feature_map = output_dict["feature_map"]
feed_list = [img.name]
task = hub.ImageClassifierTask( data_reader=data_reader, feed_list=feed_list, feature=feature_map, num_classes=dataset.num_labels, config=config) run_states = task.finetune_and_eval()
import os import numpy as np import paddle.fluid as fluid import paddlehub as hub module_map = { "resnet50": "resnet_v2_50_imagenet", "resnet101": "resnet_v2_101_imagenet", "resnet152": "resnet_v2_152_imagenet", "mobilenet": "mobilenet_v2_imagenet", "nasnet": "nasnet_imagenet", "pnasnet": "pnasnet_imagenet" }
module_name = module_map["mobilenet"] module = hub.Module(name = module_name) input_dict, output_dict, program = module.context(trainable=False) img = input_dict["image"] feature_map = output_dict["feature_map"] feed_list = [img.name] data_reader = hub.reader.ImageClassificationReader( image_width=module.get_expected_image_width(), image_height=module.get_expected_image_height(), images_mean=module.get_pretrained_images_mean(), images_std=module.get_pretrained_images_std(), dataset=None)
dataset = hub.dataset.DogCat() task = hub.ImageClassifierTask( data_reader=data_reader, feed_list=feed_list, feature=feature_map, num_classes=dataset.num_labels)
data = [ "test_img_dog.jpg", "test_img_cat.jpg" ]
predict_reader = data_reader.data_generator( phase="predict", batch_size=1, data=data)
label_dict = dataset.label_dict()
with fluid.program_guard(task.inference_program()): place = fluid.CPUPlace() exe = fluid.Executor(place) pretrained_model_dir = os.path.join("cv_finetune_turtorial_demo", "best_model") fluid.io.load_persistables(exe, pretrained_model_dir) feeder = fluid.DataFeeder(feed_list=feed_list, place=place) for index, batch in enumerate(predict_reader()): result, = exe.run( feed=feeder.feed(batch), fetch_list=[task.variable('probs')]) predict_result = np.argsort(result[0])[::-1][0] print("input %i is %s, and the predict result is %s" % (index+1, data[index], label_dict[predict_result]))
[2020-05-30 13:43:16,238] [ INFO] - Installing mobilenet_v2_imagenet module [2020-05-30 13:43:16,636] [ INFO] - Module mobilenet_v2_imagenet already installed in C:\Users\jacbs.paddlehub\modules\mobilenet_v2_imagenet [2020-05-30 13:43:17,277] [ INFO] - 267 pretrained paramaters loaded by PaddleHub [2020-05-30 13:43:17,280] [ INFO] - Dataset is None or it has not any labels, label map = {} [2020-05-30 13:43:17,282] [ INFO] - Dataset C:\Users\jacbs.paddlehub\dataset\dog-cat already cached. [2020-05-30 13:43:17,643] [ INFO] - Checkpoint dir: ckpt_20200530134317
AttributeError Traceback (most recent call last) in 47 48 # Step 4: load pretrained parameters ---> 49 with fluid.program_guard(task.inference_program()): 50 place = fluid.CPUPlace() 51 exe = fluid.Executor(place)
AttributeError: 'ClassifierTask' object has no attribute 'inference_program'