Related
I am trying to create a matrix m x n x 3 base on some joints with cardinal cartesian x,y,z. First, I arrange the indices of the joints in the first image skeleton image into a 2D grid 2D grid
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
What I cannot do is to add the cartesian coordinate (x,y,z) of those indices along the third dimension of my matrice A in order to get m x n x 3. The x,y,z of each joint will be similar to the Chanel R, G, B of a color image with R=x, g=y, b=z
The the example code below produces a matrix mxnx3 from matrix A, where A illustrate indexes of elements in a skeleton, resulting in a A_result like:
# Create mxnx3 matrix from matrix A, where A include index name of skeleton element in each mxn location
import numpy as np
from matplotlib import pyplot as plt
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
#Let's rearrange slightly...
A_sub=A[0][:][:]
#Take the size of the matrix...
(m_max,n_max)=A_sub.shape
#Let's create basis for result matrix...note the format where mxn is first and after them the 3 dimensions of "colors"
A_result=np.zeros((A.shape[1],A.shape[2],3))
#demonstration function for the xyz coordinates...
def tell_me_xyz_coordinate_of_element(element_number):
#...perhaps in real application there is some measurement or the like functionality...
#...which investigate the element_number and then gives back its location...
#...but here to exemplify we simple return random int values back...
x=np.random.randint(0,255)
y=np.random.randint(0,255)
z=np.random.randint(0,255)
return x,y,z
#let's create the result matrix...
for m in range(m_max):
for n in range(n_max):
#Define x,y,z -values of the element in this m,n coordinate,
#where the value in m,n coordinate tells the number of corresponding element...
element_number=A_sub[m][n]
(x,y,z)=tell_me_xyz_coordinate_of_element(element_number)
#Set the results in the matrix...
A_result[m][n][0]=x
A_result[m][n][1]=y
A_result[m][n][2]=z
#Let's investigate the resulting nympy-matrix as a image...
#...remember to change the data format to uint8 to be able to investigate as a image
plt.imshow(np.uint8(A_result),interpolation='nearest')
title_text=''.join(["Result matrix mxnx3, \nwhere m=",str(m_max+1), " n=",str(n_max+1),",\n" "with color codes 0-255 in each m and n"])
plt.title(title_text)
plt.show()
This example may help you to solve your problem, but if not, please describe your question again in a more clear format.
#Create mxnx3 arrays and tensor form specific A matrix and then let's show how to create mxnx3 np-array and the same also in tensor format
import numpy as np
import tensorflow as tf
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
#Let's convert A to a tensor:
A_in_tensorformat=tf.Variable(A)
#Let's make a numpy of size mxnx3:
m=123
n=45
B=np.ones((m,n,3))
B_in_tensorformat=tf.Variable(B)
I have been training a model for a study on one-shot learning.
It has 19280 examples in the training dataset (basically the popular Omniglot dataset), and a 300-length vector for each data sample.
The model consists of the following architecture-
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 32, 50, 50] 1,600
BatchNorm2d-2 [-1, 32, 50, 50] 64
ReLU-3 [-1, 32, 50, 50] 0
Conv2d-4 [-1, 32, 50, 50] 9,248
BatchNorm2d-5 [-1, 32, 50, 50] 64
ReLU-6 [-1, 32, 50, 50] 0
Conv2d-7 [-1, 32, 50, 50] 9,248
BatchNorm2d-8 [-1, 32, 50, 50] 64
ReLU-9 [-1, 32, 50, 50] 0
Conv2d-10 [-1, 64, 24, 24] 18,496
BatchNorm2d-11 [-1, 64, 24, 24] 128
ReLU-12 [-1, 64, 24, 24] 0
Conv2d-13 [-1, 64, 24, 24] 36,928
BatchNorm2d-14 [-1, 64, 24, 24] 128
ReLU-15 [-1, 64, 24, 24] 0
Conv2d-16 [-1, 256, 11, 11] 147,712
BatchNorm2d-17 [-1, 256, 11, 11] 512
ReLU-18 [-1, 256, 11, 11] 0
Conv2d-19 [-1, 512, 5, 5] 1,180,160
BatchNorm2d-20 [-1, 512, 5, 5] 1,024
ReLU-21 [-1, 512, 5, 5] 0
Conv2d-22 [-1, 1024, 2, 2] 4,719,616
BatchNorm2d-23 [-1, 1024, 2, 2] 2,048
ReLU-24 [-1, 1024, 2, 2] 0
Linear-25 [-1, 300] 1,229,100
================================================================
Total params: 7,356,140
Trainable params: 7,356,140
Non-trainable params: 0
----------------------------------------------------------------
Basically the input is a 105x105 image, single channel (grayscale). I have applied sigmoid on the output.
While I train the model, I use simple Mean-squared error. I use the Adam optimizer with the learning rate set to $10^{-5}$ , to help tuning.
The model gets struck at the same constant loss after 2 or 3 epochs.
On further investigating, I found out that the model generalizes and makes outputs a zero vector in each case. I am assuming it is stuck on the local minima, but how do I go about training my model successfully?
Also, the model architecture has been chosen randomly (not literally, but no particular logic behind the dimensionality retained at the end of each layer), so please advise me if you think there is any present irregularity that would better training after rectification, in terms of layers.
I would love to hear some tips :) .
I am trying to write a function to create a CNN model. I get the following error whenever I run the script:
lua:15: unexpected symbol near '['
require('torch')
require('nn')
function CeateNvidiaModel()
--The Nvidia model
--Input dimensions
local n_channels = 3
local height = 66
local width = 200
local nvidia_model = nn.Sequential();
--nvida_model:add(nn.Normalize()
--Convolutional Layers
nvidia_model:add(nn.SpatialConvolution(n_channels, 24, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(24, 36, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(36, 48, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(48, 64, 3, 3))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(64, 64, 3, 3))
nvidia_model:add(nn.ELU(true))
-- Flatten Layer
nvidia_model:add(nn.Reshape(1164))
-- FC Layers
nvida_model:add(nn.Linear(1164, 100))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(100, 50))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(50, 10))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(10, 1))
return nvida_model
end
I assume you are confusing [] and {}. In many other languages, you write array literals as [1, 2, 3], but in Lua [ and ] are only used for indexing; to declare an "array literal", you write {1, 2, 3} (because arrays in Lua are just tables).
The error message is a bit misleading; it says unexpected symbol near '[', but in reality the [ is the unexpected symbol.
R beginner here.
I have created a dataframe (called combined_ts2) based on a dataset I was given.See this link for the dataframe.
Based on the dataframe a made this ts object (find code at bottom of post).
I am supposed to make a plot based on the TS object. I'm thinking this can be achieved with a ts.plot function, but I can't figure out how to use this function.
The result I want is to get a bar graph for capacity and a line graph for fixtures. Does anyone know how to achieve this, and if I can actually achieve this from a ts object?
dput for dataframe
structure(list(week = c(26, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48), capacity = c(45000L,
39000L, 495500L, 855300L, 1318300L, 1301885L, 8211550L, 18515400L,
32282950L, 31568400L, 35410200L, 29867500L, 34809050L, 36420050L,
33960520L, 33987550L, 33465500L, 24599000L, 11597000L, 4553000L,
1375000L, 545000L), fixtures = c(2L, 4L, 12L, 13L, 18L, 29L,
161L, 338L, 393L, 405L, 439L, 386L, 442L, 406L, 413L, 421L, 326L,
180L, 84L, 23L, 6L, 3L)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -22L))
Time series code
weekly_timeseries <- ts(combined_ts2, start=c(2019,26), frequency = 52)
I'm trying to train an SVC classifier for image data. Yet, when I run this code:
classifier = svm.SVC(gamma=0.001)
classifier.fit(train_set, train_set_labels)
I get this error:
ValueError: setting an array element with a sequence.
I produced the images into an array with Matplotlib: plt.imread(image).
The error seems like it's not in an array, yet when I check the types of the data and the labels they're both lists (I manually add to a list for the labels data):
print(type(train_set))
print(type(train_set_labels))
<class 'list'>
<class 'list'>
If I do a plt.imshow(items[0]) then the image shows correctly in the output.
I also called train_test_split from scikit-learn:
train_set, test_set = train_test_split(items, test_size=0.2, random_state=42)
Example input:
train_set[0]
array([[[212, 134, 34],
[221, 140, 48],
[240, 154, 71],
...,
[245, 182, 51],
[235, 175, 43],
[242, 182, 50]],
[[230, 152, 51],
[222, 139, 47],
[236, 147, 65],
...,
[246, 184, 49],
[238, 179, 43],
[245, 186, 50]],
[[229, 150, 47],
[205, 122, 28],
[220, 129, 46],
...,
[232, 171, 28],
[237, 179, 35],
[244, 188, 43]],
...,
[[115, 112, 103],
[112, 109, 102],
[ 80, 77, 72],
...,
[ 34, 25, 28],
[ 55, 46, 49],
[ 80, 71, 74]],
[[ 59, 56, 47],
[ 66, 63, 56],
[ 48, 45, 40],
...,
[ 32, 23, 26],
[ 56, 47, 50],
[ 82, 73, 76]],
[[ 29, 26, 17],
[ 41, 38, 31],
[ 32, 29, 24],
...,
[ 56, 47, 50],
[ 59, 50, 53],
[ 84, 75, 78]]], dtype=uint8)
Example label:
train_set_labels[0]
'Picasso'
I'm not sure what step I'm missing to get the data in the form that the classifier needs in order to train it. Can anyone see what may be needed?
The error message you are receiving:
ValueError: setting an array element with a sequence,
normally results when you are trying to put a list somewhere that a single value is required. This would suggest to me that your train_set is made up of a list of multidimensional elements, although you do state that your inputs are lists. Would you be able to post an example of your inputs and labels?
UPDATE
Yes, it's as I thought. The first element of your training data, train_set[0], corresponds to a long list (I can't tell how long), each element of which consists of a list of 3 elements. You are therefore calling the classifier on a list of lists of lists, when the classifier requires a list of lists (m rows corresponding to the number of training examples with each row made up of a list of n features). What else is in your train_set array? Is the full data set in train_set[0]? If so, you would need to create a new array with each element corresponding to each of the subelements of train_set[0], and then I believe your code should run, although I am not too familiar with that classifier. Alternatively you could try running the classifier with train_set[0].
UPDATE 2
I don't have experience with scikit-learn.svc so I wouldn't be able to tell you what the best way of preprocessing the data in order for it to be acceptable to the algorithm, but one method would be to do as I said previously and for each element of train_set, which is composed of lists of lists, would be to recurse through and place all the elements of sublist into the list above. For example
new_train_set = []
for i in range(len(train_set)):
for j in range(len(train_set[i]):
new_train_set.append([train_set[i,j])
I would then train with new_train_set and the training labels.