pytorch构建卷积⾃编码器实现图⽚的压缩的功能,图⽚检索(以图搜图)功能
1.使⽤Pytorch构建卷积⾃编码器,尝试对MNIST数据集进⾏压缩。
2.利⽤上述⾃编码器完成⼿写数字的“以图搜图”的功能。
要求有构建与训练⾃编码器的代码和训练过程,以及以图搜图的结果(在测试集中随机拿出10张类别不同的图⽚,查找其中每个图⽚
的最接近的5-10张图⽚)。
代码实现:
importnumpyasnp
importtorch
importtorchvision
portDatat,DataLoader
fromtorchimportoptim
tsimportMNIST
fromPILimportImage
asplt
_schedulerimportStepLR
importos
fromtorchvisionimporttransformsasT
importcv2
n["CUDA_VISIBLE_DEVICES"]="0,1,2"
transform=e([
((28,28)),
ale(),
or(),
])
#定义数据对象
classMNISTDatat(Datat):
def__init__(lf,images,transform):
t=(32)
orm=transform
def__getitem__(lf,index):
data=t[index]
data=y(data)
data=ray(data)
data=orm(data)
returndata
def__len__(lf):
returnlen(t)
##加载数据
defload_mnist():
trainMnist=MNIST('',download=Fal,train=True)
testMnist=MNIST('',download=Fal,train=Fal)
trainMnist=MNISTDatat(trainMnist,transform=transform)
testMnist=MNISTDatat(testMnist,transform=transform)
trainLoader=DataLoader(trainMnist,shuffle=True,batch_size=256)
testLoader=DataLoader(testMnist,shuffle=Fal,batch_size=24)
return(trainLoader,testLoader,trainMnist,testMnist)
#定义⽹络模型(卷积⾃编码器)
classEncodeModel():
def__init__(lf,judge=True):
super(EncodeModel,lf).__init__()
=tial(
2d(1,16,kernel_size=7),#16*22*22
orm2d(16),
(inplace=True),
(inplace=True),
l2d(kernel_size=2,stride=2),#16*11*11
2d(16,4,kernel_size=3),#4*9*9
orm2d(4),
(inplace=True),
l2d(kernel_size=2,stride=2),#4*4*4
2d(4,4,kernel_size=2),#4*3*3
orm2d(4),
(inplace=True),
)
=tial(
anspo2d(4,1,kernel_size=5,stride=2),#1*9*9
orm2d(1),
(inplace=True),
anspo2d(1,1,kernel_size=7,stride=2),#1*23*23
orm2d(1),
(inplace=True),
anspo2d(1,1,kernel_size=6,stride=1),#1*28*28
orm2d(1),
(inplace=True),
)
defforward(lf,x,judge):
enOutputs=(x)
outputs=(enOutputs)
ifjudge:
returnoutputs
el:
returnenOutputs
deftrain(trainLoader,testLoader):
model=EncodeModel().cuda()
optimizer=(ters(),lr=0.001)
scheduler=StepLR(optimizer,step_size=5,gamma=0.8)
criterion=s().cuda()
epochs=50
forepochinrange(epochs):
for(i,trainData)inenumerate(trainLoader):
trainData=()
outputs=model(trainData,True).cuda()
_grad()
loss=criterion(outputs,trainData)
rd()
()
(_dict(),'')
print('epoch:{}loss:{:7f}'.format(epoch,()))
()
(Fal)
for(i,testData)inenumerate(testLoader):
testData=()
outputs=model(testData,True)
(1)
testData=('cpu')
outputs=('cpu')
((_grid(outputs).permute((1,2,0))).detach().numpy())
()
break
(True)
returnmodel
#以图搜图函数
defarch_by_image(testMnist,inputImage,K=5):
model=EncodeModel()
model=EncodeModel()
_state_dict((''))
(Fal)
criterion=s()
testLoader=DataLoader(testMnist,batch_size=1,shuffle=Fal)
inputImage=eze(0)
inputEncode=model(inputImage,Fal)
lossList=[]
for(i,testImage)inenumerate(testLoader):
testEncode=model(testImage,Fal)
enLoss=criterion(inputEncode,testEncode)
((i,()))
lossList=sorted(lossList,key=lambdax:x[1],rever=Fal)[:K]
(1)
trueImage=e(0).squeeze(0)
(trueImage,cmap='gray',shape=(28,28))
('true')
()
forjinrange(K):
showImage=testMnist[lossList[j][0]]
showImage=e(0)
showImage=(showImage)
t(1,5,j+1)
(showImage,cmap='gray')
('sim')
()
if__name__=='__main__':
trainLoader,testLoader,trainMnist,testMnist=load_mnist()
#model=train(trainLoader,testLoader)
i=0
forinputImageintestMnist:
arch_by_image(testMnist,inputImage)
i+=1
ifi>200:
break
训练效果:
本文发布于:2023-03-11 00:57:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1678467470209286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:以图找图.doc
本文 PDF 下载地址:以图找图.pdf
留言与评论(共有 0 条评论) |