faranbutt789 commited on
Commit
f0ab5ad
·
verified ·
1 Parent(s): b791b88

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import io
3
+ import os
4
+ from typing import List
5
+ import gradio as gr
6
+ import torch
7
+ import torch.nn as nn
8
+ import torchvision.models as models
9
+ import torchvision.transforms as T
10
+ from PIL import Image, ImageDraw, ImageFont
11
+ import numpy as np
12
+
13
+
14
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+
16
+
17
+ class AgeGenderClassifier(nn.Module):
18
+ def __init__(self):
19
+ super(AgeGenderClassifier, self).__init__()
20
+
21
+ self.intermediate = nn.Sequential(
22
+ nn.Linear(2048, 512),
23
+ nn.ReLU(),
24
+ nn.Dropout(0.4),
25
+ nn.Linear(512, 128),
26
+ nn.ReLU(),
27
+ nn.Dropout(0.4),
28
+ nn.Linear(128, 64),
29
+ nn.ReLU(),
30
+ )
31
+ self.age_classifier = nn.Sequential(
32
+ nn.Linear(64, 1),
33
+ nn.Sigmoid()
34
+ )
35
+ self.gender_classifier = nn.Sequential(
36
+ nn.Linear(64, 1),
37
+ nn.Sigmoid()
38
+ )
39
+
40
+
41
+ def forward(self, x):
42
+ x = self.intermediate(x)
43
+ age = self.age_classifier(x)
44
+ gender = self.gender_classifier(x)
45
+ return age, gender
46
+
47
+
48
+
49
+
50
+ def build_model(weights_path: str):
51
+ """Rebuild VGG16 backbone + custom avgpool/classifier then load weights."""
52
+ backbone = models.vgg16(weights=models.VGG16_Weights.IMAGENET1K_V1)
53
+
54
+ for p in backbone.parameters():
55
+ p.requires_grad = False
56
+
57
+
58
+
59
+ for p in backbone.features[24:].parameters():
60
+ p.requires_grad = True
61
+
62
+
63
+ backbone.avgpool = nn.Sequential(
64
+ nn.Conv2d(512, 512, kernel_size=3),
65
+ nn.MaxPool2d(2),
66
+ nn.ReLU(),
67
+ nn.Flatten()
68
+ demo.launch()