Tensorflow学习笔记:数据集加工和转化为TensorFlow专用格式——Finetuning,猫狗大战,VGGNet的重新针对训练

Kaggle 猫狗大战
猫狗大战的数据集来源于Kaggle上的一个竞赛:Dogs vs. Cats 
 
猫狗大战的数据集下载地址http://www.kaggle.com/c/dogs-vs-cats,其中数据集有12500只猫和12500只狗 ,官方数据集下载需要帐号,大家也可以百度以下,百度网盘上数据有
 
整个项目的文件结构如下:

下载下来来的图片尺寸都不一样,我这里先统一把图像尺寸调整到224*224*3(虽然后面转换为TensorFlow专用格式的时候代码也会调整图片尺寸,但是没有这一步运行的精度不一样),调整图片的代码如下img_resize.py:

import cv2
import os
dir='./kaggle/train'
for root, dirs, files in os.walk(dir):
    for file in files:
        filepath = os.path.join(root, file)
        try:
            image = cv2.imread(filepath)
            dim = (224, 224)
            resized = cv2.resize(image, dim)
            path = './cat_and_dog/'+file
            cv2.imwrite(path, resized)
        except:
            print(filepath)
            os.remove(filepath)
    cv2.waitKey(0)

经过上面的代码,所有图片的尺寸都变成了224*224*3,因为我们需要获取那些图片是猫,那些图片是狗,虽然图片的名称里标注了是猫还是狗,不过我们这里采用手动的方式,把前12500张猫的图片放到cat文件夹里,后面的12500张狗的图片放到dog文件夹里

接下来我们根据这两个文件夹,我把图片读取进来,分成2类,猫分类为0,狗分类1,并转换为TensorFlow专用格式,见

create_and_read_TFRecord2.py
import tensorflow as tf
import numpy as np
import os
img_width = 224
img_height = 224


def get_file(file_dir):
    images = []
    temp = []
    for root, sub_folders, files in os.walk(file_dir):
        for name in files:
            images.append(os.path.join(root, name))
        for name in sub_folders:
            temp.append(os.path.join(root, name))
    labels = []
    for one_folder in temp:
        n_img = len(os.listdir(one_folder))
        letter = one_folder.split('\\')[-1]
        if letter == 'cat':
            labels = np.append(labels, n_img * [0])
        else:
            labels = np.append(labels, n_img * [1])
    # shuffle
    temp = np.array([images, labels])
    temp = temp.transpose()
    np.random.shuffle(temp)
    image_list = list(temp[:, 0])
    label_list = list(temp[:, 1])
    label_list = [int(float(i)) for i in label_list]

    return image_list, label_list


def get_batch(image_list, label_list, img_width, img_height, batch_size, capacity):

    image = tf.cast(image_list, tf.string)
    label = tf.cast(label_list, tf.int32)

    input_queue = tf.train.slice_input_producer([image,label])

    label = input_queue[1]
    image_contents = tf.read_file(input_queue[0])
    image = tf.image.decode_jpeg(image_contents,channels=3)

    image = tf.image.resize_image_with_crop_or_pad(image,img_width,img_height)
    image = tf.image.per_image_standardization(image) # 将图片标准化
    image_batch,label_batch = tf.train.batch([image,label],batch_size=batch_size,num_threads=64,capacity=capacity)
    label_batch = tf.reshape(label_batch,[batch_size])

    return image_batch,label_batch


def onehot(labels):
    n_sample = len(labels)
    n_class = max(labels) + 1
    onehot_labels = np.zeros((n_sample, n_class))
    onehot_labels[np.arange(n_sample), labels] = 1
    return onehot_labels

这里定义的get_file函数对输入文件的文件夹进行分类,通过以不同的文件夹作为分类标准将图片分为2类,使用2个列表文件分别用来存储图片地址和对应的标记地址

get_batch函数是通过对列表地址的读取而循环载入具有参数batch_size大小而定的图片,并读取相应的图片标签作为数据标签一同进行训练

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值