Downloads · 30 days
0
minchyeom/Aesthetic-Anime-Art-Predictor
Aesthetic-Anime-Art-Predictor is a machine learning model from minchyeom. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as apache-2.0.
Made for my friend. This model classifies if the given anime art is good one or not.
Downloads · 30 days
0
Access
Public
Updated Feb 23, 2024
Repo size
105 MB
Likes
0
Public
Click a slice to open those files.
.pth105 MB · 100%
From the Hugging Face model README
Made for my friend. This model classifies if the given anime art is good one or not.
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from PIL import Image
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class CNN(nn.Module):
def __init__(self, num_classes=2, neurons=128):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(128, 128, kernel_size=3, padding=1)
self.conv5 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
self.conv6 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(256 * 12 * 12, neurons)
self.fc2 = nn.Linear(neurons, num_classes)
self.dropout = nn.Dropout(0.5)
self.batch_norm1 = nn.BatchNorm2d(32)
self.batch_norm2 = nn.BatchNorm2d(64)
self.batch_norm3 = nn.BatchNorm2d(128)
self.batch_norm4 = nn.BatchNorm2d(128)
self.batch_norm5 = nn.BatchNorm2d(256)
self.batch_norm6 = nn.BatchNorm2d(256)
def forward(self, x):
x = self.pool(torch.relu(self.batch_norm1(self.conv1(x))))
x = self.pool(torch.relu(self.batch_norm2(self.conv2(x))))
x = self.pool(torch.relu(self.batch_norm3(self.conv3(x))))
x = self.pool(torch.relu(self.batch_norm4(self.conv4(x))))
x = self.pool(torch.relu(self.batch_norm5(self.conv5(x))))
x = self.pool(torch.relu(self.batch_norm6(self.conv6(x))))
x = x.view(-1, 256 * 12 * 12)
x = self.dropout(x)
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = CNN().to(device).half() ## CHANGE THIS LINE
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=2.5e-5)
transform = transforms.Compose([
transforms.Resize((768, 768)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
def infer(model, image_path):
model.eval()
image = Image.open(image_path)
image = transform(image).unsqueeze(0).to(device).half() ## ALSO CHANGE THIS
with torch.no_grad():
output = model(image)
predicted_class = torch.argmax(output).item()
return predicted_class
checkpoint = torch.load('half_precision_model_checkpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']
image_path = 'bad.jpg'
predicted_class = infer(model, image_path)
if int(predicted_class) == 0:
print('Predicted class: Bad Image')
elif int(predicted_class) == 1:
print('Predicted class: Good Image')
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from PIL import Image
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class CNN(nn.Module):
def __init__(self, num_classes=2, neurons=128):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(128, 128, kernel_size=3, padding=1)
self.conv5 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
self.conv6 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(256 * 12 * 12, neurons)
self.fc2 = nn.Linear(neurons, num_classes)
self.dropout = nn.Dropout(0.5)
self.batch_norm1 = nn.BatchNorm2d(32)
self.batch_norm2 = nn.BatchNorm2d(64)
self.batch_norm3 = nn.BatchNorm2d(128)
self.batch_norm4 = nn.BatchNorm2d(128)
self.batch_norm5 = nn.BatchNorm2d(256)
self.batch_norm6 = nn.BatchNorm2d(256)
def forward(self, x):
x = self.pool(torch.relu(self.batch_norm1(self.conv1(x))))
x = self.pool(torch.relu(self.batch_norm2(self.conv2(x))))
x = self.pool(torch.relu(self.batch_norm3(self.conv3(x))))
x = self.pool(torch.relu(self.batch_norm4(self.conv4(x))))
x = self.pool(torch.relu(self.batch_norm5(self.conv5(x))))
x = self.pool(torch.relu(self.batch_norm6(self.conv6(x))))
x = x.view(-1, 256 * 12 * 12)
x = self.dropout(x)
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = CNN().to(device) ## CHANGE THIS LINE
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=2.5e-5)
transform = transforms.Compose([
transforms.Resize((768, 768)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
def infer(model, image_path):
model.eval()
image = Image.open(image_path)
image = transform(image).unsqueeze(0).to(device) ## ALSO CHANGE THIS
with torch.no_grad():
output = model(image)
predicted_class = torch.argmax(output).item()
return predicted_class
checkpoint = torch.load('model_checkpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']
image_path = 'bad.jpg'
predicted_class = infer(model, image_path)
if int(predicted_class) == 0:
print('Predicted class: Bad Image')
elif int(predicted_class) == 1:
print('Predicted class: Good Image')