如何在Django中解码图像以在TF模型中进行分类

问题描述

我正在尝试在tf模型中对图像进行分类。这是我在做什么:

views.py

def make_prediction(tensor,model='MOB'):
    tf.keras.backend.reset_uids()
    folders = {'VGG': 'vgg','MOB': 'mobilenet','CNN': 'cnn3'}
    model_as_json = 'upload_images/model/%s/modelo.json' % (folders[model])
    weights = 'upload_images/model/%s/modelo.h5' % (folders[model])
    json_file = open(model_as_json,'r')    
    loaded_json_model = json_file.read()
    json_file.close()
    model = tf.keras.models.model_from_json(loaded_json_model)
    model.load_weights(weights)
    return model.predict(tensor)


def upload_image_view(request):
    message = ''
    status = ''
    prediction = -1
    if request.method == 'POST':
        form = forms.UploadImageForm(request.POST,request.FILES)
        if form.is_valid():
            m = form.save(commit=False)
            # try:
            image = tf.io.decode_image(request.FILES['image'].read(),channels=3)
            image = tf.image.convert_image_dtype(image,tf.float32) 
            image = tf.image.resize(image,size=[224,224])
            tensor = tf.constant(image)
            pred = make_prediction(tensor,form.cleaned_data['predicted_with'])[0][0]
            if pred > 0.5:
            # continue...

用户上传的图像被发送到Azure存储,因此我无法获得图像路径来执行预处理和分类

当我处理本地图像时,可以通过m.image.path访问该图像,但是现在我不能这样做。因此,我在上面显示的方式是如何访问图像信息以解码,调整大小并转换为张量,但出现此错误
ValueError: Input 0 of layer Conv1_pad is incompatible with the layer: expected ndim=4,found ndim=3. Full shape received: [32,224,3]

我该如何解决

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)