How to use an image prediction program

Asked 1 years ago, Updated 1 years ago, 37 views

http://kivantium.hateblo.jp/entry/2015/11/18/233834
I would like to use this site to create an image determination program using the Python learning model, but I don't know how to run this program, so I would like to ask you a question.
For example, save it under the name eval.py and
at the terminal. I understand that typing python eval.py is not enough, but should I specify an image or a learned model with the command line argument?
I'm afraid I'm a shallow student, but I'd appreciate it if you could teach me.

Here are the codes for reference:

#!/usr/bin/env python
#! -*-coding:utf-8-*-

import sys
import numpy as np
import tensorflow as tf
import cv2


NUM_CLASSES=2
IMAGE_SIZE = 28
IMAGE_PIXELS=IMAGE_SIZE*IMAGE_SIZE*3

def reference(images_placeholder, keep_prob):
""" a function that creates a model

Arguments: 
  images_placeholder —Placeholder of the image created with inputs()
  keep_prob —place_holder for dropout rate

Return Value:
  cross_entropy —Model calculation results
"""
def weight_variable(shape):
  initial=tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable (initial)

def bias_variable(shape):
  initial=tf.constant(0.1,shape=shape)
  return tf.Variable (initial)

def conv2d(x,W):
  return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')

def max_pool_2x2(x):
  return tf.nn.max_pool(x,ksize=[1,2,2,1],
                        strides=[1,2,2,1], padding='SAME')

x_image=tf.reshape (images_placeholder, [-1, 28, 28, 3])

with tf.name_scope('conv1') as scope:
    W_conv1 = weight_variable ([5,5,3,32])
    b_conv1 = bias_variable([32])
    h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)

with tf.name_scope('pool1') as scope:
    h_pool1 = max_pool_2x2(h_conv1)

with tf.name_scope('conv2') as scope:
    W_conv2 = weight_variable ([5, 5, 32, 64])
    b_conv2 = bias_variable ([64])
    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)

with tf.name_scope('pool2') as scope:
    h_pool2 = max_pool_2x2(h_conv2)

with tf.name_scope('fc1') as scope:
    W_fc1 = weight_variable ([7*7*64,1024])
    b_fc1 = bias_variable ([1024])
    h_pool2_flat=tf.reshape (h_pool2, [-1,7*7*64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
    h_fc1_drop=tf.nn.dropout(h_fc1, keep_prob)

with tf.name_scope('fc2') as scope:
    W_fc2 = weight_variable ([1024, NUM_CLASSES])
    b_fc2 = bias_variable ([NUM_CLASSES])

with tf.name_scope('softmax') as scope:
    y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)

return_conv

if__name__=='__main__':
test_image=[ ]
for i in range (1,len(sys.argv)):
    img=cv2.imread(sys.argv[i])
    img = cv2.resize(img, (28,28))
    test_image.append(img.flatten().astype(np.float32)/255.0)
test_image=np.asarray(test_image)

images_placeholder=tf.placeholder("float", shape=(None, IMAGE_PIXELS))
labels_placeholder=tf.placeholder("float", shape=(None, NUM_CLASSES))
keep_prob=tf.placeholder("float")

logs = reference(images_placeholder, keep_prob)
sess=tf.InteractiveSession()

saver=tf.train.Saver()
sess.run(tf.initialize_all_variables())
saver.restore(sess, "model.ckpt")

for i in range(len(test_image))):
    pred=np.argmax(logits.eval(feed_dict={ 
        images_placeholder: [test_image[i]],
        keep_prob:1.0}) [0])
    print pred

python tensorflow

2022-09-29 21:25

1 Answers

for the main equivalent,

for i in range (1,len(sys.argv)):
    img=cv2.imread(sys.argv[i])

It seems that you have loaded any number of images as , so you can run them properly by specifying the following arguments:

python eval.py image1.png image2.png


2022-09-29 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.