Spaces:
Build error
Build error
danielquillanroxas
commited on
Commit
·
9a7d30c
0
Parent(s):
Initial fresh commit with LFS
Browse files- .gitattributes +2 -0
- app.py +489 -0
- models/classification/content_encoder.pkl +0 -0
- models/classification/content_model.h5 +3 -0
- models/classification/style_encoder.pkl +0 -0
- models/classification/style_model.h5 +3 -0
- models/classification/time_of_day_encoder.pkl +0 -0
- models/classification/time_of_day_model.h5 +3 -0
- models/classification/weather_encoder.pkl +0 -0
- models/classification/weather_model.h5 +3 -0
- models/gan/day_night/day_to_night_generator_final.keras +3 -0
- models/gan/day_night/night_to_day_generator_final.keras +3 -0
- models/gan/foggy/foggy_to_normal_generator_final.keras +3 -0
- models/gan/foggy/normal_to_foggy_generator_final.keras +3 -0
- models/gan/japanese/old models/photo_to_ukiyoe_generator.keras +3 -0
- models/gan/japanese/old models/ukiyoe_to_photo_generator.keras +3 -0
- models/gan/japanese/photo_to_ukiyoe_generator.keras +3 -0
- models/gan/japanese/ukiyoe_to_photo_generator.keras +3 -0
- models/gan/summer_winter/summer_to_winter_generator_final.keras +3 -0
- models/gan/summer_winter/winter_to_summer_generator_final.keras +3 -0
- readme.md +57 -0
- requirements.txt +7 -0
.gitattributes
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.keras filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import pickle
|
| 6 |
+
import os
|
| 7 |
+
import tempfile
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
class MultiAttributeClassifier:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.models = {}
|
| 13 |
+
self.encoders = {}
|
| 14 |
+
self.gan_models = {}
|
| 15 |
+
self.confidence_threshold = 0.6
|
| 16 |
+
self.categories = ['content', 'style', 'time_of_day', 'weather']
|
| 17 |
+
self.load_classification_models()
|
| 18 |
+
self.load_gan_models()
|
| 19 |
+
|
| 20 |
+
def load_classification_models(self):
|
| 21 |
+
"""Load all classification models and encoders"""
|
| 22 |
+
print("Loading classification models...")
|
| 23 |
+
|
| 24 |
+
for category in self.categories:
|
| 25 |
+
try:
|
| 26 |
+
# Load model from correct path
|
| 27 |
+
model_path = f"models/classification/{category}_model.h5"
|
| 28 |
+
if os.path.exists(model_path):
|
| 29 |
+
self.models[category] = tf.keras.models.load_model(model_path)
|
| 30 |
+
print(f"✅ Loaded {category} model")
|
| 31 |
+
|
| 32 |
+
# Load encoder
|
| 33 |
+
encoder_path = f"models/classification/{category}_encoder.pkl"
|
| 34 |
+
if os.path.exists(encoder_path):
|
| 35 |
+
with open(encoder_path, 'rb') as f:
|
| 36 |
+
self.encoders[category] = pickle.load(f)
|
| 37 |
+
else:
|
| 38 |
+
print(f"⚠️ {category} encoder not found")
|
| 39 |
+
else:
|
| 40 |
+
print(f"⚠️ {category} model not found at {model_path}")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"❌ Failed to load {category}: {e}")
|
| 43 |
+
|
| 44 |
+
def load_gan_models(self):
|
| 45 |
+
"""Load all GAN models for style transfer"""
|
| 46 |
+
print("Loading GAN models...")
|
| 47 |
+
|
| 48 |
+
gan_paths = {
|
| 49 |
+
# Day/Night models
|
| 50 |
+
'day_to_night': 'models/gan/day_night/day_to_night_generator_final.keras',
|
| 51 |
+
'night_to_day': 'models/gan/day_night/night_to_day_generator_final.keras',
|
| 52 |
+
|
| 53 |
+
# Foggy/Clear models
|
| 54 |
+
'foggy_to_clear': 'models/gan/foggy/foggy_to_normal_generator_final.keras',
|
| 55 |
+
'clear_to_foggy': 'models/gan/foggy/normal_to_foggy_generator_final.keras',
|
| 56 |
+
|
| 57 |
+
# Japanese art models
|
| 58 |
+
'photo_to_japanese': 'models/gan/japanese/photo_to_ukiyoe_generator.keras',
|
| 59 |
+
'japanese_to_photo': 'models/gan/japanese/ukiyoe_to_photo_generator.keras',
|
| 60 |
+
|
| 61 |
+
# Season models
|
| 62 |
+
'summer_to_winter': 'models/gan/summer_winter/summer_to_winter_generator_final.keras',
|
| 63 |
+
'winter_to_summer': 'models/gan/summer_winter/winter_to_summer_generator_final.keras'
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
for model_name, model_path in gan_paths.items():
|
| 67 |
+
try:
|
| 68 |
+
if os.path.exists(model_path):
|
| 69 |
+
self.gan_models[model_name] = tf.keras.models.load_model(model_path)
|
| 70 |
+
print(f"✅ Loaded GAN: {model_name}")
|
| 71 |
+
else:
|
| 72 |
+
print(f"⚠️ GAN model not found: {model_path}")
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f"❌ Failed to load GAN {model_name}: {e}")
|
| 75 |
+
|
| 76 |
+
def preprocess_image_for_classification(self, image_path):
|
| 77 |
+
"""Preprocess image for classification (224x224)"""
|
| 78 |
+
img = cv2.imread(image_path)
|
| 79 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 80 |
+
img = cv2.resize(img, (224, 224))
|
| 81 |
+
img = img.astype(np.float32) / 255.0
|
| 82 |
+
img = (img - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
|
| 83 |
+
img = np.expand_dims(img, axis=0)
|
| 84 |
+
return img
|
| 85 |
+
|
| 86 |
+
def preprocess_image_for_gan(self, image_path, target_size=(256, 256)):
|
| 87 |
+
"""Preprocess image for GAN models (256x256, normalized to [-1,1])"""
|
| 88 |
+
img = cv2.imread(image_path)
|
| 89 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 90 |
+
img = cv2.resize(img, target_size)
|
| 91 |
+
img = img.astype(np.float32)
|
| 92 |
+
img = (img / 127.5) - 1.0 # Normalize to [-1, 1]
|
| 93 |
+
img = np.expand_dims(img, axis=0)
|
| 94 |
+
return img
|
| 95 |
+
|
| 96 |
+
def postprocess_gan_output(self, generated_img):
|
| 97 |
+
"""Convert GAN output back to displayable image"""
|
| 98 |
+
# Convert from [-1, 1] to [0, 255]
|
| 99 |
+
img = (generated_img[0] + 1.0) * 127.5
|
| 100 |
+
img = np.clip(img, 0, 255).astype(np.uint8)
|
| 101 |
+
return Image.fromarray(img)
|
| 102 |
+
|
| 103 |
+
def classify_image(self, image_path):
|
| 104 |
+
"""Classify image across all loaded attributes"""
|
| 105 |
+
img = self.preprocess_image_for_classification(image_path)
|
| 106 |
+
results = {}
|
| 107 |
+
|
| 108 |
+
for category in self.categories:
|
| 109 |
+
if category in self.models and category in self.encoders:
|
| 110 |
+
try:
|
| 111 |
+
# Get predictions
|
| 112 |
+
predictions = self.models[category].predict(img, verbose=0)
|
| 113 |
+
if len(predictions.shape) > 1:
|
| 114 |
+
predictions = predictions[0]
|
| 115 |
+
|
| 116 |
+
# Get class names
|
| 117 |
+
class_names = list(self.encoders[category].keys())
|
| 118 |
+
predicted_idx = np.argmax(predictions)
|
| 119 |
+
confidence = float(predictions[predicted_idx])
|
| 120 |
+
predicted_class = class_names[predicted_idx]
|
| 121 |
+
|
| 122 |
+
# Get top predictions
|
| 123 |
+
top_indices = np.argsort(predictions)[-3:][::-1]
|
| 124 |
+
top_3 = [(class_names[i], float(predictions[i])) for i in top_indices]
|
| 125 |
+
|
| 126 |
+
results[category] = {
|
| 127 |
+
'predicted_class': predicted_class,
|
| 128 |
+
'confidence': confidence,
|
| 129 |
+
'is_confident': confidence >= self.confidence_threshold,
|
| 130 |
+
'top_3': top_3
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
results[category] = {
|
| 135 |
+
'predicted_class': 'error',
|
| 136 |
+
'confidence': 0.0,
|
| 137 |
+
'is_confident': False,
|
| 138 |
+
'error': str(e)
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
return results
|
| 142 |
+
|
| 143 |
+
def apply_gan_transformation(self, image_path, transformation_type):
|
| 144 |
+
"""Apply GAN transformation to image"""
|
| 145 |
+
if transformation_type not in self.gan_models:
|
| 146 |
+
return None, f"GAN model '{transformation_type}' not available"
|
| 147 |
+
|
| 148 |
+
try:
|
| 149 |
+
# Preprocess image for GAN
|
| 150 |
+
img = self.preprocess_image_for_gan(image_path)
|
| 151 |
+
|
| 152 |
+
# Apply transformation
|
| 153 |
+
generated = self.gan_models[transformation_type].predict(img, verbose=0)
|
| 154 |
+
|
| 155 |
+
# Postprocess result
|
| 156 |
+
result_image = self.postprocess_gan_output(generated)
|
| 157 |
+
|
| 158 |
+
return result_image, "Success"
|
| 159 |
+
|
| 160 |
+
except Exception as e:
|
| 161 |
+
return None, f"Error applying {transformation_type}: {str(e)}"
|
| 162 |
+
|
| 163 |
+
def get_available_transfers(classification_results):
|
| 164 |
+
"""Get available style transfers based on classifications"""
|
| 165 |
+
transfers = []
|
| 166 |
+
|
| 167 |
+
for category, result in classification_results.items():
|
| 168 |
+
if not result.get('is_confident', False):
|
| 169 |
+
continue
|
| 170 |
+
|
| 171 |
+
predicted = result['predicted_class']
|
| 172 |
+
confidence = result['confidence']
|
| 173 |
+
|
| 174 |
+
if category == 'time_of_day':
|
| 175 |
+
if predicted == 'day':
|
| 176 |
+
transfers.append({
|
| 177 |
+
'name': 'Day → Night',
|
| 178 |
+
'gan_model': 'day_to_night',
|
| 179 |
+
'confidence': confidence,
|
| 180 |
+
'description': 'Transform daytime scene to nighttime'
|
| 181 |
+
})
|
| 182 |
+
elif predicted == 'night':
|
| 183 |
+
transfers.append({
|
| 184 |
+
'name': 'Night → Day',
|
| 185 |
+
'gan_model': 'night_to_day',
|
| 186 |
+
'confidence': confidence,
|
| 187 |
+
'description': 'Transform nighttime scene to daytime'
|
| 188 |
+
})
|
| 189 |
+
|
| 190 |
+
elif category == 'style':
|
| 191 |
+
if predicted == 'photograph':
|
| 192 |
+
transfers.append({
|
| 193 |
+
'name': 'Photo → Japanese Art',
|
| 194 |
+
'gan_model': 'photo_to_japanese',
|
| 195 |
+
'confidence': confidence,
|
| 196 |
+
'description': 'Convert realistic photo to Japanese ukiyo-e art style'
|
| 197 |
+
})
|
| 198 |
+
elif predicted == 'japanese_art':
|
| 199 |
+
transfers.append({
|
| 200 |
+
'name': 'Japanese Art → Photo',
|
| 201 |
+
'gan_model': 'japanese_to_photo',
|
| 202 |
+
'confidence': confidence,
|
| 203 |
+
'description': 'Convert artistic style to realistic photo'
|
| 204 |
+
})
|
| 205 |
+
|
| 206 |
+
elif category == 'weather':
|
| 207 |
+
if predicted == 'foggy':
|
| 208 |
+
transfers.append({
|
| 209 |
+
'name': 'Foggy → Clear',
|
| 210 |
+
'gan_model': 'foggy_to_clear',
|
| 211 |
+
'confidence': confidence,
|
| 212 |
+
'description': 'Remove fog and enhance visibility'
|
| 213 |
+
})
|
| 214 |
+
elif predicted == 'clear':
|
| 215 |
+
transfers.append({
|
| 216 |
+
'name': 'Clear → Foggy',
|
| 217 |
+
'gan_model': 'clear_to_foggy',
|
| 218 |
+
'confidence': confidence,
|
| 219 |
+
'description': 'Add atmospheric fog effect'
|
| 220 |
+
})
|
| 221 |
+
|
| 222 |
+
# Add season transfers (these work regardless of season classification)
|
| 223 |
+
# You might want to add season classification logic here if you have a season model
|
| 224 |
+
transfers.extend([
|
| 225 |
+
{
|
| 226 |
+
'name': 'Add Winter Atmosphere',
|
| 227 |
+
'gan_model': 'summer_to_winter',
|
| 228 |
+
'confidence': 0.8,
|
| 229 |
+
'description': 'Transform scene to winter with snow and cold atmosphere'
|
| 230 |
+
},
|
| 231 |
+
{
|
| 232 |
+
'name': 'Add Summer Atmosphere',
|
| 233 |
+
'gan_model': 'winter_to_summer',
|
| 234 |
+
'confidence': 0.8,
|
| 235 |
+
'description': 'Transform scene to summer with warm, lush atmosphere'
|
| 236 |
+
}
|
| 237 |
+
])
|
| 238 |
+
|
| 239 |
+
return transfers
|
| 240 |
+
|
| 241 |
+
# Initialize classifier globally
|
| 242 |
+
classifier = MultiAttributeClassifier()
|
| 243 |
+
|
| 244 |
+
def analyze_image(image):
|
| 245 |
+
"""Main analysis function"""
|
| 246 |
+
if image is None:
|
| 247 |
+
return "Please upload an image first!", gr.update(choices=[], visible=False), gr.update(visible=False)
|
| 248 |
+
|
| 249 |
+
# Save uploaded image to temporary file
|
| 250 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file:
|
| 251 |
+
image.save(tmp_file.name)
|
| 252 |
+
temp_path = tmp_file.name
|
| 253 |
+
|
| 254 |
+
try:
|
| 255 |
+
# Get classifications
|
| 256 |
+
results = classifier.classify_image(temp_path)
|
| 257 |
+
|
| 258 |
+
# Format analysis results
|
| 259 |
+
analysis_text = "## 📊 Image Analysis Results\n\n"
|
| 260 |
+
|
| 261 |
+
for category, result in results.items():
|
| 262 |
+
if 'error' not in result:
|
| 263 |
+
confidence = result['confidence']
|
| 264 |
+
status = "✅ CONFIDENT" if result['is_confident'] else "⚠️ UNCERTAIN"
|
| 265 |
+
predicted = result['predicted_class']
|
| 266 |
+
|
| 267 |
+
analysis_text += f"**{category.replace('_', ' ').title()}:** {predicted} ({confidence:.1%}) {status}\n\n"
|
| 268 |
+
|
| 269 |
+
# Show top alternatives
|
| 270 |
+
if len(result['top_3']) > 1:
|
| 271 |
+
alt_text = ", ".join([f"{name} ({score:.1%})" for name, score in result['top_3'][1:]])
|
| 272 |
+
analysis_text += f" *Alternatives: {alt_text}*\n\n"
|
| 273 |
+
else:
|
| 274 |
+
analysis_text += f"**{category.replace('_', ' ').title()}:** Error - {result.get('error', 'Unknown error')}\n\n"
|
| 275 |
+
|
| 276 |
+
# Get available transfers
|
| 277 |
+
transfers = get_available_transfers(results)
|
| 278 |
+
|
| 279 |
+
if transfers:
|
| 280 |
+
analysis_text += "## 🎨 Available Style Transfers\n\n"
|
| 281 |
+
transfer_choices = []
|
| 282 |
+
|
| 283 |
+
for transfer in transfers:
|
| 284 |
+
analysis_text += f"**{transfer['name']}** ({transfer['confidence']:.1%})\n"
|
| 285 |
+
analysis_text += f"*{transfer['description']}*\n\n"
|
| 286 |
+
transfer_choices.append(transfer['name'])
|
| 287 |
+
else:
|
| 288 |
+
analysis_text += "## ⚠️ No Style Transfers Available\n\n"
|
| 289 |
+
analysis_text += "Could not confidently classify the image for available transformations.\n\n"
|
| 290 |
+
transfer_choices = []
|
| 291 |
+
|
| 292 |
+
return (
|
| 293 |
+
analysis_text,
|
| 294 |
+
gr.update(choices=transfer_choices, visible=len(transfer_choices) > 0),
|
| 295 |
+
gr.update(visible=len(transfer_choices) > 0)
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
except Exception as e:
|
| 299 |
+
return f"Error analyzing image: {str(e)}", gr.update(choices=[], visible=False), gr.update(visible=False)
|
| 300 |
+
|
| 301 |
+
finally:
|
| 302 |
+
# Clean up temp file
|
| 303 |
+
if os.path.exists(temp_path):
|
| 304 |
+
os.unlink(temp_path)
|
| 305 |
+
|
| 306 |
+
def apply_style_transfer(original_image, selected_transfers):
|
| 307 |
+
"""Apply selected style transfers using actual GAN models"""
|
| 308 |
+
if not selected_transfers:
|
| 309 |
+
return original_image, "⚠️ No transfers selected!"
|
| 310 |
+
|
| 311 |
+
if original_image is None:
|
| 312 |
+
return None, "⚠️ No image provided!"
|
| 313 |
+
|
| 314 |
+
# Save original image to temp file
|
| 315 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file:
|
| 316 |
+
original_image.save(tmp_file.name)
|
| 317 |
+
temp_path = tmp_file.name
|
| 318 |
+
|
| 319 |
+
result_text = "## 🎨 Applied Transformations\n\n"
|
| 320 |
+
current_image = original_image
|
| 321 |
+
|
| 322 |
+
try:
|
| 323 |
+
# Map transfer names to GAN models
|
| 324 |
+
transfer_mapping = {
|
| 325 |
+
'Day → Night': 'day_to_night',
|
| 326 |
+
'Night → Day': 'night_to_day',
|
| 327 |
+
'Photo → Japanese Art': 'photo_to_japanese',
|
| 328 |
+
'Japanese Art → Photo': 'japanese_to_photo',
|
| 329 |
+
'Foggy → Clear': 'foggy_to_clear',
|
| 330 |
+
'Clear → Foggy': 'clear_to_foggy',
|
| 331 |
+
'Add Winter Atmosphere': 'summer_to_winter',
|
| 332 |
+
'Add Summer Atmosphere': 'winter_to_summer'
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
# Apply each selected transformation
|
| 336 |
+
for transfer_name in selected_transfers:
|
| 337 |
+
if transfer_name in transfer_mapping:
|
| 338 |
+
gan_model = transfer_mapping[transfer_name]
|
| 339 |
+
|
| 340 |
+
# Apply transformation
|
| 341 |
+
transformed_image, status = classifier.apply_gan_transformation(temp_path, gan_model)
|
| 342 |
+
|
| 343 |
+
if transformed_image is not None:
|
| 344 |
+
result_text += f"✅ **{transfer_name}** - {status}\n"
|
| 345 |
+
current_image = transformed_image
|
| 346 |
+
|
| 347 |
+
# Save transformed image for next transformation
|
| 348 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as new_tmp:
|
| 349 |
+
transformed_image.save(new_tmp.name)
|
| 350 |
+
if os.path.exists(temp_path):
|
| 351 |
+
os.unlink(temp_path)
|
| 352 |
+
temp_path = new_tmp.name
|
| 353 |
+
else:
|
| 354 |
+
result_text += f"❌ **{transfer_name}** - {status}\n"
|
| 355 |
+
else:
|
| 356 |
+
result_text += f"⚠️ **{transfer_name}** - Transfer not implemented\n"
|
| 357 |
+
|
| 358 |
+
result_text += f"\n🎉 **Transformation{'s' if len(selected_transfers) > 1 else ''} complete!**\n\n"
|
| 359 |
+
|
| 360 |
+
if len(selected_transfers) > 1:
|
| 361 |
+
result_text += "*Multiple transformations were applied in sequence for a combined effect.*"
|
| 362 |
+
|
| 363 |
+
return current_image, result_text
|
| 364 |
+
|
| 365 |
+
except Exception as e:
|
| 366 |
+
return original_image, f"❌ Error during transformation: {str(e)}"
|
| 367 |
+
|
| 368 |
+
finally:
|
| 369 |
+
# Clean up temp file
|
| 370 |
+
if os.path.exists(temp_path):
|
| 371 |
+
os.unlink(temp_path)
|
| 372 |
+
|
| 373 |
+
# Create Gradio interface
|
| 374 |
+
with gr.Blocks(
|
| 375 |
+
title="🎨 Intelligent Style Transfer System",
|
| 376 |
+
theme=gr.themes.Soft()
|
| 377 |
+
) as demo:
|
| 378 |
+
|
| 379 |
+
gr.Markdown("""
|
| 380 |
+
# 🎨 Intelligent Multi-Attribute Style Transfer
|
| 381 |
+
|
| 382 |
+
Upload an image and our AI will analyze multiple attributes (content, style, time, weather)
|
| 383 |
+
and suggest relevant style transfers using trained GAN models!
|
| 384 |
+
|
| 385 |
+
**Available Transformations:**
|
| 386 |
+
- 🌅 Day ↔ Night conversion (CycleGAN)
|
| 387 |
+
- 🎨 Photo ↔ Japanese Ukiyo-e art style (CycleGAN)
|
| 388 |
+
- 🌫️ Foggy ↔ Clear weather transformation (CycleGAN)
|
| 389 |
+
- 🌿 Summer ↔ Winter seasonal atmosphere (CycleGAN)
|
| 390 |
+
""")
|
| 391 |
+
|
| 392 |
+
with gr.Row():
|
| 393 |
+
with gr.Column(scale=1):
|
| 394 |
+
input_image = gr.Image(
|
| 395 |
+
type="pil",
|
| 396 |
+
label="📤 Upload Your Image",
|
| 397 |
+
height=400
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
analyze_btn = gr.Button(
|
| 401 |
+
"🔍 Analyze Image",
|
| 402 |
+
variant="primary",
|
| 403 |
+
size="lg"
|
| 404 |
+
)
|
| 405 |
+
|
| 406 |
+
with gr.Column(scale=1):
|
| 407 |
+
analysis_output = gr.Markdown(
|
| 408 |
+
value="Upload an image and click 'Analyze Image' to see what our AI detects!",
|
| 409 |
+
height=400
|
| 410 |
+
)
|
| 411 |
+
|
| 412 |
+
with gr.Row():
|
| 413 |
+
transfer_selector = gr.CheckboxGroup(
|
| 414 |
+
label="🎨 Select Style Transfers to Apply",
|
| 415 |
+
choices=[],
|
| 416 |
+
visible=False
|
| 417 |
+
)
|
| 418 |
+
|
| 419 |
+
with gr.Row():
|
| 420 |
+
apply_btn = gr.Button(
|
| 421 |
+
"✨ Apply Selected Transfers",
|
| 422 |
+
variant="secondary",
|
| 423 |
+
visible=False,
|
| 424 |
+
size="lg"
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
with gr.Row():
|
| 428 |
+
with gr.Column(scale=1):
|
| 429 |
+
output_image = gr.Image(
|
| 430 |
+
label="🎉 Transformed Image",
|
| 431 |
+
height=400
|
| 432 |
+
)
|
| 433 |
+
|
| 434 |
+
with gr.Column(scale=1):
|
| 435 |
+
result_output = gr.Markdown(
|
| 436 |
+
value="Select transfers and click 'Apply' to see the magic happen!"
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
# Example images (add some to your examples folder)
|
| 440 |
+
gr.Examples(
|
| 441 |
+
examples=[
|
| 442 |
+
["examples/example(1).jpg"],
|
| 443 |
+
["examples/example(2).jpg"],
|
| 444 |
+
["examples/example(3).jpg"],
|
| 445 |
+
["examples/example(4).jpg"],
|
| 446 |
+
["examples/example(5).jpg"],
|
| 447 |
+
["examples/example(6).jpg"],
|
| 448 |
+
],
|
| 449 |
+
inputs=input_image,
|
| 450 |
+
label="🖼️ Try Example Images"
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
# Connect the interface
|
| 454 |
+
analyze_btn.click(
|
| 455 |
+
fn=analyze_image,
|
| 456 |
+
inputs=[input_image],
|
| 457 |
+
outputs=[analysis_output, transfer_selector, apply_btn]
|
| 458 |
+
)
|
| 459 |
+
|
| 460 |
+
apply_btn.click(
|
| 461 |
+
fn=apply_style_transfer,
|
| 462 |
+
inputs=[input_image, transfer_selector],
|
| 463 |
+
outputs=[output_image, result_output]
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
gr.Markdown("""
|
| 467 |
+
---
|
| 468 |
+
### 🔧 Technical Details
|
| 469 |
+
|
| 470 |
+
This system uses multiple trained models:
|
| 471 |
+
|
| 472 |
+
**Classification Models:**
|
| 473 |
+
- **Content classifier**: Human vs Landscape (97% accuracy)
|
| 474 |
+
- **Style classifier**: Photograph vs Japanese Art (92% accuracy)
|
| 475 |
+
- **Time classifier**: Day vs Night (90% accuracy)
|
| 476 |
+
- **Weather classifier**: Foggy vs Clear (85% accuracy)
|
| 477 |
+
|
| 478 |
+
**GAN Models:**
|
| 479 |
+
- **Day/Night**: CycleGAN for time-of-day transformation
|
| 480 |
+
- **Style Transfer**: CycleGAN for photo ↔ Japanese art conversion
|
| 481 |
+
- **Weather**: CycleGAN for fog removal/addition
|
| 482 |
+
- **Seasons**: CycleGAN for summer ↔ winter atmosphere
|
| 483 |
+
|
| 484 |
+
Only confident predictions (>60%) trigger relevant style transfer suggestions.
|
| 485 |
+
Multiple transformations can be combined for creative effects!
|
| 486 |
+
""")
|
| 487 |
+
|
| 488 |
+
if __name__ == "__main__":
|
| 489 |
+
demo.launch()
|
models/classification/content_encoder.pkl
ADDED
|
Binary file (40 Bytes). View file
|
|
|
models/classification/content_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cebaa9f0c5e64fdff5eff7860c3173d8e5ca7a51325d4bdb963061a21b93d27c
|
| 3 |
+
size 101454064
|
models/classification/style_encoder.pkl
ADDED
|
Binary file (48 Bytes). View file
|
|
|
models/classification/style_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0526c3c8036ff799cc0b6bc4323b74651ee9e8398499243561329da5a11bae4e
|
| 3 |
+
size 97904800
|
models/classification/time_of_day_encoder.pkl
ADDED
|
Binary file (234 Bytes). View file
|
|
|
models/classification/time_of_day_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:895f4a9b819e6cfc97c54715d7fc27e708f676e08c8c2a8851f3a37eb5851d13
|
| 3 |
+
size 90220424
|
models/classification/weather_encoder.pkl
ADDED
|
Binary file (67 Bytes). View file
|
|
|
models/classification/weather_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:71263e6c5d531f5a66b098718bde4f05e4d548a73a666d4ebf48b51cb238f024
|
| 3 |
+
size 11380896
|
models/gan/day_night/day_to_night_generator_final.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f4718c29bda4e8503b78cbab343d17affd8bd33077b1b99f1f0b4048ea6982eb
|
| 3 |
+
size 16810357
|
models/gan/day_night/night_to_day_generator_final.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:066336b5fad006ec9ad69d253d36ebebb2730dcbd255ab6b220bb1c613e60e5d
|
| 3 |
+
size 16810430
|
models/gan/foggy/foggy_to_normal_generator_final.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bdff3b35e503adcd7029ee7c7ddbe2c005d9ac1bf21e8777b2b25d68109736d9
|
| 3 |
+
size 16810430
|
models/gan/foggy/normal_to_foggy_generator_final.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2452b59d6acf605f6650a99604c323c5dcdd018fc150b23ab2180d6891474bd6
|
| 3 |
+
size 16810357
|
models/gan/japanese/old models/photo_to_ukiyoe_generator.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dd283e2af32699564dc09b95176ba313d6265801e0eba5d23c1c82492277bf18
|
| 3 |
+
size 66776713
|
models/gan/japanese/old models/ukiyoe_to_photo_generator.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c2f2b967acea3f8ef6fc5af683579a7ef1cd58f3885594c3ecc280d3094ed577
|
| 3 |
+
size 66776786
|
models/gan/japanese/photo_to_ukiyoe_generator.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:be3ce4020e54893403f842d76335836e971996300ecb0ebfc4b5c893c956521a
|
| 3 |
+
size 16810357
|
models/gan/japanese/ukiyoe_to_photo_generator.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d342b6d5c41e3051e3e06f5c675ea94bedba658f5a1a2eeaa1e0f6e4f6f05d34
|
| 3 |
+
size 16810430
|
models/gan/summer_winter/summer_to_winter_generator_final.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:94a1c8cec3157b0e0a7c2fb143a3d4914342a29de6a7155f33c62fc84f39432f
|
| 3 |
+
size 66776713
|
models/gan/summer_winter/winter_to_summer_generator_final.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c966e6270224ec9e5271e02b5c434d02160dd52e11b0a9c91a73fc7d05ef73a
|
| 3 |
+
size 66776786
|
readme.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Intelligent Style Transfer System
|
| 3 |
+
emoji: 🎨
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# 🎨 Intelligent Multi-Attribute Style Transfer
|
| 14 |
+
|
| 15 |
+
An AI-powered system that analyzes images across multiple attributes and intelligently suggests relevant style transfers.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- **Multi-Attribute Analysis**: Simultaneously classifies content, style, time of day, and weather
|
| 20 |
+
- **Intelligent Recommendations**: Only suggests style transfers that make sense for your image
|
| 21 |
+
- **User Choice**: Pick single effects or combine multiple transformations
|
| 22 |
+
- **High Accuracy**: 90%+ accuracy on key classification tasks
|
| 23 |
+
|
| 24 |
+
## How It Works
|
| 25 |
+
|
| 26 |
+
1. **Upload** your image
|
| 27 |
+
2. **AI Analysis** across 4 different attributes
|
| 28 |
+
3. **Smart Suggestions** based on confident predictions
|
| 29 |
+
4. **Apply** single or combined style transfers
|
| 30 |
+
|
| 31 |
+
## Available Transformations
|
| 32 |
+
|
| 33 |
+
- 🌅 Day ↔ Night conversion
|
| 34 |
+
- 🎨 Photo ↔ Japanese Art style
|
| 35 |
+
- 🌫️ Fog removal (Foggy → Clear)
|
| 36 |
+
- 🖼️ Content-aware enhancement
|
| 37 |
+
|
| 38 |
+
## Technical Details
|
| 39 |
+
|
| 40 |
+
This system uses multiple trained CNN models:
|
| 41 |
+
|
| 42 |
+
- **Content**: Human vs Landscape classification (97% accuracy)
|
| 43 |
+
- **Style**: Photograph vs Japanese Art classification (92% accuracy)
|
| 44 |
+
- **Time**: Day vs Night classification (90% accuracy)
|
| 45 |
+
- **Weather**: Foggy vs Clear classification (85% accuracy)
|
| 46 |
+
|
| 47 |
+
Only confident predictions (>60%) trigger style transfer suggestions, ensuring relevant recommendations.
|
| 48 |
+
|
| 49 |
+
## Model Architecture
|
| 50 |
+
|
| 51 |
+
- Base: ResNet50 and MobileNetV2 with ImageNet pretraining
|
| 52 |
+
- Training: Transfer learning with data augmentation
|
| 53 |
+
- Deployment: TensorFlow models optimized for inference
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
_Built with Gradio, TensorFlow, and ❤️_
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
tensorflow==2.15.0
|
| 3 |
+
opencv-python-headless==4.8.1.78
|
| 4 |
+
Pillow==10.0.1
|
| 5 |
+
numpy==1.24.3
|
| 6 |
+
huggingface-hub==0.17.3
|
| 7 |
+
scikit-learn==1.3.0
|