Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import from_pretrained_keras
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow import keras
|
| 5 |
+
from tensorflow.keras.applications import inception_v3
|
| 6 |
+
|
| 7 |
+
model = from_pretrained_keras("keras-io/deep-dream")
|
| 8 |
+
|
| 9 |
+
#base_image_path = keras.utils.get_file("sky.jpg", "https://i.imgur.com/aGBdQyK.jpg")
|
| 10 |
+
result_prefix = "dream"
|
| 11 |
+
|
| 12 |
+
# These are the names of the layers
|
| 13 |
+
# for which we try to maximize activation,
|
| 14 |
+
# as well as their weight in the final loss
|
| 15 |
+
# we try to maximize.
|
| 16 |
+
# You can tweak these setting to obtain new visual effects.
|
| 17 |
+
layer_settings = {
|
| 18 |
+
"mixed4": 1.0,
|
| 19 |
+
"mixed5": 1.5,
|
| 20 |
+
"mixed6": 2.0,
|
| 21 |
+
"mixed7": 2.5,
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Playing with these hyperparameters will also allow you to achieve new effects
|
| 25 |
+
step = 0.01 # Gradient ascent step size
|
| 26 |
+
num_octave = 3 # Number of scales at which to run gradient ascent
|
| 27 |
+
octave_scale = 1.4 # Size ratio between scales
|
| 28 |
+
iterations = 20 # Number of ascent steps per scale
|
| 29 |
+
max_loss = 15.0
|
| 30 |
+
|
| 31 |
+
def preprocess_image(image_path):
|
| 32 |
+
# Util function to open, resize and format pictures
|
| 33 |
+
# into appropriate arrays.
|
| 34 |
+
img = keras.preprocessing.image.load_img(image_path)
|
| 35 |
+
img = keras.preprocessing.image.img_to_array(img)
|
| 36 |
+
img = np.expand_dims(img, axis=0)
|
| 37 |
+
img = inception_v3.preprocess_input(img)
|
| 38 |
+
return img
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def deprocess_image(x):
|
| 42 |
+
# Util function to convert a NumPy array into a valid image.
|
| 43 |
+
x = x.reshape((x.shape[1], x.shape[2], 3))
|
| 44 |
+
# Undo inception v3 preprocessing
|
| 45 |
+
x /= 2.0
|
| 46 |
+
x += 0.5
|
| 47 |
+
x *= 255.0
|
| 48 |
+
# Convert to uint8 and clip to the valid range [0, 255]
|
| 49 |
+
x = np.clip(x, 0, 255).astype("uint8")
|
| 50 |
+
return x
|
| 51 |
+
|
| 52 |
+
# Get the symbolic outputs of each "key" layer (we gave them unique names).
|
| 53 |
+
outputs_dict = dict(
|
| 54 |
+
[
|
| 55 |
+
(layer.name, layer.output)
|
| 56 |
+
for layer in [model.get_layer(name) for name in layer_settings.keys()]
|
| 57 |
+
]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Set up a model that returns the activation values for every target layer
|
| 61 |
+
# (as a dict)
|
| 62 |
+
feature_extractor = keras.Model(inputs=model.inputs, outputs=outputs_dict)
|
| 63 |
+
|
| 64 |
+
def compute_loss(input_image):
|
| 65 |
+
features = feature_extractor(input_image)
|
| 66 |
+
# Initialize the loss
|
| 67 |
+
loss = tf.zeros(shape=())
|
| 68 |
+
for name in features.keys():
|
| 69 |
+
coeff = layer_settings[name]
|
| 70 |
+
activation = features[name]
|
| 71 |
+
# We avoid border artifacts by only involving non-border pixels in the loss.
|
| 72 |
+
scaling = tf.reduce_prod(tf.cast(tf.shape(activation), "float32"))
|
| 73 |
+
loss += coeff * tf.reduce_sum(tf.square(activation[:, 2:-2, 2:-2, :])) / scaling
|
| 74 |
+
return loss
|
| 75 |
+
|
| 76 |
+
def gradient_ascent_step(img, learning_rate):
|
| 77 |
+
with tf.GradientTape() as tape:
|
| 78 |
+
tape.watch(img)
|
| 79 |
+
loss = compute_loss(img)
|
| 80 |
+
# Compute gradients.
|
| 81 |
+
grads = tape.gradient(loss, img)
|
| 82 |
+
# Normalize gradients.
|
| 83 |
+
grads /= tf.maximum(tf.reduce_mean(tf.abs(grads)), 1e-6)
|
| 84 |
+
img += learning_rate * grads
|
| 85 |
+
return loss, img
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def gradient_ascent_loop(img, iterations, learning_rate, max_loss=None):
|
| 89 |
+
for i in range(iterations):
|
| 90 |
+
loss, img = gradient_ascent_step(img, learning_rate)
|
| 91 |
+
if max_loss is not None and loss > max_loss:
|
| 92 |
+
break
|
| 93 |
+
print("... Loss value at step %d: %.2f" % (i, loss))
|
| 94 |
+
return img
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def process_image(imgPath):
|
| 98 |
+
original_img = preprocess_image(base_image_path)
|
| 99 |
+
original_shape = original_img.shape[1:3]
|
| 100 |
+
|
| 101 |
+
successive_shapes = [original_shape]
|
| 102 |
+
for i in range(1, num_octave):
|
| 103 |
+
shape = tuple([int(dim / (octave_scale ** i)) for dim in original_shape])
|
| 104 |
+
successive_shapes.append(shape)
|
| 105 |
+
successive_shapes = successive_shapes[::-1]
|
| 106 |
+
shrunk_original_img = tf.image.resize(original_img, successive_shapes[0])
|
| 107 |
+
|
| 108 |
+
img = tf.identity(original_img) # Make a copy
|
| 109 |
+
for i, shape in enumerate(successive_shapes):
|
| 110 |
+
print("Processing octave %d with shape %s" % (i, shape))
|
| 111 |
+
img = tf.image.resize(img, shape)
|
| 112 |
+
img = gradient_ascent_loop(
|
| 113 |
+
img, iterations=iterations, learning_rate=step, max_loss=max_loss
|
| 114 |
+
)
|
| 115 |
+
upscaled_shrunk_original_img = tf.image.resize(shrunk_original_img, shape)
|
| 116 |
+
same_size_original = tf.image.resize(original_img, shape)
|
| 117 |
+
lost_detail = same_size_original - upscaled_shrunk_original_img
|
| 118 |
+
|
| 119 |
+
img += lost_detail
|
| 120 |
+
shrunk_original_img = tf.image.resize(original_img, shape)
|
| 121 |
+
|
| 122 |
+
return deprocess_image(img.numpy())
|
| 123 |
+
|
| 124 |
+
image = gr.inputs.Image()
|
| 125 |
+
label = gr.outputs.Image()
|
| 126 |
+
|
| 127 |
+
iface = gr.Interface(classify_image,image,label,
|
| 128 |
+
#outputs=[
|
| 129 |
+
# gr.outputs.Textbox(label="Engine issue"),
|
| 130 |
+
# gr.outputs.Textbox(label="Engine issue score")],
|
| 131 |
+
examples=["sky.jpg"], title="Image classification on CIFAR-100",
|
| 132 |
+
description = "Model for classifying images from the CIFAR dataset using a vision transformer trained with small data.",
|
| 133 |
+
article = "Author: <a href=\"https://huggingface.co/joheras\">Jónathan Heras</a>"
|
| 134 |
+
# examples = ["sample.csv"],
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
iface.launch()
|