基于Tensorflow的图像深度识别

    xiaoxiao2022-07-02  119

            前几天因为项目关系需要相机测距功能,需求上说只能通过拍照来测量距离,这从技术原理上来说其实根本是行不通的,因此想到了深度学习方法。然而本猿是个深度学习小白,找来找去只找到了这一个方法——FCRN Depth,地址:https://github.com/iro-cp/FCRN-DepthPrediction

    tensorflow框架的安装就不多说了,多的很,下面就说一下这个库怎么用。首先在github上下载已经学习好的模型,下载ckpt就行并放到项目目录下。

     然后将提供的predict.py代码复制到本地,

    但是提供的代码没法运行的,因为训练方法和模型再新的tensorflow中好像都没有,因此修改代码如下:

    import argparse import os import numpy as np import tensorflow as tf from matplotlib import pyplot as plt from PIL import Image def predict(model_data_path, image_path): # Default input size height = 228 width = 304 channels = 3 batch_size = 1 # Read image img = Image.open(image_path) # img = img.resize([width, height], Image.ANTIALIAS) width = img.width height = img.height img = np.array(img).astype('float32') img = np.expand_dims(np.asarray(img), axis=0) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, height, width, channels)) # Construct the network net = tf.keras.applications.ResNet50(input_tensor=input_node,weights='imagenet',include_top=False) # [Variable and model creation goes here.] with tf.Session() as sess: # Load the converted parameters print('Loading the model') new_saver = tf.train.import_meta_graph(model_data_path+".meta") # Use to load from ckpt file new_saver.restore(sess, model_data_path) init = tf.global_variables_initializer() sess.run(init) # Use to load from npy file # net.load(model_data_path, sess) # Evalute the network for the given image output_node=net.output pred = sess.run(output_node, feed_dict={input_node: img}) # Plot result fig = plt.figure() ii = plt.imshow(pred[0, :, :, 0], interpolation='nearest') fig.colorbar(ii) plt.show() return pred def main(): # Parse arguments # parser = argparse.ArgumentParser() # parser.add_argument('-model_path', '-NYU_FCRN-checkpoint') # parser.add_argument('-image_paths', '-timg.jpg') # args = parser.parse_args() # print(args) # Predict the image pred = predict("model/NYU_FCRN.ckpt", "timg1.jpg") os._exit(0) if __name__ == '__main__': main()

    最终结果如下图所示:

     

    最新回复(0)