[RISE] 11주차 활동내용

모델 개선 결과


Flask에서 저장한 모델 불러와서 예측하기


colab에서 예측해본 결과:



colab 코드:

from google.colab import drive
drive.mount('/content/drive')

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.optimizers import *
import cv2

Path = "./drive/MyDrive/"
model = keras.models.load_model(Path+"my_model_2.h5")

optimizer = Adam(lr=0.00001)
model.compile(loss='categorical_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy'])

test_data = Path+".jpg"
class_names = ['Chinese', 'Japanese', 'Korean']


img = cv2.imread(Path+'test_image4.jpg')
img = cv2.resize(img,(224,224))
img = np.reshape(img,[1,224,224,3])

classes = np.argmax(model.predict(img), axis = -1)
print(classes)
names = [class_names[i] for i in classes]
print(names)



flask 코드:

from flask import Flask, render_template, request
import numpy as np

from tensorflow import keras
from tensorflow.keras.optimizers import *
import cv2

app = Flask(__name__)

@app.route('/')
def basic():
    return render_template("upload.html")

@app.route('/predict', methods=['POST'])
def predict():
    f = request.files['file']
    Path = "./"
    f.save(Path+f.filename)
    model = keras.models.load_model(Path + "my_model_2.h5")
    optimizer = Adam(lr=0.00001)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])

    test_data = Path + ".jpg"
    class_names = ['Chinese', 'Japanese', 'Korean']

    img = cv2.imread(Path + 'test_image4.jpg')
    img = cv2.resize(img, (224, 224))
    img = np.reshape(img, [1, 224, 224, 3])

    classes = np.argmax(model.predict(img), axis=-1)
    print(classes)
    names = [class_names[i] for i in classes]
    print(names)

def post():
    value = request.form['input']
    return render_template('default.html', name=value)

if __name__ == '__main__':
    app.run(debug=True)

좋은 웹페이지 즐겨찾기