hello, stranger -kind enough to help a random python newbie here.
I'm trying to follow the example code provided in Tensorflow Hub, which is about calculating "integrated gradient".
Reading through the descriptions on the page, I've been typing through the example code provided on the page, and it does not give the same result as the Hub page. Please refer to:
The code provided below here, is supposed to provide two different pictures (fireboat and panda) with prediction probabilities. Instead, it doesn't show anything and crashes with a line, stating
"Process finished with exit code -1073740791 (0xC0000409)"
I've googled this warning message and each case is very different from others. Is there anything wrong with the code? I am using PyCharm Community edition, and Python 3.10.2.
import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
model = tf.keras.Sequential([
hub.KerasLayer(
name='inception_v1',
handle='https://tfhub.dev/google/imagenet/inception_v1/classification/4',
trainable=False),
])
model.build([None, 224, 224, 3])
model.summary()
def load_imagenet_labels(file_path):
labels_file = tf.keras.utils.get_file('ImageNetLabels.txt', file_path)
with open(labels_file) as reader:
f = reader.read()
labels = f.splitlines()
return np.array(labels)
imagenet_labels = load_imagenet_labels('https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')
def read_image(file_name):
image = tf.io.read_file(file_name)
image = tf.io.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize_with_pad(image, target_height=224, target_width=224)
return image
img_url = {
'Fireboat': 'http://storage.googleapis.com/download.tensorflow.org/example_images/San_Francisco_fireboat_showing_off.jpg',
'Giant Panda': 'http://storage.googleapis.com/download.tensorflow.org/example_images/Giant_Panda_2.jpeg',
}
img_paths = {name: tf.keras.utils.get_file(name, url) for (name, url) in img_url.items()}
img_name_tensors = {name: read_image(img_path) for (name, img_path) in img_paths.items()}
plt.figure(figsize=(8, 8))
for n, (name, img_tensors) in enumerate(img_name_tensors.items()):
ax = plt.subplot(1, 2, n+1)
ax.imshow(img_tensors)
ax.set_title(name)
ax.axis('off')
plt.tight_layout()
#plt.show()
def top_k_predictions(img, k=3):
image_batch = tf.expand_dims(img, 0)
predictions = model(image_batch)
probs = tf.nn.softmax(predictions, axis=-1)
top_probs, top_idxs = tf.math.top_k(input=probs, k=k)
top_labels = imagenet_labels[tuple(top_idxs)]
return top_labels, top_probs[0]
for (name, img_tensor) in img_name_tensors.items():
plt.imshow(img_tensor)
plt.title(name, fontweight='bold')
plt.axis('off')
plt.show()
pred_label, pred_prob = top_k_predictions(img_tensor)
for label, prob in zip(pred_label, pred_prob):
print(f'{label}: {prob:0.1%}')
That is a good question. I was unaware of that sample and it looks impressive.
I also looked at your error message in google and it comes up a lot with pycharm on windows. What I suggest is for you to do is implement the code using google collab. The sample is already in collab so it should be trivial. There is a lot to it and its hard to debug.
Once you get it working and understand it in collab. Try to reproduce your work in a generic python jupyter notebook. If you can still do it, then try it in pycharm's IDE. I suspect the error lies in the IDE and not the original code.