python图像处理-图⽚分类项⽬⽬录
最近帮朋友做⼀个图⽚数据分类的项⽬,借鉴了⼤佬的代码,修修改改,话不多说直接上代码!⼀、图⽚转batch⽂件
import numpy as np
from PIL import Image
import operator
from os import listdir
import sys
import pickle
import random
data ={}
list1 =[]
list2 =[]
list3 =[]
# 将图⽚转化为32*32的三通道图⽚
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 fname
# 我们将点操作与写⽂件写在⼀个函数mkcf()函数中
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)# 打开图⽚
im = Image.open(currentpath)# 打开图⽚
# im = im.convert("RGB")
im = im.convert('L')
with open(binpath,'a')as f:# a是函数的设定,打开图⽚,添加信息到list1
for i in range(0,32):
for j in range(0,32):
# print(im)
# de)
# pixel((0, 0)))
cl = im.getpixel((i, j))
print(cl)
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')]='batch_1'.encode('utf-8')# batch label包的名字 data.setdefault('labels'.encode('utf-8'), label)# lable 特征
data.setdefault('data'.encode('utf-8'), arr2)# data 图像
data.setdefault('filenames'.encode('utf-8'), list3)# filename⽂件名
output =open(binpath,'wb')
pickle.dump(data, output)
output.close()
# folder = r"D:\lhh\python\demo\图⽚制作数据集\left50s"
name ='test'
folder_ad = r"D:\lhh\python\demo\图⽚制作数据集\shujuji\size54_50pics\{}".format(name) imglist = listdir(folder_ad)
num =len(imglist)
# img_tra()
label =[]
for i in range(0, num):
label.append(seplabel(imglist[i]))
# print(label)
binpath = r"D:\lhh\python\demo\图⽚制作数据集\{}batch".format(name)
mkcf()
⽣成batch⽂件
⼆、batch⽂件转json对应图⽚
读取batch⽂件,做成对应的json⽂件导⼊
#%%
import torch
import torchvision
import numpy as np
import cv2
import os
import json
def unpickle(file):
import pickle
with open(file,'rb')as fo:
dict= pickle.load(fo, encoding='bytes')
return dict
anno_loc = r'D:\lhh\python\demo\图⽚制作数据集\shujuji\annotations' #判断⽂件夹是否存在,不存在的话创建⽂件夹
if ists(anno_loc)==False:
os.mkdir(anno_loc)
#⽤于存放图⽚⽂件名及标注
train_filenames =[]
train_annotations =[]
test_filenames =[]
test_annotations=[]
#训练集有五个批次,每个批次10000个图⽚,测试集有10000张图⽚def eye_annotations(file_dir):
print('creat train_img annotations')
for i in range(1,5):
data_name = file_dir +'/'+'data_batch_'+str(i)
data_dict = unpickle(data_name)
print(data_name +' is processing')
for j in range(10):
img_name =str(data_dict[b'labels'][j])
img_annotations = data_dict[b'labels'][j]
train_filenames.append(img_name)
train_annotations.append(img_annotations)
print(data_name +' is done')
test_data_name = file_dir +'/test_batch'
print(test_data_name +' is processing')
test_dict = unpickle(test_data_name)
for m in range(10):
testimg_name =str(test_dict[b'labels'][m])
testimg_annotations = test_dict[b'labels'][m]#str(test_dict[b'labels'][m]) test_dict[b'labels'][m]
test_filenames.append(testimg_name)
test_annotations.append(testimg_annotations)
print(test_data_name +' is done')
import pickleprint('Finish file processing')
if __name__ =='__main__':
file_dir ='D:\lhh\python\demo\图⽚制作数据集\shujuji'
eye_annotations(file_dir)
train_annot_dict ={
'images': train_filenames,
'categories': train_annotations
}
test_annot_dict ={
'images':test_filenames,
'categories':test_annotations
}
# print(annotation)
train_json = json.dumps(train_annot_dict)
print(train_json)
train_file =open(r'D:\lhh\python\demo\图⽚制作数据集\shujuji\annotations\eye_train.json','w')
train_file.write(train_json)
train_file.close()
test_json =json.dumps(test_annot_dict)
test_file =open(r'D:\lhh\python\demo\图⽚制作数据集\shujuji\annotations\eye_test.json','w')
test_file.write(test_json)
test_file.close()
print('annotations have writen to json file')
#%%
三、模型训练
# %% md
# ⽬录
## 1. [CIFAR10数据集介绍](#1.CIFAR10数据集介绍)
## 2. [VGG19起源与介绍](#2.VGG19起源与介绍)
# 2.1.[VGGNet简介]( # 2.1.VGGNet简介)
# 2.2.[VGG16与VGG19的对⽐]( # 2.2.VGG16与VGG19的对⽐)
# ## 3. [利⽤Pytorch构建VGG19模型](#3.利⽤Pytorch构建VGG19模型)
# 3.1.[数据预处理]( # 3.1.数据预处理)
# 3.2.[搭建VGG19]( # 3.2.搭建VGG19)
# 3.3.[训练VGG19]( # 3.3.训练VGG19)
# ## 4. [总结](#4.总结)
#
# # %% md
#
# # 1.CIFAR10数据集介绍
# CIFAR - 10
# 数据集由10个类别共60000张32×32
# 的彩⾊图像组成,每个类别有6000张图像。有50000张训练图像和10000张测试图像。
#
# 数据集分为五个训练批次和⼀个测试批次,每个批次有10000张图像。测试批次包含来⾃每个类别的恰好1000个随机选择的图像。但⼀些训练批次中某个类别的图像张数要⽐其它类别多,总体来说,五个训练批次⼀共包含来⾃每个类别的正好5000张图像。
#
# 以下是数据集中的类别,以及来⾃每个类别的10个随机图像:
# ![png](https: // i.loli / 2018 / 02 / 21 / 5
# a8cd1b0076ff.png)
# %% md
# 2.VGG19起源与介绍
# %% md
## 2.1. VGGNet简介
# VGGNet是⽜津⼤学计算机视觉组(Visual
# Geometry
# Group)和Google
# DeepMind公司的研究员⼀起研发的卷积神经⽹络。VGGNet探索了卷积神经⽹络的深度与其性能之间的关系,通过反复的使⽤$3\times3$的⼩型卷积核和$2\t imes2$的最⼤池化层,VGGNet成功地构筑了16~19
# 层深的卷积神经⽹络。
#
# VGG19的⽹络结构如图所⽰:
# ![png](https: // raw.githubusercontent / shiyadong123 / Myimage / master / 20170816092916647.
# png)
# %% md
## 2.2. VGG16与VGG19的对⽐
# 相较于VGG16,VGG19⽹络增加了3个卷积层,其余的部分是相同的。
# ![png](https: // github / shiyadong123 / Myimage / blob / master / 20190217165325787
# _meitu_1.png?raw = true)
# %% md
# 3. 利⽤Pytorch构建VGG19模型
# %% md
## 3.1. 数据预处理
# 在搭建VGG19⽹络结构之前,⾸先要对数据进⾏⼀些预处理。
# %%
'''
import json
import matplotlib.pyplot as plt
import numpy as np
from torch.utils.data import Dataset,DataLoader
'''
# %%
# 导⼊必要的库
import torch
import torchvision
from torch.optim import lr_scheduler
as nn
functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torchvision.datasets as dsets
ansforms as trans
import time
import matplotlib.pyplot as plt
import numpy as np
# %%
from torch.utils.data import Dataset
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论