Spaces:
Sleeping
Sleeping
crazyscientist
commited on
Commit
•
7b9d0de
1
Parent(s):
25cd769
Update modInference.py
Browse files- modInference.py +6 -60
modInference.py
CHANGED
@@ -3,80 +3,26 @@ import cv2
|
|
3 |
import torch
|
4 |
import glob as glob
|
5 |
import os
|
|
|
|
|
6 |
import matplotlib.pyplot as plt
|
7 |
|
8 |
-
from models.create_fasterrcnn_model import create_model
|
9 |
from utils.annotations import CNNpostAnnotations
|
10 |
#from utils.annotations import inference_annotations
|
11 |
-
from utils.general import set_infer_dir
|
12 |
from utils.transforms import infer_transforms
|
13 |
|
14 |
-
|
15 |
-
from skimage import transform
|
16 |
-
import os
|
17 |
-
from keras.models import Model
|
18 |
-
from keras.optimizers import Adam
|
19 |
-
from keras.applications.vgg16 import VGG16, preprocess_input
|
20 |
-
from keras.layers import Dense, Dropout, Flatten
|
21 |
-
import numpy as np
|
22 |
-
|
23 |
-
|
24 |
-
conv_base = VGG16(include_top=False,
|
25 |
-
weights='imagenet',
|
26 |
-
input_shape=(200,200,3))
|
27 |
-
|
28 |
-
|
29 |
-
if 2 > 0:
|
30 |
-
for layer in conv_base.layers[:-2]:
|
31 |
-
layer.trainable = False
|
32 |
-
else:
|
33 |
-
for layer in conv_base.layers:
|
34 |
-
layer.trainable = False
|
35 |
-
|
36 |
-
|
37 |
-
top_model = conv_base.output
|
38 |
-
top_model = Flatten(name="flatten")(top_model)
|
39 |
-
top_model = Dense(4096, activation='relu')(top_model)
|
40 |
-
top_model = Dense(1048, activation='relu')(top_model)
|
41 |
-
top_model = Dense(256, activation='relu')(top_model)
|
42 |
-
top_model = Dense(128, activation='relu')(top_model)
|
43 |
-
top_model = Dense(64, activation='relu')(top_model)
|
44 |
-
top_model = Dropout(0.2)(top_model)
|
45 |
-
output_layer = Dense(5, activation='softmax')(top_model)
|
46 |
-
|
47 |
-
CNN = Model(inputs=conv_base.input, outputs=output_layer)
|
48 |
-
CNN.load_weights("CNN.hdf5")
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
def main(weightUrl, input):
|
53 |
np.random.seed(42)
|
54 |
|
55 |
-
NUM_CLASSES = 2
|
56 |
-
CLASSES = ['__background__', 'Cell']
|
57 |
-
|
58 |
-
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
59 |
-
OUT_DIR = set_infer_dir()
|
60 |
-
|
61 |
-
checkpoint = torch.load(weightUrl, map_location=DEVICE)
|
62 |
-
data_configs = True
|
63 |
-
NUM_CLASSES = checkpoint['config']['NC']
|
64 |
-
CLASSES = checkpoint['config']['CLASSES']
|
65 |
-
build_model = create_model[checkpoint['model_name']]
|
66 |
-
|
67 |
-
model = build_model(num_classes=NUM_CLASSES, coco_model=False)
|
68 |
-
model.load_state_dict(checkpoint['model_state_dict'])
|
69 |
-
model.to(DEVICE).eval()
|
70 |
-
|
71 |
-
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
|
72 |
-
|
73 |
-
|
74 |
image = input
|
75 |
orig_image = image.copy()
|
76 |
image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
|
77 |
image = infer_transforms(image)
|
78 |
image = torch.unsqueeze(image, 0)
|
79 |
|
|
|
|
|
|
|
80 |
outputs = model(image.to(DEVICE))
|
81 |
|
82 |
# Load all detection to CPU for further operations.
|
|
|
3 |
import torch
|
4 |
import glob as glob
|
5 |
import os
|
6 |
+
import time
|
7 |
+
|
8 |
import matplotlib.pyplot as plt
|
9 |
|
|
|
10 |
from utils.annotations import CNNpostAnnotations
|
11 |
#from utils.annotations import inference_annotations
|
|
|
12 |
from utils.transforms import infer_transforms
|
13 |
|
14 |
+
def main(CNN, model, input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
np.random.seed(42)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
image = input
|
18 |
orig_image = image.copy()
|
19 |
image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
|
20 |
image = infer_transforms(image)
|
21 |
image = torch.unsqueeze(image, 0)
|
22 |
|
23 |
+
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
24 |
+
CLASSES = ['__background__', 'Cell']
|
25 |
+
|
26 |
outputs = model(image.to(DEVICE))
|
27 |
|
28 |
# Load all detection to CPU for further operations.
|