Zehaos / MobileNet

MobileNet build with Tensorflow
Apache License 2.0
1.62k stars 470 forks source link

Test single image #65

Open mustafa-qamaruddin opened 6 years ago

mustafa-qamaruddin commented 6 years ago

ValueError: Cannot feed value of shape (1, 224, 224, 3) for Tensor 'import/fifo_queue_Dequeue:0', which has shape '(32, 224, 224, 3)'

import tensorflow as tf
from scipy.misc import imread, imresize
import numpy as np

# Quantize
use_quantized_graph = True

# Read image
img = imread("./temp.jpg")
img = imresize(img, (224, 224, 3))
img = img.astype(np.float32)
img = np.expand_dims(img, 0)

# Preprocess
img = img / 255.
img = img - 0.5
img = img * 2.

# Graph
if use_quantized_graph:
  graph_filename = "./frozen_graph.pb"
else:
  graph_filename = "./frozen_graph.pb"

# Create labels dict from labels.txt
LABELS_CLASSES = [
    "Boat Section Middle 6 x 12 x 3.333 Cargo Hold with LtGray Deck",
    "Boat Section Middle 6 x 12 x 3.333 Hull",
    "Boat Section Middle 6 x 12 x 3.333 Deck",
    "Duplo Brick 2 x 10",
    "Brick 1 x 4",
    "Brick 1 x 3",
    "Plate 1 x 3",
    "Slope Brick Curved 2 x 1",
    "Slope Brick Curved 2 x 2 x 0.667",
    "Tyre 44/ 91 x 43 R",
    "Plate 1 x 2 with Handles on Opposite Ends",
    "Technic Turntable 60 Tooth Top",
    "Plate 2 x 6 with Two Rounded Corners",
    "Brick 1 x 2 Log",
    "Slope Brick 18 4 x 2",
    "Plate 4 x 4 without Corner",
    "Tyre 38/ 50 x 43 Off Road",
    "Car Mudguard 4 x 2.5 x 2",
    "Wheel Rim 26 x 43 with 6 Spokes and 6 Pegholes with Tyre 38/ 50 x 43 Off Road",
    "Wheel Rim 26 x 43 with 6 Spokes and 6 Pegholes"
]

# Create a graph def object to read the graph
with tf.gfile.GFile(graph_filename, "rb") as f:
  graph_def = tf.GraphDef()
  graph_def.ParseFromString(f.read())

# Construct the graph and import the graph from graphdef
with tf.Graph().as_default() as graph:
  tf.import_graph_def(graph_def)

  # We define the input and output node we will feed in
  input_node = graph.get_tensor_by_name('import/fifo_queue_Dequeue:0')
  output_node = graph.get_tensor_by_name('import/MobileNet/Predictions/Softmax:0')

  with tf.Session() as sess:
    predictions = sess.run(output_node, feed_dict={input_node: img})[0]
    top_5_predictions = predictions.argsort()[-5:][::-1]
    top_5_probabilities = predictions[top_5_predictions]
    prediction_names = [LABELS_CLASSES[i] for i in top_5_predictions]

    for i in range(len(prediction_names)):
      print('Prediction: %s, Probability: %s \n' % (prediction_names[i], top_5_probabilities[i]))

I have written a custom TF Record writer that converts a custom dataset into TF records, and then I have trained the model, and exported it as frozen model for testing. However, I am getting the error above...

mustafa-qamaruddin commented 6 years ago

That's how I freeze the graph:

$ python tools/freeze_graph.py \
    --input_graph /tmp/mobilenet-model-0004/graph.pbtxt \
    --input_checkpoint /tmp/mobilenet-model-0004/model.ckpt-0 \
    --output_graph ./frozen_graph.pb \
    --output_node_names MobileNet/Predictions/Softmax

That's how I did the training:

$ python train_image_classifier.py \
    --train_dir=/tmp/mobilenet-model-0004 \
    --dataset_name=custom \
    --dataset_split_name=train_val \
    --dataset_dir=LEGOSET/records \
    --model_name=mobilenet \
    --preprocessing_name=mobilenet \
    --width_multiplier=1.0 \
    --max_number_of_steps=1000 \
    --batch_size=32 \
    --save_interval_secs=240 \
    --save_summaries_secs=240 \
    --log_every_n_steps=100 \
    --optimizer=adam \
    --opt_epsilon=0.000000001 \
    --learning_rate=0.0001 \
    --learning_rate_decay_factor=0.00001 \
    --num_epochs_per_decay=2 \
    --weight_decay=0.00005 \
    --adam_beta1=0.09 \
    --adam_beta2=0.0999
mustafa-qamaruddin commented 6 years ago

input_node = graph.get_tensor_by_name('import/MobileNet/input_images:0') gives the following: KeyError: "The name 'import/MobileNet/input_images:0' refers to a Tensor which does not exist. The operation, 'import/MobileNet/input_images', does not exist in the graph."

DRAWPIETY commented 4 years ago

Hello, I have the same mistake as you. Can you tell me how to solve it?