Reacher commited on
Commit
3a9d31e
·
1 Parent(s): 9a8aede

first commit

Browse files
Files changed (6) hide show
  1. 10013.jpg +0 -0
  2. 10061.jpg +0 -0
  3. app.py +16 -0
  4. model.py +28 -0
  5. predict.py +21 -0
  6. utils.py +22 -0
10013.jpg ADDED
10061.jpg ADDED
app.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from predict import predict_one_image
3
+
4
+ input_image = [
5
+ gr.components.Image(type='filepath',label='Input Image')
6
+ ]
7
+ label = gr.outputs.Label()
8
+ examples = ['10061.jpg','10013.jpg']
9
+ gr.Interface(
10
+ fn=predict_one_image,
11
+ inputs=input_image,
12
+ outputs=label,
13
+ title="Ai Image Generated Detector app",
14
+ examples=examples,
15
+ cache_examples=False,
16
+ ).launch(share=True)
model.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ from torch import nn
4
+ import timm
5
+ from torch.nn.parameter import Parameter
6
+
7
+ class Backbone(nn.Module) :
8
+ def __init__(self,name,pretrained) :
9
+ super(Backbone,self).__init__()
10
+ self.net = timm.create_model(name,pretrained=pretrained)
11
+ self.out_features = self.net.get_classifier().in_features
12
+ def forward(self,x) :
13
+ x = self.net.forward_features(x)
14
+ return x
15
+
16
+ class CustomModel(nn.Module) :
17
+ def __init__(self) :
18
+ super(CustomModel,self).__init__()
19
+ self.backbone = Backbone("tf_efficientnetv2_b0",False)
20
+ self.pooling = nn.AdaptiveAvgPool2d(1)
21
+ self.head = nn.Linear(self.backbone.out_features,1)
22
+ def forward(self,x) :
23
+ x = self.backbone(x)
24
+ x = self.pooling(x).squeeze()
25
+ target = self.head(x)
26
+ output = {}
27
+ output['label'] = target
28
+ return output
predict.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from utils import read_image,get_valid_augs
3
+ import torch
4
+ import torch.nn.functional as F
5
+ from model import CustomModel
6
+
7
+ CKPT = 'fold_0.pt'
8
+ Targets = ['Not AI' 'AI Generated']
9
+ def predict_one_image(path) :
10
+ image = read_image(path)
11
+ image = get_valid_augs()(image=image)['image']
12
+ image = torch.tensor(image,dtype=torch.float)
13
+ image = image.reshape((1,3,224,224))
14
+ model = CustomModel()
15
+ #loading ckpt
16
+ model.load_state_dict(torch.load(CKPT,map_location=torch.device('cpu')))
17
+ with torch.no_grad() :
18
+ outputs = model(image)
19
+ proba = F.sigmoid(outputs['label']).detach().numpy()[0]
20
+ return {'Not AI' : 1-float(proba),'AI' : float(proba)}#(proba>0.5)*1
21
+
utils.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import albumentations as A
3
+ from albumentations.pytorch import ToTensorV2
4
+
5
+ IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
6
+ IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
7
+
8
+ def read_image(path) :
9
+ img = cv2.imread(path)
10
+ img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
11
+ return img
12
+
13
+ def get_valid_augs() :
14
+ return A.Compose([
15
+ A.Resize(height=224, width=224, always_apply=True, p=1),
16
+ A.Normalize(
17
+ mean = IMAGENET_DEFAULT_MEAN,
18
+ std = IMAGENET_DEFAULT_STD,
19
+ max_pixel_value=255
20
+ ),
21
+ ToTensorV2(),
22
+ ])