前提・実現したいこと
Tensor flowで複数のモデルを読込み、推論を実行させたい。
発生している問題・エラーメッセージ
name 'model2' is not defined
該当のソースコード
Python
1import os 2import sys 3from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions 4from keras.models import Sequential, Model 5from keras.layers import Input, Activation, Dropout, Flatten, Dense 6from keras.preprocessing import image 7from keras.preprocessing.image import array_to_img 8from pathlib import Path 9import glob 10import pathlib 11import numpy as np 12from PIL import Image 13from keras.models import load_model 14import csv 15import cv2 16from matplotlib import pyplot as plt 17from keras import backend as K 18from keras.preprocessing import image 19import tensorflow as tf 20from tensorflow.python.framework import ops 21import gc 22import tracemalloc 23import time 24#import objgraph 25#import pdb; pdb.set_trace() 26 27 28file =glob.glob(r"E:\Deep Learning用\VGG16 finetuning\image\1. Marking\test\test2*.bmp") 29 30result_dir = "Result" 31 32img_height, img_width = 224, 224 33channels = 3 34 35input_tensor = Input(shape=(img_height, img_width, 3)) 36vgg16_model = VGG16(include_top=False, weights="imagenet", input_tensor= input_tensor) 37top_model = Sequential() 38top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:])) 39top_model.add(Dense(256, activation="relu")) 40top_model.add(Dropout(0.3)) 41top_model.add(Dense(1, activation="sigmoid")) 42 43model1 = Model(input=vgg16_model.input, output=top_model(vgg16_model.output)) 44model1.save(r"E:\Deep Learning用\VGG16 finetuning\Result\finetuning_model.h5") 45model1.load_weights(r"E:\Deep Learning用\VGG16 finetuning\Result\VGG16_finetuning(marking).h5") 46model1.compile(loss="binary_crossentropy",optimizer="adam", metrics=["accuracy"] ) 47 48model2.load_model(r"E:\Deep Learning用\VGG16 finetuning\Result\finetuning_model.h5") 49model2.load_weights(r"E:\Deep Learning用\VGG16 finetuning\Result\VGG16_finetuning(marking).h5") 50model2.compile(loss="binary_crossentropy",optimizer="adam", metrics=["accuracy"] ) 51 52K.set_learning_phase(1) 53 54with open("test_result.csv","wt",newline="", encoding="utf-8") as out: 55 writer = csv.writer(out) 56 writer.writerow(["name", "Probability","time"]) 57 58 for i in file: 59 t1 = time.perf_counter() 60 img = Image.open(i) 61 x = image.img_to_array(img) 62 x = np.expand_dims(x, axis=0) 63 x = x.astype("float32") 64 preprocessed_input = x/255.0 65 66 pred = sess.model.predict(preprocessed_input)[0] 67 pred1 = sess.model1.predict(preprocessed_inputnput[0]) 68 t2 = time.perf_counter() 69 sub_time = (t2-t1)*1000 70 print(i,pred,sub_time) 71 writer.writerow([i, pred, sub_time]) 72 K.clear_session() 73
試したこと
補足情報(FW/ツールのバージョンなど)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。