Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,15 +26,16 @@ class ImageClassifier(nn.Module):
|
|
| 26 |
output=self.pretrain_model(input)
|
| 27 |
return output
|
| 28 |
|
| 29 |
-
model = ImageClassifier()
|
| 30 |
-
model.load_state_dict(torch.load('model-data_comet-torch-model.pth'))
|
| 31 |
-
|
| 32 |
def predict(inp):
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
labels = ['normal', 'cancer']
|
| 35 |
-
inp = image_transform(inp).unsqueeze(dim=0)
|
| 36 |
with torch.no_grad():
|
| 37 |
-
prediction = torch.nn.functional.softmax(model(inp))
|
| 38 |
confidences = {labels[i]: float(prediction.squeeze()[i]) for i in range(len(labels))}
|
| 39 |
return confidences
|
| 40 |
|
|
@@ -44,4 +45,4 @@ gr.Interface(fn=predict,
|
|
| 44 |
title=title,
|
| 45 |
description=description,
|
| 46 |
article=article,
|
| 47 |
-
examples=['image-1.jpg', 'image-2.jpg']).launch()
|
|
|
|
| 26 |
output=self.pretrain_model(input)
|
| 27 |
return output
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
def predict(inp):
|
| 30 |
+
if not hasattr(predict, "model"):
|
| 31 |
+
# Load the model only if it hasn't been loaded yet
|
| 32 |
+
predict.model = ImageClassifier()
|
| 33 |
+
predict.model.load_state_dict(torch.load('model-data_comet-torch-model.pth'))
|
| 34 |
+
predict.image_transform = transforms.Compose([transforms.Resize(size=(224, 224)), transforms.ToTensor()])
|
| 35 |
labels = ['normal', 'cancer']
|
| 36 |
+
inp = predict.image_transform(inp).unsqueeze(dim=0)
|
| 37 |
with torch.no_grad():
|
| 38 |
+
prediction = torch.nn.functional.softmax(predict.model(inp))
|
| 39 |
confidences = {labels[i]: float(prediction.squeeze()[i]) for i in range(len(labels))}
|
| 40 |
return confidences
|
| 41 |
|
|
|
|
| 45 |
title=title,
|
| 46 |
description=description,
|
| 47 |
article=article,
|
| 48 |
+
examples=['image-1.jpg', 'image-2.jpg']).launch()
|