样式迁移 讨论区

http://zh.diveintodeeplearning.org/chapter_computer-vision/neural-style.html

Where to find the document for mx.image.imresize() ?

从网上找了很久,但是MXNet的API貌似没有这个函数的相关信息。

>>> help(mxnet.image.imresize)
Help on function _cvimresize:

_cvimresize(src=None, w=_Null, h=_Null, interp=_Null, out=None, name=None, **kwargs)
    Resize image with OpenCV.


    Parameters
    ----------
    src : NDArray
        source image
    w : int, required
        Width of resized image.
    h : int, required
        Height of resized image.
    interp : int, optional, default='1'
        Interpolation method (default=cv2.INTER_LINEAR).

    out : NDArray, optional
        The output NDArray to hold the result.

    Returns
    -------
    out : NDArray or list of NDArrays
        The output of this function.

是不是底层算法有问题?在训练1200 x 800尺寸时,报如下错误,Jupyter Kernel 重启,能帮忙看看是什么原因吗?非常感谢!

出错的地方是broadcast失败,原因不明。如果我改成训练小尺寸,比如300x200就不会有问题。

我用的CPU 直接跑的,没有用GPU, 减少了epoch到20次。

[15:09:16] src/operator/tensor/./elemwise_binary_broadcast_op.h:67: Check failed: l == 1 || r == 1 operands could not be broadcast together with shapes [1,512,100,150] [1,512,25,37]


MXNetError Traceback (most recent call last)
in ()
13 z = train(x, 20, 0.1, 100)
14
—> 15 train1200x800()

in train1200x800()
11
12 #z = train(x, 300, 0.1, 100)
—> 13 z = train(x, 20, 0.1, 100)
14
15 train1200x800()

in train(x, max_epochs, lr, lr_decay_epoch)
9 x, content_layers, style_layers)
10 content_L = sum_loss(
—> 11 content_loss, content_py, content_y, content_weights)
12 style_L = sum_loss(
13 style_loss, style_py, style_y, style_weights)

in sum_loss(loss, preds, truths, weights)
1 def sum_loss(loss, preds, truths, weights):
2 return nd.add_n([wloss(yhat, y) for w, yhat, y in zip(
----> 3 weights, preds, truths)])

in (.0)
1 def sum_loss(loss, preds, truths, weights):
----> 2 return nd.add_n([wloss(yhat, y) for w, yhat, y in zip(
3 weights, preds, truths)])

in content_loss(yhat, y)
1 def content_loss(yhat, y):
----> 2 return (yhat-y).square().mean()

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/ndarray/ndarray.py in sub(self, other)
214 def sub(self, other):
215 “”“x.sub(y) <=> x-y <=> mx.nd.subtract(x, y) “””
–> 216 return subtract(self, other)
217
218 def isub(self, other):

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/ndarray/ndarray.py in subtract(lhs, rhs)
2503 operator.sub,
2504 _internal._minus_scalar,
-> 2505 _internal._rminus_scalar)
2506 # pylint: enable= no-member, protected-access
2507

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/ndarray/ndarray.py in _ufunc_helper(lhs, rhs, fn_array, fn_scalar, lfn_scalar, rfn_scalar)
2377 return lfn_scalar(lhs, float(rhs))
2378 elif isinstance(rhs, NDArray):
-> 2379 return fn_array(lhs, rhs)
2380 else:
2381 raise TypeError(‘type %s not supported’ % str(type(rhs)))

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/ndarray/register.py in broadcast_sub(lhs, rhs, out, name, **kwargs)

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/_ctypes/ndarray.py in _imperative_invoke(handle, ndargs, keys, vals, out)
90 c_str_array(keys),
91 c_str_array([str(s) for s in vals]),
—> 92 ctypes.byref(out_stypes)))
93
94 if original_output is not None:

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/base.py in check_call(ret)
144 “”"
145 if ret != 0:
–> 146 raise MXNetError(py_str(_LIB.MXGetLastError()))
147
148

MXNetError: [15:09:16] src/operator/tensor/./elemwise_binary_broadcast_op.h:67: Check failed: l == 1 || r == 1 operands could not be broadcast together with shapes [1,512,100,150] [1,512,25,37]

Stack trace returned 10 entries:
[bt] (0) /home/essen/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x194dc2) [0x7fc95f0ccdc2]
[bt] (1) /home/essen/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x1953b8) [0x7fc95f0cd3b8]
[bt] (2) /home/essen/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x7fd18f) [0x7fc95f73518f]
[bt] (3) /home/essen/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x240a55f) [0x7fc96134255f]
[bt] (4) /home/essen/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x23f5349) [0x7fc96132d349]
[bt] (5) /home/essen/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x233e95b) [0x7fc96127695b]
[bt] (6) /home/essen/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/libmxnet.so(MXImperativeInvokeEx+0x63) [0x7fc961276ec3]
[bt] (7) /home/essen/anaconda3/envs/gluon/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(ffi_call_unix64+0x4c) [0x7fc98a7dc5b0]
[bt] (8) /home/essen/anaconda3/envs/gluon/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(ffi_call+0x1f5) [0x7fc98a7dbd55]
[bt] (9) /home/essen/anaconda3/envs/gluon/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(_ctypes_callproc+0x3dc) [0x7fc98a7d389c]

在训练1200*800的图片时候爆内存了。。。。。。pythonimage

把图片尺寸改成600400都能跑,但是改成900600就跑不动了,爆内存了。。。。。机器16g的内存,张到10g的时候程序就自动崩了。有没有大神有这个问题的解决办法。。。谢谢。。。。

是内存的问题。我也跟你情况一样,是单机上跑的。

搞错了,hahah

怎么直观地理解Gram矩阵能代表style?
网上查了很久,我自己也把各个layer的feature_map用mxboard画出来观察很久,没想明白

协方差矩阵代表的应该是分布之间的相关性

def style_loss(y_hat, gram_y):
    return (gram(y_hat) - gram_y).square().mean()

我理解的这个损失,就是预测的style的分布于原始样式图片的分布之间的误差。

1赞

gram对应格拉姆矩阵,主要对不同通道间进行内积操作。

这里讲了特征抽取是如何进行的。

1赞

这样理解可能更准确些:
这个gram矩阵表示不同通道上的样式特征的协方差矩阵,表示不同通道上样式特征的相关性分布;
style_loss表示了输出图片的style分布与原始样式图片的style分布之间的差异


应该是有的,上面这个链接里给出了.


这个问题怎么解决啊 下的文件损坏了

个人觉得老版代码跑出来的效果好些

网络不好,导致文件没下载完,所以说ZIP包是坏的

小白求助,下载预训练的VGG-19模型时报错: