tensorflow2.0_数据预处理+VGG16迁移学习(不使用API给模型输入数据)

数据预处理+VGG16迁移学习1、加载数据库2、参数设置3、读取文件中的图片4、对数据进行预处理4.1、查看数据及标签的数量5、对数据集进行划分5.1、查看切分后的数据集6、训练分类模型7、训练并保存模型8、模型训练效果8.1、曲线平滑处理8.2、测试集测试效果9、将模型保存为pb文件三级目录开发环境jupyter、tensorflow2.01、加载数据库%matplotlib inlineimport matplotlib as mplimport matplotlib.pyplot as pl
摘要由CSDN通过智能技术生成


开发环境jupyter、tensorflow2.0

1、加载数据库

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import sklearn
import sys
import tensorflow as tf
#import time
import cv2
#import glob

# from PIL import Image
# import matplotlib.patches as patch 
# import json

from tensorflow import keras

from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications import VGG16, VGG19
from tensorflow.keras.models import load_model
from tensorflow.keras import layers, models

print(tf.__version__)
print(sys.version_info)
for module in mpl,np,pd,sklearn,tf,keras:
    print(module.__name__,module.__version__)

在这里插入图片描述

from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications import VGG16, VGG19
from tensorflow.keras.models import load_model
from tensorflow.keras import layers, models
from tensorflow.compat.v1 import graph_util
from tensorflow.python.keras import backend as K
tf.compat.v1.disable_eager_execution()
K.set_learning_phase(0)

2、参数设置

preprocessedFolder = 'H:\\jupyter_project1\\模式识别作业\\ClassificationData\\'
outModelFileName = 'H:\\jupyter_project1\\模式识别作业\\'
ImageWidth = 512
ImageHeight = 320
ImageNumChannels = 3
TrainingPercent = 70
ValidationPercent = 15

3、读取文件中的图片


def read_dl_classifier_data_set(preprocessedFolder):
    img_list = []
    label_list = []
    cnt_class = 0 #存放每个图像的label
    cnt_img = 0
    for directory in os.listdir(preprocessedFolder):
        cnt_class += 1
        tmp_dir = preprocessedFolder + directory
        for image in os.listdir(tmp_dir):
            cnt_img += 1
            tmp_img_filepath = tmp_dir + '\\'+image
            tmp_img = cv2.imread(tmp_img_filepath)
            tmp_img = cv2.resize(tmp_img,(ImageWidth, ImageHeight))
            img_list.append(tmp_img)
            label_list.append(cnt_class)
            if cnt_img % 50 ==0:
                print(str(cnt_img) + " :Load " + tmp_img_filepath + " success!")
    print("Total " + str(cnt_img) + " images read belong to " + str(cnt_class) + "classes" )
    return np.array(img_list),np.array(label_list)

if __name__ == "__main__":
    #使用相对路径,改为绝对路径后,也可直接运行
    preprocessedFolder = '.\\ClassificationData\\' 
    outModelFileName = '.\\ClassificationData\\'
    ImageWidth = 512
    ImageHeight = 320
    ImageNumChannels = 3
    TrainingPercent = 70
    ValidationPercent = 15
all_data,all_label = read_dl_classifier_data_set(preprocessedFolder)

在这里插入图片描述

4、对数据进行预处理

1、将图像数据像素值压缩至0.0-1.0之间
2、对label使用one-hot编码

def preprocess_dl_Image(all_data, all_label):
    all_data = all_data
  • 1
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值