π FocalNet NSFW Image Classifier: Your Content Moderation Superhero! π¦ΈββοΈ
27
2 commits
2 linked in READMEs
updated Sep 13, 2024
π Are you ready for a revolution in content moderation? Meet the FocalNet NSFW Image Classifier - your new, lightning-fast, and super-smart assistant in the battle against inappropriate content!
I'm an advanced AI model, built on the powerful microsoft/focalnet-base. My superpower is the lightning-fast classification of images into three categories:
Imagine you're the guardian of the internet galaxy. Your mission? Protect users from shocking, inappropriate content. But how do you review millions of images daily? That's where I come in!
Ready for an adventure? Here's how you can harness my power:
Install my powers:
pip install transformers==4.37.2 torch==2.3.1 torchvision Pillow
Summon me in your code:
import os
from PIL import Image
import torch
from torchvision import transforms
from transformers import AutoProcessor, FocalNetForImageClassification
# Path to the folder with images
image_folder = ""
# Path to the model
model_path = "MichalMlodawski/nsfw-image-detection-large"
# List of jpg files in the folder
jpg_files = [file for file in os.listdir(image_folder) if file.lower().endswith(".jpg")]
# Check if there are jpg files in the folder
if not jpg_files:
print("π« No jpg files found in folder:", image_folder)
exit()
# Load the model and feature extractor
feature_extractor = AutoProcessor.from_pretrained(model_path)
model = FocalNetForImageClassification.from_pretrained(model_path)
model.eval()
# Image transformations
transform = transforms.Compose([
transforms.Resize((512, 512)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Mapping from model labels to NSFW categories
label_to_category = {
"LABEL_0": "Safe",
"LABEL_1": "Questionable",
"LABEL_2": "Unsafe"
}
# Processing and prediction for each image
results = []
for jpg_file in jpg_files:
selected_image = os.path.join(image_folder, jpg_file)
image = Image.open(selected_image).convert("RGB")
image_tensor = transform(image).unsqueeze(0)
# Process image using feature_extractor
inputs = feature_extractor(images=image, return_tensors="pt")
# Prediction using the model
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
confidence, predicted = torch.max(probabilities, 1)
# Get the label from the model's configuration
label = model.config.id2label[predicted.item()]
results.append((jpg_file, label, confidence.item() * 100))
# Display results
print("πΌοΈ NSFW Classification Results πΌοΈ")
print("=" * 40)
for jpg_file, label, confidence in results:
category = label_to_category.get(label, "Unknown")
emoji = {"Safe": "β
", "Questionable": "β οΈ", "Unsafe": "π"}.get(category, "β")
confidence_bar = "π©" * int(confidence // 10) + "β¬" * (10 - int(confidence // 10))
print(f"π File name: {jpg_file}")
print(f"π·οΈ Model Label: {label}")
print(f"{emoji} NSFW Category: {category}")
print(f"π― Confidence: {confidence:.2f}% {confidence_bar}")
print(f"{'=' * 40}")
print("π Classification completed! π")
Remember, I'm part of an ongoing research process. With each update, I become smarter, faster, and even more incredible!
Ready to revolutionize content moderation together? Bring me on board your project and watch the magic happen! π©β¨
Join the AI revolution today and make the internet a safer place! ππͺ
Let's make the digital world safer, one image at a time! π
2 commits
π FocalNet NSFW Image Classifier: Your Content Moderation Superhero! π¦ΈββοΈ
27
2 commits
2 linked in READMEs
updated Sep 13, 2024
π Are you ready for a revolution in content moderation? Meet the FocalNet NSFW Image Classifier - your new, lightning-fast, and super-smart assistant in the battle against inappropriate content!
I'm an advanced AI model, built on the powerful microsoft/focalnet-base. My superpower is the lightning-fast classification of images into three categories:
Imagine you're the guardian of the internet galaxy. Your mission? Protect users from shocking, inappropriate content. But how do you review millions of images daily? That's where I come in!
Ready for an adventure? Here's how you can harness my power:
Install my powers:
pip install transformers==4.37.2 torch==2.3.1 torchvision Pillow
Summon me in your code:
import os
from PIL import Image
import torch
from torchvision import transforms
from transformers import AutoProcessor, FocalNetForImageClassification
# Path to the folder with images
image_folder = ""
# Path to the model
model_path = "MichalMlodawski/nsfw-image-detection-large"
# List of jpg files in the folder
jpg_files = [file for file in os.listdir(image_folder) if file.lower().endswith(".jpg")]
# Check if there are jpg files in the folder
if not jpg_files:
print("π« No jpg files found in folder:", image_folder)
exit()
# Load the model and feature extractor
feature_extractor = AutoProcessor.from_pretrained(model_path)
model = FocalNetForImageClassification.from_pretrained(model_path)
model.eval()
# Image transformations
transform = transforms.Compose([
transforms.Resize((512, 512)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Mapping from model labels to NSFW categories
label_to_category = {
"LABEL_0": "Safe",
"LABEL_1": "Questionable",
"LABEL_2": "Unsafe"
}
# Processing and prediction for each image
results = []
for jpg_file in jpg_files:
selected_image = os.path.join(image_folder, jpg_file)
image = Image.open(selected_image).convert("RGB")
image_tensor = transform(image).unsqueeze(0)
# Process image using feature_extractor
inputs = feature_extractor(images=image, return_tensors="pt")
# Prediction using the model
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
confidence, predicted = torch.max(probabilities, 1)
# Get the label from the model's configuration
label = model.config.id2label[predicted.item()]
results.append((jpg_file, label, confidence.item() * 100))
# Display results
print("πΌοΈ NSFW Classification Results πΌοΈ")
print("=" * 40)
for jpg_file, label, confidence in results:
category = label_to_category.get(label, "Unknown")
emoji = {"Safe": "β
", "Questionable": "β οΈ", "Unsafe": "π"}.get(category, "β")
confidence_bar = "π©" * int(confidence // 10) + "β¬" * (10 - int(confidence // 10))
print(f"π File name: {jpg_file}")
print(f"π·οΈ Model Label: {label}")
print(f"{emoji} NSFW Category: {category}")
print(f"π― Confidence: {confidence:.2f}% {confidence_bar}")
print(f"{'=' * 40}")
print("π Classification completed! π")
Remember, I'm part of an ongoing research process. With each update, I become smarter, faster, and even more incredible!
Ready to revolutionize content moderation together? Bring me on board your project and watch the magic happen! π©β¨
Join the AI revolution today and make the internet a safer place! ππͺ
Let's make the digital world safer, one image at a time! π
2 commits