2017-11-14 4 views
3

이미지 처리 분류자를 만들고 있는데이 코드는 전체 코드가이 라인 (pred = model.predict_classes (test_image))을 제외하고 실행중인 이미지의 이미지 클래스를 예측하는 API이 API는 Django 프레임 워크에서 만들어졌으며 Python을 사용하고 있습니다. 2.7/image/Tensor Tensor ("activation_5/Softmax : 0", shape = (?, 4), dtype = float32)의 ValueError가이 그래프의 요소가 아닙니다.

정상적으로 (API를 만들지 않고)이 코드를 실행하면 완벽하게 실행됩니다.

def classify_image(request): 
if request.method == 'POST' and request.FILES['test_image']: 

    fs = FileSystemStorage() 
    fs.save(request.FILES['test_image'].name, request.FILES['test_image']) 


    test_image = cv2.imread('media/'+request.FILES['test_image'].name) 

    if test_image is not None: 
     test_image = cv2.resize(test_image, (128, 128)) 
     test_image = np.array(test_image) 
     test_image = test_image.astype('float32') 
     test_image /= 255 
     print(test_image.shape) 
    else: 
     print('image didnt load') 

    test_image = np.expand_dims(test_image, axis=0) 
    print(test_image) 
    print(test_image.shape) 

    pred = model.predict_classes(test_image) 
    print(pred) 

return JsonResponse(pred, safe=False) 
+0

어떻게 _model_을 정의합니까? 아마도 비슷한 문제에 대한이 문제에 대한 의견이 도움이 될 수 있습니다. [Tensorflow 백엔드 - 버그] (0128-388-331) # – aseipel

+0

내가있을 때 –

답변

2

test_image와 tensorflow 모델 입력이 일치하지 않습니다.

# Your image shape is (, , 3) 
test_image = cv2.imread('media/'+request.FILES['test_image'].name) 

if test_image is not None: 
    test_image = cv2.resize(test_image, (128, 128)) 
    test_image = np.array(test_image) 
    test_image = test_image.astype('float32') 
    test_image /= 255 
    print(test_image.shape) 
else: 
    print('image didnt load') 

# Your image shape is (, , 4) 
test_image = np.expand_dims(test_image, axis=0) 
print(test_image) 
print(test_image.shape) 

pred = model.predict_classes(test_image) 

위의 내용은 단지 가정입니다. 디버그하려면 이미지 크기를 인쇄하고 모델 정의의 첫 번째 레이아웃과 비교해야합니다. 크기 (너비, 높이, 깊이)가 일치하는지 확인하십시오.

+0

모델이 없습니다. 정상적으로 django 대신이 코드를 실행하면 완벽하게 실행되지만 API에서이 오류가 발생합니다 이유는 알 수 없습니다 –

+0

classify_image 클래스의 모든 로그를 게시 할 수 있습니까? –

+0

(u는 내가이 API를 제공하고 있음을 의미)로 로그인합니다. –

관련 문제