keras 乱七八糟
程序员文章站
2024-03-25 08:04:58
...
keras保存模型:model.save(mp)
根据已有的模型预测
from keras.preprocessing import image
model = load_model('ResNet50.h5')
img = image.load_img('0.jpg', target_size=(64, 64))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
predictions = model.predict(x)
print(predictions[0])
print(np.argmax(predictions[0]))
画图:其中h位h = model.fit(...)
# plt.figure(figsize=[10, 4])
# plt.subplot(1, 2, 1)
# plt.plot(h.history['loss'])
# plt.plot(h.history['val_loss'])
# plt.legend(['loss', 'val_loss'])
# plt.ylabel('loss')
# plt.xlabel('epoch')
#
# plt.subplot(1, 2, 2)
# plt.plot(h.history['acc'])
# plt.plot(h.history['val_acc'])
# plt.legend(['acc', 'val_acc'])
# plt.ylabel('acc')
# plt.xlabel('epoch')
jupyter 中的训练图片展示
import random
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
plt.figure(figsize=(12,10))
for i in range(12):
random_index = random.randint(0,n-1)
plt.subplot(3,4,1+i)
plt.imshow(X[random_index])
plt.title(['dog','cat'][Y[random_index]])
tensorflow和Keras 转换RGB到BGR的实现代码:来自https://blog.csdn.net/Will_Ye/article/details/88062567
import numpy as np
import tensorflow as tf
vgg_mean = [103.939, 116.779, 123.68]
self.tfx = tf.placeholder(tf.float32, [None, 224, 224, 3])
self.tfy = tf.placeholder(tf.float32, [None, 1])
# Convert RGB to BGR
red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=self.tfx * 255.0)
bgr = tf.concat(axis=3, values=[
blue - self.vgg_mean[0],
green - self.vgg_mean[1],
red - self.vgg_mean[2],
])
import cv2
from keras import datasets
from keras.applications.vgg16 import VGG16
from keras.datasets import mnist
import numpy as np
(X_train,y_train),(X_test,y_test) = mnist.load_data()
#转成VGG16需要的格式
#RGB ->> bgr格式
X_train = [cv2.cvtColor(cv2.resize(i,(ishape,ishape)), cv2.COLOR_GRAY2BGR) for i in X_train]
X_train = np.concatenate([arr[np.newaxis] for arr in X_train]).astype('float32')
X_test = [cv2.cvtColor(cv2.resize(i,(ishape,ishape)), cv2.COLOR_GRAY2BGR) for i in X_test ]
X_test = np.concatenate([arr[np.newaxis] for arr in X_test] ).astype('float32')
下一篇: SSH前言学习笔记(3)