Error in output.shape[[output.names]] <- dim((X$value())$label) : attempt to select less than one element in OneIndex

i try reproduce this example for captcha recognition

OS win 2012R2

library(mxnet)

data <- mx.symbol.Variable(‘C:/Users/kravchik/Downloads/captcha_example/captcha_train.rec’)
label <- mx.symbol.Variable(‘C:/Users/kravchik/Downloads/captcha_example/captcha_train.lst’)
conv1 <- mx.symbol.Convolution(data = data, kernel = c(5, 5), num_filter = 32)
pool1 <- mx.symbol.Pooling(data = conv1, pool_type = “max”, kernel = c(2, 2), stride = c(1, 1))
relu1 <- mx.symbol.Activation(data = pool1, act_type = “relu”)

conv2 <- mx.symbol.Convolution(data = relu1, kernel = c(5, 5), num_filter = 32)
pool2 <- mx.symbol.Pooling(data = conv2, pool_type = “avg”, kernel = c(2, 2), stride = c(1, 1))
relu2 <- mx.symbol.Activation(data = pool2, act_type = “relu”)

flatten <- mx.symbol.Flatten(data = relu2)
fc1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 120)
fc21 <- mx.symbol.FullyConnected(data = fc1, num_hidden = 10)
fc22 <- mx.symbol.FullyConnected(data = fc1, num_hidden = 10)
fc23 <- mx.symbol.FullyConnected(data = fc1, num_hidden = 10)
fc24 <- mx.symbol.FullyConnected(data = fc1, num_hidden = 10)
fc2 <- mx.symbol.concat(c(fc21, fc22, fc23, fc24), dim = 0, num.args = 4)
label <- mx.symbol.transpose(data = label)
label <- mx.symbol.Reshape(data = label, target_shape = c(0))
captcha_net <- mx.symbol.SoftmaxOutput(data = fc2, label = label, name = “softmax”)

mx.metric.acc2 <- mx.metric.custom(“accuracy”, function(label, pred) {
ypred <- max.col(t(pred)) - 1
ypred <- matrix(ypred, nrow = nrow(label), ncol = ncol(label), byrow = TRUE)
return(sum(colSums(label == ypred) == 4) / ncol(label))
})

data.shape <- c(80, 30, 3)

batch_size <- 40

train <- mx.io.ImageRecordIter(
path.imgrec = “C:/Users/kravchik/Downloads/captcha_example/captcha_train.rec”,
path.imglist = “C:/Users/kravchik/Downloads/captcha_example/captcha_train.lst”,
batch.size = batch_size,
label.width = 4,
data.shape = data.shape,
mean.img = “mean.bin”
)
train$reset()

train$iter.next()

train$value()$label

View(train$value()$data)

val <- mx.io.ImageRecordIter(
path.imgrec = “C:/Users/kravchik/Downloads/captcha_example/captcha_test.rec”,
path.imglist = “C:/Users/kravchik/Downloads/captcha_example/captcha_test.lst”,
batch.size = batch_size,
label.width = 4,
data.shape = data.shape,
mean.img = “mean.bin”
)

mx.set.seed(42)

val$reset()

val$iter.next()

val$value()$label

val$value()$data

model <- mx.model.FeedForward.create(
X = train,
eval.data = val,
ctx = mx.cpu(),
symbol = captcha_net,
eval.metric = mx.metric.acc2,
num.round = 10,
learning.rate = 0.0001,
momentum = 0.9,
wd = 0.00001,
batch.end.callback = mx.callback.log.train.metric(50),
initializer = mx.init.Xavier(factor_type = “in”, magnitude = 2.34),
optimizer = “sgd”,
clip_gradient = 10
)

and i get the error

Error in output.shape[[output.names]] <- dim((X$value())$label) :
attempt to select less than one element in OneIndex

what’s wrong? how to perform analysis ?

You have a mistake in the first 2 lines. Instead of

data <- mx.symbol.Variable(‘C:/Users/kravchik/Downloads/captcha_example/captcha_train.rec’)
label <- mx.symbol.Variable(‘C:/Users/kravchik/Downloads/captcha_example/captcha_train.lst’)

it should be

data <- mx.symbol.Variable('data')
label <- mx.symbol.Variable('label')

The error you got is probably because MXNet could not find the symbols with name ‘data’ and ‘label’.

when i do so, i get next errors
[12:59:41] c:\jenkins\workspace\mxnet\mxnet\src\operator\tensor./matrix_op-inl.h:166: Using target_shape will be deprecated.
Start training with 1 devices
[12:59:41] c:\jenkins\workspace\mxnet\mxnet\src\operator\tensor./matrix_op-inl.h:166: Using target_shape will be deprecated.
[12:59:41] c:\jenkins\workspace\mxnet\mxnet\src\operator\tensor./matrix_op-inl.h:166: Using target_shape will be deprecated.
Show Traceback

Rerun with Debug
** Error in t.default(pred) : argument is not a matrix**

What does it means

