Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import onnxruntime as ort
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from torchvision import transforms
|
| 6 |
-
import torchvision.transforms.v2 as T
|
| 7 |
import io
|
| 8 |
import rdkit
|
| 9 |
from rdkit import Chem
|
|
@@ -17,24 +16,22 @@ idx_to_labels = {0:'other',1:'C',2:'O',3:'N',4:'Cl',5:'Br',6:'S',7:'F',8:'B',
|
|
| 17 |
16:'=',17:'#',18:'-4',19:'-2',20:'-1',21:'1',22:'+2',} #NONE is single ?
|
| 18 |
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
image = Image.open(image_path)
|
| 23 |
w, h = image.size
|
| 24 |
-
# print("width: {}, height: {}".format(w, h))
|
| 25 |
-
# Define a transform to convert the image to a tensor and normalize it
|
| 26 |
-
transform = transforms.Compose([
|
| 27 |
-
# transforms.Grayscale(num_output_channels=1), # Convert to grayscale (1 channel)
|
| 28 |
-
T.Resize((640, 640)), # Resize the image to 224x224
|
| 29 |
-
T.ToImageTensor(), # Convert to Tensor (C x H x W)
|
| 30 |
-
T.ConvertDtype(dtype=torch.float32)
|
| 31 |
-
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Optional normalization for pretrained models
|
| 32 |
-
])
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
return
|
| 38 |
|
| 39 |
|
| 40 |
def visualize_molecule(smiles):
|
|
@@ -60,18 +57,19 @@ def predict(input_image):
|
|
| 60 |
|
| 61 |
# 预处理图片
|
| 62 |
# Example usage: #change thie image
|
| 63 |
-
|
| 64 |
-
processed_image=
|
| 65 |
|
| 66 |
# 获取模型输入输出名称
|
| 67 |
input_name = session.get_inputs()[0].name
|
| 68 |
output_name = session.get_outputs()[0].name
|
| 69 |
|
| 70 |
# 进行推理
|
| 71 |
-
outputs = session.run(
|
|
|
|
| 72 |
ori_size=torch.Tensor([w,h]).long().unsqueeze(0)
|
| 73 |
-
postprocessor = RTDETRPostProcessor()
|
| 74 |
-
result_ = postprocessor(
|
| 75 |
score_=result_[0]['scores']
|
| 76 |
boxe_=result_[0]['boxes']
|
| 77 |
label_=result_[0]['labels']
|
|
@@ -94,9 +92,9 @@ def predict(input_image):
|
|
| 94 |
'scores': output["scores"].to("cpu").numpy(),
|
| 95 |
'pred_classes': output["labels"].to("cpu").numpy()}
|
| 96 |
|
| 97 |
-
atoms_df, bonds_list
|
| 98 |
bond_labels=bond_labels, result=[])
|
| 99 |
-
smiles,mol_rebuit=mol_from_graph_with_chiral(atoms_df, bonds_list
|
| 100 |
|
| 101 |
# 使用RDKit生成分子结构图
|
| 102 |
mol_image = visualize_molecule(smiles)
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from torchvision import transforms
|
|
|
|
| 6 |
import io
|
| 7 |
import rdkit
|
| 8 |
from rdkit import Chem
|
|
|
|
| 16 |
16:'=',17:'#',18:'-4',19:'-2',20:'-1',21:'1',22:'+2',} #NONE is single ?
|
| 17 |
|
| 18 |
|
| 19 |
+
def image_to_numpy(image_path):
|
| 20 |
+
|
| 21 |
image = Image.open(image_path)
|
| 22 |
w, h = image.size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
img_array = np.array(image)
|
| 25 |
+
|
| 26 |
+
img_resized = cv2.resize(img_array, (640, 640), interpolation=cv2.INTER_LINEAR)
|
| 27 |
+
|
| 28 |
+
img_float = img_resized.astype(np.float32)
|
| 29 |
+
img_normalized = img_float / 255.0
|
| 30 |
+
|
| 31 |
+
if len(img_normalized.shape) == 3:
|
| 32 |
+
img_normalized = img_normalized.transpose(2, 0, 1)
|
| 33 |
|
| 34 |
+
return img_normalized, w, h
|
| 35 |
|
| 36 |
|
| 37 |
def visualize_molecule(smiles):
|
|
|
|
| 57 |
|
| 58 |
# 预处理图片
|
| 59 |
# Example usage: #change thie image
|
| 60 |
+
img_array,w,h = image_to_numpy(input_image)
|
| 61 |
+
processed_image=np.expand_dims(img_array, 0)
|
| 62 |
|
| 63 |
# 获取模型输入输出名称
|
| 64 |
input_name = session.get_inputs()[0].name
|
| 65 |
output_name = session.get_outputs()[0].name
|
| 66 |
|
| 67 |
# 进行推理
|
| 68 |
+
outputs = session.run(None, {input_name: processed_image})
|
| 69 |
+
preds = {'pred_logits':torch.from_numpy(outputs[0]), 'pred_boxes':torch.from_numpy(outputs[1])}
|
| 70 |
ori_size=torch.Tensor([w,h]).long().unsqueeze(0)
|
| 71 |
+
postprocessor = RTDETRPostProcessor(num_classes=23, use_focal_loss=True)
|
| 72 |
+
result_ = postprocessor(preds, ori_size)
|
| 73 |
score_=result_[0]['scores']
|
| 74 |
boxe_=result_[0]['boxes']
|
| 75 |
label_=result_[0]['labels']
|
|
|
|
| 92 |
'scores': output["scores"].to("cpu").numpy(),
|
| 93 |
'pred_classes': output["labels"].to("cpu").numpy()}
|
| 94 |
|
| 95 |
+
atoms_df, bonds_list = bbox_to_graph_with_charge(output, idx_to_labels=idx_to_labels,
|
| 96 |
bond_labels=bond_labels, result=[])
|
| 97 |
+
smiles, mol_rebuit = mol_from_graph_with_chiral(atoms_df, bonds_list)
|
| 98 |
|
| 99 |
# 使用RDKit生成分子结构图
|
| 100 |
mol_image = visualize_molecule(smiles)
|