我正在尝试创建一个包含squeezenet的django Web应用程序(因此,我使用的是树莓类型1,因此无法选择启动方式。)
我想知道的是,每当我使用python接口手动尝试预测命令时,总能得到想要的结果。但是,当我将其部署在django views.py上时,总是会遇到错误。
这是我的代码:
from django.shortcuts import render, redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from uploads.core.models import Document
from uploads.core.forms import DocumentForm
import os, json
from glob import glob
import tensorflow.python.keras
import tensorflow as tf
import time
from keras_squeezenet import SqueezeNet
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.python.keras import backend as K
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from keras.preprocessing import image
import numpy as np
tf.keras.backend.clear_session()
model = SqueezeNet()
def home(request):
documents = Document.objects.all()
return render(request, 'core/home.html', { 'documents': documents })
def simple_upload(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
img = image.load_img('/home/pi/a/simple-file-upload'+uploaded_file_url, target_size=(227, 227))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
result = decode_predictions(preds)
return render(request, 'core/simple_upload.html', {
'uploaded_file_url': uploaded_file_url, 'result' : result
})
return render(request, 'core/simple_upload.html')
第一步,它可以很好地读取我的图像(正确获取阵列和RGB),但是当它下降时,它会将输入形状视为(?,227,227,3)。
inputs
[<tf.Tensor 'input_1:0' shape=(?, 227, 227, 3) dtype=float32>,
<tf.Tensor 'keras_learning_phase:0' shape=() dtype=bool>]
kwargs
{}
self
<keras.engine.training.Model object at 0xaa6061d0>
我在代码上做错了什么吗? 我在Django之外使用了完全相同的代码,并且有效