当前位置: 首页 > news >正文

有哪些做封面的网站代理贷款平台加盟

有哪些做封面的网站,代理贷款平台加盟,网站开发完整教程,自建网站编程学习参考来自 CNN可视化Convolutional Featureshttps://github.com/wmn7/ML_Practice/blob/master/2019_05_27/filter_visualizer.ipynb 文章目录 filter 的激活值 filter 的激活值 原理#xff1a;找一张图片#xff0c;使得某个 layer 的 filter 的激活值最大#xff0c… 学习参考来自 CNN可视化Convolutional Featureshttps://github.com/wmn7/ML_Practice/blob/master/2019_05_27/filter_visualizer.ipynb 文章目录 filter 的激活值 filter 的激活值 原理找一张图片使得某个 layer 的 filter 的激活值最大这张图片就是能被这个 filter 所检测的对象。 来个案例流程 初始化一张图片, 56X56使用预训练好的 VGG16 网络固定网络参数若想可视化第 40 层 layer 的第 k 个 filter 的 conv, 我们设置 loss 函数为 (-1*神经元激活值)梯度下降, 对初始图片进行更新对得到的图片X1.2, 得到新的图片重复上面的步骤 其中第五步比较关键我们可以看到初始化的图片不是很大只有56X56. 这是因为原文作者在实际做的时候发现若初始图片较大得到的特征的频率会较高即没有现在这么好的显示效果。 import torch from torch.autograd import Variable from PIL import Image, ImageOps import torchvision.transforms as transforms import torchvision.models as modelsimport numpy as np import cv2 from cv2 import resize from matplotlib import pyplot as pltdevice torch.device(cuda if torch.cuda.is_available() else cpu)initialize input image sz 56 img np.uint(np.random.uniform(150, 180, (3, sz, sz))) / 255 # (3, 56, 56) img torch.from_numpy(img[None]).float().to(device) # (1, 3, 56, 56)pretrained model model_vgg16 models.vgg16_bn(pretrainedTrue).features.to(device).eval() # downloading /home/xxx/.cache/torch/hub/checkpoints/vgg16_bn-6c64b313.pth, 500M # print(model_vgg16) # print(len(list(model_vgg16.children()))) # 44 # print(list(model_vgg16.children()))get the filters output of one layer # 使用hook来得到网络中间层的输出 class SaveFeatures():def __init__(self, module):self.hook module.register_forward_hook(self.hook_fn)def hook_fn(self, module, input, output):self.features output.clone()def close(self):self.hook.remove()layer 42 activations SaveFeatures(list(model_vgg16.children())[layer])backpropagation, setting hyper-parameters lr 0.1 opt_steps 25 # 迭代次数 filters 265 # layer 42 的第 265 个 filter使其激活值最大 upscaling_steps 13 # 图像放大次数 blur 3 upscaling_factor 1.2 # 放大倍率preprocessing of datasets cnn_normalization_mean torch.tensor([0.485, 0.456, 0.406]).view(-1, 1, 1).to(device) cnn_normalization_std torch.tensor([0.299, 0.224, 0.225]).view(-1, 1, 1).to(device)gradient descent for epoch in range(upscaling_steps): # scale the image up up_scaling_steps timesimg (img - cnn_normalization_mean) / cnn_normalization_stdimg[img 1] 1img[img 0] 0print(Image Shape1:, img.shape)img_var Variable(img, requires_gradTrue) # convert image to Variable that requires gradoptimizeroptimizer torch.optim.Adam([img_var], lrlr, weight_decay1e-6)for n in range(opt_steps):optimizer.zero_grad()model_vgg16(img_var) # forwardloss -activations.features[0, filters].mean() # max the activationsloss.backward()optimizer.step()restore the imageprint(Loss:, loss.cpu().detach().numpy())img img_var * cnn_normalization_std cnn_normalization_meanimg[img1] 1img[img0] 0img img.data.cpu().numpy()[0].transpose(1,2,0)sz int(upscaling_factor * sz) # calculate new image sizeimg cv2.resize(img, (sz, sz), interpolationcv2.INTER_CUBIC) # scale image upif blur is not None:img cv2.blur(img, (blur, blur)) # blur image to reduce high frequency patternsprint(Image Shape2:, img.shape)img torch.from_numpy(img.transpose(2, 0, 1)[None]).to(device)print(Image Shape3:, img.shape)print(str(epoch), , Finished)print(*10)activations.close() # remove the hookimage img.cpu().clone() image image.squeeze(0) unloader transforms.ToPILImage()image unloader(image) image cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) cv2.imwrite(res1.jpg, image) torch.cuda.empty_cache() Image Shape1: torch.Size([1, 3, 56, 56]) Loss: -6.0634975 Image Shape2: (67, 67, 3) Image Shape3: torch.Size([1, 3, 67, 67]) 0 , FinishedImage Shape1: torch.Size([1, 3, 67, 67]) Loss: -7.8898916 Image Shape2: (80, 80, 3) Image Shape3: torch.Size([1, 3, 80, 80]) 1 , FinishedImage Shape1: torch.Size([1, 3, 80, 80]) Loss: -8.730318 Image Shape2: (96, 96, 3) Image Shape3: torch.Size([1, 3, 96, 96]) 2 , FinishedImage Shape1: torch.Size([1, 3, 96, 96]) Loss: -9.697872 Image Shape2: (115, 115, 3) Image Shape3: torch.Size([1, 3, 115, 115]) 3 , FinishedImage Shape1: torch.Size([1, 3, 115, 115]) Loss: -10.190881 Image Shape2: (138, 138, 3) Image Shape3: torch.Size([1, 3, 138, 138]) 4 , FinishedImage Shape1: torch.Size([1, 3, 138, 138]) Loss: -10.315895 Image Shape2: (165, 165, 3) Image Shape3: torch.Size([1, 3, 165, 165]) 5 , FinishedImage Shape1: torch.Size([1, 3, 165, 165]) Loss: -9.73861 Image Shape2: (198, 198, 3) Image Shape3: torch.Size([1, 3, 198, 198]) 6 , FinishedImage Shape1: torch.Size([1, 3, 198, 198]) Loss: -9.503629 Image Shape2: (237, 237, 3) Image Shape3: torch.Size([1, 3, 237, 237]) 7 , FinishedImage Shape1: torch.Size([1, 3, 237, 237]) Loss: -9.488493 Image Shape2: (284, 284, 3) Image Shape3: torch.Size([1, 3, 284, 284]) 8 , FinishedImage Shape1: torch.Size([1, 3, 284, 284]) Loss: -9.100454 Image Shape2: (340, 340, 3) Image Shape3: torch.Size([1, 3, 340, 340]) 9 , FinishedImage Shape1: torch.Size([1, 3, 340, 340]) Loss: -8.699549 Image Shape2: (408, 408, 3) Image Shape3: torch.Size([1, 3, 408, 408]) 10 , FinishedImage Shape1: torch.Size([1, 3, 408, 408]) Loss: -8.90135 Image Shape2: (489, 489, 3) Image Shape3: torch.Size([1, 3, 489, 489]) 11 , FinishedImage Shape1: torch.Size([1, 3, 489, 489]) Loss: -8.838546 Image Shape2: (586, 586, 3) Image Shape3: torch.Size([1, 3, 586, 586]) 12 , Finished Process finished with exit code 0得到特征图 网上找个图片测试下看响应是不是最大 测试图片 import torch from torch.autograd import Variable from PIL import Image, ImageOps import torchvision.transforms as transforms import torchvision.models as modelsimport numpy as np import cv2 from cv2 import resize from matplotlib import pyplot as pltdevice torch.device(cuda if torch.cuda.is_available() else cpu)class SaveFeatures():def __init__(self, module):self.hook module.register_forward_hook(self.hook_fn)def hook_fn(self, module, input, output):self.features output.clone()def close(self):self.hook.remove()size (224, 224) picture Image.open(./bird.jpg).convert(RGB) picture ImageOps.fit(picture, size, Image.ANTIALIAS)loader transforms.ToTensor() picture loader(picture).to(device) print(picture.shape)cnn_normalization_mean torch.tensor([0.485, 0.456, 0.406]).view(-1, 1, 1).to(device) cnn_normalization_std torch.tensor([0.229, 0.224, 0.225]).view(-1, 1, 1).to(device)picture (picture-cnn_normalization_mean) / cnn_normalization_stdmodel_vgg16 models.vgg16_bn(pretrainedTrue).features.to(device).eval() print(list(model_vgg16.children())[40]) # Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)) print(list(model_vgg16.children())[41]) # BatchNorm2d(512, eps1e-05, momentum0.1, affineTrue, track_running_statsTrue) print(list(model_vgg16.children())[42]) # ReLU(inplaceTrue)layer 42 filters 265 activations SaveFeatures(list(model_vgg16.children())[layer])with torch.no_grad():picture_var Variable(picture[None])model_vgg16(picture_var) activations.close()print(activations.features.shape) # torch.Size([1, 512, 14, 14])# 画出每个 filter 的平均值 mean_act [activations.features[0, i].mean().item() for i in range(activations.features.shape[1])] plt.figure(figsize(7,5)) act plt.plot(mean_act, linewidth2.) extraticks [filters] ax act[0].axes ax.set_xlim(0, 500) plt.axvline(xfilters, colorgray, linestyle--) ax.set_xlabel(feature map) ax.set_ylabel(mane activation) ax.set_xticks([0, 200, 400] extraticks) plt.show() torch.Size([3, 224, 224]) Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)) BatchNorm2d(512, eps1e-05, momentum0.1, affineTrue, track_running_statsTrue) ReLU(inplaceTrue) torch.Size([1, 512, 14, 14])可以看到265 特征图对该输入的相应最高 总结实测了其他 layer 和 filter画出来的直方图中对应的 filter 相应未必是最高的不过也很高可能找的待测图片并不是最贴合设定 layer 的某个 filter 的特征。
http://www.hkea.cn/news/14192688/

