Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - how to if statement a model.predict array

The model.predict output is either [[1.0.0.]], [[0.1.0.]], [[0.0.1.]] depending on the category of the image. currently struggling to get the category name as output instead such as squat, bench or deadlift and wondering the best way to do this.

model = load_model('imageModel.h5')
img_width, img_height = 150, 150
img = image.load_img('PATH', target_size=(img_width, img_height))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
(model.predict(img))
print(model.predict(img))

I tried using for statement however this printed out all 3 categories instead of just the category the image is classified in:

array = np.array(model.predict(img))
arr1 = [[1, 0, 0]]
arr2 = [[0, 1, 0]]
arr3 = [[0, 0, 1]]
for arr1 in array == True:
   print("bench")
for arr2 in array == True:
print("deadlift")
for arr3 in array == True:
print("squat")

Any help would be greatly appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think what you've intended to do:

arr1 = np.array([1, 0, 0])
arr2 =  np.array([0, 1, 0])
arr3 =  np.array([0, 0, 1])

if arr1[0] == array[0]:
   print("bench")
if arr2[1] == array[1]:
    print("deadlift")
if arr3[2] == array[2]:
    print("squat")

With a simple for loop:

class_names = ['bench', 'deadlift', 'squat']
for each in range(len(array)):
    if array[each] == 1:
       print(class_names[each])
       break
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...