I am working with a teammate to create an image classification and localization system. We are using Tensorflow 2 and python 3. We know that we are using some deprecated functions that are part of Tensorflow 1 though. I trained a model in tensorflow and exported it as a frozen_inference_graph.pb My teammate used a prebuilt frozen inference graph, found here as Faster-RCNN Inception v2: https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API#use-existing-config-file-for-your-model
When he runs his code normally, it runs fine, but when we replace the prebuilt frozen inference graph with my frozen inference graph we get the error: TypeError: Cannot interpret feed_dict key as Tensor: The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph.
We believe that the inference graph that my teammate used contained an input tensor named "image_tensor" and in my model we have no input tensors. Is this an accurate assessment? If his model used this input tensor and mine didn't, then is there a work around, or will I have to remake my model?
Has anyone had this problem, or at least could point me in a direction? I've looked at similar questions and nothing has helped.
Code for my model is:
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
import PIL
from PIL import Image
print(tf.__version__)
img_height = 28
img_width = 28
model = keras.Sequential([
#complex
layers.Input((28,28,1)), #28 * 28 pixels, only 1 color (greyscale)
#layers.LayerNormalization(axis=1),
layers.Dense(256, activation="selu"), #256 nodes, activation = rectify linear unit, fast activation function
layers.Dense(128, activation="selu"),
layers.Conv2D(128,3, padding='same'),
layers.MaxPooling2D(),
layers.Dropout(rate=0.2),
layers.Dense(64, activation="selu"),
layers.Dense(32, activation="selu"),
layers.Conv2D(32,3, padding='same'), #2d convulution layer
layers.MaxPooling2D(),#downsamples input so that feature maps can be created. usually added after convulution layer
layers.Flatten(), #[[1],[2],[3]] --> [1,2,3]
layers.Dense(3), #representing 3 classes, output layer
layers.Activation('softmax')
#simple
#layers.Input((28,28,1)),
#layers.Flatten(input_shape=(28, 28, 3)),
#layers.Dense(64, activation="relu"),
#layers.Dropout(0.3),
#layers.Dense(3),
])
model.summary()
#SPLITTING DATA
#Train file contains ALL pictures, at random move 50% to the Validation file
import os
import shutil
#Moving I
source = "C:/Users/evanp/AI/Capstone/Demo_images/Train/I"
destination = "C:/Users/evanp/AI/Capstone/Demo_images/Validation/I"
files = os.listdir(source)
for f in files:
if np.random.rand(1) < 0.5:
shutil.move(source + '/'+ f, destination + '/'+ f)
#moving L
source = "C:/Users/evanp/AI/Capstone/Demo_images/Train/L"
destination = "C:/Users/evanp/AI/Capstone/Demo_images/Validation/L"
files = os.listdir(source)
for f in files:
if np.random.rand(1) < 0.5:
shutil.move(source + '/'+ f, destination + '/'+ f)
#moving T
source = "C:/Users/evanp/AI/Capstone/Demo_images/Train/T"
destination = "C:/Users/evanp/AI/Capstone/Demo_images/Validation/T"
files = os.listdir(source)
for f in files:
if np.random.rand(1) < 0.5:
shutil.move(source + '/'+ f, destination + '/'+ f)
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
valid_datagen = ImageDataGenerator(rescale=1./255)
ds_train = train_datagen.flow_from_directory(
'C:/Users/evanp/AI/Capstone/Demo_images/Train',
target_size=(28, 28),
color_mode='grayscale',
batch_size=1,
class_mode='categorical',
shuffle=True,
seed=1953)
ds_validation = valid_datagen.flow_from_directory(
'C:/Users/evanp/AI/Capstone/Demo_images/Validation',
target_size=(28, 28),
color_mode='grayscale',
batch_size=1,
class_mode='categorical',
shuffle=True,
seed=1953)
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=0.001),
loss=[
keras.losses.CategoricalCrossentropy(from_logits=True),
],
metrics=["accuracy"],
)
STEP_SIZE_TRAIN=ds_train.n//ds_train.batch_size
STEP_SIZE_VALID=ds_validation.n//ds_validation.batch_size
model.fit_generator(generator=ds_train,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=ds_validation,
validation_steps=STEP_SIZE_VALID,
epochs=10
)
The code that my teammate is using is as follows, and the only thing that we change is
PATH_TO_CKPT = "frozen_inference_graph.pb"
becomes
PATH_TO_CKPT = "MY_frozen_inference_graph.pb"
import pyrealsense2 as rs
import numpy as np
import cv2
import tensorflow as tf
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, 30)
print("Starting streaming")
pipeline.start(config)
# load tensorflow
print("[INFO] Loading model...")
PATH_TO_CKPT = "frozen_inference_graph.pb"
# Load the Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.compat.v1.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.compat.v1.import_graph_def(od_graph_def, name='')
sess = tf.compat.v1.Session(graph=detection_graph)
# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
print("[INFO] Model loaded.")
colors_hash = {}
while True:
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
depth = frames.get_depth_frame()
# Convert images to numpy arrays
color_image = np.asanyarray(color_frame.get_data())
scaled_size = (color_frame.width, color_frame.height)
# expand image dimensions to have shape: [1, None, None, 3]
# i.e. a single-column array, where each item in the column has the pixel RGB value
image_expanded = np.expand_dims(color_image, axis=0)
# Perform the actual detection by running the model with the image as input
(boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_expanded})
boxes = np.squeeze(boxes)
classes = np.squeeze(classes).astype(np.int32)
scores = np.squeeze(scores)
for idx in range(int(num)):
class_ = classes[idx]
score = scores[idx]
box = boxes[idx]
if class_ not in colors_hash:
colors_hash[class_] = tuple(np.random.choice(range(256), size=3))
if score > 0.6:
left = int(box[1] * color_frame.width)
top = int(box[0] * color_frame.height)
right = int(box[3] * color_frame.width)
bottom = int(box[2] * color_frame.height)
p1 = (left, top)
p2 = (right, bottom)
# draw box
r, g, b = colors_hash[class_]
cv2.rectangle(color_image, p1, p2, (int(r), int(g), int(b)), 2, 1)
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', color_image)
cv2.waitKey(1)
print("[INFO] stop streaming ...")
pipeline.stop()
Edit: I tried the solution here and it didn't work KeyError : The tensor variable , Refer to the tensor which does not exists