Home>
Two label images are classified as 0 and 1. I'd like to use that model to display the prediction label for a new image, but I'm stuck with an error statement and I don't know what it means
Error messageDepth of input (1) is not a multiple of input depth of filter (3) for 'conv1_1/Conv2D' (op: 'Conv2D') with input shapes: [?, 28,28,1] ,
[5,5,3,32].</Code></pre>
<strong>Applicable source code</strong>
<pre><code data-language = "Python">ValueError Traceback (most recent call last)
<ipython-input-6-3673e8a03bd8>in<module>
96 keep_prob = tf.placeholder ("float")
97
--->98 logits = interence (x_image, keep_prob)
99 sess = tf.InteractiveSession ()
100
<ipython-input-6-3673e8a03bd8>in interence (imegs_placeholder, keep_prob)
46 W_conv1 = weight_variable ([5, 5, 3, 32])
47 b_conv1 = bias_variable ([32])
--->48 h_conv1 = tf.nn.relu (conv2d (x_image, W_conv1) + b_conv1)
49
50 # Create pooling layer 1
<ipython-input-6-3673e8a03bd8>in conv2d (x, W)
33 # Convolution layer
34 def conv2d (x, W):
--->35 return tf.nn.conv2d (x, W, strides = [1,1,1,1],
padding = "SAME")
36
37 # Pooling layer
CNN model
import sys
import cv2
import numpy as np
import tensorflow as tf
import tensorflow.python.platform
import os
import matplotlib.pyplot as plt
NUM_CLASSES = 2
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE * 3
flags = tf.app.flags
FLAGS = flags.FLAGS
tf.app.flags.DEFINE_string ('f', '', 'kernel')
flags.DEFINE_string ('train', 'train.txt', 'File name of train data')
flags.DEFINE_string ('test', 'test.txt', 'File name of train data')
flags.DEFINE_string ('train_dir', '/ tmp/data', 'Directory to put the training data.')
flags.DEFINE_integer ('max_steps', 200, 'Number of steps to run trainer.')
flags.DEFINE_integer ('batch_size', 64, 'Batch size'
'Must divide evenly into the dataset sizes.')
flags.DEFINE_float ('learning_rate', 1e-4, 'Initial learning rate.')
def inference (images_placeholder, keep_prob):
"" "Function to create a predictive model
Argument:
images_placeholder: image placeholder
keep_prob: dropout rate place_holder
Returns:
y_conv: Probability of each class (such as)
"" "
# Initialize weights with normal distribution with standard deviation 0.1
def weight_variable (shape):
initial = tf.truncated_normal (shape, stddev = 0.1)
return tf.Variable (initial)
# Initialize bias with normal distribution with standard deviation 0.1
def bias_variable (shape):
initial = tf.constant (0.1, shape = shape)
return tf.Variable (initial)
# Create a convolutional layer
def conv2d (x, W):
return tf.nn.conv2d (x, W, strides = [1, 1, 1, 1],padding = 'SAME')
# Create a pooling layer
def max_pool_2x2 (x):
return tf.nn.max_pool (x, ksize = [1, 2, 2, 1],
strides = [1, 2, 2, 1],
padding = 'SAME')
# Transform input to 28x28x3
x_image = tf.reshape (images_placeholder, [-1, 28, 28, 3])
# Create convolution layer 1
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)
# Create pooling layer 1
with tf.name_scope ('pool1') as scope:
h_pool1 = max_pool_2x2 (h_conv1)
# Create convolution layer 2
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)
# Create pooling layer 2
with tf.name_scope ('pool2') as scope:
h_pool2 = max_pool_2x2 (h_conv2)
# Create fully connected layer 1
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)
# set dropout
h_fc1_drop = tf.nn.dropout (h_fc1, keep_prob)
# Create fully connected layer 2
with tf.name_scope ('fc2') as scope:
W_fc2 = weight_variable ([1024, NUM_CLASSES])
b_fc2 = bias_variable ([NUM_CLASSES])
# Normalization with softmax function
with tf.name_scope ('softmax') as scope:
y_conv = tf.nn.softmax (tf.matmul (h_fc1_drop, W_fc2) + b_fc2)
# Return something like the probability of each label
return y_conv
def loss (logits, labels):
"" "function to calculate loss
argument:
logits: logit tensor, float-[batch_size, NUM_CLASSES]
labels: tensor of labels, int32-[batch_size, NUM_CLASSES]
Returns:
cross_entropy: cross entropy tensor, float
"" "
# Calculate cross entropy
cross_entropy = -tf.reduce_sum (labels * tf.log (logits))
# Specify to display with TensorBoard
tf.summary.scalar ("cross_entropy", cross_entropy)
return cross_entropy
def training (loss, learning_rate):
"" "Function that defines the training Op
argument:
loss: Loss tensor, result of loss ()
learning_rate: Learning factor
Returns:
train_step: Training Op
"" "
train_step = tf.train.AdamOptimizer (learning_rate) .minimize (loss)
return train_step
def accuracy (logits, labels):
"" "Function to calculate accuracy
argument:logits: result of inference ()
labels: tensor of labels, int32-[batch_size, NUM_CLASSES]
Returns:
accuracy: accuracy rate (float)
"" "
correct_prediction = tf.equal (tf.argmax (logits, 1), tf.argmax (labels, 1))
accuracy = tf.reduce_mean (tf.cast (correct_prediction, "float"))
tf.summary.scalar ("accuracy", accuracy)
return accuracy
if __name__ == '__main__':
# Open file
f = open ('train/train.txt')
# Array to put data
train_image = []
train_label = []
for line in f:
# Use space delimiter except new line
line = line.rstrip ()
l = line.split ()
# Read data and reduce to 28x28
img = cv2.imread ('train /' + l [0])
img = cv2.resize (img, (28, 28))
# Set to a float value between 0 and 1
train_image.append (img.flatten (). astype (np.float32) /255.0)
# Prepare labels in 1-of-k format
tmp = np.zeros (NUM_CLASSES)
tmp [int (l [1])] = 1
train_label.append (tmp)
# convert to numpy format
train_image = np.asarray (train_image)
train_label = np.asarray (train_label)
f.close ()
f = open ('test/test.txt')
test_image = []
test_label = []
for line in f:
line = line.rstrip ()
l = line.split ()
img = cv2.imread ('test /' + l [0])
img = cv2.resize (img, (28, 28))
test_image.append (img.flatten (). astype (np.float32) /255.0)
tmp = np.zeros (NUM_CLASSES)
tmp [int (l [1])] = 1
test_label.append (tmp)
test_image = np.asarray (test_image)
test_label = np.asarray (test_label)
f.close ()
with tf.Graph (). as_default ():
# Temporary Tensor to put images
images_placeholder = tf.placeholder ("float", shape = (None, IMAGE_PIXELS))
# Temporary Tensor to put the label
labels_placeholder = tf.placeholder ("float", shape = (None, NUM_CLASSES))
# Temporary Tensor with dropout rate
keep_prob = tf.placeholder ("float")
# create a model by calling inference ()
logits = inference (images_placeholder, keep_prob)
# call loss () to calculate loss
loss_value = loss (logits, labels_placeholder)
# call training () to train
train_op = training (loss_value, FLAGS.learning_rate)
# Accuracy calculation
acc = accuracy (logits, labels_placeholder)
prec = precision (logits, labels_placeholder)
# Prepare to save
saver = tf.train.Saver ()
# Create a session
sess = tf.Session ()
# Variable initialization
sess.run (tf.initialize_all_variables ())
# Run trainingif len (train_image)% FLAGS.batch_size is 0:
train_batch = len (train_image) /FLAGS.batch_size
else:
train_batch = int ((len (train_image) /FLAGS.batch_size) +1)
shuffle_idx = np.arange (len (train_label))
for step in range (FLAGS.max_steps):
np.random.shuffle (shuffle_idx)
for i in range (train_batch):
# run training on batch_size images
batch = FLAGS.batch_size * i
batch_idx = shuffle_idx [batch: batch + FLAGS.batch_size]
# specify the data to put in placeholder with feed_dict
sess.run (train_op, feed_dict = {
images_placeholder: train_image [batch_idx],
labels_placeholder: train_label [batch_idx],
keep_prob: 1.0})
# Calculate accuracy after every step
train_accuracy = sess.run (acc, feed_dict = {
images_placeholder: train_image,
labels_placeholder: train_label,
keep_prob: 1.0})
print (step, train_accuracy)
# Show accuracy for test data after training
print (sess.run (acc, feed_dict = {
images_placeholder: test_image,
labels_placeholder: test_label,
keep_prob: 1.0}))
# Save the final model
save_path = saver.save (sess, ". \ ckpt \ model.ckpt")
Program that wants to display prediction labels
import tensorflow as tf
import cv2
import numpy as np
import os
NUM_CLASSES = 2
def interence (imegs_placeholder, keep_prob):
--Omitted--
return y_conv
if __name__ == "__main__":
print (os.getcwd ())
# Load image
img = input ("Please enter the image path>")
img = cv2.imread (img, cv2.IMREAD_GRAYSCALE)
img = cv2.resize (img, (28, 28))
ximage = img.flatten (). astype (np.float32) /255.0 #change format
# Set variables for expression
x_image = tf.placeholder ("float", shape = [None, 784]) # input
y_label = tf.placeholder ("float", shape = [None, 2])
keep_prob = tf.placeholder ("float")
logits = interence (x_image, keep_prob)
sess = tf.InteractiveSession ()
saver = tf.train.Saver ()
sess.run (tf.global_variables_initializer ())
ckpt = tf.train.get_checkpoint_state ('./ ckpt')
saver.restore (sess, ckpt.model_checkpoint_path) # Read variable data
pred = np.argmax (logits.eval (feed_dict = {x_image: [ximage],
keep_prob: 1.0}) [0])
print (pred)
input
C: \ Users \ koukioki \ Desktop \ isu_oki
Enter image path>./ syorigo/1.jpg
Please tell me because I wasn't sure if I investigated it myself
-
Answer # 1
Related articles
- python - i want to switch images using itemconfig in the gui, but it doesn't work
- python - fine chew using a trained model of your own dataset
- i don't understand the exercises using python trigonometric functions
- python - error in image binarization using cv2adaptivethreshold function
- python - i want to separate by a specific word using the split function
- python - image recognition using cnn keras multiple inputs
- python - typeerror when calculating the array of images called by imageopen ()
- about batch change of file name using python
- display images by drag and drop on wxpython gui
- python 3x - i want to get the nth array with an argument using python3 argparse
- python 3x - how to rename a folder created using jupyternotebook
- i want to display images periodically in python, what should i do?
- python 3x - processing to jump to the link destination using chrome driver in python
- i want to click and number a few images like a switch in python
- python - i want to put the image file path in a variable and open it using that variable
- i want to adjust the execution result using the while statement in python as expected
- python - i want to download images from flickr
- python - aggregation processing using pandas
- specifying the range of the graph using python date
- python - about the fibonacci sequence using recursion
x_image = tf.reshape (images_placeholder, [-1, 28, 28, 3]
Please tell me about x_image.shape.
Is the 1D part of the array the number of data?