Keras对数据图片的读取问题

第一、读取图片 数据

所用方式为:import os自带的功能

函数:

base_dir = '//home//workspace//wanghao//Demo2//CVD'
train_dir = os.path.join(base_dir,'train')
#os.mkdir(train_dir)

train = os.path.join(base_dir,‘train’)

train -------> ‘//home//workspace//wanghao//Demo2//CVD/train’
类似于path.join()类似于字符串拼接功能,用来创建地址的路径

path = os.listdir(base_dir)

读取 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',
 ........................................................

接下来利用opencv 或者keras来读取

  • [ ]
  • [ ]
  • [ ]
  • [ ]
  • [ ]
  • [ ]
  • [ ]

第二、利用keras 读取图片

在已经得到图片的绝对地址的情况下

  • [创建 image_generator对象来读取 ]

利用test_datagen.flow_from_directory(图像地址,目标size,批量数目,标签分类情况)

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 编码标签的迭代器。

Then,如何使用该迭代器?

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

Output

在这里插入图片描述

对迭代器的解析

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 是一个keras迭代器,里面包含了127个元祖(一共2024个图片,分成了每个batch 16个图,所以 2024/16 = 127)。每个元祖包含一个二维向量,其中一维是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个标签
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值