I had a look on the example and there seems to be a bug. You need to change the following line:
ypred <- max.col(t(pred)) - 1
to:
ypred <- max.col(t(data.matrix(pred))) - 1

i did it, but another
error
Error in mx.nd.internal.dispatch.Ops(.Generic, e1, e2) :
Expecting a single value: [extent=160].

What’s parameter i have to change

You also need to change label to data.matrix(label). The following should work:

mx.metric.acc2 <- mx.metric.custom("accuracy", function(label, pred) {

     ypred <- max.col(t(data.matrix(pred))) - 1
     ypred <- matrix(ypred, nrow = nrow(label), ncol = ncol(label), byrow = TRUE)
     return(sum(colSums(data.matrix(label) == ypred) == 4)) / ncol(label)
})
1 Like

Yes, it is work. you very helped me, but may i ask you TWO questions
the first, after working, i get the accuracy
Batch [50] Train-accuracy=0
Batch [100] Train-accuracy=0
Batch [150] Train-accuracy=0
Batch [200] Train-accuracy=0
Batch [250] Train-accuracy=0
[1] Train-accuracy=0
[1] Validation-accuracy=0
Batch [50] Train-accuracy=0
Batch [100] Train-accuracy=0
Batch [150] Train-accuracy=0
Batch [200] Train-accuracy=0
Batch [250] Train-accuracy=0.004
[2] Train-accuracy=0.004
[2] Validation-accuracy=0.08
Batch [50] Train-accuracy=0.14
Batch [100] Train-accuracy=0.31
Batch [150] Train-accuracy=0.62
Batch [200] Train-accuracy=0.865
Batch [250] Train-accuracy=1.272
[3] Train-accuracy=1.272
[3] Validation-accuracy=4.12
Batch [50] Train-accuracy=3.62
Batch [100] Train-accuracy=4.46
Batch [150] Train-accuracy=4.90666666666667
Batch [200] Train-accuracy=5.11
Batch [250] Train-accuracy=5.5
[4] Train-accuracy=5.5
[4] Validation-accuracy=6.56
Batch [50] Train-accuracy=7.74
Batch [100] Train-accuracy=7.78
Batch [150] Train-accuracy=7.88666666666667
Batch [200] Train-accuracy=8
Batch [250] Train-accuracy=8.2
[5] Train-accuracy=8.2
[5] Validation-accuracy=7.84
Batch [50] Train-accuracy=8.12
Batch [100] Train-accuracy=8.73
Batch [150] Train-accuracy=9.08
Batch [200] Train-accuracy=9.315
Batch [250] Train-accuracy=9.608
[6] Train-accuracy=9.608
[6] Validation-accuracy=6.8
Batch [50] Train-accuracy=10.54
Batch [100] Train-accuracy=10.57
Batch [150] Train-accuracy=10.66
Batch [200] Train-accuracy=10.775
Batch [250] Train-accuracy=10.916
[7] Train-accuracy=10.916
[7] Validation-accuracy=6.08
Batch [50] Train-accuracy=11.46
Batch [100] Train-accuracy=11.39
Batch [150] Train-accuracy=10.94
Batch [200] Train-accuracy=11.295
Batch [250] Train-accuracy=11.66
[8] Train-accuracy=11.66
[8] Validation-accuracy=5.4
Batch [50] Train-accuracy=11.56
Batch [100] Train-accuracy=11.07
Batch [150] Train-accuracy=11.0266666666667
Batch [200] Train-accuracy=11.465
Batch [250] Train-accuracy=11.768
[9] Train-accuracy=11.768
[9] Validation-accuracy=6.76
Batch [50] Train-accuracy=11.48
Batch [100] Train-accuracy=11.81
Batch [150] Train-accuracy=11.9866666666667
Batch [200] Train-accuracy=12.275
Batch [250] Train-accuracy=12.548
[10] Train-accuracy=12.548
[10] Validation-accuracy=7.28

I think it is bad accuracy or i am wrong?

The second querstion
why here so small matrix

train$reset()

train$iter.next()
[1] TRUE

train$value()$label
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] 6 6 8 9 4 8 2 0 3 0 8 3 6 8 7 2 6 8 2 5
[2,] 3 0 9 7 1 5 8 3 3 8 3 7 6 5 6 0 1 7 7 7
[3,] 9 8 0 0 5 0 8 7 8 0 3 6 4 2 3 7 4 9 4 1
[4,] 4 9 2 4 7 9 6 0 7 7 3 6 3 8 1 2 6 7 7 9
[,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39]
[1,] 8 0 0 9 6 1 9 1 9 1 9 7 1 4 2 8 3 5 1
[2,] 4 5 8 1 4 7 7 7 0 7 6 8 0 4 2 7 9 6 8
[3,] 9 9 6 3 4 4 0 7 6 4 4 0 6 6 9 6 8 0 9
[4,] 1 3 6 9 6 2 7 5 6 7 4 9 1 2 4 1 4 5 7
[,40]
[1,] 8
[2,] 2
[3,] 1
[4,] 9

is it normal?