I try to run the code below, but this error keeps happening
NotImplementedError
: Cannot convert a symbolictf.Tensor
(Mean:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.
This is the code:
import tensorflow as tf
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import random
base_model = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet')
base_model.summary()
names = ['mixed3','mixed5']
layers = [base_model.get_layer(name).output for name in names]
deepdream_model = tf.keras.Model(inputs=base_model.input, outputs=layers)
Sample_Image = tf.keras.preprocessing.image.load_img(r'apple.jpg', target_size =(225, 375))
np.shape(Sample_Image)
Sample_Image = np.array(Sample_Image)/255.0
Sample_Image.shape
plt.imshow(Sample_Image)
Sample_Image.max()
Sample_Image.min()
Sample_Image = tf.keras.preprocessing.image.img_to_array(Sample_Image)
Sample_Image.shape
Sample_Image = tf.Variable(tf.keras.applications.inception_v3.preprocess_input(Sample_Image))
Sample_Image = tf.expand_dims(Sample_Image, axis = 0)
np.shape(Sample_Image)
activations = deepdream_model(Sample_Image)
def calc_loss(image, model):
img_batch = tf.expand_dims(image, axis=0)
layer_activations = model(img_batch)
print('VALORES DE ACTIVACION (LAYER OUTPUT) =\n', layer_activations)
losses = []
for act in layer_activations:
loss = tf.math.reduce_mean(act)
losses.append(loss)
print('PERDIDAS (DE MULTIPLES CAPAS DE ACTIVACION) = ', losses)
print('FORMA DE PERDIDA (DE MULTIPLES CAPAS DE ACTIVACION) =', np.shape(losses))
print('SUMA DE TODAS LAS CAPAS PERDIDAS (DE TODAS LAS CAPAS SELECCIONADAS) =', tf.reduce_sum(losses))
return tf.reduce_sum(losses)
Sample_Image = tf.keras.preprocessing.image.load_img(r'apple.jpg', target_size =(225, 375))
Sample_Image = np.array(Sample_Image)/255.0
Sample_Image = tf.keras.preprocessing.image.img_to_array(Sample_Image)
Sample_Image = tf.Variable(tf.keras.applications.inception_v3.preprocess_input(Sample_Image))
loss = calc_loss(Sample_Image, deepdream_model)
loss
@tf.function
def deepdream(model, image, step_size):
with tf.GradientTape() as tape:
tape.watch(image)
loss = calc_loss(image, model)
gradients = tape.gradient(loss, image)
print('GRADIENTES = \n', gradients)
print('FORMA DE GRADIENTES =\n', np.shape(gradients))
gradients /= tf.math.reduce_std(gradients)
image = image + gradients * step_size
image = tf.clip_by_value(image, -1, 1)
return loss, image
def run_deep_dream_simple(model, image, steps=100, step_size=0.01):
image = tf.keras.applications.inception_v3.preprocess_input(image)
for step in range(steps):
loss, image = deepdream(model, image, step_size)
if step % 100 == 0:
plt.figure(figsize=(12, 12))
plt.imshow(deprocess(image))
plt.show()
print("Step {}, loss{}".format(step, loss))
plt.figure(figsize=(12, 12))
plt.imshow(deprocess(image))
plt.show()
return deprocess(image)
def deprocess(image):
image = 255 * (image + 1.0) / 2.0
return tf.cast(image, tf.uint8)
Sample_Image= tf.keras.preprocessing.image.load_img(r'apple.jpg', target_size = (225, 375))
Sample_Image = np.array(Sample_Image)
dream_img = run_deep_dream_simple(model=deepdream_model, image=Sample_Image, steps=2000, step_size=0.001)`
It should run the models and images.