Prasanta4 commited on
Commit
01acb13
·
verified ·
1 Parent(s): fba2954

Upload 5 files

Browse files
Files changed (5) hide show
  1. GB_stu_mob.pth +3 -0
  2. app.py +72 -0
  3. dockerfile +32 -0
  4. model.py +93 -0
  5. requirements.txt +5 -0
GB_stu_mob.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cfae4e0e40798907c65fec0776f72b7f13b23dacd171ee515ad63f4e1512929f
3
+ size 9217869
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torchvision import transforms
4
+ from model import ModifiedMobileNetV2
5
+ import numpy as np
6
+ from PIL import Image
7
+ from fastapi import FastAPI, File, UploadFile, HTTPException
8
+ from io import BytesIO
9
+ import logging
10
+
11
+ # Set up logging
12
+ logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
+
15
+ app = FastAPI()
16
+
17
+ # Class names provided by user
18
+ class_names = ['Gallstones', 'Cholecystitis', 'Gangrenous_Cholecystitis', 'Perforation', 'Polyps&Cholesterol_Crystal', 'WallThickening', 'Adenomyomatosis', 'Carcinoma', 'Intra-abdominal&Retroperitoneum', 'Normal']
19
+
20
+ # Device setup
21
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
22
+ logger.info(f"Using device: {device}")
23
+
24
+ # Load model
25
+ try:
26
+ model = ModifiedMobileNetV2(num_classes=len(class_names)).to(device)
27
+ model.load_state_dict(torch.load('GB_stu_mob.pth', map_location=device))
28
+ model.eval()
29
+ logger.info("Model loaded successfully")
30
+ except Exception as e:
31
+ logger.error(f"Error loading model: {str(e)}")
32
+ raise
33
+
34
+ # Preprocessing
35
+ preprocess = transforms.Compose([
36
+ transforms.Resize((224, 224)),
37
+ transforms.ToTensor(),
38
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
39
+ ])
40
+
41
+ # Inference function
42
+ def predict(image):
43
+ with torch.no_grad():
44
+ if not torch.is_tensor(image):
45
+ image = preprocess(image).unsqueeze(0)
46
+ image = image.to(device)
47
+ output = model(image)
48
+ probabilities = torch.softmax(output, dim=1)
49
+ predicted_class = torch.argmax(probabilities, dim=1)
50
+ confidence_score = probabilities[0, predicted_class.item()].item()
51
+ return class_names[predicted_class.item()], confidence_score
52
+
53
+ @app.post("/predict")
54
+ async def predict_image(file: UploadFile = File(...)):
55
+ try:
56
+ # Read image file
57
+ contents = await file.read()
58
+ image = Image.open(BytesIO(contents)).convert('RGB')
59
+ # Run prediction
60
+ class_name, confidence_score = predict(image)
61
+ return {
62
+ "filename": file.filename,
63
+ "predicted_class": class_name,
64
+ "confidence_score": confidence_score
65
+ }
66
+ except Exception as e:
67
+ logger.error(f"Error processing image: {str(e)}")
68
+ raise HTTPException(status_code=400, detail=f"Error processing image: {str(e)}")
69
+
70
+ @app.get("/")
71
+ async def root():
72
+ return {"message": "Welcome to the ModifiedMobileNetV2 API. Use POST /predict to upload an image."}
dockerfile ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ # Install system dependencies for pillow
6
+ RUN apt-get update && apt-get install -y \
7
+ libpng-dev \
8
+ libjpeg-dev \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ # Copy and install requirements first for caching
12
+ COPY ./requirements.txt /code/requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
14
+
15
+ # Create non-root user
16
+ RUN useradd user
17
+ USER user
18
+ ENV HOME=/home/user \
19
+ PATH=/home/user/.local/bin:$PATH
20
+
21
+ # Set working directory for app
22
+ WORKDIR $HOME/app
23
+
24
+ # Copy application files with correct ownership
25
+ COPY --chown=user ./app.py $HOME/app/app.py
26
+ COPY --chown=user ./model.py $HOME/app/model.py
27
+ COPY --chown=user ./GB_stu_mob.pth $HOME/app/GB_stu_mob.pth
28
+
29
+ # Expose port
30
+ EXPOSE 7860
31
+
32
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
model.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torchvision.models import mobilenet_v2
4
+
5
+ class TripletAttention(nn.Module):
6
+ def __init__(self, in_channels, kernel_size=7):
7
+ super(TripletAttention, self).__init__()
8
+ self.conv1 = nn.Conv2d(2, 1, kernel_size=kernel_size, padding=kernel_size // 2)
9
+ self.sigmoid = nn.Sigmoid()
10
+ def forward(self, x):
11
+ x_perm1 = x
12
+ x_perm2 = x.permute(0, 2, 1, 3)
13
+ x_perm3 = x.permute(0, 3, 2, 1)
14
+ out1 = self._attention(x_perm1)
15
+ out2 = self._attention(x_perm2).permute(0, 2, 1, 3)
16
+ out3 = self._attention(x_perm3).permute(0, 3, 2, 1)
17
+ out = (out1 + out2 + out3) / 3
18
+ return out
19
+ def _attention(self, x):
20
+ avg_out = torch.mean(x, dim=1, keepdim=True)
21
+ max_out, _ = torch.max(x, dim=1, keepdim=True)
22
+ pool = torch.cat([avg_out, max_out], dim=1)
23
+ attn = self.conv1(pool)
24
+ attn = self.sigmoid(attn)
25
+ return x * attn
26
+
27
+ class SEBlock(nn.Module):
28
+ def __init__(self, in_channels, reduction=16):
29
+ super(SEBlock, self).__init__()
30
+ self.fc1 = nn.Conv2d(in_channels, in_channels // reduction, kernel_size=1)
31
+ self.relu = nn.ReLU(inplace=True)
32
+ self.fc2 = nn.Conv2d(in_channels // reduction, in_channels, kernel_size=1)
33
+ self.sigmoid = nn.Sigmoid()
34
+ def forward(self, x):
35
+ w = nn.functional.adaptive_avg_pool2d(x, 1)
36
+ w = self.relu(self.fc1(w))
37
+ w = self.sigmoid(self.fc2(w))
38
+ return x * w
39
+
40
+ class ECABlock(nn.Module):
41
+ def __init__(self, channels, k_size=3):
42
+ super(ECABlock, self).__init__()
43
+ self.avg_pool = nn.AdaptiveAvgPool2d(1)
44
+ self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
45
+ self.sigmoid = nn.Sigmoid()
46
+ def forward(self, x):
47
+ y = self.avg_pool(x)
48
+ y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
49
+ y = self.sigmoid(y)
50
+ return x * y.expand_as(x)
51
+
52
+ class RESBlock(nn.Module):
53
+ def __init__(self, in_channels):
54
+ super(RESBlock, self).__init__()
55
+ self.se = SEBlock(in_channels)
56
+ self.eca = ECABlock(in_channels)
57
+ def forward(self, x):
58
+ out_se = self.se(x)
59
+ out_eca = self.eca(x)
60
+ return out_se + out_eca
61
+
62
+ class ModifiedMobileNetV2(nn.Module):
63
+ def __init__(self, num_classes=10, insert_indices=(3, 5, 8, 10, 13, 15)):
64
+ super().__init__()
65
+ base = mobilenet_v2(weights='DEFAULT')
66
+ self.features = nn.Sequential()
67
+ attention_count = 0
68
+ resblock_count = 0
69
+ ta_insert_points = set([3, 8, 13])
70
+ res_insert_points = set([5, 10, 15])
71
+ for idx, layer in enumerate(base.features):
72
+ self.features.add_module(str(idx), layer)
73
+ out_channels = None
74
+ if hasattr(layer, 'out_channels'):
75
+ out_channels = layer.out_channels
76
+ elif hasattr(layer, 'conv'):
77
+ out_channels = layer.conv[-1].out_channels
78
+ else:
79
+ out_channels = layer[0].out_channels
80
+ if idx in ta_insert_points:
81
+ self.features.add_module(f'ta{attention_count+1}', TripletAttention(out_channels))
82
+ attention_count += 1
83
+ if idx in res_insert_points:
84
+ self.features.add_module(f'res{resblock_count+1}', RESBlock(out_channels))
85
+ resblock_count += 1
86
+ self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
87
+ self.classifier = nn.Linear(base.last_channel, num_classes)
88
+ def forward(self, x):
89
+ x = self.features(x)
90
+ x = self.avgpool(x)
91
+ x = torch.flatten(x, 1)
92
+ x = self.classifier(x)
93
+ return x
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.103.0
2
+ uvicorn==0.23.2
3
+ torchvision==0.15.2
4
+ pillow==9.5.0
5
+ python-multipart==0.0.9