sk2003 commited on
Commit
10cb574
·
verified ·
1 Parent(s): d1eee78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -5,31 +5,31 @@ from torchvision import models, transforms
5
  from PIL import Image
6
  from huggingface_hub import hf_hub_download
7
  import torch.nn as nn
8
- import torch.optim as optim
9
 
10
- # LoadING the VGG16 model
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
- vgg16_model_path = hf_hub_download(repo_id="sk2003/style_recognizer_vgg", filename="vgg16_model.pth")
13
 
14
- vgg16 = models.vgg16(pretrained=True)
15
- for param in vgg16.parameters():
 
16
  param.requires_grad = False
17
 
18
- num_classes = 8
19
- vgg16.classifier[6] = nn.Linear(vgg16.classifier[6].in_features, num_classes)
20
- vgg16 = vgg16.to(device)
21
 
22
  # Loading the saved state dict
23
- checkpoint = torch.load(vgg16_model_path, map_location=device)
24
- vgg16.load_state_dict(checkpoint['model_state_dict'])
25
- vgg16.eval()
26
 
27
  # Fine-tuned Stable Diffusion model from your Hugging Face repository
28
  model_id = "sk2003/room-styler"
29
  pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
30
  pipe.to(device)
31
 
32
- # Prediction function for the VGG16 model
33
  def predict_and_show(image):
34
  transform = transforms.Compose([
35
  transforms.Resize((224, 224)),
@@ -39,10 +39,10 @@ def predict_and_show(image):
39
  image_tensor = transform(image).unsqueeze(0).to(device)
40
 
41
  with torch.no_grad():
42
- outputs = vgg16(image_tensor)
43
  _, predicted = torch.max(outputs.data, 1)
44
 
45
- class_names = ["Classic", "Modern", "Vintage", "Glamour", "Scandinavian", "Rustic", "ArtDeco", "Industrial"]
46
  predicted_label = class_names[predicted.item()]
47
 
48
  return predicted_label
 
5
  from PIL import Image
6
  from huggingface_hub import hf_hub_download
7
  import torch.nn as nn
 
8
 
9
+ # Loading the ResNet50 model from your Hugging Face repository
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ resnet50_model_path = hf_hub_download(repo_id="sk2003/style_recognizer_resnet", filename="resnet50_model.pth")
12
 
13
+ # ResNet50 model
14
+ resnet50 = models.resnet50(pretrained=True)
15
+ for param in resnet50.parameters():
16
  param.requires_grad = False
17
 
18
+ num_classes = 8
19
+ resnet50.fc = nn.Linear(resnet50.fc.in_features, num_classes)
20
+ resnet50 = resnet50.to(device)
21
 
22
  # Loading the saved state dict
23
+ checkpoint = torch.load(resnet50_model_path, map_location=device)
24
+ resnet50.load_state_dict(checkpoint['model_state_dict'])
25
+ resnet50.eval()
26
 
27
  # Fine-tuned Stable Diffusion model from your Hugging Face repository
28
  model_id = "sk2003/room-styler"
29
  pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
30
  pipe.to(device)
31
 
32
+ # Prediction function for the ResNet50 model
33
  def predict_and_show(image):
34
  transform = transforms.Compose([
35
  transforms.Resize((224, 224)),
 
39
  image_tensor = transform(image).unsqueeze(0).to(device)
40
 
41
  with torch.no_grad():
42
+ outputs = resnet50(image_tensor)
43
  _, predicted = torch.max(outputs.data, 1)
44
 
45
+ class_names = ["Classic", "Modern", "Vintage", "Glamour", "Scandinavian", "Rustic", "ArtDeco", "Industrial"]
46
  predicted_label = class_names[predicted.item()]
47
 
48
  return predicted_label