Tensor must be 4-D with last dim 1, 3, or 4,bug记录

标签: bug 图片 深度学习 tensorflow pytorch
6人阅读 评论(0) 收藏 举报
分类:

torch学习入坑(1)
记录一下第一个坑,防止自己忘记。
在用torchvision.utils.make_grid处理完图片之后,维度会变为三维,如果需要使用tf.summary.image ,需要增加batch size 的那一维,需要用到的函数为 tf.expand_dims。

注意:

1、torchvision.utils.make_grid函数
输入:Tensor of shape (B x C x H x W)
输出:多个图片拼接成的一个大图 三维 没有batch size 那一维
like this:
batch size 16

以下解释来自官网
链接http://pytorch.org/docs/master/torchvision/utils.html
torchvision.utils.make_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0)
scale_each=False, pad_value=0)

作用:Make a grid of images.
Parameters:

    tensor (Tensor or list) – 4D mini-batch Tensor of shape (B x C x H x W) or a list of images all of the same size.
    nrow (int, optional) – Number of images displayed in each row of the grid. The Final grid size is (B / nrow, nrow). Default is 8.
    padding (int, optional) – amount of padding. Default is 2.
    normalize (bool, optional) – If True, shift the image to the range (0, 1), by subtracting the minimum and dividing by the maximum pixel value.
    range (tuple, optional) – tuple (min, max) where min and max are numbers, then these numbers are used to normalize the image. By default, min and max are computed from the tensor.
    scale_each (bool, optional) – If True, scale each image in the batch of images separately rather than the (min, max) over all images.
    pad_value (float, optional) – Value for the padded pixels.

2、顺便记录一下另外一个函数 torchvision.utils.save_image:
torchvision.utils.save_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0)
Save a given Tensor into an image file.
Parameters:

tensor (Tensor or list) – Image to be saved. If given a mini-batch tensor, saves the tensor as a grid of images by calling make_grid.
   **kwargs – Other arguments are documented in make_grid.

3、tf.summary.image:
官网链接:
http://www.tensorfly.cn/tfdoc/api_docs/python/train.html#image_summary

tf.image_summary(tag, tensor, max_images=None, collections=None, name=None)

Outputs a Summary protocol buffer with images.

The summary has up to max_images summary values containing images. The images are built from tensor which must be 4-D with shape [batch_size, height, width, channels] and where channels can be:

    1: tensor is interpreted as Grayscale.
    3: tensor is interpreted as RGB.
    4: tensor is interpreted as RGBA.

4、 tf.expand_dims 增加维度:
官网例子:

# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]

# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

Args:
input: A Tensor.
dim: A Tensor. Must be one of the following types: int32, int64. 0-D (scalar). Specifies the dimension index at which to expand the shape of input.
name: A name for the operation (optional).

Returns:
A Tensor. Has the same type as input. Contains the same data as input, but its shape has an additional dimension of size 1 added.

例子:重要!!

# coding=utf-8


from __future__ import print_function
from six.moves import range

import torch.backends.cudnn as cudnn
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
import torchvision.utils as vutils
import torchvision.datasets as dset
import torchvision.transforms as transforms
import numpy as np
import os
import time
import torchvision
from PIL import Image, ImageFont, ImageDraw
from copy import deepcopy
import tensorflow as tf
from torch.utils.data import DataLoader, Dataset
# from miscc.config import cfg
# from miscc.utils import mkdir_p
# from PIL import Image
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

sess = tf.InteractiveSession()
# def test():
# torchvision输出的是PILImage,值的范围是[0, 1].
# 我们将其转化为tensor数据,并归一化为[-1, 1]。
transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                                ])

# 训练集,将相对目录./data下的cifar-10-batches-py文件夹中的全部数据(50000张图片作为训练数据)加载到内存中,若download为True时,会自动从网上下载数据并解压
trainset = torchvision.datasets.CIFAR10(root='./test', train=True, download=False, transform=transform)

