keras 乱七八糟

keras保存模型:model.save(mp)

图片正则和生成标签

def normalized(rgb):
    #return rgb/255.0
    norm=np.zeros((rgb.shape[0], rgb.shape[1], 3),np.float32)

    b=rgb[:,:,0]
    g=rgb[:,:,1]
    r=rgb[:,:,2]

    norm[:,:,0]=cv2.equalizeHist(b)
    norm[:,:,1]=cv2.equalizeHist(g)
    norm[:,:,2]=cv2.equalizeHist(r)

    return norm

def binarylab(labels):
    x = np.zeros([360,480,12])
    for i in range(360):
        for j in range(480):
            x[i,j,labels[i][j]]=1
    return x

 

根据已有的模型预测

    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')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值