Tensorflow2.0基础-笔记-图像识别-猫狗数据集

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

import glob
image_filenames1=glob.glob('./DataSet/猫狗数据集_2000/dc_2000/train/cat/*.jpg')
image_filenames2=glob.glob('./DataSet/猫狗数据集_2000/dc_2000/train/dog/*.jpg')
image_filenames=image_filenames1+image_filenames2  #把所有的数据放进一个list里。

cat_length=len(image_filenames1)
dog_length=len(image_filenames2)
train_labels=[0]*cat_length+[1]*dog_length  #因为列表前半部分是一类,后半部分是一类,所以标签设置为[0,,0,0,0,.....,1,1,1,1,1]

train_dataset=list(zip(image_filenames,train_labels)) #将数据和标签组合起来。为了后面打乱数据准备,如果没有组合只将数据打乱,数据的标签就不好找了。

#对一个list进行随机排序
train_dataset=np.random.permutation(train_dataset)
train_dataset

def pre_read(imame_file_name):
    image=tf.io.read_file(imame_file_name)
    image=tf.image.decode_jpeg(image,channels=3)
    image=tf.image.rgb_to_grayscale(image) #转化成灰度图片,单通道
    image=tf.image.resize(image,(200,200))
    image=tf.image.per_image_standardization(image) #归一化 即除以255
    return image

train_image_dataset=tf.data.Dataset.from_tensor_slices([x[0] for x in train_dataset])
train_image_dataset=train_image_dataset.map(pre_read)
train_label_dataset=tf.data.Dataset.from_tensor_slices([int(x[1]) for x in train_dataset])
train_image_label_dataset=tf.data.Dataset.zip((train_image_dataset,train_label_dataset))

train_image_label_dataset=train_image_label_dataset.shuffle(100).repeat().batch(32)

#处理测试数据,这里的测试数据在另一个文件下,所以也需要同样的数据处理操作。
test_image_filenames1=glob.glob('./DataSet/猫狗数据集_2000/dc_2000/test/cat/*.jpg')
test_image_filenames2=glob.glob('./DataSet/猫狗数据集_2000/dc_2000/test/dog/*.jpg')
test_image_filenames=image_filenames1+image_filenames2
test_cat_length=len(image_filenames1)
test_dog_length=len(image_filenames2)
test_labels=[0]*cat_length+[1]*dog_length

test_dataset=list(zip(image_filenames,test_labels))
#train_dataset=np.random.permutation(test_dataset) ,因为不需要训练,乱序就没有意义
test_image_dataset=tf.data.Dataset.from_tensor_slices([x[0] for x in test_dataset])
test_image_dataset=test_image_dataset.map(pre_read)
test_label_dataset=tf.data.Dataset.from_tensor_slices([int(x[1]) for x in test_dataset])
test_image_label_dataset=tf.data.Dataset.zip((test_image_dataset,test_label_dataset))
test_image_label_dataset=test_image_label_dataset.batch(32) #只需要batch

model=tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(200,200,1)))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64,(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64,(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64,activation='relu'))
model.add(tf.keras.layers.Dense(1,activation='sigmoid'))
model.summary()

model.compile(loss='binary_crossentropy',
             optimizer='adam',
             metrics=['acc'])

len(train_labels)//32,len(test_labels)//32

history=model.fit(train_image_label_dataset,
                  epochs=10,
                  steps_per_epoch=len(train_labels)//32,
                  validation_data=test_image_label_dataset,
                  validation_steps=len(test_labels)//32)

你可能感兴趣的:(tensorflow2.0,tensorflow,图像识别)