相关文章:

  • 自己购买域名做网站做网站的标性
  • 营口建设信息网站新乡网站建设费用
  • 公司网站设计注意什么访问自己做的网站吗
  • 网站建设的流程范文1500字科技公司企业网站源码
  • 长沙做网站的公司哪家最好各人可做的外贸网站
  • 网站做京东联盟深圳网络营销做什么的
  • 网站上传连接失败的原因做淘宝美工和网站设计那个好
  • 江苏军民融合网站建设海南信息港官网
  • 找能做网站的好品质高端网站设计推荐
  • 网站建设代理开发科技企业服务wordpress 用户组
  • 巴州区建设局网站网站建设技术jsp课程设计
  • 网站联盟系统ip备案信息查询
  • 网站logo怎么做动态资金盘网站开发价格
  • 淘宝网站基础建设 托管自己如何做团购网站
  • 做二手车有哪些网站有哪些手续费网站怎么做付费项目
  • 开发一个网站系统报价html查看器
  • 做盈利的设计素材网站有前途辽宁科技学院教务系统
  • 专做蔬菜水果的网站手机网站切图
  • 在百度上做购物网站无锡装修网站
  • 网站都需要续费做餐饮的餐具网站有哪些
  • 凯杰建设有限公司官方网站开网站供免费下载
  • 辽宁智能建站系统价格静态网站html
  • 做视频网站 投入电商模式
  • 卓越亚马逊网站建设目的凡科建站代理平台
  • 旅游网站模块报价wordpress去除谷歌
  • wordpress建站难不难菏泽定制网站建设推广
  • 最流行的网站设计风格关键词查找
  • 建设网站存在的问题免费的个人网站注册
  • 成都专业网站设计公司网站源码可以做淘宝客
  • php做网站后台教程常见的网络服务有哪些