matlab vgg19提取语义特征,基于Pythorch加载vgg19的Python深度学习方法(第12章实际生产模型第12.1节深度梦想模型),PyTorch,VGG19,使用,Chapter12...

import torch

import torch.nn as nn

from torchvision import models

from torchvision import transforms, utils

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

from PIL import Image, ImageFilter, ImageChops

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

dtype = torch.float

def load_image(path):

image = Image.open(path)

plt.imshow(image)

plt.title("Image loaded successfully")

return image

normalise = transforms.Normalize(

mean=[0.485, 0.456, 0.406],

std=[0.229, 0.224, 0.225]

)

preprocess = transforms.Compose([

transforms.Resize((224,224)),

transforms.ToTensor(),

normalise

])

def deprocess(image):

images=image * torch.tensor([0.229, 0.224, 0.225],device=device) + torch.tensor([0.485, 0.456, 0.406],device=device)

return images

#加载已经放到本地的VGG19载预训练模型,可与下面这边博客对比着看,下面几行是唯一的差别

#https://blog.csdn.net/a15261893837/article/details/107074632

vgg = models.vgg19(pretrained=False)

#下面是VGG19模型本地放置的绝对路径,根据自己放置位置填写就可以了

#C:\Users\shh\.cache\torch\checkpoints\vgg19-dcbb9e9d.pth

pre = torch.load(r'C:\Users\shh\.cache\torch\checkpoints\vgg19-dcbb9e9d.pth')

vgg.load_state_dict(pre)

# vgg = models.vgg19(pretrained=True)

#vgg = vgg.to(device)

print(vgg)

modulelist = list(vgg.features.modules())

def prod(image, layer, iterations, lr):

input = preprocess(image).unsqueeze(0)

input=input.to(device).requires_grad_(True)

vgg.zero_grad()

for i in range(iterations):

out = input

for j in range(layer):

out = modulelist[j+1](out)

#以特征值的L2为损失值

loss = out.norm()

loss.backward()

#使梯度增大

with torch.no_grad():

input += lr * input.grad

input = input.squeeze()

#交互维度

input.transpose_(0,1)

input.transpose_(1,2)

#使数据限制在[0,1]之间

input = np.clip(deprocess(input).detach().cpu().numpy(), 0, 1)

im = Image.fromarray(np.uint8(input*255))

return im

def deep_dream_vgg(image, layer, iterations, lr, octave_scale=2, num_octaves=20):

if num_octaves>0:

image1 = image.filter(ImageFilter.GaussianBlur(2))

if(image1.size[0]/octave_scale < 1 or image1.size[1]/octave_scale<1):

size = image1.size

else:

size = (int(image1.size[0]/octave_scale), int(image1.size[1]/octave_scale))

#缩小图片

image1 = image1.resize(size,Image.ANTIALIAS)

image1 = deep_dream_vgg(image1, layer, iterations, lr, octave_scale, num_octaves-1)

size = (image.size[0], image.size[1])

#放大图片

image1 = image1.resize(size,Image.ANTIALIAS)

image = ImageChops.blend(image, image1, 0.6)

img_result = prod(image, layer, iterations, lr)

img_result = img_result.resize(image.size)

plt.imshow(img_result)

return img_result

night_sky = load_image('data/starry_night.jpg')

#使用VGG19的第32层

night_sky_32 = deep_dream_vgg(night_sky, 32, 6, 0.2)

#使用VGG19的第8层

night_sky_8 = deep_dream_vgg(night_sky, 8, 6, 0.2)

#使用VGG19的第4层

night_sky_4 = deep_dream_vgg(night_sky, 4, 6, 0.2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值