tensorflow实现PoseNet总结

1.Ubuntu 16 释放显存的方法

sudo kill -9 PID

PID为显卡占用进程号,使用以下命令查看PID:

nvidia-smi

×××××××××在出现的界面下方有PID。

参考连接:https://blog.csdn.net/xbcreal/article/details/73733446

2.运行python train.py

    1)  IndentationError: unindent does not match any outer indentation level

  File "train.py", line 53
    mean[0] /= N
               ^
IndentationError: unindent does not match any outer indentation level

       solve:  因为用的python3,代码中tab键和空格键混合用,要统一,建议都用空格键。

 

2) ValueError: operands could not be broadcast together with shapes (224,224) (3,224) (224,224)

File "train.py", line 49, in preprocess
    mean[0][0] += X[:,:,0]
ValueError: operands could not be broadcast together with shapes (224,224) (3,224) (224,224)

违反了广播机制。

广播机制如下:

当我们使用ufunc函数对两个数组进行计算时,ufunc函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同)。如果两个数组的shape不同的话,会进行如下的广播(broadcasting)处理:

    1.让所有输入数组都向其中shape最长的数组看齐,shape中不足的部分都通过在前面加1补齐
    2.输出数组的shape是输入数组shape的各个轴上的最大值
    3.如果输入数组的某个轴和输出数组的对应轴的长度相同或者其长度为1时,这个数组能够用来计算,否则出错
    4.当输入数组的某个轴的长度为1时,沿着此轴运算时都用此轴上的第一组值

    mean = np.zeros((1, 3, 224, 224))    
    for X in tqdm(images_cropped):
                X = np.transpose(X,(2,0,1))
                mean[0][0] += X[:,:,0]
                mean[0][1] += X[:,:,1]
                mean[0][2] += X[:,:,2]
                N += 1

我们图片X的shape是(224,224,3),经过矩阵转置为(3,224,224),而mean[0][0]、mean[0][1]、mean[0][2]的shape分别都是(224, 224,1),因此X的转置与mean[0][0]、mean[0][1]、mean[0][2]不能相加,去掉转置X的格式与mean[0][0]、mean[0][1]、mean[0][2]一致,因此去掉转置即可。
修改后的程序为:

    mean = np.zeros((1, 3, 224, 224))    
    for X in tqdm(images_cropped):
                #X = np.transpose(X,(2,0,1))     #将这一操作注释掉,矩阵转制操作看本节参考连接
                mean[0][0] += X[:,:,0]
                mean[0][1] += X[:,:,1]
                mean[0][2] += X[:,:,2]
                N += 1

参考链接:https://blog.csdn.net/qq_18433441/article/details/56834207

参考链接(numpy.transpose):https://blog.csdn.net/u012762410/article/details/78912667

 

3)NameError: name 'basestring' is not defined

  File "/home/llx/tensorflow-posenet/network.py", line 77, in feed
    if isinstance(fed_layer, basestring):
NameError: name 'basestring' is not defined

basestring() 方法是 strunicode 的超类(父类),也是抽象类,因此不能被调用和实例化,但可以被用来判断一个对象是否为 str 或者 unicode 的实例,isinstance(obj, basestring) 等价于 isinstance(obj, (str, unicode))。

将源代码中:

if isinstance(fed_layer, basestring):

改为:

if isinstance(fed_layer, str):

参考连接:http://www.runoob.com/python/python-func-basestring.html

 

4)TypeError: unsupported operand type(s) for /: 'Dimension' and 'int'

  File "/home/llx/tensorflow-posenet/network.py", line 127, in conv
    kernel = self.make_var('weights', shape=[k_h, k_w, c_i / group, c_o])
TypeError: unsupported operand type(s) for /: 'Dimension' and 'int'

使用以下代码打印出c_i的类型:

print(type(c_i))

结果发现c_i的类型是:tensorflow.python.framework.tensor_shape.Dimension

solve:通过添加以下代码来强制转换c_i的类型为int:

c_i = int(input.get_shape()[-1])

 

5)UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 1: ordinal not in range(128)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 1: ordinal not in range(128)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 163, in <module>
    main()
  File "train.py", line 144, in main
    net.load('posenet.npy', sess)

ordinal not in range(128)')
You may need to pass the encoding= option to numpy.load

以下为源代码:

    def load(self, data_path, session, ignore_missing=False):
        '''Load network weights.
        data_path: The path to the numpy-serialized network weights
        session: The current TensorFlow session
        ignore_missing: If true, serialized weights for missing layers are ignored.
        '''
        data_dict = np.load(data_path).item()

在np.load()中加入encoding="latin1",修改后为:

    def load(self, data_path, session, ignore_missing=False):
        '''Load network weights.
        data_path: The path to the numpy-serialized network weights
        session: The current TensorFlow session
        ignore_missing: If true, serialized weights for missing layers are ignored.
        '''
        data_dict = np.load(data_path,encoding="latin1").item()    #加入encoding="latin1"

 

6)AttributeError: 'dict' object has no attribute 'iteritems'

File "/home/llx/tensorflow-posenet/network.py", line 62, in load
    for param_name, data in data_dict[op_name].iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

之所以会出现上述错误是因为python3中已经没有这个属性,直接改为items即可:

for param_name, data in data_dict[op_name].items():

参考连接:https://blog.csdn.net/sinat_35512245/article/details/78639317

 

7)ValueError: Parent directory of PoseNet.ckpt doesn't exist, can't save.

File "train.py", line 156, in main
    saver.save(sess, outputFile)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 1669, in save
    raise exc
ValueError: Parent directory of PoseNet.ckpt doesn't exist, can't save.

将源代码中outputFile的绝对路径加上即可。

源码:outputFile = "PoseNet.ckpt"
修改后:outputFile = "/home/llx/tensorflow-posenet/PoseNet.ckpt"

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值