使用CNNs网络,基于caltech 101数据集实现分类

首先下载101数据集

caltech 101

把文件解压,路径格式如图所示。

此处是以pycharm为基础(pycharm以当前文件夹的目录为根目录)

路径描述

使用该数据集,制作npy易读文件格式

def data_process(img_size):
    imgs = []
    labels = []
    img_size = img_size
    size = (img_size, img_size)

    for i, category in enumerate(tqdm(categories)):
        for f in os.listdir(path + "/" + categories[i]):
            fullpath = os.path.join(path + "/" + categories[i], f)
            # print(fullpath)
            img = Image.open(fullpath)
            img = np.asarray(img.resize(size, Image.ANTIALIAS))
            # img = np.asarray(img.resize(size)
            if img.shape == (img_size, img_size, 3):
                imgs.append(np.array(img))
                label_curr = i
                labels.append(label_curr)
                # imgs_temp = [imgs, labels]
    np.save(root_path + '/' + 'x'+str(img_size), imgs)
    np.save(root_path + '/' + 'y'+str(img_size), labels)
img_size = 200
full_path =root_path + '/' + 'x'+str(img_size)
if os.path.exists(full_path) is True:
    data_process(img_size)
    print("{} file already exists.".format(full_path))

相应的模块就import一下,后续会展示详细的代码。
此时就会在dataset目录下生成x200.npy和y200.npy两个文件。后续处理的时候,就会直接读取这两个npy文件作为输入数据集。

下面开始说明网络训练过程和导入。

cal_101_googlenet.py

from keras import backend as K
from keras.utils import np_utils
import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from tqdm import tqdm
from modles.googlenet import GoogLeNetBN

# set GPU usage
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
# config.gpu_options.per_process_gpu_memory_fraction = 0.4
set_session(tf.Session(config=config))
# 导入相应的模块以及进行GPU的设置

# 几个超参数的设计
image_size = 200
classes = 101

root_path = 'dataset'
path = 'dataset/Caltech 101/101_ObjectCategories'
categories = sorted(os.listdir(path))
ncategories = len(categories)
print(ncategories)
## 设置数据集的路径以及有多少类

def data_process(img_size):
    imgs = []
    labels = []
    img_size = img_size
    size = (img_size, img_size)

    for i, category in enumerate(tqdm(categories)):
        for f in os.listdir(path + "/" + categories[i]):
            fullpath = os.path.join(path + "/" + categories[i], f)
            # print(fullpath)
            img = Image.open(fullpath)
            img = np.asarray(img.resize(size, Image.ANTIALIAS))
            # img = np.asarray(img.resize(size)
            if img.shape == (img_size, img_size, 3):
                imgs.append(np.array(img))
                label_curr = i
                labels.append(label_curr)
                # imgs_temp = [imgs, labels]
    np.save(root_path 
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值