# 将训练集的50000张图片划分成12500份,每份4张图,用于mini-batch输入。shffule=True在表示不同批次的数据遍历时,打乱顺序。num_workers=2表示使用两个子进程来加载数据
trainloader = torch.utils.data.DataLoader(trainset, batch_size=3, shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
print ("len(trainset)",len(trainset))
print ("len(trainloader)",len(trainloader))
print("end1")

for i, data in enumerate(trainloader, 0):
    # print(data[i][0])
    #
    # img = transforms.ToPILImage()(data[i][0])
    # img.show()
    # break
""
以上代码来自简书 https://www.jianshu.com/p/8da9b24b2fb6
""

    real_img_set = vutils.make_grid(data[i][0]).numpy()
    # print("real_img_set1",real_img_set)
    print("real_img_set_make_grid.shape",real_img_set.shape)
    real_img_set = np.transpose(real_img_set, (1, 2, 0))
    # print("real_img_set_transpose",real_img_set)
    print("real_img_set_transpose.shape",real_img_set.shape)
    real_img_set = real_img_set * 255
    # print("real_img_set255",real_img_set)
    print("real_img_set255.shape",real_img_set.shape)
    real_img_set = real_img_set.astype(np.uint8)
    print("real_img_setend",real_img_set)
    print("real_img_setend.shape",real_img_set.shape)

    super_real_img_set = tf.expand_dims(real_img_set, 0)
    print ("super_real_img_old", super_real_img_set)
    print ("super_real_img_old shape", super_real_img_set.shape)
    print ("super_real_img_old [0]", super_real_img_set[0])
    sup_real_img = tf.summary.image('real_img', super_real_img_set)
    print("sup_real_img", sup_real_img)
    print("sup_real_img shape", sup_real_img.shape)
    sup_real_img_new = sess.run(sup_real_img)
    # summary_writer.add_summary(sup_real_img_new, count)

    break
查看评论

3、TensorFlow 的数据模型-----张量(Tensor)

一、Tensor 类简介 Tensor 定义 A Tensor is a symbolic handle to one of the outputs of an Operation. It d...
  • mzpmzk
  • mzpmzk
  • 2017-11-26 11:10:36
  • 816

TensorFlow学习--CIFAR-10

CIFAR-10数据集 CIFAR-10数据集包含10个类的60000张32x32的彩色图像,每个类有6000张图像.有50000张训练图像和10000张测试图像.CIFAR-10数据集 10个分...
  • akadiao
  • akadiao
  • 2018-03-20 16:25:25
  • 159

Python数据分析001——numpy数组的深入思考

很多人都觉得程序员离不开google,有问题就google已经成了习惯,但是我个人感觉,仅仅每次遇到问题就查查查,并不是最好的做法,深入思考原理和总结是更关键的。最近工作中接触到很多的数据分析,每天都...
  • shengxiaobufu
  • shengxiaobufu
  • 2017-12-29 15:13:10
  • 108

TensorFlow 从入门到精通(六):tensorflow.nn 详解

看过前面的例子,会发现实现深度神经网络需要使用 tensorflow.nn 这个核心模块。我们通过源码来一探究竟。# Copyright 2015 Google Inc. All Rights Res...
  • kkk584520
  • kkk584520
  • 2016-05-23 14:30:06
  • 54063

AsTensorError: ('Cannot convert Tensor("mul:0", shape=(19, 19, 5, 80), dtype=float32) to TensorType'

在做deeplearning.ai 第四课第三周的作业Autonomous driving application - Car detection 时出现的问题,原因是我的后端是theano,需要切换...
  • lh15123as
  • lh15123as
  • 2017-12-20 16:13:36
  • 204

ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32)

将Django和Keras结合时,可能回报出ValueError: Tensor Tensor(“Placeholder:0”, shape=(3, 3, 1, 32), dtype=float32)...
  • qq_37879432
  • qq_37879432
  • 2018-01-11 12:04:07
  • 617

Tensor A must be from the same graph as Tensor B

关于tf.Graph().as_default()带来的ValueError: Tensor A must be from the same graph as Tensor B问题...
  • potato_uncle
  • potato_uncle
  • 2017-12-01 22:45:58
  • 1025

机器学习和数据挖掘4——NumPy快速入门

机器学习和数据挖掘4——NumPy快速入门 NumPy快速入门 一、关于Numpy Numpy是Python第一个矩阵类型,提供了大量矩阵处理的函...
  • qq_33792843
  • qq_33792843
  • 2017-11-10 11:20:48
  • 93

Numpy快速入门

Numpy快速入门
  • sunpeng19960715
  • sunpeng19960715
  • 2016-10-10 13:55:51
  • 2701

使用tf.convert_to_tensor 时遇到value error 怎么解决

问题描述最近写tensorflow 程序,使用PIL读取图片后转存为numpy 数组,然后创建batch数据时使用的是list,即,list元素是array,然而在将这个list转换为tensor时,...
  • u010698086
  • u010698086
  • 2017-10-08 17:04:02
  • 1032
    个人资料
    持之以恒
    等级:
    访问量: 4978
    积分: 211
    排名: 34万+