File size: 6,187 Bytes
8209f26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from PIL import Image
import numpy as np
from fastapi import File, UploadFile
import os
import tensorflow as tf
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Animal names here
ANIMALS = ['Cat', 'Dog', 'Panda']

# Model path - check multiple possible locations for different model formats
model_path = None
model_type = None  # 'savedmodel' or 'keras'

# Check for SavedModel format (try both old and new naming conventions)
savedmodel_paths = [
    "animal-classification/INPUT_model_path/animal-cnn/savedmodel",
    "animal-classification/animal-cnn/savedmodel",
    "/app/animal-classification/INPUT_model_path/animal-cnn/savedmodel",
    "/app/animal-classification/animal-cnn/savedmodel",
    "animal-classification/INPUT_model_path/animal-classification/animal-cnn-savedmodel",
    "animal-classification/animal-cnn-savedmodel",
    "/app/animal-classification/INPUT_model_path/animal-classification/animal-cnn-savedmodel",
    "/app/animal-classification/animal-cnn-savedmodel"
]

# Check for Keras format (.keras file)
keras_paths = [
    "animal-classification/INPUT_model_path/animal-cnn/model.keras",
    "animal-classification/animal-cnn/model.keras",
    "/app/animal-classification/INPUT_model_path/animal-cnn/model.keras",
    "/app/animal-classification/animal-cnn/model.keras"
]

for path in savedmodel_paths:
    if os.path.exists(path):
        model_path = path
        model_type = 'savedmodel'
        break

if not model_path:
    for path in keras_paths:
        if os.path.exists(path):
            model_path = path
            model_type = 'keras'
            break

if not model_path:
    # Fallback: try to find any model in the directory structure
    model_base = "animal-classification"
    print(f"Current working directory: {os.getcwd()}")
    print(f"Files in current directory: {os.listdir('.')}")

    if os.path.exists(model_base):
        print(f"Contents of {model_base}:")
        for root, dirs, files in os.walk(model_base):
            level = root.replace(model_base, '').count(os.sep)
            indent = ' ' * 2 * level
            print(f"{indent}{os.path.basename(root)}/")
            subindent = ' ' * 2 * (level + 1)
            for file in files[:10]:
                print(f"{subindent}{file}")

            # Check for SavedModel directories (multiple naming conventions)
            if 'savedmodel' in dirs:
                model_path = os.path.join(root, 'savedmodel')
                model_type = 'savedmodel'
                break
            if 'animal-cnn-savedmodel' in dirs:
                model_path = os.path.join(root, 'animal-cnn-savedmodel')
                model_type = 'savedmodel'
                break
            # Check if any directory contains saved_model.pb (indicating SavedModel format)
            for dir_name in dirs:
                potential_savedmodel = os.path.join(root, dir_name)
                if os.path.exists(os.path.join(potential_savedmodel, 'saved_model.pb')):
                    model_path = potential_savedmodel
                    model_type = 'savedmodel'
                    break
            if model_path:
                break

            # Check for .keras files
            for file in files:
                if file.endswith('.keras'):
                    model_path = os.path.join(root, file)
                    model_type = 'keras'
                    break
            if model_path:
                break

        if not model_path:
            raise FileNotFoundError(
                f"Could not find any model (SavedModel or .keras) in {model_base}. Directory structure printed above.")
    else:
        raise FileNotFoundError(
            f"Model directory {model_base} not found. Current directory: {os.getcwd()}, Contents: {os.listdir('.')}")

print(f"Loading model from: {model_path}")
print(f"Model type: {model_type}")
print(f"Model path exists: {os.path.exists(model_path)}")

# Load the model based on its type
try:
    if model_type == 'savedmodel':
        loaded_model = tf.saved_model.load(model_path)
        infer = loaded_model.signatures["serving_default"]
        print("SavedModel loaded successfully!")
    else:  # keras
        # Try loading with compile=False to avoid optimizer/loss issues
        try:
            loaded_model = tf.keras.models.load_model(
                model_path, compile=False)
            print("Keras model loaded successfully (compile=False)!")
        except Exception as e1:
            print(f"Failed to load with compile=False: {e1}")
            # Try with safe_mode if available (newer Keras versions)
            try:
                loaded_model = tf.keras.models.load_model(
                    model_path, safe_mode=False)
                print("Keras model loaded successfully (safe_mode=False)!")
            except Exception as e2:
                print(f"Failed to load with safe_mode=False: {e2}")
                raise
        # For keras models, we'll use the model directly, not via signatures
        infer = None
except Exception as e:
    print(f"Error loading model: {e}")
    import traceback
    traceback.print_exc()
    raise


@app.get('/health')
async def health():
    return {"status": "healthy"}


@app.post('/upload/image')
async def uploadImage(img: UploadFile = File(...)):
    # Image inlezen
    original_image = Image.open(img.file)
    resized_image = original_image.resize((64, 64))
    images_to_predict = np.expand_dims(
        np.array(resized_image), axis=0).astype(np.float32)

    # Predict based on model type
    if model_type == 'savedmodel':
        # Tensor maken en infer voor SavedModel
        input_tensor = tf.convert_to_tensor(images_to_predict)
        result = infer(input_tensor)
        predictions = list(result.values())[0].numpy()
    else:  # keras
        # Direct prediction voor Keras model
        predictions = loaded_model.predict(images_to_predict, verbose=0)

    classification = predictions.argmax(axis=1)[0]
    return ANIMALS[classification]