Update README.md
Browse files
README.md
CHANGED
|
@@ -46,31 +46,26 @@ from skimage.util import random_noise
|
|
| 46 |
import numpy as np
|
| 47 |
import matplotlib.pyplot as plt
|
| 48 |
|
| 49 |
-
# Load the model from Hugging Face
|
| 50 |
model_path = hf_hub_download(repo_id="BueormLLC/AID", filename="model.h5")
|
| 51 |
-
autoencoder = tf.keras.models.load_model(model_path)
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
def add_noise(img, noise_type="gaussian"):
|
|
|
|
| 55 |
if noise_type == "gaussian":
|
| 56 |
-
noisy_img = random_noise(
|
| 57 |
elif noise_type == "salt_pepper":
|
| 58 |
-
noisy_img = random_noise(
|
| 59 |
return np.clip(noisy_img, 0., 1.)
|
| 60 |
|
| 61 |
-
# Example: Load an image, add noise, and denoise
|
| 62 |
def predict_denoised_image(autoencoder, image):
|
| 63 |
-
# Preprocess image (resize to 256x256, normalize to [0, 1])
|
| 64 |
img_resized = tf.image.resize(image, (256, 256)) / 255.0
|
| 65 |
img_array = np.expand_dims(img_resized, axis=0)
|
| 66 |
|
| 67 |
-
# Add noise
|
| 68 |
noisy_image = add_noise(img_resized)
|
| 69 |
|
| 70 |
-
# Denoise the image
|
| 71 |
denoised_image = autoencoder.predict(np.expand_dims(noisy_image, axis=0))
|
| 72 |
|
| 73 |
-
# Plot results
|
| 74 |
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
|
| 75 |
ax[0].imshow(noisy_image)
|
| 76 |
ax[0].set_title("Noisy Image")
|
|
@@ -82,11 +77,9 @@ def predict_denoised_image(autoencoder, image):
|
|
| 82 |
|
| 83 |
plt.show()
|
| 84 |
|
| 85 |
-
|
| 86 |
-
test_image = tf.keras.preprocessing.image.load_img('your_image.jpg', target_size=(256, 256))
|
| 87 |
test_image = tf.keras.preprocessing.image.img_to_array(test_image)
|
| 88 |
|
| 89 |
-
# Run denoising prediction
|
| 90 |
predict_denoised_image(autoencoder, test_image)
|
| 91 |
```
|
| 92 |
|
|
|
|
| 46 |
import numpy as np
|
| 47 |
import matplotlib.pyplot as plt
|
| 48 |
|
|
|
|
| 49 |
model_path = hf_hub_download(repo_id="BueormLLC/AID", filename="model.h5")
|
|
|
|
| 50 |
|
| 51 |
+
autoencoder = tf.keras.models.load_model(model_path, custom_objects={'mse': tf.keras.losses.MeanSquaredError()})
|
| 52 |
+
|
| 53 |
def add_noise(img, noise_type="gaussian"):
|
| 54 |
+
img_np = img.numpy()
|
| 55 |
if noise_type == "gaussian":
|
| 56 |
+
noisy_img = random_noise(img_np, mode='gaussian', var=0.1)
|
| 57 |
elif noise_type == "salt_pepper":
|
| 58 |
+
noisy_img = random_noise(img_np, mode='s&p', amount=0.1)
|
| 59 |
return np.clip(noisy_img, 0., 1.)
|
| 60 |
|
|
|
|
| 61 |
def predict_denoised_image(autoencoder, image):
|
|
|
|
| 62 |
img_resized = tf.image.resize(image, (256, 256)) / 255.0
|
| 63 |
img_array = np.expand_dims(img_resized, axis=0)
|
| 64 |
|
|
|
|
| 65 |
noisy_image = add_noise(img_resized)
|
| 66 |
|
|
|
|
| 67 |
denoised_image = autoencoder.predict(np.expand_dims(noisy_image, axis=0))
|
| 68 |
|
|
|
|
| 69 |
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
|
| 70 |
ax[0].imshow(noisy_image)
|
| 71 |
ax[0].set_title("Noisy Image")
|
|
|
|
| 77 |
|
| 78 |
plt.show()
|
| 79 |
|
| 80 |
+
test_image = tf.keras.preprocessing.image.load_img('image.jpg', target_size=(256, 256))
|
|
|
|
| 81 |
test_image = tf.keras.preprocessing.image.img_to_array(test_image)
|
| 82 |
|
|
|
|
| 83 |
predict_denoised_image(autoencoder, test_image)
|
| 84 |
```
|
| 85 |
|