TensorFlow Keras預訓練模型測試

2021-08-21 06:09:20 字數 4050 閱讀 5758

densenet121

densenet169

densenet201

inceptionresnetv2

inceptionv3

mobilenet

nasnetlarge

nasnetmobile

resnet50

vgg16

vgg19

xception

這些人工神經網路模型的類都有各自對應的預處理函式。我在ilsvrc2012的驗證集上對各種網路進行了測試。對影象的預處理流程如下:

h=影象的高,w=影象的寬。

邊長=min(h, w),偏置項=abs(h-w)//2。

如果h>w,裁剪後的影象=原影象[偏置項:偏置項+邊長,邊長],轉到第5步;否則轉到第4步。

裁剪後的影象=原影象[邊長,偏置項:偏置項+邊長]。

將裁剪後的影象放縮到模型所需要的大小。

用對應的preprocess_input函式進行處理。

測試結果如下圖所示

大部分預訓練模型的測試結果差於**所聲稱的結果。只有inception-resnet-v2的測試結果好於原**。這其中的原因有待考究,有可能是我對測試影象的預處理有偏差,或者預訓練模型並非官方提供。

各種神經網路模型的**錯誤率是取各自多種變體中最高的乙個。mobilenet、nasnetlarge和xception**資料暫缺。

下面是測試所用的源**

# 匯入所需模組

import tensorflow as tf

import os

import tensorflow

import csv

from nclasses import labels

import numpy as np

import utils

import matplotlib.image as mpimg

import matplotlib.pyplot as plt

import cv2

import time

# 匯入預處理函式

# 匯入模型類

# 匯入image子模組

image = tf.keras.preprocessing.image

# 獲得驗證集影象位址和標籤

image_paths, image_labels = utils.get_paths_labels()

# 例項化模型類

# resnet50 = resnet50()

# model = densenet121()

# model = densenet169()

# model = densenet201()

# model = inceptionresnetv2()

# model = inceptionv3()

# model = mobilenet()

# model = nasnetlarge()

# model = vgg16()

# model = vgg19()

model = xception()

# 記錄top1、top5正確數目

top1_cnt = 0

top5_cnt = 0

# 用於記錄測試用時

begin_time = time.clock()

# 記錄總影象數目

cnt = 0

for image_path, image_label in zip(image_paths, image_labels):

# 預處理開始

raw_image = image.img_to_array(image.load_img(image_path))

image_copy = np.copy(raw_image)

shape = image_copy.shape

h, w = shape[0], shape[1]

if h > w:

h_start = (h - w) // 2

image_copy = image_copy[h_start:h_start+w, :]

else:

w_start = (w - h) // 2

image_copy = image_copy[:, w_start:w_start+h]

image_resized = cv2.resize(image_copy, (299, 299), interpolation=cv2.inter_cubic)

processed_image = preprocess_input(image_resized).reshape((1, 299, 299, -1))

# 預處理結束,用模型例項進行**

res = model.predict(processed_image)

# 處理得到的結果,與標籤進行對比

# argsort()是numpy.ndarray的成員函式,從小到大排序,返回排序好的各元素對應的排序前的下標

top5 = res.argsort().squeeze()[-1:-6:-1]

if image_label in top5:

top5_cnt += 1

if image_label == top5[0]:

top1_cnt += 1

cnt += 1

# 每10000張,輸出一次測試耗時

if cnt % 10000 == 0:

end_time = time.clock()

print('%d steps: %f' % (cnt, end_time - begin_time))

begin_time = end_time

print('top1 accuracy:', top1_cnt / 50000)

print('top5 accuracy:', top5_cnt / 50000)

import numpy as np 

import csv

from nclasses import labels

import os

def get_paths_labels():

# 資料集位址

imagenet_path = r"d:\ilsvrc2012"

# 獲取訓練影象資料夾名,即原始標籤

raw_labels = os.listdir(os.path.join(imagenet_path, 'img_train'))

# 將原始標籤對映到0-999

label_dict = {}

num_labels = np.arange(1000)

for raw_label, num_label in zip(raw_labels, num_labels):

label_dict[raw_label] = num_label

# 從csv檔案中讀取每張影象的位址和標籤。可自行處理資料集得到這樣乙個csv檔案。

with open(os.path.join(imagenet_path, 'val_images.csv'), 'r') as csvfile:

csvfile.readline()

lines = csvfile.readlines()

image_paths =

image_labels =

for line in lines:

image_path, image_label = line.strip().split(',')

return image_paths, image_labels

下面是nclasses.py的**,將0-999的索引值對映到真實標籤:

#!/usr/bin/python

#coding:utf-8

# 每個影象的真實標籤,以及對應的索引值

labels =

TensorFlow Keras 環境安裝

tensorflow keras深度學習人工智慧實踐 閱讀筆記 深度學習以大量矩陣模擬神經元的工作方式。矩陣運算特性 單一運算簡單,但是需要大量運算,適合平行計算。機器學習 1.訓練資料由features和label組成 2.機器學習兩個階段 訓練,3.分類 有監督的學習,無監督的學習,增強式學習。...

Tensorflow Keras 指定CPU執行

執行tensorflow 時候常出現oom out of memory 的錯誤,原因是batch size設定得太大導致視訊記憶體不足。如果想讓 僅僅執行在cpu下,可在原 中加入如下 import os os.environ cuda device order pci bus id os.envi...

Tensorflow keras 回歸模型

import matplotlib as mpl import matplotlib.pyplot as plt matplotlib inline import numpy as np import sklearn import pandas as pd import os import sys ...