如何在Python中使用TensorFlow?

时间:2019-05-14 02:35:39

标签: python python-3.x tensorflow keras

我已经使用TensorFlow编写了一些代码。运行代码时,模块中出现错误,指出即使我已经安装了TensorFlow模块也不存在。

您能帮我解决错误吗?

我正在使用的TensorFlow版本是:

  • tensorflow 1.13.1
  • tensorflow-estimator 1.13.0

代码如下:

# from scipy.misc import toimage

start = time.time()

#Define Path
model_path = './models/ output.hdf5'
# model_weights_path = './models/weights.h5'
test_path = 'test_image'

#Load the pre-trained models
model = load_model(model_path)
# model.load_weights(model_weights_path)

#Define image parameters
rows = 24
columns = 24
# i = 0
# names = ['Agustin','Amran', 'Andre', 'Angela', 'Anjas', 'Ayu', 'Ekaristi', 'Ester', 'Ferry', 'Fibonaccy', 'Grace', 'Indra', 'Joel', 'Kamna', 'Lukas', 'Lyoci',
#         'Maya', 'Melda','Nindy', 'Pedro', 'Prima', 'Sharon', 'Talenta', 'Wendi', 'Yenni']
# labels = []
# plt.figure(figsize=(30,20))

#Prediction Function
def predict(file):
    x = cv2.imread(file)
    #x = np.full((100,80,3), 12, dtype = np.uint8)
    x = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
    x = cv2.resize(x,(rows,columns))
    x = img_to_array(x)

    x = np.expand_dims(x, axis=0)
#     print(x)
#     x = x.astype('float32')
    #   plt.show()
#     print(x)
    array = model.predict(x)
#     result = array[0]
#     print(result)
    answer = np.argmax(array)
    if answer == 0:
    #         labels.append('Agustin')
        print("Predicted: Maya")
    elif answer == 1:
    #         labels.append('Amran')
    #         plt.subplot(5/columns + 1, columns, i+1)
    #         plt.title('This is' + names[i])
    #         imgplot = plt.imshow(answer)
    #         i += 1
    #         if i % 10 == 0:
    #             break

  print("Predicted: Agustin")
elif answer == 2:    
    print("Predicted: Amran")
elif answer == 3:
    print("Predicted: Andre")
elif answer == 4:
    print("Predicted: Angela")
elif answer == 5:
    print("Predicted: Anjas")
elif answer == 6:
    print("Predicted: Ayu")
elif answer == 7:
    print("Predicted: Ekaristi")
elif answer == 8:
    print("Predicted: Ester")
elif answer == 9:
    print("Predicted: Ferry")
elif answer == 10:
    print("Predicted: Fibonaccy")
elif answer == 11:
    print("Predicted: Grace")
elif answer == 12:
    print("Predicted: Indra")
elif answer == 13:
    print("Predicted: Joel")
elif answer == 14:
    print("Predicted: Kamna")
elif answer == 15:
    print("Predicted: Lukas")
elif answer == 16:
    print("Predicted: Lyoci")
elif answer == 17:
    print("Predicted: Maya")
elif answer == 18:
    print("Predicted: Melda")
elif answer == 19:
    print("Predicted: Nindy")
elif answer == 20:
    print("Predicted: Pedro")
elif answer == 21:
    print("Predicted: Prima")
elif answer == 22:
    print("Predicted: Sharon")
elif answer == 23:
    print("Predicted: Talenta")
elif answer == 24:
    print("Predicted: Wendi")
elif answer == 25:
    print("Predicted: Yenni")
    return answer

#Walk the directory for every image
for i, ret in enumerate(os.walk(test_path)):
    for i, filename in enumerate(ret[2]):
        if filename.startswith("."):
            continue

        print(ret[0] + '/' + filename)
        result = predict(ret[0] + '/' + filename)
        print(" ")
#     imgplot = plt.imshow(ret[0] + '/' + filename)
#     print(test_image)

#Calculate execution time
end = time.time()
dur = end-start

if dur<60:
    print("Execution Time:",dur,"seconds")
elif dur>60 and dur<3600:
    dur=dur/60
    print("Execution Time:",dur,"minutes")
else:
    dur=dur/(60*60)
    print("Execution Time:",dur,"hours")

这是输出:

File "Testing.py", line 9, in <module>
    from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
  File "build/bdist.linux-armv7l/egg/keras/__init__.py", line 3, in <module>
  File "build/bdist.linux-armv7l/egg/keras/utils/__init__.py", line 6, in <module>
  File "build/bdist.linux-armv7l/egg/keras/utils/conv_utils.py", line 9, in <module>
  File "build/bdist.linux-armv7l/egg/keras/backend/__init__.py", line 89, in <module>
  File "build/bdist.linux-armv7l/egg/keras/backend/tensorflow_backend.py", line 5, in <module>
ImportError: No module named tensorflow

0 个答案:

没有答案