前期准备:3通道图片60000张,如果你没有那么大的数据量,需要改变cifar-10-API中的定义,下面会具体说到。
如果你的图片是灰度图(单通道)可以用这种方法来改为三通道:
opencv将灰度图转化为RGB三通道图像
要求为python2.7版本,由于cifar10就是在python2.7下面定义的,用python3版本与2.7版本最主要的不同是在2.7版本中打包模块为cPickle,而在python3中为pickle。在cifar-10-API中打开二进制文件为cPickle。
有任何问题的可以在下方留言,或者邮箱留言:[email protected]
def img_tra():
for k in range(0,num):
currentpath=folder+"/"+imglist[k]
im=Image.open(currentpath)
#width=im.size[0]
#height=im.size[1]
x_s=32
y_s=32
out = im.resize((x_s,y_s),Image.ANTIALIAS)
out.save(folder_ad+"/"+str(imglist[k]))
python版本的cifar数据集格式为在一个字典中存在batch_label,labels,data,filenames四种信息,其中batch_label与filenames为utf-8编码的字符串,data为uint8编码的numpy数组,labels为utf-8编码的列表。先将条桶图片转化为32*32的RGB图,然后依次读取RGB通道像素值,存入3*1024numpy数组,图片标签存入labels,图片名存入filenames,将其打包成一个二进制文件。
下图是打开官方cifar数据集文件内容(6个文件都一样,以data_batch_1为例)
import cPickle as p
import numpy as np
import chardet
def unpickle(file):
import cPickle
with open(file, 'rb') as fo:
dict = cPickle.load(fo, encoding='latin-1')
return dict
cc=unpickle("J:/get4/data_atch_1")
print(cc)
我们将点操作与写文件写在一个函数mkcf()函数中:
def mkcf():
global data
global list1
global list2
global list3
global list4
for k in range(0,num):
currentpath=folder_ad+"/"+imglist[k]
im=Image.open(currentpath)
with open(binpath, 'a') as f:
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
#print(imglist[k])
#print(type(cl[0]))
#with open(binpath, 'a') as f:
#print(str(cl[0]))
list1.append(cl[0])
#print(list1)
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
#with open(binpath, 'a') as f:
#mid=str(cl[1])
#f.write(mid)
list1.append(cl[1])
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
#with open(binpath, 'a') as f:
#mid=str(cl[2])
#f.write(mid)
list1.append(cl[2])
list2.append(list1)
list1=[]
#arr2=np.array(list2)
#print(arr4)
f.close()
print("image"+str(k+1)+"saved.")
list3.append(imglist[k].encode('utf-8'))
arr2=np.array(list2,dtype=np.uint8)
data['batch_label'.encode('utf-8')]='testing batch 1 of 1'.encode('utf-8')
#addWord(cifar10,"batch_label".encode('utf-8'),'training batch 5 of 5'.encode('utf-8'))
data.setdefault('labels'.encode('utf-8'),label)
data.setdefault('data'.encode('utf-8'),arr2)
#addWord(cifar10,'labels'.encode('utf-8'),label)
#addWord(cifar10,'data'.encode('utf-8'),arr2)
data.setdefault('filenames'.encode('utf-8'),list3)
#addWord(cifar10,'filenames'.encode('utf-8'),list3)
output = open(binpath, 'wb')
pickle.dump(data, output)
output.close()
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 18 14:17:45 2017
@author: ielij
"""
import numpy as np
from PIL import Image
import operator
from os import listdir
import sys
import cPickle as pickle
import random
data={}
list1=[]
list2=[]
list3=[]
def img_tra():
for k in range(0,num):
currentpath=folder+"/"+imglist[k]
im=Image.open(currentpath)
#width=im.size[0]
#height=im.size[1]
x_s=32
y_s=32
out = im.resize((x_s,y_s),Image.ANTIALIAS)
out.save(folder_ad+"/"+str(imglist[k]))
def addWord(theIndex,word,adder):
theIndex.setdefault(word,[]).append(adder)
def seplabel(fname):
filestr=fname.split(".")[0]
label=int(filestr.split("_")[0])
return label
def mkcf():
global data
global list1
global list2
global list3
for k in range(0,num):
currentpath=folder_ad+"/"+imglist[k]
im=Image.open(currentpath)
with open(binpath, 'a') as f:
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
list1.append(cl[0])
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
#with open(binpath, 'a') as f:
#mid=str(cl[1])
#f.write(mid)
list1.append(cl[1])
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
list1.append(cl[2])
list2.append(list1)
list1=[]
f.close()
print("image"+str(k+1)+"saved.")
list3.append(imglist[k].encode('utf-8'))
arr2=np.array(list2,dtype=np.uint8)
data['batch_label'.encode('utf-8')]='testing batch 1 of 1'.encode('utf-8')
data.setdefault('labels'.encode('utf-8'),label)
data.setdefault('data'.encode('utf-8'),arr2)
data.setdefault('filenames'.encode('utf-8'),list3)
output = open(binpath, 'wb')
pickle.dump(data, output)
output.close()
folder="H:/VOC2007/test_ad"
folder_ad="H:/VOC2007/test_rs"
imglist=listdir(folder_ad)
num=len(imglist)
img_tra()
label=[]
for i in range (0,num):
label.append(seplabel(imglist[i]))
binpath="H:/VOC2007/get4/test_batch"
print(binpath)
mkcf()
需要说明一点,图片存储格式为:0_126.jpg,其中0为标签,得到标签的方式为(此函数已在总代码里)
def seplabel(fname):
filestr=fname.split(".")[0]
label=int(filestr.split("_")[0])
return label
注:如果没有那么多数据集,只想换其中一个类,即只替换6000张照片,可以考虑我做的另一个版本(cifar10-置换版本,置换标签为9的类):
http://download.csdn.net/detail/l297969586/9832449