base_dir = '//home//workspace//wanghao//Demo2//CVD'
train_dir = os.path.join(base_dir,'train')
#os.mkdir(train_dir)
train -------> ‘//home//workspace//wanghao//Demo2//CVD/train’
类似于path.join()类似于字符串拼接功能,用来创建地址的路径
读取 base_dir目录下的文件的名字
path = os.listdir(base_dir)
print("The directory Info: ")
for i in path:
print(i)
The directory Info:
test
train
比如在train_dogs的目录下有3000个图,如何读取进来?
file1 = os.listdir(train_dogs)
output:
['dog.1000.jpg',
'dog.1001.jpg',
'dog.1002.jpg',
'dog.1003.jpg',
'dog.1004.jpg',
'dog.1005.jpg',
'dog.1006.jpg',
'dog.1007.jpg'
。。。。。。。。。。
读取到该文件下所有图片的名字(相对路径),接下来要合成绝对路径,以用读取。
image1 = [os.path.join(train_dogs,i) for i in file1]
print(image1)
Output:
['//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1000.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1001.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1002.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1003.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1004.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1005.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1006.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1007.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1008.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1009.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1010.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1011.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1012.jpg',
'//home//workspace//wanghao//Demo2//CVD/train/dogs/dog.1013.jpg',
........................................................
test_datagen = ImageDataGenerator(rescale= 1./255)
validation_generator = test_datagen.flow_from_directory(validation_dir,
target_size = (128,128),
batch_size = 32,
class_mode = 'binary')
validation_generator 是包含有所有图片和自动化分one-hot 编码标签的迭代器。
k= 0
import numpy as np
names = ['Cats','Dogs']
for batch_image,batch_labels in validation_generator:
k+=1
if k==1:
print("Batch_image_shape: ",batch_image.shape)
print("Batch_labels_shape: ",batch_labels.shape)
batch_labels = batch_labels.astype(np.int8)
plt.subplot(141),plt.imshow(batch_image[0]),plt.title(names[batch_labels[0]])
plt.subplot(142),plt.imshow(batch_image[1]),plt.title(names[batch_labels[1]])
plt.subplot(143),plt.imshow(batch_image[25]),plt.title(names[batch_labels[25]])
plt.subplot(144),plt.imshow(batch_image[31]),plt.title(names[batch_labels[31]])
plt.show()
if k==2:
break
test_genera = ImageDataGenerator(rescale=1./255)
test_image = test_genera.flow_from_directory(test_dir,target_size=(128,128),batch_size=16,class_mode='binary')
Batch1 = test_image[0]
x_image ,y_label = Batch1[:]
print("Batch1[0]_shape : ",x_image.shape)
print("Batch1[1]_shape :",y_label.shape)
Batch1[0]_shape : (16, 128, 128, 3)
Batch1[1]_shape : (16,)
test_image[0] #是一个元祖
test_image[1]
test_image[2]
................
test_image[126]
解析其中一个元祖
Batch1 = test_image[0]
print("Batch: ",len(Batch1))
x_image = Batch1[0]
y_label = Batch1[1]
print("Batch1[0]_shape : ",x_image.shape)
print("Batch1[1]_shape :",y_label.shape)
Output:
Batch: 2
Batch1[0]_shape : (16, 128, 128, 3) # 16个3D图片
Batch1[1]_shape : (16,) #16个标签