from tensorflow.keras.applications.resnet_v2 import ResNet50V2, decode_predictions, preprocess_input
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization
from tensorflow.keras.layers import GlobalAveragePooling2D
resnet50_v2.trainable=False
model = Sequential([
resnet50_v2,
Dense(512,activation='relu'),
BatchNormalization(),
tf.keras.layers.GlobalAveragePooling2D(),
Dense(1000, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics='accuracy')
from keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
resize_img = np.array([globals()['resize_img{}'.format(idx)] for idx in range(15)])
pred = model.predict(resize_img)
import matplotlib.pyplot as plt
for idx in range(15):
plt.imshow(globals()['resize_img{}'.format(idx)])
plt.show()
print('결과 : ',decode_predictions(pred)[idx][0][1])
print('--------------------------------------------------')
file_path = tf.keras.utils.get_file('ImageNetLabels.txt','<https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt>')
labels = open(file_path).read().splitlines()
print(len(labels)) #1001
del labels[0]
print(len(labels)) #1000
decode_predictions(preds, top=3)[0]
for idx, label in zip(range(len(labels)), labels):
predict_dictionary[idx] = label
for idx in range(15):
plt.imshow(globals()['resize_img{}'.format(idx)])
plt.show()
print('결과 : ',decode_predictions(pred)[idx][0][1])
print('--------------------------------------------------')