使用自训练的TensorFlow模型标记图像

我成功地在TensorFlow中训练了自己的模型,使用的图表如下:

我的网络

在Python中看起来是这样的:

with tf.name_scope("Reshaping_data") as scope:    x = tf.reshape(x, shape=[-1, imgSize, imgSize, 1], name="inp") #(?, 48, 48, 1)with tf.name_scope("Conv1") as scope:    conv1 = conv2d(x, weights['wc1'], biases['bc1']) #(?, 48, 48, 32)    conv1 = maxpool2d(conv1, k=2) #(?, 24, 24, 32)

…(更多卷积和全连接层)…

out = tf.add(tf.matmul(fc1, weights['out']), biases['out'], name="out") #(?, 43)

我使用GTSRB 数据集进行了训练,并保存了模型。现在我想用这个模型标记新图像。我当前的label.py文件如下:

import tensorflow as tfcheckpoint_file = tf.train.latest_checkpoint("saved_models")graph = tf.Graph()with graph.as_default():     sess = tf.Session()     with sess.as_default():         saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))         saver.restore(sess,checkpoint_file)         inp = graph.get_operation_by_name("Reshaping_data/inp").outputs[0]         prediction=graph.get_operation_by_name("out").outputs[0]         input_img = tf.image.decode_jpeg(tf.read_file("/home/DB/GTSRB/Test/00021/07406.jpg"), channels=3)         reshaped_image = tf.image.resize_image_with_crop_or_pad(tf.cast(input_img, tf.float32), 48, 48)         float_image = tf.image.per_image_standardization(reshaped_image)         images = tf.expand_dims(float_image, 0)         print(sess.run(prediction,feed_dict={inp:images}))

但是当读取feed_dict时失败了。我做错了什么?

Traceback (most recent call last):  File "label.py", line 23, in <module>    print(sess.run(prediction,feed_dict={inp:images}))  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 767, in runrun_metadata_ptr)  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 925, in _run    raise TypeError('The value of a feed cannot be a tf.Tensor object. ' TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

非常感谢!


回答:

我这样解决了这个问题:

checkpoint_file = tf.train.latest_checkpoint("saved_models")imgSize = 48graph = tf.Graph()with graph.as_default():     sess = tf.Session()     with sess.as_default():         saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))         saver.restore(sess,checkpoint_file)         inp = graph.get_operation_by_name("Reshaping_data/inp").outputs[0]         prediction=graph.get_operation_by_name("out").outputs[0]         img = imread(imagepath, flatten=True)         img = imresize(img, [imgSize, imgSize])         img = img.astype('float32')         img_mean = np.mean(img)         img_var = np.std(img)         img = (img - img_mean)/img_var         #img = (48, 48)         img = np.expand_dims(img, axis=2)         #img = (48, 48, 1)         img = np.expand_dims(img, axis=0)         #img = (1, 48, 48, 1)         #inp expects (?, 48, 48, 1)         res = sess.run(prediction,feed_dict={inp:img})         print(res)         print(res.argmax(axis=1))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注