def predict(file):
x = load_img(file, target_size=(img_width,img_height))
x = img_to_array(x)
x = np.expand_dims(x, axis=0)
array = model.predict(x)
result = array[0]
if result[0] > result[1]:
if result[0] > 0.9:
print("Predicted answer: Buy")
answer = 'buy'
print(result)
print(array)
else:
print("Predicted answer: Not confident")
answer = 'n/a'
print(result)
return answer
else:
if result[1] > 0.9:
print("Predicted answer: Sell")
answer = 'sell'
print(result)
else:
print("Predicted answer: Not confident")
answer = 'n/a'
print(result)
return answer
def predict_files(test_path):
truebuy = 0
falsebuy = 0
truesell = 0
falsesell = 0
na = 0
for i, ret in enumerate(os.walk(str(test_path) + '/buy')):
for i, filename in enumerate(ret[2]):
if filename.startswith("."):
continue
print("Label: buy")
result = predict(ret[0] + '/' + filename)
if result == "buy":
truebuy += 1
elif result == 'n/a':
print('no action')
na +=1
elif result == 'sell':
falsebuy += 1
for i, ret in enumerate(os.walk(str(test_path) + '/sell')):
for i, filename in enumerate(ret[2]):
if filename.startswith("."):
continue
print("Label: sell")
result = predict(ret[0] + '/' + filename)
if result == "sell":
truesell += 1
elif result == 'n/a':
print('no action')
na += 1
elif result == 'buy':
falsesell += 1
I'm sure i'm doing something wrong, but I can't figure out what to do next.
I've completed all of the steps up until predict-binary.
I run the script but it prints Using plaidml.keras.backend backend in terminal and then just stops
heres my code, what am I missing? I think i'm missing an image file to predict? but what image file? one of the images graphwerk produced or another?
`import os import numpy as np os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array from keras.models import Sequential, load_model
img_width, img_height = 150, 150 model_path = r'G:\inpredo-master\inpredo\models\model.h5' weights_path = r'G:\inpredo-master\inpredo\models\weights.h5' model = load_model(model_path) test_path = r'G:\inpredo-master\inpredo\data\validation'
def predict(file): x = load_img(file, target_size=(img_width,img_height)) x = img_to_array(x) x = np.expand_dims(x, axis=0) array = model.predict(x) result = array[0] if result[0] > result[1]: if result[0] > 0.9: print("Predicted answer: Buy") answer = 'buy' print(result) print(array) else: print("Predicted answer: Not confident") answer = 'n/a' print(result) return answer else: if result[1] > 0.9: print("Predicted answer: Sell") answer = 'sell' print(result) else: print("Predicted answer: Not confident") answer = 'n/a' print(result) return answer
def predict_files(test_path): truebuy = 0 falsebuy = 0 truesell = 0 falsesell = 0 na = 0 for i, ret in enumerate(os.walk(str(test_path) + '/buy')): for i, filename in enumerate(ret[2]): if filename.startswith("."): continue print("Label: buy") result = predict(ret[0] + '/' + filename) if result == "buy": truebuy += 1 elif result == 'n/a': print('no action') na +=1 elif result == 'sell': falsebuy += 1
for i, ret in enumerate(os.walk(str(test_path) + '/sell')): for i, filename in enumerate(ret[2]): if filename.startswith("."): continue print("Label: sell") result = predict(ret[0] + '/' + filename) if result == "sell": truesell += 1 elif result == 'n/a': print('no action') na += 1 elif result == 'buy': falsesell += 1
print("True buy: ", truebuy) print("True sell: ", truesell) print("False buy: ", falsebuy) # important print("False sell: ", falsesell) print("no action:", na) precision = truesell / (truesell + falsesell) precision2 = truebuy / (truebuy + falsebuy) recall = truebuy / (truebuy + falsesell) print("Sell Precision: ", precision) print("Buy Precision", precision2) print("Recall: ", recall) precision1 = (truesell + truebuy) / (truesell + truebuy + falsesell + falsebuy) print("Precision: ", precision1) f_measure = (2 recall precision) / (recall + precision) print("F-measure: ", f_measure) return precision1, na `